< prev index next >

src/hotspot/share/opto/macro.cpp

Print this page

   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"

  26 #include "compiler/compileLog.hpp"
  27 #include "gc/shared/collectedHeap.inline.hpp"
  28 #include "gc/shared/tlab_globals.hpp"
  29 #include "libadt/vectset.hpp"
  30 #include "memory/universe.hpp"
  31 #include "opto/addnode.hpp"
  32 #include "opto/arraycopynode.hpp"
  33 #include "opto/callnode.hpp"
  34 #include "opto/castnode.hpp"
  35 #include "opto/cfgnode.hpp"
  36 #include "opto/compile.hpp"
  37 #include "opto/convertnode.hpp"
  38 #include "opto/graphKit.hpp"

  39 #include "opto/intrinsicnode.hpp"
  40 #include "opto/locknode.hpp"
  41 #include "opto/loopnode.hpp"
  42 #include "opto/macro.hpp"
  43 #include "opto/memnode.hpp"
  44 #include "opto/narrowptrnode.hpp"
  45 #include "opto/node.hpp"
  46 #include "opto/opaquenode.hpp"
  47 #include "opto/phaseX.hpp"
  48 #include "opto/rootnode.hpp"
  49 #include "opto/runtime.hpp"
  50 #include "opto/subnode.hpp"
  51 #include "opto/subtypenode.hpp"
  52 #include "opto/type.hpp"
  53 #include "prims/jvmtiExport.hpp"
  54 #include "runtime/continuation.hpp"
  55 #include "runtime/sharedRuntime.hpp"

  56 #include "utilities/macros.hpp"
  57 #include "utilities/powerOfTwo.hpp"
  58 #if INCLUDE_G1GC
  59 #include "gc/g1/g1ThreadLocalData.hpp"
  60 #endif // INCLUDE_G1GC
  61 #if INCLUDE_SHENANDOAHGC
  62 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  63 #endif
  64 
  65 
  66 //
  67 // Replace any references to "oldref" in inputs to "use" with "newref".
  68 // Returns the number of replacements made.
  69 //
  70 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
  71   int nreplacements = 0;
  72   uint req = use->req();
  73   for (uint j = 0; j < use->len(); j++) {
  74     Node *uin = use->in(j);
  75     if (uin == oldref) {
  76       if (j < req)
  77         use->set_req(j, newref);
  78       else
  79         use->set_prec(j, newref);
  80       nreplacements++;
  81     } else if (j >= req && uin == NULL) {
  82       break;
  83     }
  84   }
  85   return nreplacements;
  86 }
  87 
  88 void PhaseMacroExpand::migrate_outs(Node *old, Node *target) {
  89   assert(old != NULL, "sanity");
  90   for (DUIterator_Fast imax, i = old->fast_outs(imax); i < imax; i++) {
  91     Node* use = old->fast_out(i);
  92     _igvn.rehash_node_delayed(use);
  93     imax -= replace_input(use, old, target);
  94     // back up iterator
  95     --i;
  96   }
  97   assert(old->outcnt() == 0, "all uses must be deleted");
  98 }
  99 
 100 Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) {
 101   Node* cmp;
 102   if (mask != 0) {
 103     Node* and_node = transform_later(new AndXNode(word, MakeConX(mask)));
 104     cmp = transform_later(new CmpXNode(and_node, MakeConX(bits)));
 105   } else {
 106     cmp = word;
 107   }
 108   Node* bol = transform_later(new BoolNode(cmp, BoolTest::ne));
 109   IfNode* iff = new IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
 110   transform_later(iff);
 111 
 112   // Fast path taken.
 113   Node *fast_taken = transform_later(new IfFalseNode(iff));
 114 
 115   // Fast path not-taken, i.e. slow path
 116   Node *slow_taken = transform_later(new IfTrueNode(iff));
 117 
 118   if (return_fast_path) {
 119     region->init_req(edge, slow_taken); // Capture slow-control

 142   // Slow-path call
 143  CallNode *call = leaf_name
 144    ? (CallNode*)new CallLeafNode      ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
 145    : (CallNode*)new CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), TypeRawPtr::BOTTOM );
 146 
 147   // Slow path call has no side-effects, uses few values
 148   copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
 149   if (parm0 != NULL)  call->init_req(TypeFunc::Parms+0, parm0);
 150   if (parm1 != NULL)  call->init_req(TypeFunc::Parms+1, parm1);
 151   if (parm2 != NULL)  call->init_req(TypeFunc::Parms+2, parm2);
 152   call->copy_call_debug_info(&_igvn, oldcall);
 153   call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
 154   _igvn.replace_node(oldcall, call);
 155   transform_later(call);
 156 
 157   return call;
 158 }
 159 
 160 void PhaseMacroExpand::eliminate_gc_barrier(Node* p2x) {
 161   BarrierSetC2 *bs = BarrierSet::barrier_set()->barrier_set_c2();
 162   bs->eliminate_gc_barrier(this, p2x);
 163 #ifndef PRODUCT
 164   if (PrintOptoStatistics) {
 165     Atomic::inc(&PhaseMacroExpand::_GC_barriers_removed_counter);
 166   }
 167 #endif
 168 }
 169 
 170 // Search for a memory operation for the specified memory slice.
 171 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) {
 172   Node *orig_mem = mem;
 173   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 174   const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr();
 175   while (true) {
 176     if (mem == alloc_mem || mem == start_mem ) {
 177       return mem;  // hit one of our sentinels
 178     } else if (mem->is_MergeMem()) {
 179       mem = mem->as_MergeMem()->memory_at(alias_idx);
 180     } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
 181       Node *in = mem->in(0);
 182       // we can safely skip over safepoints, calls, locks and membars because we

 196         ArrayCopyNode* ac = NULL;
 197         if (ArrayCopyNode::may_modify(tinst, in->as_MemBar(), phase, ac)) {
 198           if (ac != NULL) {
 199             assert(ac->is_clonebasic(), "Only basic clone is a non escaping clone");
 200             return ac;
 201           }
 202         }
 203         mem = in->in(TypeFunc::Memory);
 204       } else {
 205 #ifdef ASSERT
 206         in->dump();
 207         mem->dump();
 208         assert(false, "unexpected projection");
 209 #endif
 210       }
 211     } else if (mem->is_Store()) {
 212       const TypePtr* atype = mem->as_Store()->adr_type();
 213       int adr_idx = phase->C->get_alias_index(atype);
 214       if (adr_idx == alias_idx) {
 215         assert(atype->isa_oopptr(), "address type must be oopptr");
 216         int adr_offset = atype->offset();
 217         uint adr_iid = atype->is_oopptr()->instance_id();
 218         // Array elements references have the same alias_idx
 219         // but different offset and different instance_id.
 220         if (adr_offset == offset && adr_iid == alloc->_idx) {
 221           return mem;
 222         }
 223       } else {
 224         assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
 225       }
 226       mem = mem->in(MemNode::Memory);
 227     } else if (mem->is_ClearArray()) {
 228       if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) {
 229         // Can not bypass initialization of the instance
 230         // we are looking.
 231         debug_only(intptr_t offset;)
 232         assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity");
 233         InitializeNode* init = alloc->as_Allocate()->initialization();
 234         // We are looking for stored value, return Initialize node
 235         // or memory edge from Allocate node.
 236         if (init != NULL) {

 241       }
 242       // Otherwise skip it (the call updated 'mem' value).
 243     } else if (mem->Opcode() == Op_SCMemProj) {
 244       mem = mem->in(0);
 245       Node* adr = NULL;
 246       if (mem->is_LoadStore()) {
 247         adr = mem->in(MemNode::Address);
 248       } else {
 249         assert(mem->Opcode() == Op_EncodeISOArray ||
 250                mem->Opcode() == Op_StrCompressedCopy, "sanity");
 251         adr = mem->in(3); // Destination array
 252       }
 253       const TypePtr* atype = adr->bottom_type()->is_ptr();
 254       int adr_idx = phase->C->get_alias_index(atype);
 255       if (adr_idx == alias_idx) {
 256         DEBUG_ONLY(mem->dump();)
 257         assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
 258         return NULL;
 259       }
 260       mem = mem->in(MemNode::Memory);
 261    } else if (mem->Opcode() == Op_StrInflatedCopy) {
 262       Node* adr = mem->in(3); // Destination array
 263       const TypePtr* atype = adr->bottom_type()->is_ptr();
 264       int adr_idx = phase->C->get_alias_index(atype);
 265       if (adr_idx == alias_idx) {
 266         DEBUG_ONLY(mem->dump();)
 267         assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field");
 268         return NULL;
 269       }
 270       mem = mem->in(MemNode::Memory);
 271     } else {
 272       return mem;
 273     }
 274     assert(mem != orig_mem, "dead memory loop");
 275   }
 276 }
 277 
 278 // Generate loads from source of the arraycopy for fields of
 279 // destination needed at a deoptimization point
 280 Node* PhaseMacroExpand::make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc) {
 281   BasicType bt = ft;

 286   }
 287   Node* res = NULL;
 288   if (ac->is_clonebasic()) {
 289     assert(ac->in(ArrayCopyNode::Src) != ac->in(ArrayCopyNode::Dest), "clone source equals destination");
 290     Node* base = ac->in(ArrayCopyNode::Src);
 291     Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset)));
 292     const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset);
 293     MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem();
 294     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 295     res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt);
 296   } else {
 297     if (ac->modifies(offset, offset, &_igvn, true)) {
 298       assert(ac->in(ArrayCopyNode::Dest) == alloc->result_cast(), "arraycopy destination should be allocation's result");
 299       uint shift = exact_log2(type2aelembytes(bt));
 300       Node* src_pos = ac->in(ArrayCopyNode::SrcPos);
 301       Node* dest_pos = ac->in(ArrayCopyNode::DestPos);
 302       const TypeInt* src_pos_t = _igvn.type(src_pos)->is_int();
 303       const TypeInt* dest_pos_t = _igvn.type(dest_pos)->is_int();
 304 
 305       Node* adr = NULL;
 306       const TypePtr* adr_type = NULL;




 307       if (src_pos_t->is_con() && dest_pos_t->is_con()) {
 308         intptr_t off = ((src_pos_t->get_con() - dest_pos_t->get_con()) << shift) + offset;
 309         Node* base = ac->in(ArrayCopyNode::Src);
 310         adr = _igvn.transform(new AddPNode(base, base, MakeConX(off)));
 311         adr_type = _igvn.type(base)->is_ptr()->add_offset(off);

 312         if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) {
 313           // Don't emit a new load from src if src == dst but try to get the value from memory instead
 314           return value_from_mem(ac->in(TypeFunc::Memory), ctl, ft, ftype, adr_type->isa_oopptr(), alloc);
 315         }
 316       } else {





 317         Node* diff = _igvn.transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos)));
 318 #ifdef _LP64
 319         diff = _igvn.transform(new ConvI2LNode(diff));
 320 #endif
 321         diff = _igvn.transform(new LShiftXNode(diff, intcon(shift)));
 322 
 323         Node* off = _igvn.transform(new AddXNode(MakeConX(offset), diff));
 324         Node* base = ac->in(ArrayCopyNode::Src);
 325         adr = _igvn.transform(new AddPNode(base, base, off));
 326         adr_type = _igvn.type(base)->is_ptr()->add_offset(Type::OffsetBot);
 327         if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) {
 328           // Non constant offset in the array: we can't statically
 329           // determine the value
 330           return NULL;
 331         }
 332       }
 333       MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem();
 334       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 335       res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt);
 336     }
 337   }
 338   if (res != NULL) {
 339     if (ftype->isa_narrowoop()) {
 340       // PhaseMacroExpand::scalar_replacement adds DecodeN nodes

 341       res = _igvn.transform(new EncodePNode(res, ftype));
 342     }
 343     return res;
 344   }
 345   return NULL;
 346 }
 347 
 348 //
 349 // Given a Memory Phi, compute a value Phi containing the values from stores
 350 // on the input paths.
 351 // Note: this function is recursive, its depth is limited by the "level" argument
 352 // Returns the computed Phi, or NULL if it cannot compute it.
 353 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level) {
 354   assert(mem->is_Phi(), "sanity");
 355   int alias_idx = C->get_alias_index(adr_t);
 356   int offset = adr_t->offset();
 357   int instance_id = adr_t->instance_id();
 358 
 359   // Check if an appropriate value phi already exists.
 360   Node* region = mem->in(0);
 361   for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
 362     Node* phi = region->fast_out(k);
 363     if (phi->is_Phi() && phi != mem &&
 364         phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) {
 365       return phi;
 366     }
 367   }
 368   // Check if an appropriate new value phi already exists.
 369   Node* new_phi = value_phis->find(mem->_idx);
 370   if (new_phi != NULL)
 371     return new_phi;
 372 
 373   if (level <= 0) {
 374     return NULL; // Give up: phi tree too deep
 375   }
 376   Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
 377   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 378 
 379   uint length = mem->req();
 380   GrowableArray <Node *> values(length, length, NULL);
 381 
 382   // create a new Phi for the value
 383   PhiNode *phi = new PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset);
 384   transform_later(phi);
 385   value_phis->push(phi, mem->_idx);
 386 
 387   for (uint j = 1; j < length; j++) {
 388     Node *in = mem->in(j);
 389     if (in == NULL || in->is_top()) {
 390       values.at_put(j, in);
 391     } else  {
 392       Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn);
 393       if (val == start_mem || val == alloc_mem) {
 394         // hit a sentinel, return appropriate 0 value
 395         values.at_put(j, _igvn.zerocon(ft));






 396         continue;
 397       }
 398       if (val->is_Initialize()) {
 399         val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
 400       }
 401       if (val == NULL) {
 402         return NULL;  // can't find a value on this path
 403       }
 404       if (val == mem) {
 405         values.at_put(j, mem);
 406       } else if (val->is_Store()) {
 407         Node* n = val->in(MemNode::ValueIn);
 408         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 409         n = bs->step_over_gc_barrier(n);
 410         if (is_subword_type(ft)) {
 411           n = Compile::narrow_value(ft, n, phi_type, &_igvn, true);
 412         }
 413         values.at_put(j, n);
 414       } else if(val->is_Proj() && val->in(0) == alloc) {
 415         values.at_put(j, _igvn.zerocon(ft));






 416       } else if (val->is_Phi()) {
 417         val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1);
 418         if (val == NULL) {
 419           return NULL;
 420         }
 421         values.at_put(j, val);
 422       } else if (val->Opcode() == Op_SCMemProj) {
 423         assert(val->in(0)->is_LoadStore() ||
 424                val->in(0)->Opcode() == Op_EncodeISOArray ||
 425                val->in(0)->Opcode() == Op_StrCompressedCopy, "sanity");
 426         assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
 427         return NULL;
 428       } else if (val->is_ArrayCopy()) {
 429         Node* res = make_arraycopy_load(val->as_ArrayCopy(), offset, val->in(0), val->in(TypeFunc::Memory), ft, phi_type, alloc);
 430         if (res == NULL) {
 431           return NULL;
 432         }
 433         values.at_put(j, res);
 434       } else {
 435         DEBUG_ONLY( val->dump(); )

 439     }
 440   }
 441   // Set Phi's inputs
 442   for (uint j = 1; j < length; j++) {
 443     if (values.at(j) == mem) {
 444       phi->init_req(j, phi);
 445     } else {
 446       phi->init_req(j, values.at(j));
 447     }
 448   }
 449   return phi;
 450 }
 451 
 452 // Search the last value stored into the object's field.
 453 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, Node *sfpt_ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc) {
 454   assert(adr_t->is_known_instance_field(), "instance required");
 455   int instance_id = adr_t->instance_id();
 456   assert((uint)instance_id == alloc->_idx, "wrong allocation");
 457 
 458   int alias_idx = C->get_alias_index(adr_t);
 459   int offset = adr_t->offset();
 460   Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
 461   Node *alloc_ctrl = alloc->in(TypeFunc::Control);
 462   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 463   VectorSet visited;
 464 
 465   bool done = sfpt_mem == alloc_mem;
 466   Node *mem = sfpt_mem;
 467   while (!done) {
 468     if (visited.test_set(mem->_idx)) {
 469       return NULL;  // found a loop, give up
 470     }
 471     mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn);
 472     if (mem == start_mem || mem == alloc_mem) {
 473       done = true;  // hit a sentinel, return appropriate 0 value
 474     } else if (mem->is_Initialize()) {
 475       mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
 476       if (mem == NULL) {
 477         done = true; // Something go wrong.
 478       } else if (mem->is_Store()) {
 479         const TypePtr* atype = mem->as_Store()->adr_type();
 480         assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
 481         done = true;
 482       }
 483     } else if (mem->is_Store()) {
 484       const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
 485       assert(atype != NULL, "address type must be oopptr");
 486       assert(C->get_alias_index(atype) == alias_idx &&
 487              atype->is_known_instance_field() && atype->offset() == offset &&
 488              atype->instance_id() == instance_id, "store is correct memory slice");
 489       done = true;
 490     } else if (mem->is_Phi()) {
 491       // try to find a phi's unique input
 492       Node *unique_input = NULL;
 493       Node *top = C->top();
 494       for (uint i = 1; i < mem->req(); i++) {
 495         Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn);
 496         if (n == NULL || n == top || n == mem) {
 497           continue;
 498         } else if (unique_input == NULL) {
 499           unique_input = n;
 500         } else if (unique_input != n) {
 501           unique_input = top;
 502           break;
 503         }
 504       }
 505       if (unique_input != NULL && unique_input != top) {
 506         mem = unique_input;
 507       } else {
 508         done = true;
 509       }
 510     } else if (mem->is_ArrayCopy()) {
 511       done = true;
 512     } else {
 513       DEBUG_ONLY( mem->dump(); )
 514       assert(false, "unexpected node");
 515     }
 516   }
 517   if (mem != NULL) {
 518     if (mem == start_mem || mem == alloc_mem) {
 519       // hit a sentinel, return appropriate 0 value





 520       return _igvn.zerocon(ft);
 521     } else if (mem->is_Store()) {
 522       Node* n = mem->in(MemNode::ValueIn);
 523       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 524       n = bs->step_over_gc_barrier(n);
 525       return n;
 526     } else if (mem->is_Phi()) {
 527       // attempt to produce a Phi reflecting the values on the input paths of the Phi
 528       Node_Stack value_phis(8);
 529       Node* phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit);
 530       if (phi != NULL) {
 531         return phi;
 532       } else {
 533         // Kill all new Phis
 534         while(value_phis.is_nonempty()) {
 535           Node* n = value_phis.node();
 536           _igvn.replace_node(n, C->top());
 537           value_phis.pop();
 538         }
 539       }
 540     } else if (mem->is_ArrayCopy()) {
 541       Node* ctl = mem->in(0);
 542       Node* m = mem->in(TypeFunc::Memory);
 543       if (sfpt_ctl->is_Proj() && sfpt_ctl->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) {
 544         // pin the loads in the uncommon trap path
 545         ctl = sfpt_ctl;
 546         m = sfpt_mem;
 547       }
 548       return make_arraycopy_load(mem->as_ArrayCopy(), offset, ctl, m, ft, ftype, alloc);
 549     }
 550   }
 551   // Something go wrong.
 552   return NULL;
 553 }
 554 










































 555 // Check the possibility of scalar replacement.
 556 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
 557   //  Scan the uses of the allocation to check for anything that would
 558   //  prevent us from eliminating it.
 559   NOT_PRODUCT( const char* fail_eliminate = NULL; )
 560   DEBUG_ONLY( Node* disq_node = NULL; )
 561   bool  can_eliminate = true;
 562 

 563   Node* res = alloc->result_cast();
 564   const TypeOopPtr* res_type = NULL;
 565   if (res == NULL) {
 566     // All users were eliminated.
 567   } else if (!res->is_CheckCastPP()) {
 568     NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
 569     can_eliminate = false;
 570   } else {

 571     res_type = _igvn.type(res)->isa_oopptr();
 572     if (res_type == NULL) {
 573       NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
 574       can_eliminate = false;
 575     } else if (res_type->isa_aryptr()) {
 576       int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
 577       if (length < 0) {
 578         NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
 579         can_eliminate = false;
 580       }
 581     }
 582   }
 583 
 584   if (can_eliminate && res != NULL) {
 585     for (DUIterator_Fast jmax, j = res->fast_outs(jmax);
 586                                j < jmax && can_eliminate; j++) {
 587       Node* use = res->fast_out(j);
 588 
 589       if (use->is_AddP()) {
 590         const TypePtr* addp_type = _igvn.type(use)->is_ptr();
 591         int offset = addp_type->offset();
 592 
 593         if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
 594           NOT_PRODUCT(fail_eliminate = "Undefined field reference";)
 595           can_eliminate = false;
 596           break;
 597         }
 598         for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
 599                                    k < kmax && can_eliminate; k++) {
 600           Node* n = use->fast_out(k);
 601           if (!n->is_Store() && n->Opcode() != Op_CastP2X
 602               SHENANDOAHGC_ONLY(&& (!UseShenandoahGC || !ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(n))) ) {
 603             DEBUG_ONLY(disq_node = n;)
 604             if (n->is_Load() || n->is_LoadStore()) {
 605               NOT_PRODUCT(fail_eliminate = "Field load";)
 606             } else {

 615                   use->as_ArrayCopy()->is_copyof_validated() ||
 616                   use->as_ArrayCopy()->is_copyofrange_validated()) &&
 617                  use->in(ArrayCopyNode::Dest) == res) {
 618         // ok to eliminate
 619       } else if (use->is_SafePoint()) {
 620         SafePointNode* sfpt = use->as_SafePoint();
 621         if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
 622           // Object is passed as argument.
 623           DEBUG_ONLY(disq_node = use;)
 624           NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
 625           can_eliminate = false;
 626         }
 627         Node* sfptMem = sfpt->memory();
 628         if (sfptMem == NULL || sfptMem->is_top()) {
 629           DEBUG_ONLY(disq_node = use;)
 630           NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
 631           can_eliminate = false;
 632         } else {
 633           safepoints.append_if_missing(sfpt);
 634         }








 635       } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
 636         if (use->is_Phi()) {
 637           if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
 638             NOT_PRODUCT(fail_eliminate = "Object is return value";)
 639           } else {
 640             NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
 641           }
 642           DEBUG_ONLY(disq_node = use;)
 643         } else {
 644           if (use->Opcode() == Op_Return) {
 645             NOT_PRODUCT(fail_eliminate = "Object is return value";)
 646           }else {
 647             NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
 648           }
 649           DEBUG_ONLY(disq_node = use;)
 650         }
 651         can_eliminate = false;



 652       }
 653     }
 654   }
 655 
 656 #ifndef PRODUCT
 657   if (PrintEliminateAllocations) {
 658     if (can_eliminate) {
 659       tty->print("Scalar ");
 660       if (res == NULL)
 661         alloc->dump();
 662       else
 663         res->dump();
 664     } else if (alloc->_is_scalar_replaceable) {
 665       tty->print("NotScalar (%s)", fail_eliminate);
 666       if (res == NULL)
 667         alloc->dump();
 668       else
 669         res->dump();
 670 #ifdef ASSERT
 671       if (disq_node != NULL) {
 672           tty->print("  >>>> ");
 673           disq_node->dump();
 674       }
 675 #endif /*ASSERT*/
 676     }
 677   }
 678 #endif
 679   return can_eliminate;
 680 }
 681 
 682 // Do scalar replacement.
 683 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
 684   GrowableArray <SafePointNode *> safepoints_done;

 690   BasicType basic_elem_type = T_ILLEGAL;
 691   const Type* field_type = NULL;
 692 
 693   Node* res = alloc->result_cast();
 694   assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result");
 695   const TypeOopPtr* res_type = NULL;
 696   if (res != NULL) { // Could be NULL when there are no users
 697     res_type = _igvn.type(res)->isa_oopptr();
 698   }
 699 
 700   if (res != NULL) {
 701     if (res_type->isa_instptr()) {
 702       // find the fields of the class which will be needed for safepoint debug information
 703       iklass = res_type->is_instptr()->instance_klass();
 704       nfields = iklass->nof_nonstatic_fields();
 705     } else {
 706       // find the array's elements which will be needed for safepoint debug information
 707       nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
 708       assert(nfields >= 0, "must be an array klass.");
 709       basic_elem_type = res_type->is_aryptr()->elem()->array_element_basic_type();



 710       array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
 711       element_size = type2aelembytes(basic_elem_type);
 712       field_type = res_type->is_aryptr()->elem();




 713     }
 714   }
 715   //
 716   // Process the safepoint uses
 717   //


 718   while (safepoints.length() > 0) {
 719     SafePointNode* sfpt = safepoints.pop();
 720     Node* mem = sfpt->memory();
 721     Node* ctl = sfpt->control();
 722     assert(sfpt->jvms() != NULL, "missed JVMS");
 723     // Fields of scalar objs are referenced only at the end
 724     // of regular debuginfo at the last (youngest) JVMS.
 725     // Record relative start index.
 726     uint first_ind = (sfpt->req() - sfpt->jvms()->scloff());
 727     SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 728 #ifdef ASSERT
 729                                                  alloc,
 730 #endif
 731                                                  first_ind, nfields);
 732     sobj->init_req(0, C->root());
 733     transform_later(sobj);
 734 
 735     // Scan object's fields adding an input to the safepoint for each field.
 736     for (int j = 0; j < nfields; j++) {
 737       intptr_t offset;
 738       ciField* field = NULL;
 739       if (iklass != NULL) {
 740         field = iklass->nonstatic_field_at(j);
 741         offset = field->offset();
 742         ciType* elem_type = field->type();
 743         basic_elem_type = field->layout_type();

 744 
 745         // The next code is taken from Parse::do_get_xxx().
 746         if (is_reference_type(basic_elem_type)) {
 747           if (!elem_type->is_loaded()) {
 748             field_type = TypeInstPtr::BOTTOM;
 749           } else if (field != NULL && field->is_static_constant()) {
 750             ciObject* con = field->constant_value().as_object();
 751             // Do not "join" in the previous type; it doesn't add value,
 752             // and may yield a vacuous result if the field is of interface type.
 753             field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
 754             assert(field_type != NULL, "field singleton type must be consistent");
 755           } else {
 756             field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
 757           }
 758           if (UseCompressedOops) {
 759             field_type = field_type->make_narrowoop();
 760             basic_elem_type = T_NARROWOOP;
 761           }
 762         } else {
 763           field_type = Type::get_const_basic_type(basic_elem_type);
 764         }
 765       } else {
 766         offset = array_base + j * (intptr_t)element_size;
 767       }
 768 
 769       const TypeOopPtr *field_addr_type = res_type->add_offset(offset)->isa_oopptr();
 770 
 771       Node *field_val = value_from_mem(mem, ctl, basic_elem_type, field_type, field_addr_type, alloc);






 772       if (field_val == NULL) {
 773         // We weren't able to find a value for this field,
 774         // give up on eliminating this allocation.
 775 
 776         // Remove any extra entries we added to the safepoint.
 777         uint last = sfpt->req() - 1;
 778         for (int k = 0;  k < j; k++) {
 779           sfpt->del_req(last--);
 780         }
 781         _igvn._worklist.push(sfpt);
 782         // rollback processed safepoints
 783         while (safepoints_done.length() > 0) {
 784           SafePointNode* sfpt_done = safepoints_done.pop();
 785           // remove any extra entries we added to the safepoint
 786           last = sfpt_done->req() - 1;
 787           for (int k = 0;  k < nfields; k++) {
 788             sfpt_done->del_req(last--);
 789           }
 790           JVMState *jvms = sfpt_done->jvms();
 791           jvms->set_endoff(sfpt_done->req());

 814             int field_idx = C->get_alias_index(field_addr_type);
 815             tty->print(" (alias_idx=%d)", field_idx);
 816           } else { // Array's element
 817             tty->print("=== At SafePoint node %d can't find value of array element [%d]",
 818                        sfpt->_idx, j);
 819           }
 820           tty->print(", which prevents elimination of: ");
 821           if (res == NULL)
 822             alloc->dump();
 823           else
 824             res->dump();
 825         }
 826 #endif
 827         return false;
 828       }
 829       if (UseCompressedOops && field_type->isa_narrowoop()) {
 830         // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
 831         // to be able scalar replace the allocation.
 832         if (field_val->is_EncodeP()) {
 833           field_val = field_val->in(1);
 834         } else {
 835           field_val = transform_later(new DecodeNNode(field_val, field_val->get_ptr_type()));
 836         }
 837       }




 838       sfpt->add_req(field_val);
 839     }
 840     JVMState *jvms = sfpt->jvms();
 841     jvms->set_endoff(sfpt->req());
 842     // Now make a pass over the debug information replacing any references
 843     // to the allocated object with "sobj"
 844     int start = jvms->debug_start();
 845     int end   = jvms->debug_end();
 846     sfpt->replace_edges_in_range(res, sobj, start, end, &_igvn);
 847     _igvn._worklist.push(sfpt);
 848     safepoints_done.append_if_missing(sfpt); // keep it for rollback
 849   }








 850   return true;
 851 }
 852 
 853 static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) {
 854   Node* ctl_proj = n->proj_out_or_null(TypeFunc::Control);
 855   Node* mem_proj = n->proj_out_or_null(TypeFunc::Memory);
 856   if (ctl_proj != NULL) {
 857     igvn.replace_node(ctl_proj, n->in(0));
 858   }
 859   if (mem_proj != NULL) {
 860     igvn.replace_node(mem_proj, n->in(TypeFunc::Memory));
 861   }
 862 }
 863 
 864 // Process users of eliminated allocation.
 865 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc) {

 866   Node* res = alloc->result_cast();
 867   if (res != NULL) {




 868     for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
 869       Node *use = res->last_out(j);
 870       uint oc1 = res->outcnt();
 871 
 872       if (use->is_AddP()) {
 873         for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
 874           Node *n = use->last_out(k);
 875           uint oc2 = use->outcnt();
 876           if (n->is_Store()) {
 877 #ifdef ASSERT
 878             // Verify that there is no dependent MemBarVolatile nodes,
 879             // they should be removed during IGVN, see MemBarNode::Ideal().
 880             for (DUIterator_Fast pmax, p = n->fast_outs(pmax);
 881                                        p < pmax; p++) {
 882               Node* mb = n->fast_out(p);
 883               assert(mb->is_Initialize() || !mb->is_MemBar() ||
 884                      mb->req() <= MemBarNode::Precedent ||
 885                      mb->in(MemBarNode::Precedent) != n,
 886                      "MemBarVolatile should be eliminated for non-escaping object");
 887             }
 888 #endif
 889             _igvn.replace_node(n, n->in(MemNode::Memory));
 890           } else {
 891             eliminate_gc_barrier(n);
 892           }
 893           k -= (oc2 - use->outcnt());
 894         }
 895         _igvn.remove_dead_node(use);
 896       } else if (use->is_ArrayCopy()) {
 897         // Disconnect ArrayCopy node
 898         ArrayCopyNode* ac = use->as_ArrayCopy();
 899         if (ac->is_clonebasic()) {
 900           Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out();
 901           disconnect_projections(ac, _igvn);
 902           assert(alloc->in(TypeFunc::Memory)->is_Proj() && alloc->in(TypeFunc::Memory)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation");
 903           Node* membar_before = alloc->in(TypeFunc::Memory)->in(0);
 904           disconnect_projections(membar_before->as_MemBar(), _igvn);
 905           if (membar_after->is_MemBar()) {
 906             disconnect_projections(membar_after->as_MemBar(), _igvn);
 907           }
 908         } else {
 909           assert(ac->is_arraycopy_validated() ||
 910                  ac->is_copyof_validated() ||
 911                  ac->is_copyofrange_validated(), "unsupported");
 912           CallProjections callprojs;
 913           ac->extract_projections(&callprojs, true);
 914 
 915           _igvn.replace_node(callprojs.fallthrough_ioproj, ac->in(TypeFunc::I_O));
 916           _igvn.replace_node(callprojs.fallthrough_memproj, ac->in(TypeFunc::Memory));
 917           _igvn.replace_node(callprojs.fallthrough_catchproj, ac->in(TypeFunc::Control));
 918 
 919           // Set control to top. IGVN will remove the remaining projections
 920           ac->set_req(0, top());
 921           ac->replace_edge(res, top(), &_igvn);
 922 
 923           // Disconnect src right away: it can help find new
 924           // opportunities for allocation elimination
 925           Node* src = ac->in(ArrayCopyNode::Src);
 926           ac->replace_edge(src, top(), &_igvn);
 927           // src can be top at this point if src and dest of the
 928           // arraycopy were the same
 929           if (src->outcnt() == 0 && !src->is_top()) {
 930             _igvn.remove_dead_node(src);
 931           }
 932         }
 933         _igvn._worklist.push(ac);














 934       } else {
 935         eliminate_gc_barrier(use);
 936       }
 937       j -= (oc1 - res->outcnt());
 938     }
 939     assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
 940     _igvn.remove_dead_node(res);
 941   }
 942 
 943   //
 944   // Process other users of allocation's projections
 945   //
 946   if (_callprojs.resproj != NULL && _callprojs.resproj->outcnt() != 0) {
 947     // First disconnect stores captured by Initialize node.
 948     // If Initialize node is eliminated first in the following code,
 949     // it will kill such stores and DUIterator_Last will assert.
 950     for (DUIterator_Fast jmax, j = _callprojs.resproj->fast_outs(jmax);  j < jmax; j++) {
 951       Node* use = _callprojs.resproj->fast_out(j);
 952       if (use->is_AddP()) {
 953         // raw memory addresses used only by the initialization
 954         _igvn.replace_node(use, C->top());
 955         --j; --jmax;
 956       }
 957     }
 958     for (DUIterator_Last jmin, j = _callprojs.resproj->last_outs(jmin); j >= jmin; ) {
 959       Node* use = _callprojs.resproj->last_out(j);
 960       uint oc1 = _callprojs.resproj->outcnt();
 961       if (use->is_Initialize()) {
 962         // Eliminate Initialize node.
 963         InitializeNode *init = use->as_Initialize();
 964         assert(init->outcnt() <= 2, "only a control and memory projection expected");
 965         Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control);
 966         if (ctrl_proj != NULL) {
 967           _igvn.replace_node(ctrl_proj, init->in(TypeFunc::Control));
 968 #ifdef ASSERT
 969           // If the InitializeNode has no memory out, it will die, and tmp will become NULL
 970           Node* tmp = init->in(TypeFunc::Control);
 971           assert(tmp == NULL || tmp == _callprojs.fallthrough_catchproj, "allocation control projection");
 972 #endif
 973         }
 974         Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory);
 975         if (mem_proj != NULL) {
 976           Node *mem = init->in(TypeFunc::Memory);
 977 #ifdef ASSERT
 978           if (mem->is_MergeMem()) {
 979             assert(mem->in(TypeFunc::Memory) == _callprojs.fallthrough_memproj, "allocation memory projection");
 980           } else {
 981             assert(mem == _callprojs.fallthrough_memproj, "allocation memory projection");
 982           }
 983 #endif
 984           _igvn.replace_node(mem_proj, mem);
 985         }




 986       } else  {
 987         assert(false, "only Initialize or AddP expected");
 988       }
 989       j -= (oc1 - _callprojs.resproj->outcnt());
 990     }
 991   }
 992   if (_callprojs.fallthrough_catchproj != NULL) {
 993     _igvn.replace_node(_callprojs.fallthrough_catchproj, alloc->in(TypeFunc::Control));
 994   }
 995   if (_callprojs.fallthrough_memproj != NULL) {
 996     _igvn.replace_node(_callprojs.fallthrough_memproj, alloc->in(TypeFunc::Memory));
 997   }
 998   if (_callprojs.catchall_memproj != NULL) {
 999     _igvn.replace_node(_callprojs.catchall_memproj, C->top());
1000   }
1001   if (_callprojs.fallthrough_ioproj != NULL) {
1002     _igvn.replace_node(_callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
1003   }
1004   if (_callprojs.catchall_ioproj != NULL) {
1005     _igvn.replace_node(_callprojs.catchall_ioproj, C->top());
1006   }
1007   if (_callprojs.catchall_catchproj != NULL) {
1008     _igvn.replace_node(_callprojs.catchall_catchproj, C->top());
1009   }
1010 }
1011 
1012 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
1013   // If reallocation fails during deoptimization we'll pop all
1014   // interpreter frames for this compiled frame and that won't play
1015   // nice with JVMTI popframe.
1016   // We avoid this issue by eager reallocation when the popframe request
1017   // is received.
1018   if (!EliminateAllocations || !alloc->_is_non_escaping) {
1019     return false;
1020   }
1021   Node* klass = alloc->in(AllocateNode::KlassNode);
1022   const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr();
1023   Node* res = alloc->result_cast();







