1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact 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 "classfile/systemDictionary.hpp"
  27 #include "compiler/compileLog.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/cfgnode.hpp"
  32 #include "opto/compile.hpp"
  33 #include "opto/connode.hpp"
  34 #include "opto/convertnode.hpp"
  35 #include "opto/loopnode.hpp"
  36 #include "opto/machnode.hpp"
  37 #include "opto/matcher.hpp"
  38 #include "opto/memnode.hpp"
  39 #include "opto/mulnode.hpp"
  40 #include "opto/narrowptrnode.hpp"
  41 #include "opto/phaseX.hpp"
  42 #include "opto/regmask.hpp"
  43 #include "utilities/copy.hpp"
  44 
  45 // Portions of code courtesy of Clifford Click
  46 
  47 // Optimization - Graph Style
  48 
  49 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem,  const TypePtr *tp, const TypePtr *adr_check, outputStream *st);
  50 
  51 //=============================================================================
  52 uint MemNode::size_of() const { return sizeof(*this); }
  53 
  54 const TypePtr *MemNode::adr_type() const {
  55   Node* adr = in(Address);
  56   if (adr == NULL)  return NULL; // node is dead
  57   const TypePtr* cross_check = NULL;
  58   DEBUG_ONLY(cross_check = _adr_type);
  59   return calculate_adr_type(adr->bottom_type(), cross_check);
  60 }
  61 
  62 #ifndef PRODUCT
  63 void MemNode::dump_spec(outputStream *st) const {
  64   if (in(Address) == NULL)  return; // node is dead
  65 #ifndef ASSERT
  66   // fake the missing field
  67   const TypePtr* _adr_type = NULL;
  68   if (in(Address) != NULL)
  69     _adr_type = in(Address)->bottom_type()->isa_ptr();
  70 #endif
  71   dump_adr_type(this, _adr_type, st);
  72 
  73   Compile* C = Compile::current();
  74   if( C->alias_type(_adr_type)->is_volatile() )
  75     st->print(" Volatile!");
  76 }
  77 
  78 void MemNode::dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st) {
  79   st->print(" @");
  80   if (adr_type == NULL) {
  81     st->print("NULL");
  82   } else {
  83     adr_type->dump_on(st);
  84     Compile* C = Compile::current();
  85     Compile::AliasType* atp = NULL;
  86     if (C->have_alias_type(adr_type))  atp = C->alias_type(adr_type);
  87     if (atp == NULL)
  88       st->print(", idx=?\?;");
  89     else if (atp->index() == Compile::AliasIdxBot)
  90       st->print(", idx=Bot;");
  91     else if (atp->index() == Compile::AliasIdxTop)
  92       st->print(", idx=Top;");
  93     else if (atp->index() == Compile::AliasIdxRaw)
  94       st->print(", idx=Raw;");
  95     else {
  96       ciField* field = atp->field();
  97       if (field) {
  98         st->print(", name=");
  99         field->print_name_on(st);
 100       }
 101       st->print(", idx=%d;", atp->index());
 102     }
 103   }
 104 }
 105 
 106 extern void print_alias_types();
 107 
 108 #endif
 109 
 110 Node *MemNode::optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase) {
 111   assert((t_oop != NULL), "sanity");
 112   bool is_instance = t_oop->is_known_instance_field();
 113   bool is_boxed_value_load = t_oop->is_ptr_to_boxed_value() &&
 114                              (load != NULL) && load->is_Load() &&
 115                              (phase->is_IterGVN() != NULL);
 116   if (!(is_instance || is_boxed_value_load))
 117     return mchain;  // don't try to optimize non-instance types
 118   uint instance_id = t_oop->instance_id();
 119   Node *start_mem = phase->C->start()->proj_out(TypeFunc::Memory);
 120   Node *prev = NULL;
 121   Node *result = mchain;
 122   while (prev != result) {
 123     prev = result;
 124     if (result == start_mem)
 125       break;  // hit one of our sentinels
 126     // skip over a call which does not affect this memory slice
 127     if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
 128       Node *proj_in = result->in(0);
 129       if (proj_in->is_Allocate() && proj_in->_idx == instance_id) {
 130         break;  // hit one of our sentinels
 131       } else if (proj_in->is_Call()) {
 132         CallNode *call = proj_in->as_Call();
 133         if (!call->may_modify(t_oop, phase)) { // returns false for instances
 134           result = call->in(TypeFunc::Memory);
 135         }
 136       } else if (proj_in->is_Initialize()) {
 137         AllocateNode* alloc = proj_in->as_Initialize()->allocation();
 138         // Stop if this is the initialization for the object instance which
 139         // which contains this memory slice, otherwise skip over it.
 140         if ((alloc == NULL) || (alloc->_idx == instance_id)) {
 141           break;
 142         }
 143         if (is_instance) {
 144           result = proj_in->in(TypeFunc::Memory);
 145         } else if (is_boxed_value_load) {
 146           Node* klass = alloc->in(AllocateNode::KlassNode);
 147           const TypeKlassPtr* tklass = phase->type(klass)->is_klassptr();
 148           if (tklass->klass_is_exact() && !tklass->klass()->equals(t_oop->klass())) {
 149             result = proj_in->in(TypeFunc::Memory); // not related allocation
 150           }
 151         }
 152       } else if (proj_in->is_MemBar()) {
 153         result = proj_in->in(TypeFunc::Memory);
 154       } else {
 155         assert(false, "unexpected projection");
 156       }
 157     } else if (result->is_ClearArray()) {
 158       if (!is_instance || !ClearArrayNode::step_through(&result, instance_id, phase)) {
 159         // Can not bypass initialization of the instance
 160         // we are looking for.
 161         break;
 162       }
 163       // Otherwise skip it (the call updated 'result' value).
 164     } else if (result->is_MergeMem()) {
 165       result = step_through_mergemem(phase, result->as_MergeMem(), t_oop, NULL, tty);
 166     }
 167   }
 168   return result;
 169 }
 170 
 171 Node *MemNode::optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase) {
 172   const TypeOopPtr* t_oop = t_adr->isa_oopptr();
 173   if (t_oop == NULL)
 174     return mchain;  // don't try to optimize non-oop types
 175   Node* result = optimize_simple_memory_chain(mchain, t_oop, load, phase);
 176   bool is_instance = t_oop->is_known_instance_field();
 177   PhaseIterGVN *igvn = phase->is_IterGVN();
 178   if (is_instance && igvn != NULL  && result->is_Phi()) {
 179     PhiNode *mphi = result->as_Phi();
 180     assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
 181     const TypePtr *t = mphi->adr_type();
 182     if (t == TypePtr::BOTTOM || t == TypeRawPtr::BOTTOM ||
 183         t->isa_oopptr() && !t->is_oopptr()->is_known_instance() &&
 184         t->is_oopptr()->cast_to_exactness(true)
 185          ->is_oopptr()->cast_to_ptr_type(t_oop->ptr())
 186          ->is_oopptr()->cast_to_instance_id(t_oop->instance_id()) == t_oop) {
 187       // clone the Phi with our address type
 188       result = mphi->split_out_instance(t_adr, igvn);
 189     } else {
 190       assert(phase->C->get_alias_index(t) == phase->C->get_alias_index(t_adr), "correct memory chain");
 191     }
 192   }
 193   return result;
 194 }
 195 
 196 static Node *step_through_mergemem(PhaseGVN *phase, MergeMemNode *mmem,  const TypePtr *tp, const TypePtr *adr_check, outputStream *st) {
 197   uint alias_idx = phase->C->get_alias_index(tp);
 198   Node *mem = mmem;
 199 #ifdef ASSERT
 200   {
 201     // Check that current type is consistent with the alias index used during graph construction
 202     assert(alias_idx >= Compile::AliasIdxRaw, "must not be a bad alias_idx");
 203     bool consistent =  adr_check == NULL || adr_check->empty() ||
 204                        phase->C->must_alias(adr_check, alias_idx );
 205     // Sometimes dead array references collapse to a[-1], a[-2], or a[-3]
 206     if( !consistent && adr_check != NULL && !adr_check->empty() &&
 207                tp->isa_aryptr() &&        tp->offset() == Type::OffsetBot &&
 208         adr_check->isa_aryptr() && adr_check->offset() != Type::OffsetBot &&
 209         ( adr_check->offset() == arrayOopDesc::length_offset_in_bytes() ||
 210           adr_check->offset() == oopDesc::klass_offset_in_bytes() ||
 211           adr_check->offset() == oopDesc::mark_offset_in_bytes() ) ) {
 212       // don't assert if it is dead code.
 213       consistent = true;
 214     }
 215     if( !consistent ) {
 216       st->print("alias_idx==%d, adr_check==", alias_idx);
 217       if( adr_check == NULL ) {
 218         st->print("NULL");
 219       } else {
 220         adr_check->dump();
 221       }
 222       st->cr();
 223       print_alias_types();
 224       assert(consistent, "adr_check must match alias idx");
 225     }
 226   }
 227 #endif
 228   // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
 229   // means an array I have not precisely typed yet.  Do not do any
 230   // alias stuff with it any time soon.
 231   const TypeOopPtr *toop = tp->isa_oopptr();
 232   if( tp->base() != Type::AnyPtr &&
 233       !(toop &&
 234         toop->klass() != NULL &&
 235         toop->klass()->is_java_lang_Object() &&
 236         toop->offset() == Type::OffsetBot) ) {
 237     // compress paths and change unreachable cycles to TOP
 238     // If not, we can update the input infinitely along a MergeMem cycle
 239     // Equivalent code in PhiNode::Ideal
 240     Node* m  = phase->transform(mmem);
 241     // If transformed to a MergeMem, get the desired slice
 242     // Otherwise the returned node represents memory for every slice
 243     mem = (m->is_MergeMem())? m->as_MergeMem()->memory_at(alias_idx) : m;
 244     // Update input if it is progress over what we have now
 245   }
 246   return mem;
 247 }
 248 
 249 //--------------------------Ideal_common---------------------------------------
 250 // Look for degenerate control and memory inputs.  Bypass MergeMem inputs.
 251 // Unhook non-raw memories from complete (macro-expanded) initializations.
 252 Node *MemNode::Ideal_common(PhaseGVN *phase, bool can_reshape) {
 253   // If our control input is a dead region, kill all below the region
 254   Node *ctl = in(MemNode::Control);
 255   if (ctl && remove_dead_region(phase, can_reshape))
 256     return this;
 257   ctl = in(MemNode::Control);
 258   // Don't bother trying to transform a dead node
 259   if (ctl && ctl->is_top())  return NodeSentinel;
 260 
 261   PhaseIterGVN *igvn = phase->is_IterGVN();
 262   // Wait if control on the worklist.
 263   if (ctl && can_reshape && igvn != NULL) {
 264     Node* bol = NULL;
 265     Node* cmp = NULL;
 266     if (ctl->in(0)->is_If()) {
 267       assert(ctl->is_IfTrue() || ctl->is_IfFalse(), "sanity");
 268       bol = ctl->in(0)->in(1);
 269       if (bol->is_Bool())
 270         cmp = ctl->in(0)->in(1)->in(1);
 271     }
 272     if (igvn->_worklist.member(ctl) ||
 273         (bol != NULL && igvn->_worklist.member(bol)) ||
 274         (cmp != NULL && igvn->_worklist.member(cmp)) ) {
 275       // This control path may be dead.
 276       // Delay this memory node transformation until the control is processed.
 277       phase->is_IterGVN()->_worklist.push(this);
 278       return NodeSentinel; // caller will return NULL
 279     }
 280   }
 281   // Ignore if memory is dead, or self-loop
 282   Node *mem = in(MemNode::Memory);
 283   if (phase->type( mem ) == Type::TOP) return NodeSentinel; // caller will return NULL
 284   assert(mem != this, "dead loop in MemNode::Ideal");
 285 
 286   if (can_reshape && igvn != NULL && igvn->_worklist.member(mem)) {
 287     // This memory slice may be dead.
 288     // Delay this mem node transformation until the memory is processed.
 289     phase->is_IterGVN()->_worklist.push(this);
 290     return NodeSentinel; // caller will return NULL
 291   }
 292 
 293   Node *address = in(MemNode::Address);
 294   const Type *t_adr = phase->type(address);
 295   if (t_adr == Type::TOP)              return NodeSentinel; // caller will return NULL
 296 
 297   if (can_reshape && igvn != NULL &&
 298       (igvn->_worklist.member(address) ||
 299        igvn->_worklist.size() > 0 && (t_adr != adr_type())) ) {
 300     // The address's base and type may change when the address is processed.
 301     // Delay this mem node transformation until the address is processed.
 302     phase->is_IterGVN()->_worklist.push(this);
 303     return NodeSentinel; // caller will return NULL
 304   }
 305 
 306   // Do NOT remove or optimize the next lines: ensure a new alias index
 307   // is allocated for an oop pointer type before Escape Analysis.
 308   // Note: C++ will not remove it since the call has side effect.
 309   if (t_adr->isa_oopptr()) {
 310     int alias_idx = phase->C->get_alias_index(t_adr->is_ptr());
 311   }
 312 
 313   Node* base = NULL;
 314   if (address->is_AddP()) {
 315     base = address->in(AddPNode::Base);
 316   }
 317   if (base != NULL && phase->type(base)->higher_equal(TypePtr::NULL_PTR) &&
 318       !t_adr->isa_rawptr()) {
 319     // Note: raw address has TOP base and top->higher_equal(TypePtr::NULL_PTR) is true.
 320     // Skip this node optimization if its address has TOP base.
 321     return NodeSentinel; // caller will return NULL
 322   }
 323 
 324   // Avoid independent memory operations
 325   Node* old_mem = mem;
 326 
 327   // The code which unhooks non-raw memories from complete (macro-expanded)
 328   // initializations was removed. After macro-expansion all stores catched
 329   // by Initialize node became raw stores and there is no information
 330   // which memory slices they modify. So it is unsafe to move any memory
 331   // operation above these stores. Also in most cases hooked non-raw memories
 332   // were already unhooked by using information from detect_ptr_independence()
 333   // and find_previous_store().
 334 
 335   if (mem->is_MergeMem()) {
 336     MergeMemNode* mmem = mem->as_MergeMem();
 337     const TypePtr *tp = t_adr->is_ptr();
 338 
 339     mem = step_through_mergemem(phase, mmem, tp, adr_type(), tty);
 340   }
 341 
 342   if (mem != old_mem) {
 343     set_req(MemNode::Memory, mem);
 344     if (can_reshape && old_mem->outcnt() == 0) {
 345         igvn->_worklist.push(old_mem);
 346     }
 347     if (phase->type( mem ) == Type::TOP) return NodeSentinel;
 348     return this;
 349   }
 350 
 351   // let the subclass continue analyzing...
 352   return NULL;
 353 }
 354 
 355 // Helper function for proving some simple control dominations.
 356 // Attempt to prove that all control inputs of 'dom' dominate 'sub'.
 357 // Already assumes that 'dom' is available at 'sub', and that 'sub'
 358 // is not a constant (dominated by the method's StartNode).
 359 // Used by MemNode::find_previous_store to prove that the
 360 // control input of a memory operation predates (dominates)
 361 // an allocation it wants to look past.
 362 bool MemNode::all_controls_dominate(Node* dom, Node* sub) {
 363   if (dom == NULL || dom->is_top() || sub == NULL || sub->is_top())
 364     return false; // Conservative answer for dead code
 365 
 366   // Check 'dom'. Skip Proj and CatchProj nodes.
 367   dom = dom->find_exact_control(dom);
 368   if (dom == NULL || dom->is_top())
 369     return false; // Conservative answer for dead code
 370 
 371   if (dom == sub) {
 372     // For the case when, for example, 'sub' is Initialize and the original
 373     // 'dom' is Proj node of the 'sub'.
 374     return false;
 375   }
 376 
 377   if (dom->is_Con() || dom->is_Start() || dom->is_Root() || dom == sub)
 378     return true;
 379 
 380   // 'dom' dominates 'sub' if its control edge and control edges
 381   // of all its inputs dominate or equal to sub's control edge.
 382 
 383   // Currently 'sub' is either Allocate, Initialize or Start nodes.
 384   // Or Region for the check in LoadNode::Ideal();
 385   // 'sub' should have sub->in(0) != NULL.
 386   assert(sub->is_Allocate() || sub->is_Initialize() || sub->is_Start() ||
 387          sub->is_Region() || sub->is_Call(), "expecting only these nodes");
 388 
 389   // Get control edge of 'sub'.
 390   Node* orig_sub = sub;
 391   sub = sub->find_exact_control(sub->in(0));
 392   if (sub == NULL || sub->is_top())
 393     return false; // Conservative answer for dead code
 394 
 395   assert(sub->is_CFG(), "expecting control");
 396 
 397   if (sub == dom)
 398     return true;
 399 
 400   if (sub->is_Start() || sub->is_Root())
 401     return false;
 402 
 403   {
 404     // Check all control edges of 'dom'.
 405 
 406     ResourceMark rm;
 407     Arena* arena = Thread::current()->resource_area();
 408     Node_List nlist(arena);
 409     Unique_Node_List dom_list(arena);
 410 
 411     dom_list.push(dom);
 412     bool only_dominating_controls = false;
 413 
 414     for (uint next = 0; next < dom_list.size(); next++) {
 415       Node* n = dom_list.at(next);
 416       if (n == orig_sub)
 417         return false; // One of dom's inputs dominated by sub.
 418       if (!n->is_CFG() && n->pinned()) {
 419         // Check only own control edge for pinned non-control nodes.
 420         n = n->find_exact_control(n->in(0));
 421         if (n == NULL || n->is_top())
 422           return false; // Conservative answer for dead code
 423         assert(n->is_CFG(), "expecting control");
 424         dom_list.push(n);
 425       } else if (n->is_Con() || n->is_Start() || n->is_Root()) {
 426         only_dominating_controls = true;
 427       } else if (n->is_CFG()) {
 428         if (n->dominates(sub, nlist))
 429           only_dominating_controls = true;
 430         else
 431           return false;
 432       } else {
 433         // First, own control edge.
 434         Node* m = n->find_exact_control(n->in(0));
 435         if (m != NULL) {
 436           if (m->is_top())
 437             return false; // Conservative answer for dead code
 438           dom_list.push(m);
 439         }
 440         // Now, the rest of edges.
 441         uint cnt = n->req();
 442         for (uint i = 1; i < cnt; i++) {
 443           m = n->find_exact_control(n->in(i));
 444           if (m == NULL || m->is_top())
 445             continue;
 446           dom_list.push(m);
 447         }
 448       }
 449     }
 450     return only_dominating_controls;
 451   }
 452 }
 453 
 454 //---------------------detect_ptr_independence---------------------------------
 455 // Used by MemNode::find_previous_store to prove that two base
 456 // pointers are never equal.
 457 // The pointers are accompanied by their associated allocations,
 458 // if any, which have been previously discovered by the caller.
 459 bool MemNode::detect_ptr_independence(Node* p1, AllocateNode* a1,
 460                                       Node* p2, AllocateNode* a2,
 461                                       PhaseTransform* phase) {
 462   // Attempt to prove that these two pointers cannot be aliased.
 463   // They may both manifestly be allocations, and they should differ.
 464   // Or, if they are not both allocations, they can be distinct constants.
 465   // Otherwise, one is an allocation and the other a pre-existing value.
 466   if (a1 == NULL && a2 == NULL) {           // neither an allocation
 467     return (p1 != p2) && p1->is_Con() && p2->is_Con();
 468   } else if (a1 != NULL && a2 != NULL) {    // both allocations
 469     return (a1 != a2);
 470   } else if (a1 != NULL) {                  // one allocation a1
 471     // (Note:  p2->is_Con implies p2->in(0)->is_Root, which dominates.)
 472     return all_controls_dominate(p2, a1);
 473   } else { //(a2 != NULL)                   // one allocation a2
 474     return all_controls_dominate(p1, a2);
 475   }
 476   return false;
 477 }
 478 
 479 
 480 // The logic for reordering loads and stores uses four steps:
 481 // (a) Walk carefully past stores and initializations which we
 482 //     can prove are independent of this load.
 483 // (b) Observe that the next memory state makes an exact match
 484 //     with self (load or store), and locate the relevant store.
 485 // (c) Ensure that, if we were to wire self directly to the store,
 486 //     the optimizer would fold it up somehow.
 487 // (d) Do the rewiring, and return, depending on some other part of
 488 //     the optimizer to fold up the load.
 489 // This routine handles steps (a) and (b).  Steps (c) and (d) are
 490 // specific to loads and stores, so they are handled by the callers.
 491 // (Currently, only LoadNode::Ideal has steps (c), (d).  More later.)
 492 //
 493 Node* MemNode::find_previous_store(PhaseTransform* phase) {
 494   Node*         ctrl   = in(MemNode::Control);
 495   Node*         adr    = in(MemNode::Address);
 496   intptr_t      offset = 0;
 497   Node*         base   = AddPNode::Ideal_base_and_offset(adr, phase, offset);
 498   AllocateNode* alloc  = AllocateNode::Ideal_allocation(base, phase);
 499 
 500   if (offset == Type::OffsetBot)
 501     return NULL;            // cannot unalias unless there are precise offsets
 502 
 503   const TypeOopPtr *addr_t = adr->bottom_type()->isa_oopptr();
 504 
 505   intptr_t size_in_bytes = memory_size();
 506 
 507   Node* mem = in(MemNode::Memory);   // start searching here...
 508 
 509   int cnt = 50;             // Cycle limiter
 510   for (;;) {                // While we can dance past unrelated stores...
 511     if (--cnt < 0)  break;  // Caught in cycle or a complicated dance?
 512 
 513     if (mem->is_Store()) {
 514       Node* st_adr = mem->in(MemNode::Address);
 515       intptr_t st_offset = 0;
 516       Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
 517       if (st_base == NULL)
 518         break;              // inscrutable pointer
 519       if (st_offset != offset && st_offset != Type::OffsetBot) {
 520         const int MAX_STORE = BytesPerLong;
 521         if (st_offset >= offset + size_in_bytes ||
 522             st_offset <= offset - MAX_STORE ||
 523             st_offset <= offset - mem->as_Store()->memory_size()) {
 524           // Success:  The offsets are provably independent.
 525           // (You may ask, why not just test st_offset != offset and be done?
 526           // The answer is that stores of different sizes can co-exist
 527           // in the same sequence of RawMem effects.  We sometimes initialize
 528           // a whole 'tile' of array elements with a single jint or jlong.)
 529           mem = mem->in(MemNode::Memory);
 530           continue;           // (a) advance through independent store memory
 531         }
 532       }
 533       if (st_base != base &&
 534           detect_ptr_independence(base, alloc,
 535                                   st_base,
 536                                   AllocateNode::Ideal_allocation(st_base, phase),
 537                                   phase)) {
 538         // Success:  The bases are provably independent.
 539         mem = mem->in(MemNode::Memory);
 540         continue;           // (a) advance through independent store memory
 541       }
 542 
 543       // (b) At this point, if the bases or offsets do not agree, we lose,
 544       // since we have not managed to prove 'this' and 'mem' independent.
 545       if (st_base == base && st_offset == offset) {
 546         return mem;         // let caller handle steps (c), (d)
 547       }
 548 
 549     } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
 550       InitializeNode* st_init = mem->in(0)->as_Initialize();
 551       AllocateNode*  st_alloc = st_init->allocation();
 552       if (st_alloc == NULL)
 553         break;              // something degenerated
 554       bool known_identical = false;
 555       bool known_independent = false;
 556       if (alloc == st_alloc)
 557         known_identical = true;
 558       else if (alloc != NULL)
 559         known_independent = true;
 560       else if (all_controls_dominate(this, st_alloc))
 561         known_independent = true;
 562 
 563       if (known_independent) {
 564         // The bases are provably independent: Either they are
 565         // manifestly distinct allocations, or else the control
 566         // of this load dominates the store's allocation.
 567         int alias_idx = phase->C->get_alias_index(adr_type());
 568         if (alias_idx == Compile::AliasIdxRaw) {
 569           mem = st_alloc->in(TypeFunc::Memory);
 570         } else {
 571           mem = st_init->memory(alias_idx);
 572         }
 573         continue;           // (a) advance through independent store memory
 574       }
 575 
 576       // (b) at this point, if we are not looking at a store initializing
 577       // the same allocation we are loading from, we lose.
 578       if (known_identical) {
 579         // From caller, can_see_stored_value will consult find_captured_store.
 580         return mem;         // let caller handle steps (c), (d)
 581       }
 582 
 583     } else if (addr_t != NULL && addr_t->is_known_instance_field()) {
 584       // Can't use optimize_simple_memory_chain() since it needs PhaseGVN.
 585       if (mem->is_Proj() && mem->in(0)->is_Call()) {
 586         CallNode *call = mem->in(0)->as_Call();
 587         if (!call->may_modify(addr_t, phase)) {
 588           mem = call->in(TypeFunc::Memory);
 589           continue;         // (a) advance through independent call memory
 590         }
 591       } else if (mem->is_Proj() && mem->in(0)->is_MemBar()) {
 592         mem = mem->in(0)->in(TypeFunc::Memory);
 593         continue;           // (a) advance through independent MemBar memory
 594       } else if (mem->is_ClearArray()) {
 595         if (ClearArrayNode::step_through(&mem, (uint)addr_t->instance_id(), phase)) {
 596           // (the call updated 'mem' value)
 597           continue;         // (a) advance through independent allocation memory
 598         } else {
 599           // Can not bypass initialization of the instance
 600           // we are looking for.
 601           return mem;
 602         }
 603       } else if (mem->is_MergeMem()) {
 604         int alias_idx = phase->C->get_alias_index(adr_type());
 605         mem = mem->as_MergeMem()->memory_at(alias_idx);
 606         continue;           // (a) advance through independent MergeMem memory
 607       }
 608     }
 609 
 610     // Unless there is an explicit 'continue', we must bail out here,
 611     // because 'mem' is an inscrutable memory state (e.g., a call).
 612     break;
 613   }
 614 
 615   return NULL;              // bail out
 616 }
 617 
 618 //----------------------calculate_adr_type-------------------------------------
 619 // Helper function.  Notices when the given type of address hits top or bottom.
 620 // Also, asserts a cross-check of the type against the expected address type.
 621 const TypePtr* MemNode::calculate_adr_type(const Type* t, const TypePtr* cross_check) {
 622   if (t == Type::TOP)  return NULL; // does not touch memory any more?
 623   #ifdef PRODUCT
 624   cross_check = NULL;
 625   #else
 626   if (!VerifyAliases || is_error_reported() || Node::in_dump())  cross_check = NULL;
 627   #endif
 628   const TypePtr* tp = t->isa_ptr();
 629   if (tp == NULL) {
 630     assert(cross_check == NULL || cross_check == TypePtr::BOTTOM, "expected memory type must be wide");
 631     return TypePtr::BOTTOM;           // touches lots of memory
 632   } else {
 633     #ifdef ASSERT
 634     // %%%% [phh] We don't check the alias index if cross_check is
 635     //            TypeRawPtr::BOTTOM.  Needs to be investigated.
 636     if (cross_check != NULL &&
 637         cross_check != TypePtr::BOTTOM &&
 638         cross_check != TypeRawPtr::BOTTOM) {
 639       // Recheck the alias index, to see if it has changed (due to a bug).
 640       Compile* C = Compile::current();
 641       assert(C->get_alias_index(cross_check) == C->get_alias_index(tp),
 642              "must stay in the original alias category");
 643       // The type of the address must be contained in the adr_type,
 644       // disregarding "null"-ness.
 645       // (We make an exception for TypeRawPtr::BOTTOM, which is a bit bucket.)
 646       const TypePtr* tp_notnull = tp->join(TypePtr::NOTNULL)->is_ptr();
 647       assert(cross_check->meet(tp_notnull) == cross_check->remove_speculative(),
 648              "real address must not escape from expected memory type");
 649     }
 650     #endif
 651     return tp;
 652   }
 653 }
 654 
 655 //=============================================================================
 656 // Should LoadNode::Ideal() attempt to remove control edges?
 657 bool LoadNode::can_remove_control() const {
 658   return true;
 659 }
 660 uint LoadNode::size_of() const { return sizeof(*this); }
 661 uint LoadNode::cmp( const Node &n ) const
 662 { return !Type::cmp( _type, ((LoadNode&)n)._type ); }
 663 const Type *LoadNode::bottom_type() const { return _type; }
 664 uint LoadNode::ideal_reg() const {
 665   return _type->ideal_reg();
 666 }
 667 
 668 #ifndef PRODUCT
 669 void LoadNode::dump_spec(outputStream *st) const {
 670   MemNode::dump_spec(st);
 671   if( !Verbose && !WizardMode ) {
 672     // standard dump does this in Verbose and WizardMode
 673     st->print(" #"); _type->dump_on(st);
 674   }
 675 }
 676 #endif
 677 
 678 #ifdef ASSERT
 679 //----------------------------is_immutable_value-------------------------------
 680 // Helper function to allow a raw load without control edge for some cases
 681 bool LoadNode::is_immutable_value(Node* adr) {
 682   return (adr->is_AddP() && adr->in(AddPNode::Base)->is_top() &&
 683           adr->in(AddPNode::Address)->Opcode() == Op_ThreadLocal &&
 684           (adr->in(AddPNode::Offset)->find_intptr_t_con(-1) ==
 685            in_bytes(JavaThread::osthread_offset())));
 686 }
 687 #endif
 688 
 689 //----------------------------LoadNode::make-----------------------------------
 690 // Polymorphic factory method:
 691 Node *LoadNode::make(PhaseGVN& gvn, Node *ctl, Node *mem, Node *adr, const TypePtr* adr_type, const Type *rt, BasicType bt, MemOrd mo) {
 692   Compile* C = gvn.C;
 693 
 694   // sanity check the alias category against the created node type
 695   assert(!(adr_type->isa_oopptr() &&
 696            adr_type->offset() == oopDesc::klass_offset_in_bytes()),
 697          "use LoadKlassNode instead");
 698   assert(!(adr_type->isa_aryptr() &&
 699            adr_type->offset() == arrayOopDesc::length_offset_in_bytes()),
 700          "use LoadRangeNode instead");
 701   // Check control edge of raw loads
 702   assert( ctl != NULL || C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
 703           // oop will be recorded in oop map if load crosses safepoint
 704           rt->isa_oopptr() || is_immutable_value(adr),
 705           "raw memory operations should have control edge");
 706   switch (bt) {
 707   case T_BOOLEAN: return new LoadUBNode(ctl, mem, adr, adr_type, rt->is_int(),  mo);
 708   case T_BYTE:    return new LoadBNode (ctl, mem, adr, adr_type, rt->is_int(),  mo);
 709   case T_INT:     return new LoadINode (ctl, mem, adr, adr_type, rt->is_int(),  mo);
 710   case T_CHAR:    return new LoadUSNode(ctl, mem, adr, adr_type, rt->is_int(),  mo);
 711   case T_SHORT:   return new LoadSNode (ctl, mem, adr, adr_type, rt->is_int(),  mo);
 712   case T_LONG:    return new LoadLNode (ctl, mem, adr, adr_type, rt->is_long(), mo);
 713   case T_FLOAT:   return new LoadFNode (ctl, mem, adr, adr_type, rt,            mo);
 714   case T_DOUBLE:  return new LoadDNode (ctl, mem, adr, adr_type, rt,            mo);
 715   case T_ADDRESS: return new LoadPNode (ctl, mem, adr, adr_type, rt->is_ptr(),  mo);
 716   case T_OBJECT:
 717 #ifdef _LP64
 718     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
 719       Node* load  = gvn.transform(new LoadNNode(ctl, mem, adr, adr_type, rt->make_narrowoop(), mo));
 720       return new DecodeNNode(load, load->bottom_type()->make_ptr());
 721     } else
 722 #endif
 723     {
 724       assert(!adr->bottom_type()->is_ptr_to_narrowoop() && !adr->bottom_type()->is_ptr_to_narrowklass(), "should have got back a narrow oop");
 725       return new LoadPNode(ctl, mem, adr, adr_type, rt->is_oopptr(), mo);
 726     }
 727   }
 728   ShouldNotReachHere();
 729   return (LoadNode*)NULL;
 730 }
 731 
 732 LoadLNode* LoadLNode::make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt, MemOrd mo) {
 733   bool require_atomic = true;
 734   return new LoadLNode(ctl, mem, adr, adr_type, rt->is_long(), mo, require_atomic);
 735 }
 736 
 737 LoadDNode* LoadDNode::make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, const Type* rt, MemOrd mo) {
 738   bool require_atomic = true;
 739   return new LoadDNode(ctl, mem, adr, adr_type, rt, mo, require_atomic);
 740 }
 741 
 742 
 743 
 744 //------------------------------hash-------------------------------------------
 745 uint LoadNode::hash() const {
 746   // unroll addition of interesting fields
 747   return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address);
 748 }
 749 
 750 static bool skip_through_membars(Compile::AliasType* atp, const TypeInstPtr* tp, bool eliminate_boxing) {
 751   if ((atp != NULL) && (atp->index() >= Compile::AliasIdxRaw)) {
 752     bool non_volatile = (atp->field() != NULL) && !atp->field()->is_volatile();
 753     bool is_stable_ary = FoldStableValues &&
 754                          (tp != NULL) && (tp->isa_aryptr() != NULL) &&
 755                          tp->isa_aryptr()->is_stable();
 756 
 757     return (eliminate_boxing && non_volatile) || is_stable_ary;
 758   }
 759 
 760   return false;
 761 }
 762 
 763 //---------------------------can_see_stored_value------------------------------
 764 // This routine exists to make sure this set of tests is done the same
 765 // everywhere.  We need to make a coordinated change: first LoadNode::Ideal
 766 // will change the graph shape in a way which makes memory alive twice at the
 767 // same time (uses the Oracle model of aliasing), then some
 768 // LoadXNode::Identity will fold things back to the equivalence-class model
 769 // of aliasing.
 770 Node* MemNode::can_see_stored_value(Node* st, PhaseTransform* phase) const {
 771   Node* ld_adr = in(MemNode::Address);
 772   intptr_t ld_off = 0;
 773   AllocateNode* ld_alloc = AllocateNode::Ideal_allocation(ld_adr, phase, ld_off);
 774   const TypeInstPtr* tp = phase->type(ld_adr)->isa_instptr();
 775   Compile::AliasType* atp = (tp != NULL) ? phase->C->alias_type(tp) : NULL;
 776   // This is more general than load from boxing objects.
 777   if (skip_through_membars(atp, tp, phase->C->eliminate_boxing())) {
 778     uint alias_idx = atp->index();
 779     bool final = !atp->is_rewritable();
 780     Node* result = NULL;
 781     Node* current = st;
 782     // Skip through chains of MemBarNodes checking the MergeMems for
 783     // new states for the slice of this load.  Stop once any other
 784     // kind of node is encountered.  Loads from final memory can skip
 785     // through any kind of MemBar but normal loads shouldn't skip
 786     // through MemBarAcquire since the could allow them to move out of
 787     // a synchronized region.
 788     while (current->is_Proj()) {
 789       int opc = current->in(0)->Opcode();
 790       if ((final && (opc == Op_MemBarAcquire ||
 791                      opc == Op_MemBarAcquireLock ||
 792                      opc == Op_LoadFence)) ||
 793           opc == Op_MemBarRelease ||
 794           opc == Op_StoreFence ||
 795           opc == Op_MemBarReleaseLock ||
 796           opc == Op_MemBarCPUOrder) {
 797         Node* mem = current->in(0)->in(TypeFunc::Memory);
 798         if (mem->is_MergeMem()) {
 799           MergeMemNode* merge = mem->as_MergeMem();
 800           Node* new_st = merge->memory_at(alias_idx);
 801           if (new_st == merge->base_memory()) {
 802             // Keep searching
 803             current = new_st;
 804             continue;
 805           }
 806           // Save the new memory state for the slice and fall through
 807           // to exit.
 808           result = new_st;
 809         }
 810       }
 811       break;
 812     }
 813     if (result != NULL) {
 814       st = result;
 815     }
 816   }
 817 
 818   // Loop around twice in the case Load -> Initialize -> Store.
 819   // (See PhaseIterGVN::add_users_to_worklist, which knows about this case.)
 820   for (int trip = 0; trip <= 1; trip++) {
 821 
 822     if (st->is_Store()) {
 823       Node* st_adr = st->in(MemNode::Address);
 824       if (!phase->eqv(st_adr, ld_adr)) {
 825         // Try harder before giving up...  Match raw and non-raw pointers.
 826         intptr_t st_off = 0;
 827         AllocateNode* alloc = AllocateNode::Ideal_allocation(st_adr, phase, st_off);
 828         if (alloc == NULL)       return NULL;
 829         if (alloc != ld_alloc)   return NULL;
 830         if (ld_off != st_off)    return NULL;
 831         // At this point we have proven something like this setup:
 832         //  A = Allocate(...)
 833         //  L = LoadQ(,  AddP(CastPP(, A.Parm),, #Off))
 834         //  S = StoreQ(, AddP(,        A.Parm  , #Off), V)
 835         // (Actually, we haven't yet proven the Q's are the same.)
 836         // In other words, we are loading from a casted version of
 837         // the same pointer-and-offset that we stored to.
 838         // Thus, we are able to replace L by V.
 839       }
 840       // Now prove that we have a LoadQ matched to a StoreQ, for some Q.
 841       if (store_Opcode() != st->Opcode())
 842         return NULL;
 843       return st->in(MemNode::ValueIn);
 844     }
 845 
 846     // A load from a freshly-created object always returns zero.
 847     // (This can happen after LoadNode::Ideal resets the load's memory input
 848     // to find_captured_store, which returned InitializeNode::zero_memory.)
 849     if (st->is_Proj() && st->in(0)->is_Allocate() &&
 850         (st->in(0) == ld_alloc) &&
 851         (ld_off >= st->in(0)->as_Allocate()->minimum_header_size())) {
 852       // return a zero value for the load's basic type
 853       // (This is one of the few places where a generic PhaseTransform
 854       // can create new nodes.  Think of it as lazily manifesting
 855       // virtually pre-existing constants.)
 856       return phase->zerocon(memory_type());
 857     }
 858 
 859     // A load from an initialization barrier can match a captured store.
 860     if (st->is_Proj() && st->in(0)->is_Initialize()) {
 861       InitializeNode* init = st->in(0)->as_Initialize();
 862       AllocateNode* alloc = init->allocation();
 863       if ((alloc != NULL) && (alloc == ld_alloc)) {
 864         // examine a captured store value
 865         st = init->find_captured_store(ld_off, memory_size(), phase);
 866         if (st != NULL)
 867           continue;             // take one more trip around
 868       }
 869     }
 870 
 871     // Load boxed value from result of valueOf() call is input parameter.
 872     if (this->is_Load() && ld_adr->is_AddP() &&
 873         (tp != NULL) && tp->is_ptr_to_boxed_value()) {
 874       intptr_t ignore = 0;
 875       Node* base = AddPNode::Ideal_base_and_offset(ld_adr, phase, ignore);
 876       if (base != NULL && base->is_Proj() &&
 877           base->as_Proj()->_con == TypeFunc::Parms &&
 878           base->in(0)->is_CallStaticJava() &&
 879           base->in(0)->as_CallStaticJava()->is_boxing_method()) {
 880         return base->in(0)->in(TypeFunc::Parms);
 881       }
 882     }
 883 
 884     break;
 885   }
 886 
 887   return NULL;
 888 }
 889 
 890 //----------------------is_instance_field_load_with_local_phi------------------
 891 bool LoadNode::is_instance_field_load_with_local_phi(Node* ctrl) {
 892   if( in(Memory)->is_Phi() && in(Memory)->in(0) == ctrl &&
 893       in(Address)->is_AddP() ) {
 894     const TypeOopPtr* t_oop = in(Address)->bottom_type()->isa_oopptr();
 895     // Only instances and boxed values.
 896     if( t_oop != NULL &&
 897         (t_oop->is_ptr_to_boxed_value() ||
 898          t_oop->is_known_instance_field()) &&
 899         t_oop->offset() != Type::OffsetBot &&
 900         t_oop->offset() != Type::OffsetTop) {
 901       return true;
 902     }
 903   }
 904   return false;
 905 }
 906 
 907 //------------------------------Identity---------------------------------------
 908 // Loads are identity if previous store is to same address
 909 Node *LoadNode::Identity( PhaseTransform *phase ) {
 910   // If the previous store-maker is the right kind of Store, and the store is
 911   // to the same address, then we are equal to the value stored.
 912   Node* mem = in(Memory);
 913   Node* value = can_see_stored_value(mem, phase);
 914   if( value ) {
 915     // byte, short & char stores truncate naturally.
 916     // A load has to load the truncated value which requires
 917     // some sort of masking operation and that requires an
 918     // Ideal call instead of an Identity call.
 919     if (memory_size() < BytesPerInt) {
 920       // If the input to the store does not fit with the load's result type,
 921       // it must be truncated via an Ideal call.
 922       if (!phase->type(value)->higher_equal(phase->type(this)))
 923         return this;
 924     }
 925     // (This works even when value is a Con, but LoadNode::Value
 926     // usually runs first, producing the singleton type of the Con.)
 927     return value;
 928   }
 929 
 930   // Search for an existing data phi which was generated before for the same
 931   // instance's field to avoid infinite generation of phis in a loop.
 932   Node *region = mem->in(0);
 933   if (is_instance_field_load_with_local_phi(region)) {
 934     const TypeOopPtr *addr_t = in(Address)->bottom_type()->isa_oopptr();
 935     int this_index  = phase->C->get_alias_index(addr_t);
 936     int this_offset = addr_t->offset();
 937     int this_iid    = addr_t->instance_id();
 938     if (!addr_t->is_known_instance() &&
 939          addr_t->is_ptr_to_boxed_value()) {
 940       // Use _idx of address base (could be Phi node) for boxed values.
 941       intptr_t   ignore = 0;
 942       Node*      base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
 943       this_iid = base->_idx;
 944     }
 945     const Type* this_type = bottom_type();
 946     for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
 947       Node* phi = region->fast_out(i);
 948       if (phi->is_Phi() && phi != mem &&
 949           phi->as_Phi()->is_same_inst_field(this_type, this_iid, this_index, this_offset)) {
 950         return phi;
 951       }
 952     }
 953   }
 954 
 955   return this;
 956 }
 957 
 958 // We're loading from an object which has autobox behaviour.
 959 // If this object is result of a valueOf call we'll have a phi
 960 // merging a newly allocated object and a load from the cache.
 961 // We want to replace this load with the original incoming
 962 // argument to the valueOf call.
 963 Node* LoadNode::eliminate_autobox(PhaseGVN* phase) {
 964   assert(phase->C->eliminate_boxing(), "sanity");
 965   intptr_t ignore = 0;
 966   Node* base = AddPNode::Ideal_base_and_offset(in(Address), phase, ignore);
 967   if ((base == NULL) || base->is_Phi()) {
 968     // Push the loads from the phi that comes from valueOf up
 969     // through it to allow elimination of the loads and the recovery
 970     // of the original value. It is done in split_through_phi().
 971     return NULL;
 972   } else if (base->is_Load() ||
 973              base->is_DecodeN() && base->in(1)->is_Load()) {
 974     // Eliminate the load of boxed value for integer types from the cache
 975     // array by deriving the value from the index into the array.
 976     // Capture the offset of the load and then reverse the computation.
 977 
 978     // Get LoadN node which loads a boxing object from 'cache' array.
 979     if (base->is_DecodeN()) {
 980       base = base->in(1);
 981     }
 982     if (!base->in(Address)->is_AddP()) {
 983       return NULL; // Complex address
 984     }
 985     AddPNode* address = base->in(Address)->as_AddP();
 986     Node* cache_base = address->in(AddPNode::Base);
 987     if ((cache_base != NULL) && cache_base->is_DecodeN()) {
 988       // Get ConP node which is static 'cache' field.
 989       cache_base = cache_base->in(1);
 990     }
 991     if ((cache_base != NULL) && cache_base->is_Con()) {
 992       const TypeAryPtr* base_type = cache_base->bottom_type()->isa_aryptr();
 993       if ((base_type != NULL) && base_type->is_autobox_cache()) {
 994         Node* elements[4];
 995         int shift = exact_log2(type2aelembytes(T_OBJECT));
 996         int count = address->unpack_offsets(elements, ARRAY_SIZE(elements));
 997         if ((count >  0) && elements[0]->is_Con() &&
 998             ((count == 1) ||
 999              (count == 2) && elements[1]->Opcode() == Op_LShiftX &&
1000                              elements[1]->in(2) == phase->intcon(shift))) {
1001           ciObjArray* array = base_type->const_oop()->as_obj_array();
1002           // Fetch the box object cache[0] at the base of the array and get its value
1003           ciInstance* box = array->obj_at(0)->as_instance();
1004           ciInstanceKlass* ik = box->klass()->as_instance_klass();
1005           assert(ik->is_box_klass(), "sanity");
1006           assert(ik->nof_nonstatic_fields() == 1, "change following code");
1007           if (ik->nof_nonstatic_fields() == 1) {
1008             // This should be true nonstatic_field_at requires calling
1009             // nof_nonstatic_fields so check it anyway
1010             ciConstant c = box->field_value(ik->nonstatic_field_at(0));
1011             BasicType bt = c.basic_type();
1012             // Only integer types have boxing cache.
1013             assert(bt == T_BOOLEAN || bt == T_CHAR  ||
1014                    bt == T_BYTE    || bt == T_SHORT ||
1015                    bt == T_INT     || bt == T_LONG, err_msg_res("wrong type = %s", type2name(bt)));
1016             jlong cache_low = (bt == T_LONG) ? c.as_long() : c.as_int();
1017             if (cache_low != (int)cache_low) {
1018               return NULL; // should not happen since cache is array indexed by value
1019             }
1020             jlong offset = arrayOopDesc::base_offset_in_bytes(T_OBJECT) - (cache_low << shift);
1021             if (offset != (int)offset) {
1022               return NULL; // should not happen since cache is array indexed by value
1023             }
1024            // Add up all the offsets making of the address of the load
1025             Node* result = elements[0];
1026             for (int i = 1; i < count; i++) {
1027               result = phase->transform(new AddXNode(result, elements[i]));
1028             }
1029             // Remove the constant offset from the address and then
1030             result = phase->transform(new AddXNode(result, phase->MakeConX(-(int)offset)));
1031             // remove the scaling of the offset to recover the original index.
1032             if (result->Opcode() == Op_LShiftX && result->in(2) == phase->intcon(shift)) {
1033               // Peel the shift off directly but wrap it in a dummy node
1034               // since Ideal can't return existing nodes
1035               result = new RShiftXNode(result->in(1), phase->intcon(0));
1036             } else if (result->is_Add() && result->in(2)->is_Con() &&
1037                        result->in(1)->Opcode() == Op_LShiftX &&
1038                        result->in(1)->in(2) == phase->intcon(shift)) {
1039               // We can't do general optimization: ((X<<Z) + Y) >> Z ==> X + (Y>>Z)
1040               // but for boxing cache access we know that X<<Z will not overflow
1041               // (there is range check) so we do this optimizatrion by hand here.
1042               Node* add_con = new RShiftXNode(result->in(2), phase->intcon(shift));
1043               result = new AddXNode(result->in(1)->in(1), phase->transform(add_con));
1044             } else {
1045               result = new RShiftXNode(result, phase->intcon(shift));
1046             }
1047 #ifdef _LP64
1048             if (bt != T_LONG) {
1049               result = new ConvL2INode(phase->transform(result));
1050             }
1051 #else
1052             if (bt == T_LONG) {
1053               result = new ConvI2LNode(phase->transform(result));
1054             }
1055 #endif
1056             // Boxing/unboxing can be done from signed & unsigned loads (e.g. LoadUB -> ... -> LoadB pair).
1057             // Need to preserve unboxing load type if it is unsigned.
1058             switch(this->Opcode()) {
1059               case Op_LoadUB:
1060                 result = new AndINode(phase->transform(result), phase->intcon(0xFF));
1061                 break;
1062               case Op_LoadUS:
1063                 result = new AndINode(phase->transform(result), phase->intcon(0xFFFF));
1064                 break;
1065             }
1066             return result;
1067           }
1068         }
1069       }
1070     }
1071   }
1072   return NULL;
1073 }
1074 
1075 static bool stable_phi(PhiNode* phi, PhaseGVN *phase) {
1076   Node* region = phi->in(0);
1077   if (region == NULL) {
1078     return false; // Wait stable graph
1079   }
1080   uint cnt = phi->req();
1081   for (uint i = 1; i < cnt; i++) {
1082     Node* rc = region->in(i);
1083     if (rc == NULL || phase->type(rc) == Type::TOP)
1084       return false; // Wait stable graph
1085     Node* in = phi->in(i);
1086     if (in == NULL || phase->type(in) == Type::TOP)
1087       return false; // Wait stable graph
1088   }
1089   return true;
1090 }
1091 //------------------------------split_through_phi------------------------------
1092 // Split instance or boxed field load through Phi.
1093 Node *LoadNode::split_through_phi(PhaseGVN *phase) {
1094   Node* mem     = in(Memory);
1095   Node* address = in(Address);
1096   const TypeOopPtr *t_oop = phase->type(address)->isa_oopptr();
1097 
1098   assert((t_oop != NULL) &&
1099          (t_oop->is_known_instance_field() ||
1100           t_oop->is_ptr_to_boxed_value()), "invalide conditions");
1101 
1102   Compile* C = phase->C;
1103   intptr_t ignore = 0;
1104   Node*    base = AddPNode::Ideal_base_and_offset(address, phase, ignore);
1105   bool base_is_phi = (base != NULL) && base->is_Phi();
1106   bool load_boxed_values = t_oop->is_ptr_to_boxed_value() && C->aggressive_unboxing() &&
1107                            (base != NULL) && (base == address->in(AddPNode::Base)) &&
1108                            phase->type(base)->higher_equal(TypePtr::NOTNULL);
1109 
1110   if (!((mem->is_Phi() || base_is_phi) &&
1111         (load_boxed_values || t_oop->is_known_instance_field()))) {
1112     return NULL; // memory is not Phi
1113   }
1114 
1115   if (mem->is_Phi()) {
1116     if (!stable_phi(mem->as_Phi(), phase)) {
1117       return NULL; // Wait stable graph
1118     }
1119     uint cnt = mem->req();
1120     // Check for loop invariant memory.
1121     if (cnt == 3) {
1122       for (uint i = 1; i < cnt; i++) {
1123         Node* in = mem->in(i);
1124         Node*  m = optimize_memory_chain(in, t_oop, this, phase);
1125         if (m == mem) {
1126           set_req(Memory, mem->in(cnt - i));
1127           return this; // made change
1128         }
1129       }
1130     }
1131   }
1132   if (base_is_phi) {
1133     if (!stable_phi(base->as_Phi(), phase)) {
1134       return NULL; // Wait stable graph
1135     }
1136     uint cnt = base->req();
1137     // Check for loop invariant memory.
1138     if (cnt == 3) {
1139       for (uint i = 1; i < cnt; i++) {
1140         if (base->in(i) == base) {
1141           return NULL; // Wait stable graph
1142         }
1143       }
1144     }
1145   }
1146 
1147   bool load_boxed_phi = load_boxed_values && base_is_phi && (base->in(0) == mem->in(0));
1148 
1149   // Split through Phi (see original code in loopopts.cpp).
1150   assert(C->have_alias_type(t_oop), "instance should have alias type");
1151 
1152   // Do nothing here if Identity will find a value
1153   // (to avoid infinite chain of value phis generation).
1154   if (!phase->eqv(this, this->Identity(phase)))
1155     return NULL;
1156 
1157   // Select Region to split through.
1158   Node* region;
1159   if (!base_is_phi) {
1160     assert(mem->is_Phi(), "sanity");
1161     region = mem->in(0);
1162     // Skip if the region dominates some control edge of the address.
1163     if (!MemNode::all_controls_dominate(address, region))
1164       return NULL;
1165   } else if (!mem->is_Phi()) {
1166     assert(base_is_phi, "sanity");
1167     region = base->in(0);
1168     // Skip if the region dominates some control edge of the memory.
1169     if (!MemNode::all_controls_dominate(mem, region))
1170       return NULL;
1171   } else if (base->in(0) != mem->in(0)) {
1172     assert(base_is_phi && mem->is_Phi(), "sanity");
1173     if (MemNode::all_controls_dominate(mem, base->in(0))) {
1174       region = base->in(0);
1175     } else if (MemNode::all_controls_dominate(address, mem->in(0))) {
1176       region = mem->in(0);
1177     } else {
1178       return NULL; // complex graph
1179     }
1180   } else {
1181     assert(base->in(0) == mem->in(0), "sanity");
1182     region = mem->in(0);
1183   }
1184 
1185   const Type* this_type = this->bottom_type();
1186   int this_index  = C->get_alias_index(t_oop);
1187   int this_offset = t_oop->offset();
1188   int this_iid    = t_oop->instance_id();
1189   if (!t_oop->is_known_instance() && load_boxed_values) {
1190     // Use _idx of address base for boxed values.
1191     this_iid = base->_idx;
1192   }
1193   PhaseIterGVN* igvn = phase->is_IterGVN();
1194   Node* phi = new PhiNode(region, this_type, NULL, this_iid, this_index, this_offset);
1195   for (uint i = 1; i < region->req(); i++) {
1196     Node* x;
1197     Node* the_clone = NULL;
1198     if (region->in(i) == C->top()) {
1199       x = C->top();      // Dead path?  Use a dead data op
1200     } else {
1201       x = this->clone();        // Else clone up the data op
1202       the_clone = x;            // Remember for possible deletion.
1203       // Alter data node to use pre-phi inputs
1204       if (this->in(0) == region) {
1205         x->set_req(0, region->in(i));
1206       } else {
1207         x->set_req(0, NULL);
1208       }
1209       if (mem->is_Phi() && (mem->in(0) == region)) {
1210         x->set_req(Memory, mem->in(i)); // Use pre-Phi input for the clone.
1211       }
1212       if (address->is_Phi() && address->in(0) == region) {
1213         x->set_req(Address, address->in(i)); // Use pre-Phi input for the clone
1214       }
1215       if (base_is_phi && (base->in(0) == region)) {
1216         Node* base_x = base->in(i); // Clone address for loads from boxed objects.
1217         Node* adr_x = phase->transform(new AddPNode(base_x,base_x,address->in(AddPNode::Offset)));
1218         x->set_req(Address, adr_x);
1219       }
1220     }
1221     // Check for a 'win' on some paths
1222     const Type *t = x->Value(igvn);
1223 
1224     bool singleton = t->singleton();
1225 
1226     // See comments in PhaseIdealLoop::split_thru_phi().
1227     if (singleton && t == Type::TOP) {
1228       singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
1229     }
1230 
1231     if (singleton) {
1232       x = igvn->makecon(t);
1233     } else {
1234       // We now call Identity to try to simplify the cloned node.
1235       // Note that some Identity methods call phase->type(this).
1236       // Make sure that the type array is big enough for
1237       // our new node, even though we may throw the node away.
1238       // (This tweaking with igvn only works because x is a new node.)
1239       igvn->set_type(x, t);
1240       // If x is a TypeNode, capture any more-precise type permanently into Node
1241       // otherwise it will be not updated during igvn->transform since
1242       // igvn->type(x) is set to x->Value() already.
1243       x->raise_bottom_type(t);
1244       Node *y = x->Identity(igvn);
1245       if (y != x) {
1246         x = y;
1247       } else {
1248         y = igvn->hash_find_insert(x);
1249         if (y) {
1250           x = y;
1251         } else {
1252           // Else x is a new node we are keeping
1253           // We do not need register_new_node_with_optimizer
1254           // because set_type has already been called.
1255           igvn->_worklist.push(x);
1256         }
1257       }
1258     }
1259     if (x != the_clone && the_clone != NULL) {
1260       igvn->remove_dead_node(the_clone);
1261     }
1262     phi->set_req(i, x);
1263   }
1264   // Record Phi
1265   igvn->register_new_node_with_optimizer(phi);
1266   return phi;
1267 }
1268 
1269 //------------------------------Ideal------------------------------------------
1270 // If the load is from Field memory and the pointer is non-null, it might be possible to
1271 // zero out the control input.
1272 // If the offset is constant and the base is an object allocation,
1273 // try to hook me up to the exact initializing store.
1274 Node *LoadNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1275   Node* p = MemNode::Ideal_common(phase, can_reshape);
1276   if (p)  return (p == NodeSentinel) ? NULL : p;
1277 
1278   Node* ctrl    = in(MemNode::Control);
1279   Node* address = in(MemNode::Address);
1280   bool progress = false;
1281 
1282   // Skip up past a SafePoint control.  Cannot do this for Stores because
1283   // pointer stores & cardmarks must stay on the same side of a SafePoint.
1284   if( ctrl != NULL && ctrl->Opcode() == Op_SafePoint &&
1285       phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw ) {
1286     ctrl = ctrl->in(0);
1287     set_req(MemNode::Control,ctrl);
1288     progress = true;
1289   }
1290 
1291   intptr_t ignore = 0;
1292   Node*    base   = AddPNode::Ideal_base_and_offset(address, phase, ignore);
1293   if (base != NULL
1294       && phase->C->get_alias_index(phase->type(address)->is_ptr()) != Compile::AliasIdxRaw) {
1295     // Check for useless control edge in some common special cases
1296     if (in(MemNode::Control) != NULL
1297         && can_remove_control()
1298         && phase->type(base)->higher_equal(TypePtr::NOTNULL)
1299         && all_controls_dominate(base, phase->C->start())) {
1300       // A method-invariant, non-null address (constant or 'this' argument).
1301       set_req(MemNode::Control, NULL);
1302       progress = true;
1303     }
1304   }
1305 
1306   Node* mem = in(MemNode::Memory);
1307   const TypePtr *addr_t = phase->type(address)->isa_ptr();
1308 
1309   if (can_reshape && (addr_t != NULL)) {
1310     // try to optimize our memory input
1311     Node* opt_mem = MemNode::optimize_memory_chain(mem, addr_t, this, phase);
1312     if (opt_mem != mem) {
1313       set_req(MemNode::Memory, opt_mem);
1314       if (phase->type( opt_mem ) == Type::TOP) return NULL;
1315       return this;
1316     }
1317     const TypeOopPtr *t_oop = addr_t->isa_oopptr();
1318     if ((t_oop != NULL) &&
1319         (t_oop->is_known_instance_field() ||
1320          t_oop->is_ptr_to_boxed_value())) {
1321       PhaseIterGVN *igvn = phase->is_IterGVN();
1322       if (igvn != NULL && igvn->_worklist.member(opt_mem)) {
1323         // Delay this transformation until memory Phi is processed.
1324         phase->is_IterGVN()->_worklist.push(this);
1325         return NULL;
1326       }
1327       // Split instance field load through Phi.
1328       Node* result = split_through_phi(phase);
1329       if (result != NULL) return result;
1330 
1331       if (t_oop->is_ptr_to_boxed_value()) {
1332         Node* result = eliminate_autobox(phase);
1333         if (result != NULL) return result;
1334       }
1335     }
1336   }
1337 
1338   // Check for prior store with a different base or offset; make Load
1339   // independent.  Skip through any number of them.  Bail out if the stores
1340   // are in an endless dead cycle and report no progress.  This is a key
1341   // transform for Reflection.  However, if after skipping through the Stores
1342   // we can't then fold up against a prior store do NOT do the transform as
1343   // this amounts to using the 'Oracle' model of aliasing.  It leaves the same
1344   // array memory alive twice: once for the hoisted Load and again after the
1345   // bypassed Store.  This situation only works if EVERYBODY who does
1346   // anti-dependence work knows how to bypass.  I.e. we need all
1347   // anti-dependence checks to ask the same Oracle.  Right now, that Oracle is
1348   // the alias index stuff.  So instead, peek through Stores and IFF we can
1349   // fold up, do so.
1350   Node* prev_mem = find_previous_store(phase);
1351   // Steps (a), (b):  Walk past independent stores to find an exact match.
1352   if (prev_mem != NULL && prev_mem != in(MemNode::Memory)) {
1353     // (c) See if we can fold up on the spot, but don't fold up here.
1354     // Fold-up might require truncation (for LoadB/LoadS/LoadUS) or
1355     // just return a prior value, which is done by Identity calls.
1356     if (can_see_stored_value(prev_mem, phase)) {
1357       // Make ready for step (d):
1358       set_req(MemNode::Memory, prev_mem);
1359       return this;
1360     }
1361   }
1362 
1363   return progress ? this : NULL;
1364 }
1365 
1366 // Helper to recognize certain Klass fields which are invariant across
1367 // some group of array types (e.g., int[] or all T[] where T < Object).
1368 const Type*
1369 LoadNode::load_array_final_field(const TypeKlassPtr *tkls,
1370                                  ciKlass* klass) const {
1371   if (tkls->offset() == in_bytes(Klass::modifier_flags_offset())) {
1372     // The field is Klass::_modifier_flags.  Return its (constant) value.
1373     // (Folds up the 2nd indirection in aClassConstant.getModifiers().)
1374     assert(this->Opcode() == Op_LoadI, "must load an int from _modifier_flags");
1375     return TypeInt::make(klass->modifier_flags());
1376   }
1377   if (tkls->offset() == in_bytes(Klass::access_flags_offset())) {
1378     // The field is Klass::_access_flags.  Return its (constant) value.
1379     // (Folds up the 2nd indirection in Reflection.getClassAccessFlags(aClassConstant).)
1380     assert(this->Opcode() == Op_LoadI, "must load an int from _access_flags");
1381     return TypeInt::make(klass->access_flags());
1382   }
1383   if (tkls->offset() == in_bytes(Klass::layout_helper_offset())) {
1384     // The field is Klass::_layout_helper.  Return its constant value if known.
1385     assert(this->Opcode() == Op_LoadI, "must load an int from _layout_helper");
1386     return TypeInt::make(klass->layout_helper());
1387   }
1388 
1389   // No match.
1390   return NULL;
1391 }
1392 
1393 // Try to constant-fold a stable array element.
1394 static const Type* fold_stable_ary_elem(const TypeAryPtr* ary, int off, BasicType loadbt) {
1395   assert(ary->const_oop(), "array should be constant");
1396   assert(ary->is_stable(), "array should be stable");
1397 
1398   // Decode the results of GraphKit::array_element_address.
1399   ciArray* aobj = ary->const_oop()->as_array();
1400   ciConstant con = aobj->element_value_by_offset(off);
1401 
1402   if (con.basic_type() != T_ILLEGAL && !con.is_null_or_zero()) {
1403     const Type* con_type = Type::make_from_constant(con);
1404     if (con_type != NULL) {
1405       if (con_type->isa_aryptr()) {
1406         // Join with the array element type, in case it is also stable.
1407         int dim = ary->stable_dimension();
1408         con_type = con_type->is_aryptr()->cast_to_stable(true, dim-1);
1409       }
1410       if (loadbt == T_NARROWOOP && con_type->isa_oopptr()) {
1411         con_type = con_type->make_narrowoop();
1412       }
1413 #ifndef PRODUCT
1414       if (TraceIterativeGVN) {
1415         tty->print("FoldStableValues: array element [off=%d]: con_type=", off);
1416         con_type->dump(); tty->cr();
1417       }
1418 #endif //PRODUCT
1419       return con_type;
1420     }
1421   }
1422   return NULL;
1423 }
1424 
1425 //------------------------------Value-----------------------------------------
1426 const Type *LoadNode::Value( PhaseTransform *phase ) const {
1427   // Either input is TOP ==> the result is TOP
1428   Node* mem = in(MemNode::Memory);
1429   const Type *t1 = phase->type(mem);
1430   if (t1 == Type::TOP)  return Type::TOP;
1431   Node* adr = in(MemNode::Address);
1432   const TypePtr* tp = phase->type(adr)->isa_ptr();
1433   if (tp == NULL || tp->empty())  return Type::TOP;
1434   int off = tp->offset();
1435   assert(off != Type::OffsetTop, "case covered by TypePtr::empty");
1436   Compile* C = phase->C;
1437 
1438   // Try to guess loaded type from pointer type
1439   if (tp->isa_aryptr()) {
1440     const TypeAryPtr* ary = tp->is_aryptr();
1441     const Type* t = ary->elem();
1442 
1443     // Determine whether the reference is beyond the header or not, by comparing
1444     // the offset against the offset of the start of the array's data.
1445     // Different array types begin at slightly different offsets (12 vs. 16).
1446     // We choose T_BYTE as an example base type that is least restrictive
1447     // as to alignment, which will therefore produce the smallest
1448     // possible base offset.
1449     const int min_base_off = arrayOopDesc::base_offset_in_bytes(T_BYTE);
1450     const bool off_beyond_header = ((uint)off >= (uint)min_base_off);
1451 
1452     // Try to constant-fold a stable array element.
1453     if (FoldStableValues && ary->is_stable() && ary->const_oop() != NULL) {
1454       // Make sure the reference is not into the header and the offset is constant
1455       if (off_beyond_header && adr->is_AddP() && off != Type::OffsetBot) {
1456         const Type* con_type = fold_stable_ary_elem(ary, off, memory_type());
1457         if (con_type != NULL) {
1458           return con_type;
1459         }
1460       }
1461     }
1462 
1463     // Don't do this for integer types. There is only potential profit if
1464     // the element type t is lower than _type; that is, for int types, if _type is
1465     // more restrictive than t.  This only happens here if one is short and the other
1466     // char (both 16 bits), and in those cases we've made an intentional decision
1467     // to use one kind of load over the other. See AndINode::Ideal and 4965907.
1468     // Also, do not try to narrow the type for a LoadKlass, regardless of offset.
1469     //
1470     // Yes, it is possible to encounter an expression like (LoadKlass p1:(AddP x x 8))
1471     // where the _gvn.type of the AddP is wider than 8.  This occurs when an earlier
1472     // copy p0 of (AddP x x 8) has been proven equal to p1, and the p0 has been
1473     // subsumed by p1.  If p1 is on the worklist but has not yet been re-transformed,
1474     // it is possible that p1 will have a type like Foo*[int+]:NotNull*+any.
1475     // In fact, that could have been the original type of p1, and p1 could have
1476     // had an original form like p1:(AddP x x (LShiftL quux 3)), where the
1477     // expression (LShiftL quux 3) independently optimized to the constant 8.
1478     if ((t->isa_int() == NULL) && (t->isa_long() == NULL)
1479         && (_type->isa_vect() == NULL)
1480         && Opcode() != Op_LoadKlass && Opcode() != Op_LoadNKlass) {
1481       // t might actually be lower than _type, if _type is a unique
1482       // concrete subclass of abstract class t.
1483       if (off_beyond_header) {  // is the offset beyond the header?
1484         const Type* jt = t->join_speculative(_type);
1485         // In any case, do not allow the join, per se, to empty out the type.
1486         if (jt->empty() && !t->empty()) {
1487           // This can happen if a interface-typed array narrows to a class type.
1488           jt = _type;
1489         }
1490 #ifdef ASSERT
1491         if (phase->C->eliminate_boxing() && adr->is_AddP()) {
1492           // The pointers in the autobox arrays are always non-null
1493           Node* base = adr->in(AddPNode::Base);
1494           if ((base != NULL) && base->is_DecodeN()) {
1495             // Get LoadN node which loads IntegerCache.cache field
1496             base = base->in(1);
1497           }
1498           if ((base != NULL) && base->is_Con()) {
1499             const TypeAryPtr* base_type = base->bottom_type()->isa_aryptr();
1500             if ((base_type != NULL) && base_type->is_autobox_cache()) {
1501               // It could be narrow oop
1502               assert(jt->make_ptr()->ptr() == TypePtr::NotNull,"sanity");
1503             }
1504           }
1505         }
1506 #endif
1507         return jt;
1508       }
1509     }
1510   } else if (tp->base() == Type::InstPtr) {
1511     ciEnv* env = C->env();
1512     const TypeInstPtr* tinst = tp->is_instptr();
1513     ciKlass* klass = tinst->klass();
1514     assert( off != Type::OffsetBot ||
1515             // arrays can be cast to Objects
1516             tp->is_oopptr()->klass()->is_java_lang_Object() ||
1517             // unsafe field access may not have a constant offset
1518             C->has_unsafe_access(),
1519             "Field accesses must be precise" );
1520     // For oop loads, we expect the _type to be precise
1521     if (klass == env->String_klass() &&
1522         adr->is_AddP() && off != Type::OffsetBot) {
1523       // For constant Strings treat the final fields as compile time constants.
1524       Node* base = adr->in(AddPNode::Base);
1525       const TypeOopPtr* t = phase->type(base)->isa_oopptr();
1526       if (t != NULL && t->singleton()) {
1527         ciField* field = env->String_klass()->get_field_by_offset(off, false);
1528         if (field != NULL && field->is_final()) {
1529           ciObject* string = t->const_oop();
1530           ciConstant constant = string->as_instance()->field_value(field);
1531           if (constant.basic_type() == T_INT) {
1532             return TypeInt::make(constant.as_int());
1533           } else if (constant.basic_type() == T_ARRAY) {
1534             if (adr->bottom_type()->is_ptr_to_narrowoop()) {
1535               return TypeNarrowOop::make_from_constant(constant.as_object(), true);
1536             } else {
1537               return TypeOopPtr::make_from_constant(constant.as_object(), true);
1538             }
1539           }
1540         }
1541       }
1542     }
1543     // Optimizations for constant objects
1544     ciObject* const_oop = tinst->const_oop();
1545     if (const_oop != NULL) {
1546       // For constant Boxed value treat the target field as a compile time constant.
1547       if (tinst->is_ptr_to_boxed_value()) {
1548         return tinst->get_const_boxed_value();
1549       } else
1550       // For constant CallSites treat the target field as a compile time constant.
1551       if (const_oop->is_call_site()) {
1552         ciCallSite* call_site = const_oop->as_call_site();
1553         ciField* field = call_site->klass()->as_instance_klass()->get_field_by_offset(off, /*is_static=*/ false);
1554         if (field != NULL && field->is_call_site_target()) {
1555           ciMethodHandle* target = call_site->get_target();
1556           if (target != NULL) {  // just in case
1557             ciConstant constant(T_OBJECT, target);
1558             const Type* t;
1559             if (adr->bottom_type()->is_ptr_to_narrowoop()) {
1560               t = TypeNarrowOop::make_from_constant(constant.as_object(), true);
1561             } else {
1562               t = TypeOopPtr::make_from_constant(constant.as_object(), true);
1563             }
1564             // Add a dependence for invalidation of the optimization.
1565             if (!call_site->is_constant_call_site()) {
1566               C->dependencies()->assert_call_site_target_value(call_site, target);
1567             }
1568             return t;
1569           }
1570         }
1571       }
1572     }
1573   } else if (tp->base() == Type::KlassPtr) {
1574     assert( off != Type::OffsetBot ||
1575             // arrays can be cast to Objects
1576             tp->is_klassptr()->klass()->is_java_lang_Object() ||
1577             // also allow array-loading from the primary supertype
1578             // array during subtype checks
1579             Opcode() == Op_LoadKlass,
1580             "Field accesses must be precise" );
1581     // For klass/static loads, we expect the _type to be precise
1582   }
1583 
1584   const TypeKlassPtr *tkls = tp->isa_klassptr();
1585   if (tkls != NULL && !StressReflectiveCode) {
1586     ciKlass* klass = tkls->klass();
1587     if (klass->is_loaded() && tkls->klass_is_exact()) {
1588       // We are loading a field from a Klass metaobject whose identity
1589       // is known at compile time (the type is "exact" or "precise").
1590       // Check for fields we know are maintained as constants by the VM.
1591       if (tkls->offset() == in_bytes(Klass::super_check_offset_offset())) {
1592         // The field is Klass::_super_check_offset.  Return its (constant) value.
1593         // (Folds up type checking code.)
1594         assert(Opcode() == Op_LoadI, "must load an int from _super_check_offset");
1595         return TypeInt::make(klass->super_check_offset());
1596       }
1597       // Compute index into primary_supers array
1598       juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
1599       // Check for overflowing; use unsigned compare to handle the negative case.
1600       if( depth < ciKlass::primary_super_limit() ) {
1601         // The field is an element of Klass::_primary_supers.  Return its (constant) value.
1602         // (Folds up type checking code.)
1603         assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
1604         ciKlass *ss = klass->super_of_depth(depth);
1605         return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
1606       }
1607       const Type* aift = load_array_final_field(tkls, klass);
1608       if (aift != NULL)  return aift;
1609       if (tkls->offset() == in_bytes(Klass::java_mirror_offset())) {
1610         // The field is Klass::_java_mirror.  Return its (constant) value.
1611         // (Folds up the 2nd indirection in anObjConstant.getClass().)
1612         assert(Opcode() == Op_LoadP, "must load an oop from _java_mirror");
1613         return TypeInstPtr::make(klass->java_mirror());
1614       }
1615     }
1616 
1617     // We can still check if we are loading from the primary_supers array at a
1618     // shallow enough depth.  Even though the klass is not exact, entries less
1619     // than or equal to its super depth are correct.
1620     if (klass->is_loaded() ) {
1621       ciType *inner = klass;
1622       while( inner->is_obj_array_klass() )
1623         inner = inner->as_obj_array_klass()->base_element_type();
1624       if( inner->is_instance_klass() &&
1625           !inner->as_instance_klass()->flags().is_interface() ) {
1626         // Compute index into primary_supers array
1627         juint depth = (tkls->offset() - in_bytes(Klass::primary_supers_offset())) / sizeof(Klass*);
1628         // Check for overflowing; use unsigned compare to handle the negative case.
1629         if( depth < ciKlass::primary_super_limit() &&
1630             depth <= klass->super_depth() ) { // allow self-depth checks to handle self-check case
1631           // The field is an element of Klass::_primary_supers.  Return its (constant) value.
1632           // (Folds up type checking code.)
1633           assert(Opcode() == Op_LoadKlass, "must load a klass from _primary_supers");
1634           ciKlass *ss = klass->super_of_depth(depth);
1635           return ss ? TypeKlassPtr::make(ss) : TypePtr::NULL_PTR;
1636         }
1637       }
1638     }
1639 
1640     // If the type is enough to determine that the thing is not an array,
1641     // we can give the layout_helper a positive interval type.
1642     // This will help short-circuit some reflective code.
1643     if (tkls->offset() == in_bytes(Klass::layout_helper_offset())
1644         && !klass->is_array_klass() // not directly typed as an array
1645         && !klass->is_interface()  // specifically not Serializable & Cloneable
1646         && !klass->is_java_lang_Object()   // not the supertype of all T[]
1647         ) {
1648       // Note:  When interfaces are reliable, we can narrow the interface
1649       // test to (klass != Serializable && klass != Cloneable).
1650       assert(Opcode() == Op_LoadI, "must load an int from _layout_helper");
1651       jint min_size = Klass::instance_layout_helper(oopDesc::header_size(), false);
1652       // The key property of this type is that it folds up tests
1653       // for array-ness, since it proves that the layout_helper is positive.
1654       // Thus, a generic value like the basic object layout helper works fine.
1655       return TypeInt::make(min_size, max_jint, Type::WidenMin);
1656     }
1657   }
1658 
1659   // If we are loading from a freshly-allocated object, produce a zero,
1660   // if the load is provably beyond the header of the object.
1661   // (Also allow a variable load from a fresh array to produce zero.)
1662   const TypeOopPtr *tinst = tp->isa_oopptr();
1663   bool is_instance = (tinst != NULL) && tinst->is_known_instance_field();
1664   bool is_boxed_value = (tinst != NULL) && tinst->is_ptr_to_boxed_value();
1665   if (ReduceFieldZeroing || is_instance || is_boxed_value) {
1666     Node* value = can_see_stored_value(mem,phase);
1667     if (value != NULL && value->is_Con()) {
1668       assert(value->bottom_type()->higher_equal(_type),"sanity");
1669       return value->bottom_type();
1670     }
1671   }
1672 
1673   if (is_instance) {
1674     // If we have an instance type and our memory input is the
1675     // programs's initial memory state, there is no matching store,
1676     // so just return a zero of the appropriate type
1677     Node *mem = in(MemNode::Memory);
1678     if (mem->is_Parm() && mem->in(0)->is_Start()) {
1679       assert(mem->as_Parm()->_con == TypeFunc::Memory, "must be memory Parm");
1680       return Type::get_zero_type(_type->basic_type());
1681     }
1682   }
1683   return _type;
1684 }
1685 
1686 //------------------------------match_edge-------------------------------------
1687 // Do we Match on this edge index or not?  Match only the address.
1688 uint LoadNode::match_edge(uint idx) const {
1689   return idx == MemNode::Address;
1690 }
1691 
1692 //--------------------------LoadBNode::Ideal--------------------------------------
1693 //
1694 //  If the previous store is to the same address as this load,
1695 //  and the value stored was larger than a byte, replace this load
1696 //  with the value stored truncated to a byte.  If no truncation is
1697 //  needed, the replacement is done in LoadNode::Identity().
1698 //
1699 Node *LoadBNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1700   Node* mem = in(MemNode::Memory);
1701   Node* value = can_see_stored_value(mem,phase);
1702   if( value && !phase->type(value)->higher_equal( _type ) ) {
1703     Node *result = phase->transform( new LShiftINode(value, phase->intcon(24)) );
1704     return new RShiftINode(result, phase->intcon(24));
1705   }
1706   // Identity call will handle the case where truncation is not needed.
1707   return LoadNode::Ideal(phase, can_reshape);
1708 }
1709 
1710 const Type* LoadBNode::Value(PhaseTransform *phase) const {
1711   Node* mem = in(MemNode::Memory);
1712   Node* value = can_see_stored_value(mem,phase);
1713   if (value != NULL && value->is_Con() &&
1714       !value->bottom_type()->higher_equal(_type)) {
1715     // If the input to the store does not fit with the load's result type,
1716     // it must be truncated. We can't delay until Ideal call since
1717     // a singleton Value is needed for split_thru_phi optimization.
1718     int con = value->get_int();
1719     return TypeInt::make((con << 24) >> 24);
1720   }
1721   return LoadNode::Value(phase);
1722 }
1723 
1724 //--------------------------LoadUBNode::Ideal-------------------------------------
1725 //
1726 //  If the previous store is to the same address as this load,
1727 //  and the value stored was larger than a byte, replace this load
1728 //  with the value stored truncated to a byte.  If no truncation is
1729 //  needed, the replacement is done in LoadNode::Identity().
1730 //
1731 Node* LoadUBNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1732   Node* mem = in(MemNode::Memory);
1733   Node* value = can_see_stored_value(mem, phase);
1734   if (value && !phase->type(value)->higher_equal(_type))
1735     return new AndINode(value, phase->intcon(0xFF));
1736   // Identity call will handle the case where truncation is not needed.
1737   return LoadNode::Ideal(phase, can_reshape);
1738 }
1739 
1740 const Type* LoadUBNode::Value(PhaseTransform *phase) const {
1741   Node* mem = in(MemNode::Memory);
1742   Node* value = can_see_stored_value(mem,phase);
1743   if (value != NULL && value->is_Con() &&
1744       !value->bottom_type()->higher_equal(_type)) {
1745     // If the input to the store does not fit with the load's result type,
1746     // it must be truncated. We can't delay until Ideal call since
1747     // a singleton Value is needed for split_thru_phi optimization.
1748     int con = value->get_int();
1749     return TypeInt::make(con & 0xFF);
1750   }
1751   return LoadNode::Value(phase);
1752 }
1753 
1754 //--------------------------LoadUSNode::Ideal-------------------------------------
1755 //
1756 //  If the previous store is to the same address as this load,
1757 //  and the value stored was larger than a char, replace this load
1758 //  with the value stored truncated to a char.  If no truncation is
1759 //  needed, the replacement is done in LoadNode::Identity().
1760 //
1761 Node *LoadUSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1762   Node* mem = in(MemNode::Memory);
1763   Node* value = can_see_stored_value(mem,phase);
1764   if( value && !phase->type(value)->higher_equal( _type ) )
1765     return new AndINode(value,phase->intcon(0xFFFF));
1766   // Identity call will handle the case where truncation is not needed.
1767   return LoadNode::Ideal(phase, can_reshape);
1768 }
1769 
1770 const Type* LoadUSNode::Value(PhaseTransform *phase) const {
1771   Node* mem = in(MemNode::Memory);
1772   Node* value = can_see_stored_value(mem,phase);
1773   if (value != NULL && value->is_Con() &&
1774       !value->bottom_type()->higher_equal(_type)) {
1775     // If the input to the store does not fit with the load's result type,
1776     // it must be truncated. We can't delay until Ideal call since
1777     // a singleton Value is needed for split_thru_phi optimization.
1778     int con = value->get_int();
1779     return TypeInt::make(con & 0xFFFF);
1780   }
1781   return LoadNode::Value(phase);
1782 }
1783 
1784 //--------------------------LoadSNode::Ideal--------------------------------------
1785 //
1786 //  If the previous store is to the same address as this load,
1787 //  and the value stored was larger than a short, replace this load
1788 //  with the value stored truncated to a short.  If no truncation is
1789 //  needed, the replacement is done in LoadNode::Identity().
1790 //
1791 Node *LoadSNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1792   Node* mem = in(MemNode::Memory);
1793   Node* value = can_see_stored_value(mem,phase);
1794   if( value && !phase->type(value)->higher_equal( _type ) ) {
1795     Node *result = phase->transform( new LShiftINode(value, phase->intcon(16)) );
1796     return new RShiftINode(result, phase->intcon(16));
1797   }
1798   // Identity call will handle the case where truncation is not needed.
1799   return LoadNode::Ideal(phase, can_reshape);
1800 }
1801 
1802 const Type* LoadSNode::Value(PhaseTransform *phase) const {
1803   Node* mem = in(MemNode::Memory);
1804   Node* value = can_see_stored_value(mem,phase);
1805   if (value != NULL && value->is_Con() &&
1806       !value->bottom_type()->higher_equal(_type)) {
1807     // If the input to the store does not fit with the load's result type,
1808     // it must be truncated. We can't delay until Ideal call since
1809     // a singleton Value is needed for split_thru_phi optimization.
1810     int con = value->get_int();
1811     return TypeInt::make((con << 16) >> 16);
1812   }
1813   return LoadNode::Value(phase);
1814 }
1815 
1816 //=============================================================================
1817 //----------------------------LoadKlassNode::make------------------------------
1818 // Polymorphic factory method:
1819 Node* LoadKlassNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* at, const TypeKlassPtr* tk) {
1820   // sanity check the alias category against the created node type
1821   const TypePtr *adr_type = adr->bottom_type()->isa_ptr();
1822   assert(adr_type != NULL, "expecting TypeKlassPtr");
1823 #ifdef _LP64
1824   if (adr_type->is_ptr_to_narrowklass()) {
1825     assert(UseCompressedClassPointers, "no compressed klasses");
1826     Node* load_klass = gvn.transform(new LoadNKlassNode(ctl, mem, adr, at, tk->make_narrowklass(), MemNode::unordered));
1827     return new DecodeNKlassNode(load_klass, load_klass->bottom_type()->make_ptr());
1828   }
1829 #endif
1830   assert(!adr_type->is_ptr_to_narrowklass() && !adr_type->is_ptr_to_narrowoop(), "should have got back a narrow oop");
1831   return new LoadKlassNode(ctl, mem, adr, at, tk, MemNode::unordered);
1832 }
1833 
1834 //------------------------------Value------------------------------------------
1835 const Type *LoadKlassNode::Value( PhaseTransform *phase ) const {
1836   return klass_value_common(phase);
1837 }
1838 
1839 // In most cases, LoadKlassNode does not have the control input set. If the control
1840 // input is set, it must not be removed (by LoadNode::Ideal()).
1841 bool LoadKlassNode::can_remove_control() const {
1842   return false;
1843 }
1844 
1845 const Type *LoadNode::klass_value_common( PhaseTransform *phase ) const {
1846   // Either input is TOP ==> the result is TOP
1847   const Type *t1 = phase->type( in(MemNode::Memory) );
1848   if (t1 == Type::TOP)  return Type::TOP;
1849   Node *adr = in(MemNode::Address);
1850   const Type *t2 = phase->type( adr );
1851   if (t2 == Type::TOP)  return Type::TOP;
1852   const TypePtr *tp = t2->is_ptr();
1853   if (TypePtr::above_centerline(tp->ptr()) ||
1854       tp->ptr() == TypePtr::Null)  return Type::TOP;
1855 
1856   // Return a more precise klass, if possible
1857   const TypeInstPtr *tinst = tp->isa_instptr();
1858   if (tinst != NULL) {
1859     ciInstanceKlass* ik = tinst->klass()->as_instance_klass();
1860     int offset = tinst->offset();
1861     if (ik == phase->C->env()->Class_klass()
1862         && (offset == java_lang_Class::klass_offset_in_bytes() ||
1863             offset == java_lang_Class::array_klass_offset_in_bytes())) {
1864       // We are loading a special hidden field from a Class mirror object,
1865       // the field which points to the VM's Klass metaobject.
1866       ciType* t = tinst->java_mirror_type();
1867       // java_mirror_type returns non-null for compile-time Class constants.
1868       if (t != NULL) {
1869         // constant oop => constant klass
1870         if (offset == java_lang_Class::array_klass_offset_in_bytes()) {
1871           if (t->is_void()) {
1872             // We cannot create a void array.  Since void is a primitive type return null
1873             // klass.  Users of this result need to do a null check on the returned klass.
1874             return TypePtr::NULL_PTR;
1875           }
1876           return TypeKlassPtr::make(ciArrayKlass::make(t));
1877         }
1878         if (!t->is_klass()) {
1879           // a primitive Class (e.g., int.class) has NULL for a klass field
1880           return TypePtr::NULL_PTR;
1881         }
1882         // (Folds up the 1st indirection in aClassConstant.getModifiers().)
1883         return TypeKlassPtr::make(t->as_klass());
1884       }
1885       // non-constant mirror, so we can't tell what's going on
1886     }
1887     if( !ik->is_loaded() )
1888       return _type;             // Bail out if not loaded
1889     if (offset == oopDesc::klass_offset_in_bytes()) {
1890       if (tinst->klass_is_exact()) {
1891         return TypeKlassPtr::make(ik);
1892       }
1893       // See if we can become precise: no subklasses and no interface
1894       // (Note:  We need to support verified interfaces.)
1895       if (!ik->is_interface() && !ik->has_subklass()) {
1896         //assert(!UseExactTypes, "this code should be useless with exact types");
1897         // Add a dependence; if any subclass added we need to recompile
1898         if (!ik->is_final()) {
1899           // %%% should use stronger assert_unique_concrete_subtype instead
1900           phase->C->dependencies()->assert_leaf_type(ik);
1901         }
1902         // Return precise klass
1903         return TypeKlassPtr::make(ik);
1904       }
1905 
1906       // Return root of possible klass
1907       return TypeKlassPtr::make(TypePtr::NotNull, ik, 0/*offset*/);
1908     }
1909   }
1910 
1911   // Check for loading klass from an array
1912   const TypeAryPtr *tary = tp->isa_aryptr();
1913   if( tary != NULL ) {
1914     ciKlass *tary_klass = tary->klass();
1915     if (tary_klass != NULL   // can be NULL when at BOTTOM or TOP
1916         && tary->offset() == oopDesc::klass_offset_in_bytes()) {
1917       if (tary->klass_is_exact()) {
1918         return TypeKlassPtr::make(tary_klass);
1919       }
1920       ciArrayKlass *ak = tary->klass()->as_array_klass();
1921       // If the klass is an object array, we defer the question to the
1922       // array component klass.
1923       if( ak->is_obj_array_klass() ) {
1924         assert( ak->is_loaded(), "" );
1925         ciKlass *base_k = ak->as_obj_array_klass()->base_element_klass();
1926         if( base_k->is_loaded() && base_k->is_instance_klass() ) {
1927           ciInstanceKlass* ik = base_k->as_instance_klass();
1928           // See if we can become precise: no subklasses and no interface
1929           if (!ik->is_interface() && !ik->has_subklass()) {
1930             //assert(!UseExactTypes, "this code should be useless with exact types");
1931             // Add a dependence; if any subclass added we need to recompile
1932             if (!ik->is_final()) {
1933               phase->C->dependencies()->assert_leaf_type(ik);
1934             }
1935             // Return precise array klass
1936             return TypeKlassPtr::make(ak);
1937           }
1938         }
1939         return TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
1940       } else {                  // Found a type-array?
1941         //assert(!UseExactTypes, "this code should be useless with exact types");
1942         assert( ak->is_type_array_klass(), "" );
1943         return TypeKlassPtr::make(ak); // These are always precise
1944       }
1945     }
1946   }
1947 
1948   // Check for loading klass from an array klass
1949   const TypeKlassPtr *tkls = tp->isa_klassptr();
1950   if (tkls != NULL && !StressReflectiveCode) {
1951     ciKlass* klass = tkls->klass();
1952     if( !klass->is_loaded() )
1953       return _type;             // Bail out if not loaded
1954     if( klass->is_obj_array_klass() &&
1955         tkls->offset() == in_bytes(ObjArrayKlass::element_klass_offset())) {
1956       ciKlass* elem = klass->as_obj_array_klass()->element_klass();
1957       // // Always returning precise element type is incorrect,
1958       // // e.g., element type could be object and array may contain strings
1959       // return TypeKlassPtr::make(TypePtr::Constant, elem, 0);
1960 
1961       // The array's TypeKlassPtr was declared 'precise' or 'not precise'
1962       // according to the element type's subclassing.
1963       return TypeKlassPtr::make(tkls->ptr(), elem, 0/*offset*/);
1964     }
1965     if( klass->is_instance_klass() && tkls->klass_is_exact() &&
1966         tkls->offset() == in_bytes(Klass::super_offset())) {
1967       ciKlass* sup = klass->as_instance_klass()->super();
1968       // The field is Klass::_super.  Return its (constant) value.
1969       // (Folds up the 2nd indirection in aClassConstant.getSuperClass().)
1970       return sup ? TypeKlassPtr::make(sup) : TypePtr::NULL_PTR;
1971     }
1972   }
1973 
1974   // Bailout case
1975   return LoadNode::Value(phase);
1976 }
1977 
1978 //------------------------------Identity---------------------------------------
1979 // To clean up reflective code, simplify k.java_mirror.as_klass to plain k.
1980 // Also feed through the klass in Allocate(...klass...)._klass.
1981 Node* LoadKlassNode::Identity( PhaseTransform *phase ) {
1982   return klass_identity_common(phase);
1983 }
1984 
1985 Node* LoadNode::klass_identity_common(PhaseTransform *phase ) {
1986   Node* x = LoadNode::Identity(phase);
1987   if (x != this)  return x;
1988 
1989   // Take apart the address into an oop and and offset.
1990   // Return 'this' if we cannot.
1991   Node*    adr    = in(MemNode::Address);
1992   intptr_t offset = 0;
1993   Node*    base   = AddPNode::Ideal_base_and_offset(adr, phase, offset);
1994   if (base == NULL)     return this;
1995   const TypeOopPtr* toop = phase->type(adr)->isa_oopptr();
1996   if (toop == NULL)     return this;
1997 
1998   // We can fetch the klass directly through an AllocateNode.
1999   // This works even if the klass is not constant (clone or newArray).
2000   if (offset == oopDesc::klass_offset_in_bytes()) {
2001     Node* allocated_klass = AllocateNode::Ideal_klass(base, phase);
2002     if (allocated_klass != NULL) {
2003       return allocated_klass;
2004     }
2005   }
2006 
2007   // Simplify k.java_mirror.as_klass to plain k, where k is a Klass*.
2008   // See inline_native_Class_query for occurrences of these patterns.
2009   // Java Example:  x.getClass().isAssignableFrom(y)
2010   //
2011   // This improves reflective code, often making the Class
2012   // mirror go completely dead.  (Current exception:  Class
2013   // mirrors may appear in debug info, but we could clean them out by
2014   // introducing a new debug info operator for Klass*.java_mirror).
2015   if (toop->isa_instptr() && toop->klass() == phase->C->env()->Class_klass()
2016       && offset == java_lang_Class::klass_offset_in_bytes()) {
2017     // We are loading a special hidden field from a Class mirror,
2018     // the field which points to its Klass or ArrayKlass metaobject.
2019     if (base->is_Load()) {
2020       Node* adr2 = base->in(MemNode::Address);
2021       const TypeKlassPtr* tkls = phase->type(adr2)->isa_klassptr();
2022       if (tkls != NULL && !tkls->empty()
2023           && (tkls->klass()->is_instance_klass() ||
2024               tkls->klass()->is_array_klass())
2025           && adr2->is_AddP()
2026           ) {
2027         int mirror_field = in_bytes(Klass::java_mirror_offset());
2028         if (tkls->offset() == mirror_field) {
2029           return adr2->in(AddPNode::Base);
2030         }
2031       }
2032     }
2033   }
2034 
2035   return this;
2036 }
2037 
2038 
2039 //------------------------------Value------------------------------------------
2040 const Type *LoadNKlassNode::Value( PhaseTransform *phase ) const {
2041   const Type *t = klass_value_common(phase);
2042   if (t == Type::TOP)
2043     return t;
2044 
2045   return t->make_narrowklass();
2046 }
2047 
2048 //------------------------------Identity---------------------------------------
2049 // To clean up reflective code, simplify k.java_mirror.as_klass to narrow k.
2050 // Also feed through the klass in Allocate(...klass...)._klass.
2051 Node* LoadNKlassNode::Identity( PhaseTransform *phase ) {
2052   Node *x = klass_identity_common(phase);
2053 
2054   const Type *t = phase->type( x );
2055   if( t == Type::TOP ) return x;
2056   if( t->isa_narrowklass()) return x;
2057   assert (!t->isa_narrowoop(), "no narrow oop here");
2058 
2059   return phase->transform(new EncodePKlassNode(x, t->make_narrowklass()));
2060 }
2061 
2062 //------------------------------Value-----------------------------------------
2063 const Type *LoadRangeNode::Value( PhaseTransform *phase ) const {
2064   // Either input is TOP ==> the result is TOP
2065   const Type *t1 = phase->type( in(MemNode::Memory) );
2066   if( t1 == Type::TOP ) return Type::TOP;
2067   Node *adr = in(MemNode::Address);
2068   const Type *t2 = phase->type( adr );
2069   if( t2 == Type::TOP ) return Type::TOP;
2070   const TypePtr *tp = t2->is_ptr();
2071   if (TypePtr::above_centerline(tp->ptr()))  return Type::TOP;
2072   const TypeAryPtr *tap = tp->isa_aryptr();
2073   if( !tap ) return _type;
2074   return tap->size();
2075 }
2076 
2077 //-------------------------------Ideal---------------------------------------
2078 // Feed through the length in AllocateArray(...length...)._length.
2079 Node *LoadRangeNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2080   Node* p = MemNode::Ideal_common(phase, can_reshape);
2081   if (p)  return (p == NodeSentinel) ? NULL : p;
2082 
2083   // Take apart the address into an oop and and offset.
2084   // Return 'this' if we cannot.
2085   Node*    adr    = in(MemNode::Address);
2086   intptr_t offset = 0;
2087   Node*    base   = AddPNode::Ideal_base_and_offset(adr, phase,  offset);
2088   if (base == NULL)     return NULL;
2089   const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
2090   if (tary == NULL)     return NULL;
2091 
2092   // We can fetch the length directly through an AllocateArrayNode.
2093   // This works even if the length is not constant (clone or newArray).
2094   if (offset == arrayOopDesc::length_offset_in_bytes()) {
2095     AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
2096     if (alloc != NULL) {
2097       Node* allocated_length = alloc->Ideal_length();
2098       Node* len = alloc->make_ideal_length(tary, phase);
2099       if (allocated_length != len) {
2100         // New CastII improves on this.
2101         return len;
2102       }
2103     }
2104   }
2105 
2106   return NULL;
2107 }
2108 
2109 //------------------------------Identity---------------------------------------
2110 // Feed through the length in AllocateArray(...length...)._length.
2111 Node* LoadRangeNode::Identity( PhaseTransform *phase ) {
2112   Node* x = LoadINode::Identity(phase);
2113   if (x != this)  return x;
2114 
2115   // Take apart the address into an oop and and offset.
2116   // Return 'this' if we cannot.
2117   Node*    adr    = in(MemNode::Address);
2118   intptr_t offset = 0;
2119   Node*    base   = AddPNode::Ideal_base_and_offset(adr, phase, offset);
2120   if (base == NULL)     return this;
2121   const TypeAryPtr* tary = phase->type(adr)->isa_aryptr();
2122   if (tary == NULL)     return this;
2123 
2124   // We can fetch the length directly through an AllocateArrayNode.
2125   // This works even if the length is not constant (clone or newArray).
2126   if (offset == arrayOopDesc::length_offset_in_bytes()) {
2127     AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(base, phase);
2128     if (alloc != NULL) {
2129       Node* allocated_length = alloc->Ideal_length();
2130       // Do not allow make_ideal_length to allocate a CastII node.
2131       Node* len = alloc->make_ideal_length(tary, phase, false);
2132       if (allocated_length == len) {
2133         // Return allocated_length only if it would not be improved by a CastII.
2134         return allocated_length;
2135       }
2136     }
2137   }
2138 
2139   return this;
2140 
2141 }
2142 
2143 //=============================================================================
2144 //---------------------------StoreNode::make-----------------------------------
2145 // Polymorphic factory method:
2146 StoreNode* StoreNode::make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, BasicType bt, MemOrd mo) {
2147   assert((mo == unordered || mo == release), "unexpected");
2148   Compile* C = gvn.C;
2149   assert(C->get_alias_index(adr_type) != Compile::AliasIdxRaw ||
2150          ctl != NULL, "raw memory operations should have control edge");
2151 
2152   switch (bt) {
2153   case T_BOOLEAN:
2154   case T_BYTE:    return new StoreBNode(ctl, mem, adr, adr_type, val, mo);
2155   case T_INT:     return new StoreINode(ctl, mem, adr, adr_type, val, mo);
2156   case T_CHAR:
2157   case T_SHORT:   return new StoreCNode(ctl, mem, adr, adr_type, val, mo);
2158   case T_LONG:    return new StoreLNode(ctl, mem, adr, adr_type, val, mo);
2159   case T_FLOAT:   return new StoreFNode(ctl, mem, adr, adr_type, val, mo);
2160   case T_DOUBLE:  return new StoreDNode(ctl, mem, adr, adr_type, val, mo);
2161   case T_METADATA:
2162   case T_ADDRESS:
2163   case T_OBJECT:
2164 #ifdef _LP64
2165     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2166       val = gvn.transform(new EncodePNode(val, val->bottom_type()->make_narrowoop()));
2167       return new StoreNNode(ctl, mem, adr, adr_type, val, mo);
2168     } else if (adr->bottom_type()->is_ptr_to_narrowklass() ||
2169                (UseCompressedClassPointers && val->bottom_type()->isa_klassptr() &&
2170                 adr->bottom_type()->isa_rawptr())) {
2171       val = gvn.transform(new EncodePKlassNode(val, val->bottom_type()->make_narrowklass()));
2172       return new StoreNKlassNode(ctl, mem, adr, adr_type, val, mo);
2173     }
2174 #endif
2175     {
2176       return new StorePNode(ctl, mem, adr, adr_type, val, mo);
2177     }
2178   }
2179   ShouldNotReachHere();
2180   return (StoreNode*)NULL;
2181 }
2182 
2183 StoreLNode* StoreLNode::make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo) {
2184   bool require_atomic = true;
2185   return new StoreLNode(ctl, mem, adr, adr_type, val, mo, require_atomic);
2186 }
2187 
2188 StoreDNode* StoreDNode::make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo) {
2189   bool require_atomic = true;
2190   return new StoreDNode(ctl, mem, adr, adr_type, val, mo, require_atomic);
2191 }
2192 
2193 
2194 //--------------------------bottom_type----------------------------------------
2195 const Type *StoreNode::bottom_type() const {
2196   return Type::MEMORY;
2197 }
2198 
2199 //------------------------------hash-------------------------------------------
2200 uint StoreNode::hash() const {
2201   // unroll addition of interesting fields
2202   //return (uintptr_t)in(Control) + (uintptr_t)in(Memory) + (uintptr_t)in(Address) + (uintptr_t)in(ValueIn);
2203 
2204   // Since they are not commoned, do not hash them:
2205   return NO_HASH;
2206 }
2207 
2208 //------------------------------Ideal------------------------------------------
2209 // Change back-to-back Store(, p, x) -> Store(m, p, y) to Store(m, p, x).
2210 // When a store immediately follows a relevant allocation/initialization,
2211 // try to capture it into the initialization, or hoist it above.
2212 Node *StoreNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2213   Node* p = MemNode::Ideal_common(phase, can_reshape);
2214   if (p)  return (p == NodeSentinel) ? NULL : p;
2215 
2216   Node* mem     = in(MemNode::Memory);
2217   Node* address = in(MemNode::Address);
2218 
2219   // Back-to-back stores to same address?  Fold em up.  Generally
2220   // unsafe if I have intervening uses...  Also disallowed for StoreCM
2221   // since they must follow each StoreP operation.  Redundant StoreCMs
2222   // are eliminated just before matching in final_graph_reshape.
2223   if (mem->is_Store() && mem->in(MemNode::Address)->eqv_uncast(address) &&
2224       mem->Opcode() != Op_StoreCM) {
2225     // Looking at a dead closed cycle of memory?
2226     assert(mem != mem->in(MemNode::Memory), "dead loop in StoreNode::Ideal");
2227 
2228     assert(Opcode() == mem->Opcode() ||
2229            phase->C->get_alias_index(adr_type()) == Compile::AliasIdxRaw,
2230            "no mismatched stores, except on raw memory");
2231 
2232     if (mem->outcnt() == 1 &&           // check for intervening uses
2233         mem->as_Store()->memory_size() <= this->memory_size()) {
2234       // If anybody other than 'this' uses 'mem', we cannot fold 'mem' away.
2235       // For example, 'mem' might be the final state at a conditional return.
2236       // Or, 'mem' might be used by some node which is live at the same time
2237       // 'this' is live, which might be unschedulable.  So, require exactly
2238       // ONE user, the 'this' store, until such time as we clone 'mem' for
2239       // each of 'mem's uses (thus making the exactly-1-user-rule hold true).
2240       if (can_reshape) {  // (%%% is this an anachronism?)
2241         set_req_X(MemNode::Memory, mem->in(MemNode::Memory),
2242                   phase->is_IterGVN());
2243       } else {
2244         // It's OK to do this in the parser, since DU info is always accurate,
2245         // and the parser always refers to nodes via SafePointNode maps.
2246         set_req(MemNode::Memory, mem->in(MemNode::Memory));
2247       }
2248       return this;
2249     }
2250   }
2251 
2252   // Capture an unaliased, unconditional, simple store into an initializer.
2253   // Or, if it is independent of the allocation, hoist it above the allocation.
2254   if (ReduceFieldZeroing && /*can_reshape &&*/
2255       mem->is_Proj() && mem->in(0)->is_Initialize()) {
2256     InitializeNode* init = mem->in(0)->as_Initialize();
2257     intptr_t offset = init->can_capture_store(this, phase, can_reshape);
2258     if (offset > 0) {
2259       Node* moved = init->capture_store(this, offset, phase, can_reshape);
2260       // If the InitializeNode captured me, it made a raw copy of me,
2261       // and I need to disappear.
2262       if (moved != NULL) {
2263         // %%% hack to ensure that Ideal returns a new node:
2264         mem = MergeMemNode::make(mem);
2265         return mem;             // fold me away
2266       }
2267     }
2268   }
2269 
2270   return NULL;                  // No further progress
2271 }
2272 
2273 //------------------------------Value-----------------------------------------
2274 const Type *StoreNode::Value( PhaseTransform *phase ) const {
2275   // Either input is TOP ==> the result is TOP
2276   const Type *t1 = phase->type( in(MemNode::Memory) );
2277   if( t1 == Type::TOP ) return Type::TOP;
2278   const Type *t2 = phase->type( in(MemNode::Address) );
2279   if( t2 == Type::TOP ) return Type::TOP;
2280   const Type *t3 = phase->type( in(MemNode::ValueIn) );
2281   if( t3 == Type::TOP ) return Type::TOP;
2282   return Type::MEMORY;
2283 }
2284 
2285 //------------------------------Identity---------------------------------------
2286 // Remove redundant stores:
2287 //   Store(m, p, Load(m, p)) changes to m.
2288 //   Store(, p, x) -> Store(m, p, x) changes to Store(m, p, x).
2289 Node *StoreNode::Identity( PhaseTransform *phase ) {
2290   Node* mem = in(MemNode::Memory);
2291   Node* adr = in(MemNode::Address);
2292   Node* val = in(MemNode::ValueIn);
2293 
2294   // Load then Store?  Then the Store is useless
2295   if (val->is_Load() &&
2296       val->in(MemNode::Address)->eqv_uncast(adr) &&
2297       val->in(MemNode::Memory )->eqv_uncast(mem) &&
2298       val->as_Load()->store_Opcode() == Opcode()) {
2299     return mem;
2300   }
2301 
2302   // Two stores in a row of the same value?
2303   if (mem->is_Store() &&
2304       mem->in(MemNode::Address)->eqv_uncast(adr) &&
2305       mem->in(MemNode::ValueIn)->eqv_uncast(val) &&
2306       mem->Opcode() == Opcode()) {
2307     return mem;
2308   }
2309 
2310   // Store of zero anywhere into a freshly-allocated object?
2311   // Then the store is useless.
2312   // (It must already have been captured by the InitializeNode.)
2313   if (ReduceFieldZeroing && phase->type(val)->is_zero_type()) {
2314     // a newly allocated object is already all-zeroes everywhere
2315     if (mem->is_Proj() && mem->in(0)->is_Allocate()) {
2316       return mem;
2317     }
2318 
2319     // the store may also apply to zero-bits in an earlier object
2320     Node* prev_mem = find_previous_store(phase);
2321     // Steps (a), (b):  Walk past independent stores to find an exact match.
2322     if (prev_mem != NULL) {
2323       Node* prev_val = can_see_stored_value(prev_mem, phase);
2324       if (prev_val != NULL && phase->eqv(prev_val, val)) {
2325         // prev_val and val might differ by a cast; it would be good
2326         // to keep the more informative of the two.
2327         return mem;
2328       }
2329     }
2330   }
2331 
2332   return this;
2333 }
2334 
2335 //------------------------------match_edge-------------------------------------
2336 // Do we Match on this edge index or not?  Match only memory & value
2337 uint StoreNode::match_edge(uint idx) const {
2338   return idx == MemNode::Address || idx == MemNode::ValueIn;
2339 }
2340 
2341 //------------------------------cmp--------------------------------------------
2342 // Do not common stores up together.  They generally have to be split
2343 // back up anyways, so do not bother.
2344 uint StoreNode::cmp( const Node &n ) const {
2345   return (&n == this);          // Always fail except on self
2346 }
2347 
2348 //------------------------------Ideal_masked_input-----------------------------
2349 // Check for a useless mask before a partial-word store
2350 // (StoreB ... (AndI valIn conIa) )
2351 // If (conIa & mask == mask) this simplifies to
2352 // (StoreB ... (valIn) )
2353 Node *StoreNode::Ideal_masked_input(PhaseGVN *phase, uint mask) {
2354   Node *val = in(MemNode::ValueIn);
2355   if( val->Opcode() == Op_AndI ) {
2356     const TypeInt *t = phase->type( val->in(2) )->isa_int();
2357     if( t && t->is_con() && (t->get_con() & mask) == mask ) {
2358       set_req(MemNode::ValueIn, val->in(1));
2359       return this;
2360     }
2361   }
2362   return NULL;
2363 }
2364 
2365 
2366 //------------------------------Ideal_sign_extended_input----------------------
2367 // Check for useless sign-extension before a partial-word store
2368 // (StoreB ... (RShiftI _ (LShiftI _ valIn conIL ) conIR) )
2369 // If (conIL == conIR && conIR <= num_bits)  this simplifies to
2370 // (StoreB ... (valIn) )
2371 Node *StoreNode::Ideal_sign_extended_input(PhaseGVN *phase, int num_bits) {
2372   Node *val = in(MemNode::ValueIn);
2373   if( val->Opcode() == Op_RShiftI ) {
2374     const TypeInt *t = phase->type( val->in(2) )->isa_int();
2375     if( t && t->is_con() && (t->get_con() <= num_bits) ) {
2376       Node *shl = val->in(1);
2377       if( shl->Opcode() == Op_LShiftI ) {
2378         const TypeInt *t2 = phase->type( shl->in(2) )->isa_int();
2379         if( t2 && t2->is_con() && (t2->get_con() == t->get_con()) ) {
2380           set_req(MemNode::ValueIn, shl->in(1));
2381           return this;
2382         }
2383       }
2384     }
2385   }
2386   return NULL;
2387 }
2388 
2389 //------------------------------value_never_loaded-----------------------------------
2390 // Determine whether there are any possible loads of the value stored.
2391 // For simplicity, we actually check if there are any loads from the
2392 // address stored to, not just for loads of the value stored by this node.
2393 //
2394 bool StoreNode::value_never_loaded( PhaseTransform *phase) const {
2395   Node *adr = in(Address);
2396   const TypeOopPtr *adr_oop = phase->type(adr)->isa_oopptr();
2397   if (adr_oop == NULL)
2398     return false;
2399   if (!adr_oop->is_known_instance_field())
2400     return false; // if not a distinct instance, there may be aliases of the address
2401   for (DUIterator_Fast imax, i = adr->fast_outs(imax); i < imax; i++) {
2402     Node *use = adr->fast_out(i);
2403     if (use->is_Load() || use->is_LoadStore()) {
2404       return false;
2405     }
2406   }
2407   return true;
2408 }
2409 
2410 //=============================================================================
2411 //------------------------------Ideal------------------------------------------
2412 // If the store is from an AND mask that leaves the low bits untouched, then
2413 // we can skip the AND operation.  If the store is from a sign-extension
2414 // (a left shift, then right shift) we can skip both.
2415 Node *StoreBNode::Ideal(PhaseGVN *phase, bool can_reshape){
2416   Node *progress = StoreNode::Ideal_masked_input(phase, 0xFF);
2417   if( progress != NULL ) return progress;
2418 
2419   progress = StoreNode::Ideal_sign_extended_input(phase, 24);
2420   if( progress != NULL ) return progress;
2421 
2422   // Finally check the default case
2423   return StoreNode::Ideal(phase, can_reshape);
2424 }
2425 
2426 //=============================================================================
2427 //------------------------------Ideal------------------------------------------
2428 // If the store is from an AND mask that leaves the low bits untouched, then
2429 // we can skip the AND operation
2430 Node *StoreCNode::Ideal(PhaseGVN *phase, bool can_reshape){
2431   Node *progress = StoreNode::Ideal_masked_input(phase, 0xFFFF);
2432   if( progress != NULL ) return progress;
2433 
2434   progress = StoreNode::Ideal_sign_extended_input(phase, 16);
2435   if( progress != NULL ) return progress;
2436 
2437   // Finally check the default case
2438   return StoreNode::Ideal(phase, can_reshape);
2439 }
2440 
2441 //=============================================================================
2442 //------------------------------Identity---------------------------------------
2443 Node *StoreCMNode::Identity( PhaseTransform *phase ) {
2444   // No need to card mark when storing a null ptr
2445   Node* my_store = in(MemNode::OopStore);
2446   if (my_store->is_Store()) {
2447     const Type *t1 = phase->type( my_store->in(MemNode::ValueIn) );
2448     if( t1 == TypePtr::NULL_PTR ) {
2449       return in(MemNode::Memory);
2450     }
2451   }
2452   return this;
2453 }
2454 
2455 //=============================================================================
2456 //------------------------------Ideal---------------------------------------
2457 Node *StoreCMNode::Ideal(PhaseGVN *phase, bool can_reshape){
2458   Node* progress = StoreNode::Ideal(phase, can_reshape);
2459   if (progress != NULL) return progress;
2460 
2461   Node* my_store = in(MemNode::OopStore);
2462   if (my_store->is_MergeMem()) {
2463     Node* mem = my_store->as_MergeMem()->memory_at(oop_alias_idx());
2464     set_req(MemNode::OopStore, mem);
2465     return this;
2466   }
2467 
2468   return NULL;
2469 }
2470 
2471 //------------------------------Value-----------------------------------------
2472 const Type *StoreCMNode::Value( PhaseTransform *phase ) const {
2473   // Either input is TOP ==> the result is TOP
2474   const Type *t = phase->type( in(MemNode::Memory) );
2475   if( t == Type::TOP ) return Type::TOP;
2476   t = phase->type( in(MemNode::Address) );
2477   if( t == Type::TOP ) return Type::TOP;
2478   t = phase->type( in(MemNode::ValueIn) );
2479   if( t == Type::TOP ) return Type::TOP;
2480   // If extra input is TOP ==> the result is TOP
2481   t = phase->type( in(MemNode::OopStore) );
2482   if( t == Type::TOP ) return Type::TOP;
2483 
2484   return StoreNode::Value( phase );
2485 }
2486 
2487 
2488 //=============================================================================
2489 //----------------------------------SCMemProjNode------------------------------
2490 const Type * SCMemProjNode::Value( PhaseTransform *phase ) const
2491 {
2492   return bottom_type();
2493 }
2494 
2495 //=============================================================================
2496 //----------------------------------LoadStoreNode------------------------------
2497 LoadStoreNode::LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required )
2498   : Node(required),
2499     _type(rt),
2500     _adr_type(at)
2501 {
2502   init_req(MemNode::Control, c  );
2503   init_req(MemNode::Memory , mem);
2504   init_req(MemNode::Address, adr);
2505   init_req(MemNode::ValueIn, val);
2506   init_class_id(Class_LoadStore);
2507 }
2508 
2509 uint LoadStoreNode::ideal_reg() const {
2510   return _type->ideal_reg();
2511 }
2512 
2513 bool LoadStoreNode::result_not_used() const {
2514   for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) {
2515     Node *x = fast_out(i);
2516     if (x->Opcode() == Op_SCMemProj) continue;
2517     return false;
2518   }
2519   return true;
2520 }
2521 
2522 uint LoadStoreNode::size_of() const { return sizeof(*this); }
2523 
2524 //=============================================================================
2525 //----------------------------------LoadStoreConditionalNode--------------------
2526 LoadStoreConditionalNode::LoadStoreConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex ) : LoadStoreNode(c, mem, adr, val, NULL, TypeInt::BOOL, 5) {
2527   init_req(ExpectedIn, ex );
2528 }
2529 
2530 //=============================================================================
2531 //-------------------------------adr_type--------------------------------------
2532 // Do we Match on this edge index or not?  Do not match memory
2533 const TypePtr* ClearArrayNode::adr_type() const {
2534   Node *adr = in(3);
2535   if (adr == NULL)  return NULL; // node is dead
2536   return MemNode::calculate_adr_type(adr->bottom_type());
2537 }
2538 
2539 //------------------------------match_edge-------------------------------------
2540 // Do we Match on this edge index or not?  Do not match memory
2541 uint ClearArrayNode::match_edge(uint idx) const {
2542   return idx > 1;
2543 }
2544 
2545 //------------------------------Identity---------------------------------------
2546 // Clearing a zero length array does nothing
2547 Node *ClearArrayNode::Identity( PhaseTransform *phase ) {
2548   return phase->type(in(2))->higher_equal(TypeX::ZERO)  ? in(1) : this;
2549 }
2550 
2551 //------------------------------Idealize---------------------------------------
2552 // Clearing a short array is faster with stores
2553 Node *ClearArrayNode::Ideal(PhaseGVN *phase, bool can_reshape){
2554   const int unit = BytesPerLong;
2555   const TypeX* t = phase->type(in(2))->isa_intptr_t();
2556   if (!t)  return NULL;
2557   if (!t->is_con())  return NULL;
2558   intptr_t raw_count = t->get_con();
2559   intptr_t size = raw_count;
2560   if (!Matcher::init_array_count_is_in_bytes) size *= unit;
2561   // Clearing nothing uses the Identity call.
2562   // Negative clears are possible on dead ClearArrays
2563   // (see jck test stmt114.stmt11402.val).
2564   if (size <= 0 || size % unit != 0)  return NULL;
2565   intptr_t count = size / unit;
2566   // Length too long; use fast hardware clear
2567   if (size > Matcher::init_array_short_size)  return NULL;
2568   Node *mem = in(1);
2569   if( phase->type(mem)==Type::TOP ) return NULL;
2570   Node *adr = in(3);
2571   const Type* at = phase->type(adr);
2572   if( at==Type::TOP ) return NULL;
2573   const TypePtr* atp = at->isa_ptr();
2574   // adjust atp to be the correct array element address type
2575   if (atp == NULL)  atp = TypePtr::BOTTOM;
2576   else              atp = atp->add_offset(Type::OffsetBot);
2577   // Get base for derived pointer purposes
2578   if( adr->Opcode() != Op_AddP ) Unimplemented();
2579   Node *base = adr->in(1);
2580 
2581   Node *zero = phase->makecon(TypeLong::ZERO);
2582   Node *off  = phase->MakeConX(BytesPerLong);
2583   mem = new StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
2584   count--;
2585   while( count-- ) {
2586     mem = phase->transform(mem);
2587     adr = phase->transform(new AddPNode(base,adr,off));
2588     mem = new StoreLNode(in(0),mem,adr,atp,zero,MemNode::unordered,false);
2589   }
2590   return mem;
2591 }
2592 
2593 //----------------------------step_through----------------------------------
2594 // Return allocation input memory edge if it is different instance
2595 // or itself if it is the one we are looking for.
2596 bool ClearArrayNode::step_through(Node** np, uint instance_id, PhaseTransform* phase) {
2597   Node* n = *np;
2598   assert(n->is_ClearArray(), "sanity");
2599   intptr_t offset;
2600   AllocateNode* alloc = AllocateNode::Ideal_allocation(n->in(3), phase, offset);
2601   // This method is called only before Allocate nodes are expanded
2602   // during macro nodes expansion. Before that ClearArray nodes are
2603   // only generated in PhaseMacroExpand::generate_arraycopy() (before
2604   // Allocate nodes are expanded) which follows allocations.
2605   assert(alloc != NULL, "should have allocation");
2606   if (alloc->_idx == instance_id) {
2607     // Can not bypass initialization of the instance we are looking for.
2608     return false;
2609   }
2610   // Otherwise skip it.
2611   InitializeNode* init = alloc->initialization();
2612   if (init != NULL)
2613     *np = init->in(TypeFunc::Memory);
2614   else
2615     *np = alloc->in(TypeFunc::Memory);
2616   return true;
2617 }
2618 
2619 //----------------------------clear_memory-------------------------------------
2620 // Generate code to initialize object storage to zero.
2621 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2622                                    intptr_t start_offset,
2623                                    Node* end_offset,
2624                                    PhaseGVN* phase) {
2625   intptr_t offset = start_offset;
2626 
2627   int unit = BytesPerLong;
2628   if ((offset % unit) != 0) {
2629     Node* adr = new AddPNode(dest, dest, phase->MakeConX(offset));
2630     adr = phase->transform(adr);
2631     const TypePtr* atp = TypeRawPtr::BOTTOM;
2632     mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
2633     mem = phase->transform(mem);
2634     offset += BytesPerInt;
2635   }
2636   assert((offset % unit) == 0, "");
2637 
2638   // Initialize the remaining stuff, if any, with a ClearArray.
2639   return clear_memory(ctl, mem, dest, phase->MakeConX(offset), end_offset, phase);
2640 }
2641 
2642 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2643                                    Node* start_offset,
2644                                    Node* end_offset,
2645                                    PhaseGVN* phase) {
2646   if (start_offset == end_offset) {
2647     // nothing to do
2648     return mem;
2649   }
2650 
2651   int unit = BytesPerLong;
2652   Node* zbase = start_offset;
2653   Node* zend  = end_offset;
2654 
2655   // Scale to the unit required by the CPU:
2656   if (!Matcher::init_array_count_is_in_bytes) {
2657     Node* shift = phase->intcon(exact_log2(unit));
2658     zbase = phase->transform(new URShiftXNode(zbase, shift) );
2659     zend  = phase->transform(new URShiftXNode(zend,  shift) );
2660   }
2661 
2662   // Bulk clear double-words
2663   Node* zsize = phase->transform(new SubXNode(zend, zbase) );
2664   Node* adr = phase->transform(new AddPNode(dest, dest, start_offset) );
2665   mem = new ClearArrayNode(ctl, mem, zsize, adr);
2666   return phase->transform(mem);
2667 }
2668 
2669 Node* ClearArrayNode::clear_memory(Node* ctl, Node* mem, Node* dest,
2670                                    intptr_t start_offset,
2671                                    intptr_t end_offset,
2672                                    PhaseGVN* phase) {
2673   if (start_offset == end_offset) {
2674     // nothing to do
2675     return mem;
2676   }
2677 
2678   assert((end_offset % BytesPerInt) == 0, "odd end offset");
2679   intptr_t done_offset = end_offset;
2680   if ((done_offset % BytesPerLong) != 0) {
2681     done_offset -= BytesPerInt;
2682   }
2683   if (done_offset > start_offset) {
2684     mem = clear_memory(ctl, mem, dest,
2685                        start_offset, phase->MakeConX(done_offset), phase);
2686   }
2687   if (done_offset < end_offset) { // emit the final 32-bit store
2688     Node* adr = new AddPNode(dest, dest, phase->MakeConX(done_offset));
2689     adr = phase->transform(adr);
2690     const TypePtr* atp = TypeRawPtr::BOTTOM;
2691     mem = StoreNode::make(*phase, ctl, mem, adr, atp, phase->zerocon(T_INT), T_INT, MemNode::unordered);
2692     mem = phase->transform(mem);
2693     done_offset += BytesPerInt;
2694   }
2695   assert(done_offset == end_offset, "");
2696   return mem;
2697 }
2698 
2699 //=============================================================================
2700 MemBarNode::MemBarNode(Compile* C, int alias_idx, Node* precedent)
2701   : MultiNode(TypeFunc::Parms + (precedent == NULL? 0: 1)),
2702     _adr_type(C->get_adr_type(alias_idx))
2703 {
2704   init_class_id(Class_MemBar);
2705   Node* top = C->top();
2706   init_req(TypeFunc::I_O,top);
2707   init_req(TypeFunc::FramePtr,top);
2708   init_req(TypeFunc::ReturnAdr,top);
2709   if (precedent != NULL)
2710     init_req(TypeFunc::Parms, precedent);
2711 }
2712 
2713 //------------------------------cmp--------------------------------------------
2714 uint MemBarNode::hash() const { return NO_HASH; }
2715 uint MemBarNode::cmp( const Node &n ) const {
2716   return (&n == this);          // Always fail except on self
2717 }
2718 
2719 //------------------------------make-------------------------------------------
2720 MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
2721   switch (opcode) {
2722   case Op_MemBarAcquire:     return new MemBarAcquireNode(C, atp, pn);
2723   case Op_LoadFence:         return new LoadFenceNode(C, atp, pn);
2724   case Op_MemBarRelease:     return new MemBarReleaseNode(C, atp, pn);
2725   case Op_StoreFence:        return new StoreFenceNode(C, atp, pn);
2726   case Op_MemBarAcquireLock: return new MemBarAcquireLockNode(C, atp, pn);
2727   case Op_MemBarReleaseLock: return new MemBarReleaseLockNode(C, atp, pn);
2728   case Op_MemBarVolatile:    return new MemBarVolatileNode(C, atp, pn);
2729   case Op_MemBarCPUOrder:    return new MemBarCPUOrderNode(C, atp, pn);
2730   case Op_Initialize:        return new InitializeNode(C, atp, pn);
2731   case Op_MemBarStoreStore:  return new MemBarStoreStoreNode(C, atp, pn);
2732   default: ShouldNotReachHere(); return NULL;
2733   }
2734 }
2735 
2736 //------------------------------Ideal------------------------------------------
2737 // Return a node which is more "ideal" than the current node.  Strip out
2738 // control copies
2739 Node *MemBarNode::Ideal(PhaseGVN *phase, bool can_reshape) {
2740   if (remove_dead_region(phase, can_reshape)) return this;
2741   // Don't bother trying to transform a dead node
2742   if (in(0) && in(0)->is_top()) {
2743     return NULL;
2744   }
2745 
2746   bool progress = false;
2747   // Eliminate volatile MemBars for scalar replaced objects.
2748   if (can_reshape && req() == (Precedent+1)) {
2749     bool eliminate = false;
2750     int opc = Opcode();
2751     if ((opc == Op_MemBarAcquire || opc == Op_MemBarVolatile)) {
2752       // Volatile field loads and stores.
2753       Node* my_mem = in(MemBarNode::Precedent);
2754       // The MembarAquire may keep an unused LoadNode alive through the Precedent edge
2755       if ((my_mem != NULL) && (opc == Op_MemBarAcquire) && (my_mem->outcnt() == 1)) {
2756         // if the Precedent is a decodeN and its input (a Load) is used at more than one place,
2757         // replace this Precedent (decodeN) with the Load instead.
2758         if ((my_mem->Opcode() == Op_DecodeN) && (my_mem->in(1)->outcnt() > 1))  {
2759           Node* load_node = my_mem->in(1);
2760           set_req(MemBarNode::Precedent, load_node);
2761           phase->is_IterGVN()->_worklist.push(my_mem);
2762           my_mem = load_node;
2763         } else {
2764           assert(my_mem->unique_out() == this, "sanity");
2765           del_req(Precedent);
2766           phase->is_IterGVN()->_worklist.push(my_mem); // remove dead node later
2767           my_mem = NULL;
2768         }
2769         progress = true;
2770       }
2771       if (my_mem != NULL && my_mem->is_Mem()) {
2772         const TypeOopPtr* t_oop = my_mem->in(MemNode::Address)->bottom_type()->isa_oopptr();
2773         // Check for scalar replaced object reference.
2774         if( t_oop != NULL && t_oop->is_known_instance_field() &&
2775             t_oop->offset() != Type::OffsetBot &&
2776             t_oop->offset() != Type::OffsetTop) {
2777           eliminate = true;
2778         }
2779       }
2780     } else if (opc == Op_MemBarRelease) {
2781       // Final field stores.
2782       Node* alloc = AllocateNode::Ideal_allocation(in(MemBarNode::Precedent), phase);
2783       if ((alloc != NULL) && alloc->is_Allocate() &&
2784           alloc->as_Allocate()->_is_non_escaping) {
2785         // The allocated object does not escape.
2786         eliminate = true;
2787       }
2788     }
2789     if (eliminate) {
2790       // Replace MemBar projections by its inputs.
2791       PhaseIterGVN* igvn = phase->is_IterGVN();
2792       igvn->replace_node(proj_out(TypeFunc::Memory), in(TypeFunc::Memory));
2793       igvn->replace_node(proj_out(TypeFunc::Control), in(TypeFunc::Control));
2794       // Must return either the original node (now dead) or a new node
2795       // (Do not return a top here, since that would break the uniqueness of top.)
2796       return new ConINode(TypeInt::ZERO);
2797     }
2798   }
2799   return progress ? this : NULL;
2800 }
2801 
2802 //------------------------------Value------------------------------------------
2803 const Type *MemBarNode::Value( PhaseTransform *phase ) const {
2804   if( !in(0) ) return Type::TOP;
2805   if( phase->type(in(0)) == Type::TOP )
2806     return Type::TOP;
2807   return TypeTuple::MEMBAR;
2808 }
2809 
2810 //------------------------------match------------------------------------------
2811 // Construct projections for memory.
2812 Node *MemBarNode::match( const ProjNode *proj, const Matcher *m ) {
2813   switch (proj->_con) {
2814   case TypeFunc::Control:
2815   case TypeFunc::Memory:
2816     return new MachProjNode(this,proj->_con,RegMask::Empty,MachProjNode::unmatched_proj);
2817   }
2818   ShouldNotReachHere();
2819   return NULL;
2820 }
2821 
2822 //===========================InitializeNode====================================
2823 // SUMMARY:
2824 // This node acts as a memory barrier on raw memory, after some raw stores.
2825 // The 'cooked' oop value feeds from the Initialize, not the Allocation.
2826 // The Initialize can 'capture' suitably constrained stores as raw inits.
2827 // It can coalesce related raw stores into larger units (called 'tiles').
2828 // It can avoid zeroing new storage for memory units which have raw inits.
2829 // At macro-expansion, it is marked 'complete', and does not optimize further.
2830 //
2831 // EXAMPLE:
2832 // The object 'new short[2]' occupies 16 bytes in a 32-bit machine.
2833 //   ctl = incoming control; mem* = incoming memory
2834 // (Note:  A star * on a memory edge denotes I/O and other standard edges.)
2835 // First allocate uninitialized memory and fill in the header:
2836 //   alloc = (Allocate ctl mem* 16 #short[].klass ...)
2837 //   ctl := alloc.Control; mem* := alloc.Memory*
2838 //   rawmem = alloc.Memory; rawoop = alloc.RawAddress
2839 // Then initialize to zero the non-header parts of the raw memory block:
2840 //   init = (Initialize alloc.Control alloc.Memory* alloc.RawAddress)
2841 //   ctl := init.Control; mem.SLICE(#short[*]) := init.Memory
2842 // After the initialize node executes, the object is ready for service:
2843 //   oop := (CheckCastPP init.Control alloc.RawAddress #short[])
2844 // Suppose its body is immediately initialized as {1,2}:
2845 //   store1 = (StoreC init.Control init.Memory (+ oop 12) 1)
2846 //   store2 = (StoreC init.Control store1      (+ oop 14) 2)
2847 //   mem.SLICE(#short[*]) := store2
2848 //
2849 // DETAILS:
2850 // An InitializeNode collects and isolates object initialization after
2851 // an AllocateNode and before the next possible safepoint.  As a
2852 // memory barrier (MemBarNode), it keeps critical stores from drifting
2853 // down past any safepoint or any publication of the allocation.
2854 // Before this barrier, a newly-allocated object may have uninitialized bits.
2855 // After this barrier, it may be treated as a real oop, and GC is allowed.
2856 //
2857 // The semantics of the InitializeNode include an implicit zeroing of
2858 // the new object from object header to the end of the object.
2859 // (The object header and end are determined by the AllocateNode.)
2860 //
2861 // Certain stores may be added as direct inputs to the InitializeNode.
2862 // These stores must update raw memory, and they must be to addresses
2863 // derived from the raw address produced by AllocateNode, and with
2864 // a constant offset.  They must be ordered by increasing offset.
2865 // The first one is at in(RawStores), the last at in(req()-1).
2866 // Unlike most memory operations, they are not linked in a chain,
2867 // but are displayed in parallel as users of the rawmem output of
2868 // the allocation.
2869 //
2870 // (See comments in InitializeNode::capture_store, which continue
2871 // the example given above.)
2872 //
2873 // When the associated Allocate is macro-expanded, the InitializeNode
2874 // may be rewritten to optimize collected stores.  A ClearArrayNode
2875 // may also be created at that point to represent any required zeroing.
2876 // The InitializeNode is then marked 'complete', prohibiting further
2877 // capturing of nearby memory operations.
2878 //
2879 // During macro-expansion, all captured initializations which store
2880 // constant values of 32 bits or smaller are coalesced (if advantageous)
2881 // into larger 'tiles' 32 or 64 bits.  This allows an object to be
2882 // initialized in fewer memory operations.  Memory words which are
2883 // covered by neither tiles nor non-constant stores are pre-zeroed
2884 // by explicit stores of zero.  (The code shape happens to do all
2885 // zeroing first, then all other stores, with both sequences occurring
2886 // in order of ascending offsets.)
2887 //
2888 // Alternatively, code may be inserted between an AllocateNode and its
2889 // InitializeNode, to perform arbitrary initialization of the new object.
2890 // E.g., the object copying intrinsics insert complex data transfers here.
2891 // The initialization must then be marked as 'complete' disable the
2892 // built-in zeroing semantics and the collection of initializing stores.
2893 //
2894 // While an InitializeNode is incomplete, reads from the memory state
2895 // produced by it are optimizable if they match the control edge and
2896 // new oop address associated with the allocation/initialization.
2897 // They return a stored value (if the offset matches) or else zero.
2898 // A write to the memory state, if it matches control and address,
2899 // and if it is to a constant offset, may be 'captured' by the
2900 // InitializeNode.  It is cloned as a raw memory operation and rewired
2901 // inside the initialization, to the raw oop produced by the allocation.
2902 // Operations on addresses which are provably distinct (e.g., to
2903 // other AllocateNodes) are allowed to bypass the initialization.
2904 //
2905 // The effect of all this is to consolidate object initialization
2906 // (both arrays and non-arrays, both piecewise and bulk) into a
2907 // single location, where it can be optimized as a unit.
2908 //
2909 // Only stores with an offset less than TrackedInitializationLimit words
2910 // will be considered for capture by an InitializeNode.  This puts a
2911 // reasonable limit on the complexity of optimized initializations.
2912 
2913 //---------------------------InitializeNode------------------------------------
2914 InitializeNode::InitializeNode(Compile* C, int adr_type, Node* rawoop)
2915   : _is_complete(Incomplete), _does_not_escape(false),
2916     MemBarNode(C, adr_type, rawoop)
2917 {
2918   init_class_id(Class_Initialize);
2919 
2920   assert(adr_type == Compile::AliasIdxRaw, "only valid atp");
2921   assert(in(RawAddress) == rawoop, "proper init");
2922   // Note:  allocation() can be NULL, for secondary initialization barriers
2923 }
2924 
2925 // Since this node is not matched, it will be processed by the
2926 // register allocator.  Declare that there are no constraints
2927 // on the allocation of the RawAddress edge.
2928 const RegMask &InitializeNode::in_RegMask(uint idx) const {
2929   // This edge should be set to top, by the set_complete.  But be conservative.
2930   if (idx == InitializeNode::RawAddress)
2931     return *(Compile::current()->matcher()->idealreg2spillmask[in(idx)->ideal_reg()]);
2932   return RegMask::Empty;
2933 }
2934 
2935 Node* InitializeNode::memory(uint alias_idx) {
2936   Node* mem = in(Memory);
2937   if (mem->is_MergeMem()) {
2938     return mem->as_MergeMem()->memory_at(alias_idx);
2939   } else {
2940     // incoming raw memory is not split
2941     return mem;
2942   }
2943 }
2944 
2945 bool InitializeNode::is_non_zero() {
2946   if (is_complete())  return false;
2947   remove_extra_zeroes();
2948   return (req() > RawStores);
2949 }
2950 
2951 void InitializeNode::set_complete(PhaseGVN* phase) {
2952   assert(!is_complete(), "caller responsibility");
2953   _is_complete = Complete;
2954 
2955   // After this node is complete, it contains a bunch of
2956   // raw-memory initializations.  There is no need for
2957   // it to have anything to do with non-raw memory effects.
2958   // Therefore, tell all non-raw users to re-optimize themselves,
2959   // after skipping the memory effects of this initialization.
2960   PhaseIterGVN* igvn = phase->is_IterGVN();
2961   if (igvn)  igvn->add_users_to_worklist(this);
2962 }
2963 
2964 // convenience function
2965 // return false if the init contains any stores already
2966 bool AllocateNode::maybe_set_complete(PhaseGVN* phase) {
2967   InitializeNode* init = initialization();
2968   if (init == NULL || init->is_complete())  return false;
2969   init->remove_extra_zeroes();
2970   // for now, if this allocation has already collected any inits, bail:
2971   if (init->is_non_zero())  return false;
2972   init->set_complete(phase);
2973   return true;
2974 }
2975 
2976 void InitializeNode::remove_extra_zeroes() {
2977   if (req() == RawStores)  return;
2978   Node* zmem = zero_memory();
2979   uint fill = RawStores;
2980   for (uint i = fill; i < req(); i++) {
2981     Node* n = in(i);
2982     if (n->is_top() || n == zmem)  continue;  // skip
2983     if (fill < i)  set_req(fill, n);          // compact
2984     ++fill;
2985   }
2986   // delete any empty spaces created:
2987   while (fill < req()) {
2988     del_req(fill);
2989   }
2990 }
2991 
2992 // Helper for remembering which stores go with which offsets.
2993 intptr_t InitializeNode::get_store_offset(Node* st, PhaseTransform* phase) {
2994   if (!st->is_Store())  return -1;  // can happen to dead code via subsume_node
2995   intptr_t offset = -1;
2996   Node* base = AddPNode::Ideal_base_and_offset(st->in(MemNode::Address),
2997                                                phase, offset);
2998   if (base == NULL)     return -1;  // something is dead,
2999   if (offset < 0)       return -1;  //        dead, dead
3000   return offset;
3001 }
3002 
3003 // Helper for proving that an initialization expression is
3004 // "simple enough" to be folded into an object initialization.
3005 // Attempts to prove that a store's initial value 'n' can be captured
3006 // within the initialization without creating a vicious cycle, such as:
3007 //     { Foo p = new Foo(); p.next = p; }
3008 // True for constants and parameters and small combinations thereof.
3009 bool InitializeNode::detect_init_independence(Node* n, int& count) {
3010   if (n == NULL)      return true;   // (can this really happen?)
3011   if (n->is_Proj())   n = n->in(0);
3012   if (n == this)      return false;  // found a cycle
3013   if (n->is_Con())    return true;
3014   if (n->is_Start())  return true;   // params, etc., are OK
3015   if (n->is_Root())   return true;   // even better
3016 
3017   Node* ctl = n->in(0);
3018   if (ctl != NULL && !ctl->is_top()) {
3019     if (ctl->is_Proj())  ctl = ctl->in(0);
3020     if (ctl == this)  return false;
3021 
3022     // If we already know that the enclosing memory op is pinned right after
3023     // the init, then any control flow that the store has picked up
3024     // must have preceded the init, or else be equal to the init.
3025     // Even after loop optimizations (which might change control edges)
3026     // a store is never pinned *before* the availability of its inputs.
3027     if (!MemNode::all_controls_dominate(n, this))
3028       return false;                  // failed to prove a good control
3029   }
3030 
3031   // Check data edges for possible dependencies on 'this'.
3032   if ((count += 1) > 20)  return false;  // complexity limit
3033   for (uint i = 1; i < n->req(); i++) {
3034     Node* m = n->in(i);
3035     if (m == NULL || m == n || m->is_top())  continue;
3036     uint first_i = n->find_edge(m);
3037     if (i != first_i)  continue;  // process duplicate edge just once
3038     if (!detect_init_independence(m, count)) {
3039       return false;
3040     }
3041   }
3042 
3043   return true;
3044 }
3045 
3046 // Here are all the checks a Store must pass before it can be moved into
3047 // an initialization.  Returns zero if a check fails.
3048 // On success, returns the (constant) offset to which the store applies,
3049 // within the initialized memory.
3050 intptr_t InitializeNode::can_capture_store(StoreNode* st, PhaseTransform* phase, bool can_reshape) {
3051   const int FAIL = 0;
3052   if (st->req() != MemNode::ValueIn + 1)
3053     return FAIL;                // an inscrutable StoreNode (card mark?)
3054   Node* ctl = st->in(MemNode::Control);
3055   if (!(ctl != NULL && ctl->is_Proj() && ctl->in(0) == this))
3056     return FAIL;                // must be unconditional after the initialization
3057   Node* mem = st->in(MemNode::Memory);
3058   if (!(mem->is_Proj() && mem->in(0) == this))
3059     return FAIL;                // must not be preceded by other stores
3060   Node* adr = st->in(MemNode::Address);
3061   intptr_t offset;
3062   AllocateNode* alloc = AllocateNode::Ideal_allocation(adr, phase, offset);
3063   if (alloc == NULL)
3064     return FAIL;                // inscrutable address
3065   if (alloc != allocation())
3066     return FAIL;                // wrong allocation!  (store needs to float up)
3067   Node* val = st->in(MemNode::ValueIn);
3068   int complexity_count = 0;
3069   if (!detect_init_independence(val, complexity_count))
3070     return FAIL;                // stored value must be 'simple enough'
3071 
3072   // The Store can be captured only if nothing after the allocation
3073   // and before the Store is using the memory location that the store
3074   // overwrites.
3075   bool failed = false;
3076   // If is_complete_with_arraycopy() is true the shape of the graph is
3077   // well defined and is safe so no need for extra checks.
3078   if (!is_complete_with_arraycopy()) {
3079     // We are going to look at each use of the memory state following
3080     // the allocation to make sure nothing reads the memory that the
3081     // Store writes.
3082     const TypePtr* t_adr = phase->type(adr)->isa_ptr();
3083     int alias_idx = phase->C->get_alias_index(t_adr);
3084     ResourceMark rm;
3085     Unique_Node_List mems;
3086     mems.push(mem);
3087     Node* unique_merge = NULL;
3088     for (uint next = 0; next < mems.size(); ++next) {
3089       Node *m  = mems.at(next);
3090       for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
3091         Node *n = m->fast_out(j);
3092         if (n->outcnt() == 0) {
3093           continue;
3094         }
3095         if (n == st) {
3096           continue;
3097         } else if (n->in(0) != NULL && n->in(0) != ctl) {
3098           // If the control of this use is different from the control
3099           // of the Store which is right after the InitializeNode then
3100           // this node cannot be between the InitializeNode and the
3101           // Store.
3102           continue;
3103         } else if (n->is_MergeMem()) {
3104           if (n->as_MergeMem()->memory_at(alias_idx) == m) {
3105             // We can hit a MergeMemNode (that will likely go away
3106             // later) that is a direct use of the memory state
3107             // following the InitializeNode on the same slice as the
3108             // store node that we'd like to capture. We need to check
3109             // the uses of the MergeMemNode.
3110             mems.push(n);
3111           }
3112         } else if (n->is_Mem()) {
3113           Node* other_adr = n->in(MemNode::Address);
3114           if (other_adr == adr) {
3115             failed = true;
3116             break;
3117           } else {
3118             const TypePtr* other_t_adr = phase->type(other_adr)->isa_ptr();
3119             if (other_t_adr != NULL) {
3120               int other_alias_idx = phase->C->get_alias_index(other_t_adr);
3121               if (other_alias_idx == alias_idx) {
3122                 // A load from the same memory slice as the store right
3123                 // after the InitializeNode. We check the control of the
3124                 // object/array that is loaded from. If it's the same as
3125                 // the store control then we cannot capture the store.
3126                 assert(!n->is_Store(), "2 stores to same slice on same control?");
3127                 Node* base = other_adr;
3128                 assert(base->is_AddP(), err_msg_res("should be addp but is %s", base->Name()));
3129                 base = base->in(AddPNode::Base);
3130                 if (base != NULL) {
3131                   base = base->uncast();
3132                   if (base->is_Proj() && base->in(0) == alloc) {
3133                     failed = true;
3134                     break;
3135                   }
3136                 }
3137               }
3138             }
3139           }
3140         } else {
3141           failed = true;
3142           break;
3143         }
3144       }
3145     }
3146   }
3147   if (failed) {
3148     if (!can_reshape) {
3149       // We decided we couldn't capture the store during parsing. We
3150       // should try again during the next IGVN once the graph is
3151       // cleaner.
3152       phase->C->record_for_igvn(st);
3153     }
3154     return FAIL;
3155   }
3156 
3157   return offset;                // success
3158 }
3159 
3160 // Find the captured store in(i) which corresponds to the range
3161 // [start..start+size) in the initialized object.
3162 // If there is one, return its index i.  If there isn't, return the
3163 // negative of the index where it should be inserted.
3164 // Return 0 if the queried range overlaps an initialization boundary
3165 // or if dead code is encountered.
3166 // If size_in_bytes is zero, do not bother with overlap checks.
3167 int InitializeNode::captured_store_insertion_point(intptr_t start,
3168                                                    int size_in_bytes,
3169                                                    PhaseTransform* phase) {
3170   const int FAIL = 0, MAX_STORE = BytesPerLong;
3171 
3172   if (is_complete())
3173     return FAIL;                // arraycopy got here first; punt
3174 
3175   assert(allocation() != NULL, "must be present");
3176 
3177   // no negatives, no header fields:
3178   if (start < (intptr_t) allocation()->minimum_header_size())  return FAIL;
3179 
3180   // after a certain size, we bail out on tracking all the stores:
3181   intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
3182   if (start >= ti_limit)  return FAIL;
3183 
3184   for (uint i = InitializeNode::RawStores, limit = req(); ; ) {
3185     if (i >= limit)  return -(int)i; // not found; here is where to put it
3186 
3187     Node*    st     = in(i);
3188     intptr_t st_off = get_store_offset(st, phase);
3189     if (st_off < 0) {
3190       if (st != zero_memory()) {
3191         return FAIL;            // bail out if there is dead garbage
3192       }
3193     } else if (st_off > start) {
3194       // ...we are done, since stores are ordered
3195       if (st_off < start + size_in_bytes) {
3196         return FAIL;            // the next store overlaps
3197       }
3198       return -(int)i;           // not found; here is where to put it
3199     } else if (st_off < start) {
3200       if (size_in_bytes != 0 &&
3201           start < st_off + MAX_STORE &&
3202           start < st_off + st->as_Store()->memory_size()) {
3203         return FAIL;            // the previous store overlaps
3204       }
3205     } else {
3206       if (size_in_bytes != 0 &&
3207           st->as_Store()->memory_size() != size_in_bytes) {
3208         return FAIL;            // mismatched store size
3209       }
3210       return i;
3211     }
3212 
3213     ++i;
3214   }
3215 }
3216 
3217 // Look for a captured store which initializes at the offset 'start'
3218 // with the given size.  If there is no such store, and no other
3219 // initialization interferes, then return zero_memory (the memory
3220 // projection of the AllocateNode).
3221 Node* InitializeNode::find_captured_store(intptr_t start, int size_in_bytes,
3222                                           PhaseTransform* phase) {
3223   assert(stores_are_sane(phase), "");
3224   int i = captured_store_insertion_point(start, size_in_bytes, phase);
3225   if (i == 0) {
3226     return NULL;                // something is dead
3227   } else if (i < 0) {
3228     return zero_memory();       // just primordial zero bits here
3229   } else {
3230     Node* st = in(i);           // here is the store at this position
3231     assert(get_store_offset(st->as_Store(), phase) == start, "sanity");
3232     return st;
3233   }
3234 }
3235 
3236 // Create, as a raw pointer, an address within my new object at 'offset'.
3237 Node* InitializeNode::make_raw_address(intptr_t offset,
3238                                        PhaseTransform* phase) {
3239   Node* addr = in(RawAddress);
3240   if (offset != 0) {
3241     Compile* C = phase->C;
3242     addr = phase->transform( new AddPNode(C->top(), addr,
3243                                                  phase->MakeConX(offset)) );
3244   }
3245   return addr;
3246 }
3247 
3248 // Clone the given store, converting it into a raw store
3249 // initializing a field or element of my new object.
3250 // Caller is responsible for retiring the original store,
3251 // with subsume_node or the like.
3252 //
3253 // From the example above InitializeNode::InitializeNode,
3254 // here are the old stores to be captured:
3255 //   store1 = (StoreC init.Control init.Memory (+ oop 12) 1)
3256 //   store2 = (StoreC init.Control store1      (+ oop 14) 2)
3257 //
3258 // Here is the changed code; note the extra edges on init:
3259 //   alloc = (Allocate ...)
3260 //   rawoop = alloc.RawAddress
3261 //   rawstore1 = (StoreC alloc.Control alloc.Memory (+ rawoop 12) 1)
3262 //   rawstore2 = (StoreC alloc.Control alloc.Memory (+ rawoop 14) 2)
3263 //   init = (Initialize alloc.Control alloc.Memory rawoop
3264 //                      rawstore1 rawstore2)
3265 //
3266 Node* InitializeNode::capture_store(StoreNode* st, intptr_t start,
3267                                     PhaseTransform* phase, bool can_reshape) {
3268   assert(stores_are_sane(phase), "");
3269 
3270   if (start < 0)  return NULL;
3271   assert(can_capture_store(st, phase, can_reshape) == start, "sanity");
3272 
3273   Compile* C = phase->C;
3274   int size_in_bytes = st->memory_size();
3275   int i = captured_store_insertion_point(start, size_in_bytes, phase);
3276   if (i == 0)  return NULL;     // bail out
3277   Node* prev_mem = NULL;        // raw memory for the captured store
3278   if (i > 0) {
3279     prev_mem = in(i);           // there is a pre-existing store under this one
3280     set_req(i, C->top());       // temporarily disconnect it
3281     // See StoreNode::Ideal 'st->outcnt() == 1' for the reason to disconnect.
3282   } else {
3283     i = -i;                     // no pre-existing store
3284     prev_mem = zero_memory();   // a slice of the newly allocated object
3285     if (i > InitializeNode::RawStores && in(i-1) == prev_mem)
3286       set_req(--i, C->top());   // reuse this edge; it has been folded away
3287     else
3288       ins_req(i, C->top());     // build a new edge
3289   }
3290   Node* new_st = st->clone();
3291   new_st->set_req(MemNode::Control, in(Control));
3292   new_st->set_req(MemNode::Memory,  prev_mem);
3293   new_st->set_req(MemNode::Address, make_raw_address(start, phase));
3294   new_st = phase->transform(new_st);
3295 
3296   // At this point, new_st might have swallowed a pre-existing store
3297   // at the same offset, or perhaps new_st might have disappeared,
3298   // if it redundantly stored the same value (or zero to fresh memory).
3299 
3300   // In any case, wire it in:
3301   phase->igvn_rehash_node_delayed(this);
3302   set_req(i, new_st);
3303 
3304   // The caller may now kill the old guy.
3305   DEBUG_ONLY(Node* check_st = find_captured_store(start, size_in_bytes, phase));
3306   assert(check_st == new_st || check_st == NULL, "must be findable");
3307   assert(!is_complete(), "");
3308   return new_st;
3309 }
3310 
3311 static bool store_constant(jlong* tiles, int num_tiles,
3312                            intptr_t st_off, int st_size,
3313                            jlong con) {
3314   if ((st_off & (st_size-1)) != 0)
3315     return false;               // strange store offset (assume size==2**N)
3316   address addr = (address)tiles + st_off;
3317   assert(st_off >= 0 && addr+st_size <= (address)&tiles[num_tiles], "oob");
3318   switch (st_size) {
3319   case sizeof(jbyte):  *(jbyte*) addr = (jbyte) con; break;
3320   case sizeof(jchar):  *(jchar*) addr = (jchar) con; break;
3321   case sizeof(jint):   *(jint*)  addr = (jint)  con; break;
3322   case sizeof(jlong):  *(jlong*) addr = (jlong) con; break;
3323   default: return false;        // strange store size (detect size!=2**N here)
3324   }
3325   return true;                  // return success to caller
3326 }
3327 
3328 // Coalesce subword constants into int constants and possibly
3329 // into long constants.  The goal, if the CPU permits,
3330 // is to initialize the object with a small number of 64-bit tiles.
3331 // Also, convert floating-point constants to bit patterns.
3332 // Non-constants are not relevant to this pass.
3333 //
3334 // In terms of the running example on InitializeNode::InitializeNode
3335 // and InitializeNode::capture_store, here is the transformation
3336 // of rawstore1 and rawstore2 into rawstore12:
3337 //   alloc = (Allocate ...)
3338 //   rawoop = alloc.RawAddress
3339 //   tile12 = 0x00010002
3340 //   rawstore12 = (StoreI alloc.Control alloc.Memory (+ rawoop 12) tile12)
3341 //   init = (Initialize alloc.Control alloc.Memory rawoop rawstore12)
3342 //
3343 void
3344 InitializeNode::coalesce_subword_stores(intptr_t header_size,
3345                                         Node* size_in_bytes,
3346                                         PhaseGVN* phase) {
3347   Compile* C = phase->C;
3348 
3349   assert(stores_are_sane(phase), "");
3350   // Note:  After this pass, they are not completely sane,
3351   // since there may be some overlaps.
3352 
3353   int old_subword = 0, old_long = 0, new_int = 0, new_long = 0;
3354 
3355   intptr_t ti_limit = (TrackedInitializationLimit * HeapWordSize);
3356   intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, ti_limit);
3357   size_limit = MIN2(size_limit, ti_limit);
3358   size_limit = align_size_up(size_limit, BytesPerLong);
3359   int num_tiles = size_limit / BytesPerLong;
3360 
3361   // allocate space for the tile map:
3362   const int small_len = DEBUG_ONLY(true ? 3 :) 30; // keep stack frames small
3363   jlong  tiles_buf[small_len];
3364   Node*  nodes_buf[small_len];
3365   jlong  inits_buf[small_len];
3366   jlong* tiles = ((num_tiles <= small_len) ? &tiles_buf[0]
3367                   : NEW_RESOURCE_ARRAY(jlong, num_tiles));
3368   Node** nodes = ((num_tiles <= small_len) ? &nodes_buf[0]
3369                   : NEW_RESOURCE_ARRAY(Node*, num_tiles));
3370   jlong* inits = ((num_tiles <= small_len) ? &inits_buf[0]
3371                   : NEW_RESOURCE_ARRAY(jlong, num_tiles));
3372   // tiles: exact bitwise model of all primitive constants
3373   // nodes: last constant-storing node subsumed into the tiles model
3374   // inits: which bytes (in each tile) are touched by any initializations
3375 
3376   //// Pass A: Fill in the tile model with any relevant stores.
3377 
3378   Copy::zero_to_bytes(tiles, sizeof(tiles[0]) * num_tiles);
3379   Copy::zero_to_bytes(nodes, sizeof(nodes[0]) * num_tiles);
3380   Copy::zero_to_bytes(inits, sizeof(inits[0]) * num_tiles);
3381   Node* zmem = zero_memory(); // initially zero memory state
3382   for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
3383     Node* st = in(i);
3384     intptr_t st_off = get_store_offset(st, phase);
3385 
3386     // Figure out the store's offset and constant value:
3387     if (st_off < header_size)             continue; //skip (ignore header)
3388     if (st->in(MemNode::Memory) != zmem)  continue; //skip (odd store chain)
3389     int st_size = st->as_Store()->memory_size();
3390     if (st_off + st_size > size_limit)    break;
3391 
3392     // Record which bytes are touched, whether by constant or not.
3393     if (!store_constant(inits, num_tiles, st_off, st_size, (jlong) -1))
3394       continue;                 // skip (strange store size)
3395 
3396     const Type* val = phase->type(st->in(MemNode::ValueIn));
3397     if (!val->singleton())                continue; //skip (non-con store)
3398     BasicType type = val->basic_type();
3399 
3400     jlong con = 0;
3401     switch (type) {
3402     case T_INT:    con = val->is_int()->get_con();  break;
3403     case T_LONG:   con = val->is_long()->get_con(); break;
3404     case T_FLOAT:  con = jint_cast(val->getf());    break;
3405     case T_DOUBLE: con = jlong_cast(val->getd());   break;
3406     default:                              continue; //skip (odd store type)
3407     }
3408 
3409     if (type == T_LONG && Matcher::isSimpleConstant64(con) &&
3410         st->Opcode() == Op_StoreL) {
3411       continue;                 // This StoreL is already optimal.
3412     }
3413 
3414     // Store down the constant.
3415     store_constant(tiles, num_tiles, st_off, st_size, con);
3416 
3417     intptr_t j = st_off >> LogBytesPerLong;
3418 
3419     if (type == T_INT && st_size == BytesPerInt
3420         && (st_off & BytesPerInt) == BytesPerInt) {
3421       jlong lcon = tiles[j];
3422       if (!Matcher::isSimpleConstant64(lcon) &&
3423           st->Opcode() == Op_StoreI) {
3424         // This StoreI is already optimal by itself.
3425         jint* intcon = (jint*) &tiles[j];
3426         intcon[1] = 0;  // undo the store_constant()
3427 
3428         // If the previous store is also optimal by itself, back up and
3429         // undo the action of the previous loop iteration... if we can.
3430         // But if we can't, just let the previous half take care of itself.
3431         st = nodes[j];
3432         st_off -= BytesPerInt;
3433         con = intcon[0];
3434         if (con != 0 && st != NULL && st->Opcode() == Op_StoreI) {
3435           assert(st_off >= header_size, "still ignoring header");
3436           assert(get_store_offset(st, phase) == st_off, "must be");
3437           assert(in(i-1) == zmem, "must be");
3438           DEBUG_ONLY(const Type* tcon = phase->type(st->in(MemNode::ValueIn)));
3439           assert(con == tcon->is_int()->get_con(), "must be");
3440           // Undo the effects of the previous loop trip, which swallowed st:
3441           intcon[0] = 0;        // undo store_constant()
3442           set_req(i-1, st);     // undo set_req(i, zmem)
3443           nodes[j] = NULL;      // undo nodes[j] = st
3444           --old_subword;        // undo ++old_subword
3445         }
3446         continue;               // This StoreI is already optimal.
3447       }
3448     }
3449 
3450     // This store is not needed.
3451     set_req(i, zmem);
3452     nodes[j] = st;              // record for the moment
3453     if (st_size < BytesPerLong) // something has changed
3454           ++old_subword;        // includes int/float, but who's counting...
3455     else  ++old_long;
3456   }
3457 
3458   if ((old_subword + old_long) == 0)
3459     return;                     // nothing more to do
3460 
3461   //// Pass B: Convert any non-zero tiles into optimal constant stores.
3462   // Be sure to insert them before overlapping non-constant stores.
3463   // (E.g., byte[] x = { 1,2,y,4 }  =>  x[int 0] = 0x01020004, x[2]=y.)
3464   for (int j = 0; j < num_tiles; j++) {
3465     jlong con  = tiles[j];
3466     jlong init = inits[j];
3467     if (con == 0)  continue;
3468     jint con0,  con1;           // split the constant, address-wise
3469     jint init0, init1;          // split the init map, address-wise
3470     { union { jlong con; jint intcon[2]; } u;
3471       u.con = con;
3472       con0  = u.intcon[0];
3473       con1  = u.intcon[1];
3474       u.con = init;
3475       init0 = u.intcon[0];
3476       init1 = u.intcon[1];
3477     }
3478 
3479     Node* old = nodes[j];
3480     assert(old != NULL, "need the prior store");
3481     intptr_t offset = (j * BytesPerLong);
3482 
3483     bool split = !Matcher::isSimpleConstant64(con);
3484 
3485     if (offset < header_size) {
3486       assert(offset + BytesPerInt >= header_size, "second int counts");
3487       assert(*(jint*)&tiles[j] == 0, "junk in header");
3488       split = true;             // only the second word counts
3489       // Example:  int a[] = { 42 ... }
3490     } else if (con0 == 0 && init0 == -1) {
3491       split = true;             // first word is covered by full inits
3492       // Example:  int a[] = { ... foo(), 42 ... }
3493     } else if (con1 == 0 && init1 == -1) {
3494       split = true;             // second word is covered by full inits
3495       // Example:  int a[] = { ... 42, foo() ... }
3496     }
3497 
3498     // Here's a case where init0 is neither 0 nor -1:
3499     //   byte a[] = { ... 0,0,foo(),0,  0,0,0,42 ... }
3500     // Assuming big-endian memory, init0, init1 are 0x0000FF00, 0x000000FF.
3501     // In this case the tile is not split; it is (jlong)42.
3502     // The big tile is stored down, and then the foo() value is inserted.
3503     // (If there were foo(),foo() instead of foo(),0, init0 would be -1.)
3504 
3505     Node* ctl = old->in(MemNode::Control);
3506     Node* adr = make_raw_address(offset, phase);
3507     const TypePtr* atp = TypeRawPtr::BOTTOM;
3508 
3509     // One or two coalesced stores to plop down.
3510     Node*    st[2];
3511     intptr_t off[2];
3512     int  nst = 0;
3513     if (!split) {
3514       ++new_long;
3515       off[nst] = offset;
3516       st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3517                                   phase->longcon(con), T_LONG, MemNode::unordered);
3518     } else {
3519       // Omit either if it is a zero.
3520       if (con0 != 0) {
3521         ++new_int;
3522         off[nst]  = offset;
3523         st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3524                                     phase->intcon(con0), T_INT, MemNode::unordered);
3525       }
3526       if (con1 != 0) {
3527         ++new_int;
3528         offset += BytesPerInt;
3529         adr = make_raw_address(offset, phase);
3530         off[nst]  = offset;
3531         st[nst++] = StoreNode::make(*phase, ctl, zmem, adr, atp,
3532                                     phase->intcon(con1), T_INT, MemNode::unordered);
3533       }
3534     }
3535 
3536     // Insert second store first, then the first before the second.
3537     // Insert each one just before any overlapping non-constant stores.
3538     while (nst > 0) {
3539       Node* st1 = st[--nst];
3540       C->copy_node_notes_to(st1, old);
3541       st1 = phase->transform(st1);
3542       offset = off[nst];
3543       assert(offset >= header_size, "do not smash header");
3544       int ins_idx = captured_store_insertion_point(offset, /*size:*/0, phase);
3545       guarantee(ins_idx != 0, "must re-insert constant store");
3546       if (ins_idx < 0)  ins_idx = -ins_idx;  // never overlap
3547       if (ins_idx > InitializeNode::RawStores && in(ins_idx-1) == zmem)
3548         set_req(--ins_idx, st1);
3549       else
3550         ins_req(ins_idx, st1);
3551     }
3552   }
3553 
3554   if (PrintCompilation && WizardMode)
3555     tty->print_cr("Changed %d/%d subword/long constants into %d/%d int/long",
3556                   old_subword, old_long, new_int, new_long);
3557   if (C->log() != NULL)
3558     C->log()->elem("comment that='%d/%d subword/long to %d/%d int/long'",
3559                    old_subword, old_long, new_int, new_long);
3560 
3561   // Clean up any remaining occurrences of zmem:
3562   remove_extra_zeroes();
3563 }
3564 
3565 // Explore forward from in(start) to find the first fully initialized
3566 // word, and return its offset.  Skip groups of subword stores which
3567 // together initialize full words.  If in(start) is itself part of a
3568 // fully initialized word, return the offset of in(start).  If there
3569 // are no following full-word stores, or if something is fishy, return
3570 // a negative value.
3571 intptr_t InitializeNode::find_next_fullword_store(uint start, PhaseGVN* phase) {
3572   int       int_map = 0;
3573   intptr_t  int_map_off = 0;
3574   const int FULL_MAP = right_n_bits(BytesPerInt);  // the int_map we hope for
3575 
3576   for (uint i = start, limit = req(); i < limit; i++) {
3577     Node* st = in(i);
3578 
3579     intptr_t st_off = get_store_offset(st, phase);
3580     if (st_off < 0)  break;  // return conservative answer
3581 
3582     int st_size = st->as_Store()->memory_size();
3583     if (st_size >= BytesPerInt && (st_off % BytesPerInt) == 0) {
3584       return st_off;            // we found a complete word init
3585     }
3586 
3587     // update the map:
3588 
3589     intptr_t this_int_off = align_size_down(st_off, BytesPerInt);
3590     if (this_int_off != int_map_off) {
3591       // reset the map:
3592       int_map = 0;
3593       int_map_off = this_int_off;
3594     }
3595 
3596     int subword_off = st_off - this_int_off;
3597     int_map |= right_n_bits(st_size) << subword_off;
3598     if ((int_map & FULL_MAP) == FULL_MAP) {
3599       return this_int_off;      // we found a complete word init
3600     }
3601 
3602     // Did this store hit or cross the word boundary?
3603     intptr_t next_int_off = align_size_down(st_off + st_size, BytesPerInt);
3604     if (next_int_off == this_int_off + BytesPerInt) {
3605       // We passed the current int, without fully initializing it.
3606       int_map_off = next_int_off;
3607       int_map >>= BytesPerInt;
3608     } else if (next_int_off > this_int_off + BytesPerInt) {
3609       // We passed the current and next int.
3610       return this_int_off + BytesPerInt;
3611     }
3612   }
3613 
3614   return -1;
3615 }
3616 
3617 
3618 // Called when the associated AllocateNode is expanded into CFG.
3619 // At this point, we may perform additional optimizations.
3620 // Linearize the stores by ascending offset, to make memory
3621 // activity as coherent as possible.
3622 Node* InitializeNode::complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
3623                                       intptr_t header_size,
3624                                       Node* size_in_bytes,
3625                                       PhaseGVN* phase) {
3626   assert(!is_complete(), "not already complete");
3627   assert(stores_are_sane(phase), "");
3628   assert(allocation() != NULL, "must be present");
3629 
3630   remove_extra_zeroes();
3631 
3632   if (ReduceFieldZeroing || ReduceBulkZeroing)
3633     // reduce instruction count for common initialization patterns
3634     coalesce_subword_stores(header_size, size_in_bytes, phase);
3635 
3636   Node* zmem = zero_memory();   // initially zero memory state
3637   Node* inits = zmem;           // accumulating a linearized chain of inits
3638   #ifdef ASSERT
3639   intptr_t first_offset = allocation()->minimum_header_size();
3640   intptr_t last_init_off = first_offset;  // previous init offset
3641   intptr_t last_init_end = first_offset;  // previous init offset+size
3642   intptr_t last_tile_end = first_offset;  // previous tile offset+size
3643   #endif
3644   intptr_t zeroes_done = header_size;
3645 
3646   bool do_zeroing = true;       // we might give up if inits are very sparse
3647   int  big_init_gaps = 0;       // how many large gaps have we seen?
3648 
3649   if (ZeroTLAB)  do_zeroing = false;
3650   if (!ReduceFieldZeroing && !ReduceBulkZeroing)  do_zeroing = false;
3651 
3652   for (uint i = InitializeNode::RawStores, limit = req(); i < limit; i++) {
3653     Node* st = in(i);
3654     intptr_t st_off = get_store_offset(st, phase);
3655     if (st_off < 0)
3656       break;                    // unknown junk in the inits
3657     if (st->in(MemNode::Memory) != zmem)
3658       break;                    // complicated store chains somehow in list
3659 
3660     int st_size = st->as_Store()->memory_size();
3661     intptr_t next_init_off = st_off + st_size;
3662 
3663     if (do_zeroing && zeroes_done < next_init_off) {
3664       // See if this store needs a zero before it or under it.
3665       intptr_t zeroes_needed = st_off;
3666 
3667       if (st_size < BytesPerInt) {
3668         // Look for subword stores which only partially initialize words.
3669         // If we find some, we must lay down some word-level zeroes first,
3670         // underneath the subword stores.
3671         //
3672         // Examples:
3673         //   byte[] a = { p,q,r,s }  =>  a[0]=p,a[1]=q,a[2]=r,a[3]=s
3674         //   byte[] a = { x,y,0,0 }  =>  a[0..3] = 0, a[0]=x,a[1]=y
3675         //   byte[] a = { 0,0,z,0 }  =>  a[0..3] = 0, a[2]=z
3676         //
3677         // Note:  coalesce_subword_stores may have already done this,
3678         // if it was prompted by constant non-zero subword initializers.
3679         // But this case can still arise with non-constant stores.
3680 
3681         intptr_t next_full_store = find_next_fullword_store(i, phase);
3682 
3683         // In the examples above:
3684         //   in(i)          p   q   r   s     x   y     z
3685         //   st_off        12  13  14  15    12  13    14
3686         //   st_size        1   1   1   1     1   1     1
3687         //   next_full_s.  12  16  16  16    16  16    16
3688         //   z's_done      12  16  16  16    12  16    12
3689         //   z's_needed    12  16  16  16    16  16    16
3690         //   zsize          0   0   0   0     4   0     4
3691         if (next_full_store < 0) {
3692           // Conservative tack:  Zero to end of current word.
3693           zeroes_needed = align_size_up(zeroes_needed, BytesPerInt);
3694         } else {
3695           // Zero to beginning of next fully initialized word.
3696           // Or, don't zero at all, if we are already in that word.
3697           assert(next_full_store >= zeroes_needed, "must go forward");
3698           assert((next_full_store & (BytesPerInt-1)) == 0, "even boundary");
3699           zeroes_needed = next_full_store;
3700         }
3701       }
3702 
3703       if (zeroes_needed > zeroes_done) {
3704         intptr_t zsize = zeroes_needed - zeroes_done;
3705         // Do some incremental zeroing on rawmem, in parallel with inits.
3706         zeroes_done = align_size_down(zeroes_done, BytesPerInt);
3707         rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
3708                                               zeroes_done, zeroes_needed,
3709                                               phase);
3710         zeroes_done = zeroes_needed;
3711         if (zsize > Matcher::init_array_short_size && ++big_init_gaps > 2)
3712           do_zeroing = false;   // leave the hole, next time
3713       }
3714     }
3715 
3716     // Collect the store and move on:
3717     st->set_req(MemNode::Memory, inits);
3718     inits = st;                 // put it on the linearized chain
3719     set_req(i, zmem);           // unhook from previous position
3720 
3721     if (zeroes_done == st_off)
3722       zeroes_done = next_init_off;
3723 
3724     assert(!do_zeroing || zeroes_done >= next_init_off, "don't miss any");
3725 
3726     #ifdef ASSERT
3727     // Various order invariants.  Weaker than stores_are_sane because
3728     // a large constant tile can be filled in by smaller non-constant stores.
3729     assert(st_off >= last_init_off, "inits do not reverse");
3730     last_init_off = st_off;
3731     const Type* val = NULL;
3732     if (st_size >= BytesPerInt &&
3733         (val = phase->type(st->in(MemNode::ValueIn)))->singleton() &&
3734         (int)val->basic_type() < (int)T_OBJECT) {
3735       assert(st_off >= last_tile_end, "tiles do not overlap");
3736       assert(st_off >= last_init_end, "tiles do not overwrite inits");
3737       last_tile_end = MAX2(last_tile_end, next_init_off);
3738     } else {
3739       intptr_t st_tile_end = align_size_up(next_init_off, BytesPerLong);
3740       assert(st_tile_end >= last_tile_end, "inits stay with tiles");
3741       assert(st_off      >= last_init_end, "inits do not overlap");
3742       last_init_end = next_init_off;  // it's a non-tile
3743     }
3744     #endif //ASSERT
3745   }
3746 
3747   remove_extra_zeroes();        // clear out all the zmems left over
3748   add_req(inits);
3749 
3750   if (!ZeroTLAB) {
3751     // If anything remains to be zeroed, zero it all now.
3752     zeroes_done = align_size_down(zeroes_done, BytesPerInt);
3753     // if it is the last unused 4 bytes of an instance, forget about it
3754     intptr_t size_limit = phase->find_intptr_t_con(size_in_bytes, max_jint);
3755     if (zeroes_done + BytesPerLong >= size_limit) {
3756       assert(allocation() != NULL, "");
3757       if (allocation()->Opcode() == Op_Allocate) {
3758         Node* klass_node = allocation()->in(AllocateNode::KlassNode);
3759         ciKlass* k = phase->type(klass_node)->is_klassptr()->klass();
3760         if (zeroes_done == k->layout_helper())
3761           zeroes_done = size_limit;
3762       }
3763     }
3764     if (zeroes_done < size_limit) {
3765       rawmem = ClearArrayNode::clear_memory(rawctl, rawmem, rawptr,
3766                                             zeroes_done, size_in_bytes, phase);
3767     }
3768   }
3769 
3770   set_complete(phase);
3771   return rawmem;
3772 }
3773 
3774 
3775 #ifdef ASSERT
3776 bool InitializeNode::stores_are_sane(PhaseTransform* phase) {
3777   if (is_complete())
3778     return true;                // stores could be anything at this point
3779   assert(allocation() != NULL, "must be present");
3780   intptr_t last_off = allocation()->minimum_header_size();
3781   for (uint i = InitializeNode::RawStores; i < req(); i++) {
3782     Node* st = in(i);
3783     intptr_t st_off = get_store_offset(st, phase);
3784     if (st_off < 0)  continue;  // ignore dead garbage
3785     if (last_off > st_off) {
3786       tty->print_cr("*** bad store offset at %d: " INTX_FORMAT " > " INTX_FORMAT, i, last_off, st_off);
3787       this->dump(2);
3788       assert(false, "ascending store offsets");
3789       return false;
3790     }
3791     last_off = st_off + st->as_Store()->memory_size();
3792   }
3793   return true;
3794 }
3795 #endif //ASSERT
3796 
3797 
3798 
3799 
3800 //============================MergeMemNode=====================================
3801 //
3802 // SEMANTICS OF MEMORY MERGES:  A MergeMem is a memory state assembled from several
3803 // contributing store or call operations.  Each contributor provides the memory
3804 // state for a particular "alias type" (see Compile::alias_type).  For example,
3805 // if a MergeMem has an input X for alias category #6, then any memory reference
3806 // to alias category #6 may use X as its memory state input, as an exact equivalent
3807 // to using the MergeMem as a whole.
3808 //   Load<6>( MergeMem(<6>: X, ...), p ) <==> Load<6>(X,p)
3809 //
3810 // (Here, the <N> notation gives the index of the relevant adr_type.)
3811 //
3812 // In one special case (and more cases in the future), alias categories overlap.
3813 // The special alias category "Bot" (Compile::AliasIdxBot) includes all memory
3814 // states.  Therefore, if a MergeMem has only one contributing input W for Bot,
3815 // it is exactly equivalent to that state W:
3816 //   MergeMem(<Bot>: W) <==> W
3817 //
3818 // Usually, the merge has more than one input.  In that case, where inputs
3819 // overlap (i.e., one is Bot), the narrower alias type determines the memory
3820 // state for that type, and the wider alias type (Bot) fills in everywhere else:
3821 //   Load<5>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<5>(W,p)
3822 //   Load<6>( MergeMem(<Bot>: W, <6>: X), p ) <==> Load<6>(X,p)
3823 //
3824 // A merge can take a "wide" memory state as one of its narrow inputs.
3825 // This simply means that the merge observes out only the relevant parts of
3826 // the wide input.  That is, wide memory states arriving at narrow merge inputs
3827 // are implicitly "filtered" or "sliced" as necessary.  (This is rare.)
3828 //
3829 // These rules imply that MergeMem nodes may cascade (via their <Bot> links),
3830 // and that memory slices "leak through":
3831 //   MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y)) <==> MergeMem(<Bot>: W, <7>: Y)
3832 //
3833 // But, in such a cascade, repeated memory slices can "block the leak":
3834 //   MergeMem(<Bot>: MergeMem(<Bot>: W, <7>: Y), <7>: Y') <==> MergeMem(<Bot>: W, <7>: Y')
3835 //
3836 // In the last example, Y is not part of the combined memory state of the
3837 // outermost MergeMem.  The system must, of course, prevent unschedulable
3838 // memory states from arising, so you can be sure that the state Y is somehow
3839 // a precursor to state Y'.
3840 //
3841 //
3842 // REPRESENTATION OF MEMORY MERGES: The indexes used to address the Node::in array
3843 // of each MergeMemNode array are exactly the numerical alias indexes, including
3844 // but not limited to AliasIdxTop, AliasIdxBot, and AliasIdxRaw.  The functions
3845 // Compile::alias_type (and kin) produce and manage these indexes.
3846 //
3847 // By convention, the value of in(AliasIdxTop) (i.e., in(1)) is always the top node.
3848 // (Note that this provides quick access to the top node inside MergeMem methods,
3849 // without the need to reach out via TLS to Compile::current.)
3850 //
3851 // As a consequence of what was just described, a MergeMem that represents a full
3852 // memory state has an edge in(AliasIdxBot) which is a "wide" memory state,
3853 // containing all alias categories.
3854 //
3855 // MergeMem nodes never (?) have control inputs, so in(0) is NULL.
3856 //
3857 // All other edges in(N) (including in(AliasIdxRaw), which is in(3)) are either
3858 // a memory state for the alias type <N>, or else the top node, meaning that
3859 // there is no particular input for that alias type.  Note that the length of
3860 // a MergeMem is variable, and may be extended at any time to accommodate new
3861 // memory states at larger alias indexes.  When merges grow, they are of course
3862 // filled with "top" in the unused in() positions.
3863 //
3864 // This use of top is named "empty_memory()", or "empty_mem" (no-memory) as a variable.
3865 // (Top was chosen because it works smoothly with passes like GCM.)
3866 //
3867 // For convenience, we hardwire the alias index for TypeRawPtr::BOTTOM.  (It is
3868 // the type of random VM bits like TLS references.)  Since it is always the
3869 // first non-Bot memory slice, some low-level loops use it to initialize an
3870 // index variable:  for (i = AliasIdxRaw; i < req(); i++).
3871 //
3872 //
3873 // ACCESSORS:  There is a special accessor MergeMemNode::base_memory which returns
3874 // the distinguished "wide" state.  The accessor MergeMemNode::memory_at(N) returns
3875 // the memory state for alias type <N>, or (if there is no particular slice at <N>,
3876 // it returns the base memory.  To prevent bugs, memory_at does not accept <Top>
3877 // or <Bot> indexes.  The iterator MergeMemStream provides robust iteration over
3878 // MergeMem nodes or pairs of such nodes, ensuring that the non-top edges are visited.
3879 //
3880 // %%%% We may get rid of base_memory as a separate accessor at some point; it isn't
3881 // really that different from the other memory inputs.  An abbreviation called
3882 // "bot_memory()" for "memory_at(AliasIdxBot)" would keep code tidy.
3883 //
3884 //
3885 // PARTIAL MEMORY STATES:  During optimization, MergeMem nodes may arise that represent
3886 // partial memory states.  When a Phi splits through a MergeMem, the copy of the Phi
3887 // that "emerges though" the base memory will be marked as excluding the alias types
3888 // of the other (narrow-memory) copies which "emerged through" the narrow edges:
3889 //
3890 //   Phi<Bot>(U, MergeMem(<Bot>: W, <8>: Y))
3891 //     ==Ideal=>  MergeMem(<Bot>: Phi<Bot-8>(U, W), Phi<8>(U, Y))
3892 //
3893 // This strange "subtraction" effect is necessary to ensure IGVN convergence.
3894 // (It is currently unimplemented.)  As you can see, the resulting merge is
3895 // actually a disjoint union of memory states, rather than an overlay.
3896 //
3897 
3898 //------------------------------MergeMemNode-----------------------------------
3899 Node* MergeMemNode::make_empty_memory() {
3900   Node* empty_memory = (Node*) Compile::current()->top();
3901   assert(empty_memory->is_top(), "correct sentinel identity");
3902   return empty_memory;
3903 }
3904 
3905 MergeMemNode::MergeMemNode(Node *new_base) : Node(1+Compile::AliasIdxRaw) {
3906   init_class_id(Class_MergeMem);
3907   // all inputs are nullified in Node::Node(int)
3908   // set_input(0, NULL);  // no control input
3909 
3910   // Initialize the edges uniformly to top, for starters.
3911   Node* empty_mem = make_empty_memory();
3912   for (uint i = Compile::AliasIdxTop; i < req(); i++) {
3913     init_req(i,empty_mem);
3914   }
3915   assert(empty_memory() == empty_mem, "");
3916 
3917   if( new_base != NULL && new_base->is_MergeMem() ) {
3918     MergeMemNode* mdef = new_base->as_MergeMem();
3919     assert(mdef->empty_memory() == empty_mem, "consistent sentinels");
3920     for (MergeMemStream mms(this, mdef); mms.next_non_empty2(); ) {
3921       mms.set_memory(mms.memory2());
3922     }
3923     assert(base_memory() == mdef->base_memory(), "");
3924   } else {
3925     set_base_memory(new_base);
3926   }
3927 }
3928 
3929 // Make a new, untransformed MergeMem with the same base as 'mem'.
3930 // If mem is itself a MergeMem, populate the result with the same edges.
3931 MergeMemNode* MergeMemNode::make(Node* mem) {
3932   return new MergeMemNode(mem);
3933 }
3934 
3935 //------------------------------cmp--------------------------------------------
3936 uint MergeMemNode::hash() const { return NO_HASH; }
3937 uint MergeMemNode::cmp( const Node &n ) const {
3938   return (&n == this);          // Always fail except on self
3939 }
3940 
3941 //------------------------------Identity---------------------------------------
3942 Node* MergeMemNode::Identity(PhaseTransform *phase) {
3943   // Identity if this merge point does not record any interesting memory
3944   // disambiguations.
3945   Node* base_mem = base_memory();
3946   Node* empty_mem = empty_memory();
3947   if (base_mem != empty_mem) {  // Memory path is not dead?
3948     for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
3949       Node* mem = in(i);
3950       if (mem != empty_mem && mem != base_mem) {
3951         return this;            // Many memory splits; no change
3952       }
3953     }
3954   }
3955   return base_mem;              // No memory splits; ID on the one true input
3956 }
3957 
3958 //------------------------------Ideal------------------------------------------
3959 // This method is invoked recursively on chains of MergeMem nodes
3960 Node *MergeMemNode::Ideal(PhaseGVN *phase, bool can_reshape) {
3961   // Remove chain'd MergeMems
3962   //
3963   // This is delicate, because the each "in(i)" (i >= Raw) is interpreted
3964   // relative to the "in(Bot)".  Since we are patching both at the same time,
3965   // we have to be careful to read each "in(i)" relative to the old "in(Bot)",
3966   // but rewrite each "in(i)" relative to the new "in(Bot)".
3967   Node *progress = NULL;
3968 
3969 
3970   Node* old_base = base_memory();
3971   Node* empty_mem = empty_memory();
3972   if (old_base == empty_mem)
3973     return NULL; // Dead memory path.
3974 
3975   MergeMemNode* old_mbase;
3976   if (old_base != NULL && old_base->is_MergeMem())
3977     old_mbase = old_base->as_MergeMem();
3978   else
3979     old_mbase = NULL;
3980   Node* new_base = old_base;
3981 
3982   // simplify stacked MergeMems in base memory
3983   if (old_mbase)  new_base = old_mbase->base_memory();
3984 
3985   // the base memory might contribute new slices beyond my req()
3986   if (old_mbase)  grow_to_match(old_mbase);
3987 
3988   // Look carefully at the base node if it is a phi.
3989   PhiNode* phi_base;
3990   if (new_base != NULL && new_base->is_Phi())
3991     phi_base = new_base->as_Phi();
3992   else
3993     phi_base = NULL;
3994 
3995   Node*    phi_reg = NULL;
3996   uint     phi_len = (uint)-1;
3997   if (phi_base != NULL && !phi_base->is_copy()) {
3998     // do not examine phi if degraded to a copy
3999     phi_reg = phi_base->region();
4000     phi_len = phi_base->req();
4001     // see if the phi is unfinished
4002     for (uint i = 1; i < phi_len; i++) {
4003       if (phi_base->in(i) == NULL) {
4004         // incomplete phi; do not look at it yet!
4005         phi_reg = NULL;
4006         phi_len = (uint)-1;
4007         break;
4008       }
4009     }
4010   }
4011 
4012   // Note:  We do not call verify_sparse on entry, because inputs
4013   // can normalize to the base_memory via subsume_node or similar
4014   // mechanisms.  This method repairs that damage.
4015 
4016   assert(!old_mbase || old_mbase->is_empty_memory(empty_mem), "consistent sentinels");
4017 
4018   // Look at each slice.
4019   for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
4020     Node* old_in = in(i);
4021     // calculate the old memory value
4022     Node* old_mem = old_in;
4023     if (old_mem == empty_mem)  old_mem = old_base;
4024     assert(old_mem == memory_at(i), "");
4025 
4026     // maybe update (reslice) the old memory value
4027 
4028     // simplify stacked MergeMems
4029     Node* new_mem = old_mem;
4030     MergeMemNode* old_mmem;
4031     if (old_mem != NULL && old_mem->is_MergeMem())
4032       old_mmem = old_mem->as_MergeMem();
4033     else
4034       old_mmem = NULL;
4035     if (old_mmem == this) {
4036       // This can happen if loops break up and safepoints disappear.
4037       // A merge of BotPtr (default) with a RawPtr memory derived from a
4038       // safepoint can be rewritten to a merge of the same BotPtr with
4039       // the BotPtr phi coming into the loop.  If that phi disappears
4040       // also, we can end up with a self-loop of the mergemem.
4041       // In general, if loops degenerate and memory effects disappear,
4042       // a mergemem can be left looking at itself.  This simply means
4043       // that the mergemem's default should be used, since there is
4044       // no longer any apparent effect on this slice.
4045       // Note: If a memory slice is a MergeMem cycle, it is unreachable
4046       //       from start.  Update the input to TOP.
4047       new_mem = (new_base == this || new_base == empty_mem)? empty_mem : new_base;
4048     }
4049     else if (old_mmem != NULL) {
4050       new_mem = old_mmem->memory_at(i);
4051     }
4052     // else preceding memory was not a MergeMem
4053 
4054     // replace equivalent phis (unfortunately, they do not GVN together)
4055     if (new_mem != NULL && new_mem != new_base &&
4056         new_mem->req() == phi_len && new_mem->in(0) == phi_reg) {
4057       if (new_mem->is_Phi()) {
4058         PhiNode* phi_mem = new_mem->as_Phi();
4059         for (uint i = 1; i < phi_len; i++) {
4060           if (phi_base->in(i) != phi_mem->in(i)) {
4061             phi_mem = NULL;
4062             break;
4063           }
4064         }
4065         if (phi_mem != NULL) {
4066           // equivalent phi nodes; revert to the def
4067           new_mem = new_base;
4068         }
4069       }
4070     }
4071 
4072     // maybe store down a new value
4073     Node* new_in = new_mem;
4074     if (new_in == new_base)  new_in = empty_mem;
4075 
4076     if (new_in != old_in) {
4077       // Warning:  Do not combine this "if" with the previous "if"
4078       // A memory slice might have be be rewritten even if it is semantically
4079       // unchanged, if the base_memory value has changed.
4080       set_req(i, new_in);
4081       progress = this;          // Report progress
4082     }
4083   }
4084 
4085   if (new_base != old_base) {
4086     set_req(Compile::AliasIdxBot, new_base);
4087     // Don't use set_base_memory(new_base), because we need to update du.
4088     assert(base_memory() == new_base, "");
4089     progress = this;
4090   }
4091 
4092   if( base_memory() == this ) {
4093     // a self cycle indicates this memory path is dead
4094     set_req(Compile::AliasIdxBot, empty_mem);
4095   }
4096 
4097   // Resolve external cycles by calling Ideal on a MergeMem base_memory
4098   // Recursion must occur after the self cycle check above
4099   if( base_memory()->is_MergeMem() ) {
4100     MergeMemNode *new_mbase = base_memory()->as_MergeMem();
4101     Node *m = phase->transform(new_mbase);  // Rollup any cycles
4102     if( m != NULL && (m->is_top() ||
4103         m->is_MergeMem() && m->as_MergeMem()->base_memory() == empty_mem) ) {
4104       // propagate rollup of dead cycle to self
4105       set_req(Compile::AliasIdxBot, empty_mem);
4106     }
4107   }
4108 
4109   if( base_memory() == empty_mem ) {
4110     progress = this;
4111     // Cut inputs during Parse phase only.
4112     // During Optimize phase a dead MergeMem node will be subsumed by Top.
4113     if( !can_reshape ) {
4114       for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
4115         if( in(i) != empty_mem ) { set_req(i, empty_mem); }
4116       }
4117     }
4118   }
4119 
4120   if( !progress && base_memory()->is_Phi() && can_reshape ) {
4121     // Check if PhiNode::Ideal's "Split phis through memory merges"
4122     // transform should be attempted. Look for this->phi->this cycle.
4123     uint merge_width = req();
4124     if (merge_width > Compile::AliasIdxRaw) {
4125       PhiNode* phi = base_memory()->as_Phi();
4126       for( uint i = 1; i < phi->req(); ++i ) {// For all paths in
4127         if (phi->in(i) == this) {
4128           phase->is_IterGVN()->_worklist.push(phi);
4129           break;
4130         }
4131       }
4132     }
4133   }
4134 
4135   assert(progress || verify_sparse(), "please, no dups of base");
4136   return progress;
4137 }
4138 
4139 //-------------------------set_base_memory-------------------------------------
4140 void MergeMemNode::set_base_memory(Node *new_base) {
4141   Node* empty_mem = empty_memory();
4142   set_req(Compile::AliasIdxBot, new_base);
4143   assert(memory_at(req()) == new_base, "must set default memory");
4144   // Clear out other occurrences of new_base:
4145   if (new_base != empty_mem) {
4146     for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
4147       if (in(i) == new_base)  set_req(i, empty_mem);
4148     }
4149   }
4150 }
4151 
4152 //------------------------------out_RegMask------------------------------------
4153 const RegMask &MergeMemNode::out_RegMask() const {
4154   return RegMask::Empty;
4155 }
4156 
4157 //------------------------------dump_spec--------------------------------------
4158 #ifndef PRODUCT
4159 void MergeMemNode::dump_spec(outputStream *st) const {
4160   st->print(" {");
4161   Node* base_mem = base_memory();
4162   for( uint i = Compile::AliasIdxRaw; i < req(); i++ ) {
4163     Node* mem = (in(i) != NULL) ? memory_at(i) : base_mem;
4164     if (mem == base_mem) { st->print(" -"); continue; }
4165     st->print( " N%d:", mem->_idx );
4166     Compile::current()->get_adr_type(i)->dump_on(st);
4167   }
4168   st->print(" }");
4169 }
4170 #endif // !PRODUCT
4171 
4172 
4173 #ifdef ASSERT
4174 static bool might_be_same(Node* a, Node* b) {
4175   if (a == b)  return true;
4176   if (!(a->is_Phi() || b->is_Phi()))  return false;
4177   // phis shift around during optimization
4178   return true;  // pretty stupid...
4179 }
4180 
4181 // verify a narrow slice (either incoming or outgoing)
4182 static void verify_memory_slice(const MergeMemNode* m, int alias_idx, Node* n) {
4183   if (!VerifyAliases)       return;  // don't bother to verify unless requested
4184   if (is_error_reported())  return;  // muzzle asserts when debugging an error
4185   if (Node::in_dump())      return;  // muzzle asserts when printing
4186   assert(alias_idx >= Compile::AliasIdxRaw, "must not disturb base_memory or sentinel");
4187   assert(n != NULL, "");
4188   // Elide intervening MergeMem's
4189   while (n->is_MergeMem()) {
4190     n = n->as_MergeMem()->memory_at(alias_idx);
4191   }
4192   Compile* C = Compile::current();
4193   const TypePtr* n_adr_type = n->adr_type();
4194   if (n == m->empty_memory()) {
4195     // Implicit copy of base_memory()
4196   } else if (n_adr_type != TypePtr::BOTTOM) {
4197     assert(n_adr_type != NULL, "new memory must have a well-defined adr_type");
4198     assert(C->must_alias(n_adr_type, alias_idx), "new memory must match selected slice");
4199   } else {
4200     // A few places like make_runtime_call "know" that VM calls are narrow,
4201     // and can be used to update only the VM bits stored as TypeRawPtr::BOTTOM.
4202     bool expected_wide_mem = false;
4203     if (n == m->base_memory()) {
4204       expected_wide_mem = true;
4205     } else if (alias_idx == Compile::AliasIdxRaw ||
4206                n == m->memory_at(Compile::AliasIdxRaw)) {
4207       expected_wide_mem = true;
4208     } else if (!C->alias_type(alias_idx)->is_rewritable()) {
4209       // memory can "leak through" calls on channels that
4210       // are write-once.  Allow this also.
4211       expected_wide_mem = true;
4212     }
4213     assert(expected_wide_mem, "expected narrow slice replacement");
4214   }
4215 }
4216 #else // !ASSERT
4217 #define verify_memory_slice(m,i,n) (void)(0)  // PRODUCT version is no-op
4218 #endif
4219 
4220 
4221 //-----------------------------memory_at---------------------------------------
4222 Node* MergeMemNode::memory_at(uint alias_idx) const {
4223   assert(alias_idx >= Compile::AliasIdxRaw ||
4224          alias_idx == Compile::AliasIdxBot && Compile::current()->AliasLevel() == 0,
4225          "must avoid base_memory and AliasIdxTop");
4226 
4227   // Otherwise, it is a narrow slice.
4228   Node* n = alias_idx < req() ? in(alias_idx) : empty_memory();
4229   Compile *C = Compile::current();
4230   if (is_empty_memory(n)) {
4231     // the array is sparse; empty slots are the "top" node
4232     n = base_memory();
4233     assert(Node::in_dump()
4234            || n == NULL || n->bottom_type() == Type::TOP
4235            || n->adr_type() == NULL // address is TOP
4236            || n->adr_type() == TypePtr::BOTTOM
4237            || n->adr_type() == TypeRawPtr::BOTTOM
4238            || Compile::current()->AliasLevel() == 0,
4239            "must be a wide memory");
4240     // AliasLevel == 0 if we are organizing the memory states manually.
4241     // See verify_memory_slice for comments on TypeRawPtr::BOTTOM.
4242   } else {
4243     // make sure the stored slice is sane
4244     #ifdef ASSERT
4245     if (is_error_reported() || Node::in_dump()) {
4246     } else if (might_be_same(n, base_memory())) {
4247       // Give it a pass:  It is a mostly harmless repetition of the base.
4248       // This can arise normally from node subsumption during optimization.
4249     } else {
4250       verify_memory_slice(this, alias_idx, n);
4251     }
4252     #endif
4253   }
4254   return n;
4255 }
4256 
4257 //---------------------------set_memory_at-------------------------------------
4258 void MergeMemNode::set_memory_at(uint alias_idx, Node *n) {
4259   verify_memory_slice(this, alias_idx, n);
4260   Node* empty_mem = empty_memory();
4261   if (n == base_memory())  n = empty_mem;  // collapse default
4262   uint need_req = alias_idx+1;
4263   if (req() < need_req) {
4264     if (n == empty_mem)  return;  // already the default, so do not grow me
4265     // grow the sparse array
4266     do {
4267       add_req(empty_mem);
4268     } while (req() < need_req);
4269   }
4270   set_req( alias_idx, n );
4271 }
4272 
4273 
4274 
4275 //--------------------------iteration_setup------------------------------------
4276 void MergeMemNode::iteration_setup(const MergeMemNode* other) {
4277   if (other != NULL) {
4278     grow_to_match(other);
4279     // invariant:  the finite support of mm2 is within mm->req()
4280     #ifdef ASSERT
4281     for (uint i = req(); i < other->req(); i++) {
4282       assert(other->is_empty_memory(other->in(i)), "slice left uncovered");
4283     }
4284     #endif
4285   }
4286   // Replace spurious copies of base_memory by top.
4287   Node* base_mem = base_memory();
4288   if (base_mem != NULL && !base_mem->is_top()) {
4289     for (uint i = Compile::AliasIdxBot+1, imax = req(); i < imax; i++) {
4290       if (in(i) == base_mem)
4291         set_req(i, empty_memory());
4292     }
4293   }
4294 }
4295 
4296 //---------------------------grow_to_match-------------------------------------
4297 void MergeMemNode::grow_to_match(const MergeMemNode* other) {
4298   Node* empty_mem = empty_memory();
4299   assert(other->is_empty_memory(empty_mem), "consistent sentinels");
4300   // look for the finite support of the other memory
4301   for (uint i = other->req(); --i >= req(); ) {
4302     if (other->in(i) != empty_mem) {
4303       uint new_len = i+1;
4304       while (req() < new_len)  add_req(empty_mem);
4305       break;
4306     }
4307   }
4308 }
4309 
4310 //---------------------------verify_sparse-------------------------------------
4311 #ifndef PRODUCT
4312 bool MergeMemNode::verify_sparse() const {
4313   assert(is_empty_memory(make_empty_memory()), "sane sentinel");
4314   Node* base_mem = base_memory();
4315   // The following can happen in degenerate cases, since empty==top.
4316   if (is_empty_memory(base_mem))  return true;
4317   for (uint i = Compile::AliasIdxRaw; i < req(); i++) {
4318     assert(in(i) != NULL, "sane slice");
4319     if (in(i) == base_mem)  return false;  // should have been the sentinel value!
4320   }
4321   return true;
4322 }
4323 
4324 bool MergeMemStream::match_memory(Node* mem, const MergeMemNode* mm, int idx) {
4325   Node* n;
4326   n = mm->in(idx);
4327   if (mem == n)  return true;  // might be empty_memory()
4328   n = (idx == Compile::AliasIdxBot)? mm->base_memory(): mm->memory_at(idx);
4329   if (mem == n)  return true;
4330   while (n->is_Phi() && (n = n->as_Phi()->is_copy()) != NULL) {
4331     if (mem == n)  return true;
4332     if (n == NULL)  break;
4333   }
4334   return false;
4335 }
4336 #endif // !PRODUCT