Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/idealKit.cpp
32285 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "opto/addnode.hpp"26#include "opto/callnode.hpp"27#include "opto/cfgnode.hpp"28#include "opto/idealKit.hpp"29#include "opto/runtime.hpp"3031// Static initialization3233// This declares the position where vars are kept in the cvstate34// For some degree of consistency we use the TypeFunc enum to35// soak up spots in the inputs even though we only use early Control36// and Memory slots. (So far.)37const uint IdealKit::first_var = TypeFunc::Parms + 1;3839//----------------------------IdealKit-----------------------------------------40IdealKit::IdealKit(GraphKit* gkit, bool delay_all_transforms, bool has_declarations) :41_gvn(gkit->gvn()), C(gkit->C) {42_initial_ctrl = gkit->control();43_initial_memory = gkit->merged_memory();44_initial_i_o = gkit->i_o();45_delay_all_transforms = delay_all_transforms;46_var_ct = 0;47_cvstate = NULL;48// We can go memory state free or else we need the entire memory state49assert(_initial_memory == NULL || _initial_memory->Opcode() == Op_MergeMem, "memory must be pre-split");50assert(!_gvn.is_IterGVN(), "IdealKit can't be used during Optimize phase");51int init_size = 5;52_pending_cvstates = new (C->node_arena()) GrowableArray<Node*>(C->node_arena(), init_size, 0, 0);53DEBUG_ONLY(_state = new (C->node_arena()) GrowableArray<int>(C->node_arena(), init_size, 0, 0));54if (!has_declarations) {55declarations_done();56}57}5859//----------------------------sync_kit-----------------------------------------60void IdealKit::sync_kit(GraphKit* gkit) {61set_all_memory(gkit->merged_memory());62set_i_o(gkit->i_o());63set_ctrl(gkit->control());64}6566//-------------------------------if_then-------------------------------------67// Create: if(left relop right)68// / \69// iffalse iftrue70// Push the iffalse cvstate onto the stack. The iftrue becomes the current cvstate.71void IdealKit::if_then(Node* left, BoolTest::mask relop,72Node* right, float prob, float cnt, bool push_new_state) {73assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new If");74Node* bol;75if (left->bottom_type()->isa_ptr() == NULL) {76if (left->bottom_type()->isa_int() != NULL) {77bol = Bool(CmpI(left, right), relop);78} else {79assert(left->bottom_type()->isa_long() != NULL, "what else?");80bol = Bool(CmpL(left, right), relop);81}8283} else {84bol = Bool(CmpP(left, right), relop);85}86// Delay gvn.tranform on if-nodes until construction is finished87// to prevent a constant bool input from discarding a control output.88IfNode* iff = delay_transform(new (C) IfNode(ctrl(), bol, prob, cnt))->as_If();89Node* then = IfTrue(iff);90Node* elsen = IfFalse(iff);91Node* else_cvstate = copy_cvstate();92else_cvstate->set_req(TypeFunc::Control, elsen);93_pending_cvstates->push(else_cvstate);94DEBUG_ONLY(if (push_new_state) _state->push(IfThenS));95set_ctrl(then);96}9798//-------------------------------else_-------------------------------------99// Pop the else cvstate off the stack, and push the (current) then cvstate.100// The else cvstate becomes the current cvstate.101void IdealKit::else_() {102assert(state() == IfThenS, "bad state for new Else");103Node* else_cvstate = _pending_cvstates->pop();104DEBUG_ONLY(_state->pop());105// save current (then) cvstate for later use at endif106_pending_cvstates->push(_cvstate);107DEBUG_ONLY(_state->push(ElseS));108_cvstate = else_cvstate;109}110111//-------------------------------end_if-------------------------------------112// Merge the "then" and "else" cvstates.113//114// The if_then() pushed a copy of the current state for later use115// as the initial state for a future "else" clause. The116// current state then became the initial state for the117// then clause. If an "else" clause was encountered, it will118// pop the top state and use it for it's initial state.119// It will also push the current state (the state at the end of120// the "then" clause) for latter use at the end_if.121//122// At the endif, the states are:123// 1) else exists a) current state is end of "else" clause124// b) top stack state is end of "then" clause125//126// 2) no else: a) current state is end of "then" clause127// b) top stack state is from the "if_then" which128// would have been the initial state of the else.129//130// Merging the states is accomplished by:131// 1) make a label for the merge132// 2) terminate the current state with a goto to the label133// 3) pop the top state from the stack and make it the134// current state135// 4) bind the label at the current state. Binding a label136// terminates the current state with a goto to the137// label and makes the label's state the current state.138//139void IdealKit::end_if() {140assert(state() & (IfThenS|ElseS), "bad state for new Endif");141Node* lab = make_label(1);142143// Node* join_state = _pending_cvstates->pop();144/* merging, join */145goto_(lab);146_cvstate = _pending_cvstates->pop();147148bind(lab);149DEBUG_ONLY(_state->pop());150}151152//-------------------------------loop-------------------------------------153// Create the loop head portion (*) of:154// * iv = init155// * top: (region node)156// * if (iv relop limit) {157// loop body158// i = i + 1159// goto top160// * } else // exits loop161//162// Pushes the loop top cvstate first, then the else (loop exit) cvstate163// onto the stack.164void IdealKit::loop(GraphKit* gkit, int nargs, IdealVariable& iv, Node* init, BoolTest::mask relop, Node* limit, float prob, float cnt) {165assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new loop");166if (UseLoopPredicate) {167// Sync IdealKit and graphKit.168gkit->sync_kit(*this);169// Add loop predicate.170gkit->add_predicate(nargs);171// Update IdealKit memory.172sync_kit(gkit);173}174set(iv, init);175Node* head = make_label(1);176bind(head);177_pending_cvstates->push(head); // push for use at end_loop178_cvstate = copy_cvstate();179if_then(value(iv), relop, limit, prob, cnt, false /* no new state */);180DEBUG_ONLY(_state->push(LoopS));181assert(ctrl()->is_IfTrue(), "true branch stays in loop");182assert(_pending_cvstates->top()->in(TypeFunc::Control)->is_IfFalse(), "false branch exits loop");183}184185//-------------------------------end_loop-------------------------------------186// Creates the goto top label.187// Expects the else (loop exit) cvstate to be on top of the188// stack, and the loop top cvstate to be 2nd.189void IdealKit::end_loop() {190assert((state() == LoopS), "bad state for new end_loop");191Node* exit = _pending_cvstates->pop();192Node* head = _pending_cvstates->pop();193goto_(head);194clear(head);195DEBUG_ONLY(_state->pop());196_cvstate = exit;197}198199//-------------------------------make_label-------------------------------------200// Creates a label. The number of goto's201// must be specified (which should be 1 less than202// the number of precedessors.)203Node* IdealKit::make_label(int goto_ct) {204assert(_cvstate != NULL, "must declare variables before labels");205Node* lab = new_cvstate();206int sz = 1 + goto_ct + 1 /* fall thru */;207Node* reg = delay_transform(new (C) RegionNode(sz));208lab->init_req(TypeFunc::Control, reg);209return lab;210}211212//-------------------------------bind-------------------------------------213// Bind a label at the current cvstate by simulating214// a goto to the label.215void IdealKit::bind(Node* lab) {216goto_(lab, true /* bind */);217_cvstate = lab;218}219220//-------------------------------goto_-------------------------------------221// Make the current cvstate a predecessor of the label,222// creating phi's to merge values. If bind is true and223// this is not the last control edge, then ensure that224// all live values have phis created. Used to create phis225// at loop-top regions.226void IdealKit::goto_(Node* lab, bool bind) {227Node* reg = lab->in(TypeFunc::Control);228// find next empty slot in region229uint slot = 1;230while (slot < reg->req() && reg->in(slot) != NULL) slot++;231assert(slot < reg->req(), "too many gotos");232// If this is last predecessor, then don't force phi creation233if (slot == reg->req() - 1) bind = false;234reg->init_req(slot, ctrl());235assert(first_var + _var_ct == _cvstate->req(), "bad _cvstate size");236for (uint i = first_var; i < _cvstate->req(); i++) {237238// l is the value of var reaching the label. Could be a single value239// reaching the label, or a phi that merges multiples values reaching240// the label. The latter is true if the label's input: in(..) is241// a phi whose control input is the region node for the label.242243Node* l = lab->in(i);244// Get the current value of the var245Node* m = _cvstate->in(i);246// If the var went unused no need for a phi247if (m == NULL) {248continue;249} else if (l == NULL || m == l) {250// Only one unique value "m" is known to reach this label so a phi251// is not yet necessary unless:252// the label is being bound and all predecessors have not been seen,253// in which case "bind" will be true.254if (bind) {255m = promote_to_phi(m, reg);256}257// Record the phi/value used for this var in the label's cvstate258lab->set_req(i, m);259} else {260// More than one value for the variable reaches this label so261// a create a phi if one does not already exist.262if (!was_promoted_to_phi(l, reg)) {263l = promote_to_phi(l, reg);264lab->set_req(i, l);265}266// Record in the phi, the var's value from the current state267l->set_req(slot, m);268}269}270do_memory_merge(_cvstate, lab);271stop();272}273274//-----------------------------promote_to_phi-----------------------------------275Node* IdealKit::promote_to_phi(Node* n, Node* reg) {276assert(!was_promoted_to_phi(n, reg), "n already promoted to phi on this region");277// Get a conservative type for the phi278const BasicType bt = n->bottom_type()->basic_type();279const Type* ct = Type::get_const_basic_type(bt);280return delay_transform(PhiNode::make(reg, n, ct));281}282283//-----------------------------declarations_done-------------------------------284void IdealKit::declarations_done() {285_cvstate = new_cvstate(); // initialize current cvstate286set_ctrl(_initial_ctrl); // initialize control in current cvstate287set_all_memory(_initial_memory);// initialize memory in current cvstate288set_i_o(_initial_i_o); // initialize i_o in current cvstate289DEBUG_ONLY(_state->push(BlockS));290}291292//-----------------------------transform-----------------------------------293Node* IdealKit::transform(Node* n) {294if (_delay_all_transforms) {295return delay_transform(n);296} else {297n = gvn().transform(n);298C->record_for_igvn(n);299return n;300}301}302303//-----------------------------delay_transform-----------------------------------304Node* IdealKit::delay_transform(Node* n) {305// Delay transform until IterativeGVN306gvn().set_type(n, n->bottom_type());307C->record_for_igvn(n);308return n;309}310311//-----------------------------new_cvstate-----------------------------------312Node* IdealKit::new_cvstate() {313uint sz = _var_ct + first_var;314return new (C) Node(sz);315}316317//-----------------------------copy_cvstate-----------------------------------318Node* IdealKit::copy_cvstate() {319Node* ns = new_cvstate();320for (uint i = 0; i < ns->req(); i++) ns->init_req(i, _cvstate->in(i));321// We must clone memory since it will be updated as we do stores.322ns->set_req(TypeFunc::Memory, MergeMemNode::make(C, ns->in(TypeFunc::Memory)));323return ns;324}325326//-----------------------------clear-----------------------------------327void IdealKit::clear(Node* m) {328for (uint i = 0; i < m->req(); i++) m->set_req(i, NULL);329}330331//-----------------------------IdealVariable----------------------------332IdealVariable::IdealVariable(IdealKit &k) {333k.declare(this);334}335336Node* IdealKit::memory(uint alias_idx) {337MergeMemNode* mem = merged_memory();338Node* p = mem->memory_at(alias_idx);339_gvn.set_type(p, Type::MEMORY); // must be mapped340return p;341}342343void IdealKit::set_memory(Node* mem, uint alias_idx) {344merged_memory()->set_memory_at(alias_idx, mem);345}346347//----------------------------- make_load ----------------------------348Node* IdealKit::load(Node* ctl,349Node* adr,350const Type* t,351BasicType bt,352int adr_idx,353bool require_atomic_access) {354355assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );356const TypePtr* adr_type = NULL; // debug-mode-only argument357debug_only(adr_type = C->get_adr_type(adr_idx));358Node* mem = memory(adr_idx);359Node* ld;360if (require_atomic_access && bt == T_LONG) {361ld = LoadLNode::make_atomic(C, ctl, mem, adr, adr_type, t, MemNode::unordered);362} else {363ld = LoadNode::make(_gvn, ctl, mem, adr, adr_type, t, bt, MemNode::unordered);364}365return transform(ld);366}367368Node* IdealKit::store(Node* ctl, Node* adr, Node *val, BasicType bt,369int adr_idx,370MemNode::MemOrd mo, bool require_atomic_access,371bool mismatched) {372assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory");373const TypePtr* adr_type = NULL;374debug_only(adr_type = C->get_adr_type(adr_idx));375Node *mem = memory(adr_idx);376Node* st;377if (require_atomic_access && bt == T_LONG) {378st = StoreLNode::make_atomic(C, ctl, mem, adr, adr_type, val, mo);379} else {380st = StoreNode::make(_gvn, ctl, mem, adr, adr_type, val, bt, mo);381}382if (mismatched) {383st->as_Store()->set_mismatched_access();384}385st = transform(st);386set_memory(st, adr_idx);387388return st;389}390391// Card mark store. Must be ordered so that it will come after the store of392// the oop.393Node* IdealKit::storeCM(Node* ctl, Node* adr, Node *val, Node* oop_store, int oop_adr_idx,394BasicType bt,395int adr_idx) {396assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );397const TypePtr* adr_type = NULL;398debug_only(adr_type = C->get_adr_type(adr_idx));399Node *mem = memory(adr_idx);400401// Add required edge to oop_store, optimizer does not support precedence edges.402// Convert required edge to precedence edge before allocation.403Node* st = new (C) StoreCMNode(ctl, mem, adr, adr_type, val, oop_store, oop_adr_idx);404405st = transform(st);406set_memory(st, adr_idx);407408return st;409}410411//---------------------------- do_memory_merge --------------------------------412// The memory from one merging cvstate needs to be merged with the memory for another413// join cvstate. If the join cvstate doesn't have a merged memory yet then we414// can just copy the state from the merging cvstate415416// Merge one slow path into the rest of memory.417void IdealKit::do_memory_merge(Node* merging, Node* join) {418419// Get the region for the join state420Node* join_region = join->in(TypeFunc::Control);421assert(join_region != NULL, "join region must exist");422if (join->in(TypeFunc::I_O) == NULL ) {423join->set_req(TypeFunc::I_O, merging->in(TypeFunc::I_O));424}425if (join->in(TypeFunc::Memory) == NULL ) {426join->set_req(TypeFunc::Memory, merging->in(TypeFunc::Memory));427return;428}429430// The control flow for merging must have already been attached to the join region431// we need its index for the phis.432uint slot;433for (slot = 1; slot < join_region->req() ; slot ++ ) {434if (join_region->in(slot) == merging->in(TypeFunc::Control)) break;435}436assert(slot != join_region->req(), "edge must already exist");437438MergeMemNode* join_m = join->in(TypeFunc::Memory)->as_MergeMem();439MergeMemNode* merging_m = merging->in(TypeFunc::Memory)->as_MergeMem();440441// join_m should be an ancestor mergemem of merging442// Slow path memory comes from the current map (which is from a slow call)443// Fast path/null path memory comes from the call's input444445// Merge the other fast-memory inputs with the new slow-default memory.446// for (MergeMemStream mms(merged_memory(), fast_mem->as_MergeMem()); mms.next_non_empty2(); ) {447for (MergeMemStream mms(join_m, merging_m); mms.next_non_empty2(); ) {448Node* join_slice = mms.force_memory();449Node* merging_slice = mms.memory2();450if (join_slice != merging_slice) {451PhiNode* phi;452// bool new_phi = false;453// Is the phi for this slice one that we created for this join region or simply454// one we copied? If it is ours then add455if (join_slice->is_Phi() && join_slice->as_Phi()->region() == join_region) {456phi = join_slice->as_Phi();457} else {458// create the phi with join_slice filling supplying memory for all of the459// control edges to the join region460phi = PhiNode::make(join_region, join_slice, Type::MEMORY, mms.adr_type(C));461phi = (PhiNode*) delay_transform(phi);462// gvn().set_type(phi, Type::MEMORY);463// new_phi = true;464}465// Now update the phi with the slice for the merging slice466phi->set_req(slot, merging_slice/* slow_path, slow_slice */);467// this updates join_m with the phi468mms.set_memory(phi);469}470}471472Node* join_io = join->in(TypeFunc::I_O);473Node* merging_io = merging->in(TypeFunc::I_O);474if (join_io != merging_io) {475PhiNode* phi;476if (join_io->is_Phi() && join_io->as_Phi()->region() == join_region) {477phi = join_io->as_Phi();478} else {479phi = PhiNode::make(join_region, join_io, Type::ABIO);480phi = (PhiNode*) delay_transform(phi);481join->set_req(TypeFunc::I_O, phi);482}483phi->set_req(slot, merging_io);484}485}486487488//----------------------------- make_call ----------------------------489// Trivial runtime call490void IdealKit::make_leaf_call(const TypeFunc *slow_call_type,491address slow_call,492const char *leaf_name,493Node* parm0,494Node* parm1,495Node* parm2,496Node* parm3) {497498// We only handle taking in RawMem and modifying RawMem499const TypePtr* adr_type = TypeRawPtr::BOTTOM;500uint adr_idx = C->get_alias_index(adr_type);501502// Slow-path leaf call503CallNode *call = (CallNode*)new (C) CallLeafNode( slow_call_type, slow_call, leaf_name, adr_type);504505// Set fixed predefined input arguments506call->init_req( TypeFunc::Control, ctrl() );507call->init_req( TypeFunc::I_O , top() ) ; // does no i/o508// Narrow memory as only memory input509call->init_req( TypeFunc::Memory , memory(adr_idx));510call->init_req( TypeFunc::FramePtr, top() /* frameptr() */ );511call->init_req( TypeFunc::ReturnAdr, top() );512513if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0);514if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1);515if (parm2 != NULL) call->init_req(TypeFunc::Parms+2, parm2);516if (parm3 != NULL) call->init_req(TypeFunc::Parms+3, parm3);517518// Node *c = _gvn.transform(call);519call = (CallNode *) _gvn.transform(call);520Node *c = call; // dbx gets confused with call call->dump()521522// Slow leaf call has no side-effects, sets few values523524set_ctrl(transform( new (C) ProjNode(call,TypeFunc::Control) ));525526// Make memory for the call527Node* mem = _gvn.transform( new (C) ProjNode(call, TypeFunc::Memory) );528529// Set the RawPtr memory state only.530set_memory(mem, adr_idx);531532assert(C->alias_type(call->adr_type()) == C->alias_type(adr_type),533"call node must be constructed correctly");534}535536537void IdealKit::make_leaf_call_no_fp(const TypeFunc *slow_call_type,538address slow_call,539const char *leaf_name,540const TypePtr* adr_type,541Node* parm0,542Node* parm1,543Node* parm2,544Node* parm3) {545546// We only handle taking in RawMem and modifying RawMem547uint adr_idx = C->get_alias_index(adr_type);548549// Slow-path leaf call550CallNode *call = (CallNode*)new (C) CallLeafNoFPNode( slow_call_type, slow_call, leaf_name, adr_type);551552// Set fixed predefined input arguments553call->init_req( TypeFunc::Control, ctrl() );554call->init_req( TypeFunc::I_O , top() ) ; // does no i/o555// Narrow memory as only memory input556call->init_req( TypeFunc::Memory , memory(adr_idx));557call->init_req( TypeFunc::FramePtr, top() /* frameptr() */ );558call->init_req( TypeFunc::ReturnAdr, top() );559560if (parm0 != NULL) call->init_req(TypeFunc::Parms+0, parm0);561if (parm1 != NULL) call->init_req(TypeFunc::Parms+1, parm1);562if (parm2 != NULL) call->init_req(TypeFunc::Parms+2, parm2);563if (parm3 != NULL) call->init_req(TypeFunc::Parms+3, parm3);564565// Node *c = _gvn.transform(call);566call = (CallNode *) _gvn.transform(call);567Node *c = call; // dbx gets confused with call call->dump()568569// Slow leaf call has no side-effects, sets few values570571set_ctrl(transform( new (C) ProjNode(call,TypeFunc::Control) ));572573// Make memory for the call574Node* mem = _gvn.transform( new (C) ProjNode(call, TypeFunc::Memory) );575576// Set the RawPtr memory state only.577set_memory(mem, adr_idx);578579assert(C->alias_type(call->adr_type()) == C->alias_type(adr_type),580"call node must be constructed correctly");581}582583584