1024   // Eliminate boxing allocations which are not used
1025   // regardless scalar replaceable status.
1026   bool boxing_alloc = C->eliminate_boxing() &&

1027                       tklass->isa_instklassptr() &&
1028                       tklass->is_instklassptr()->instance_klass()->is_box_klass();
1029   if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) {
1030     return false;
1031   }
1032 
1033   alloc->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1034 
1035   GrowableArray <SafePointNode *> safepoints;
1036   if (!can_eliminate_allocation(alloc, safepoints)) {
1037     return false;
1038   }
1039 
1040   if (!alloc->_is_scalar_replaceable) {
1041     assert(res == NULL, "sanity");
1042     // We can only eliminate allocation if all debug info references
1043     // are already replaced with SafePointScalarObject because
1044     // we can't search for a fields value without instance_id.
1045     if (safepoints.length() > 0) {

1046       return false;
1047     }
1048   }
1049 
1050   if (!scalar_replacement(alloc, safepoints)) {
1051     return false;
1052   }
1053 
1054   CompileLog* log = C->log();
1055   if (log != NULL) {
1056     log->head("eliminate_allocation type='%d'",
1057               log->identify(tklass->exact_klass()));
1058     JVMState* p = alloc->jvms();
1059     while (p != NULL) {
1060       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1061       p = p->caller();
1062     }
1063     log->tail("eliminate_allocation");
1064   }
1065 
1066   process_users_of_allocation(alloc);
1067 
1068 #ifndef PRODUCT
1069   if (PrintEliminateAllocations) {
1070     if (alloc->is_AllocateArray())
1071       tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
1072     else
1073       tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
1074   }
1075 #endif
1076 
1077   return true;
1078 }
1079 
1080 bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) {
1081   // EA should remove all uses of non-escaping boxing node.
1082   if (!C->eliminate_boxing() || boxing->proj_out_or_null(TypeFunc::Parms) != NULL) {
1083     return false;
1084   }
1085 
1086   assert(boxing->result_cast() == NULL, "unexpected boxing node result");
1087 
1088   boxing->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1089 
1090   const TypeTuple* r = boxing->tf()->range();
1091   assert(r->cnt() > TypeFunc::Parms, "sanity");
1092   const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr();
1093   assert(t != NULL, "sanity");
1094 
1095   CompileLog* log = C->log();
1096   if (log != NULL) {
1097     log->head("eliminate_boxing type='%d'",
1098               log->identify(t->instance_klass()));
1099     JVMState* p = boxing->jvms();
1100     while (p != NULL) {
1101       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1102       p = p->caller();
1103     }
1104     log->tail("eliminate_boxing");
1105   }
1106 
1107   process_users_of_allocation(boxing);
1108 
1109 #ifndef PRODUCT
1110   if (PrintEliminateAllocations) {

1253         }
1254       }
1255 #endif
1256       yank_alloc_node(alloc);
1257       return;
1258     }
1259   }
1260 
1261   enum { too_big_or_final_path = 1, need_gc_path = 2 };
1262   Node *slow_region = NULL;
1263   Node *toobig_false = ctrl;
1264 
1265   // generate the initial test if necessary
1266   if (initial_slow_test != NULL ) {
1267     assert (expand_fast_path, "Only need test if there is a fast path");
1268     slow_region = new RegionNode(3);
1269 
1270     // Now make the initial failure test.  Usually a too-big test but
1271     // might be a TRUE for finalizers or a fancy class check for
1272     // newInstance0.
1273     IfNode *toobig_iff = new IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
1274     transform_later(toobig_iff);
1275     // Plug the failing-too-big test into the slow-path region
1276     Node *toobig_true = new IfTrueNode( toobig_iff );
1277     transform_later(toobig_true);
1278     slow_region    ->init_req( too_big_or_final_path, toobig_true );
1279     toobig_false = new IfFalseNode( toobig_iff );
1280     transform_later(toobig_false);
1281   } else {
1282     // No initial test, just fall into next case
1283     assert(allocation_has_use || !expand_fast_path, "Should already have been handled");
1284     toobig_false = ctrl;
1285     debug_only(slow_region = NodeSentinel);
1286   }
1287 
1288   // If we are here there are several possibilities
1289   // - expand_fast_path is false - then only a slow path is expanded. That's it.
1290   // no_initial_check means a constant allocation.
1291   // - If check always evaluates to false -> expand_fast_path is false (see above)
1292   // - If check always evaluates to true -> directly into fast path (but may bailout to slowpath)
1293   // if !allocation_has_use the fast path is empty
1294   // if !allocation_has_use && no_initial_check
1295   // - Then there are no fastpath that can fall out to slowpath -> no allocation code at all.
1296   //   removed by yank_alloc_node above.
1297 
1298   Node *slow_mem = mem;  // save the current memory state for slow path
1299   // generate the fast allocation code unless we know that the initial test will always go slow
1300   if (expand_fast_path) {
1301     // Fast path modifies only raw memory.
1302     if (mem->is_MergeMem()) {
1303       mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
1304     }
1305 
1306     // allocate the Region and Phi nodes for the result
1307     result_region = new RegionNode(3);
1308     result_phi_rawmem = new PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM);
1309     result_phi_i_o    = new PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch
1310 
1311     // Grab regular I/O before optional prefetch may change it.
1312     // Slow-path does no I/O so just set it to the original I/O.
1313     result_phi_i_o->init_req(slow_result_path, i_o);
1314 
1315     // Name successful fast-path variables
1316     Node* fast_oop_ctrl;
1317     Node* fast_oop_rawmem;

