src/share/vm/opto/macro.cpp

Thu, 21 Aug 2008 23:36:31 -0400

author
tonyp
date
Thu, 21 Aug 2008 23:36:31 -0400
changeset 791
1ee8caae33af
parent 786
fab5f738c515
parent 688
b0fe4deeb9fb
child 797
f8199438385b
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 2005-2008 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/_macro.cpp.incl"
    29 //
    30 // Replace any references to "oldref" in inputs to "use" with "newref".
    31 // Returns the number of replacements made.
    32 //
    33 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
    34   int nreplacements = 0;
    35   uint req = use->req();
    36   for (uint j = 0; j < use->len(); j++) {
    37     Node *uin = use->in(j);
    38     if (uin == oldref) {
    39       if (j < req)
    40         use->set_req(j, newref);
    41       else
    42         use->set_prec(j, newref);
    43       nreplacements++;
    44     } else if (j >= req && uin == NULL) {
    45       break;
    46     }
    47   }
    48   return nreplacements;
    49 }
    51 void PhaseMacroExpand::copy_call_debug_info(CallNode *oldcall, CallNode * newcall) {
    52   // Copy debug information and adjust JVMState information
    53   uint old_dbg_start = oldcall->tf()->domain()->cnt();
    54   uint new_dbg_start = newcall->tf()->domain()->cnt();
    55   int jvms_adj  = new_dbg_start - old_dbg_start;
    56   assert (new_dbg_start == newcall->req(), "argument count mismatch");
    58   Dict* sosn_map = new Dict(cmpkey,hashkey);
    59   for (uint i = old_dbg_start; i < oldcall->req(); i++) {
    60     Node* old_in = oldcall->in(i);
    61     // Clone old SafePointScalarObjectNodes, adjusting their field contents.
    62     if (old_in->is_SafePointScalarObject()) {
    63       SafePointScalarObjectNode* old_sosn = old_in->as_SafePointScalarObject();
    64       uint old_unique = C->unique();
    65       Node* new_in = old_sosn->clone(jvms_adj, sosn_map);
    66       if (old_unique != C->unique()) {
    67         new_in = transform_later(new_in); // Register new node.
    68       }
    69       old_in = new_in;
    70     }
    71     newcall->add_req(old_in);
    72   }
    74   newcall->set_jvms(oldcall->jvms());
    75   for (JVMState *jvms = newcall->jvms(); jvms != NULL; jvms = jvms->caller()) {
    76     jvms->set_map(newcall);
    77     jvms->set_locoff(jvms->locoff()+jvms_adj);
    78     jvms->set_stkoff(jvms->stkoff()+jvms_adj);
    79     jvms->set_monoff(jvms->monoff()+jvms_adj);
    80     jvms->set_scloff(jvms->scloff()+jvms_adj);
    81     jvms->set_endoff(jvms->endoff()+jvms_adj);
    82   }
    83 }
    85 Node* PhaseMacroExpand::opt_iff(Node* region, Node* iff) {
    86   IfNode *opt_iff = transform_later(iff)->as_If();
    88   // Fast path taken; set region slot 2
    89   Node *fast_taken = transform_later( new (C, 1) IfFalseNode(opt_iff) );
    90   region->init_req(2,fast_taken); // Capture fast-control
    92   // Fast path not-taken, i.e. slow path
    93   Node *slow_taken = transform_later( new (C, 1) IfTrueNode(opt_iff) );
    94   return slow_taken;
    95 }
    97 //--------------------copy_predefined_input_for_runtime_call--------------------
    98 void PhaseMacroExpand::copy_predefined_input_for_runtime_call(Node * ctrl, CallNode* oldcall, CallNode* call) {
    99   // Set fixed predefined input arguments
   100   call->init_req( TypeFunc::Control, ctrl );
   101   call->init_req( TypeFunc::I_O    , oldcall->in( TypeFunc::I_O) );
   102   call->init_req( TypeFunc::Memory , oldcall->in( TypeFunc::Memory ) ); // ?????
   103   call->init_req( TypeFunc::ReturnAdr, oldcall->in( TypeFunc::ReturnAdr ) );
   104   call->init_req( TypeFunc::FramePtr, oldcall->in( TypeFunc::FramePtr ) );
   105 }
   107 //------------------------------make_slow_call---------------------------------
   108 CallNode* PhaseMacroExpand::make_slow_call(CallNode *oldcall, const TypeFunc* slow_call_type, address slow_call, const char* leaf_name, Node* slow_path, Node* parm0, Node* parm1) {
   110   // Slow-path call
   111   int size = slow_call_type->domain()->cnt();
   112  CallNode *call = leaf_name
   113    ? (CallNode*)new (C, size) CallLeafNode      ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
   114    : (CallNode*)new (C, size) CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), oldcall->jvms()->bci(), TypeRawPtr::BOTTOM );
   116   // Slow path call has no side-effects, uses few values
   117   copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
   118   if (parm0 != NULL)  call->init_req(TypeFunc::Parms+0, parm0);
   119   if (parm1 != NULL)  call->init_req(TypeFunc::Parms+1, parm1);
   120   copy_call_debug_info(oldcall, call);
   121   call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
   122   _igvn.hash_delete(oldcall);
   123   _igvn.subsume_node(oldcall, call);
   124   transform_later(call);
   126   return call;
   127 }
   129 void PhaseMacroExpand::extract_call_projections(CallNode *call) {
   130   _fallthroughproj = NULL;
   131   _fallthroughcatchproj = NULL;
   132   _ioproj_fallthrough = NULL;
   133   _ioproj_catchall = NULL;
   134   _catchallcatchproj = NULL;
   135   _memproj_fallthrough = NULL;
   136   _memproj_catchall = NULL;
   137   _resproj = NULL;
   138   for (DUIterator_Fast imax, i = call->fast_outs(imax); i < imax; i++) {
   139     ProjNode *pn = call->fast_out(i)->as_Proj();
   140     switch (pn->_con) {
   141       case TypeFunc::Control:
   142       {
   143         // For Control (fallthrough) and I_O (catch_all_index) we have CatchProj -> Catch -> Proj
   144         _fallthroughproj = pn;
   145         DUIterator_Fast jmax, j = pn->fast_outs(jmax);
   146         const Node *cn = pn->fast_out(j);
   147         if (cn->is_Catch()) {
   148           ProjNode *cpn = NULL;
   149           for (DUIterator_Fast kmax, k = cn->fast_outs(kmax); k < kmax; k++) {
   150             cpn = cn->fast_out(k)->as_Proj();
   151             assert(cpn->is_CatchProj(), "must be a CatchProjNode");
   152             if (cpn->_con == CatchProjNode::fall_through_index)
   153               _fallthroughcatchproj = cpn;
   154             else {
   155               assert(cpn->_con == CatchProjNode::catch_all_index, "must be correct index.");
   156               _catchallcatchproj = cpn;
   157             }
   158           }
   159         }
   160         break;
   161       }
   162       case TypeFunc::I_O:
   163         if (pn->_is_io_use)
   164           _ioproj_catchall = pn;
   165         else
   166           _ioproj_fallthrough = pn;
   167         break;
   168       case TypeFunc::Memory:
   169         if (pn->_is_io_use)
   170           _memproj_catchall = pn;
   171         else
   172           _memproj_fallthrough = pn;
   173         break;
   174       case TypeFunc::Parms:
   175         _resproj = pn;
   176         break;
   177       default:
   178         assert(false, "unexpected projection from allocation node.");
   179     }
   180   }
   182 }
   184 // Eliminate a card mark sequence.  p2x is a ConvP2XNode
   185 void PhaseMacroExpand::eliminate_card_mark(Node *p2x) {
   186   assert(p2x->Opcode() == Op_CastP2X, "ConvP2XNode required");
   187   Node *shift = p2x->unique_out();
   188   Node *addp = shift->unique_out();
   189   for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
   190     Node *st = addp->last_out(j);
   191     assert(st->is_Store(), "store required");
   192     _igvn.replace_node(st, st->in(MemNode::Memory));
   193   }
   194 }
   196 // Search for a memory operation for the specified memory slice.
   197 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) {
   198   Node *orig_mem = mem;
   199   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   200   const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr();
   201   while (true) {
   202     if (mem == alloc_mem || mem == start_mem ) {
   203       return mem;  // hit one of our sentinals
   204     } else if (mem->is_MergeMem()) {
   205       mem = mem->as_MergeMem()->memory_at(alias_idx);
   206     } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
   207       Node *in = mem->in(0);
   208       // we can safely skip over safepoints, calls, locks and membars because we
   209       // already know that the object is safe to eliminate.
   210       if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) {
   211         return in;
   212       } else if (in->is_Call()) {
   213         CallNode *call = in->as_Call();
   214         if (!call->may_modify(tinst, phase)) {
   215           mem = call->in(TypeFunc::Memory);
   216         }
   217         mem = in->in(TypeFunc::Memory);
   218       } else if (in->is_MemBar()) {
   219         mem = in->in(TypeFunc::Memory);
   220       } else {
   221         assert(false, "unexpected projection");
   222       }
   223     } else if (mem->is_Store()) {
   224       const TypePtr* atype = mem->as_Store()->adr_type();
   225       int adr_idx = Compile::current()->get_alias_index(atype);
   226       if (adr_idx == alias_idx) {
   227         assert(atype->isa_oopptr(), "address type must be oopptr");
   228         int adr_offset = atype->offset();
   229         uint adr_iid = atype->is_oopptr()->instance_id();
   230         // Array elements references have the same alias_idx
   231         // but different offset and different instance_id.
   232         if (adr_offset == offset && adr_iid == alloc->_idx)
   233           return mem;
   234       } else {
   235         assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
   236       }
   237       mem = mem->in(MemNode::Memory);
   238     } else {
   239       return mem;
   240     }
   241     assert(mem != orig_mem, "dead memory loop");
   242   }
   243 }
   245 //
   246 // Given a Memory Phi, compute a value Phi containing the values from stores
   247 // on the input paths.
   248 // Note: this function is recursive, its depth is limied by the "level" argument
   249 // Returns the computed Phi, or NULL if it cannot compute it.
   250 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, Node *alloc, Node_Stack *value_phis, int level) {
   251   assert(mem->is_Phi(), "sanity");
   252   int alias_idx = C->get_alias_index(adr_t);
   253   int offset = adr_t->offset();
   254   int instance_id = adr_t->instance_id();
   256   // Check if an appropriate value phi already exists.
   257   Node* region = mem->in(0);
   258   for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
   259     Node* phi = region->fast_out(k);
   260     if (phi->is_Phi() && phi != mem &&
   261         phi->as_Phi()->is_same_inst_field(phi_type, instance_id, alias_idx, offset)) {
   262       return phi;
   263     }
   264   }
   265   // Check if an appropriate new value phi already exists.
   266   Node* new_phi = NULL;
   267   uint size = value_phis->size();
   268   for (uint i=0; i < size; i++) {
   269     if ( mem->_idx == value_phis->index_at(i) ) {
   270       return value_phis->node_at(i);
   271     }
   272   }
   274   if (level <= 0) {
   275     return NULL; // Give up: phi tree too deep
   276   }
   277   Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
   278   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   280   uint length = mem->req();
   281   GrowableArray <Node *> values(length, length, NULL);
   283   // create a new Phi for the value
   284   PhiNode *phi = new (C, length) PhiNode(mem->in(0), phi_type, NULL, instance_id, alias_idx, offset);
   285   transform_later(phi);
   286   value_phis->push(phi, mem->_idx);
   288   for (uint j = 1; j < length; j++) {
   289     Node *in = mem->in(j);
   290     if (in == NULL || in->is_top()) {
   291       values.at_put(j, in);
   292     } else  {
   293       Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn);
   294       if (val == start_mem || val == alloc_mem) {
   295         // hit a sentinel, return appropriate 0 value
   296         values.at_put(j, _igvn.zerocon(ft));
   297         continue;
   298       }
   299       if (val->is_Initialize()) {
   300         val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
   301       }
   302       if (val == NULL) {
   303         return NULL;  // can't find a value on this path
   304       }
   305       if (val == mem) {
   306         values.at_put(j, mem);
   307       } else if (val->is_Store()) {
   308         values.at_put(j, val->in(MemNode::ValueIn));
   309       } else if(val->is_Proj() && val->in(0) == alloc) {
   310         values.at_put(j, _igvn.zerocon(ft));
   311       } else if (val->is_Phi()) {
   312         val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1);
   313         if (val == NULL) {
   314           return NULL;
   315         }
   316         values.at_put(j, val);
   317       } else {
   318         assert(false, "unknown node on this path");
   319         return NULL;  // unknown node on this path
   320       }
   321     }
   322   }
   323   // Set Phi's inputs
   324   for (uint j = 1; j < length; j++) {
   325     if (values.at(j) == mem) {
   326       phi->init_req(j, phi);
   327     } else {
   328       phi->init_req(j, values.at(j));
   329     }
   330   }
   331   return phi;
   332 }
   334 // Search the last value stored into the object's field.
   335 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, Node *alloc) {
   336   assert(adr_t->is_known_instance_field(), "instance required");
   337   int instance_id = adr_t->instance_id();
   338   assert((uint)instance_id == alloc->_idx, "wrong allocation");
   340   int alias_idx = C->get_alias_index(adr_t);
   341   int offset = adr_t->offset();
   342   Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
   343   Node *alloc_ctrl = alloc->in(TypeFunc::Control);
   344   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   345   Arena *a = Thread::current()->resource_area();
   346   VectorSet visited(a);
   349   bool done = sfpt_mem == alloc_mem;
   350   Node *mem = sfpt_mem;
   351   while (!done) {
   352     if (visited.test_set(mem->_idx)) {
   353       return NULL;  // found a loop, give up
   354     }
   355     mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn);
   356     if (mem == start_mem || mem == alloc_mem) {
   357       done = true;  // hit a sentinel, return appropriate 0 value
   358     } else if (mem->is_Initialize()) {
   359       mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
   360       if (mem == NULL) {
   361         done = true; // Something go wrong.
   362       } else if (mem->is_Store()) {
   363         const TypePtr* atype = mem->as_Store()->adr_type();
   364         assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
   365         done = true;
   366       }
   367     } else if (mem->is_Store()) {
   368       const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
   369       assert(atype != NULL, "address type must be oopptr");
   370       assert(C->get_alias_index(atype) == alias_idx &&
   371              atype->is_known_instance_field() && atype->offset() == offset &&
   372              atype->instance_id() == instance_id, "store is correct memory slice");
   373       done = true;
   374     } else if (mem->is_Phi()) {
   375       // try to find a phi's unique input
   376       Node *unique_input = NULL;
   377       Node *top = C->top();
   378       for (uint i = 1; i < mem->req(); i++) {
   379         Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn);
   380         if (n == NULL || n == top || n == mem) {
   381           continue;
   382         } else if (unique_input == NULL) {
   383           unique_input = n;
   384         } else if (unique_input != n) {
   385           unique_input = top;
   386           break;
   387         }
   388       }
   389       if (unique_input != NULL && unique_input != top) {
   390         mem = unique_input;
   391       } else {
   392         done = true;
   393       }
   394     } else {
   395       assert(false, "unexpected node");
   396     }
   397   }
   398   if (mem != NULL) {
   399     if (mem == start_mem || mem == alloc_mem) {
   400       // hit a sentinel, return appropriate 0 value
   401       return _igvn.zerocon(ft);
   402     } else if (mem->is_Store()) {
   403       return mem->in(MemNode::ValueIn);
   404     } else if (mem->is_Phi()) {
   405       // attempt to produce a Phi reflecting the values on the input paths of the Phi
   406       Node_Stack value_phis(a, 8);
   407       Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit);
   408       if (phi != NULL) {
   409         return phi;
   410       } else {
   411         // Kill all new Phis
   412         while(value_phis.is_nonempty()) {
   413           Node* n = value_phis.node();
   414           _igvn.hash_delete(n);
   415           _igvn.subsume_node(n, C->top());
   416           value_phis.pop();
   417         }
   418       }
   419     }
   420   }
   421   // Something go wrong.
   422   return NULL;
   423 }
   425 // Check the possibility of scalar replacement.
   426 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
   427   //  Scan the uses of the allocation to check for anything that would
   428   //  prevent us from eliminating it.
   429   NOT_PRODUCT( const char* fail_eliminate = NULL; )
   430   DEBUG_ONLY( Node* disq_node = NULL; )
   431   bool  can_eliminate = true;
   433   Node* res = alloc->result_cast();
   434   const TypeOopPtr* res_type = NULL;
   435   if (res == NULL) {
   436     // All users were eliminated.
   437   } else if (!res->is_CheckCastPP()) {
   438     alloc->_is_scalar_replaceable = false;  // don't try again
   439     NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
   440     can_eliminate = false;
   441   } else {
   442     res_type = _igvn.type(res)->isa_oopptr();
   443     if (res_type == NULL) {
   444       NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
   445       can_eliminate = false;
   446     } else if (res_type->isa_aryptr()) {
   447       int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
   448       if (length < 0) {
   449         NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
   450         can_eliminate = false;
   451       }
   452     }
   453   }
   455   if (can_eliminate && res != NULL) {
   456     for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
   457                                j < jmax && can_eliminate; j++) {
   458       Node* use = res->fast_out(j);
   460       if (use->is_AddP()) {
   461         const TypePtr* addp_type = _igvn.type(use)->is_ptr();
   462         int offset = addp_type->offset();
   464         if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
   465           NOT_PRODUCT(fail_eliminate = "Undefined field referrence";)
   466           can_eliminate = false;
   467           break;
   468         }
   469         for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
   470                                    k < kmax && can_eliminate; k++) {
   471           Node* n = use->fast_out(k);
   472           if (!n->is_Store() && n->Opcode() != Op_CastP2X) {
   473             DEBUG_ONLY(disq_node = n;)
   474             if (n->is_Load() || n->is_LoadStore()) {
   475               NOT_PRODUCT(fail_eliminate = "Field load";)
   476             } else {
   477               NOT_PRODUCT(fail_eliminate = "Not store field referrence";)
   478             }
   479             can_eliminate = false;
   480           }
   481         }
   482       } else if (use->is_SafePoint()) {
   483         SafePointNode* sfpt = use->as_SafePoint();
   484         if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
   485           // Object is passed as argument.
   486           DEBUG_ONLY(disq_node = use;)
   487           NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
   488           can_eliminate = false;
   489         }
   490         Node* sfptMem = sfpt->memory();
   491         if (sfptMem == NULL || sfptMem->is_top()) {
   492           DEBUG_ONLY(disq_node = use;)
   493           NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
   494           can_eliminate = false;
   495         } else {
   496           safepoints.append_if_missing(sfpt);
   497         }
   498       } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
   499         if (use->is_Phi()) {
   500           if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
   501             NOT_PRODUCT(fail_eliminate = "Object is return value";)
   502           } else {
   503             NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
   504           }
   505           DEBUG_ONLY(disq_node = use;)
   506         } else {
   507           if (use->Opcode() == Op_Return) {
   508             NOT_PRODUCT(fail_eliminate = "Object is return value";)
   509           }else {
   510             NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
   511           }
   512           DEBUG_ONLY(disq_node = use;)
   513         }
   514         can_eliminate = false;
   515       }
   516     }
   517   }
   519 #ifndef PRODUCT
   520   if (PrintEliminateAllocations) {
   521     if (can_eliminate) {
   522       tty->print("Scalar ");
   523       if (res == NULL)
   524         alloc->dump();
   525       else
   526         res->dump();
   527     } else {
   528       tty->print("NotScalar (%s)", fail_eliminate);
   529       if (res == NULL)
   530         alloc->dump();
   531       else
   532         res->dump();
   533 #ifdef ASSERT
   534       if (disq_node != NULL) {
   535           tty->print("  >>>> ");
   536           disq_node->dump();
   537       }
   538 #endif /*ASSERT*/
   539     }
   540   }
   541 #endif
   542   return can_eliminate;
   543 }
   545 // Do scalar replacement.
   546 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
   547   GrowableArray <SafePointNode *> safepoints_done;
   549   ciKlass* klass = NULL;
   550   ciInstanceKlass* iklass = NULL;
   551   int nfields = 0;
   552   int array_base;
   553   int element_size;
   554   BasicType basic_elem_type;
   555   ciType* elem_type;
   557   Node* res = alloc->result_cast();
   558   const TypeOopPtr* res_type = NULL;
   559   if (res != NULL) { // Could be NULL when there are no users
   560     res_type = _igvn.type(res)->isa_oopptr();
   561   }
   563   if (res != NULL) {
   564     klass = res_type->klass();
   565     if (res_type->isa_instptr()) {
   566       // find the fields of the class which will be needed for safepoint debug information
   567       assert(klass->is_instance_klass(), "must be an instance klass.");
   568       iklass = klass->as_instance_klass();
   569       nfields = iklass->nof_nonstatic_fields();
   570     } else {
   571       // find the array's elements which will be needed for safepoint debug information
   572       nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
   573       assert(klass->is_array_klass() && nfields >= 0, "must be an array klass.");
   574       elem_type = klass->as_array_klass()->element_type();
   575       basic_elem_type = elem_type->basic_type();
   576       array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
   577       element_size = type2aelembytes(basic_elem_type);
   578     }
   579   }
   580   //
   581   // Process the safepoint uses
   582   //
   583   while (safepoints.length() > 0) {
   584     SafePointNode* sfpt = safepoints.pop();
   585     Node* mem = sfpt->memory();
   586     uint first_ind = sfpt->req();
   587     SafePointScalarObjectNode* sobj = new (C, 1) SafePointScalarObjectNode(res_type,
   588 #ifdef ASSERT
   589                                                  alloc,
   590 #endif
   591                                                  first_ind, nfields);
   592     sobj->init_req(0, sfpt->in(TypeFunc::Control));
   593     transform_later(sobj);
   595     // Scan object's fields adding an input to the safepoint for each field.
   596     for (int j = 0; j < nfields; j++) {
   597       int offset;
   598       ciField* field = NULL;
   599       if (iklass != NULL) {
   600         field = iklass->nonstatic_field_at(j);
   601         offset = field->offset();
   602         elem_type = field->type();
   603         basic_elem_type = field->layout_type();
   604       } else {
   605         offset = array_base + j * element_size;
   606       }
   608       const Type *field_type;
   609       // The next code is taken from Parse::do_get_xxx().
   610       if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) {
   611         if (!elem_type->is_loaded()) {
   612           field_type = TypeInstPtr::BOTTOM;
   613         } else if (field != NULL && field->is_constant()) {
   614           // This can happen if the constant oop is non-perm.
   615           ciObject* con = field->constant_value().as_object();
   616           // Do not "join" in the previous type; it doesn't add value,
   617           // and may yield a vacuous result if the field is of interface type.
   618           field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
   619           assert(field_type != NULL, "field singleton type must be consistent");
   620         } else {
   621           field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
   622         }
   623         if (UseCompressedOops) {
   624           field_type = field_type->make_narrowoop();
   625           basic_elem_type = T_NARROWOOP;
   626         }
   627       } else {
   628         field_type = Type::get_const_basic_type(basic_elem_type);
   629       }
   631       const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
   633       Node *field_val = value_from_mem(mem, basic_elem_type, field_type, field_addr_type, alloc);
   634       if (field_val == NULL) {
   635         // we weren't able to find a value for this field,
   636         // give up on eliminating this allocation
   637         alloc->_is_scalar_replaceable = false;  // don't try again
   638         // remove any extra entries we added to the safepoint
   639         uint last = sfpt->req() - 1;
   640         for (int k = 0;  k < j; k++) {
   641           sfpt->del_req(last--);
   642         }
   643         // rollback processed safepoints
   644         while (safepoints_done.length() > 0) {
   645           SafePointNode* sfpt_done = safepoints_done.pop();
   646           // remove any extra entries we added to the safepoint
   647           last = sfpt_done->req() - 1;
   648           for (int k = 0;  k < nfields; k++) {
   649             sfpt_done->del_req(last--);
   650           }
   651           JVMState *jvms = sfpt_done->jvms();
   652           jvms->set_endoff(sfpt_done->req());
   653           // Now make a pass over the debug information replacing any references
   654           // to SafePointScalarObjectNode with the allocated object.
   655           int start = jvms->debug_start();
   656           int end   = jvms->debug_end();
   657           for (int i = start; i < end; i++) {
   658             if (sfpt_done->in(i)->is_SafePointScalarObject()) {
   659               SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject();
   660               if (scobj->first_index() == sfpt_done->req() &&
   661                   scobj->n_fields() == (uint)nfields) {
   662                 assert(scobj->alloc() == alloc, "sanity");
   663                 sfpt_done->set_req(i, res);
   664               }
   665             }
   666           }
   667         }
   668 #ifndef PRODUCT
   669         if (PrintEliminateAllocations) {
   670           if (field != NULL) {
   671             tty->print("=== At SafePoint node %d can't find value of Field: ",
   672                        sfpt->_idx);
   673             field->print();
   674             int field_idx = C->get_alias_index(field_addr_type);
   675             tty->print(" (alias_idx=%d)", field_idx);
   676           } else { // Array's element
   677             tty->print("=== At SafePoint node %d can't find value of array element [%d]",
   678                        sfpt->_idx, j);
   679           }
   680           tty->print(", which prevents elimination of: ");
   681           if (res == NULL)
   682             alloc->dump();
   683           else
   684             res->dump();
   685         }
   686 #endif
   687         return false;
   688       }
   689       if (UseCompressedOops && field_type->isa_narrowoop()) {
   690         // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
   691         // to be able scalar replace the allocation.
   692         if (field_val->is_EncodeP()) {
   693           field_val = field_val->in(1);
   694         } else {
   695           field_val = transform_later(new (C, 2) DecodeNNode(field_val, field_val->bottom_type()->make_ptr()));
   696         }
   697       }
   698       sfpt->add_req(field_val);
   699     }
   700     JVMState *jvms = sfpt->jvms();
   701     jvms->set_endoff(sfpt->req());
   702     // Now make a pass over the debug information replacing any references
   703     // to the allocated object with "sobj"
   704     int start = jvms->debug_start();
   705     int end   = jvms->debug_end();
   706     for (int i = start; i < end; i++) {
   707       if (sfpt->in(i) == res) {
   708         sfpt->set_req(i, sobj);
   709       }
   710     }
   711     safepoints_done.append_if_missing(sfpt); // keep it for rollback
   712   }
   713   return true;
   714 }
   716 // Process users of eliminated allocation.
   717 void PhaseMacroExpand::process_users_of_allocation(AllocateNode *alloc) {
   718   Node* res = alloc->result_cast();
   719   if (res != NULL) {
   720     for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
   721       Node *use = res->last_out(j);
   722       uint oc1 = res->outcnt();
   724       if (use->is_AddP()) {
   725         for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
   726           Node *n = use->last_out(k);
   727           uint oc2 = use->outcnt();
   728           if (n->is_Store()) {
   729             _igvn.replace_node(n, n->in(MemNode::Memory));
   730           } else {
   731             assert( n->Opcode() == Op_CastP2X, "CastP2X required");
   732             eliminate_card_mark(n);
   733           }
   734           k -= (oc2 - use->outcnt());
   735         }
   736       } else {
   737         assert( !use->is_SafePoint(), "safepoint uses must have been already elimiated");
   738         assert( use->Opcode() == Op_CastP2X, "CastP2X required");
   739         eliminate_card_mark(use);
   740       }
   741       j -= (oc1 - res->outcnt());
   742     }
   743     assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
   744     _igvn.remove_dead_node(res);
   745   }
   747   //
   748   // Process other users of allocation's projections
   749   //
   750   if (_resproj != NULL && _resproj->outcnt() != 0) {
   751     for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) {
   752       Node *use = _resproj->last_out(j);
   753       uint oc1 = _resproj->outcnt();
   754       if (use->is_Initialize()) {
   755         // Eliminate Initialize node.
   756         InitializeNode *init = use->as_Initialize();
   757         assert(init->outcnt() <= 2, "only a control and memory projection expected");
   758         Node *ctrl_proj = init->proj_out(TypeFunc::Control);
   759         if (ctrl_proj != NULL) {
   760            assert(init->in(TypeFunc::Control) == _fallthroughcatchproj, "allocation control projection");
   761           _igvn.replace_node(ctrl_proj, _fallthroughcatchproj);
   762         }
   763         Node *mem_proj = init->proj_out(TypeFunc::Memory);
   764         if (mem_proj != NULL) {
   765           Node *mem = init->in(TypeFunc::Memory);
   766 #ifdef ASSERT
   767           if (mem->is_MergeMem()) {
   768             assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection");
   769           } else {
   770             assert(mem == _memproj_fallthrough, "allocation memory projection");
   771           }
   772 #endif
   773           _igvn.replace_node(mem_proj, mem);
   774         }
   775       } else if (use->is_AddP()) {
   776         // raw memory addresses used only by the initialization
   777         _igvn.hash_delete(use);
   778         _igvn.subsume_node(use, C->top());
   779       } else  {
   780         assert(false, "only Initialize or AddP expected");
   781       }
   782       j -= (oc1 - _resproj->outcnt());
   783     }
   784   }
   785   if (_fallthroughcatchproj != NULL) {
   786     _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control));
   787   }
   788   if (_memproj_fallthrough != NULL) {
   789     _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory));
   790   }
   791   if (_memproj_catchall != NULL) {
   792     _igvn.replace_node(_memproj_catchall, C->top());
   793   }
   794   if (_ioproj_fallthrough != NULL) {
   795     _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O));
   796   }
   797   if (_ioproj_catchall != NULL) {
   798     _igvn.replace_node(_ioproj_catchall, C->top());
   799   }
   800   if (_catchallcatchproj != NULL) {
   801     _igvn.replace_node(_catchallcatchproj, C->top());
   802   }
   803 }
   805 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
   807   if (!EliminateAllocations || !alloc->_is_scalar_replaceable) {
   808     return false;
   809   }
   811   extract_call_projections(alloc);
   813   GrowableArray <SafePointNode *> safepoints;
   814   if (!can_eliminate_allocation(alloc, safepoints)) {
   815     return false;
   816   }
   818   if (!scalar_replacement(alloc, safepoints)) {
   819     return false;
   820   }
   822   process_users_of_allocation(alloc);
   824 #ifndef PRODUCT
   825 if (PrintEliminateAllocations) {
   826   if (alloc->is_AllocateArray())
   827     tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
   828   else
   829     tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
   830 }
   831 #endif
   833   return true;
   834 }
   837 //---------------------------set_eden_pointers-------------------------
   838 void PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) {
   839   if (UseTLAB) {                // Private allocation: load from TLS
   840     Node* thread = transform_later(new (C, 1) ThreadLocalNode());
   841     int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset());
   842     int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset());
   843     eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset);
   844     eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset);
   845   } else {                      // Shared allocation: load from globals
   846     CollectedHeap* ch = Universe::heap();
   847     address top_adr = (address)ch->top_addr();
   848     address end_adr = (address)ch->end_addr();
   849     eden_top_adr = makecon(TypeRawPtr::make(top_adr));
   850     eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr);
   851   }
   852 }
   855 Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) {
   856   Node* adr = basic_plus_adr(base, offset);
   857   const TypePtr* adr_type = TypeRawPtr::BOTTOM;
   858   Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt);
   859   transform_later(value);
   860   return value;
   861 }
   864 Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) {
   865   Node* adr = basic_plus_adr(base, offset);
   866   mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt);
   867   transform_later(mem);
   868   return mem;
   869 }
   871 //=============================================================================
   872 //
   873 //                              A L L O C A T I O N
   874 //
   875 // Allocation attempts to be fast in the case of frequent small objects.
   876 // It breaks down like this:
   877 //
   878 // 1) Size in doublewords is computed.  This is a constant for objects and
   879 // variable for most arrays.  Doubleword units are used to avoid size
   880 // overflow of huge doubleword arrays.  We need doublewords in the end for
   881 // rounding.
   882 //
   883 // 2) Size is checked for being 'too large'.  Too-large allocations will go
   884 // the slow path into the VM.  The slow path can throw any required
   885 // exceptions, and does all the special checks for very large arrays.  The
   886 // size test can constant-fold away for objects.  For objects with
   887 // finalizers it constant-folds the otherway: you always go slow with
   888 // finalizers.
   889 //
   890 // 3) If NOT using TLABs, this is the contended loop-back point.
   891 // Load-Locked the heap top.  If using TLABs normal-load the heap top.
   892 //
   893 // 4) Check that heap top + size*8 < max.  If we fail go the slow ` route.
   894 // NOTE: "top+size*8" cannot wrap the 4Gig line!  Here's why: for largish
   895 // "size*8" we always enter the VM, where "largish" is a constant picked small
   896 // enough that there's always space between the eden max and 4Gig (old space is
   897 // there so it's quite large) and large enough that the cost of entering the VM
   898 // is dwarfed by the cost to initialize the space.
   899 //
   900 // 5) If NOT using TLABs, Store-Conditional the adjusted heap top back
   901 // down.  If contended, repeat at step 3.  If using TLABs normal-store
   902 // adjusted heap top back down; there is no contention.
   903 //
   904 // 6) If !ZeroTLAB then Bulk-clear the object/array.  Fill in klass & mark
   905 // fields.
   906 //
   907 // 7) Merge with the slow-path; cast the raw memory pointer to the correct
   908 // oop flavor.
   909 //
   910 //=============================================================================
   911 // FastAllocateSizeLimit value is in DOUBLEWORDS.
   912 // Allocations bigger than this always go the slow route.
   913 // This value must be small enough that allocation attempts that need to
   914 // trigger exceptions go the slow route.  Also, it must be small enough so
   915 // that heap_top + size_in_bytes does not wrap around the 4Gig limit.
   916 //=============================================================================j//
   917 // %%% Here is an old comment from parseHelper.cpp; is it outdated?
   918 // The allocator will coalesce int->oop copies away.  See comment in
   919 // coalesce.cpp about how this works.  It depends critically on the exact
   920 // code shape produced here, so if you are changing this code shape
   921 // make sure the GC info for the heap-top is correct in and around the
   922 // slow-path call.
   923 //
   925 void PhaseMacroExpand::expand_allocate_common(
   926             AllocateNode* alloc, // allocation node to be expanded
   927             Node* length,  // array length for an array allocation
   928             const TypeFunc* slow_call_type, // Type of slow call
   929             address slow_call_address  // Address of slow call
   930     )
   931 {
   933   Node* ctrl = alloc->in(TypeFunc::Control);
   934   Node* mem  = alloc->in(TypeFunc::Memory);
   935   Node* i_o  = alloc->in(TypeFunc::I_O);
   936   Node* size_in_bytes     = alloc->in(AllocateNode::AllocSize);
   937   Node* klass_node        = alloc->in(AllocateNode::KlassNode);
   938   Node* initial_slow_test = alloc->in(AllocateNode::InitialTest);
   940   // With escape analysis, the entire memory state was needed to be able to
   941   // eliminate the allocation.  Since the allocations cannot be eliminated,
   942   // optimize it to the raw slice.
   943   if (mem->is_MergeMem()) {
   944     mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
   945   }
   947   assert(ctrl != NULL, "must have control");
   948   // We need a Region and corresponding Phi's to merge the slow-path and fast-path results.
   949   // they will not be used if "always_slow" is set
   950   enum { slow_result_path = 1, fast_result_path = 2 };
   951   Node *result_region;
   952   Node *result_phi_rawmem;
   953   Node *result_phi_rawoop;
   954   Node *result_phi_i_o;
   956   // The initial slow comparison is a size check, the comparison
   957   // we want to do is a BoolTest::gt
   958   bool always_slow = false;
   959   int tv = _igvn.find_int_con(initial_slow_test, -1);
   960   if (tv >= 0) {
   961     always_slow = (tv == 1);
   962     initial_slow_test = NULL;
   963   } else {
   964     initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn);
   965   }
   967   if (DTraceAllocProbes ||
   968       !UseTLAB && (!Universe::heap()->supports_inline_contig_alloc() ||
   969                    (UseConcMarkSweepGC && CMSIncrementalMode))) {
   970     // Force slow-path allocation
   971     always_slow = true;
   972     initial_slow_test = NULL;
   973   }
   976   enum { too_big_or_final_path = 1, need_gc_path = 2 };
   977   Node *slow_region = NULL;
   978   Node *toobig_false = ctrl;
   980   assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent");
   981   // generate the initial test if necessary
   982   if (initial_slow_test != NULL ) {
   983     slow_region = new (C, 3) RegionNode(3);
   985     // Now make the initial failure test.  Usually a too-big test but
   986     // might be a TRUE for finalizers or a fancy class check for
   987     // newInstance0.
   988     IfNode *toobig_iff = new (C, 2) IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
   989     transform_later(toobig_iff);
   990     // Plug the failing-too-big test into the slow-path region
   991     Node *toobig_true = new (C, 1) IfTrueNode( toobig_iff );
   992     transform_later(toobig_true);
   993     slow_region    ->init_req( too_big_or_final_path, toobig_true );
   994     toobig_false = new (C, 1) IfFalseNode( toobig_iff );
   995     transform_later(toobig_false);
   996   } else {         // No initial test, just fall into next case
   997     toobig_false = ctrl;
   998     debug_only(slow_region = NodeSentinel);
   999   }
  1001   Node *slow_mem = mem;  // save the current memory state for slow path
  1002   // generate the fast allocation code unless we know that the initial test will always go slow
  1003   if (!always_slow) {
  1004     Node* eden_top_adr;
  1005     Node* eden_end_adr;
  1007     set_eden_pointers(eden_top_adr, eden_end_adr);
  1009     // Load Eden::end.  Loop invariant and hoisted.
  1010     //
  1011     // Note: We set the control input on "eden_end" and "old_eden_top" when using
  1012     //       a TLAB to work around a bug where these values were being moved across
  1013     //       a safepoint.  These are not oops, so they cannot be include in the oop
  1014     //       map, but the can be changed by a GC.   The proper way to fix this would
  1015     //       be to set the raw memory state when generating a  SafepointNode.  However
  1016     //       this will require extensive changes to the loop optimization in order to
  1017     //       prevent a degradation of the optimization.
  1018     //       See comment in memnode.hpp, around line 227 in class LoadPNode.
  1019     Node *eden_end = make_load(ctrl, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS);
  1021     // allocate the Region and Phi nodes for the result
  1022     result_region = new (C, 3) RegionNode(3);
  1023     result_phi_rawmem = new (C, 3) PhiNode( result_region, Type::MEMORY, TypeRawPtr::BOTTOM );
  1024     result_phi_rawoop = new (C, 3) PhiNode( result_region, TypeRawPtr::BOTTOM );
  1025     result_phi_i_o    = new (C, 3) PhiNode( result_region, Type::ABIO ); // I/O is used for Prefetch
  1027     // We need a Region for the loop-back contended case.
  1028     enum { fall_in_path = 1, contended_loopback_path = 2 };
  1029     Node *contended_region;
  1030     Node *contended_phi_rawmem;
  1031     if( UseTLAB ) {
  1032       contended_region = toobig_false;
  1033       contended_phi_rawmem = mem;
  1034     } else {
  1035       contended_region = new (C, 3) RegionNode(3);
  1036       contended_phi_rawmem = new (C, 3) PhiNode( contended_region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1037       // Now handle the passing-too-big test.  We fall into the contended
  1038       // loop-back merge point.
  1039       contended_region    ->init_req( fall_in_path, toobig_false );
  1040       contended_phi_rawmem->init_req( fall_in_path, mem );
  1041       transform_later(contended_region);
  1042       transform_later(contended_phi_rawmem);
  1045     // Load(-locked) the heap top.
  1046     // See note above concerning the control input when using a TLAB
  1047     Node *old_eden_top = UseTLAB
  1048       ? new (C, 3) LoadPNode     ( ctrl, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM )
  1049       : new (C, 3) LoadPLockedNode( contended_region, contended_phi_rawmem, eden_top_adr );
  1051     transform_later(old_eden_top);
  1052     // Add to heap top to get a new heap top
  1053     Node *new_eden_top = new (C, 4) AddPNode( top(), old_eden_top, size_in_bytes );
  1054     transform_later(new_eden_top);
  1055     // Check for needing a GC; compare against heap end
  1056     Node *needgc_cmp = new (C, 3) CmpPNode( new_eden_top, eden_end );
  1057     transform_later(needgc_cmp);
  1058     Node *needgc_bol = new (C, 2) BoolNode( needgc_cmp, BoolTest::ge );
  1059     transform_later(needgc_bol);
  1060     IfNode *needgc_iff = new (C, 2) IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
  1061     transform_later(needgc_iff);
  1063     // Plug the failing-heap-space-need-gc test into the slow-path region
  1064     Node *needgc_true = new (C, 1) IfTrueNode( needgc_iff );
  1065     transform_later(needgc_true);
  1066     if( initial_slow_test ) {
  1067       slow_region    ->init_req( need_gc_path, needgc_true );
  1068       // This completes all paths into the slow merge point
  1069       transform_later(slow_region);
  1070     } else {                      // No initial slow path needed!
  1071       // Just fall from the need-GC path straight into the VM call.
  1072       slow_region    = needgc_true;
  1074     // No need for a GC.  Setup for the Store-Conditional
  1075     Node *needgc_false = new (C, 1) IfFalseNode( needgc_iff );
  1076     transform_later(needgc_false);
  1078     // Grab regular I/O before optional prefetch may change it.
  1079     // Slow-path does no I/O so just set it to the original I/O.
  1080     result_phi_i_o->init_req( slow_result_path, i_o );
  1082     i_o = prefetch_allocation(i_o, needgc_false, contended_phi_rawmem,
  1083                               old_eden_top, new_eden_top, length);
  1085     // Store (-conditional) the modified eden top back down.
  1086     // StorePConditional produces flags for a test PLUS a modified raw
  1087     // memory state.
  1088     Node *store_eden_top;
  1089     Node *fast_oop_ctrl;
  1090     if( UseTLAB ) {
  1091       store_eden_top = new (C, 4) StorePNode( needgc_false, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, new_eden_top );
  1092       transform_later(store_eden_top);
  1093       fast_oop_ctrl = needgc_false; // No contention, so this is the fast path
  1094     } else {
  1095       store_eden_top = new (C, 5) StorePConditionalNode( needgc_false, contended_phi_rawmem, eden_top_adr, new_eden_top, old_eden_top );
  1096       transform_later(store_eden_top);
  1097       Node *contention_check = new (C, 2) BoolNode( store_eden_top, BoolTest::ne );
  1098       transform_later(contention_check);
  1099       store_eden_top = new (C, 1) SCMemProjNode(store_eden_top);
  1100       transform_later(store_eden_top);
  1102       // If not using TLABs, check to see if there was contention.
  1103       IfNode *contention_iff = new (C, 2) IfNode ( needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN );
  1104       transform_later(contention_iff);
  1105       Node *contention_true = new (C, 1) IfTrueNode( contention_iff );
  1106       transform_later(contention_true);
  1107       // If contention, loopback and try again.
  1108       contended_region->init_req( contended_loopback_path, contention_true );
  1109       contended_phi_rawmem->init_req( contended_loopback_path, store_eden_top );
  1111       // Fast-path succeeded with no contention!
  1112       Node *contention_false = new (C, 1) IfFalseNode( contention_iff );
  1113       transform_later(contention_false);
  1114       fast_oop_ctrl = contention_false;
  1117     // Rename successful fast-path variables to make meaning more obvious
  1118     Node* fast_oop        = old_eden_top;
  1119     Node* fast_oop_rawmem = store_eden_top;
  1120     fast_oop_rawmem = initialize_object(alloc,
  1121                                         fast_oop_ctrl, fast_oop_rawmem, fast_oop,
  1122                                         klass_node, length, size_in_bytes);
  1124     if (ExtendedDTraceProbes) {
  1125       // Slow-path call
  1126       int size = TypeFunc::Parms + 2;
  1127       CallLeafNode *call = new (C, size) CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(),
  1128                                                       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base),
  1129                                                       "dtrace_object_alloc",
  1130                                                       TypeRawPtr::BOTTOM);
  1132       // Get base of thread-local storage area
  1133       Node* thread = new (C, 1) ThreadLocalNode();
  1134       transform_later(thread);
  1136       call->init_req(TypeFunc::Parms+0, thread);
  1137       call->init_req(TypeFunc::Parms+1, fast_oop);
  1138       call->init_req( TypeFunc::Control, fast_oop_ctrl );
  1139       call->init_req( TypeFunc::I_O    , top() )        ;   // does no i/o
  1140       call->init_req( TypeFunc::Memory , fast_oop_rawmem );
  1141       call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
  1142       call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
  1143       transform_later(call);
  1144       fast_oop_ctrl = new (C, 1) ProjNode(call,TypeFunc::Control);
  1145       transform_later(fast_oop_ctrl);
  1146       fast_oop_rawmem = new (C, 1) ProjNode(call,TypeFunc::Memory);
  1147       transform_later(fast_oop_rawmem);
  1150     // Plug in the successful fast-path into the result merge point
  1151     result_region    ->init_req( fast_result_path, fast_oop_ctrl );
  1152     result_phi_rawoop->init_req( fast_result_path, fast_oop );
  1153     result_phi_i_o   ->init_req( fast_result_path, i_o );
  1154     result_phi_rawmem->init_req( fast_result_path, fast_oop_rawmem );
  1155   } else {
  1156     slow_region = ctrl;
  1159   // Generate slow-path call
  1160   CallNode *call = new (C, slow_call_type->domain()->cnt())
  1161     CallStaticJavaNode(slow_call_type, slow_call_address,
  1162                        OptoRuntime::stub_name(slow_call_address),
  1163                        alloc->jvms()->bci(),
  1164                        TypePtr::BOTTOM);
  1165   call->init_req( TypeFunc::Control, slow_region );
  1166   call->init_req( TypeFunc::I_O    , top() )     ;   // does no i/o
  1167   call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs
  1168   call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
  1169   call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
  1171   call->init_req(TypeFunc::Parms+0, klass_node);
  1172   if (length != NULL) {
  1173     call->init_req(TypeFunc::Parms+1, length);
  1176   // Copy debug information and adjust JVMState information, then replace
  1177   // allocate node with the call
  1178   copy_call_debug_info((CallNode *) alloc,  call);
  1179   if (!always_slow) {
  1180     call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
  1182   _igvn.hash_delete(alloc);
  1183   _igvn.subsume_node(alloc, call);
  1184   transform_later(call);
  1186   // Identify the output projections from the allocate node and
  1187   // adjust any references to them.
  1188   // The control and io projections look like:
  1189   //
  1190   //        v---Proj(ctrl) <-----+   v---CatchProj(ctrl)
  1191   //  Allocate                   Catch
  1192   //        ^---Proj(io) <-------+   ^---CatchProj(io)
  1193   //
  1194   //  We are interested in the CatchProj nodes.
  1195   //
  1196   extract_call_projections(call);
  1198   // An allocate node has separate memory projections for the uses on the control and i_o paths
  1199   // Replace uses of the control memory projection with result_phi_rawmem (unless we are only generating a slow call)
  1200   if (!always_slow && _memproj_fallthrough != NULL) {
  1201     for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) {
  1202       Node *use = _memproj_fallthrough->fast_out(i);
  1203       _igvn.hash_delete(use);
  1204       imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem);
  1205       _igvn._worklist.push(use);
  1206       // back up iterator
  1207       --i;
  1210   // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete _memproj_catchall so
  1211   // we end up with a call that has only 1 memory projection
  1212   if (_memproj_catchall != NULL ) {
  1213     if (_memproj_fallthrough == NULL) {
  1214       _memproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::Memory);
  1215       transform_later(_memproj_fallthrough);
  1217     for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) {
  1218       Node *use = _memproj_catchall->fast_out(i);
  1219       _igvn.hash_delete(use);
  1220       imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough);
  1221       _igvn._worklist.push(use);
  1222       // back up iterator
  1223       --i;
  1227   mem = result_phi_rawmem;
  1229   // An allocate node has separate i_o projections for the uses on the control and i_o paths
  1230   // Replace uses of the control i_o projection with result_phi_i_o (unless we are only generating a slow call)
  1231   if (_ioproj_fallthrough == NULL) {
  1232     _ioproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::I_O);
  1233     transform_later(_ioproj_fallthrough);
  1234   } else if (!always_slow) {
  1235     for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) {
  1236       Node *use = _ioproj_fallthrough->fast_out(i);
  1238       _igvn.hash_delete(use);
  1239       imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o);
  1240       _igvn._worklist.push(use);
  1241       // back up iterator
  1242       --i;
  1245   // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete _ioproj_catchall so
  1246   // we end up with a call that has only 1 control projection
  1247   if (_ioproj_catchall != NULL ) {
  1248     for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) {
  1249       Node *use = _ioproj_catchall->fast_out(i);
  1250       _igvn.hash_delete(use);
  1251       imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough);
  1252       _igvn._worklist.push(use);
  1253       // back up iterator
  1254       --i;
  1258   // if we generated only a slow call, we are done
  1259   if (always_slow)
  1260     return;
  1263   if (_fallthroughcatchproj != NULL) {
  1264     ctrl = _fallthroughcatchproj->clone();
  1265     transform_later(ctrl);
  1266     _igvn.hash_delete(_fallthroughcatchproj);
  1267     _igvn.subsume_node(_fallthroughcatchproj, result_region);
  1268   } else {
  1269     ctrl = top();
  1271   Node *slow_result;
  1272   if (_resproj == NULL) {
  1273     // no uses of the allocation result
  1274     slow_result = top();
  1275   } else {
  1276     slow_result = _resproj->clone();
  1277     transform_later(slow_result);
  1278     _igvn.hash_delete(_resproj);
  1279     _igvn.subsume_node(_resproj, result_phi_rawoop);
  1282   // Plug slow-path into result merge point
  1283   result_region    ->init_req( slow_result_path, ctrl );
  1284   result_phi_rawoop->init_req( slow_result_path, slow_result);
  1285   result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough );
  1286   transform_later(result_region);
  1287   transform_later(result_phi_rawoop);
  1288   transform_later(result_phi_rawmem);
  1289   transform_later(result_phi_i_o);
  1290   // This completes all paths into the result merge point
  1294 // Helper for PhaseMacroExpand::expand_allocate_common.
  1295 // Initializes the newly-allocated storage.
  1296 Node*
  1297 PhaseMacroExpand::initialize_object(AllocateNode* alloc,
  1298                                     Node* control, Node* rawmem, Node* object,
  1299                                     Node* klass_node, Node* length,
  1300                                     Node* size_in_bytes) {
  1301   InitializeNode* init = alloc->initialization();
  1302   // Store the klass & mark bits
  1303   Node* mark_node = NULL;
  1304   // For now only enable fast locking for non-array types
  1305   if (UseBiasedLocking && (length == NULL)) {
  1306     mark_node = make_load(NULL, rawmem, klass_node, Klass::prototype_header_offset_in_bytes() + sizeof(oopDesc), TypeRawPtr::BOTTOM, T_ADDRESS);
  1307   } else {
  1308     mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype()));
  1310   rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
  1312   rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_OBJECT);
  1313   int header_size = alloc->minimum_header_size();  // conservatively small
  1315   // Array length
  1316   if (length != NULL) {         // Arrays need length field
  1317     rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
  1318     // conservatively small header size:
  1319     header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
  1320     ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
  1321     if (k->is_array_klass())    // we know the exact header size in most cases:
  1322       header_size = Klass::layout_helper_header_size(k->layout_helper());
  1325   // Clear the object body, if necessary.
  1326   if (init == NULL) {
  1327     // The init has somehow disappeared; be cautious and clear everything.
  1328     //
  1329     // This can happen if a node is allocated but an uncommon trap occurs
  1330     // immediately.  In this case, the Initialize gets associated with the
  1331     // trap, and may be placed in a different (outer) loop, if the Allocate
  1332     // is in a loop.  If (this is rare) the inner loop gets unrolled, then
  1333     // there can be two Allocates to one Initialize.  The answer in all these
  1334     // edge cases is safety first.  It is always safe to clear immediately
  1335     // within an Allocate, and then (maybe or maybe not) clear some more later.
  1336     if (!ZeroTLAB)
  1337       rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
  1338                                             header_size, size_in_bytes,
  1339                                             &_igvn);
  1340   } else {
  1341     if (!init->is_complete()) {
  1342       // Try to win by zeroing only what the init does not store.
  1343       // We can also try to do some peephole optimizations,
  1344       // such as combining some adjacent subword stores.
  1345       rawmem = init->complete_stores(control, rawmem, object,
  1346                                      header_size, size_in_bytes, &_igvn);
  1348     // We have no more use for this link, since the AllocateNode goes away:
  1349     init->set_req(InitializeNode::RawAddress, top());
  1350     // (If we keep the link, it just confuses the register allocator,
  1351     // who thinks he sees a real use of the address by the membar.)
  1354   return rawmem;
  1357 // Generate prefetch instructions for next allocations.
  1358 Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false,
  1359                                         Node*& contended_phi_rawmem,
  1360                                         Node* old_eden_top, Node* new_eden_top,
  1361                                         Node* length) {
  1362    if( UseTLAB && AllocatePrefetchStyle == 2 ) {
  1363       // Generate prefetch allocation with watermark check.
  1364       // As an allocation hits the watermark, we will prefetch starting
  1365       // at a "distance" away from watermark.
  1366       enum { fall_in_path = 1, pf_path = 2 };
  1368       Node *pf_region = new (C, 3) RegionNode(3);
  1369       Node *pf_phi_rawmem = new (C, 3) PhiNode( pf_region, Type::MEMORY,
  1370                                                 TypeRawPtr::BOTTOM );
  1371       // I/O is used for Prefetch
  1372       Node *pf_phi_abio = new (C, 3) PhiNode( pf_region, Type::ABIO );
  1374       Node *thread = new (C, 1) ThreadLocalNode();
  1375       transform_later(thread);
  1377       Node *eden_pf_adr = new (C, 4) AddPNode( top()/*not oop*/, thread,
  1378                    _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) );
  1379       transform_later(eden_pf_adr);
  1381       Node *old_pf_wm = new (C, 3) LoadPNode( needgc_false,
  1382                                    contended_phi_rawmem, eden_pf_adr,
  1383                                    TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM );
  1384       transform_later(old_pf_wm);
  1386       // check against new_eden_top
  1387       Node *need_pf_cmp = new (C, 3) CmpPNode( new_eden_top, old_pf_wm );
  1388       transform_later(need_pf_cmp);
  1389       Node *need_pf_bol = new (C, 2) BoolNode( need_pf_cmp, BoolTest::ge );
  1390       transform_later(need_pf_bol);
  1391       IfNode *need_pf_iff = new (C, 2) IfNode( needgc_false, need_pf_bol,
  1392                                        PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
  1393       transform_later(need_pf_iff);
  1395       // true node, add prefetchdistance
  1396       Node *need_pf_true = new (C, 1) IfTrueNode( need_pf_iff );
  1397       transform_later(need_pf_true);
  1399       Node *need_pf_false = new (C, 1) IfFalseNode( need_pf_iff );
  1400       transform_later(need_pf_false);
  1402       Node *new_pf_wmt = new (C, 4) AddPNode( top(), old_pf_wm,
  1403                                     _igvn.MakeConX(AllocatePrefetchDistance) );
  1404       transform_later(new_pf_wmt );
  1405       new_pf_wmt->set_req(0, need_pf_true);
  1407       Node *store_new_wmt = new (C, 4) StorePNode( need_pf_true,
  1408                                        contended_phi_rawmem, eden_pf_adr,
  1409                                        TypeRawPtr::BOTTOM, new_pf_wmt );
  1410       transform_later(store_new_wmt);
  1412       // adding prefetches
  1413       pf_phi_abio->init_req( fall_in_path, i_o );
  1415       Node *prefetch_adr;
  1416       Node *prefetch;
  1417       uint lines = AllocatePrefetchDistance / AllocatePrefetchStepSize;
  1418       uint step_size = AllocatePrefetchStepSize;
  1419       uint distance = 0;
  1421       for ( uint i = 0; i < lines; i++ ) {
  1422         prefetch_adr = new (C, 4) AddPNode( old_pf_wm, new_pf_wmt,
  1423                                             _igvn.MakeConX(distance) );
  1424         transform_later(prefetch_adr);
  1425         prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
  1426         transform_later(prefetch);
  1427         distance += step_size;
  1428         i_o = prefetch;
  1430       pf_phi_abio->set_req( pf_path, i_o );
  1432       pf_region->init_req( fall_in_path, need_pf_false );
  1433       pf_region->init_req( pf_path, need_pf_true );
  1435       pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem );
  1436       pf_phi_rawmem->init_req( pf_path, store_new_wmt );
  1438       transform_later(pf_region);
  1439       transform_later(pf_phi_rawmem);
  1440       transform_later(pf_phi_abio);
  1442       needgc_false = pf_region;
  1443       contended_phi_rawmem = pf_phi_rawmem;
  1444       i_o = pf_phi_abio;
  1445    } else if( AllocatePrefetchStyle > 0 ) {
  1446       // Insert a prefetch for each allocation only on the fast-path
  1447       Node *prefetch_adr;
  1448       Node *prefetch;
  1449       // Generate several prefetch instructions only for arrays.
  1450       uint lines = (length != NULL) ? AllocatePrefetchLines : 1;
  1451       uint step_size = AllocatePrefetchStepSize;
  1452       uint distance = AllocatePrefetchDistance;
  1453       for ( uint i = 0; i < lines; i++ ) {
  1454         prefetch_adr = new (C, 4) AddPNode( old_eden_top, new_eden_top,
  1455                                             _igvn.MakeConX(distance) );
  1456         transform_later(prefetch_adr);
  1457         prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
  1458         // Do not let it float too high, since if eden_top == eden_end,
  1459         // both might be null.
  1460         if( i == 0 ) { // Set control for first prefetch, next follows it
  1461           prefetch->init_req(0, needgc_false);
  1463         transform_later(prefetch);
  1464         distance += step_size;
  1465         i_o = prefetch;
  1468    return i_o;
  1472 void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) {
  1473   expand_allocate_common(alloc, NULL,
  1474                          OptoRuntime::new_instance_Type(),
  1475                          OptoRuntime::new_instance_Java());
  1478 void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) {
  1479   Node* length = alloc->in(AllocateNode::ALength);
  1480   expand_allocate_common(alloc, length,
  1481                          OptoRuntime::new_array_Type(),
  1482                          OptoRuntime::new_array_Java());
  1486 // we have determined that this lock/unlock can be eliminated, we simply
  1487 // eliminate the node without expanding it.
  1488 //
  1489 // Note:  The membar's associated with the lock/unlock are currently not
  1490 //        eliminated.  This should be investigated as a future enhancement.
  1491 //
  1492 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
  1494   if (!alock->is_eliminated()) {
  1495     return false;
  1497   // Mark the box lock as eliminated if all correspondent locks are eliminated
  1498   // to construct correct debug info.
  1499   BoxLockNode* box = alock->box_node()->as_BoxLock();
  1500   if (!box->is_eliminated()) {
  1501     bool eliminate = true;
  1502     for (DUIterator_Fast imax, i = box->fast_outs(imax); i < imax; i++) {
  1503       Node *lck = box->fast_out(i);
  1504       if (lck->is_Lock() && !lck->as_AbstractLock()->is_eliminated()) {
  1505         eliminate = false;
  1506         break;
  1509     if (eliminate)
  1510       box->set_eliminated();
  1513   #ifndef PRODUCT
  1514   if (PrintEliminateLocks) {
  1515     if (alock->is_Lock()) {
  1516       tty->print_cr("++++ Eliminating: %d Lock", alock->_idx);
  1517     } else {
  1518       tty->print_cr("++++ Eliminating: %d Unlock", alock->_idx);
  1521   #endif
  1523   Node* mem  = alock->in(TypeFunc::Memory);
  1524   Node* ctrl = alock->in(TypeFunc::Control);
  1526   extract_call_projections(alock);
  1527   // There are 2 projections from the lock.  The lock node will
  1528   // be deleted when its last use is subsumed below.
  1529   assert(alock->outcnt() == 2 &&
  1530          _fallthroughproj != NULL &&
  1531          _memproj_fallthrough != NULL,
  1532          "Unexpected projections from Lock/Unlock");
  1534   Node* fallthroughproj = _fallthroughproj;
  1535   Node* memproj_fallthrough = _memproj_fallthrough;
  1537   // The memory projection from a lock/unlock is RawMem
  1538   // The input to a Lock is merged memory, so extract its RawMem input
  1539   // (unless the MergeMem has been optimized away.)
  1540   if (alock->is_Lock()) {
  1541     // Seach for MemBarAcquire node and delete it also.
  1542     MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
  1543     assert(membar != NULL && membar->Opcode() == Op_MemBarAcquire, "");
  1544     Node* ctrlproj = membar->proj_out(TypeFunc::Control);
  1545     Node* memproj = membar->proj_out(TypeFunc::Memory);
  1546     _igvn.hash_delete(ctrlproj);
  1547     _igvn.subsume_node(ctrlproj, fallthroughproj);
  1548     _igvn.hash_delete(memproj);
  1549     _igvn.subsume_node(memproj, memproj_fallthrough);
  1552   // Seach for MemBarRelease node and delete it also.
  1553   if (alock->is_Unlock() && ctrl != NULL && ctrl->is_Proj() &&
  1554       ctrl->in(0)->is_MemBar()) {
  1555     MemBarNode* membar = ctrl->in(0)->as_MemBar();
  1556     assert(membar->Opcode() == Op_MemBarRelease &&
  1557            mem->is_Proj() && membar == mem->in(0), "");
  1558     _igvn.hash_delete(fallthroughproj);
  1559     _igvn.subsume_node(fallthroughproj, ctrl);
  1560     _igvn.hash_delete(memproj_fallthrough);
  1561     _igvn.subsume_node(memproj_fallthrough, mem);
  1562     fallthroughproj = ctrl;
  1563     memproj_fallthrough = mem;
  1564     ctrl = membar->in(TypeFunc::Control);
  1565     mem  = membar->in(TypeFunc::Memory);
  1568   _igvn.hash_delete(fallthroughproj);
  1569   _igvn.subsume_node(fallthroughproj, ctrl);
  1570   _igvn.hash_delete(memproj_fallthrough);
  1571   _igvn.subsume_node(memproj_fallthrough, mem);
  1572   return true;
  1576 //------------------------------expand_lock_node----------------------
  1577 void PhaseMacroExpand::expand_lock_node(LockNode *lock) {
  1579   Node* ctrl = lock->in(TypeFunc::Control);
  1580   Node* mem = lock->in(TypeFunc::Memory);
  1581   Node* obj = lock->obj_node();
  1582   Node* box = lock->box_node();
  1583   Node* flock = lock->fastlock_node();
  1585   // Make the merge point
  1586   Node *region = new (C, 3) RegionNode(3);
  1588   Node *bol = transform_later(new (C, 2) BoolNode(flock,BoolTest::ne));
  1589   Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
  1590   // Optimize test; set region slot 2
  1591   Node *slow_path = opt_iff(region,iff);
  1593   // Make slow path call
  1594   CallNode *call = make_slow_call( (CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, obj, box );
  1596   extract_call_projections(call);
  1598   // Slow path can only throw asynchronous exceptions, which are always
  1599   // de-opted.  So the compiler thinks the slow-call can never throw an
  1600   // exception.  If it DOES throw an exception we would need the debug
  1601   // info removed first (since if it throws there is no monitor).
  1602   assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
  1603            _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
  1605   // Capture slow path
  1606   // disconnect fall-through projection from call and create a new one
  1607   // hook up users of fall-through projection to region
  1608   Node *slow_ctrl = _fallthroughproj->clone();
  1609   transform_later(slow_ctrl);
  1610   _igvn.hash_delete(_fallthroughproj);
  1611   _fallthroughproj->disconnect_inputs(NULL);
  1612   region->init_req(1, slow_ctrl);
  1613   // region inputs are now complete
  1614   transform_later(region);
  1615   _igvn.subsume_node(_fallthroughproj, region);
  1617   // create a Phi for the memory state
  1618   Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1619   Node *memproj = transform_later( new (C, 1) ProjNode(call, TypeFunc::Memory) );
  1620   mem_phi->init_req(1, memproj );
  1621   mem_phi->init_req(2, mem);
  1622   transform_later(mem_phi);
  1623     _igvn.hash_delete(_memproj_fallthrough);
  1624   _igvn.subsume_node(_memproj_fallthrough, mem_phi);
  1629 //------------------------------expand_unlock_node----------------------
  1630 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
  1632   Node* ctrl = unlock->in(TypeFunc::Control);
  1633   Node* mem = unlock->in(TypeFunc::Memory);
  1634   Node* obj = unlock->obj_node();
  1635   Node* box = unlock->box_node();
  1637   // No need for a null check on unlock
  1639   // Make the merge point
  1640   RegionNode *region = new (C, 3) RegionNode(3);
  1642   FastUnlockNode *funlock = new (C, 3) FastUnlockNode( ctrl, obj, box );
  1643   funlock = transform_later( funlock )->as_FastUnlock();
  1644   Node *bol = transform_later(new (C, 2) BoolNode(funlock,BoolTest::ne));
  1645   Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
  1646   // Optimize test; set region slot 2
  1647   Node *slow_path = opt_iff(region,iff);
  1649   CallNode *call = make_slow_call( (CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), "complete_monitor_unlocking_C", slow_path, obj, box );
  1651   extract_call_projections(call);
  1653   assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
  1654            _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
  1656   // No exceptions for unlocking
  1657   // Capture slow path
  1658   // disconnect fall-through projection from call and create a new one
  1659   // hook up users of fall-through projection to region
  1660   Node *slow_ctrl = _fallthroughproj->clone();
  1661   transform_later(slow_ctrl);
  1662   _igvn.hash_delete(_fallthroughproj);
  1663   _fallthroughproj->disconnect_inputs(NULL);
  1664   region->init_req(1, slow_ctrl);
  1665   // region inputs are now complete
  1666   transform_later(region);
  1667   _igvn.subsume_node(_fallthroughproj, region);
  1669   // create a Phi for the memory state
  1670   Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1671   Node *memproj = transform_later( new(C, 1) ProjNode(call, TypeFunc::Memory) );
  1672   mem_phi->init_req(1, memproj );
  1673   mem_phi->init_req(2, mem);
  1674   transform_later(mem_phi);
  1675     _igvn.hash_delete(_memproj_fallthrough);
  1676   _igvn.subsume_node(_memproj_fallthrough, mem_phi);
  1681 //------------------------------expand_macro_nodes----------------------
  1682 //  Returns true if a failure occurred.
  1683 bool PhaseMacroExpand::expand_macro_nodes() {
  1684   if (C->macro_count() == 0)
  1685     return false;
  1686   // attempt to eliminate allocations
  1687   bool progress = true;
  1688   while (progress) {
  1689     progress = false;
  1690     for (int i = C->macro_count(); i > 0; i--) {
  1691       Node * n = C->macro_node(i-1);
  1692       bool success = false;
  1693       debug_only(int old_macro_count = C->macro_count(););
  1694       switch (n->class_id()) {
  1695       case Node::Class_Allocate:
  1696       case Node::Class_AllocateArray:
  1697         success = eliminate_allocate_node(n->as_Allocate());
  1698         break;
  1699       case Node::Class_Lock:
  1700       case Node::Class_Unlock:
  1701         success = eliminate_locking_node(n->as_AbstractLock());
  1702         break;
  1703       default:
  1704         if (n->Opcode() == Op_Opaque1 || n->Opcode() == Op_Opaque2) {
  1705           _igvn.add_users_to_worklist(n);
  1706           _igvn.hash_delete(n);
  1707           _igvn.subsume_node(n, n->in(1));
  1708           success = true;
  1709         } else {
  1710           assert(false, "unknown node type in macro list");
  1713       assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
  1714       progress = progress || success;
  1717   // Make sure expansion will not cause node limit to be exceeded.
  1718   // Worst case is a macro node gets expanded into about 50 nodes.
  1719   // Allow 50% more for optimization.
  1720   if (C->check_node_count(C->macro_count() * 75, "out of nodes before macro expansion" ) )
  1721     return true;
  1723   // expand "macro" nodes
  1724   // nodes are removed from the macro list as they are processed
  1725   while (C->macro_count() > 0) {
  1726     int macro_count = C->macro_count();
  1727     Node * n = C->macro_node(macro_count-1);
  1728     assert(n->is_macro(), "only macro nodes expected here");
  1729     if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) {
  1730       // node is unreachable, so don't try to expand it
  1731       C->remove_macro_node(n);
  1732       continue;
  1734     switch (n->class_id()) {
  1735     case Node::Class_Allocate:
  1736       expand_allocate(n->as_Allocate());
  1737       break;
  1738     case Node::Class_AllocateArray:
  1739       expand_allocate_array(n->as_AllocateArray());
  1740       break;
  1741     case Node::Class_Lock:
  1742       expand_lock_node(n->as_Lock());
  1743       break;
  1744     case Node::Class_Unlock:
  1745       expand_unlock_node(n->as_Unlock());
  1746       break;
  1747     default:
  1748       assert(false, "unknown node type in macro list");
  1750     assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
  1751     if (C->failing())  return true;
  1754   _igvn.set_delay_transform(false);
  1755   _igvn.optimize();
  1756   return false;

mercurial