src/share/vm/opto/idealKit.cpp

Thu, 20 Mar 2008 15:11:44 -0700

author
kvn
date
Thu, 20 Mar 2008 15:11:44 -0700
changeset 509
2a9af0b9cb1c
parent 435
a61af66fc99e
child 548
ba764ed4b6f2
permissions
-rw-r--r--

6674600: (Escape Analysis) Optimize memory graph for instance's fields
Summary: EA gives opportunite to do more aggressive memory optimizations.
Reviewed-by: never, jrose

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_idealKit.cpp.incl"
    28 // Static initialization
    30 // This declares the position where vars are kept in the cvstate
    31 // For some degree of consistency we use the TypeFunc enum to
    32 // soak up spots in the inputs even though we only use early Control
    33 // and Memory slots. (So far.)
    34 const uint IdealKit::first_var = TypeFunc::Parms + 1;
    36 //----------------------------IdealKit-----------------------------------------
    37 IdealKit::IdealKit(PhaseGVN &gvn, Node* control, Node* mem, bool delay_all_transforms) :
    38   _gvn(gvn), C(gvn.C) {
    39   _initial_ctrl = control;
    40   _initial_memory = mem;
    41   _delay_all_transforms = delay_all_transforms;
    42   _var_ct = 0;
    43   _cvstate = NULL;
    44   // We can go memory state free or else we need the entire memory state
    45   assert(mem == NULL || mem->Opcode() == Op_MergeMem, "memory must be pre-split");
    46   int init_size = 5;
    47   _pending_cvstates = new (C->node_arena()) GrowableArray<Node*>(C->node_arena(), init_size, 0, 0);
    48   _delay_transform  = new (C->node_arena()) GrowableArray<Node*>(C->node_arena(), init_size, 0, 0);
    49   DEBUG_ONLY(_state = new (C->node_arena()) GrowableArray<int>(C->node_arena(), init_size, 0, 0));
    50 }
    52 //-------------------------------if_then-------------------------------------
    53 // Create:  if(left relop right)
    54 //          /  \
    55 //   iffalse    iftrue
    56 // Push the iffalse cvstate onto the stack. The iftrue becomes the current cvstate.
    57 void IdealKit::if_then(Node* left, BoolTest::mask relop,
    58                        Node* right, float prob, float cnt, bool push_new_state) {
    59   assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new If");
    60   Node* bol;
    61   if (left->bottom_type()->isa_ptr() == NULL) {
    62     if (left->bottom_type()->isa_int() != NULL) {
    63       bol = Bool(CmpI(left, right), relop);
    64     } else {
    65       assert(left->bottom_type()->isa_long() != NULL, "what else?");
    66       bol = Bool(CmpL(left, right), relop);
    67     }
    69   } else {
    70     bol = Bool(CmpP(left, right), relop);
    71   }
    72   // Delay gvn.tranform on if-nodes until construction is finished
    73   // to prevent a constant bool input from discarding a control output.
    74   IfNode* iff = delay_transform(new (C, 2) IfNode(ctrl(), bol, prob, cnt))->as_If();
    75   Node* then  = IfTrue(iff);
    76   Node* elsen = IfFalse(iff);
    77   Node* else_cvstate = copy_cvstate();
    78   else_cvstate->set_req(TypeFunc::Control, elsen);
    79   _pending_cvstates->push(else_cvstate);
    80   DEBUG_ONLY(if (push_new_state) _state->push(IfThenS));
    81   set_ctrl(then);
    82 }
    84 //-------------------------------else_-------------------------------------
    85 // Pop the else cvstate off the stack, and push the (current) then cvstate.
    86 // The else cvstate becomes the current cvstate.
    87 void IdealKit::else_() {
    88   assert(state() == IfThenS, "bad state for new Else");
    89   Node* else_cvstate = _pending_cvstates->pop();
    90   DEBUG_ONLY(_state->pop());
    91   // save current (then) cvstate for later use at endif
    92   _pending_cvstates->push(_cvstate);
    93   DEBUG_ONLY(_state->push(ElseS));
    94   _cvstate = else_cvstate;
    95 }
    97 //-------------------------------end_if-------------------------------------
    98 // Merge the "then" and "else" cvstates.
    99 //
   100 // The if_then() pushed the current state for later use
   101 // as the initial state for a future "else" clause.  The
   102 // current state then became the initial state for the
   103 // then clause.  If an "else" clause was encountered, it will
   104 // pop the top state and use it for it's initial state.
   105 // It will also push the current state (the state at the end of
   106 // the "then" clause) for latter use at the end_if.
   107 //
   108 // At the endif, the states are:
   109 // 1) else exists a) current state is end of "else" clause
   110 //                b) top stack state is end of "then" clause
   111 //
   112 // 2) no else:    a) current state is end of "then" clause
   113 //                b) top stack state is from the "if_then" which
   114 //                   would have been the initial state of the else.
   115 //
   116 // Merging the states is accomplished by:
   117 //   1) make a label for the merge
   118 //   2) terminate the current state with a goto to the label
   119 //   3) pop the top state from the stack and make it the
   120 //        current state
   121 //   4) bind the label at the current state.  Binding a label
   122 //        terminates the current state with a goto to the
   123 //        label and makes the label's state the current state.
   124 //
   125 void IdealKit::end_if() {
   126   assert(state() & (IfThenS|ElseS), "bad state for new Endif");
   127   Node* lab = make_label(1);
   129   // Node* join_state = _pending_cvstates->pop();
   130                   /* merging, join */
   131   goto_(lab);
   132   _cvstate = _pending_cvstates->pop();
   134   bind(lab);
   135   DEBUG_ONLY(_state->pop());
   136 }
   138 //-------------------------------loop-------------------------------------
   139 // Create the loop head portion (*) of:
   140 //  *     iv = init
   141 //  *  top: (region node)
   142 //  *     if (iv relop limit) {
   143 //           loop body
   144 //           i = i + 1
   145 //           goto top
   146 //  *     } else // exits loop
   147 //
   148 // Pushes the loop top cvstate first, then the else (loop exit) cvstate
   149 // onto the stack.
   150 void IdealKit::loop(IdealVariable& iv, Node* init, BoolTest::mask relop, Node* limit, float prob, float cnt) {
   151   assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new loop");
   152   set(iv, init);
   153   Node* head = make_label(1);
   154   bind(head);
   155   _pending_cvstates->push(head); // push for use at end_loop
   156   _cvstate = copy_cvstate();
   157   if_then(value(iv), relop, limit, prob, cnt, false /* no new state */);
   158   DEBUG_ONLY(_state->push(LoopS));
   159   assert(ctrl()->is_IfTrue(), "true branch stays in loop");
   160   assert(_pending_cvstates->top()->in(TypeFunc::Control)->is_IfFalse(), "false branch exits loop");
   161 }
   163 //-------------------------------end_loop-------------------------------------
   164 // Creates the goto top label.
   165 // Expects the else (loop exit) cvstate to be on top of the
   166 // stack, and the loop top cvstate to be 2nd.
   167 void IdealKit::end_loop() {
   168   assert((state() == LoopS), "bad state for new end_loop");
   169   Node* exit = _pending_cvstates->pop();
   170   Node* head = _pending_cvstates->pop();
   171   goto_(head);
   172   clear(head);
   173   DEBUG_ONLY(_state->pop());
   174   _cvstate = exit;
   175 }
   177 //-------------------------------make_label-------------------------------------
   178 // Creates a label.  The number of goto's
   179 // must be specified (which should be 1 less than
   180 // the number of precedessors.)
   181 Node* IdealKit::make_label(int goto_ct) {
   182   assert(_cvstate != NULL, "must declare variables before labels");
   183   Node* lab = new_cvstate();
   184   int sz = 1 + goto_ct + 1 /* fall thru */;
   185   Node* reg = delay_transform(new (C, sz) RegionNode(sz));
   186   lab->init_req(TypeFunc::Control, reg);
   187   return lab;
   188 }
   190 //-------------------------------bind-------------------------------------
   191 // Bind a label at the current cvstate by simulating
   192 // a goto to the label.
   193 void IdealKit::bind(Node* lab) {
   194   goto_(lab, true /* bind */);
   195   _cvstate = lab;
   196 }
   198 //-------------------------------goto_-------------------------------------
   199 // Make the current cvstate a predecessor of the label,
   200 // creating phi's to merge values.  If bind is true and
   201 // this is not the last control edge, then ensure that
   202 // all live values have phis created. Used to create phis
   203 // at loop-top regions.
   204 void IdealKit::goto_(Node* lab, bool bind) {
   205   Node* reg = lab->in(TypeFunc::Control);
   206   // find next empty slot in region
   207   uint slot = 1;
   208   while (slot < reg->req() && reg->in(slot) != NULL) slot++;
   209   assert(slot < reg->req(), "too many gotos");
   210   // If this is last predecessor, then don't force phi creation
   211   if (slot == reg->req() - 1) bind = false;
   212   reg->init_req(slot, ctrl());
   213   assert(first_var + _var_ct == _cvstate->req(), "bad _cvstate size");
   214   for (uint i = first_var; i < _cvstate->req(); i++) {
   216     // l is the value of var reaching the label. Could be a single value
   217     // reaching the label, or a phi that merges multiples values reaching
   218     // the label.  The latter is true if the label's input: in(..) is
   219     // a phi whose control input is the region node for the label.
   221     Node* l = lab->in(i);
   222     // Get the current value of the var
   223     Node* m = _cvstate->in(i);
   224     // If the var went unused no need for a phi
   225     if (m == NULL) {
   226       continue;
   227     } else if (l == NULL || m == l) {
   228       // Only one unique value "m" is known to reach this label so a phi
   229       // is not yet necessary unless:
   230       //    the label is being bound and all predecessors have not been seen,
   231       //    in which case "bind" will be true.
   232       if (bind) {
   233         m = promote_to_phi(m, reg);
   234       }
   235       // Record the phi/value used for this var in the label's cvstate
   236       lab->set_req(i, m);
   237     } else {
   238       // More than one value for the variable reaches this label so
   239       // a create a phi if one does not already exist.
   240       if (!was_promoted_to_phi(l, reg)) {
   241         l = promote_to_phi(l, reg);
   242         lab->set_req(i, l);
   243       }
   244       // Record in the phi, the var's value from the current state
   245       l->set_req(slot, m);
   246     }
   247   }
   248   do_memory_merge(_cvstate, lab);
   249   stop();
   250 }
   252 //-----------------------------promote_to_phi-----------------------------------
   253 Node* IdealKit::promote_to_phi(Node* n, Node* reg) {
   254   assert(!was_promoted_to_phi(n, reg), "n already promoted to phi on this region");
   255   // Get a conservative type for the phi
   256   const BasicType bt = n->bottom_type()->basic_type();
   257   const Type* ct = Type::get_const_basic_type(bt);
   258   return delay_transform(PhiNode::make(reg, n, ct));
   259 }
   261 //-----------------------------declares_done-----------------------------------
   262 void IdealKit::declares_done() {
   263   _cvstate = new_cvstate();   // initialize current cvstate
   264   set_ctrl(_initial_ctrl);    // initialize control in current cvstate
   265   set_all_memory(_initial_memory);// initialize memory in current cvstate
   266   DEBUG_ONLY(_state->push(BlockS));
   267 }
   269 //-----------------------------transform-----------------------------------
   270 Node* IdealKit::transform(Node* n) {
   271   if (_delay_all_transforms) {
   272     return delay_transform(n);
   273   } else {
   274     return gvn().transform(n);
   275   }
   276 }
   278 //-----------------------------delay_transform-----------------------------------
   279 Node* IdealKit::delay_transform(Node* n) {
   280   gvn().set_type(n, n->bottom_type());
   281   _delay_transform->push(n);
   282   return n;
   283 }
   285 //-----------------------------new_cvstate-----------------------------------
   286 Node* IdealKit::new_cvstate() {
   287   uint sz = _var_ct + first_var;
   288   return new (C, sz) Node(sz);
   289 }
   291 //-----------------------------copy_cvstate-----------------------------------
   292 Node* IdealKit::copy_cvstate() {
   293   Node* ns = new_cvstate();
   294   for (uint i = 0; i < ns->req(); i++) ns->init_req(i, _cvstate->in(i));
   295   // We must clone memory since it will be updated as we do stores.
   296   ns->set_req(TypeFunc::Memory, MergeMemNode::make(C, ns->in(TypeFunc::Memory)));
   297   return ns;
   298 }
   300 //-----------------------------clear-----------------------------------
   301 void IdealKit::clear(Node* m) {
   302   for (uint i = 0; i < m->req(); i++) m->set_req(i, NULL);
   303 }
   305 //-----------------------------drain_delay_transform----------------------------
   306 void IdealKit::drain_delay_transform() {
   307   while (_delay_transform->length() > 0) {
   308     Node* n = _delay_transform->pop();
   309     gvn().transform(n);
   310     if (!gvn().is_IterGVN()) {
   311       C->record_for_igvn(n);
   312     }
   313   }
   314 }
   316 //-----------------------------IdealVariable----------------------------
   317 IdealVariable::IdealVariable(IdealKit &k) {
   318   k.declare(this);
   319 }
   321 Node* IdealKit::memory(uint alias_idx) {
   322   MergeMemNode* mem = merged_memory();
   323   Node* p = mem->memory_at(alias_idx);
   324   _gvn.set_type(p, Type::MEMORY);  // must be mapped
   325   return p;
   326 }
   328 void IdealKit::set_memory(Node* mem, uint alias_idx) {
   329   merged_memory()->set_memory_at(alias_idx, mem);
   330 }
   332 //----------------------------- make_load ----------------------------
   333 Node* IdealKit::load(Node* ctl,
   334                      Node* adr,
   335                      const Type* t,
   336                      BasicType bt,
   337                      int adr_idx,
   338                      bool require_atomic_access) {
   340   assert(adr_idx != Compile::AliasIdxTop, "use other make_load factory" );
   341   const TypePtr* adr_type = NULL; // debug-mode-only argument
   342   debug_only(adr_type = C->get_adr_type(adr_idx));
   343   Node* mem = memory(adr_idx);
   344   Node* ld;
   345   if (require_atomic_access && bt == T_LONG) {
   346     ld = LoadLNode::make_atomic(C, ctl, mem, adr, adr_type, t);
   347   } else {
   348     ld = LoadNode::make(C, ctl, mem, adr, adr_type, t, bt);
   349   }
   350   return transform(ld);
   351 }
   353 Node* IdealKit::store(Node* ctl, Node* adr, Node *val, BasicType bt,
   354                                 int adr_idx,
   355                                 bool require_atomic_access) {
   356   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
   357   const TypePtr* adr_type = NULL;
   358   debug_only(adr_type = C->get_adr_type(adr_idx));
   359   Node *mem = memory(adr_idx);
   360   Node* st;
   361   if (require_atomic_access && bt == T_LONG) {
   362     st = StoreLNode::make_atomic(C, ctl, mem, adr, adr_type, val);
   363   } else {
   364     st = StoreNode::make(C, ctl, mem, adr, adr_type, val, bt);
   365   }
   366   st = transform(st);
   367   set_memory(st, adr_idx);
   369   return st;
   370 }
   372 // Card mark store. Must be ordered so that it will come after the store of
   373 // the oop.
   374 Node* IdealKit::storeCM(Node* ctl, Node* adr, Node *val, Node* oop_store,
   375                         BasicType bt,
   376                         int adr_idx) {
   377   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
   378   const TypePtr* adr_type = NULL;
   379   debug_only(adr_type = C->get_adr_type(adr_idx));
   380   Node *mem = memory(adr_idx);
   382   // Add required edge to oop_store, optimizer does not support precedence edges.
   383   // Convert required edge to precedence edge before allocation.
   384   Node* st = new (C, 5) StoreCMNode(ctl, mem, adr, adr_type, val, oop_store);
   386   st = transform(st);
   387   set_memory(st, adr_idx);
   389   return st;
   390 }
   392 //---------------------------- do_memory_merge --------------------------------
   393 // The memory from one merging cvstate needs to be merged with the memory for another
   394 // join cvstate. If the join cvstate doesn't have a merged memory yet then we
   395 // can just copy the state from the merging cvstate
   397 // Merge one slow path into the rest of memory.
   398 void IdealKit::do_memory_merge(Node* merging, Node* join) {
   400   // Get the region for the join state
   401   Node* join_region = join->in(TypeFunc::Control);
   402   assert(join_region != NULL, "join region must exist");
   403   if (join->in(TypeFunc::Memory) == NULL ) {
   404     join->set_req(TypeFunc::Memory,  merging->in(TypeFunc::Memory));
   405     return;
   406   }
   408   // The control flow for merging must have already been attached to the join region
   409   // we need its index for the phis.
   410   uint slot;
   411   for (slot = 1; slot < join_region->req() ; slot ++ ) {
   412     if (join_region->in(slot) == merging->in(TypeFunc::Control)) break;
   413   }
   414   assert(slot !=  join_region->req(), "edge must already exist");
   416   MergeMemNode* join_m    = join->in(TypeFunc::Memory)->as_MergeMem();
   417   MergeMemNode* merging_m = merging->in(TypeFunc::Memory)->as_MergeMem();
   419   // join_m should be an ancestor mergemem of merging
   420   // Slow path memory comes from the current map (which is from a slow call)
   421   // Fast path/null path memory comes from the call's input
   423   // Merge the other fast-memory inputs with the new slow-default memory.
   424   // for (MergeMemStream mms(merged_memory(), fast_mem->as_MergeMem()); mms.next_non_empty2(); ) {
   425   for (MergeMemStream mms(join_m, merging_m); mms.next_non_empty2(); ) {
   426     Node* join_slice = mms.force_memory();
   427     Node* merging_slice = mms.memory2();
   428     if (join_slice != merging_slice) {
   429       PhiNode* phi;
   430       // bool new_phi = false;
   431       // Is the phi for this slice one that we created for this join region or simply
   432       // one we copied? If it is ours then add
   433       if (join_slice->is_Phi() && join_slice->as_Phi()->region() == join_region) {
   434         phi = join_slice->as_Phi();
   435       } else {
   436         // create the phi with join_slice filling supplying memory for all of the
   437         // control edges to the join region
   438         phi = PhiNode::make(join_region, join_slice, Type::MEMORY, mms.adr_type(C));
   439         phi = (PhiNode*) delay_transform(phi);
   440         // gvn().set_type(phi, Type::MEMORY);
   441         // new_phi = true;
   442       }
   443       // Now update the phi with the slice for the merging slice
   444       phi->set_req(slot, merging_slice/* slow_path, slow_slice */);
   445       // this updates join_m with the phi
   446       mms.set_memory(phi);
   447     }
   448   }
   449 }
   452 //----------------------------- make_call  ----------------------------
   453 // Trivial runtime call
   454 void IdealKit::make_leaf_call(const TypeFunc *slow_call_type,
   455                               address slow_call,
   456                               const char *leaf_name,
   457                               Node* parm0,
   458                               Node* parm1,
   459                               Node* parm2) {
   461   // We only handle taking in RawMem and modifying RawMem
   462   const TypePtr* adr_type = TypeRawPtr::BOTTOM;
   463   uint adr_idx = C->get_alias_index(adr_type);
   465   // Clone initial memory
   466   MergeMemNode* cloned_mem =  MergeMemNode::make(C, merged_memory());
   468   // Slow-path leaf call
   469   int size = slow_call_type->domain()->cnt();
   470   CallNode *call =  (CallNode*)new (C, size) CallLeafNode( slow_call_type, slow_call, leaf_name, adr_type);
   472   // Set fixed predefined input arguments
   473   call->init_req( TypeFunc::Control, ctrl() );
   474   call->init_req( TypeFunc::I_O    , top() )     ;   // does no i/o
   475   // Narrow memory as only memory input
   476   call->init_req( TypeFunc::Memory , memory(adr_idx));
   477   call->init_req( TypeFunc::FramePtr, top() /* frameptr() */ );
   478   call->init_req( TypeFunc::ReturnAdr, top() );
   480   if (parm0 != NULL)  call->init_req(TypeFunc::Parms+0, parm0);
   481   if (parm1 != NULL)  call->init_req(TypeFunc::Parms+1, parm1);
   482   if (parm2 != NULL)  call->init_req(TypeFunc::Parms+2, parm2);
   484   // Node *c = _gvn.transform(call);
   485   call = (CallNode *) _gvn.transform(call);
   486   Node *c = call; // dbx gets confused with call call->dump()
   488   // Slow leaf call has no side-effects, sets few values
   490   set_ctrl(transform( new (C, 1) ProjNode(call,TypeFunc::Control) ));
   492   // Set the incoming clone of memory as current memory
   493   set_all_memory(cloned_mem);
   495   // Make memory for the call
   496   Node* mem = _gvn.transform( new (C, 1) ProjNode(call, TypeFunc::Memory) );
   498   // Set the RawPtr memory state only.
   499   set_memory(mem, adr_idx);
   501   assert(C->alias_type(call->adr_type()) == C->alias_type(adr_type),
   502          "call node must be constructed correctly");
   503 }

mercurial