1318     if (allocation_has_use) {
1319       Node* needgc_ctrl = NULL;
1320       result_phi_rawoop = new PhiNode(result_region, TypeRawPtr::BOTTOM);
1321 
1322       intx prefetch_lines = length != NULL ? AllocatePrefetchLines : AllocateInstancePrefetchLines;
1323       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1324       Node* fast_oop = bs->obj_allocate(this, mem, toobig_false, size_in_bytes, i_o, needgc_ctrl,
1325                                         fast_oop_ctrl, fast_oop_rawmem,
1326                                         prefetch_lines);
1327 
1328       if (initial_slow_test != NULL) {
1329         // This completes all paths into the slow merge point
1330         slow_region->init_req(need_gc_path, needgc_ctrl);
1331         transform_later(slow_region);
1332       } else {
1333         // No initial slow path needed!
1334         // Just fall from the need-GC path straight into the VM call.
1335         slow_region = needgc_ctrl;
1336       }
1337 

1355     result_phi_i_o   ->init_req(fast_result_path, i_o);
1356     result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem);
1357   } else {
1358     slow_region = ctrl;
1359     result_phi_i_o = i_o; // Rename it to use in the following code.
1360   }
1361 
1362   // Generate slow-path call
1363   CallNode *call = new CallStaticJavaNode(slow_call_type, slow_call_address,
1364                                OptoRuntime::stub_name(slow_call_address),
1365                                TypePtr::BOTTOM);
1366   call->init_req(TypeFunc::Control,   slow_region);
1367   call->init_req(TypeFunc::I_O,       top());    // does no i/o
1368   call->init_req(TypeFunc::Memory,    slow_mem); // may gc ptrs
1369   call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1370   call->init_req(TypeFunc::FramePtr,  alloc->in(TypeFunc::FramePtr));
1371 
1372   call->init_req(TypeFunc::Parms+0, klass_node);
1373   if (length != NULL) {
1374     call->init_req(TypeFunc::Parms+1, length);



1375   }
1376 
1377   // Copy debug information and adjust JVMState information, then replace
1378   // allocate node with the call
1379   call->copy_call_debug_info(&_igvn, alloc);
1380   if (expand_fast_path) {
1381     call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
1382   } else {
1383     // Hook i_o projection to avoid its elimination during allocation
1384     // replacement (when only a slow call is generated).
1385     call->set_req(TypeFunc::I_O, result_phi_i_o);
1386   }
1387   _igvn.replace_node(alloc, call);
1388   transform_later(call);
1389 
1390   // Identify the output projections from the allocate node and
1391   // adjust any references to them.
1392   // The control and io projections look like:
1393   //
1394   //        v---Proj(ctrl) <-----+   v---CatchProj(ctrl)
1395   //  Allocate                   Catch
1396   //        ^---Proj(io) <-------+   ^---CatchProj(io)
1397   //
1398   //  We are interested in the CatchProj nodes.
1399   //
1400   call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1401 
1402   // An allocate node has separate memory projections for the uses on
1403   // the control and i_o paths. Replace the control memory projection with
1404   // result_phi_rawmem (unless we are only generating a slow call when
1405   // both memory projections are combined)
1406   if (expand_fast_path && _callprojs.fallthrough_memproj != NULL) {
1407     migrate_outs(_callprojs.fallthrough_memproj, result_phi_rawmem);
1408   }
1409   // Now change uses of catchall_memproj to use fallthrough_memproj and delete
1410   // catchall_memproj so we end up with a call that has only 1 memory projection.
1411   if (_callprojs.catchall_memproj != NULL ) {
1412     if (_callprojs.fallthrough_memproj == NULL) {
1413       _callprojs.fallthrough_memproj = new ProjNode(call, TypeFunc::Memory);
1414       transform_later(_callprojs.fallthrough_memproj);
1415     }
1416     migrate_outs(_callprojs.catchall_memproj, _callprojs.fallthrough_memproj);
1417     _igvn.remove_dead_node(_callprojs.catchall_memproj);
1418   }
1419 
1420   // An allocate node has separate i_o projections for the uses on the control
1421   // and i_o paths. Always replace the control i_o projection with result i_o
1422   // otherwise incoming i_o become dead when only a slow call is generated
1423   // (it is different from memory projections where both projections are
1424   // combined in such case).
1425   if (_callprojs.fallthrough_ioproj != NULL) {
1426     migrate_outs(_callprojs.fallthrough_ioproj, result_phi_i_o);
1427   }
1428   // Now change uses of catchall_ioproj to use fallthrough_ioproj and delete
1429   // catchall_ioproj so we end up with a call that has only 1 i_o projection.
1430   if (_callprojs.catchall_ioproj != NULL ) {
1431     if (_callprojs.fallthrough_ioproj == NULL) {
1432       _callprojs.fallthrough_ioproj = new ProjNode(call, TypeFunc::I_O);
1433       transform_later(_callprojs.fallthrough_ioproj);
1434     }
1435     migrate_outs(_callprojs.catchall_ioproj, _callprojs.fallthrough_ioproj);
1436     _igvn.remove_dead_node(_callprojs.catchall_ioproj);
1437   }
1438 
1439   // if we generated only a slow call, we are done
1440   if (!expand_fast_path) {
1441     // Now we can unhook i_o.
1442     if (result_phi_i_o->outcnt() > 1) {
1443       call->set_req(TypeFunc::I_O, top());
1444     } else {
1445       assert(result_phi_i_o->unique_ctrl_out() == call, "sanity");
1446       // Case of new array with negative size known during compilation.
1447       // AllocateArrayNode::Ideal() optimization disconnect unreachable
1448       // following code since call to runtime will throw exception.
1449       // As result there will be no users of i_o after the call.
1450       // Leave i_o attached to this call to avoid problems in preceding graph.
1451     }
1452     return;
1453   }
1454 
1455   if (_callprojs.fallthrough_catchproj != NULL) {
1456     ctrl = _callprojs.fallthrough_catchproj->clone();
1457     transform_later(ctrl);
1458     _igvn.replace_node(_callprojs.fallthrough_catchproj, result_region);
1459   } else {
1460     ctrl = top();
1461   }
1462   Node *slow_result;
1463   if (_callprojs.resproj == NULL) {
1464     // no uses of the allocation result
1465     slow_result = top();
1466   } else {
1467     slow_result = _callprojs.resproj->clone();
1468     transform_later(slow_result);
1469     _igvn.replace_node(_callprojs.resproj, result_phi_rawoop);
1470   }
1471 
1472   // Plug slow-path into result merge point
1473   result_region->init_req( slow_result_path, ctrl);
1474   transform_later(result_region);
1475   if (allocation_has_use) {
1476     result_phi_rawoop->init_req(slow_result_path, slow_result);
1477     transform_later(result_phi_rawoop);
1478   }
1479   result_phi_rawmem->init_req(slow_result_path, _callprojs.fallthrough_memproj);
1480   transform_later(result_phi_rawmem);
1481   transform_later(result_phi_i_o);
1482   // This completes all paths into the result merge point
1483 }
1484 
1485 // Remove alloc node that has no uses.
1486 void PhaseMacroExpand::yank_alloc_node(AllocateNode* alloc) {
1487   Node* ctrl = alloc->in(TypeFunc::Control);
1488   Node* mem  = alloc->in(TypeFunc::Memory);
1489   Node* i_o  = alloc->in(TypeFunc::I_O);
1490 
1491   alloc->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1492   if (_callprojs.resproj != NULL) {
1493     for (DUIterator_Fast imax, i = _callprojs.resproj->fast_outs(imax); i < imax; i++) {
1494       Node* use = _callprojs.resproj->fast_out(i);
1495       use->isa_MemBar()->remove(&_igvn);
1496       --imax;
1497       --i; // back up iterator
1498     }
1499     assert(_callprojs.resproj->outcnt() == 0, "all uses must be deleted");
1500     _igvn.remove_dead_node(_callprojs.resproj);
1501   }
1502   if (_callprojs.fallthrough_catchproj != NULL) {
1503     migrate_outs(_callprojs.fallthrough_catchproj, ctrl);
1504     _igvn.remove_dead_node(_callprojs.fallthrough_catchproj);
1505   }
1506   if (_callprojs.catchall_catchproj != NULL) {
1507     _igvn.rehash_node_delayed(_callprojs.catchall_catchproj);
1508     _callprojs.catchall_catchproj->set_req(0, top());
1509   }
1510   if (_callprojs.fallthrough_proj != NULL) {
1511     Node* catchnode = _callprojs.fallthrough_proj->unique_ctrl_out();
1512     _igvn.remove_dead_node(catchnode);
1513     _igvn.remove_dead_node(_callprojs.fallthrough_proj);
1514   }
1515   if (_callprojs.fallthrough_memproj != NULL) {
1516     migrate_outs(_callprojs.fallthrough_memproj, mem);
1517     _igvn.remove_dead_node(_callprojs.fallthrough_memproj);
1518   }
1519   if (_callprojs.fallthrough_ioproj != NULL) {
1520     migrate_outs(_callprojs.fallthrough_ioproj, i_o);
1521     _igvn.remove_dead_node(_callprojs.fallthrough_ioproj);
1522   }
1523   if (_callprojs.catchall_memproj != NULL) {
1524     _igvn.rehash_node_delayed(_callprojs.catchall_memproj);
1525     _callprojs.catchall_memproj->set_req(0, top());
1526   }
1527   if (_callprojs.catchall_ioproj != NULL) {
1528     _igvn.rehash_node_delayed(_callprojs.catchall_ioproj);
1529     _callprojs.catchall_ioproj->set_req(0, top());
1530   }
1531 #ifndef PRODUCT
1532   if (PrintEliminateAllocations) {
1533     if (alloc->is_AllocateArray()) {
1534       tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
1535     } else {
1536       tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
1537     }
1538   }
1539 #endif
1540   _igvn.remove_dead_node(alloc);
1541 }
1542 
1543 void PhaseMacroExpand::expand_initialize_membar(AllocateNode* alloc, InitializeNode* init,
1544                                                 Node*& fast_oop_ctrl, Node*& fast_oop_rawmem) {
1545   // If initialization is performed by an array copy, any required
1546   // MemBarStoreStore was already added. If the object does not
1547   // escape no need for a MemBarStoreStore. If the object does not
1548   // escape in its initializer and memory barrier (MemBarStoreStore or
1549   // stronger) is already added at exit of initializer, also no need

1627     Node* thread = new ThreadLocalNode();
1628     transform_later(thread);
1629 
1630     call->init_req(TypeFunc::Parms + 0, thread);
1631     call->init_req(TypeFunc::Parms + 1, oop);
1632     call->init_req(TypeFunc::Control, ctrl);
1633     call->init_req(TypeFunc::I_O    , top()); // does no i/o
1634     call->init_req(TypeFunc::Memory , rawmem);
1635     call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1636     call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr));
1637     transform_later(call);
1638     ctrl = new ProjNode(call, TypeFunc::Control);
1639     transform_later(ctrl);
1640     rawmem = new ProjNode(call, TypeFunc::Memory);
1641     transform_later(rawmem);
1642   }
1643 }
1644 
1645 // Helper for PhaseMacroExpand::expand_allocate_common.
1646 // Initializes the newly-allocated storage.
1647 Node*
1648 PhaseMacroExpand::initialize_object(AllocateNode* alloc,
1649                                     Node* control, Node* rawmem, Node* object,
1650                                     Node* klass_node, Node* length,
1651                                     Node* size_in_bytes) {
1652   InitializeNode* init = alloc->initialization();
1653   // Store the klass & mark bits
1654   Node* mark_node = alloc->make_ideal_mark(&_igvn, object, control, rawmem);
1655   if (!mark_node->is_Con()) {
1656     transform_later(mark_node);
1657   }
1658   rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, TypeX_X->basic_type());
1659 
1660   rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA);
1661   int header_size = alloc->minimum_header_size();  // conservatively small
1662 
1663   // Array length
1664   if (length != NULL) {         // Arrays need length field
1665     rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
1666     // conservatively small header size:
1667     header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1668     if (_igvn.type(klass_node)->isa_aryklassptr()) {   // we know the exact header size in most cases:
1669       BasicType elem = _igvn.type(klass_node)->is_klassptr()->as_instance_type()->isa_aryptr()->elem()->array_element_basic_type();
1670       if (is_reference_type(elem, true)) {
1671         elem = T_OBJECT;
1672       }
1673       header_size = Klass::layout_helper_header_size(Klass::array_layout_helper(elem));
1674     }
1675   }
1676 
1677   // Clear the object body, if necessary.
1678   if (init == NULL) {
1679     // The init has somehow disappeared; be cautious and clear everything.
1680     //
1681     // This can happen if a node is allocated but an uncommon trap occurs
1682     // immediately.  In this case, the Initialize gets associated with the
1683     // trap, and may be placed in a different (outer) loop, if the Allocate
1684     // is in a loop.  If (this is rare) the inner loop gets unrolled, then
1685     // there can be two Allocates to one Initialize.  The answer in all these
1686     // edge cases is safety first.  It is always safe to clear immediately
1687     // within an Allocate, and then (maybe or maybe not) clear some more later.
1688     if (!(UseTLAB && ZeroTLAB)) {
1689       rawmem = ClearArrayNode::clear_memory(control, rawmem, object,


1690                                             header_size, size_in_bytes,
1691                                             &_igvn);
1692     }
1693   } else {
1694     if (!init->is_complete()) {
1695       // Try to win by zeroing only what the init does not store.
1696       // We can also try to do some peephole optimizations,
1697       // such as combining some adjacent subword stores.
1698       rawmem = init->complete_stores(control, rawmem, object,
1699                                      header_size, size_in_bytes, &_igvn);
1700     }
1701     // We have no more use for this link, since the AllocateNode goes away:
1702     init->set_req(InitializeNode::RawAddress, top());
1703     // (If we keep the link, it just confuses the register allocator,
1704     // who thinks he sees a real use of the address by the membar.)
1705   }
1706 
1707   return rawmem;
1708 }
1709 

2038   } // EliminateNestedLocks
2039 
2040   if (alock->is_non_esc_obj()) { // Lock is used for non escaping object
2041     // Look for all locks of this object and mark them and
2042     // corresponding BoxLock nodes as eliminated.
2043     Node* obj = alock->obj_node();
2044     for (uint j = 0; j < obj->outcnt(); j++) {
2045       Node* o = obj->raw_out(j);
2046       if (o->is_AbstractLock() &&
2047           o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) {
2048         alock = o->as_AbstractLock();
2049         Node* box = alock->box_node();
2050         // Replace old box node with new eliminated box for all users
2051         // of the same object and mark related locks as eliminated.
2052         mark_eliminated_box(box, obj);
2053       }
2054     }
2055   }
2056 }
2057 











































2058 // we have determined that this lock/unlock can be eliminated, we simply
2059 // eliminate the node without expanding it.
2060 //
2061 // Note:  The membar's associated with the lock/unlock are currently not
2062 //        eliminated.  This should be investigated as a future enhancement.
2063 //
2064 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
2065 
2066   if (!alock->is_eliminated()) {
2067     return false;
2068   }
2069 #ifdef ASSERT
2070   if (!alock->is_coarsened()) {
2071     // Check that new "eliminated" BoxLock node is created.
2072     BoxLockNode* oldbox = alock->box_node()->as_BoxLock();
2073     assert(oldbox->is_eliminated(), "should be done already");
2074   }
2075 #endif
2076 
2077   alock->log_lock_optimization(C, "eliminate_lock");
2078 
2079 #ifndef PRODUCT
2080   if (PrintEliminateLocks) {
2081     tty->print_cr("++++ Eliminated: %d %s '%s'", alock->_idx, (alock->is_Lock() ? "Lock" : "Unlock"), alock->kind_as_string());
2082   }
2083 #endif
2084 
2085   Node* mem  = alock->in(TypeFunc::Memory);
2086   Node* ctrl = alock->in(TypeFunc::Control);
2087   guarantee(ctrl != NULL, "missing control projection, cannot replace_node() with NULL");
2088 
2089   alock->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
2090   // There are 2 projections from the lock.  The lock node will
2091   // be deleted when its last use is subsumed below.
2092   assert(alock->outcnt() == 2 &&
2093          _callprojs.fallthrough_proj != NULL &&
2094          _callprojs.fallthrough_memproj != NULL,
2095          "Unexpected projections from Lock/Unlock");
2096 
2097   Node* fallthroughproj = _callprojs.fallthrough_proj;
2098   Node* memproj_fallthrough = _callprojs.fallthrough_memproj;
2099 
2100   // The memory projection from a lock/unlock is RawMem
2101   // The input to a Lock is merged memory, so extract its RawMem input
2102   // (unless the MergeMem has been optimized away.)
2103   if (alock->is_Lock()) {



2104     // Search for MemBarAcquireLock node and delete it also.
2105     MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
2106     assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, "");
2107     Node* ctrlproj = membar->proj_out(TypeFunc::Control);
2108     Node* memproj = membar->proj_out(TypeFunc::Memory);
2109     _igvn.replace_node(ctrlproj, fallthroughproj);
2110     _igvn.replace_node(memproj, memproj_fallthrough);
2111 
2112     // Delete FastLock node also if this Lock node is unique user
2113     // (a loop peeling may clone a Lock node).
2114     Node* flock = alock->as_Lock()->fastlock_node();
2115     if (flock->outcnt() == 1) {
2116       assert(flock->unique_out() == alock, "sanity");
2117       _igvn.replace_node(flock, top());
2118     }
2119   }
2120 
2121   // Search for MemBarReleaseLock node and delete it also.
2122   if (alock->is_Unlock() && ctrl->is_Proj() && ctrl->in(0)->is_MemBar()) {
2123     MemBarNode* membar = ctrl->in(0)->as_MemBar();

2144   Node* mem = lock->in(TypeFunc::Memory);
2145   Node* obj = lock->obj_node();
2146   Node* box = lock->box_node();
2147   Node* flock = lock->fastlock_node();
2148 
2149   assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2150 
2151   // Make the merge point
2152   Node *region;
2153   Node *mem_phi;
2154   Node *slow_path;
2155 
2156   region  = new RegionNode(3);
2157   // create a Phi for the memory state
2158   mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2159 
2160   // Optimize test; set region slot 2
2161   slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0);
2162   mem_phi->init_req(2, mem);
2163 



2164   // Make slow path call
2165   CallNode *call = make_slow_call((CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(),
2166                                   OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path,
2167                                   obj, box, NULL);
2168 
2169   call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
2170 
2171   // Slow path can only throw asynchronous exceptions, which are always
2172   // de-opted.  So the compiler thinks the slow-call can never throw an
2173   // exception.  If it DOES throw an exception we would need the debug
2174   // info removed first (since if it throws there is no monitor).
2175   assert(_callprojs.fallthrough_ioproj == NULL && _callprojs.catchall_ioproj == NULL &&
2176          _callprojs.catchall_memproj == NULL && _callprojs.catchall_catchproj == NULL, "Unexpected projection from Lock");
2177 
2178   // Capture slow path
2179   // disconnect fall-through projection from call and create a new one
2180   // hook up users of fall-through projection to region
2181   Node *slow_ctrl = _callprojs.fallthrough_proj->clone();
2182   transform_later(slow_ctrl);
2183   _igvn.hash_delete(_callprojs.fallthrough_proj);
2184   _callprojs.fallthrough_proj->disconnect_inputs(C);
2185   region->init_req(1, slow_ctrl);
2186   // region inputs are now complete
2187   transform_later(region);
2188   _igvn.replace_node(_callprojs.fallthrough_proj, region);
2189 
2190   Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory));
2191 
2192   mem_phi->init_req(1, memproj);
2193 
2194   transform_later(mem_phi);
2195 
2196   _igvn.replace_node(_callprojs.fallthrough_memproj, mem_phi);
2197 }
2198 
2199 //------------------------------expand_unlock_node----------------------
2200 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
2201 
2202   Node* ctrl = unlock->in(TypeFunc::Control);
2203   Node* mem = unlock->in(TypeFunc::Memory);
2204   Node* obj = unlock->obj_node();
2205   Node* box = unlock->box_node();
2206 
2207   assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2208 
2209   // No need for a null check on unlock
2210 
2211   // Make the merge point
2212   Node *region;
2213   Node *mem_phi;
2214 
2215   region  = new RegionNode(3);
2216   // create a Phi for the memory state
2217   mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2218 
2219   FastUnlockNode *funlock = new FastUnlockNode( ctrl, obj, box );
2220   funlock = transform_later( funlock )->as_FastUnlock();
2221   // Optimize test; set region slot 2
2222   Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0);
2223   Node *thread = transform_later(new ThreadLocalNode());
2224 
2225   CallNode *call = make_slow_call((CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(),
2226                                   CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C),
2227                                   "complete_monitor_unlocking_C", slow_path, obj, box, thread);
2228 
2229   call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
2230   assert(_callprojs.fallthrough_ioproj == NULL && _callprojs.catchall_ioproj == NULL &&
2231          _callprojs.catchall_memproj == NULL && _callprojs.catchall_catchproj == NULL, "Unexpected projection from Lock");
2232 
2233   // No exceptions for unlocking
2234   // Capture slow path
2235   // disconnect fall-through projection from call and create a new one
2236   // hook up users of fall-through projection to region
2237   Node *slow_ctrl = _callprojs.fallthrough_proj->clone();
2238   transform_later(slow_ctrl);
2239   _igvn.hash_delete(_callprojs.fallthrough_proj);
2240   _callprojs.fallthrough_proj->disconnect_inputs(C);
2241   region->init_req(1, slow_ctrl);
2242   // region inputs are now complete
2243   transform_later(region);
2244   _igvn.replace_node(_callprojs.fallthrough_proj, region);
2245 
2246   Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory) );
2247   mem_phi->init_req(1, memproj );
2248   mem_phi->init_req(2, mem);
2249   transform_later(mem_phi);
2250 
2251   _igvn.replace_node(_callprojs.fallthrough_memproj, mem_phi);
2252 }
2253 








































































































































































































2254 void PhaseMacroExpand::expand_subtypecheck_node(SubTypeCheckNode *check) {
2255   assert(check->in(SubTypeCheckNode::Control) == NULL, "should be pinned");
2256   Node* bol = check->unique_out();
2257   Node* obj_or_subklass = check->in(SubTypeCheckNode::ObjOrSubKlass);
2258   Node* superklass = check->in(SubTypeCheckNode::SuperKlass);
2259   assert(bol->is_Bool() && bol->as_Bool()->_test._test == BoolTest::ne, "unexpected bool node");
2260 
2261   for (DUIterator_Last imin, i = bol->last_outs(imin); i >= imin; --i) {
2262     Node* iff = bol->last_out(i);
2263     assert(iff->is_If(), "where's the if?");
2264 
2265     if (iff->in(0)->is_top()) {
2266       _igvn.replace_input_of(iff, 1, C->top());
2267       continue;
2268     }
2269 
2270     Node* iftrue = iff->as_If()->proj_out(1);
2271     Node* iffalse = iff->as_If()->proj_out(0);
2272     Node* ctrl = iff->in(0);
2273 
2274     Node* subklass = NULL;
2275     if (_igvn.type(obj_or_subklass)->isa_klassptr()) {
2276       subklass = obj_or_subklass;
2277     } else {
2278       Node* k_adr = basic_plus_adr(obj_or_subklass, oopDesc::klass_offset_in_bytes());
2279       subklass = _igvn.transform(LoadKlassNode::make(_igvn, NULL, C->immutable_memory(), k_adr, TypeInstPtr::KLASS));
2280     }
2281 
2282     Node* not_subtype_ctrl = Phase::gen_subtype_check(subklass, superklass, &ctrl, NULL, _igvn);
2283 
2284     _igvn.replace_input_of(iff, 0, C->top());
2285     _igvn.replace_node(iftrue, not_subtype_ctrl);
2286     _igvn.replace_node(iffalse, ctrl);
2287   }
2288   _igvn.replace_node(check, C->top());
2289 }
2290 





































































































2291 //---------------------------eliminate_macro_nodes----------------------
2292 // Eliminate scalar replaced allocations and associated locks.
2293 void PhaseMacroExpand::eliminate_macro_nodes() {
2294   if (C->macro_count() == 0)
2295     return;
2296   NOT_PRODUCT(int membar_before = count_MemBar(C);)
2297 
2298   // Before elimination may re-mark (change to Nested or NonEscObj)
2299   // all associated (same box and obj) lock and unlock nodes.
2300   int cnt = C->macro_count();
2301   for (int i=0; i < cnt; i++) {
2302     Node *n = C->macro_node(i);
2303     if (n->is_AbstractLock()) { // Lock and Unlock nodes
2304       mark_eliminated_locking_nodes(n->as_AbstractLock());
2305     }
2306   }
2307   // Re-marking may break consistency of Coarsened locks.
2308   if (!C->coarsened_locks_consistent()) {
2309     return; // recompile without Coarsened locks if broken
2310   }

2331   }
2332   // Next, attempt to eliminate allocations
2333   _has_locks = false;
2334   progress = true;
2335   while (progress) {
2336     progress = false;
2337     for (int i = C->macro_count(); i > 0; i = MIN2(i - 1, C->macro_count())) { // more than 1 element can be eliminated at once
2338       Node* n = C->macro_node(i - 1);
2339       bool success = false;
2340       DEBUG_ONLY(int old_macro_count = C->macro_count();)
2341       switch (n->class_id()) {
2342       case Node::Class_Allocate:
2343       case Node::Class_AllocateArray:
2344         success = eliminate_allocate_node(n->as_Allocate());
2345 #ifndef PRODUCT
2346         if (success && PrintOptoStatistics) {
2347           Atomic::inc(&PhaseMacroExpand::_objs_scalar_replaced_counter);
2348         }
2349 #endif
2350         break;
2351       case Node::Class_CallStaticJava:
2352         success = eliminate_boxing_node(n->as_CallStaticJava());



2353         break;

2354       case Node::Class_Lock:
2355       case Node::Class_Unlock:
2356         assert(!n->as_AbstractLock()->is_eliminated(), "sanity");
2357         _has_locks = true;
2358         break;
2359       case Node::Class_ArrayCopy:
2360         break;
2361       case Node::Class_OuterStripMinedLoop:
2362         break;
2363       case Node::Class_SubTypeCheck:
2364         break;
2365       case Node::Class_Opaque1:
2366         break;


2367       default:
2368         assert(n->Opcode() == Op_LoopLimit ||
2369                n->Opcode() == Op_Opaque2   ||
2370                n->Opcode() == Op_Opaque3   ||
2371                n->Opcode() == Op_Opaque4   ||
2372                BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(n),
2373                "unknown node type in macro list");
2374       }
2375       assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
2376       progress = progress || success;
2377     }
2378   }
2379 #ifndef PRODUCT
2380   if (PrintOptoStatistics) {
2381     int membar_after = count_MemBar(C);
2382     Atomic::add(&PhaseMacroExpand::_memory_barriers_removed_counter, membar_before - membar_after);
2383   }
2384 #endif
2385 }
2386 

