src/share/vm/opto/macro.cpp

Wed, 23 Apr 2008 11:20:36 -0700

author
kvn
date
Wed, 23 Apr 2008 11:20:36 -0700
changeset 559
b130b98db9cf
parent 548
ba764ed4b6f2
child 598
885ed790ecf0
child 777
37f87013dfd8
permissions
-rw-r--r--

6689060: Escape Analysis does not work with Compressed Oops
Summary: 64-bits VM crashes with -XX:+AggresiveOpts (Escape Analysis + Compressed Oops)
Reviewed-by: never, sgoldman

     1 /*
     2  * Copyright 2005-2007 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) {
   198   Node *orig_mem = mem;
   199   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   200   while (true) {
   201     if (mem == alloc_mem || mem == start_mem ) {
   202       return mem;  // hit one of our sentinals
   203     } else if (mem->is_MergeMem()) {
   204       mem = mem->as_MergeMem()->memory_at(alias_idx);
   205     } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
   206       Node *in = mem->in(0);
   207       // we can safely skip over safepoints, calls, locks and membars because we
   208       // already know that the object is safe to eliminate.
   209       if (in->is_Initialize() && in->as_Initialize()->allocation() == alloc) {
   210         return in;
   211       } else if (in->is_Call() || in->is_MemBar()) {
   212         mem = in->in(TypeFunc::Memory);
   213       } else {
   214         assert(false, "unexpected projection");
   215       }
   216     } else if (mem->is_Store()) {
   217       const TypePtr* atype = mem->as_Store()->adr_type();
   218       int adr_idx = Compile::current()->get_alias_index(atype);
   219       if (adr_idx == alias_idx) {
   220         assert(atype->isa_oopptr(), "address type must be oopptr");
   221         int adr_offset = atype->offset();
   222         uint adr_iid = atype->is_oopptr()->instance_id();
   223         // Array elements references have the same alias_idx
   224         // but different offset and different instance_id.
   225         if (adr_offset == offset && adr_iid == alloc->_idx)
   226           return mem;
   227       } else {
   228         assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
   229       }
   230       mem = mem->in(MemNode::Memory);
   231     } else {
   232       return mem;
   233     }
   234     if (mem == orig_mem)
   235       return mem;
   236   }
   237 }
   239 //
   240 // Given a Memory Phi, compute a value Phi containing the values from stores
   241 // on the input paths.
   242 // Note: this function is recursive, its depth is limied by the "level" argument
   243 // Returns the computed Phi, or NULL if it cannot compute it.
   244 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, Node *alloc, int level) {
   246   if (level <= 0) {
   247     return NULL;
   248   }
   249   int alias_idx = C->get_alias_index(adr_t);
   250   int offset = adr_t->offset();
   251   int instance_id = adr_t->instance_id();
   253   Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
   254   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   256   uint length = mem->req();
   257   GrowableArray <Node *> values(length, length, NULL);
   259   for (uint j = 1; j < length; j++) {
   260     Node *in = mem->in(j);
   261     if (in == NULL || in->is_top()) {
   262       values.at_put(j, in);
   263     } else  {
   264       Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc);
   265       if (val == start_mem || val == alloc_mem) {
   266         // hit a sentinel, return appropriate 0 value
   267         values.at_put(j, _igvn.zerocon(ft));
   268         continue;
   269       }
   270       if (val->is_Initialize()) {
   271         val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
   272       }
   273       if (val == NULL) {
   274         return NULL;  // can't find a value on this path
   275       }
   276       if (val == mem) {
   277         values.at_put(j, mem);
   278       } else if (val->is_Store()) {
   279         values.at_put(j, val->in(MemNode::ValueIn));
   280       } else if(val->is_Proj() && val->in(0) == alloc) {
   281         values.at_put(j, _igvn.zerocon(ft));
   282       } else if (val->is_Phi()) {
   283         // Check if an appropriate node already exists.
   284         Node* region = val->in(0);
   285         Node* old_phi = NULL;
   286         for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
   287           Node* phi = region->fast_out(k);
   288           if (phi->is_Phi() && phi != val &&
   289               phi->as_Phi()->is_same_inst_field(phi_type, instance_id, alias_idx, offset)) {
   290             old_phi = phi;
   291             break;
   292           }
   293         }
   294         if (old_phi == NULL) {
   295           val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, level-1);
   296           if (val == NULL) {
   297             return NULL;
   298           }
   299           values.at_put(j, val);
   300         } else {
   301           values.at_put(j, old_phi);
   302         }
   303       } else {
   304         return NULL;  // unknown node  on this path
   305       }
   306     }
   307   }
   308   // create a new Phi for the value
   309   PhiNode *phi = new (C, length) PhiNode(mem->in(0), phi_type, NULL, instance_id, alias_idx, offset);
   310   for (uint j = 1; j < length; j++) {
   311     if (values.at(j) == mem) {
   312       phi->init_req(j, phi);
   313     } else {
   314       phi->init_req(j, values.at(j));
   315     }
   316   }
   317   transform_later(phi);
   318   return phi;
   319 }
   321 // Search the last value stored into the object's field.
   322 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, Node *alloc) {
   323   assert(adr_t->is_instance_field(), "instance required");
   324   uint instance_id = adr_t->instance_id();
   325   assert(instance_id == alloc->_idx, "wrong allocation");
   327   int alias_idx = C->get_alias_index(adr_t);
   328   int offset = adr_t->offset();
   329   Node *start_mem = C->start()->proj_out(TypeFunc::Memory);
   330   Node *alloc_ctrl = alloc->in(TypeFunc::Control);
   331   Node *alloc_mem = alloc->in(TypeFunc::Memory);
   332   VectorSet visited(Thread::current()->resource_area());
   335   bool done = sfpt_mem == alloc_mem;
   336   Node *mem = sfpt_mem;
   337   while (!done) {
   338     if (visited.test_set(mem->_idx)) {
   339       return NULL;  // found a loop, give up
   340     }
   341     mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc);
   342     if (mem == start_mem || mem == alloc_mem) {
   343       done = true;  // hit a sentinel, return appropriate 0 value
   344     } else if (mem->is_Initialize()) {
   345       mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
   346       if (mem == NULL) {
   347         done = true; // Something go wrong.
   348       } else if (mem->is_Store()) {
   349         const TypePtr* atype = mem->as_Store()->adr_type();
   350         assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
   351         done = true;
   352       }
   353     } else if (mem->is_Store()) {
   354       const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
   355       assert(atype != NULL, "address type must be oopptr");
   356       assert(C->get_alias_index(atype) == alias_idx &&
   357              atype->is_instance_field() && atype->offset() == offset &&
   358              atype->instance_id() == instance_id, "store is correct memory slice");
   359       done = true;
   360     } else if (mem->is_Phi()) {
   361       // try to find a phi's unique input
   362       Node *unique_input = NULL;
   363       Node *top = C->top();
   364       for (uint i = 1; i < mem->req(); i++) {
   365         Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc);
   366         if (n == NULL || n == top || n == mem) {
   367           continue;
   368         } else if (unique_input == NULL) {
   369           unique_input = n;
   370         } else if (unique_input != n) {
   371           unique_input = top;
   372           break;
   373         }
   374       }
   375       if (unique_input != NULL && unique_input != top) {
   376         mem = unique_input;
   377       } else {
   378         done = true;
   379       }
   380     } else {
   381       assert(false, "unexpected node");
   382     }
   383   }
   384   if (mem != NULL) {
   385     if (mem == start_mem || mem == alloc_mem) {
   386       // hit a sentinel, return appropriate 0 value
   387       return _igvn.zerocon(ft);
   388     } else if (mem->is_Store()) {
   389       return mem->in(MemNode::ValueIn);
   390     } else if (mem->is_Phi()) {
   391       // attempt to produce a Phi reflecting the values on the input paths of the Phi
   392       Node * phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, 8);
   393       if (phi != NULL) {
   394         return phi;
   395       }
   396     }
   397   }
   398   // Something go wrong.
   399   return NULL;
   400 }
   402 // Check the possibility of scalar replacement.
   403 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
   404   //  Scan the uses of the allocation to check for anything that would
   405   //  prevent us from eliminating it.
   406   NOT_PRODUCT( const char* fail_eliminate = NULL; )
   407   DEBUG_ONLY( Node* disq_node = NULL; )
   408   bool  can_eliminate = true;
   410   Node* res = alloc->result_cast();
   411   const TypeOopPtr* res_type = NULL;
   412   if (res == NULL) {
   413     // All users were eliminated.
   414   } else if (!res->is_CheckCastPP()) {
   415     alloc->_is_scalar_replaceable = false;  // don't try again
   416     NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
   417     can_eliminate = false;
   418   } else {
   419     res_type = _igvn.type(res)->isa_oopptr();
   420     if (res_type == NULL) {
   421       NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
   422       can_eliminate = false;
   423     } else if (res_type->isa_aryptr()) {
   424       int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
   425       if (length < 0) {
   426         NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
   427         can_eliminate = false;
   428       }
   429     }
   430   }
   432   if (can_eliminate && res != NULL) {
   433     for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
   434                                j < jmax && can_eliminate; j++) {
   435       Node* use = res->fast_out(j);
   437       if (use->is_AddP()) {
   438         const TypePtr* addp_type = _igvn.type(use)->is_ptr();
   439         int offset = addp_type->offset();
   441         if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
   442           NOT_PRODUCT(fail_eliminate = "Undefined field referrence";)
   443           can_eliminate = false;
   444           break;
   445         }
   446         for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
   447                                    k < kmax && can_eliminate; k++) {
   448           Node* n = use->fast_out(k);
   449           if (!n->is_Store() && n->Opcode() != Op_CastP2X) {
   450             DEBUG_ONLY(disq_node = n;)
   451             if (n->is_Load()) {
   452               NOT_PRODUCT(fail_eliminate = "Field load";)
   453             } else {
   454               NOT_PRODUCT(fail_eliminate = "Not store field referrence";)
   455             }
   456             can_eliminate = false;
   457           }
   458         }
   459       } else if (use->is_SafePoint()) {
   460         SafePointNode* sfpt = use->as_SafePoint();
   461         if (sfpt->has_non_debug_use(res)) {
   462           // Object is passed as argument.
   463           DEBUG_ONLY(disq_node = use;)
   464           NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
   465           can_eliminate = false;
   466         }
   467         Node* sfptMem = sfpt->memory();
   468         if (sfptMem == NULL || sfptMem->is_top()) {
   469           DEBUG_ONLY(disq_node = use;)
   470           NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
   471           can_eliminate = false;
   472         } else {
   473           safepoints.append_if_missing(sfpt);
   474         }
   475       } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
   476         if (use->is_Phi()) {
   477           if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
   478             NOT_PRODUCT(fail_eliminate = "Object is return value";)
   479           } else {
   480             NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
   481           }
   482           DEBUG_ONLY(disq_node = use;)
   483         } else {
   484           if (use->Opcode() == Op_Return) {
   485             NOT_PRODUCT(fail_eliminate = "Object is return value";)
   486           }else {
   487             NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
   488           }
   489           DEBUG_ONLY(disq_node = use;)
   490         }
   491         can_eliminate = false;
   492       }
   493     }
   494   }
   496 #ifndef PRODUCT
   497   if (PrintEliminateAllocations) {
   498     if (can_eliminate) {
   499       tty->print("Scalar ");
   500       if (res == NULL)
   501         alloc->dump();
   502       else
   503         res->dump();
   504     } else {
   505       tty->print("NotScalar (%s)", fail_eliminate);
   506       if (res == NULL)
   507         alloc->dump();
   508       else
   509         res->dump();
   510 #ifdef ASSERT
   511       if (disq_node != NULL) {
   512           tty->print("  >>>> ");
   513           disq_node->dump();
   514       }
   515 #endif /*ASSERT*/
   516     }
   517   }
   518 #endif
   519   return can_eliminate;
   520 }
   522 // Do scalar replacement.
   523 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
   524   GrowableArray <SafePointNode *> safepoints_done;
   526   ciKlass* klass = NULL;
   527   ciInstanceKlass* iklass = NULL;
   528   int nfields = 0;
   529   int array_base;
   530   int element_size;
   531   BasicType basic_elem_type;
   532   ciType* elem_type;
   534   Node* res = alloc->result_cast();
   535   const TypeOopPtr* res_type = NULL;
   536   if (res != NULL) { // Could be NULL when there are no users
   537     res_type = _igvn.type(res)->isa_oopptr();
   538   }
   540   if (res != NULL) {
   541     klass = res_type->klass();
   542     if (res_type->isa_instptr()) {
   543       // find the fields of the class which will be needed for safepoint debug information
   544       assert(klass->is_instance_klass(), "must be an instance klass.");
   545       iklass = klass->as_instance_klass();
   546       nfields = iklass->nof_nonstatic_fields();
   547     } else {
   548       // find the array's elements which will be needed for safepoint debug information
   549       nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
   550       assert(klass->is_array_klass() && nfields >= 0, "must be an array klass.");
   551       elem_type = klass->as_array_klass()->element_type();
   552       basic_elem_type = elem_type->basic_type();
   553       array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
   554       element_size = type2aelembytes(basic_elem_type);
   555     }
   556   }
   557   //
   558   // Process the safepoint uses
   559   //
   560   while (safepoints.length() > 0) {
   561     SafePointNode* sfpt = safepoints.pop();
   562     Node* mem = sfpt->memory();
   563     uint first_ind = sfpt->req();
   564     SafePointScalarObjectNode* sobj = new (C, 1) SafePointScalarObjectNode(res_type,
   565 #ifdef ASSERT
   566                                                  alloc,
   567 #endif
   568                                                  first_ind, nfields);
   569     sobj->init_req(0, sfpt->in(TypeFunc::Control));
   570     transform_later(sobj);
   572     // Scan object's fields adding an input to the safepoint for each field.
   573     for (int j = 0; j < nfields; j++) {
   574       int offset;
   575       ciField* field = NULL;
   576       if (iklass != NULL) {
   577         field = iklass->nonstatic_field_at(j);
   578         offset = field->offset();
   579         elem_type = field->type();
   580         basic_elem_type = field->layout_type();
   581       } else {
   582         offset = array_base + j * element_size;
   583       }
   585       const Type *field_type;
   586       // The next code is taken from Parse::do_get_xxx().
   587       if (basic_elem_type == T_OBJECT || basic_elem_type == T_ARRAY) {
   588         if (!elem_type->is_loaded()) {
   589           field_type = TypeInstPtr::BOTTOM;
   590         } else if (field != NULL && field->is_constant()) {
   591           // This can happen if the constant oop is non-perm.
   592           ciObject* con = field->constant_value().as_object();
   593           // Do not "join" in the previous type; it doesn't add value,
   594           // and may yield a vacuous result if the field is of interface type.
   595           field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
   596           assert(field_type != NULL, "field singleton type must be consistent");
   597         } else {
   598           field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
   599         }
   600         if (UseCompressedOops) {
   601           field_type = field_type->is_oopptr()->make_narrowoop();
   602           basic_elem_type = T_NARROWOOP;
   603         }
   604       } else {
   605         field_type = Type::get_const_basic_type(basic_elem_type);
   606       }
   608       const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
   610       Node *field_val = value_from_mem(mem, basic_elem_type, field_type, field_addr_type, alloc);
   611       if (field_val == NULL) {
   612         // we weren't able to find a value for this field,
   613         // give up on eliminating this allocation
   614         alloc->_is_scalar_replaceable = false;  // don't try again
   615         // remove any extra entries we added to the safepoint
   616         uint last = sfpt->req() - 1;
   617         for (int k = 0;  k < j; k++) {
   618           sfpt->del_req(last--);
   619         }
   620         // rollback processed safepoints
   621         while (safepoints_done.length() > 0) {
   622           SafePointNode* sfpt_done = safepoints_done.pop();
   623           // remove any extra entries we added to the safepoint
   624           last = sfpt_done->req() - 1;
   625           for (int k = 0;  k < nfields; k++) {
   626             sfpt_done->del_req(last--);
   627           }
   628           JVMState *jvms = sfpt_done->jvms();
   629           jvms->set_endoff(sfpt_done->req());
   630           // Now make a pass over the debug information replacing any references
   631           // to SafePointScalarObjectNode with the allocated object.
   632           int start = jvms->debug_start();
   633           int end   = jvms->debug_end();
   634           for (int i = start; i < end; i++) {
   635             if (sfpt_done->in(i)->is_SafePointScalarObject()) {
   636               SafePointScalarObjectNode* scobj = sfpt_done->in(i)->as_SafePointScalarObject();
   637               if (scobj->first_index() == sfpt_done->req() &&
   638                   scobj->n_fields() == (uint)nfields) {
   639                 assert(scobj->alloc() == alloc, "sanity");
   640                 sfpt_done->set_req(i, res);
   641               }
   642             }
   643           }
   644         }
   645 #ifndef PRODUCT
   646         if (PrintEliminateAllocations) {
   647           if (field != NULL) {
   648             tty->print("=== At SafePoint node %d can't find value of Field: ",
   649                        sfpt->_idx);
   650             field->print();
   651             int field_idx = C->get_alias_index(field_addr_type);
   652             tty->print(" (alias_idx=%d)", field_idx);
   653           } else { // Array's element
   654             tty->print("=== At SafePoint node %d can't find value of array element [%d]",
   655                        sfpt->_idx, j);
   656           }
   657           tty->print(", which prevents elimination of: ");
   658           if (res == NULL)
   659             alloc->dump();
   660           else
   661             res->dump();
   662         }
   663 #endif
   664         return false;
   665       }
   666       if (UseCompressedOops && field_type->isa_narrowoop()) {
   667         // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
   668         // to be able scalar replace the allocation.
   669         _igvn.set_delay_transform(false);
   670         field_val = DecodeNNode::decode(&_igvn, field_val);
   671         _igvn.set_delay_transform(true);
   672       }
   673       sfpt->add_req(field_val);
   674     }
   675     JVMState *jvms = sfpt->jvms();
   676     jvms->set_endoff(sfpt->req());
   677     // Now make a pass over the debug information replacing any references
   678     // to the allocated object with "sobj"
   679     int start = jvms->debug_start();
   680     int end   = jvms->debug_end();
   681     for (int i = start; i < end; i++) {
   682       if (sfpt->in(i) == res) {
   683         sfpt->set_req(i, sobj);
   684       }
   685     }
   686     safepoints_done.append_if_missing(sfpt); // keep it for rollback
   687   }
   688   return true;
   689 }
   691 // Process users of eliminated allocation.
   692 void PhaseMacroExpand::process_users_of_allocation(AllocateNode *alloc) {
   693   Node* res = alloc->result_cast();
   694   if (res != NULL) {
   695     for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
   696       Node *use = res->last_out(j);
   697       uint oc1 = res->outcnt();
   699       if (use->is_AddP()) {
   700         for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
   701           Node *n = use->last_out(k);
   702           uint oc2 = use->outcnt();
   703           if (n->is_Store()) {
   704             _igvn.replace_node(n, n->in(MemNode::Memory));
   705           } else {
   706             assert( n->Opcode() == Op_CastP2X, "CastP2X required");
   707             eliminate_card_mark(n);
   708           }
   709           k -= (oc2 - use->outcnt());
   710         }
   711       } else {
   712         assert( !use->is_SafePoint(), "safepoint uses must have been already elimiated");
   713         assert( use->Opcode() == Op_CastP2X, "CastP2X required");
   714         eliminate_card_mark(use);
   715       }
   716       j -= (oc1 - res->outcnt());
   717     }
   718     assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
   719     _igvn.remove_dead_node(res);
   720   }
   722   //
   723   // Process other users of allocation's projections
   724   //
   725   if (_resproj != NULL && _resproj->outcnt() != 0) {
   726     for (DUIterator_Last jmin, j = _resproj->last_outs(jmin); j >= jmin; ) {
   727       Node *use = _resproj->last_out(j);
   728       uint oc1 = _resproj->outcnt();
   729       if (use->is_Initialize()) {
   730         // Eliminate Initialize node.
   731         InitializeNode *init = use->as_Initialize();
   732         assert(init->outcnt() <= 2, "only a control and memory projection expected");
   733         Node *ctrl_proj = init->proj_out(TypeFunc::Control);
   734         if (ctrl_proj != NULL) {
   735            assert(init->in(TypeFunc::Control) == _fallthroughcatchproj, "allocation control projection");
   736           _igvn.replace_node(ctrl_proj, _fallthroughcatchproj);
   737         }
   738         Node *mem_proj = init->proj_out(TypeFunc::Memory);
   739         if (mem_proj != NULL) {
   740           Node *mem = init->in(TypeFunc::Memory);
   741 #ifdef ASSERT
   742           if (mem->is_MergeMem()) {
   743             assert(mem->in(TypeFunc::Memory) == _memproj_fallthrough, "allocation memory projection");
   744           } else {
   745             assert(mem == _memproj_fallthrough, "allocation memory projection");
   746           }
   747 #endif
   748           _igvn.replace_node(mem_proj, mem);
   749         }
   750       } else if (use->is_AddP()) {
   751         // raw memory addresses used only by the initialization
   752         _igvn.hash_delete(use);
   753         _igvn.subsume_node(use, C->top());
   754       } else  {
   755         assert(false, "only Initialize or AddP expected");
   756       }
   757       j -= (oc1 - _resproj->outcnt());
   758     }
   759   }
   760   if (_fallthroughcatchproj != NULL) {
   761     _igvn.replace_node(_fallthroughcatchproj, alloc->in(TypeFunc::Control));
   762   }
   763   if (_memproj_fallthrough != NULL) {
   764     _igvn.replace_node(_memproj_fallthrough, alloc->in(TypeFunc::Memory));
   765   }
   766   if (_memproj_catchall != NULL) {
   767     _igvn.replace_node(_memproj_catchall, C->top());
   768   }
   769   if (_ioproj_fallthrough != NULL) {
   770     _igvn.replace_node(_ioproj_fallthrough, alloc->in(TypeFunc::I_O));
   771   }
   772   if (_ioproj_catchall != NULL) {
   773     _igvn.replace_node(_ioproj_catchall, C->top());
   774   }
   775   if (_catchallcatchproj != NULL) {
   776     _igvn.replace_node(_catchallcatchproj, C->top());
   777   }
   778 }
   780 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
   782   if (!EliminateAllocations || !alloc->_is_scalar_replaceable) {
   783     return false;
   784   }
   786   extract_call_projections(alloc);
   788   GrowableArray <SafePointNode *> safepoints;
   789   if (!can_eliminate_allocation(alloc, safepoints)) {
   790     return false;
   791   }
   793   if (!scalar_replacement(alloc, safepoints)) {
   794     return false;
   795   }
   797   process_users_of_allocation(alloc);
   799 #ifndef PRODUCT
   800 if (PrintEliminateAllocations) {
   801   if (alloc->is_AllocateArray())
   802     tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
   803   else
   804     tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
   805 }
   806 #endif
   808   return true;
   809 }
   812 //---------------------------set_eden_pointers-------------------------
   813 void PhaseMacroExpand::set_eden_pointers(Node* &eden_top_adr, Node* &eden_end_adr) {
   814   if (UseTLAB) {                // Private allocation: load from TLS
   815     Node* thread = transform_later(new (C, 1) ThreadLocalNode());
   816     int tlab_top_offset = in_bytes(JavaThread::tlab_top_offset());
   817     int tlab_end_offset = in_bytes(JavaThread::tlab_end_offset());
   818     eden_top_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_top_offset);
   819     eden_end_adr = basic_plus_adr(top()/*not oop*/, thread, tlab_end_offset);
   820   } else {                      // Shared allocation: load from globals
   821     CollectedHeap* ch = Universe::heap();
   822     address top_adr = (address)ch->top_addr();
   823     address end_adr = (address)ch->end_addr();
   824     eden_top_adr = makecon(TypeRawPtr::make(top_adr));
   825     eden_end_adr = basic_plus_adr(eden_top_adr, end_adr - top_adr);
   826   }
   827 }
   830 Node* PhaseMacroExpand::make_load(Node* ctl, Node* mem, Node* base, int offset, const Type* value_type, BasicType bt) {
   831   Node* adr = basic_plus_adr(base, offset);
   832   const TypePtr* adr_type = TypeRawPtr::BOTTOM;
   833   Node* value = LoadNode::make(_igvn, ctl, mem, adr, adr_type, value_type, bt);
   834   transform_later(value);
   835   return value;
   836 }
   839 Node* PhaseMacroExpand::make_store(Node* ctl, Node* mem, Node* base, int offset, Node* value, BasicType bt) {
   840   Node* adr = basic_plus_adr(base, offset);
   841   mem = StoreNode::make(_igvn, ctl, mem, adr, NULL, value, bt);
   842   transform_later(mem);
   843   return mem;
   844 }
   846 //=============================================================================
   847 //
   848 //                              A L L O C A T I O N
   849 //
   850 // Allocation attempts to be fast in the case of frequent small objects.
   851 // It breaks down like this:
   852 //
   853 // 1) Size in doublewords is computed.  This is a constant for objects and
   854 // variable for most arrays.  Doubleword units are used to avoid size
   855 // overflow of huge doubleword arrays.  We need doublewords in the end for
   856 // rounding.
   857 //
   858 // 2) Size is checked for being 'too large'.  Too-large allocations will go
   859 // the slow path into the VM.  The slow path can throw any required
   860 // exceptions, and does all the special checks for very large arrays.  The
   861 // size test can constant-fold away for objects.  For objects with
   862 // finalizers it constant-folds the otherway: you always go slow with
   863 // finalizers.
   864 //
   865 // 3) If NOT using TLABs, this is the contended loop-back point.
   866 // Load-Locked the heap top.  If using TLABs normal-load the heap top.
   867 //
   868 // 4) Check that heap top + size*8 < max.  If we fail go the slow ` route.
   869 // NOTE: "top+size*8" cannot wrap the 4Gig line!  Here's why: for largish
   870 // "size*8" we always enter the VM, where "largish" is a constant picked small
   871 // enough that there's always space between the eden max and 4Gig (old space is
   872 // there so it's quite large) and large enough that the cost of entering the VM
   873 // is dwarfed by the cost to initialize the space.
   874 //
   875 // 5) If NOT using TLABs, Store-Conditional the adjusted heap top back
   876 // down.  If contended, repeat at step 3.  If using TLABs normal-store
   877 // adjusted heap top back down; there is no contention.
   878 //
   879 // 6) If !ZeroTLAB then Bulk-clear the object/array.  Fill in klass & mark
   880 // fields.
   881 //
   882 // 7) Merge with the slow-path; cast the raw memory pointer to the correct
   883 // oop flavor.
   884 //
   885 //=============================================================================
   886 // FastAllocateSizeLimit value is in DOUBLEWORDS.
   887 // Allocations bigger than this always go the slow route.
   888 // This value must be small enough that allocation attempts that need to
   889 // trigger exceptions go the slow route.  Also, it must be small enough so
   890 // that heap_top + size_in_bytes does not wrap around the 4Gig limit.
   891 //=============================================================================j//
   892 // %%% Here is an old comment from parseHelper.cpp; is it outdated?
   893 // The allocator will coalesce int->oop copies away.  See comment in
   894 // coalesce.cpp about how this works.  It depends critically on the exact
   895 // code shape produced here, so if you are changing this code shape
   896 // make sure the GC info for the heap-top is correct in and around the
   897 // slow-path call.
   898 //
   900 void PhaseMacroExpand::expand_allocate_common(
   901             AllocateNode* alloc, // allocation node to be expanded
   902             Node* length,  // array length for an array allocation
   903             const TypeFunc* slow_call_type, // Type of slow call
   904             address slow_call_address  // Address of slow call
   905     )
   906 {
   908   Node* ctrl = alloc->in(TypeFunc::Control);
   909   Node* mem  = alloc->in(TypeFunc::Memory);
   910   Node* i_o  = alloc->in(TypeFunc::I_O);
   911   Node* size_in_bytes     = alloc->in(AllocateNode::AllocSize);
   912   Node* klass_node        = alloc->in(AllocateNode::KlassNode);
   913   Node* initial_slow_test = alloc->in(AllocateNode::InitialTest);
   915   // With escape analysis, the entire memory state was needed to be able to
   916   // eliminate the allocation.  Since the allocations cannot be eliminated,
   917   // optimize it to the raw slice.
   918   if (mem->is_MergeMem()) {
   919     mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
   920   }
   922   Node* eden_top_adr;
   923   Node* eden_end_adr;
   924   set_eden_pointers(eden_top_adr, eden_end_adr);
   926   uint raw_idx = C->get_alias_index(TypeRawPtr::BOTTOM);
   927   assert(ctrl != NULL, "must have control");
   929   // Load Eden::end.  Loop invariant and hoisted.
   930   //
   931   // Note: We set the control input on "eden_end" and "old_eden_top" when using
   932   //       a TLAB to work around a bug where these values were being moved across
   933   //       a safepoint.  These are not oops, so they cannot be include in the oop
   934   //       map, but the can be changed by a GC.   The proper way to fix this would
   935   //       be to set the raw memory state when generating a  SafepointNode.  However
   936   //       this will require extensive changes to the loop optimization in order to
   937   //       prevent a degradation of the optimization.
   938   //       See comment in memnode.hpp, around line 227 in class LoadPNode.
   939   Node* eden_end = make_load(ctrl, mem, eden_end_adr, 0, TypeRawPtr::BOTTOM, T_ADDRESS);
   941   // We need a Region and corresponding Phi's to merge the slow-path and fast-path results.
   942   // they will not be used if "always_slow" is set
   943   enum { slow_result_path = 1, fast_result_path = 2 };
   944   Node *result_region;
   945   Node *result_phi_rawmem;
   946   Node *result_phi_rawoop;
   947   Node *result_phi_i_o;
   949   // The initial slow comparison is a size check, the comparison
   950   // we want to do is a BoolTest::gt
   951   bool always_slow = false;
   952   int tv = _igvn.find_int_con(initial_slow_test, -1);
   953   if (tv >= 0) {
   954     always_slow = (tv == 1);
   955     initial_slow_test = NULL;
   956   } else {
   957     initial_slow_test = BoolNode::make_predicate(initial_slow_test, &_igvn);
   958   }
   960   if (DTraceAllocProbes) {
   961     // Force slow-path allocation
   962     always_slow = true;
   963     initial_slow_test = NULL;
   964   }
   966   enum { too_big_or_final_path = 1, need_gc_path = 2 };
   967   Node *slow_region = NULL;
   968   Node *toobig_false = ctrl;
   970   assert (initial_slow_test == NULL || !always_slow, "arguments must be consistent");
   971   // generate the initial test if necessary
   972   if (initial_slow_test != NULL ) {
   973     slow_region = new (C, 3) RegionNode(3);
   975     // Now make the initial failure test.  Usually a too-big test but
   976     // might be a TRUE for finalizers or a fancy class check for
   977     // newInstance0.
   978     IfNode *toobig_iff = new (C, 2) IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
   979     transform_later(toobig_iff);
   980     // Plug the failing-too-big test into the slow-path region
   981     Node *toobig_true = new (C, 1) IfTrueNode( toobig_iff );
   982     transform_later(toobig_true);
   983     slow_region    ->init_req( too_big_or_final_path, toobig_true );
   984     toobig_false = new (C, 1) IfFalseNode( toobig_iff );
   985     transform_later(toobig_false);
   986   } else {         // No initial test, just fall into next case
   987     toobig_false = ctrl;
   988     debug_only(slow_region = NodeSentinel);
   989   }
   991   Node *slow_mem = mem;  // save the current memory state for slow path
   992   // generate the fast allocation code unless we know that the initial test will always go slow
   993   if (!always_slow) {
   994     // allocate the Region and Phi nodes for the result
   995     result_region = new (C, 3) RegionNode(3);
   996     result_phi_rawmem = new (C, 3) PhiNode( result_region, Type::MEMORY, TypeRawPtr::BOTTOM );
   997     result_phi_rawoop = new (C, 3) PhiNode( result_region, TypeRawPtr::BOTTOM );
   998     result_phi_i_o    = new (C, 3) PhiNode( result_region, Type::ABIO ); // I/O is used for Prefetch
  1000     // We need a Region for the loop-back contended case.
  1001     enum { fall_in_path = 1, contended_loopback_path = 2 };
  1002     Node *contended_region;
  1003     Node *contended_phi_rawmem;
  1004     if( UseTLAB ) {
  1005       contended_region = toobig_false;
  1006       contended_phi_rawmem = mem;
  1007     } else {
  1008       contended_region = new (C, 3) RegionNode(3);
  1009       contended_phi_rawmem = new (C, 3) PhiNode( contended_region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1010       // Now handle the passing-too-big test.  We fall into the contended
  1011       // loop-back merge point.
  1012       contended_region    ->init_req( fall_in_path, toobig_false );
  1013       contended_phi_rawmem->init_req( fall_in_path, mem );
  1014       transform_later(contended_region);
  1015       transform_later(contended_phi_rawmem);
  1018     // Load(-locked) the heap top.
  1019     // See note above concerning the control input when using a TLAB
  1020     Node *old_eden_top = UseTLAB
  1021       ? new (C, 3) LoadPNode     ( ctrl, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM )
  1022       : new (C, 3) LoadPLockedNode( contended_region, contended_phi_rawmem, eden_top_adr );
  1024     transform_later(old_eden_top);
  1025     // Add to heap top to get a new heap top
  1026     Node *new_eden_top = new (C, 4) AddPNode( top(), old_eden_top, size_in_bytes );
  1027     transform_later(new_eden_top);
  1028     // Check for needing a GC; compare against heap end
  1029     Node *needgc_cmp = new (C, 3) CmpPNode( new_eden_top, eden_end );
  1030     transform_later(needgc_cmp);
  1031     Node *needgc_bol = new (C, 2) BoolNode( needgc_cmp, BoolTest::ge );
  1032     transform_later(needgc_bol);
  1033     IfNode *needgc_iff = new (C, 2) IfNode(contended_region, needgc_bol, PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
  1034     transform_later(needgc_iff);
  1036     // Plug the failing-heap-space-need-gc test into the slow-path region
  1037     Node *needgc_true = new (C, 1) IfTrueNode( needgc_iff );
  1038     transform_later(needgc_true);
  1039     if( initial_slow_test ) {
  1040       slow_region    ->init_req( need_gc_path, needgc_true );
  1041       // This completes all paths into the slow merge point
  1042       transform_later(slow_region);
  1043     } else {                      // No initial slow path needed!
  1044       // Just fall from the need-GC path straight into the VM call.
  1045       slow_region    = needgc_true;
  1047     // No need for a GC.  Setup for the Store-Conditional
  1048     Node *needgc_false = new (C, 1) IfFalseNode( needgc_iff );
  1049     transform_later(needgc_false);
  1051     // Grab regular I/O before optional prefetch may change it.
  1052     // Slow-path does no I/O so just set it to the original I/O.
  1053     result_phi_i_o->init_req( slow_result_path, i_o );
  1055     i_o = prefetch_allocation(i_o, needgc_false, contended_phi_rawmem,
  1056                               old_eden_top, new_eden_top, length);
  1058     // Store (-conditional) the modified eden top back down.
  1059     // StorePConditional produces flags for a test PLUS a modified raw
  1060     // memory state.
  1061     Node *store_eden_top;
  1062     Node *fast_oop_ctrl;
  1063     if( UseTLAB ) {
  1064       store_eden_top = new (C, 4) StorePNode( needgc_false, contended_phi_rawmem, eden_top_adr, TypeRawPtr::BOTTOM, new_eden_top );
  1065       transform_later(store_eden_top);
  1066       fast_oop_ctrl = needgc_false; // No contention, so this is the fast path
  1067     } else {
  1068       store_eden_top = new (C, 5) StorePConditionalNode( needgc_false, contended_phi_rawmem, eden_top_adr, new_eden_top, old_eden_top );
  1069       transform_later(store_eden_top);
  1070       Node *contention_check = new (C, 2) BoolNode( store_eden_top, BoolTest::ne );
  1071       transform_later(contention_check);
  1072       store_eden_top = new (C, 1) SCMemProjNode(store_eden_top);
  1073       transform_later(store_eden_top);
  1075       // If not using TLABs, check to see if there was contention.
  1076       IfNode *contention_iff = new (C, 2) IfNode ( needgc_false, contention_check, PROB_MIN, COUNT_UNKNOWN );
  1077       transform_later(contention_iff);
  1078       Node *contention_true = new (C, 1) IfTrueNode( contention_iff );
  1079       transform_later(contention_true);
  1080       // If contention, loopback and try again.
  1081       contended_region->init_req( contended_loopback_path, contention_true );
  1082       contended_phi_rawmem->init_req( contended_loopback_path, store_eden_top );
  1084       // Fast-path succeeded with no contention!
  1085       Node *contention_false = new (C, 1) IfFalseNode( contention_iff );
  1086       transform_later(contention_false);
  1087       fast_oop_ctrl = contention_false;
  1090     // Rename successful fast-path variables to make meaning more obvious
  1091     Node* fast_oop        = old_eden_top;
  1092     Node* fast_oop_rawmem = store_eden_top;
  1093     fast_oop_rawmem = initialize_object(alloc,
  1094                                         fast_oop_ctrl, fast_oop_rawmem, fast_oop,
  1095                                         klass_node, length, size_in_bytes);
  1097     if (ExtendedDTraceProbes) {
  1098       // Slow-path call
  1099       int size = TypeFunc::Parms + 2;
  1100       CallLeafNode *call = new (C, size) CallLeafNode(OptoRuntime::dtrace_object_alloc_Type(),
  1101                                                       CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc_base),
  1102                                                       "dtrace_object_alloc",
  1103                                                       TypeRawPtr::BOTTOM);
  1105       // Get base of thread-local storage area
  1106       Node* thread = new (C, 1) ThreadLocalNode();
  1107       transform_later(thread);
  1109       call->init_req(TypeFunc::Parms+0, thread);
  1110       call->init_req(TypeFunc::Parms+1, fast_oop);
  1111       call->init_req( TypeFunc::Control, fast_oop_ctrl );
  1112       call->init_req( TypeFunc::I_O    , top() )        ;   // does no i/o
  1113       call->init_req( TypeFunc::Memory , fast_oop_rawmem );
  1114       call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
  1115       call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
  1116       transform_later(call);
  1117       fast_oop_ctrl = new (C, 1) ProjNode(call,TypeFunc::Control);
  1118       transform_later(fast_oop_ctrl);
  1119       fast_oop_rawmem = new (C, 1) ProjNode(call,TypeFunc::Memory);
  1120       transform_later(fast_oop_rawmem);
  1123     // Plug in the successful fast-path into the result merge point
  1124     result_region    ->init_req( fast_result_path, fast_oop_ctrl );
  1125     result_phi_rawoop->init_req( fast_result_path, fast_oop );
  1126     result_phi_i_o   ->init_req( fast_result_path, i_o );
  1127     result_phi_rawmem->init_req( fast_result_path, fast_oop_rawmem );
  1128   } else {
  1129     slow_region = ctrl;
  1132   // Generate slow-path call
  1133   CallNode *call = new (C, slow_call_type->domain()->cnt())
  1134     CallStaticJavaNode(slow_call_type, slow_call_address,
  1135                        OptoRuntime::stub_name(slow_call_address),
  1136                        alloc->jvms()->bci(),
  1137                        TypePtr::BOTTOM);
  1138   call->init_req( TypeFunc::Control, slow_region );
  1139   call->init_req( TypeFunc::I_O    , top() )     ;   // does no i/o
  1140   call->init_req( TypeFunc::Memory , slow_mem ); // may gc ptrs
  1141   call->init_req( TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr) );
  1142   call->init_req( TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr) );
  1144   call->init_req(TypeFunc::Parms+0, klass_node);
  1145   if (length != NULL) {
  1146     call->init_req(TypeFunc::Parms+1, length);
  1149   // Copy debug information and adjust JVMState information, then replace
  1150   // allocate node with the call
  1151   copy_call_debug_info((CallNode *) alloc,  call);
  1152   if (!always_slow) {
  1153     call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
  1155   _igvn.hash_delete(alloc);
  1156   _igvn.subsume_node(alloc, call);
  1157   transform_later(call);
  1159   // Identify the output projections from the allocate node and
  1160   // adjust any references to them.
  1161   // The control and io projections look like:
  1162   //
  1163   //        v---Proj(ctrl) <-----+   v---CatchProj(ctrl)
  1164   //  Allocate                   Catch
  1165   //        ^---Proj(io) <-------+   ^---CatchProj(io)
  1166   //
  1167   //  We are interested in the CatchProj nodes.
  1168   //
  1169   extract_call_projections(call);
  1171   // An allocate node has separate memory projections for the uses on the control and i_o paths
  1172   // Replace uses of the control memory projection with result_phi_rawmem (unless we are only generating a slow call)
  1173   if (!always_slow && _memproj_fallthrough != NULL) {
  1174     for (DUIterator_Fast imax, i = _memproj_fallthrough->fast_outs(imax); i < imax; i++) {
  1175       Node *use = _memproj_fallthrough->fast_out(i);
  1176       _igvn.hash_delete(use);
  1177       imax -= replace_input(use, _memproj_fallthrough, result_phi_rawmem);
  1178       _igvn._worklist.push(use);
  1179       // back up iterator
  1180       --i;
  1183   // Now change uses of _memproj_catchall to use _memproj_fallthrough and delete _memproj_catchall so
  1184   // we end up with a call that has only 1 memory projection
  1185   if (_memproj_catchall != NULL ) {
  1186     if (_memproj_fallthrough == NULL) {
  1187       _memproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::Memory);
  1188       transform_later(_memproj_fallthrough);
  1190     for (DUIterator_Fast imax, i = _memproj_catchall->fast_outs(imax); i < imax; i++) {
  1191       Node *use = _memproj_catchall->fast_out(i);
  1192       _igvn.hash_delete(use);
  1193       imax -= replace_input(use, _memproj_catchall, _memproj_fallthrough);
  1194       _igvn._worklist.push(use);
  1195       // back up iterator
  1196       --i;
  1200   mem = result_phi_rawmem;
  1202   // An allocate node has separate i_o projections for the uses on the control and i_o paths
  1203   // Replace uses of the control i_o projection with result_phi_i_o (unless we are only generating a slow call)
  1204   if (_ioproj_fallthrough == NULL) {
  1205     _ioproj_fallthrough = new (C, 1) ProjNode(call, TypeFunc::I_O);
  1206     transform_later(_ioproj_fallthrough);
  1207   } else if (!always_slow) {
  1208     for (DUIterator_Fast imax, i = _ioproj_fallthrough->fast_outs(imax); i < imax; i++) {
  1209       Node *use = _ioproj_fallthrough->fast_out(i);
  1211       _igvn.hash_delete(use);
  1212       imax -= replace_input(use, _ioproj_fallthrough, result_phi_i_o);
  1213       _igvn._worklist.push(use);
  1214       // back up iterator
  1215       --i;
  1218   // Now change uses of _ioproj_catchall to use _ioproj_fallthrough and delete _ioproj_catchall so
  1219   // we end up with a call that has only 1 control projection
  1220   if (_ioproj_catchall != NULL ) {
  1221     for (DUIterator_Fast imax, i = _ioproj_catchall->fast_outs(imax); i < imax; i++) {
  1222       Node *use = _ioproj_catchall->fast_out(i);
  1223       _igvn.hash_delete(use);
  1224       imax -= replace_input(use, _ioproj_catchall, _ioproj_fallthrough);
  1225       _igvn._worklist.push(use);
  1226       // back up iterator
  1227       --i;
  1231   // if we generated only a slow call, we are done
  1232   if (always_slow)
  1233     return;
  1236   if (_fallthroughcatchproj != NULL) {
  1237     ctrl = _fallthroughcatchproj->clone();
  1238     transform_later(ctrl);
  1239     _igvn.hash_delete(_fallthroughcatchproj);
  1240     _igvn.subsume_node(_fallthroughcatchproj, result_region);
  1241   } else {
  1242     ctrl = top();
  1244   Node *slow_result;
  1245   if (_resproj == NULL) {
  1246     // no uses of the allocation result
  1247     slow_result = top();
  1248   } else {
  1249     slow_result = _resproj->clone();
  1250     transform_later(slow_result);
  1251     _igvn.hash_delete(_resproj);
  1252     _igvn.subsume_node(_resproj, result_phi_rawoop);
  1255   // Plug slow-path into result merge point
  1256   result_region    ->init_req( slow_result_path, ctrl );
  1257   result_phi_rawoop->init_req( slow_result_path, slow_result);
  1258   result_phi_rawmem->init_req( slow_result_path, _memproj_fallthrough );
  1259   transform_later(result_region);
  1260   transform_later(result_phi_rawoop);
  1261   transform_later(result_phi_rawmem);
  1262   transform_later(result_phi_i_o);
  1263   // This completes all paths into the result merge point
  1267 // Helper for PhaseMacroExpand::expand_allocate_common.
  1268 // Initializes the newly-allocated storage.
  1269 Node*
  1270 PhaseMacroExpand::initialize_object(AllocateNode* alloc,
  1271                                     Node* control, Node* rawmem, Node* object,
  1272                                     Node* klass_node, Node* length,
  1273                                     Node* size_in_bytes) {
  1274   InitializeNode* init = alloc->initialization();
  1275   // Store the klass & mark bits
  1276   Node* mark_node = NULL;
  1277   // For now only enable fast locking for non-array types
  1278   if (UseBiasedLocking && (length == NULL)) {
  1279     mark_node = make_load(NULL, rawmem, klass_node, Klass::prototype_header_offset_in_bytes() + sizeof(oopDesc), TypeRawPtr::BOTTOM, T_ADDRESS);
  1280   } else {
  1281     mark_node = makecon(TypeRawPtr::make((address)markOopDesc::prototype()));
  1283   rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
  1285   if (UseCompressedOops) {
  1286     Node *zeronode = makecon(TypeInt::ZERO);
  1287     // store uncompressed 0 into klass ptr to zero out gap.  The gap is
  1288     // used for primitive fields and has to be zeroed.
  1289     rawmem = make_store(control, rawmem, object, oopDesc::klass_gap_offset_in_bytes(), zeronode, T_INT);
  1291   rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_OBJECT);
  1292   int header_size = alloc->minimum_header_size();  // conservatively small
  1294   // Array length
  1295   if (length != NULL) {         // Arrays need length field
  1296     rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
  1297     // conservatively small header size:
  1298     header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
  1299     ciKlass* k = _igvn.type(klass_node)->is_klassptr()->klass();
  1300     if (k->is_array_klass())    // we know the exact header size in most cases:
  1301       header_size = Klass::layout_helper_header_size(k->layout_helper());
  1304   // Clear the object body, if necessary.
  1305   if (init == NULL) {
  1306     // The init has somehow disappeared; be cautious and clear everything.
  1307     //
  1308     // This can happen if a node is allocated but an uncommon trap occurs
  1309     // immediately.  In this case, the Initialize gets associated with the
  1310     // trap, and may be placed in a different (outer) loop, if the Allocate
  1311     // is in a loop.  If (this is rare) the inner loop gets unrolled, then
  1312     // there can be two Allocates to one Initialize.  The answer in all these
  1313     // edge cases is safety first.  It is always safe to clear immediately
  1314     // within an Allocate, and then (maybe or maybe not) clear some more later.
  1315     if (!ZeroTLAB)
  1316       rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
  1317                                             header_size, size_in_bytes,
  1318                                             &_igvn);
  1319   } else {
  1320     if (!init->is_complete()) {
  1321       // Try to win by zeroing only what the init does not store.
  1322       // We can also try to do some peephole optimizations,
  1323       // such as combining some adjacent subword stores.
  1324       rawmem = init->complete_stores(control, rawmem, object,
  1325                                      header_size, size_in_bytes, &_igvn);
  1327     // We have no more use for this link, since the AllocateNode goes away:
  1328     init->set_req(InitializeNode::RawAddress, top());
  1329     // (If we keep the link, it just confuses the register allocator,
  1330     // who thinks he sees a real use of the address by the membar.)
  1333   return rawmem;
  1336 // Generate prefetch instructions for next allocations.
  1337 Node* PhaseMacroExpand::prefetch_allocation(Node* i_o, Node*& needgc_false,
  1338                                         Node*& contended_phi_rawmem,
  1339                                         Node* old_eden_top, Node* new_eden_top,
  1340                                         Node* length) {
  1341    if( UseTLAB && AllocatePrefetchStyle == 2 ) {
  1342       // Generate prefetch allocation with watermark check.
  1343       // As an allocation hits the watermark, we will prefetch starting
  1344       // at a "distance" away from watermark.
  1345       enum { fall_in_path = 1, pf_path = 2 };
  1347       Node *pf_region = new (C, 3) RegionNode(3);
  1348       Node *pf_phi_rawmem = new (C, 3) PhiNode( pf_region, Type::MEMORY,
  1349                                                 TypeRawPtr::BOTTOM );
  1350       // I/O is used for Prefetch
  1351       Node *pf_phi_abio = new (C, 3) PhiNode( pf_region, Type::ABIO );
  1353       Node *thread = new (C, 1) ThreadLocalNode();
  1354       transform_later(thread);
  1356       Node *eden_pf_adr = new (C, 4) AddPNode( top()/*not oop*/, thread,
  1357                    _igvn.MakeConX(in_bytes(JavaThread::tlab_pf_top_offset())) );
  1358       transform_later(eden_pf_adr);
  1360       Node *old_pf_wm = new (C, 3) LoadPNode( needgc_false,
  1361                                    contended_phi_rawmem, eden_pf_adr,
  1362                                    TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM );
  1363       transform_later(old_pf_wm);
  1365       // check against new_eden_top
  1366       Node *need_pf_cmp = new (C, 3) CmpPNode( new_eden_top, old_pf_wm );
  1367       transform_later(need_pf_cmp);
  1368       Node *need_pf_bol = new (C, 2) BoolNode( need_pf_cmp, BoolTest::ge );
  1369       transform_later(need_pf_bol);
  1370       IfNode *need_pf_iff = new (C, 2) IfNode( needgc_false, need_pf_bol,
  1371                                        PROB_UNLIKELY_MAG(4), COUNT_UNKNOWN );
  1372       transform_later(need_pf_iff);
  1374       // true node, add prefetchdistance
  1375       Node *need_pf_true = new (C, 1) IfTrueNode( need_pf_iff );
  1376       transform_later(need_pf_true);
  1378       Node *need_pf_false = new (C, 1) IfFalseNode( need_pf_iff );
  1379       transform_later(need_pf_false);
  1381       Node *new_pf_wmt = new (C, 4) AddPNode( top(), old_pf_wm,
  1382                                     _igvn.MakeConX(AllocatePrefetchDistance) );
  1383       transform_later(new_pf_wmt );
  1384       new_pf_wmt->set_req(0, need_pf_true);
  1386       Node *store_new_wmt = new (C, 4) StorePNode( need_pf_true,
  1387                                        contended_phi_rawmem, eden_pf_adr,
  1388                                        TypeRawPtr::BOTTOM, new_pf_wmt );
  1389       transform_later(store_new_wmt);
  1391       // adding prefetches
  1392       pf_phi_abio->init_req( fall_in_path, i_o );
  1394       Node *prefetch_adr;
  1395       Node *prefetch;
  1396       uint lines = AllocatePrefetchDistance / AllocatePrefetchStepSize;
  1397       uint step_size = AllocatePrefetchStepSize;
  1398       uint distance = 0;
  1400       for ( uint i = 0; i < lines; i++ ) {
  1401         prefetch_adr = new (C, 4) AddPNode( old_pf_wm, new_pf_wmt,
  1402                                             _igvn.MakeConX(distance) );
  1403         transform_later(prefetch_adr);
  1404         prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
  1405         transform_later(prefetch);
  1406         distance += step_size;
  1407         i_o = prefetch;
  1409       pf_phi_abio->set_req( pf_path, i_o );
  1411       pf_region->init_req( fall_in_path, need_pf_false );
  1412       pf_region->init_req( pf_path, need_pf_true );
  1414       pf_phi_rawmem->init_req( fall_in_path, contended_phi_rawmem );
  1415       pf_phi_rawmem->init_req( pf_path, store_new_wmt );
  1417       transform_later(pf_region);
  1418       transform_later(pf_phi_rawmem);
  1419       transform_later(pf_phi_abio);
  1421       needgc_false = pf_region;
  1422       contended_phi_rawmem = pf_phi_rawmem;
  1423       i_o = pf_phi_abio;
  1424    } else if( AllocatePrefetchStyle > 0 ) {
  1425       // Insert a prefetch for each allocation only on the fast-path
  1426       Node *prefetch_adr;
  1427       Node *prefetch;
  1428       // Generate several prefetch instructions only for arrays.
  1429       uint lines = (length != NULL) ? AllocatePrefetchLines : 1;
  1430       uint step_size = AllocatePrefetchStepSize;
  1431       uint distance = AllocatePrefetchDistance;
  1432       for ( uint i = 0; i < lines; i++ ) {
  1433         prefetch_adr = new (C, 4) AddPNode( old_eden_top, new_eden_top,
  1434                                             _igvn.MakeConX(distance) );
  1435         transform_later(prefetch_adr);
  1436         prefetch = new (C, 3) PrefetchWriteNode( i_o, prefetch_adr );
  1437         // Do not let it float too high, since if eden_top == eden_end,
  1438         // both might be null.
  1439         if( i == 0 ) { // Set control for first prefetch, next follows it
  1440           prefetch->init_req(0, needgc_false);
  1442         transform_later(prefetch);
  1443         distance += step_size;
  1444         i_o = prefetch;
  1447    return i_o;
  1451 void PhaseMacroExpand::expand_allocate(AllocateNode *alloc) {
  1452   expand_allocate_common(alloc, NULL,
  1453                          OptoRuntime::new_instance_Type(),
  1454                          OptoRuntime::new_instance_Java());
  1457 void PhaseMacroExpand::expand_allocate_array(AllocateArrayNode *alloc) {
  1458   Node* length = alloc->in(AllocateNode::ALength);
  1459   expand_allocate_common(alloc, length,
  1460                          OptoRuntime::new_array_Type(),
  1461                          OptoRuntime::new_array_Java());
  1465 // we have determined that this lock/unlock can be eliminated, we simply
  1466 // eliminate the node without expanding it.
  1467 //
  1468 // Note:  The membar's associated with the lock/unlock are currently not
  1469 //        eliminated.  This should be investigated as a future enhancement.
  1470 //
  1471 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
  1473   if (!alock->is_eliminated()) {
  1474     return false;
  1476   // Mark the box lock as eliminated if all correspondent locks are eliminated
  1477   // to construct correct debug info.
  1478   BoxLockNode* box = alock->box_node()->as_BoxLock();
  1479   if (!box->is_eliminated()) {
  1480     bool eliminate = true;
  1481     for (DUIterator_Fast imax, i = box->fast_outs(imax); i < imax; i++) {
  1482       Node *lck = box->fast_out(i);
  1483       if (lck->is_Lock() && !lck->as_AbstractLock()->is_eliminated()) {
  1484         eliminate = false;
  1485         break;
  1488     if (eliminate)
  1489       box->set_eliminated();
  1492   #ifndef PRODUCT
  1493   if (PrintEliminateLocks) {
  1494     if (alock->is_Lock()) {
  1495       tty->print_cr("++++ Eliminating: %d Lock", alock->_idx);
  1496     } else {
  1497       tty->print_cr("++++ Eliminating: %d Unlock", alock->_idx);
  1500   #endif
  1502   Node* mem  = alock->in(TypeFunc::Memory);
  1503   Node* ctrl = alock->in(TypeFunc::Control);
  1505   extract_call_projections(alock);
  1506   // There are 2 projections from the lock.  The lock node will
  1507   // be deleted when its last use is subsumed below.
  1508   assert(alock->outcnt() == 2 &&
  1509          _fallthroughproj != NULL &&
  1510          _memproj_fallthrough != NULL,
  1511          "Unexpected projections from Lock/Unlock");
  1513   Node* fallthroughproj = _fallthroughproj;
  1514   Node* memproj_fallthrough = _memproj_fallthrough;
  1516   // The memory projection from a lock/unlock is RawMem
  1517   // The input to a Lock is merged memory, so extract its RawMem input
  1518   // (unless the MergeMem has been optimized away.)
  1519   if (alock->is_Lock()) {
  1520     // Seach for MemBarAcquire node and delete it also.
  1521     MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
  1522     assert(membar != NULL && membar->Opcode() == Op_MemBarAcquire, "");
  1523     Node* ctrlproj = membar->proj_out(TypeFunc::Control);
  1524     Node* memproj = membar->proj_out(TypeFunc::Memory);
  1525     _igvn.hash_delete(ctrlproj);
  1526     _igvn.subsume_node(ctrlproj, fallthroughproj);
  1527     _igvn.hash_delete(memproj);
  1528     _igvn.subsume_node(memproj, memproj_fallthrough);
  1531   // Seach for MemBarRelease node and delete it also.
  1532   if (alock->is_Unlock() && ctrl != NULL && ctrl->is_Proj() &&
  1533       ctrl->in(0)->is_MemBar()) {
  1534     MemBarNode* membar = ctrl->in(0)->as_MemBar();
  1535     assert(membar->Opcode() == Op_MemBarRelease &&
  1536            mem->is_Proj() && membar == mem->in(0), "");
  1537     _igvn.hash_delete(fallthroughproj);
  1538     _igvn.subsume_node(fallthroughproj, ctrl);
  1539     _igvn.hash_delete(memproj_fallthrough);
  1540     _igvn.subsume_node(memproj_fallthrough, mem);
  1541     fallthroughproj = ctrl;
  1542     memproj_fallthrough = mem;
  1543     ctrl = membar->in(TypeFunc::Control);
  1544     mem  = membar->in(TypeFunc::Memory);
  1547   _igvn.hash_delete(fallthroughproj);
  1548   _igvn.subsume_node(fallthroughproj, ctrl);
  1549   _igvn.hash_delete(memproj_fallthrough);
  1550   _igvn.subsume_node(memproj_fallthrough, mem);
  1551   return true;
  1555 //------------------------------expand_lock_node----------------------
  1556 void PhaseMacroExpand::expand_lock_node(LockNode *lock) {
  1558   Node* ctrl = lock->in(TypeFunc::Control);
  1559   Node* mem = lock->in(TypeFunc::Memory);
  1560   Node* obj = lock->obj_node();
  1561   Node* box = lock->box_node();
  1562   Node* flock = lock->fastlock_node();
  1564   // Make the merge point
  1565   Node *region = new (C, 3) RegionNode(3);
  1567   Node *bol = transform_later(new (C, 2) BoolNode(flock,BoolTest::ne));
  1568   Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
  1569   // Optimize test; set region slot 2
  1570   Node *slow_path = opt_iff(region,iff);
  1572   // Make slow path call
  1573   CallNode *call = make_slow_call( (CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(), OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path, obj, box );
  1575   extract_call_projections(call);
  1577   // Slow path can only throw asynchronous exceptions, which are always
  1578   // de-opted.  So the compiler thinks the slow-call can never throw an
  1579   // exception.  If it DOES throw an exception we would need the debug
  1580   // info removed first (since if it throws there is no monitor).
  1581   assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
  1582            _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
  1584   // Capture slow path
  1585   // disconnect fall-through projection from call and create a new one
  1586   // hook up users of fall-through projection to region
  1587   Node *slow_ctrl = _fallthroughproj->clone();
  1588   transform_later(slow_ctrl);
  1589   _igvn.hash_delete(_fallthroughproj);
  1590   _fallthroughproj->disconnect_inputs(NULL);
  1591   region->init_req(1, slow_ctrl);
  1592   // region inputs are now complete
  1593   transform_later(region);
  1594   _igvn.subsume_node(_fallthroughproj, region);
  1596   // create a Phi for the memory state
  1597   Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1598   Node *memproj = transform_later( new (C, 1) ProjNode(call, TypeFunc::Memory) );
  1599   mem_phi->init_req(1, memproj );
  1600   mem_phi->init_req(2, mem);
  1601   transform_later(mem_phi);
  1602     _igvn.hash_delete(_memproj_fallthrough);
  1603   _igvn.subsume_node(_memproj_fallthrough, mem_phi);
  1608 //------------------------------expand_unlock_node----------------------
  1609 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
  1611   Node* ctrl = unlock->in(TypeFunc::Control);
  1612   Node* mem = unlock->in(TypeFunc::Memory);
  1613   Node* obj = unlock->obj_node();
  1614   Node* box = unlock->box_node();
  1616   // No need for a null check on unlock
  1618   // Make the merge point
  1619   RegionNode *region = new (C, 3) RegionNode(3);
  1621   FastUnlockNode *funlock = new (C, 3) FastUnlockNode( ctrl, obj, box );
  1622   funlock = transform_later( funlock )->as_FastUnlock();
  1623   Node *bol = transform_later(new (C, 2) BoolNode(funlock,BoolTest::ne));
  1624   Node *iff = new (C, 2) IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
  1625   // Optimize test; set region slot 2
  1626   Node *slow_path = opt_iff(region,iff);
  1628   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 );
  1630   extract_call_projections(call);
  1632   assert ( _ioproj_fallthrough == NULL && _ioproj_catchall == NULL &&
  1633            _memproj_catchall == NULL && _catchallcatchproj == NULL, "Unexpected projection from Lock");
  1635   // No exceptions for unlocking
  1636   // Capture slow path
  1637   // disconnect fall-through projection from call and create a new one
  1638   // hook up users of fall-through projection to region
  1639   Node *slow_ctrl = _fallthroughproj->clone();
  1640   transform_later(slow_ctrl);
  1641   _igvn.hash_delete(_fallthroughproj);
  1642   _fallthroughproj->disconnect_inputs(NULL);
  1643   region->init_req(1, slow_ctrl);
  1644   // region inputs are now complete
  1645   transform_later(region);
  1646   _igvn.subsume_node(_fallthroughproj, region);
  1648   // create a Phi for the memory state
  1649   Node *mem_phi = new (C, 3) PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
  1650   Node *memproj = transform_later( new(C, 1) ProjNode(call, TypeFunc::Memory) );
  1651   mem_phi->init_req(1, memproj );
  1652   mem_phi->init_req(2, mem);
  1653   transform_later(mem_phi);
  1654     _igvn.hash_delete(_memproj_fallthrough);
  1655   _igvn.subsume_node(_memproj_fallthrough, mem_phi);
  1660 //------------------------------expand_macro_nodes----------------------
  1661 //  Returns true if a failure occurred.
  1662 bool PhaseMacroExpand::expand_macro_nodes() {
  1663   if (C->macro_count() == 0)
  1664     return false;
  1665   // attempt to eliminate allocations
  1666   bool progress = true;
  1667   while (progress) {
  1668     progress = false;
  1669     for (int i = C->macro_count(); i > 0; i--) {
  1670       Node * n = C->macro_node(i-1);
  1671       bool success = false;
  1672       debug_only(int old_macro_count = C->macro_count(););
  1673       switch (n->class_id()) {
  1674       case Node::Class_Allocate:
  1675       case Node::Class_AllocateArray:
  1676         success = eliminate_allocate_node(n->as_Allocate());
  1677         break;
  1678       case Node::Class_Lock:
  1679       case Node::Class_Unlock:
  1680         success = eliminate_locking_node(n->as_AbstractLock());
  1681         break;
  1682       default:
  1683         assert(false, "unknown node type in macro list");
  1685       assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
  1686       progress = progress || success;
  1689   // Make sure expansion will not cause node limit to be exceeded.
  1690   // Worst case is a macro node gets expanded into about 50 nodes.
  1691   // Allow 50% more for optimization.
  1692   if (C->check_node_count(C->macro_count() * 75, "out of nodes before macro expansion" ) )
  1693     return true;
  1695   // expand "macro" nodes
  1696   // nodes are removed from the macro list as they are processed
  1697   while (C->macro_count() > 0) {
  1698     int macro_count = C->macro_count();
  1699     Node * n = C->macro_node(macro_count-1);
  1700     assert(n->is_macro(), "only macro nodes expected here");
  1701     if (_igvn.type(n) == Type::TOP || n->in(0)->is_top() ) {
  1702       // node is unreachable, so don't try to expand it
  1703       C->remove_macro_node(n);
  1704       continue;
  1706     switch (n->class_id()) {
  1707     case Node::Class_Allocate:
  1708       expand_allocate(n->as_Allocate());
  1709       break;
  1710     case Node::Class_AllocateArray:
  1711       expand_allocate_array(n->as_AllocateArray());
  1712       break;
  1713     case Node::Class_Lock:
  1714       expand_lock_node(n->as_Lock());
  1715       break;
  1716     case Node::Class_Unlock:
  1717       expand_unlock_node(n->as_Unlock());
  1718       break;
  1719     default:
  1720       assert(false, "unknown node type in macro list");
  1722     assert(C->macro_count() < macro_count, "must have deleted a node from macro list");
  1723     if (C->failing())  return true;
  1726   _igvn.set_delay_transform(false);
  1727   _igvn.optimize();
  1728   return false;

mercurial