2388 //  Returns true if a failure occurred.
2389 bool PhaseMacroExpand::expand_macro_nodes() {
2390   // Last attempt to eliminate macro nodes.
2391   eliminate_macro_nodes();
2392   if (C->failing())  return true;
2393 
2394   // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations.
2395   bool progress = true;
2396   while (progress) {
2397     progress = false;
2398     for (int i = C->macro_count(); i > 0; i--) {
2399       Node* n = C->macro_node(i-1);
2400       bool success = false;
2401       DEBUG_ONLY(int old_macro_count = C->macro_count();)
2402       if (n->Opcode() == Op_LoopLimit) {
2403         // Remove it from macro list and put on IGVN worklist to optimize.
2404         C->remove_macro_node(n);
2405         _igvn._worklist.push(n);
2406         success = true;
2407       } else if (n->Opcode() == Op_CallStaticJava) {
2408         // Remove it from macro list and put on IGVN worklist to optimize.
2409         C->remove_macro_node(n);
2410         _igvn._worklist.push(n);
2411         success = true;



2412       } else if (n->is_Opaque1() || n->Opcode() == Op_Opaque2) {
2413         _igvn.replace_node(n, n->in(1));
2414         success = true;
2415 #if INCLUDE_RTM_OPT
2416       } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) {
2417         assert(C->profile_rtm(), "should be used only in rtm deoptimization code");
2418         assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), "");
2419         Node* cmp = n->unique_out();
2420 #ifdef ASSERT
2421         // Validate graph.
2422         assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), "");
2423         BoolNode* bol = cmp->unique_out()->as_Bool();
2424         assert((bol->outcnt() == 1) && bol->unique_out()->is_If() &&
2425                (bol->_test._test == BoolTest::ne), "");
2426         IfNode* ifn = bol->unique_out()->as_If();
2427         assert((ifn->outcnt() == 2) &&
2428                ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change) != NULL, "");
2429 #endif
2430         Node* repl = n->in(1);
2431         if (!_has_locks) {

2494     // Worst case is a macro node gets expanded into about 200 nodes.
2495     // Allow 50% more for optimization.
2496     if (C->check_node_count(300, "out of nodes before macro expansion")) {
2497       return true;
2498     }
2499 
2500     DEBUG_ONLY(int old_macro_count = C->macro_count();)
2501     switch (n->class_id()) {
2502     case Node::Class_Lock:
2503       expand_lock_node(n->as_Lock());
2504       break;
2505     case Node::Class_Unlock:
2506       expand_unlock_node(n->as_Unlock());
2507       break;
2508     case Node::Class_ArrayCopy:
2509       expand_arraycopy_node(n->as_ArrayCopy());
2510       break;
2511     case Node::Class_SubTypeCheck:
2512       expand_subtypecheck_node(n->as_SubTypeCheck());
2513       break;







2514     default:
2515       assert(false, "unknown node type in macro list");
2516     }
2517     assert(C->macro_count() == (old_macro_count - 1), "expansion must have deleted one node from macro list");
2518     if (C->failing())  return true;
2519 
2520     // Clean up the graph so we're less likely to hit the maximum node
2521     // limit
2522     _igvn.set_delay_transform(false);
2523     _igvn.optimize();
2524     if (C->failing())  return true;
2525     _igvn.set_delay_transform(true);
2526   }
2527 
2528   // All nodes except Allocate nodes are expanded now. There could be
2529   // new optimization opportunities (such as folding newly created
2530   // load from a just allocated object). Run IGVN.
2531 
2532   // expand "macro" nodes
2533   // nodes are removed from the macro list as they are processed

   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciFlatArrayKlass.hpp"
  27 #include "compiler/compileLog.hpp"
  28 #include "gc/shared/collectedHeap.inline.hpp"
  29 #include "gc/shared/tlab_globals.hpp"
  30 #include "libadt/vectset.hpp"
  31 #include "memory/universe.hpp"
  32 #include "opto/addnode.hpp"
  33 #include "opto/arraycopynode.hpp"
  34 #include "opto/callnode.hpp"
  35 #include "opto/castnode.hpp"
  36 #include "opto/cfgnode.hpp"
  37 #include "opto/compile.hpp"
  38 #include "opto/convertnode.hpp"
  39 #include "opto/graphKit.hpp"
  40 #include "opto/inlinetypenode.hpp"
  41 #include "opto/intrinsicnode.hpp"
  42 #include "opto/locknode.hpp"
  43 #include "opto/loopnode.hpp"
  44 #include "opto/macro.hpp"
  45 #include "opto/memnode.hpp"
  46 #include "opto/narrowptrnode.hpp"
  47 #include "opto/node.hpp"
  48 #include "opto/opaquenode.hpp"
  49 #include "opto/phaseX.hpp"
  50 #include "opto/rootnode.hpp"
  51 #include "opto/runtime.hpp"
  52 #include "opto/subnode.hpp"
  53 #include "opto/subtypenode.hpp"
  54 #include "opto/type.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "runtime/continuation.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/stubRoutines.hpp"
  59 #include "utilities/macros.hpp"
  60 #include "utilities/powerOfTwo.hpp"
  61 #if INCLUDE_G1GC
  62 #include "gc/g1/g1ThreadLocalData.hpp"
  63 #endif // INCLUDE_G1GC
  64 #if INCLUDE_SHENANDOAHGC
  65 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  66 #endif
  67 
  68 
  69 //
  70 // Replace any references to "oldref" in inputs to "use" with "newref".
  71 // Returns the number of replacements made.
  72 //
  73 int PhaseMacroExpand::replace_input(Node *use, Node *oldref, Node *newref) {
  74   int nreplacements = 0;
  75   uint req = use->req();
  76   for (uint j = 0; j < use->len(); j++) {
  77     Node *uin = use->in(j);
  78     if (uin == oldref) {
  79       if (j < req)
  80         use->set_req(j, newref);
  81       else
  82         use->set_prec(j, newref);
  83       nreplacements++;
  84     } else if (j >= req && uin == NULL) {
  85       break;
  86     }
  87   }
  88   return nreplacements;
  89 }
  90 












  91 Node* PhaseMacroExpand::opt_bits_test(Node* ctrl, Node* region, int edge, Node* word, int mask, int bits, bool return_fast_path) {
  92   Node* cmp;
  93   if (mask != 0) {
  94     Node* and_node = transform_later(new AndXNode(word, MakeConX(mask)));
  95     cmp = transform_later(new CmpXNode(and_node, MakeConX(bits)));
  96   } else {
  97     cmp = word;
  98   }
  99   Node* bol = transform_later(new BoolNode(cmp, BoolTest::ne));
 100   IfNode* iff = new IfNode( ctrl, bol, PROB_MIN, COUNT_UNKNOWN );
 101   transform_later(iff);
 102 
 103   // Fast path taken.
 104   Node *fast_taken = transform_later(new IfFalseNode(iff));
 105 
 106   // Fast path not-taken, i.e. slow path
 107   Node *slow_taken = transform_later(new IfTrueNode(iff));
 108 
 109   if (return_fast_path) {
 110     region->init_req(edge, slow_taken); // Capture slow-control

 133   // Slow-path call
 134  CallNode *call = leaf_name
 135    ? (CallNode*)new CallLeafNode      ( slow_call_type, slow_call, leaf_name, TypeRawPtr::BOTTOM )
 136    : (CallNode*)new CallStaticJavaNode( slow_call_type, slow_call, OptoRuntime::stub_name(slow_call), TypeRawPtr::BOTTOM );
 137 
 138   // Slow path call has no side-effects, uses few values
 139   copy_predefined_input_for_runtime_call(slow_path, oldcall, call );
 140   if (parm0 != NULL)  call->init_req(TypeFunc::Parms+0, parm0);
 141   if (parm1 != NULL)  call->init_req(TypeFunc::Parms+1, parm1);
 142   if (parm2 != NULL)  call->init_req(TypeFunc::Parms+2, parm2);
 143   call->copy_call_debug_info(&_igvn, oldcall);
 144   call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
 145   _igvn.replace_node(oldcall, call);
 146   transform_later(call);
 147 
 148   return call;
 149 }
 150 
 151 void PhaseMacroExpand::eliminate_gc_barrier(Node* p2x) {
 152   BarrierSetC2 *bs = BarrierSet::barrier_set()->barrier_set_c2();
 153   bs->eliminate_gc_barrier(&_igvn, p2x);
 154 #ifndef PRODUCT
 155   if (PrintOptoStatistics) {
 156     Atomic::inc(&PhaseMacroExpand::_GC_barriers_removed_counter);
 157   }
 158 #endif
 159 }
 160 
 161 // Search for a memory operation for the specified memory slice.
 162 static Node *scan_mem_chain(Node *mem, int alias_idx, int offset, Node *start_mem, Node *alloc, PhaseGVN *phase) {
 163   Node *orig_mem = mem;
 164   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 165   const TypeOopPtr *tinst = phase->C->get_adr_type(alias_idx)->isa_oopptr();
 166   while (true) {
 167     if (mem == alloc_mem || mem == start_mem ) {
 168       return mem;  // hit one of our sentinels
 169     } else if (mem->is_MergeMem()) {
 170       mem = mem->as_MergeMem()->memory_at(alias_idx);
 171     } else if (mem->is_Proj() && mem->as_Proj()->_con == TypeFunc::Memory) {
 172       Node *in = mem->in(0);
 173       // we can safely skip over safepoints, calls, locks and membars because we

 187         ArrayCopyNode* ac = NULL;
 188         if (ArrayCopyNode::may_modify(tinst, in->as_MemBar(), phase, ac)) {
 189           if (ac != NULL) {
 190             assert(ac->is_clonebasic(), "Only basic clone is a non escaping clone");
 191             return ac;
 192           }
 193         }
 194         mem = in->in(TypeFunc::Memory);
 195       } else {
 196 #ifdef ASSERT
 197         in->dump();
 198         mem->dump();
 199         assert(false, "unexpected projection");
 200 #endif
 201       }
 202     } else if (mem->is_Store()) {
 203       const TypePtr* atype = mem->as_Store()->adr_type();
 204       int adr_idx = phase->C->get_alias_index(atype);
 205       if (adr_idx == alias_idx) {
 206         assert(atype->isa_oopptr(), "address type must be oopptr");
 207         int adr_offset = atype->flattened_offset();
 208         uint adr_iid = atype->is_oopptr()->instance_id();
 209         // Array elements references have the same alias_idx
 210         // but different offset and different instance_id.
 211         if (adr_offset == offset && adr_iid == alloc->_idx) {
 212           return mem;
 213         }
 214       } else {
 215         assert(adr_idx == Compile::AliasIdxRaw, "address must match or be raw");
 216       }
 217       mem = mem->in(MemNode::Memory);
 218     } else if (mem->is_ClearArray()) {
 219       if (!ClearArrayNode::step_through(&mem, alloc->_idx, phase)) {
 220         // Can not bypass initialization of the instance
 221         // we are looking.
 222         debug_only(intptr_t offset;)
 223         assert(alloc == AllocateNode::Ideal_allocation(mem->in(3), phase, offset), "sanity");
 224         InitializeNode* init = alloc->as_Allocate()->initialization();
 225         // We are looking for stored value, return Initialize node
 226         // or memory edge from Allocate node.
 227         if (init != NULL) {

 232       }
 233       // Otherwise skip it (the call updated 'mem' value).
 234     } else if (mem->Opcode() == Op_SCMemProj) {
 235       mem = mem->in(0);
 236       Node* adr = NULL;
 237       if (mem->is_LoadStore()) {
 238         adr = mem->in(MemNode::Address);
 239       } else {
 240         assert(mem->Opcode() == Op_EncodeISOArray ||
 241                mem->Opcode() == Op_StrCompressedCopy, "sanity");
 242         adr = mem->in(3); // Destination array
 243       }
 244       const TypePtr* atype = adr->bottom_type()->is_ptr();
 245       int adr_idx = phase->C->get_alias_index(atype);
 246       if (adr_idx == alias_idx) {
 247         DEBUG_ONLY(mem->dump();)
 248         assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
 249         return NULL;
 250       }
 251       mem = mem->in(MemNode::Memory);
 252     } else if (mem->Opcode() == Op_StrInflatedCopy) {
 253       Node* adr = mem->in(3); // Destination array
 254       const TypePtr* atype = adr->bottom_type()->is_ptr();
 255       int adr_idx = phase->C->get_alias_index(atype);
 256       if (adr_idx == alias_idx) {
 257         DEBUG_ONLY(mem->dump();)
 258         assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field");
 259         return NULL;
 260       }
 261       mem = mem->in(MemNode::Memory);
 262     } else {
 263       return mem;
 264     }
 265     assert(mem != orig_mem, "dead memory loop");
 266   }
 267 }
 268 
 269 // Generate loads from source of the arraycopy for fields of
 270 // destination needed at a deoptimization point
 271 Node* PhaseMacroExpand::make_arraycopy_load(ArrayCopyNode* ac, intptr_t offset, Node* ctl, Node* mem, BasicType ft, const Type *ftype, AllocateNode *alloc) {
 272   BasicType bt = ft;

 277   }
 278   Node* res = NULL;
 279   if (ac->is_clonebasic()) {
 280     assert(ac->in(ArrayCopyNode::Src) != ac->in(ArrayCopyNode::Dest), "clone source equals destination");
 281     Node* base = ac->in(ArrayCopyNode::Src);
 282     Node* adr = _igvn.transform(new AddPNode(base, base, MakeConX(offset)));
 283     const TypePtr* adr_type = _igvn.type(base)->is_ptr()->add_offset(offset);
 284     MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem();
 285     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 286     res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt);
 287   } else {
 288     if (ac->modifies(offset, offset, &_igvn, true)) {
 289       assert(ac->in(ArrayCopyNode::Dest) == alloc->result_cast(), "arraycopy destination should be allocation's result");
 290       uint shift = exact_log2(type2aelembytes(bt));
 291       Node* src_pos = ac->in(ArrayCopyNode::SrcPos);
 292       Node* dest_pos = ac->in(ArrayCopyNode::DestPos);
 293       const TypeInt* src_pos_t = _igvn.type(src_pos)->is_int();
 294       const TypeInt* dest_pos_t = _igvn.type(dest_pos)->is_int();
 295 
 296       Node* adr = NULL;
 297       Node* base = ac->in(ArrayCopyNode::Src);
 298       const TypeAryPtr* adr_type = _igvn.type(base)->is_aryptr();
 299       if (adr_type->is_aryptr()->is_flat()) {
 300         shift = adr_type->flat_log_elem_size();
 301       }
 302       if (src_pos_t->is_con() && dest_pos_t->is_con()) {
 303         intptr_t off = ((src_pos_t->get_con() - dest_pos_t->get_con()) << shift) + offset;

 304         adr = _igvn.transform(new AddPNode(base, base, MakeConX(off)));
 305         adr_type = _igvn.type(adr)->is_aryptr();
 306         assert(adr_type == _igvn.type(base)->is_aryptr()->add_field_offset_and_offset(off), "incorrect address type");
 307         if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) {
 308           // Don't emit a new load from src if src == dst but try to get the value from memory instead
 309           return value_from_mem(ac->in(TypeFunc::Memory), ctl, ft, ftype, adr_type, alloc);
 310         }
 311       } else {
 312         if (ac->in(ArrayCopyNode::Src) == ac->in(ArrayCopyNode::Dest)) {
 313           // Non constant offset in the array: we can't statically
 314           // determine the value
 315           return NULL;
 316         }
 317         Node* diff = _igvn.transform(new SubINode(ac->in(ArrayCopyNode::SrcPos), ac->in(ArrayCopyNode::DestPos)));
 318 #ifdef _LP64
 319         diff = _igvn.transform(new ConvI2LNode(diff));
 320 #endif
 321         diff = _igvn.transform(new LShiftXNode(diff, intcon(shift)));
 322 
 323         Node* off = _igvn.transform(new AddXNode(MakeConX(offset), diff));

 324         adr = _igvn.transform(new AddPNode(base, base, off));
 325         // In the case of a flattened inline type array, each field has its
 326         // own slice so we need to extract the field being accessed from
 327         // the address computation
 328         adr_type = adr_type->add_field_offset_and_offset(offset)->add_offset(Type::OffsetBot)->is_aryptr();
 329         adr = _igvn.transform(new CastPPNode(adr, adr_type));

 330       }
 331       MergeMemNode* mergemen = _igvn.transform(MergeMemNode::make(mem))->as_MergeMem();
 332       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 333       res = ArrayCopyNode::load(bs, &_igvn, ctl, mergemen, adr, adr_type, type, bt);
 334     }
 335   }
 336   if (res != NULL) {
 337     if (ftype->isa_narrowoop()) {
 338       // PhaseMacroExpand::scalar_replacement adds DecodeN nodes
 339       assert(res->isa_DecodeN(), "should be narrow oop");
 340       res = _igvn.transform(new EncodePNode(res, ftype));
 341     }
 342     return res;
 343   }
 344   return NULL;
 345 }
 346 
 347 //
 348 // Given a Memory Phi, compute a value Phi containing the values from stores
 349 // on the input paths.
 350 // Note: this function is recursive, its depth is limited by the "level" argument
 351 // Returns the computed Phi, or NULL if it cannot compute it.
 352 Node *PhaseMacroExpand::value_from_mem_phi(Node *mem, BasicType ft, const Type *phi_type, const TypeOopPtr *adr_t, AllocateNode *alloc, Node_Stack *value_phis, int level) {
 353   assert(mem->is_Phi(), "sanity");
 354   int alias_idx = C->get_alias_index(adr_t);
 355   int offset = adr_t->flattened_offset();
 356   int instance_id = adr_t->instance_id();
 357 
 358   // Check if an appropriate value phi already exists.
 359   Node* region = mem->in(0);
 360   for (DUIterator_Fast kmax, k = region->fast_outs(kmax); k < kmax; k++) {
 361     Node* phi = region->fast_out(k);
 362     if (phi->is_Phi() && phi != mem &&
 363         phi->as_Phi()->is_same_inst_field(phi_type, (int)mem->_idx, instance_id, alias_idx, offset)) {
 364       return phi;
 365     }
 366   }
 367   // Check if an appropriate new value phi already exists.
 368   Node* new_phi = value_phis->find(mem->_idx);
 369   if (new_phi != NULL)
 370     return new_phi;
 371 
 372   if (level <= 0) {
 373     return NULL; // Give up: phi tree too deep
 374   }
 375   Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
 376   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 377 
 378   uint length = mem->req();
 379   GrowableArray <Node *> values(length, length, NULL);
 380 
 381   // create a new Phi for the value
 382   PhiNode *phi = new PhiNode(mem->in(0), phi_type, NULL, mem->_idx, instance_id, alias_idx, offset);
 383   transform_later(phi);
 384   value_phis->push(phi, mem->_idx);
 385 
 386   for (uint j = 1; j < length; j++) {
 387     Node *in = mem->in(j);
 388     if (in == NULL || in->is_top()) {
 389       values.at_put(j, in);
 390     } else  {
 391       Node *val = scan_mem_chain(in, alias_idx, offset, start_mem, alloc, &_igvn);
 392       if (val == start_mem || val == alloc_mem) {
 393         // hit a sentinel, return appropriate 0 value
 394         Node* default_value = alloc->in(AllocateNode::DefaultValue);
 395         if (default_value != NULL) {
 396           values.at_put(j, default_value);
 397         } else {
 398           assert(alloc->in(AllocateNode::RawDefaultValue) == NULL, "default value may not be null");
 399           values.at_put(j, _igvn.zerocon(ft));
 400         }
 401         continue;
 402       }
 403       if (val->is_Initialize()) {
 404         val = val->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
 405       }
 406       if (val == NULL) {
 407         return NULL;  // can't find a value on this path
 408       }
 409       if (val == mem) {
 410         values.at_put(j, mem);
 411       } else if (val->is_Store()) {
 412         Node* n = val->in(MemNode::ValueIn);
 413         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 414         n = bs->step_over_gc_barrier(n);
 415         if (is_subword_type(ft)) {
 416           n = Compile::narrow_value(ft, n, phi_type, &_igvn, true);
 417         }
 418         values.at_put(j, n);
 419       } else if(val->is_Proj() && val->in(0) == alloc) {
 420         Node* default_value = alloc->in(AllocateNode::DefaultValue);
 421         if (default_value != NULL) {
 422           values.at_put(j, default_value);
 423         } else {
 424           assert(alloc->in(AllocateNode::RawDefaultValue) == NULL, "default value may not be null");
 425           values.at_put(j, _igvn.zerocon(ft));
 426         }
 427       } else if (val->is_Phi()) {
 428         val = value_from_mem_phi(val, ft, phi_type, adr_t, alloc, value_phis, level-1);
 429         if (val == NULL) {
 430           return NULL;
 431         }
 432         values.at_put(j, val);
 433       } else if (val->Opcode() == Op_SCMemProj) {
 434         assert(val->in(0)->is_LoadStore() ||
 435                val->in(0)->Opcode() == Op_EncodeISOArray ||
 436                val->in(0)->Opcode() == Op_StrCompressedCopy, "sanity");
 437         assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
 438         return NULL;
 439       } else if (val->is_ArrayCopy()) {
 440         Node* res = make_arraycopy_load(val->as_ArrayCopy(), offset, val->in(0), val->in(TypeFunc::Memory), ft, phi_type, alloc);
 441         if (res == NULL) {
 442           return NULL;
 443         }
 444         values.at_put(j, res);
 445       } else {
 446         DEBUG_ONLY( val->dump(); )

 450     }
 451   }
 452   // Set Phi's inputs
 453   for (uint j = 1; j < length; j++) {
 454     if (values.at(j) == mem) {
 455       phi->init_req(j, phi);
 456     } else {
 457       phi->init_req(j, values.at(j));
 458     }
 459   }
 460   return phi;
 461 }
 462 
 463 // Search the last value stored into the object's field.
 464 Node *PhaseMacroExpand::value_from_mem(Node *sfpt_mem, Node *sfpt_ctl, BasicType ft, const Type *ftype, const TypeOopPtr *adr_t, AllocateNode *alloc) {
 465   assert(adr_t->is_known_instance_field(), "instance required");
 466   int instance_id = adr_t->instance_id();
 467   assert((uint)instance_id == alloc->_idx, "wrong allocation");
 468 
 469   int alias_idx = C->get_alias_index(adr_t);
 470   int offset = adr_t->flattened_offset();
 471   Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);

 472   Node *alloc_mem = alloc->in(TypeFunc::Memory);
 473   VectorSet visited;
 474 
 475   bool done = sfpt_mem == alloc_mem;
 476   Node *mem = sfpt_mem;
 477   while (!done) {
 478     if (visited.test_set(mem->_idx)) {
 479       return NULL;  // found a loop, give up
 480     }
 481     mem = scan_mem_chain(mem, alias_idx, offset, start_mem, alloc, &_igvn);
 482     if (mem == start_mem || mem == alloc_mem) {
 483       done = true;  // hit a sentinel, return appropriate 0 value
 484     } else if (mem->is_Initialize()) {
 485       mem = mem->as_Initialize()->find_captured_store(offset, type2aelembytes(ft), &_igvn);
 486       if (mem == NULL) {
 487         done = true; // Something went wrong.
 488       } else if (mem->is_Store()) {
 489         const TypePtr* atype = mem->as_Store()->adr_type();
 490         assert(C->get_alias_index(atype) == Compile::AliasIdxRaw, "store is correct memory slice");
 491         done = true;
 492       }
 493     } else if (mem->is_Store()) {
 494       const TypeOopPtr* atype = mem->as_Store()->adr_type()->isa_oopptr();
 495       assert(atype != NULL, "address type must be oopptr");
 496       assert(C->get_alias_index(atype) == alias_idx &&
 497              atype->is_known_instance_field() && atype->flattened_offset() == offset &&
 498              atype->instance_id() == instance_id, "store is correct memory slice");
 499       done = true;
 500     } else if (mem->is_Phi()) {
 501       // try to find a phi's unique input
 502       Node *unique_input = NULL;
 503       Node *top = C->top();
 504       for (uint i = 1; i < mem->req(); i++) {
 505         Node *n = scan_mem_chain(mem->in(i), alias_idx, offset, start_mem, alloc, &_igvn);
 506         if (n == NULL || n == top || n == mem) {
 507           continue;
 508         } else if (unique_input == NULL) {
 509           unique_input = n;
 510         } else if (unique_input != n) {
 511           unique_input = top;
 512           break;
 513         }
 514       }
 515       if (unique_input != NULL && unique_input != top) {
 516         mem = unique_input;
 517       } else {
 518         done = true;
 519       }
 520     } else if (mem->is_ArrayCopy()) {
 521       done = true;
 522     } else {
 523       DEBUG_ONLY( mem->dump(); )
 524       assert(false, "unexpected node");
 525     }
 526   }
 527   if (mem != NULL) {
 528     if (mem == start_mem || mem == alloc_mem) {
 529       // hit a sentinel, return appropriate 0 value
 530       Node* default_value = alloc->in(AllocateNode::DefaultValue);
 531       if (default_value != NULL) {
 532         return default_value;
 533       }
 534       assert(alloc->in(AllocateNode::RawDefaultValue) == NULL, "default value may not be null");
 535       return _igvn.zerocon(ft);
 536     } else if (mem->is_Store()) {
 537       Node* n = mem->in(MemNode::ValueIn);
 538       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 539       n = bs->step_over_gc_barrier(n);
 540       return n;
 541     } else if (mem->is_Phi()) {
 542       // attempt to produce a Phi reflecting the values on the input paths of the Phi
 543       Node_Stack value_phis(8);
 544       Node* phi = value_from_mem_phi(mem, ft, ftype, adr_t, alloc, &value_phis, ValueSearchLimit);
 545       if (phi != NULL) {
 546         return phi;
 547       } else {
 548         // Kill all new Phis
 549         while(value_phis.is_nonempty()) {
 550           Node* n = value_phis.node();
 551           _igvn.replace_node(n, C->top());
 552           value_phis.pop();
 553         }
 554       }
 555     } else if (mem->is_ArrayCopy()) {
 556       Node* ctl = mem->in(0);
 557       Node* m = mem->in(TypeFunc::Memory);
 558       if (sfpt_ctl->is_Proj() && sfpt_ctl->as_Proj()->is_uncommon_trap_proj(Deoptimization::Reason_none)) {
 559         // pin the loads in the uncommon trap path
 560         ctl = sfpt_ctl;
 561         m = sfpt_mem;
 562       }
 563       return make_arraycopy_load(mem->as_ArrayCopy(), offset, ctl, m, ft, ftype, alloc);
 564     }
 565   }
 566   // Something went wrong.
 567   return NULL;
 568 }
 569 
 570 // Search the last value stored into the inline type's fields.
 571 Node* PhaseMacroExpand::inline_type_from_mem(Node* mem, Node* ctl, ciInlineKlass* vk, const TypeAryPtr* adr_type, int offset, AllocateNode* alloc) {
 572   // Subtract the offset of the first field to account for the missing oop header
 573   offset -= vk->first_field_offset();
 574   // Create a new InlineTypeNode and retrieve the field values from memory
 575   InlineTypeNode* vt = InlineTypeNode::make_uninitialized(_igvn, vk)->as_InlineType();
 576   transform_later(vt);
 577   for (int i = 0; i < vk->nof_declared_nonstatic_fields(); ++i) {
 578     ciType* field_type = vt->field_type(i);
 579     int field_offset = offset + vt->field_offset(i);
 580     Node* value = NULL;
 581     if (vt->field_is_flattened(i)) {
 582       value = inline_type_from_mem(mem, ctl, field_type->as_inline_klass(), adr_type, field_offset, alloc);
 583     } else {
 584       const Type* ft = Type::get_const_type(field_type);
 585       BasicType bt = type2field[field_type->basic_type()];
 586       if (UseCompressedOops && !is_java_primitive(bt)) {
 587         ft = ft->make_narrowoop();
 588         bt = T_NARROWOOP;
 589       }
 590       // Each inline type field has its own memory slice
 591       adr_type = adr_type->with_field_offset(field_offset);
 592       value = value_from_mem(mem, ctl, bt, ft, adr_type, alloc);
 593       if (value != NULL && ft->isa_narrowoop()) {
 594         assert(UseCompressedOops, "unexpected narrow oop");
 595         if (value->is_EncodeP()) {
 596           value = value->in(1);
 597         } else {
 598           value = transform_later(new DecodeNNode(value, value->get_ptr_type()));
 599         }
 600       }
 601     }
 602     if (value != NULL) {
 603       vt->set_field_value(i, value);
 604     } else {
 605       // We might have reached the TrackedInitializationLimit
 606       return NULL;
 607     }
 608   }
 609   return vt;
 610 }
 611 
 612 // Check the possibility of scalar replacement.
 613 bool PhaseMacroExpand::can_eliminate_allocation(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
 614   //  Scan the uses of the allocation to check for anything that would
 615   //  prevent us from eliminating it.
 616   NOT_PRODUCT( const char* fail_eliminate = NULL; )
 617   DEBUG_ONLY( Node* disq_node = NULL; )
 618   bool  can_eliminate = true;
 619 
 620   Unique_Node_List worklist;
 621   Node* res = alloc->result_cast();
 622   const TypeOopPtr* res_type = NULL;
 623   if (res == NULL) {
 624     // All users were eliminated.
 625   } else if (!res->is_CheckCastPP()) {
 626     NOT_PRODUCT(fail_eliminate = "Allocation does not have unique CheckCastPP";)
 627     can_eliminate = false;
 628   } else {
 629     worklist.push(res);
 630     res_type = _igvn.type(res)->isa_oopptr();
 631     if (res_type == NULL) {
 632       NOT_PRODUCT(fail_eliminate = "Neither instance or array allocation";)
 633       can_eliminate = false;
 634     } else if (res_type->isa_aryptr()) {
 635       int length = alloc->in(AllocateNode::ALength)->find_int_con(-1);
 636       if (length < 0) {
 637         NOT_PRODUCT(fail_eliminate = "Array's size is not constant";)
 638         can_eliminate = false;
 639       }
 640     }
 641   }
 642 
 643   while (can_eliminate && worklist.size() > 0) {
 644     res = worklist.pop();
 645     for (DUIterator_Fast jmax, j = res->fast_outs(jmax); j < jmax && can_eliminate; j++) {
 646       Node* use = res->fast_out(j);
 647 
 648       if (use->is_AddP()) {
 649         const TypePtr* addp_type = _igvn.type(use)->is_ptr();
 650         int offset = addp_type->offset();
 651 
 652         if (offset == Type::OffsetTop || offset == Type::OffsetBot) {
 653           NOT_PRODUCT(fail_eliminate = "Undefined field reference";)
 654           can_eliminate = false;
 655           break;
 656         }
 657         for (DUIterator_Fast kmax, k = use->fast_outs(kmax);
 658                                    k < kmax && can_eliminate; k++) {
 659           Node* n = use->fast_out(k);
 660           if (!n->is_Store() && n->Opcode() != Op_CastP2X
 661               SHENANDOAHGC_ONLY(&& (!UseShenandoahGC || !ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(n))) ) {
 662             DEBUG_ONLY(disq_node = n;)
 663             if (n->is_Load() || n->is_LoadStore()) {
 664               NOT_PRODUCT(fail_eliminate = "Field load";)
 665             } else {

 674                   use->as_ArrayCopy()->is_copyof_validated() ||
 675                   use->as_ArrayCopy()->is_copyofrange_validated()) &&
 676                  use->in(ArrayCopyNode::Dest) == res) {
 677         // ok to eliminate
 678       } else if (use->is_SafePoint()) {
 679         SafePointNode* sfpt = use->as_SafePoint();
 680         if (sfpt->is_Call() && sfpt->as_Call()->has_non_debug_use(res)) {
 681           // Object is passed as argument.
 682           DEBUG_ONLY(disq_node = use;)
 683           NOT_PRODUCT(fail_eliminate = "Object is passed as argument";)
 684           can_eliminate = false;
 685         }
 686         Node* sfptMem = sfpt->memory();
 687         if (sfptMem == NULL || sfptMem->is_top()) {
 688           DEBUG_ONLY(disq_node = use;)
 689           NOT_PRODUCT(fail_eliminate = "NULL or TOP memory";)
 690           can_eliminate = false;
 691         } else {
 692           safepoints.append_if_missing(sfpt);
 693         }
 694       } else if (use->is_InlineType() && use->isa_InlineType()->get_oop() == res) {
 695         // ok to eliminate
 696       } else if (use->is_InlineTypePtr() && use->isa_InlineTypePtr()->get_oop() == res) {
 697         // Process users
 698         worklist.push(use);
 699       } else if (use->Opcode() == Op_StoreX && use->in(MemNode::Address) == res) {
 700         // Store to mark word of inline type larval buffer
 701         assert(res_type->is_inlinetypeptr(), "Unexpected store to mark word");
 702       } else if (use->Opcode() != Op_CastP2X) { // CastP2X is used by card mark
 703         if (use->is_Phi()) {
 704           if (use->outcnt() == 1 && use->unique_out()->Opcode() == Op_Return) {
 705             NOT_PRODUCT(fail_eliminate = "Object is return value";)
 706           } else {
 707             NOT_PRODUCT(fail_eliminate = "Object is referenced by Phi";)
 708           }
 709           DEBUG_ONLY(disq_node = use;)
 710         } else {
 711           if (use->Opcode() == Op_Return) {
 712             NOT_PRODUCT(fail_eliminate = "Object is return value";)
 713           } else {
 714             NOT_PRODUCT(fail_eliminate = "Object is referenced by node";)
 715           }
 716           DEBUG_ONLY(disq_node = use;)
 717         }
 718         can_eliminate = false;
 719       } else {
 720         assert(use->Opcode() == Op_CastP2X, "should be");
 721         assert(!use->has_out_with(Op_OrL), "should have been removed because oop is never null");
 722       }
 723     }
 724   }
 725 
 726 #ifndef PRODUCT
 727   if (PrintEliminateAllocations) {
 728     if (can_eliminate) {
 729       tty->print("Scalar ");
 730       if (res == NULL)
 731         alloc->dump();
 732       else
 733         res->dump();
 734     } else {
 735       tty->print("NotScalar (%s)", fail_eliminate);
 736       if (res == NULL)
 737         alloc->dump();
 738       else
 739         res->dump();
 740 #ifdef ASSERT
 741       if (disq_node != NULL) {
 742           tty->print("  >>>> ");
 743           disq_node->dump();
 744       }
 745 #endif /*ASSERT*/
 746     }
 747   }
 748 #endif
 749   return can_eliminate;
 750 }
 751 
 752 // Do scalar replacement.
 753 bool PhaseMacroExpand::scalar_replacement(AllocateNode *alloc, GrowableArray <SafePointNode *>& safepoints) {
 754   GrowableArray <SafePointNode *> safepoints_done;

 760   BasicType basic_elem_type = T_ILLEGAL;
 761   const Type* field_type = NULL;
 762 
 763   Node* res = alloc->result_cast();
 764   assert(res == NULL || res->is_CheckCastPP(), "unexpected AllocateNode result");
 765   const TypeOopPtr* res_type = NULL;
 766   if (res != NULL) { // Could be NULL when there are no users
 767     res_type = _igvn.type(res)->isa_oopptr();
 768   }
 769 
 770   if (res != NULL) {
 771     if (res_type->isa_instptr()) {
 772       // find the fields of the class which will be needed for safepoint debug information
 773       iklass = res_type->is_instptr()->instance_klass();
 774       nfields = iklass->nof_nonstatic_fields();
 775     } else {
 776       // find the array's elements which will be needed for safepoint debug information
 777       nfields = alloc->in(AllocateNode::ALength)->find_int_con(-1);
 778       assert(nfields >= 0, "must be an array klass.");
 779       basic_elem_type = res_type->is_aryptr()->elem()->array_element_basic_type();
 780       if (basic_elem_type == T_PRIMITIVE_OBJECT && !res_type->is_aryptr()->is_flat()) {
 781         basic_elem_type = T_OBJECT;
 782       }
 783       array_base = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
 784       element_size = type2aelembytes(basic_elem_type);
 785       field_type = res_type->is_aryptr()->elem();
 786       if (res_type->is_aryptr()->is_flat()) {
 787         // Flattened inline type array
 788         element_size = res_type->is_aryptr()->flat_elem_size();
 789       }
 790     }
 791   }
 792   //
 793   // Process the safepoint uses
 794   //
 795   assert(safepoints.length() == 0 || !res_type->is_inlinetypeptr(), "Inline type allocations should not have safepoint uses");
 796   Unique_Node_List value_worklist;
 797   while (safepoints.length() > 0) {
 798     SafePointNode* sfpt = safepoints.pop();
 799     Node* mem = sfpt->memory();
 800     Node* ctl = sfpt->control();
 801     assert(sfpt->jvms() != NULL, "missed JVMS");
 802     // Fields of scalar objs are referenced only at the end
 803     // of regular debuginfo at the last (youngest) JVMS.
 804     // Record relative start index.
 805     uint first_ind = (sfpt->req() - sfpt->jvms()->scloff());
 806     SafePointScalarObjectNode* sobj = new SafePointScalarObjectNode(res_type,
 807 #ifdef ASSERT
 808                                                  alloc,
 809 #endif
 810                                                  first_ind, nfields);
 811     sobj->init_req(0, C->root());
 812     transform_later(sobj);
 813 
 814     // Scan object's fields adding an input to the safepoint for each field.
 815     for (int j = 0; j < nfields; j++) {
 816       intptr_t offset;
 817       ciField* field = NULL;
 818       if (iklass != NULL) {
 819         field = iklass->nonstatic_field_at(j);
 820         offset = field->offset();
 821         ciType* elem_type = field->type();
 822         basic_elem_type = field->layout_type();
 823         assert(!field->is_flattened(), "flattened inline type fields should not have safepoint uses");
 824 
 825         // The next code is taken from Parse::do_get_xxx().
 826         if (is_reference_type(basic_elem_type)) {
 827           if (!elem_type->is_loaded()) {
 828             field_type = TypeInstPtr::BOTTOM;
 829           } else if (field != NULL && field->is_static_constant()) {
 830             ciObject* con = field->constant_value().as_object();
 831             // Do not "join" in the previous type; it doesn't add value,
 832             // and may yield a vacuous result if the field is of interface type.
 833             field_type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
 834             assert(field_type != NULL, "field singleton type must be consistent");
 835           } else {
 836             field_type = TypeOopPtr::make_from_klass(elem_type->as_klass());
 837           }
 838           if (UseCompressedOops) {
 839             field_type = field_type->make_narrowoop();
 840             basic_elem_type = T_NARROWOOP;
 841           }
 842         } else {
 843           field_type = Type::get_const_basic_type(basic_elem_type);
 844         }
 845       } else {
 846         offset = array_base + j * (intptr_t)element_size;
 847       }
 848 
 849       Node* field_val = NULL;
 850       const TypeOopPtr* field_addr_type = res_type->add_offset(offset)->isa_oopptr();
 851       if (res_type->isa_aryptr() && res_type->is_aryptr()->is_flat()) {
 852         ciInlineKlass* vk = res_type->is_aryptr()->elem()->inline_klass();
 853         assert(vk->flatten_array(), "must be flattened");
 854         field_val = inline_type_from_mem(mem, ctl, vk, field_addr_type->isa_aryptr(), 0, alloc);
 855       } else {
 856         field_val = value_from_mem(mem, ctl, basic_elem_type, field_type, field_addr_type, alloc);
 857       }
 858       if (field_val == NULL) {
 859         // We weren't able to find a value for this field,
 860         // give up on eliminating this allocation.
 861 
 862         // Remove any extra entries we added to the safepoint.
 863         uint last = sfpt->req() - 1;
 864         for (int k = 0;  k < j; k++) {
 865           sfpt->del_req(last--);
 866         }
 867         _igvn._worklist.push(sfpt);
 868         // rollback processed safepoints
 869         while (safepoints_done.length() > 0) {
 870           SafePointNode* sfpt_done = safepoints_done.pop();
 871           // remove any extra entries we added to the safepoint
 872           last = sfpt_done->req() - 1;
 873           for (int k = 0;  k < nfields; k++) {
 874             sfpt_done->del_req(last--);
 875           }
 876           JVMState *jvms = sfpt_done->jvms();
 877           jvms->set_endoff(sfpt_done->req());

 900             int field_idx = C->get_alias_index(field_addr_type);
 901             tty->print(" (alias_idx=%d)", field_idx);
 902           } else { // Array's element
 903             tty->print("=== At SafePoint node %d can't find value of array element [%d]",
 904                        sfpt->_idx, j);
 905           }
 906           tty->print(", which prevents elimination of: ");
 907           if (res == NULL)
 908             alloc->dump();
 909           else
 910             res->dump();
 911         }
 912 #endif
 913         return false;
 914       }
 915       if (UseCompressedOops && field_type->isa_narrowoop()) {
 916         // Enable "DecodeN(EncodeP(Allocate)) --> Allocate" transformation
 917         // to be able scalar replace the allocation.
 918         if (field_val->is_EncodeP()) {
 919           field_val = field_val->in(1);
 920         } else if (!field_val->is_InlineTypeBase()) {
 921           field_val = transform_later(new DecodeNNode(field_val, field_val->get_ptr_type()));
 922         }
 923       }
 924       if (field_val->is_InlineTypeBase()) {
 925         // Keep track of inline types to scalarize them later
 926         value_worklist.push(field_val);
 927       }
 928       sfpt->add_req(field_val);
 929     }
 930     JVMState *jvms = sfpt->jvms();
 931     jvms->set_endoff(sfpt->req());
 932     // Now make a pass over the debug information replacing any references
 933     // to the allocated object with "sobj"
 934     int start = jvms->debug_start();
 935     int end   = jvms->debug_end();
 936     sfpt->replace_edges_in_range(res, sobj, start, end, &_igvn);
 937     _igvn._worklist.push(sfpt);
 938     safepoints_done.append_if_missing(sfpt); // keep it for rollback
 939   }
 940   // Scalarize inline types that were added to the safepoint.
 941   // Don't allow linking a constant oop (if available) for flat array elements
 942   // because Deoptimization::reassign_flat_array_elements needs field values.
 943   bool allow_oop = res_type != NULL && (!res_type->isa_aryptr() || !res_type->is_aryptr()->is_flat());
 944   for (uint i = 0; i < value_worklist.size(); ++i) {
 945     InlineTypeBaseNode* vt = value_worklist.at(i)->as_InlineTypeBase();
 946     vt->make_scalar_in_safepoints(&_igvn, allow_oop);
 947   }
 948   return true;
 949 }
 950 
 951 static void disconnect_projections(MultiNode* n, PhaseIterGVN& igvn) {
 952   Node* ctl_proj = n->proj_out_or_null(TypeFunc::Control);
 953   Node* mem_proj = n->proj_out_or_null(TypeFunc::Memory);
 954   if (ctl_proj != NULL) {
 955     igvn.replace_node(ctl_proj, n->in(0));
 956   }
 957   if (mem_proj != NULL) {
 958     igvn.replace_node(mem_proj, n->in(TypeFunc::Memory));
 959   }
 960 }
 961 
 962 // Process users of eliminated allocation.
 963 void PhaseMacroExpand::process_users_of_allocation(CallNode *alloc, bool inline_alloc) {
 964   Unique_Node_List worklist;
 965   Node* res = alloc->result_cast();
 966   if (res != NULL) {
 967     worklist.push(res);
 968   }
 969   while (worklist.size() > 0) {
 970     res = worklist.pop();
 971     for (DUIterator_Last jmin, j = res->last_outs(jmin); j >= jmin; ) {
 972       Node *use = res->last_out(j);
 973       uint oc1 = res->outcnt();
 974 
 975       if (use->is_AddP()) {
 976         for (DUIterator_Last kmin, k = use->last_outs(kmin); k >= kmin; ) {
 977           Node *n = use->last_out(k);
 978           uint oc2 = use->outcnt();
 979           if (n->is_Store()) {
 980             for (DUIterator_Fast pmax, p = n->fast_outs(pmax); p < pmax; p++) {
 981               MemBarNode* mb = n->fast_out(p)->isa_MemBar();
 982               if (mb != NULL && mb->req() <= MemBarNode::Precedent && mb->in(MemBarNode::Precedent) == n) {
 983                 // MemBarVolatiles should have been removed by MemBarNode::Ideal() for non-inline allocations
 984                 assert(inline_alloc, "MemBarVolatile should be eliminated for non-escaping object");
 985                 mb->remove(&_igvn);
 986               }



 987             }

 988             _igvn.replace_node(n, n->in(MemNode::Memory));
 989           } else {
 990             eliminate_gc_barrier(n);
 991           }
 992           k -= (oc2 - use->outcnt());
 993         }
 994         _igvn.remove_dead_node(use);
 995       } else if (use->is_ArrayCopy()) {
 996         // Disconnect ArrayCopy node
 997         ArrayCopyNode* ac = use->as_ArrayCopy();
 998         if (ac->is_clonebasic()) {
 999           Node* membar_after = ac->proj_out(TypeFunc::Control)->unique_ctrl_out();
1000           disconnect_projections(ac, _igvn);
1001           assert(alloc->in(TypeFunc::Memory)->is_Proj() && alloc->in(TypeFunc::Memory)->in(0)->Opcode() == Op_MemBarCPUOrder, "mem barrier expected before allocation");
1002           Node* membar_before = alloc->in(TypeFunc::Memory)->in(0);
1003           disconnect_projections(membar_before->as_MemBar(), _igvn);
1004           if (membar_after->is_MemBar()) {
1005             disconnect_projections(membar_after->as_MemBar(), _igvn);
1006           }
1007         } else {
1008           assert(ac->is_arraycopy_validated() ||
1009                  ac->is_copyof_validated() ||
1010                  ac->is_copyofrange_validated(), "unsupported");
1011           CallProjections* callprojs = ac->extract_projections(true);

1012 
1013           _igvn.replace_node(callprojs->fallthrough_ioproj, ac->in(TypeFunc::I_O));
1014           _igvn.replace_node(callprojs->fallthrough_memproj, ac->in(TypeFunc::Memory));
1015           _igvn.replace_node(callprojs->fallthrough_catchproj, ac->in(TypeFunc::Control));
1016 
1017           // Set control to top. IGVN will remove the remaining projections
1018           ac->set_req(0, top());
1019           ac->replace_edge(res, top(), &_igvn);
1020 
1021           // Disconnect src right away: it can help find new
1022           // opportunities for allocation elimination
1023           Node* src = ac->in(ArrayCopyNode::Src);
1024           ac->replace_edge(src, top(), &_igvn);
1025           // src can be top at this point if src and dest of the
1026           // arraycopy were the same
1027           if (src->outcnt() == 0 && !src->is_top()) {
1028             _igvn.remove_dead_node(src);
1029           }
1030         }
1031         _igvn._worklist.push(ac);
1032       } else if (use->is_InlineType()) {
1033         assert(use->isa_InlineType()->get_oop() == res, "unexpected inline type use");
1034         _igvn.rehash_node_delayed(use);
1035         use->isa_InlineType()->set_oop(_igvn.zerocon(T_PRIMITIVE_OBJECT));
1036       } else if (use->is_InlineTypePtr()) {
1037         assert(use->isa_InlineTypePtr()->get_oop() == res, "unexpected inline type ptr use");
1038         _igvn.rehash_node_delayed(use);
1039         use->isa_InlineTypePtr()->set_oop(_igvn.zerocon(T_PRIMITIVE_OBJECT));
1040         // Process users
1041         worklist.push(use);
1042       } else if (use->Opcode() == Op_StoreX && use->in(MemNode::Address) == res) {
1043         // Store to mark word of inline type larval buffer
1044         assert(inline_alloc, "Unexpected store to mark word");
1045         _igvn.replace_node(use, use->in(MemNode::Memory));
1046       } else {
1047         eliminate_gc_barrier(use);
1048       }
1049       j -= (oc1 - res->outcnt());
1050     }
1051     assert(res->outcnt() == 0, "all uses of allocated objects must be deleted");
1052     _igvn.remove_dead_node(res);
1053   }
1054 
1055   //
1056   // Process other users of allocation's projections
1057   //
1058   if (_callprojs->resproj[0] != NULL && _callprojs->resproj[0]->outcnt() != 0) {
1059     // First disconnect stores captured by Initialize node.
1060     // If Initialize node is eliminated first in the following code,
1061     // it will kill such stores and DUIterator_Last will assert.
1062     for (DUIterator_Fast jmax, j = _callprojs->resproj[0]->fast_outs(jmax);  j < jmax; j++) {
1063       Node* use = _callprojs->resproj[0]->fast_out(j);
1064       if (use->is_AddP()) {
1065         // raw memory addresses used only by the initialization
1066         _igvn.replace_node(use, C->top());
1067         --j; --jmax;
1068       }
1069     }
1070     for (DUIterator_Last jmin, j = _callprojs->resproj[0]->last_outs(jmin); j >= jmin; ) {
1071       Node* use = _callprojs->resproj[0]->last_out(j);
1072       uint oc1 = _callprojs->resproj[0]->outcnt();
1073       if (use->is_Initialize()) {
1074         // Eliminate Initialize node.
1075         InitializeNode *init = use->as_Initialize();
1076         assert(init->outcnt() <= 2, "only a control and memory projection expected");
1077         Node *ctrl_proj = init->proj_out_or_null(TypeFunc::Control);
1078         if (ctrl_proj != NULL) {
1079           _igvn.replace_node(ctrl_proj, init->in(TypeFunc::Control));
1080 #ifdef ASSERT
1081           // If the InitializeNode has no memory out, it will die, and tmp will become NULL
1082           Node* tmp = init->in(TypeFunc::Control);
1083           assert(tmp == NULL || tmp == _callprojs->fallthrough_catchproj, "allocation control projection");
1084 #endif
1085         }
1086         Node *mem_proj = init->proj_out_or_null(TypeFunc::Memory);
1087         if (mem_proj != NULL) {
1088           Node *mem = init->in(TypeFunc::Memory);
1089 #ifdef ASSERT
1090           if (mem->is_MergeMem()) {
1091             assert(mem->in(TypeFunc::Memory) == _callprojs->fallthrough_memproj, "allocation memory projection");
1092           } else {
1093             assert(mem == _callprojs->fallthrough_memproj, "allocation memory projection");
1094           }
1095 #endif
1096           _igvn.replace_node(mem_proj, mem);
1097         }
1098       } else if (use->Opcode() == Op_MemBarStoreStore) {
1099         // Inline type buffer allocations are followed by a membar
1100         assert(inline_alloc, "Unexpected MemBarStoreStore");
1101         use->as_MemBar()->remove(&_igvn);
1102       } else  {
1103         assert(false, "only Initialize or AddP expected");
1104       }
1105       j -= (oc1 - _callprojs->resproj[0]->outcnt());
1106     }
1107   }
1108   if (_callprojs->fallthrough_catchproj != NULL) {
1109     _igvn.replace_node(_callprojs->fallthrough_catchproj, alloc->in(TypeFunc::Control));
1110   }
1111   if (_callprojs->fallthrough_memproj != NULL) {
1112     _igvn.replace_node(_callprojs->fallthrough_memproj, alloc->in(TypeFunc::Memory));
1113   }
1114   if (_callprojs->catchall_memproj != NULL) {
1115     _igvn.replace_node(_callprojs->catchall_memproj, C->top());
1116   }
1117   if (_callprojs->fallthrough_ioproj != NULL) {
1118     _igvn.replace_node(_callprojs->fallthrough_ioproj, alloc->in(TypeFunc::I_O));
1119   }
1120   if (_callprojs->catchall_ioproj != NULL) {
1121     _igvn.replace_node(_callprojs->catchall_ioproj, C->top());
1122   }
1123   if (_callprojs->catchall_catchproj != NULL) {
1124     _igvn.replace_node(_callprojs->catchall_catchproj, C->top());
1125   }
1126 }
1127 
1128 bool PhaseMacroExpand::eliminate_allocate_node(AllocateNode *alloc) {
1129   // If reallocation fails during deoptimization we'll pop all
1130   // interpreter frames for this compiled frame and that won't play
1131   // nice with JVMTI popframe.
1132   // We avoid this issue by eager reallocation when the popframe request
1133   // is received.
1134   if (!EliminateAllocations) {
1135     return false;
1136   }
1137   Node* klass = alloc->in(AllocateNode::KlassNode);
1138   const TypeKlassPtr* tklass = _igvn.type(klass)->is_klassptr();
1139 
1140   // Attempt to eliminate inline type buffer allocations
1141   // regardless of usage and escape/replaceable status.
1142   bool inline_alloc = tklass->isa_instklassptr() &&
1143                       tklass->is_instklassptr()->instance_klass()->is_inlinetype();
1144   if (!alloc->_is_non_escaping && !inline_alloc) {
1145     return false;
1146   }
1147   // Eliminate boxing allocations which are not used
1148   // regardless scalar replaceable status.
1149   Node* res = alloc->result_cast();
1150   bool boxing_alloc = (res == NULL) && C->eliminate_boxing() &&
1151                       tklass->isa_instklassptr() &&
1152                       tklass->is_instklassptr()->instance_klass()->is_box_klass();
1153   if (!alloc->_is_scalar_replaceable && (!boxing_alloc || (res != NULL))) {
1154     return false;
1155   }
1156 
1157   _callprojs = alloc->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1158 
1159   GrowableArray <SafePointNode *> safepoints;
1160   if (!can_eliminate_allocation(alloc, safepoints)) {
1161     return false;
1162   }
1163 
1164   if (!alloc->_is_scalar_replaceable) {
1165     assert(res == NULL || inline_alloc, "sanity");
1166     // We can only eliminate allocation if all debug info references
1167     // are already replaced with SafePointScalarObject because
1168     // we can't search for a fields value without instance_id.
1169     if (safepoints.length() > 0) {
1170       assert(!inline_alloc, "Inline type allocations should not have safepoint uses");
1171       return false;
1172     }
1173   }
1174 
1175   if (!scalar_replacement(alloc, safepoints)) {
1176     return false;
1177   }
1178 
1179   CompileLog* log = C->log();
1180   if (log != NULL) {
1181     log->head("eliminate_allocation type='%d'",
1182               log->identify(tklass->exact_klass()));
1183     JVMState* p = alloc->jvms();
1184     while (p != NULL) {
1185       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1186       p = p->caller();
1187     }
1188     log->tail("eliminate_allocation");
1189   }
1190 
1191   process_users_of_allocation(alloc, inline_alloc);
1192 
1193 #ifndef PRODUCT
1194   if (PrintEliminateAllocations) {
1195     if (alloc->is_AllocateArray())
1196       tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
1197     else
1198       tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
1199   }
1200 #endif
1201 
1202   return true;
1203 }
1204 
1205 bool PhaseMacroExpand::eliminate_boxing_node(CallStaticJavaNode *boxing) {
1206   // EA should remove all uses of non-escaping boxing node.
1207   if (!C->eliminate_boxing() || boxing->proj_out_or_null(TypeFunc::Parms) != NULL) {
1208     return false;
1209   }
1210 
1211   assert(boxing->result_cast() == NULL, "unexpected boxing node result");
1212 
1213   _callprojs = boxing->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1214 
1215   const TypeTuple* r = boxing->tf()->range_sig();
1216   assert(r->cnt() > TypeFunc::Parms, "sanity");
1217   const TypeInstPtr* t = r->field_at(TypeFunc::Parms)->isa_instptr();
1218   assert(t != NULL, "sanity");
1219 
1220   CompileLog* log = C->log();
1221   if (log != NULL) {
1222     log->head("eliminate_boxing type='%d'",
1223               log->identify(t->instance_klass()));
1224     JVMState* p = boxing->jvms();
1225     while (p != NULL) {
1226       log->elem("jvms bci='%d' method='%d'", p->bci(), log->identify(p->method()));
1227       p = p->caller();
1228     }
1229     log->tail("eliminate_boxing");
1230   }
1231 
1232   process_users_of_allocation(boxing);
1233 
1234 #ifndef PRODUCT
1235   if (PrintEliminateAllocations) {

1378         }
1379       }
1380 #endif
1381       yank_alloc_node(alloc);
1382       return;
1383     }
1384   }
1385 
1386   enum { too_big_or_final_path = 1, need_gc_path = 2 };
1387   Node *slow_region = NULL;
1388   Node *toobig_false = ctrl;
1389 
1390   // generate the initial test if necessary
1391   if (initial_slow_test != NULL ) {
1392     assert (expand_fast_path, "Only need test if there is a fast path");
1393     slow_region = new RegionNode(3);
1394 
1395     // Now make the initial failure test.  Usually a too-big test but
1396     // might be a TRUE for finalizers or a fancy class check for
1397     // newInstance0.
1398     IfNode* toobig_iff = new IfNode(ctrl, initial_slow_test, PROB_MIN, COUNT_UNKNOWN);
1399     transform_later(toobig_iff);
1400     // Plug the failing-too-big test into the slow-path region
1401     Node* toobig_true = new IfTrueNode(toobig_iff);
1402     transform_later(toobig_true);
1403     slow_region    ->init_req( too_big_or_final_path, toobig_true );
1404     toobig_false = new IfFalseNode(toobig_iff);
1405     transform_later(toobig_false);
1406   } else {
1407     // No initial test, just fall into next case
1408     assert(allocation_has_use || !expand_fast_path, "Should already have been handled");
1409     toobig_false = ctrl;
1410     debug_only(slow_region = NodeSentinel);
1411   }
1412 
1413   // If we are here there are several possibilities
1414   // - expand_fast_path is false - then only a slow path is expanded. That's it.
1415   // no_initial_check means a constant allocation.
1416   // - If check always evaluates to false -> expand_fast_path is false (see above)
1417   // - If check always evaluates to true -> directly into fast path (but may bailout to slowpath)
1418   // if !allocation_has_use the fast path is empty
1419   // if !allocation_has_use && no_initial_check
1420   // - Then there are no fastpath that can fall out to slowpath -> no allocation code at all.
1421   //   removed by yank_alloc_node above.
1422 
1423   Node *slow_mem = mem;  // save the current memory state for slow path
1424   // generate the fast allocation code unless we know that the initial test will always go slow
1425   if (expand_fast_path) {
1426     // Fast path modifies only raw memory.
1427     if (mem->is_MergeMem()) {
1428       mem = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
1429     }
1430 
1431     // allocate the Region and Phi nodes for the result
1432     result_region = new RegionNode(3);
1433     result_phi_rawmem = new PhiNode(result_region, Type::MEMORY, TypeRawPtr::BOTTOM);
1434     result_phi_i_o    = new PhiNode(result_region, Type::ABIO); // I/O is used for Prefetch
1435 
1436     // Grab regular I/O before optional prefetch may change it.
1437     // Slow-path does no I/O so just set it to the original I/O.
1438     result_phi_i_o->init_req(slow_result_path, i_o);
1439 
1440     // Name successful fast-path variables
1441     Node* fast_oop_ctrl;
1442     Node* fast_oop_rawmem;
1443 
1444     if (allocation_has_use) {
1445       Node* needgc_ctrl = NULL;
1446       result_phi_rawoop = new PhiNode(result_region, TypeRawPtr::BOTTOM);
1447 
1448       intx prefetch_lines = length != NULL ? AllocatePrefetchLines : AllocateInstancePrefetchLines;
1449       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1450       Node* fast_oop = bs->obj_allocate(this, mem, toobig_false, size_in_bytes, i_o, needgc_ctrl,
1451                                         fast_oop_ctrl, fast_oop_rawmem,
1452                                         prefetch_lines);
1453 
1454       if (initial_slow_test != NULL) {
1455         // This completes all paths into the slow merge point
1456         slow_region->init_req(need_gc_path, needgc_ctrl);
1457         transform_later(slow_region);
1458       } else {
1459         // No initial slow path needed!
1460         // Just fall from the need-GC path straight into the VM call.
1461         slow_region = needgc_ctrl;
1462       }
1463 

1481     result_phi_i_o   ->init_req(fast_result_path, i_o);
1482     result_phi_rawmem->init_req(fast_result_path, fast_oop_rawmem);
1483   } else {
1484     slow_region = ctrl;
1485     result_phi_i_o = i_o; // Rename it to use in the following code.
1486   }
1487 
1488   // Generate slow-path call
1489   CallNode *call = new CallStaticJavaNode(slow_call_type, slow_call_address,
1490                                OptoRuntime::stub_name(slow_call_address),
1491                                TypePtr::BOTTOM);
1492   call->init_req(TypeFunc::Control,   slow_region);
1493   call->init_req(TypeFunc::I_O,       top());    // does no i/o
1494   call->init_req(TypeFunc::Memory,    slow_mem); // may gc ptrs
1495   call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1496   call->init_req(TypeFunc::FramePtr,  alloc->in(TypeFunc::FramePtr));
1497 
1498   call->init_req(TypeFunc::Parms+0, klass_node);
1499   if (length != NULL) {
1500     call->init_req(TypeFunc::Parms+1, length);
1501   } else {
1502     // Let the runtime know if this is a larval allocation
1503     call->init_req(TypeFunc::Parms+1, _igvn.intcon(alloc->_larval));
1504   }
1505 
1506   // Copy debug information and adjust JVMState information, then replace
1507   // allocate node with the call
1508   call->copy_call_debug_info(&_igvn, alloc);
1509   if (expand_fast_path) {
1510     call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
1511   } else {
1512     // Hook i_o projection to avoid its elimination during allocation
1513     // replacement (when only a slow call is generated).
1514     call->set_req(TypeFunc::I_O, result_phi_i_o);
1515   }
1516   _igvn.replace_node(alloc, call);
1517   transform_later(call);
1518 
1519   // Identify the output projections from the allocate node and
1520   // adjust any references to them.
1521   // The control and io projections look like:
1522   //
1523   //        v---Proj(ctrl) <-----+   v---CatchProj(ctrl)
1524   //  Allocate                   Catch
1525   //        ^---Proj(io) <-------+   ^---CatchProj(io)
1526   //
1527   //  We are interested in the CatchProj nodes.
1528   //
1529   _callprojs = call->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1530 
1531   // An allocate node has separate memory projections for the uses on
1532   // the control and i_o paths. Replace the control memory projection with
1533   // result_phi_rawmem (unless we are only generating a slow call when
1534   // both memory projections are combined)
1535   if (expand_fast_path && _callprojs->fallthrough_memproj != NULL) {
1536     _igvn.replace_in_uses(_callprojs->fallthrough_memproj, result_phi_rawmem);
1537   }
1538   // Now change uses of catchall_memproj to use fallthrough_memproj and delete
1539   // catchall_memproj so we end up with a call that has only 1 memory projection.
1540   if (_callprojs->catchall_memproj != NULL) {
1541     if (_callprojs->fallthrough_memproj == NULL) {
1542       _callprojs->fallthrough_memproj = new ProjNode(call, TypeFunc::Memory);
1543       transform_later(_callprojs->fallthrough_memproj);
1544     }
1545     _igvn.replace_in_uses(_callprojs->catchall_memproj, _callprojs->fallthrough_memproj);
1546     _igvn.remove_dead_node(_callprojs->catchall_memproj);
1547   }
1548 
1549   // An allocate node has separate i_o projections for the uses on the control
1550   // and i_o paths. Always replace the control i_o projection with result i_o
1551   // otherwise incoming i_o become dead when only a slow call is generated
1552   // (it is different from memory projections where both projections are
1553   // combined in such case).
1554   if (_callprojs->fallthrough_ioproj != NULL) {
1555     _igvn.replace_in_uses(_callprojs->fallthrough_ioproj, result_phi_i_o);
1556   }
1557   // Now change uses of catchall_ioproj to use fallthrough_ioproj and delete
1558   // catchall_ioproj so we end up with a call that has only 1 i_o projection.
1559   if (_callprojs->catchall_ioproj != NULL) {
1560     if (_callprojs->fallthrough_ioproj == NULL) {
1561       _callprojs->fallthrough_ioproj = new ProjNode(call, TypeFunc::I_O);
1562       transform_later(_callprojs->fallthrough_ioproj);
1563     }
1564     _igvn.replace_in_uses(_callprojs->catchall_ioproj, _callprojs->fallthrough_ioproj);
1565     _igvn.remove_dead_node(_callprojs->catchall_ioproj);
1566   }
1567 
1568   // if we generated only a slow call, we are done
1569   if (!expand_fast_path) {
1570     // Now we can unhook i_o.
1571     if (result_phi_i_o->outcnt() > 1) {
1572       call->set_req(TypeFunc::I_O, top());
1573     } else {
1574       assert(result_phi_i_o->unique_ctrl_out() == call, "sanity");
1575       // Case of new array with negative size known during compilation.
1576       // AllocateArrayNode::Ideal() optimization disconnect unreachable
1577       // following code since call to runtime will throw exception.
1578       // As result there will be no users of i_o after the call.
1579       // Leave i_o attached to this call to avoid problems in preceding graph.
1580     }
1581     return;
1582   }
1583 
1584   if (_callprojs->fallthrough_catchproj != NULL) {
1585     ctrl = _callprojs->fallthrough_catchproj->clone();
1586     transform_later(ctrl);
1587     _igvn.replace_node(_callprojs->fallthrough_catchproj, result_region);
1588   } else {
1589     ctrl = top();
1590   }
1591   Node *slow_result;
1592   if (_callprojs->resproj[0] == NULL) {
1593     // no uses of the allocation result
1594     slow_result = top();
1595   } else {
1596     slow_result = _callprojs->resproj[0]->clone();
1597     transform_later(slow_result);
1598     _igvn.replace_node(_callprojs->resproj[0], result_phi_rawoop);
1599   }
1600 
1601   // Plug slow-path into result merge point
1602   result_region->init_req( slow_result_path, ctrl);
1603   transform_later(result_region);
1604   if (allocation_has_use) {
1605     result_phi_rawoop->init_req(slow_result_path, slow_result);
1606     transform_later(result_phi_rawoop);
1607   }
1608   result_phi_rawmem->init_req(slow_result_path, _callprojs->fallthrough_memproj);
1609   transform_later(result_phi_rawmem);
1610   transform_later(result_phi_i_o);
1611   // This completes all paths into the result merge point
1612 }
1613 
1614 // Remove alloc node that has no uses.
1615 void PhaseMacroExpand::yank_alloc_node(AllocateNode* alloc) {
1616   Node* ctrl = alloc->in(TypeFunc::Control);
1617   Node* mem  = alloc->in(TypeFunc::Memory);
1618   Node* i_o  = alloc->in(TypeFunc::I_O);
1619 
1620   _callprojs = alloc->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1621   if (_callprojs->resproj[0] != NULL) {
1622     for (DUIterator_Fast imax, i = _callprojs->resproj[0]->fast_outs(imax); i < imax; i++) {
1623       Node* use = _callprojs->resproj[0]->fast_out(i);
1624       use->isa_MemBar()->remove(&_igvn);
1625       --imax;
1626       --i; // back up iterator
1627     }
1628     assert(_callprojs->resproj[0]->outcnt() == 0, "all uses must be deleted");
1629     _igvn.remove_dead_node(_callprojs->resproj[0]);
1630   }
1631   if (_callprojs->fallthrough_catchproj != NULL) {
1632     _igvn.replace_in_uses(_callprojs->fallthrough_catchproj, ctrl);
1633     _igvn.remove_dead_node(_callprojs->fallthrough_catchproj);
1634   }
1635   if (_callprojs->catchall_catchproj != NULL) {
1636     _igvn.rehash_node_delayed(_callprojs->catchall_catchproj);
1637     _callprojs->catchall_catchproj->set_req(0, top());
1638   }
1639   if (_callprojs->fallthrough_proj != NULL) {
1640     Node* catchnode = _callprojs->fallthrough_proj->unique_ctrl_out();
1641     _igvn.remove_dead_node(catchnode);
1642     _igvn.remove_dead_node(_callprojs->fallthrough_proj);
1643   }
1644   if (_callprojs->fallthrough_memproj != NULL) {
1645     _igvn.replace_in_uses(_callprojs->fallthrough_memproj, mem);
1646     _igvn.remove_dead_node(_callprojs->fallthrough_memproj);
1647   }
1648   if (_callprojs->fallthrough_ioproj != NULL) {
1649     _igvn.replace_in_uses(_callprojs->fallthrough_ioproj, i_o);
1650     _igvn.remove_dead_node(_callprojs->fallthrough_ioproj);
1651   }
1652   if (_callprojs->catchall_memproj != NULL) {
1653     _igvn.rehash_node_delayed(_callprojs->catchall_memproj);
1654     _callprojs->catchall_memproj->set_req(0, top());
1655   }
1656   if (_callprojs->catchall_ioproj != NULL) {
1657     _igvn.rehash_node_delayed(_callprojs->catchall_ioproj);
1658     _callprojs->catchall_ioproj->set_req(0, top());
1659   }
1660 #ifndef PRODUCT
1661   if (PrintEliminateAllocations) {
1662     if (alloc->is_AllocateArray()) {
1663       tty->print_cr("++++ Eliminated: %d AllocateArray", alloc->_idx);
1664     } else {
1665       tty->print_cr("++++ Eliminated: %d Allocate", alloc->_idx);
1666     }
1667   }
1668 #endif
1669   _igvn.remove_dead_node(alloc);
1670 }
1671 
1672 void PhaseMacroExpand::expand_initialize_membar(AllocateNode* alloc, InitializeNode* init,
1673                                                 Node*& fast_oop_ctrl, Node*& fast_oop_rawmem) {
1674   // If initialization is performed by an array copy, any required
1675   // MemBarStoreStore was already added. If the object does not
1676   // escape no need for a MemBarStoreStore. If the object does not
1677   // escape in its initializer and memory barrier (MemBarStoreStore or
1678   // stronger) is already added at exit of initializer, also no need

1756     Node* thread = new ThreadLocalNode();
1757     transform_later(thread);
1758 
1759     call->init_req(TypeFunc::Parms + 0, thread);
1760     call->init_req(TypeFunc::Parms + 1, oop);
1761     call->init_req(TypeFunc::Control, ctrl);
1762     call->init_req(TypeFunc::I_O    , top()); // does no i/o
1763     call->init_req(TypeFunc::Memory , rawmem);
1764     call->init_req(TypeFunc::ReturnAdr, alloc->in(TypeFunc::ReturnAdr));
1765     call->init_req(TypeFunc::FramePtr, alloc->in(TypeFunc::FramePtr));
1766     transform_later(call);
1767     ctrl = new ProjNode(call, TypeFunc::Control);
1768     transform_later(ctrl);
1769     rawmem = new ProjNode(call, TypeFunc::Memory);
1770     transform_later(rawmem);
1771   }
1772 }
1773 
1774 // Helper for PhaseMacroExpand::expand_allocate_common.
1775 // Initializes the newly-allocated storage.
1776 Node* PhaseMacroExpand::initialize_object(AllocateNode* alloc,
1777                                           Node* control, Node* rawmem, Node* object,
1778                                           Node* klass_node, Node* length,
1779                                           Node* size_in_bytes) {

1780   InitializeNode* init = alloc->initialization();
1781   // Store the klass & mark bits
1782   Node* mark_node = alloc->make_ideal_mark(&_igvn, control, rawmem);
1783   if (!mark_node->is_Con()) {
1784     transform_later(mark_node);
1785   }
1786   rawmem = make_store(control, rawmem, object, oopDesc::mark_offset_in_bytes(), mark_node, TypeX_X->basic_type());
1787 
1788   rawmem = make_store(control, rawmem, object, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA);
1789   int header_size = alloc->minimum_header_size();  // conservatively small
1790 
1791   // Array length
1792   if (length != NULL) {         // Arrays need length field
1793     rawmem = make_store(control, rawmem, object, arrayOopDesc::length_offset_in_bytes(), length, T_INT);
1794     // conservatively small header size:
1795     header_size = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1796     if (_igvn.type(klass_node)->isa_aryklassptr()) {   // we know the exact header size in most cases:
1797       BasicType elem = _igvn.type(klass_node)->is_klassptr()->as_instance_type()->isa_aryptr()->elem()->array_element_basic_type();
1798       if (is_reference_type(elem, true)) {
1799         elem = T_OBJECT;
1800       }
1801       header_size = Klass::layout_helper_header_size(Klass::array_layout_helper(elem));
1802     }
1803   }
1804 
1805   // Clear the object body, if necessary.
1806   if (init == NULL) {
1807     // The init has somehow disappeared; be cautious and clear everything.
1808     //
1809     // This can happen if a node is allocated but an uncommon trap occurs
1810     // immediately.  In this case, the Initialize gets associated with the
1811     // trap, and may be placed in a different (outer) loop, if the Allocate
1812     // is in a loop.  If (this is rare) the inner loop gets unrolled, then
1813     // there can be two Allocates to one Initialize.  The answer in all these
1814     // edge cases is safety first.  It is always safe to clear immediately
1815     // within an Allocate, and then (maybe or maybe not) clear some more later.
1816     if (!(UseTLAB && ZeroTLAB)) {
1817       rawmem = ClearArrayNode::clear_memory(control, rawmem, object,
1818                                             alloc->in(AllocateNode::DefaultValue),
1819                                             alloc->in(AllocateNode::RawDefaultValue),
1820                                             header_size, size_in_bytes,
1821                                             &_igvn);
1822     }
1823   } else {
1824     if (!init->is_complete()) {
1825       // Try to win by zeroing only what the init does not store.
1826       // We can also try to do some peephole optimizations,
1827       // such as combining some adjacent subword stores.
1828       rawmem = init->complete_stores(control, rawmem, object,
1829                                      header_size, size_in_bytes, &_igvn);
1830     }
1831     // We have no more use for this link, since the AllocateNode goes away:
1832     init->set_req(InitializeNode::RawAddress, top());
1833     // (If we keep the link, it just confuses the register allocator,
1834     // who thinks he sees a real use of the address by the membar.)
1835   }
1836 
1837   return rawmem;
1838 }
1839 

2168   } // EliminateNestedLocks
2169 
2170   if (alock->is_non_esc_obj()) { // Lock is used for non escaping object
2171     // Look for all locks of this object and mark them and
2172     // corresponding BoxLock nodes as eliminated.
2173     Node* obj = alock->obj_node();
2174     for (uint j = 0; j < obj->outcnt(); j++) {
2175       Node* o = obj->raw_out(j);
2176       if (o->is_AbstractLock() &&
2177           o->as_AbstractLock()->obj_node()->eqv_uncast(obj)) {
2178         alock = o->as_AbstractLock();
2179         Node* box = alock->box_node();
2180         // Replace old box node with new eliminated box for all users
2181         // of the same object and mark related locks as eliminated.
2182         mark_eliminated_box(box, obj);
2183       }
2184     }
2185   }
2186 }
2187 
2188 void PhaseMacroExpand::inline_type_guard(Node** ctrl, LockNode* lock) {
2189   Node* obj = lock->obj_node();
2190   const TypePtr* obj_type = _igvn.type(obj)->make_ptr();
2191   if (!obj_type->can_be_inline_type()) {
2192     return;
2193   }
2194   Node* mark = make_load(*ctrl, lock->memory(), obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
2195   Node* value_mask = _igvn.MakeConX(markWord::inline_type_pattern);
2196   Node* is_value = _igvn.transform(new AndXNode(mark, value_mask));
2197   Node* cmp = _igvn.transform(new CmpXNode(is_value, value_mask));
2198   Node* bol = _igvn.transform(new BoolNode(cmp, BoolTest::eq));
2199   Node* unc_ctrl = generate_slow_guard(ctrl, bol, NULL);
2200 
2201   int trap_request = Deoptimization::make_trap_request(Deoptimization::Reason_class_check, Deoptimization::Action_none);
2202   address call_addr = SharedRuntime::uncommon_trap_blob()->entry_point();
2203   const TypePtr* no_memory_effects = NULL;
2204   CallNode* unc = new CallStaticJavaNode(OptoRuntime::uncommon_trap_Type(), call_addr, "uncommon_trap",
2205                                          no_memory_effects);
2206   unc->init_req(TypeFunc::Control, unc_ctrl);
2207   unc->init_req(TypeFunc::I_O, lock->i_o());
2208   unc->init_req(TypeFunc::Memory, lock->memory());
2209   unc->init_req(TypeFunc::FramePtr,  lock->in(TypeFunc::FramePtr));
2210   unc->init_req(TypeFunc::ReturnAdr, lock->in(TypeFunc::ReturnAdr));
2211   unc->init_req(TypeFunc::Parms+0, _igvn.intcon(trap_request));
2212   unc->set_cnt(PROB_UNLIKELY_MAG(4));
2213   unc->copy_call_debug_info(&_igvn, lock);
2214 
2215   assert(unc->peek_monitor_box() == lock->box_node(), "wrong monitor");
2216   assert((obj_type->is_inlinetypeptr() && unc->peek_monitor_obj()->is_SafePointScalarObject()) ||
2217          (obj->is_InlineTypePtr() && obj->in(1) == unc->peek_monitor_obj()) ||
2218          (obj == unc->peek_monitor_obj()), "wrong monitor");
2219 
2220   // pop monitor and push obj back on stack: we trap before the monitorenter
2221   unc->pop_monitor();
2222   unc->grow_stack(unc->jvms(), 1);
2223   unc->set_stack(unc->jvms(), unc->jvms()->stk_size()-1, obj);
2224   _igvn.register_new_node_with_optimizer(unc);
2225 
2226   unc_ctrl = _igvn.transform(new ProjNode(unc, TypeFunc::Control));
2227   Node* halt = _igvn.transform(new HaltNode(unc_ctrl, lock->in(TypeFunc::FramePtr), "monitor enter on inline type"));
2228   _igvn.add_input_to(C->root(), halt);
2229 }
2230 
2231 // we have determined that this lock/unlock can be eliminated, we simply
2232 // eliminate the node without expanding it.
2233 //
2234 // Note:  The membar's associated with the lock/unlock are currently not
2235 //        eliminated.  This should be investigated as a future enhancement.
2236 //
2237 bool PhaseMacroExpand::eliminate_locking_node(AbstractLockNode *alock) {
2238 
2239   if (!alock->is_eliminated()) {
2240     return false;
2241   }
2242 #ifdef ASSERT
2243   if (!alock->is_coarsened()) {
2244     // Check that new "eliminated" BoxLock node is created.
2245     BoxLockNode* oldbox = alock->box_node()->as_BoxLock();
2246     assert(oldbox->is_eliminated(), "should be done already");
2247   }
2248 #endif
2249 
2250   alock->log_lock_optimization(C, "eliminate_lock");
2251 
2252 #ifndef PRODUCT
2253   if (PrintEliminateLocks) {
2254     tty->print_cr("++++ Eliminated: %d %s '%s'", alock->_idx, (alock->is_Lock() ? "Lock" : "Unlock"), alock->kind_as_string());
2255   }
2256 #endif
2257 
2258   Node* mem  = alock->in(TypeFunc::Memory);
2259   Node* ctrl = alock->in(TypeFunc::Control);
2260   guarantee(ctrl != NULL, "missing control projection, cannot replace_node() with NULL");
2261 
2262   _callprojs = alock->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
2263   // There are 2 projections from the lock.  The lock node will
2264   // be deleted when its last use is subsumed below.
2265   assert(alock->outcnt() == 2 &&
2266          _callprojs->fallthrough_proj != NULL &&
2267          _callprojs->fallthrough_memproj != NULL,
2268          "Unexpected projections from Lock/Unlock");
2269 
2270   Node* fallthroughproj = _callprojs->fallthrough_proj;
2271   Node* memproj_fallthrough = _callprojs->fallthrough_memproj;
2272 
2273   // The memory projection from a lock/unlock is RawMem
2274   // The input to a Lock is merged memory, so extract its RawMem input
2275   // (unless the MergeMem has been optimized away.)
2276   if (alock->is_Lock()) {
2277     // Deoptimize and re-execute if object is an inline type
2278     inline_type_guard(&ctrl, alock->as_Lock());
2279 
2280     // Search for MemBarAcquireLock node and delete it also.
2281     MemBarNode* membar = fallthroughproj->unique_ctrl_out()->as_MemBar();
2282     assert(membar != NULL && membar->Opcode() == Op_MemBarAcquireLock, "");
2283     Node* ctrlproj = membar->proj_out(TypeFunc::Control);
2284     Node* memproj = membar->proj_out(TypeFunc::Memory);
2285     _igvn.replace_node(ctrlproj, fallthroughproj);
2286     _igvn.replace_node(memproj, memproj_fallthrough);
2287 
2288     // Delete FastLock node also if this Lock node is unique user
2289     // (a loop peeling may clone a Lock node).
2290     Node* flock = alock->as_Lock()->fastlock_node();
2291     if (flock->outcnt() == 1) {
2292       assert(flock->unique_out() == alock, "sanity");
2293       _igvn.replace_node(flock, top());
2294     }
2295   }
2296 
2297   // Search for MemBarReleaseLock node and delete it also.
2298   if (alock->is_Unlock() && ctrl->is_Proj() && ctrl->in(0)->is_MemBar()) {
2299     MemBarNode* membar = ctrl->in(0)->as_MemBar();

2320   Node* mem = lock->in(TypeFunc::Memory);
2321   Node* obj = lock->obj_node();
2322   Node* box = lock->box_node();
2323   Node* flock = lock->fastlock_node();
2324 
2325   assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2326 
2327   // Make the merge point
2328   Node *region;
2329   Node *mem_phi;
2330   Node *slow_path;
2331 
2332   region  = new RegionNode(3);
2333   // create a Phi for the memory state
2334   mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2335 
2336   // Optimize test; set region slot 2
2337   slow_path = opt_bits_test(ctrl, region, 2, flock, 0, 0);
2338   mem_phi->init_req(2, mem);
2339 
2340   // Deoptimize and re-execute if object is an inline type
2341   inline_type_guard(&slow_path, lock);
2342 
2343   // Make slow path call
2344   CallNode *call = make_slow_call((CallNode *) lock, OptoRuntime::complete_monitor_enter_Type(),
2345                                   OptoRuntime::complete_monitor_locking_Java(), NULL, slow_path,
2346                                   obj, box, NULL);
2347 
2348   _callprojs = call->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
2349 
2350   // Slow path can only throw asynchronous exceptions, which are always
2351   // de-opted.  So the compiler thinks the slow-call can never throw an
2352   // exception.  If it DOES throw an exception we would need the debug
2353   // info removed first (since if it throws there is no monitor).
2354   assert(_callprojs->fallthrough_ioproj == NULL && _callprojs->catchall_ioproj == NULL &&
2355          _callprojs->catchall_memproj == NULL && _callprojs->catchall_catchproj == NULL, "Unexpected projection from Lock");
2356 
2357   // Capture slow path
2358   // disconnect fall-through projection from call and create a new one
2359   // hook up users of fall-through projection to region
2360   Node *slow_ctrl = _callprojs->fallthrough_proj->clone();
2361   transform_later(slow_ctrl);
2362   _igvn.hash_delete(_callprojs->fallthrough_proj);
2363   _callprojs->fallthrough_proj->disconnect_inputs(C);
2364   region->init_req(1, slow_ctrl);
2365   // region inputs are now complete
2366   transform_later(region);
2367   _igvn.replace_node(_callprojs->fallthrough_proj, region);
2368 
2369   Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory));
2370 
2371   mem_phi->init_req(1, memproj);
2372 
2373   transform_later(mem_phi);
2374 
2375   _igvn.replace_node(_callprojs->fallthrough_memproj, mem_phi);
2376 }
2377 
2378 //------------------------------expand_unlock_node----------------------
2379 void PhaseMacroExpand::expand_unlock_node(UnlockNode *unlock) {
2380 
2381   Node* ctrl = unlock->in(TypeFunc::Control);
2382   Node* mem = unlock->in(TypeFunc::Memory);
2383   Node* obj = unlock->obj_node();
2384   Node* box = unlock->box_node();
2385 
2386   assert(!box->as_BoxLock()->is_eliminated(), "sanity");
2387 
2388   // No need for a null check on unlock
2389 
2390   // Make the merge point
2391   Node *region;
2392   Node *mem_phi;
2393 
2394   region  = new RegionNode(3);
2395   // create a Phi for the memory state
2396   mem_phi = new PhiNode( region, Type::MEMORY, TypeRawPtr::BOTTOM);
2397 
2398   FastUnlockNode *funlock = new FastUnlockNode( ctrl, obj, box );
2399   funlock = transform_later( funlock )->as_FastUnlock();
2400   // Optimize test; set region slot 2
2401   Node *slow_path = opt_bits_test(ctrl, region, 2, funlock, 0, 0);
2402   Node *thread = transform_later(new ThreadLocalNode());
2403 
2404   CallNode *call = make_slow_call((CallNode *) unlock, OptoRuntime::complete_monitor_exit_Type(),
2405                                   CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C),
2406                                   "complete_monitor_unlocking_C", slow_path, obj, box, thread);
2407 
2408   _callprojs = call->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
2409   assert(_callprojs->fallthrough_ioproj == NULL && _callprojs->catchall_ioproj == NULL &&
2410          _callprojs->catchall_memproj == NULL && _callprojs->catchall_catchproj == NULL, "Unexpected projection from Lock");
2411 
2412   // No exceptions for unlocking
2413   // Capture slow path
2414   // disconnect fall-through projection from call and create a new one
2415   // hook up users of fall-through projection to region
2416   Node *slow_ctrl = _callprojs->fallthrough_proj->clone();
2417   transform_later(slow_ctrl);
2418   _igvn.hash_delete(_callprojs->fallthrough_proj);
2419   _callprojs->fallthrough_proj->disconnect_inputs(C);
2420   region->init_req(1, slow_ctrl);
2421   // region inputs are now complete
2422   transform_later(region);
2423   _igvn.replace_node(_callprojs->fallthrough_proj, region);
2424 
2425   Node *memproj = transform_later(new ProjNode(call, TypeFunc::Memory) );
2426   mem_phi->init_req(1, memproj );
2427   mem_phi->init_req(2, mem);
2428   transform_later(mem_phi);
2429 
2430   _igvn.replace_node(_callprojs->fallthrough_memproj, mem_phi);
2431 }
2432 
2433 // An inline type might be returned from the call but we don't know its
2434 // type. Either we get a buffered inline type (and nothing needs to be done)
2435 // or one of the inlines being returned is the klass of the inline type
2436 // and we need to allocate an inline type instance of that type and
2437 // initialize it with other values being returned. In that case, we
2438 // first try a fast path allocation and initialize the value with the
2439 // inline klass's pack handler or we fall back to a runtime call.
2440 void PhaseMacroExpand::expand_mh_intrinsic_return(CallStaticJavaNode* call) {
2441   assert(call->method()->is_method_handle_intrinsic(), "must be a method handle intrinsic call");
2442   Node* ret = call->proj_out_or_null(TypeFunc::Parms);
2443   if (ret == NULL) {
2444     return;
2445   }
2446   const TypeFunc* tf = call->_tf;
2447   const TypeTuple* domain = OptoRuntime::store_inline_type_fields_Type()->domain_cc();
2448   const TypeFunc* new_tf = TypeFunc::make(tf->domain_sig(), tf->domain_cc(), tf->range_sig(), domain);
2449   call->_tf = new_tf;
2450   // Make sure the change of type is applied before projections are processed by igvn
2451   _igvn.set_type(call, call->Value(&_igvn));
2452   _igvn.set_type(ret, ret->Value(&_igvn));
2453 
2454   // Before any new projection is added:
2455   CallProjections* projs = call->extract_projections(true, true);
2456 
2457   // Create temporary hook nodes that will be replaced below.
2458   // Add an input to prevent hook nodes from being dead.
2459   Node* ctl = new Node(call);
2460   Node* mem = new Node(ctl);
2461   Node* io = new Node(ctl);
2462   Node* ex_ctl = new Node(ctl);
2463   Node* ex_mem = new Node(ctl);
2464   Node* ex_io = new Node(ctl);
2465   Node* res = new Node(ctl);
2466 
2467   // Allocate a new buffered inline type only if a new one is not returned
2468   Node* cast = transform_later(new CastP2XNode(ctl, res));
2469   Node* mask = MakeConX(0x1);
2470   Node* masked = transform_later(new AndXNode(cast, mask));
2471   Node* cmp = transform_later(new CmpXNode(masked, mask));
2472   Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq));
2473   IfNode* allocation_iff = new IfNode(ctl, bol, PROB_MAX, COUNT_UNKNOWN);
2474   transform_later(allocation_iff);
2475   Node* allocation_ctl = transform_later(new IfTrueNode(allocation_iff));
2476   Node* no_allocation_ctl = transform_later(new IfFalseNode(allocation_iff));
2477   Node* no_allocation_res = transform_later(new CheckCastPPNode(no_allocation_ctl, res, TypeInstPtr::BOTTOM));
2478 
2479   // Try to allocate a new buffered inline instance either from TLAB or eden space
2480   Node* needgc_ctrl = NULL; // needgc means slowcase, i.e. allocation failed
2481   CallLeafNoFPNode* handler_call;
2482   const bool alloc_in_place = UseTLAB;
2483   if (alloc_in_place) {
2484     Node* fast_oop_ctrl = NULL;
2485     Node* fast_oop_rawmem = NULL;
2486     Node* mask2 = MakeConX(-2);
2487     Node* masked2 = transform_later(new AndXNode(cast, mask2));
2488     Node* rawklassptr = transform_later(new CastX2PNode(masked2));
2489     Node* klass_node = transform_later(new CheckCastPPNode(allocation_ctl, rawklassptr, TypeInstKlassPtr::OBJECT_OR_NULL));
2490     Node* layout_val = make_load(NULL, mem, klass_node, in_bytes(Klass::layout_helper_offset()), TypeInt::INT, T_INT);
2491     Node* size_in_bytes = ConvI2X(layout_val);
2492     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2493     Node* fast_oop = bs->obj_allocate(this, mem, allocation_ctl, size_in_bytes, io, needgc_ctrl,
2494                                       fast_oop_ctrl, fast_oop_rawmem,
2495                                       AllocateInstancePrefetchLines);
2496     // Allocation succeed, initialize buffered inline instance header firstly,
2497     // and then initialize its fields with an inline class specific handler
2498     Node* mark_node = makecon(TypeRawPtr::make((address)markWord::inline_type_prototype().value()));
2499     fast_oop_rawmem = make_store(fast_oop_ctrl, fast_oop_rawmem, fast_oop, oopDesc::mark_offset_in_bytes(), mark_node, T_ADDRESS);
2500     fast_oop_rawmem = make_store(fast_oop_ctrl, fast_oop_rawmem, fast_oop, oopDesc::klass_offset_in_bytes(), klass_node, T_METADATA);
2501     if (UseCompressedClassPointers) {
2502       fast_oop_rawmem = make_store(fast_oop_ctrl, fast_oop_rawmem, fast_oop, oopDesc::klass_gap_offset_in_bytes(), intcon(0), T_INT);
2503     }
2504     Node* fixed_block  = make_load(fast_oop_ctrl, fast_oop_rawmem, klass_node, in_bytes(InstanceKlass::adr_inlineklass_fixed_block_offset()), TypeRawPtr::BOTTOM, T_ADDRESS);
2505     Node* pack_handler = make_load(fast_oop_ctrl, fast_oop_rawmem, fixed_block, in_bytes(InlineKlass::pack_handler_offset()), TypeRawPtr::BOTTOM, T_ADDRESS);
2506     handler_call = new CallLeafNoFPNode(OptoRuntime::pack_inline_type_Type(),
2507                                         NULL,
2508                                         "pack handler",
2509                                         TypeRawPtr::BOTTOM);
2510     handler_call->init_req(TypeFunc::Control, fast_oop_ctrl);
2511     handler_call->init_req(TypeFunc::Memory, fast_oop_rawmem);
2512     handler_call->init_req(TypeFunc::I_O, top());
2513     handler_call->init_req(TypeFunc::FramePtr, call->in(TypeFunc::FramePtr));
2514     handler_call->init_req(TypeFunc::ReturnAdr, top());
2515     handler_call->init_req(TypeFunc::Parms, pack_handler);
2516     handler_call->init_req(TypeFunc::Parms+1, fast_oop);
2517   } else {
2518     needgc_ctrl = allocation_ctl;
2519   }
2520 
2521   // Allocation failed, fall back to a runtime call
2522   CallStaticJavaNode* slow_call = new CallStaticJavaNode(OptoRuntime::store_inline_type_fields_Type(),
2523                                                          StubRoutines::store_inline_type_fields_to_buf(),
2524                                                          "store_inline_type_fields",
2525                                                          TypePtr::BOTTOM);
2526   slow_call->init_req(TypeFunc::Control, needgc_ctrl);
2527   slow_call->init_req(TypeFunc::Memory, mem);
2528   slow_call->init_req(TypeFunc::I_O, io);
2529   slow_call->init_req(TypeFunc::FramePtr, call->in(TypeFunc::FramePtr));
2530   slow_call->init_req(TypeFunc::ReturnAdr, call->in(TypeFunc::ReturnAdr));
2531   slow_call->init_req(TypeFunc::Parms, res);
2532 
2533   Node* slow_ctl = transform_later(new ProjNode(slow_call, TypeFunc::Control));
2534   Node* slow_mem = transform_later(new ProjNode(slow_call, TypeFunc::Memory));
2535   Node* slow_io = transform_later(new ProjNode(slow_call, TypeFunc::I_O));
2536   Node* slow_res = transform_later(new ProjNode(slow_call, TypeFunc::Parms));
2537   Node* slow_catc = transform_later(new CatchNode(slow_ctl, slow_io, 2));
2538   Node* slow_norm = transform_later(new CatchProjNode(slow_catc, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci));
2539   Node* slow_excp = transform_later(new CatchProjNode(slow_catc, CatchProjNode::catch_all_index,    CatchProjNode::no_handler_bci));
2540 
2541   Node* ex_r = new RegionNode(3);
2542   Node* ex_mem_phi = new PhiNode(ex_r, Type::MEMORY, TypePtr::BOTTOM);
2543   Node* ex_io_phi = new PhiNode(ex_r, Type::ABIO);
2544   ex_r->init_req(1, slow_excp);
2545   ex_mem_phi->init_req(1, slow_mem);
2546   ex_io_phi->init_req(1, slow_io);
2547   ex_r->init_req(2, ex_ctl);
2548   ex_mem_phi->init_req(2, ex_mem);
2549   ex_io_phi->init_req(2, ex_io);
2550   transform_later(ex_r);
2551   transform_later(ex_mem_phi);
2552   transform_later(ex_io_phi);
2553 
2554   // We don't know how many values are returned. This assumes the
2555   // worst case, that all available registers are used.
2556   for (uint i = TypeFunc::Parms+1; i < domain->cnt(); i++) {
2557     if (domain->field_at(i) == Type::HALF) {
2558       slow_call->init_req(i, top());
2559       if (alloc_in_place) {
2560         handler_call->init_req(i+1, top());
2561       }
2562       continue;
2563     }
2564     Node* proj = transform_later(new ProjNode(call, i));
2565     slow_call->init_req(i, proj);
2566     if (alloc_in_place) {
2567       handler_call->init_req(i+1, proj);
2568     }
2569   }
2570   // We can safepoint at that new call
2571   slow_call->copy_call_debug_info(&_igvn, call);
2572   transform_later(slow_call);
2573   if (alloc_in_place) {
2574     transform_later(handler_call);
2575   }
2576 
2577   Node* fast_ctl = NULL;
2578   Node* fast_res = NULL;
2579   MergeMemNode* fast_mem = NULL;
2580   if (alloc_in_place) {
2581     fast_ctl = transform_later(new ProjNode(handler_call, TypeFunc::Control));
2582     Node* rawmem = transform_later(new ProjNode(handler_call, TypeFunc::Memory));
2583     fast_res = transform_later(new ProjNode(handler_call, TypeFunc::Parms));
2584     fast_mem = MergeMemNode::make(mem);
2585     fast_mem->set_memory_at(Compile::AliasIdxRaw, rawmem);
2586     transform_later(fast_mem);
2587   }
2588 
2589   Node* r = new RegionNode(alloc_in_place ? 4 : 3);
2590   Node* mem_phi = new PhiNode(r, Type::MEMORY, TypePtr::BOTTOM);
2591   Node* io_phi = new PhiNode(r, Type::ABIO);
2592   Node* res_phi = new PhiNode(r, TypeInstPtr::BOTTOM);
2593   r->init_req(1, no_allocation_ctl);
2594   mem_phi->init_req(1, mem);
2595   io_phi->init_req(1, io);
2596   res_phi->init_req(1, no_allocation_res);
2597   r->init_req(2, slow_norm);
2598   mem_phi->init_req(2, slow_mem);
2599   io_phi->init_req(2, slow_io);
2600   res_phi->init_req(2, slow_res);
2601   if (alloc_in_place) {
2602     r->init_req(3, fast_ctl);
2603     mem_phi->init_req(3, fast_mem);
2604     io_phi->init_req(3, io);
2605     res_phi->init_req(3, fast_res);
2606   }
2607   transform_later(r);
2608   transform_later(mem_phi);
2609   transform_later(io_phi);
2610   transform_later(res_phi);
2611 
2612   assert(projs->nb_resproj == 1, "unexpected number of results");
2613   _igvn.replace_in_uses(projs->fallthrough_catchproj, r);
2614   _igvn.replace_in_uses(projs->fallthrough_memproj, mem_phi);
2615   _igvn.replace_in_uses(projs->fallthrough_ioproj, io_phi);
2616   _igvn.replace_in_uses(projs->resproj[0], res_phi);
2617   _igvn.replace_in_uses(projs->catchall_catchproj, ex_r);
2618   _igvn.replace_in_uses(projs->catchall_memproj, ex_mem_phi);
2619   _igvn.replace_in_uses(projs->catchall_ioproj, ex_io_phi);
2620   // The CatchNode should not use the ex_io_phi. Re-connect it to the catchall_ioproj.
2621   Node* cn = projs->fallthrough_catchproj->in(0);
2622   _igvn.replace_input_of(cn, 1, projs->catchall_ioproj);
2623 
2624   _igvn.replace_node(ctl, projs->fallthrough_catchproj);
2625   _igvn.replace_node(mem, projs->fallthrough_memproj);
2626   _igvn.replace_node(io, projs->fallthrough_ioproj);
2627   _igvn.replace_node(res, projs->resproj[0]);
2628   _igvn.replace_node(ex_ctl, projs->catchall_catchproj);
2629   _igvn.replace_node(ex_mem, projs->catchall_memproj);
2630   _igvn.replace_node(ex_io, projs->catchall_ioproj);
2631  }
2632 
2633 void PhaseMacroExpand::expand_subtypecheck_node(SubTypeCheckNode *check) {
2634   assert(check->in(SubTypeCheckNode::Control) == NULL, "should be pinned");
2635   Node* bol = check->unique_out();
2636   Node* obj_or_subklass = check->in(SubTypeCheckNode::ObjOrSubKlass);
2637   Node* superklass = check->in(SubTypeCheckNode::SuperKlass);
2638   assert(bol->is_Bool() && bol->as_Bool()->_test._test == BoolTest::ne, "unexpected bool node");
2639 
2640   for (DUIterator_Last imin, i = bol->last_outs(imin); i >= imin; --i) {
2641     Node* iff = bol->last_out(i);
2642     assert(iff->is_If(), "where's the if?");
2643 
2644     if (iff->in(0)->is_top()) {
2645       _igvn.replace_input_of(iff, 1, C->top());
2646       continue;
2647     }
2648 
2649     Node* iftrue = iff->as_If()->proj_out(1);
2650     Node* iffalse = iff->as_If()->proj_out(0);
2651     Node* ctrl = iff->in(0);
2652 
2653     Node* subklass = NULL;
2654     if (_igvn.type(obj_or_subklass)->isa_klassptr()) {
2655       subklass = obj_or_subklass;
2656     } else {
2657       Node* k_adr = basic_plus_adr(obj_or_subklass, oopDesc::klass_offset_in_bytes());
2658       subklass = _igvn.transform(LoadKlassNode::make(_igvn, NULL, C->immutable_memory(), k_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
2659     }
2660 
2661     Node* not_subtype_ctrl = Phase::gen_subtype_check(subklass, superklass, &ctrl, NULL, _igvn);
2662 
2663     _igvn.replace_input_of(iff, 0, C->top());
2664     _igvn.replace_node(iftrue, not_subtype_ctrl);
2665     _igvn.replace_node(iffalse, ctrl);
2666   }
2667   _igvn.replace_node(check, C->top());
2668 }
2669 
2670 // FlatArrayCheckNode (array1 array2 ...) is expanded into:
2671 //
2672 // long mark = array1.mark | array2.mark | ...;
2673 // long locked_bit = markWord::unlocked_value & array1.mark & array2.mark & ...;
2674 // if (locked_bit == 0) {
2675 //   // One array is locked, load prototype header from the klass
2676 //   mark = array1.klass.proto | array2.klass.proto | ...
2677 // }
2678 // if ((mark & markWord::flat_array_bit_in_place) == 0) {
2679 //    ...
2680 // }
2681 void PhaseMacroExpand::expand_flatarraycheck_node(FlatArrayCheckNode* check) {
2682   bool array_inputs = _igvn.type(check->in(FlatArrayCheckNode::ArrayOrKlass))->isa_oopptr() != NULL;
2683   if (UseArrayMarkWordCheck && array_inputs) {
2684     Node* mark = MakeConX(0);
2685     Node* locked_bit = MakeConX(markWord::unlocked_value);
2686     Node* mem = check->in(FlatArrayCheckNode::Memory);
2687     for (uint i = FlatArrayCheckNode::ArrayOrKlass; i < check->req(); ++i) {
2688       Node* ary = check->in(i);
2689       const TypeOopPtr* t = _igvn.type(ary)->isa_oopptr();
2690       assert(t != NULL, "Mixing array and klass inputs");
2691       assert(!t->is_flat() && !t->is_not_flat(), "Should have been optimized out");
2692       Node* mark_adr = basic_plus_adr(ary, oopDesc::mark_offset_in_bytes());
2693       Node* mark_load = _igvn.transform(LoadNode::make(_igvn, NULL, mem, mark_adr, mark_adr->bottom_type()->is_ptr(), TypeX_X, TypeX_X->basic_type(), MemNode::unordered));
2694       mark = _igvn.transform(new OrXNode(mark, mark_load));
2695       locked_bit = _igvn.transform(new AndXNode(locked_bit, mark_load));
2696     }
2697     assert(!mark->is_Con(), "Should have been optimized out");
2698     Node* cmp = _igvn.transform(new CmpXNode(locked_bit, MakeConX(0)));
2699     Node* is_unlocked = _igvn.transform(new BoolNode(cmp, BoolTest::ne));
2700 
2701     // BoolNode might be shared, replace each if user
2702     Node* old_bol = check->unique_out();
2703     assert(old_bol->is_Bool() && old_bol->as_Bool()->_test._test == BoolTest::ne, "unexpected condition");
2704     for (DUIterator_Last imin, i = old_bol->last_outs(imin); i >= imin; --i) {
2705       IfNode* old_iff = old_bol->last_out(i)->as_If();
2706       Node* ctrl = old_iff->in(0);
2707       RegionNode* region = new RegionNode(3);
2708       Node* mark_phi = new PhiNode(region, TypeX_X);
2709 
2710       // Check if array is unlocked
2711       IfNode* iff = _igvn.transform(new IfNode(ctrl, is_unlocked, PROB_MAX, COUNT_UNKNOWN))->as_If();
2712 
2713       // Unlocked: Use bits from mark word
2714       region->init_req(1, _igvn.transform(new IfTrueNode(iff)));
2715       mark_phi->init_req(1, mark);
2716 
2717       // Locked: Load prototype header from klass
2718       ctrl = _igvn.transform(new IfFalseNode(iff));
2719       Node* proto = MakeConX(0);
2720       for (uint i = FlatArrayCheckNode::ArrayOrKlass; i < check->req(); ++i) {
2721         Node* ary = check->in(i);
2722         // Make loads control dependent to make sure they are only executed if array is locked
2723         Node* klass_adr = basic_plus_adr(ary, oopDesc::klass_offset_in_bytes());
2724         Node* klass = _igvn.transform(LoadKlassNode::make(_igvn, ctrl, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
2725         Node* proto_adr = basic_plus_adr(klass, in_bytes(Klass::prototype_header_offset()));
2726         Node* proto_load = _igvn.transform(LoadNode::make(_igvn, ctrl, C->immutable_memory(), proto_adr, proto_adr->bottom_type()->is_ptr(), TypeX_X, TypeX_X->basic_type(), MemNode::unordered));
2727         proto = _igvn.transform(new OrXNode(proto, proto_load));
2728       }
2729       region->init_req(2, ctrl);
2730       mark_phi->init_req(2, proto);
2731 
2732       // Check if flat array bits are set
2733       Node* mask = MakeConX(markWord::flat_array_bit_in_place);
2734       Node* masked = _igvn.transform(new AndXNode(_igvn.transform(mark_phi), mask));
2735       cmp = _igvn.transform(new CmpXNode(masked, MakeConX(0)));
2736       Node* is_not_flat = _igvn.transform(new BoolNode(cmp, BoolTest::eq));
2737 
2738       ctrl = _igvn.transform(region);
2739       iff = _igvn.transform(new IfNode(ctrl, is_not_flat, PROB_MAX, COUNT_UNKNOWN))->as_If();
2740       _igvn.replace_node(old_iff, iff);
2741     }
2742     _igvn.replace_node(check, C->top());
2743   } else {
2744     // Fall back to layout helper check
2745     Node* lhs = intcon(0);
2746     for (uint i = FlatArrayCheckNode::ArrayOrKlass; i < check->req(); ++i) {
2747       Node* array_or_klass = check->in(i);
2748       Node* klass = NULL;
2749       const TypePtr* t = _igvn.type(array_or_klass)->is_ptr();
2750       assert(!t->is_flat() && !t->is_not_flat(), "Should have been optimized out");
2751       if (t->isa_oopptr() != NULL) {
2752         Node* klass_adr = basic_plus_adr(array_or_klass, oopDesc::klass_offset_in_bytes());
2753         klass = transform_later(LoadKlassNode::make(_igvn, NULL, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
2754       } else {
2755         assert(t->isa_aryklassptr(), "Unexpected input type");
2756         klass = array_or_klass;
2757       }
2758       Node* lh_addr = basic_plus_adr(klass, in_bytes(Klass::layout_helper_offset()));
2759       Node* lh_val = _igvn.transform(LoadNode::make(_igvn, NULL, C->immutable_memory(), lh_addr, lh_addr->bottom_type()->is_ptr(), TypeInt::INT, T_INT, MemNode::unordered));
2760       lhs = _igvn.transform(new OrINode(lhs, lh_val));
2761     }
2762     Node* masked = transform_later(new AndINode(lhs, intcon(Klass::_lh_array_tag_flat_value_bit_inplace)));
2763     Node* cmp = transform_later(new CmpINode(masked, intcon(0)));
2764     Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq));
2765     Node* old_bol = check->unique_out();
2766     _igvn.replace_node(old_bol, bol);
2767     _igvn.replace_node(check, C->top());
2768   }
2769 }
2770 
2771 //---------------------------eliminate_macro_nodes----------------------
2772 // Eliminate scalar replaced allocations and associated locks.
2773 void PhaseMacroExpand::eliminate_macro_nodes() {
2774   if (C->macro_count() == 0)
2775     return;
2776   NOT_PRODUCT(int membar_before = count_MemBar(C);)
2777 
2778   // Before elimination may re-mark (change to Nested or NonEscObj)
2779   // all associated (same box and obj) lock and unlock nodes.
2780   int cnt = C->macro_count();
2781   for (int i=0; i < cnt; i++) {
2782     Node *n = C->macro_node(i);
2783     if (n->is_AbstractLock()) { // Lock and Unlock nodes
2784       mark_eliminated_locking_nodes(n->as_AbstractLock());
2785     }
2786   }
2787   // Re-marking may break consistency of Coarsened locks.
2788   if (!C->coarsened_locks_consistent()) {
2789     return; // recompile without Coarsened locks if broken
2790   }

2811   }
2812   // Next, attempt to eliminate allocations
2813   _has_locks = false;
2814   progress = true;
2815   while (progress) {
2816     progress = false;
2817     for (int i = C->macro_count(); i > 0; i = MIN2(i - 1, C->macro_count())) { // more than 1 element can be eliminated at once
2818       Node* n = C->macro_node(i - 1);
2819       bool success = false;
2820       DEBUG_ONLY(int old_macro_count = C->macro_count();)
2821       switch (n->class_id()) {
2822       case Node::Class_Allocate:
2823       case Node::Class_AllocateArray:
2824         success = eliminate_allocate_node(n->as_Allocate());
2825 #ifndef PRODUCT
2826         if (success && PrintOptoStatistics) {
2827           Atomic::inc(&PhaseMacroExpand::_objs_scalar_replaced_counter);
2828         }
2829 #endif
2830         break;
2831       case Node::Class_CallStaticJava: {
2832         CallStaticJavaNode* call = n->as_CallStaticJava();
2833         if (!call->method()->is_method_handle_intrinsic()) {
2834           success = eliminate_boxing_node(n->as_CallStaticJava());
2835         }
2836         break;
2837       }
2838       case Node::Class_Lock:
2839       case Node::Class_Unlock:
2840         assert(!n->as_AbstractLock()->is_eliminated(), "sanity");
2841         _has_locks = true;
2842         break;
2843       case Node::Class_ArrayCopy:
2844         break;
2845       case Node::Class_OuterStripMinedLoop:
2846         break;
2847       case Node::Class_SubTypeCheck:
2848         break;
2849       case Node::Class_Opaque1:
2850         break;
2851       case Node::Class_FlatArrayCheck:
2852         break;
2853       default:
2854         assert(n->Opcode() == Op_LoopLimit ||
2855                n->Opcode() == Op_Opaque2   ||
2856                n->Opcode() == Op_Opaque3   ||
2857                n->Opcode() == Op_Opaque4   ||
2858                BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(n),
2859                "unknown node type in macro list");
2860       }
2861       assert(success == (C->macro_count() < old_macro_count), "elimination reduces macro count");
2862       progress = progress || success;
2863     }
2864   }
2865 #ifndef PRODUCT
2866   if (PrintOptoStatistics) {
2867     int membar_after = count_MemBar(C);
2868     Atomic::add(&PhaseMacroExpand::_memory_barriers_removed_counter, membar_before - membar_after);
2869   }
2870 #endif
2871 }
2872 

2874 //  Returns true if a failure occurred.
2875 bool PhaseMacroExpand::expand_macro_nodes() {
2876   // Last attempt to eliminate macro nodes.
2877   eliminate_macro_nodes();
2878   if (C->failing())  return true;
2879 
2880   // Eliminate Opaque and LoopLimit nodes. Do it after all loop optimizations.
2881   bool progress = true;
2882   while (progress) {
2883     progress = false;
2884     for (int i = C->macro_count(); i > 0; i--) {
2885       Node* n = C->macro_node(i-1);
2886       bool success = false;
2887       DEBUG_ONLY(int old_macro_count = C->macro_count();)
2888       if (n->Opcode() == Op_LoopLimit) {
2889         // Remove it from macro list and put on IGVN worklist to optimize.
2890         C->remove_macro_node(n);
2891         _igvn._worklist.push(n);
2892         success = true;
2893       } else if (n->Opcode() == Op_CallStaticJava) {
2894         CallStaticJavaNode* call = n->as_CallStaticJava();
2895         if (!call->method()->is_method_handle_intrinsic()) {
2896           // Remove it from macro list and put on IGVN worklist to optimize.
2897           C->remove_macro_node(n);
2898           _igvn._worklist.push(n);
2899           success = true;
2900         }
2901       } else if (n->is_Opaque1() || n->Opcode() == Op_Opaque2) {
2902         _igvn.replace_node(n, n->in(1));
2903         success = true;
2904 #if INCLUDE_RTM_OPT
2905       } else if ((n->Opcode() == Op_Opaque3) && ((Opaque3Node*)n)->rtm_opt()) {
2906         assert(C->profile_rtm(), "should be used only in rtm deoptimization code");
2907         assert((n->outcnt() == 1) && n->unique_out()->is_Cmp(), "");
2908         Node* cmp = n->unique_out();
2909 #ifdef ASSERT
2910         // Validate graph.
2911         assert((cmp->outcnt() == 1) && cmp->unique_out()->is_Bool(), "");
2912         BoolNode* bol = cmp->unique_out()->as_Bool();
2913         assert((bol->outcnt() == 1) && bol->unique_out()->is_If() &&
2914                (bol->_test._test == BoolTest::ne), "");
2915         IfNode* ifn = bol->unique_out()->as_If();
2916         assert((ifn->outcnt() == 2) &&
2917                ifn->proj_out(1)->is_uncommon_trap_proj(Deoptimization::Reason_rtm_state_change) != NULL, "");
2918 #endif
2919         Node* repl = n->in(1);
2920         if (!_has_locks) {

2983     // Worst case is a macro node gets expanded into about 200 nodes.
2984     // Allow 50% more for optimization.
2985     if (C->check_node_count(300, "out of nodes before macro expansion")) {
2986       return true;
2987     }
2988 
2989     DEBUG_ONLY(int old_macro_count = C->macro_count();)
2990     switch (n->class_id()) {
2991     case Node::Class_Lock:
2992       expand_lock_node(n->as_Lock());
2993       break;
2994     case Node::Class_Unlock:
2995       expand_unlock_node(n->as_Unlock());
2996       break;
2997     case Node::Class_ArrayCopy:
2998       expand_arraycopy_node(n->as_ArrayCopy());
2999       break;
3000     case Node::Class_SubTypeCheck:
3001       expand_subtypecheck_node(n->as_SubTypeCheck());
3002       break;
3003     case Node::Class_CallStaticJava:
3004       expand_mh_intrinsic_return(n->as_CallStaticJava());
3005       C->remove_macro_node(n);
3006       break;
3007     case Node::Class_FlatArrayCheck:
3008       expand_flatarraycheck_node(n->as_FlatArrayCheck());
3009       break;
3010     default:
3011       assert(false, "unknown node type in macro list");
3012     }
3013     assert(C->macro_count() == (old_macro_count - 1), "expansion must have deleted one node from macro list");
3014     if (C->failing())  return true;
3015 
3016     // Clean up the graph so we're less likely to hit the maximum node
3017     // limit
3018     _igvn.set_delay_transform(false);
3019     _igvn.optimize();
3020     if (C->failing())  return true;
3021     _igvn.set_delay_transform(true);
3022   }
3023 
3024   // All nodes except Allocate nodes are expanded now. There could be
3025   // new optimization opportunities (such as folding newly created
3026   // load from a just allocated object). Run IGVN.
3027 
3028   // expand "macro" nodes
3029   // nodes are removed from the macro list as they are processed
< prev index next >