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 #ifndef SHARE_VM_OPTO_MEMNODE_HPP
  26 #define SHARE_VM_OPTO_MEMNODE_HPP
  27 
  28 #include "opto/multnode.hpp"
  29 #include "opto/node.hpp"
  30 #include "opto/opcodes.hpp"
  31 #include "opto/type.hpp"
  32 
  33 // Portions of code courtesy of Clifford Click
  34 
  35 class MultiNode;
  36 class PhaseCCP;
  37 class PhaseTransform;
  38 
  39 //------------------------------MemNode----------------------------------------
  40 // Load or Store, possibly throwing a NULL pointer exception
  41 class MemNode : public Node {
  42 private:
  43   bool _unaligned;
  44 protected:
  45 #ifdef ASSERT
  46   const TypePtr* _adr_type;     // What kind of memory is being addressed?
  47 #endif
  48   virtual uint size_of() const;
  49 public:
  50   enum { Control,               // When is it safe to do this load?
  51          Memory,                // Chunk of memory is being loaded from
  52          Address,               // Actually address, derived from base
  53          ValueIn,               // Value to store
  54          OopStore               // Preceeding oop store, only in StoreCM
  55   };
  56   typedef enum { unordered = 0,
  57                  acquire,       // Load has to acquire or be succeeded by MemBarAcquire.
  58                  release        // Store has to release or be preceded by MemBarRelease.
  59   } MemOrd;
  60 protected:
  61   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at )
  62     : Node(c0,c1,c2   ), _unaligned(false) {
  63     init_class_id(Class_Mem);
  64     debug_only(_adr_type=at; adr_type();)
  65   }
  66   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3 )
  67     : Node(c0,c1,c2,c3), _unaligned(false) {
  68     init_class_id(Class_Mem);
  69     debug_only(_adr_type=at; adr_type();)
  70   }
  71   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3, Node *c4)
  72     : Node(c0,c1,c2,c3,c4), _unaligned(false) {
  73     init_class_id(Class_Mem);
  74     debug_only(_adr_type=at; adr_type();)
  75   }
  76 
  77   virtual Node* find_previous_arraycopy(PhaseTransform* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const { return NULL; }
  78 
  79 public:
  80   // Helpers for the optimizer.  Documented in memnode.cpp.
  81   static bool detect_ptr_independence(Node* p1, AllocateNode* a1,
  82                                       Node* p2, AllocateNode* a2,
  83                                       PhaseTransform* phase);
  84   static bool adr_phi_is_loop_invariant(Node* adr_phi, Node* cast);
  85 
  86   static Node *optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase);
  87   static Node *optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase);
  88   // This one should probably be a phase-specific function:
  89   static bool all_controls_dominate(Node* dom, Node* sub);
  90 
  91   virtual const class TypePtr *adr_type() const;  // returns bottom_type of address
  92 
  93   // Shared code for Ideal methods:
  94   Node *Ideal_common(PhaseGVN *phase, bool can_reshape);  // Return -1 for short-circuit NULL.
  95 
  96   // Helper function for adr_type() implementations.
  97   static const TypePtr* calculate_adr_type(const Type* t, const TypePtr* cross_check = NULL);
  98 
  99   // Raw access function, to allow copying of adr_type efficiently in
 100   // product builds and retain the debug info for debug builds.
 101   const TypePtr *raw_adr_type() const {
 102 #ifdef ASSERT
 103     return _adr_type;
 104 #else
 105     return 0;
 106 #endif
 107   }
 108 
 109   // Map a load or store opcode to its corresponding store opcode.
 110   // (Return -1 if unknown.)
 111   virtual int store_Opcode() const { return -1; }
 112 
 113   // What is the type of the value in memory?  (T_VOID mean "unspecified".)
 114   virtual BasicType memory_type() const = 0;
 115   virtual int memory_size() const {
 116 #ifdef ASSERT
 117     return type2aelembytes(memory_type(), true);
 118 #else
 119     return type2aelembytes(memory_type());
 120 #endif
 121   }
 122 
 123   // Search through memory states which precede this node (load or store).
 124   // Look for an exact match for the address, with no intervening
 125   // aliased stores.
 126   Node* find_previous_store(PhaseTransform* phase);
 127 
 128   // Can this node (load or store) accurately see a stored value in
 129   // the given memory state?  (The state may or may not be in(Memory).)
 130   Node* can_see_stored_value(Node* st, PhaseTransform* phase) const;
 131 
 132   void set_unaligned() { _unaligned = true; }
 133   bool is_unaligned() const { return _unaligned; }
 134 
 135 #ifndef PRODUCT
 136   static void dump_adr_type(const Node* mem, const TypePtr* adr_type, outputStream *st);
 137   virtual void dump_spec(outputStream *st) const;
 138 #endif
 139 };
 140 
 141 //------------------------------LoadNode---------------------------------------
 142 // Load value; requires Memory and Address
 143 class LoadNode : public MemNode {
 144 public:
 145   // Some loads (from unsafe) should be pinned: they don't depend only
 146   // on the dominating test.  The boolean field _depends_only_on_test
 147   // below records whether that node depends only on the dominating
 148   // test.
 149   // Methods used to build LoadNodes pass an argument of type enum
 150   // ControlDependency instead of a boolean because those methods
 151   // typically have multiple boolean parameters with default values:
 152   // passing the wrong boolean to one of these parameters by mistake
 153   // goes easily unnoticed. Using an enum, the compiler can check that
 154   // the type of a value and the type of the parameter match.
 155   enum ControlDependency {
 156     Pinned,
 157     DependsOnlyOnTest
 158   };
 159 private:
 160   // LoadNode::hash() doesn't take the _depends_only_on_test field
 161   // into account: If the graph already has a non-pinned LoadNode and
 162   // we add a pinned LoadNode with the same inputs, it's safe for GVN
 163   // to replace the pinned LoadNode with the non-pinned LoadNode,
 164   // otherwise it wouldn't be safe to have a non pinned LoadNode with
 165   // those inputs in the first place. If the graph already has a
 166   // pinned LoadNode and we add a non pinned LoadNode with the same
 167   // inputs, it's safe (but suboptimal) for GVN to replace the
 168   // non-pinned LoadNode by the pinned LoadNode.
 169   bool _depends_only_on_test;
 170 
 171   // On platforms with weak memory ordering (e.g., PPC, Ia64) we distinguish
 172   // loads that can be reordered, and such requiring acquire semantics to
 173   // adhere to the Java specification.  The required behaviour is stored in
 174   // this field.
 175   const MemOrd _mo;
 176 
 177 protected:
 178   virtual uint cmp(const Node &n) const;
 179   virtual uint size_of() const; // Size is bigger
 180   // Should LoadNode::Ideal() attempt to remove control edges?
 181   virtual bool can_remove_control() const;
 182   const Type* const _type;      // What kind of value is loaded?
 183 
 184   virtual Node* find_previous_arraycopy(PhaseTransform* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const;
 185 public:
 186 
 187   LoadNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *rt, MemOrd mo, ControlDependency control_dependency)
 188     : MemNode(c,mem,adr,at), _type(rt), _mo(mo), _depends_only_on_test(control_dependency == DependsOnlyOnTest) {
 189     init_class_id(Class_Load);
 190   }
 191   inline bool is_unordered() const { return !is_acquire(); }
 192   inline bool is_acquire() const {
 193     assert(_mo == unordered || _mo == acquire, "unexpected");
 194     return _mo == acquire;
 195   }
 196 
 197   // Polymorphic factory method:
 198    static Node* make(PhaseGVN& gvn, Node *c, Node *mem, Node *adr,
 199                      const TypePtr* at, const Type *rt, BasicType bt,
 200                      MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest);
 201 
 202   virtual uint hash()   const;  // Check the type
 203 
 204   // Handle algebraic identities here.  If we have an identity, return the Node
 205   // we are equivalent to.  We look for Load of a Store.
 206   virtual Node *Identity( PhaseTransform *phase );
 207 
 208   // If the load is from Field memory and the pointer is non-null, it might be possible to
 209   // zero out the control input.
 210   // If the offset is constant and the base is an object allocation,
 211   // try to hook me up to the exact initializing store.
 212   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 213 
 214   // Split instance field load through Phi.
 215   Node* split_through_phi(PhaseGVN *phase);
 216 
 217   // Recover original value from boxed values
 218   Node *eliminate_autobox(PhaseGVN *phase);
 219 
 220   // Compute a new Type for this node.  Basically we just do the pre-check,
 221   // then call the virtual add() to set the type.
 222   virtual const Type *Value( PhaseTransform *phase ) const;
 223 
 224   // Common methods for LoadKlass and LoadNKlass nodes.
 225   const Type *klass_value_common( PhaseTransform *phase ) const;
 226   Node *klass_identity_common( PhaseTransform *phase );
 227 
 228   virtual uint ideal_reg() const;
 229   virtual const Type *bottom_type() const;
 230   // Following method is copied from TypeNode:
 231   void set_type(const Type* t) {
 232     assert(t != NULL, "sanity");
 233     debug_only(uint check_hash = (VerifyHashTableKeys && _hash_lock) ? hash() : NO_HASH);
 234     *(const Type**)&_type = t;   // cast away const-ness
 235     // If this node is in the hash table, make sure it doesn't need a rehash.
 236     assert(check_hash == NO_HASH || check_hash == hash(), "type change must preserve hash code");
 237   }
 238   const Type* type() const { assert(_type != NULL, "sanity"); return _type; };
 239 
 240   // Do not match memory edge
 241   virtual uint match_edge(uint idx) const;
 242 
 243   // Map a load opcode to its corresponding store opcode.
 244   virtual int store_Opcode() const = 0;
 245 
 246   // Check if the load's memory input is a Phi node with the same control.
 247   bool is_instance_field_load_with_local_phi(Node* ctrl);
 248 
 249 #ifndef PRODUCT
 250   virtual void dump_spec(outputStream *st) const;
 251 #endif
 252 #ifdef ASSERT
 253   // Helper function to allow a raw load without control edge for some cases
 254   static bool is_immutable_value(Node* adr);
 255 #endif
 256 protected:
 257   const Type* load_array_final_field(const TypeKlassPtr *tkls,
 258                                      ciKlass* klass) const;
 259 
 260   Node* can_see_arraycopy_value(Node* st, PhaseTransform* phase) const;
 261 
 262   // depends_only_on_test is almost always true, and needs to be almost always
 263   // true to enable key hoisting & commoning optimizations.  However, for the
 264   // special case of RawPtr loads from TLS top & end, and other loads performed by
 265   // GC barriers, the control edge carries the dependence preventing hoisting past
 266   // a Safepoint instead of the memory edge.  (An unfortunate consequence of having
 267   // Safepoints not set Raw Memory; itself an unfortunate consequence of having Nodes
 268   // which produce results (new raw memory state) inside of loops preventing all
 269   // manner of other optimizations).  Basically, it's ugly but so is the alternative.
 270   // See comment in macro.cpp, around line 125 expand_allocate_common().
 271   virtual bool depends_only_on_test() const { return adr_type() != TypeRawPtr::BOTTOM && _depends_only_on_test; }
 272 };
 273 
 274 //------------------------------LoadBNode--------------------------------------
 275 // Load a byte (8bits signed) from memory
 276 class LoadBNode : public LoadNode {
 277 public:
 278   LoadBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 279     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 280   virtual int Opcode() const;
 281   virtual uint ideal_reg() const { return Op_RegI; }
 282   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 283   virtual const Type *Value(PhaseTransform *phase) const;
 284   virtual int store_Opcode() const { return Op_StoreB; }
 285   virtual BasicType memory_type() const { return T_BYTE; }
 286 };
 287 
 288 //------------------------------LoadUBNode-------------------------------------
 289 // Load a unsigned byte (8bits unsigned) from memory
 290 class LoadUBNode : public LoadNode {
 291 public:
 292   LoadUBNode(Node* c, Node* mem, Node* adr, const TypePtr* at, const TypeInt* ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 293     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 294   virtual int Opcode() const;
 295   virtual uint ideal_reg() const { return Op_RegI; }
 296   virtual Node* Ideal(PhaseGVN *phase, bool can_reshape);
 297   virtual const Type *Value(PhaseTransform *phase) const;
 298   virtual int store_Opcode() const { return Op_StoreB; }
 299   virtual BasicType memory_type() const { return T_BYTE; }
 300 };
 301 
 302 //------------------------------LoadUSNode-------------------------------------
 303 // Load an unsigned short/char (16bits unsigned) from memory
 304 class LoadUSNode : public LoadNode {
 305 public:
 306   LoadUSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 307     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 308   virtual int Opcode() const;
 309   virtual uint ideal_reg() const { return Op_RegI; }
 310   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 311   virtual const Type *Value(PhaseTransform *phase) const;
 312   virtual int store_Opcode() const { return Op_StoreC; }
 313   virtual BasicType memory_type() const { return T_CHAR; }
 314 };
 315 
 316 //------------------------------LoadSNode--------------------------------------
 317 // Load a short (16bits signed) from memory
 318 class LoadSNode : public LoadNode {
 319 public:
 320   LoadSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 321     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 322   virtual int Opcode() const;
 323   virtual uint ideal_reg() const { return Op_RegI; }
 324   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 325   virtual const Type *Value(PhaseTransform *phase) const;
 326   virtual int store_Opcode() const { return Op_StoreC; }
 327   virtual BasicType memory_type() const { return T_SHORT; }
 328 };
 329 
 330 //------------------------------LoadINode--------------------------------------
 331 // Load an integer from memory
 332 class LoadINode : public LoadNode {
 333 public:
 334   LoadINode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 335     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 336   virtual int Opcode() const;
 337   virtual uint ideal_reg() const { return Op_RegI; }
 338   virtual int store_Opcode() const { return Op_StoreI; }
 339   virtual BasicType memory_type() const { return T_INT; }
 340 };
 341 
 342 //------------------------------LoadRangeNode----------------------------------
 343 // Load an array length from the array
 344 class LoadRangeNode : public LoadINode {
 345 public:
 346   LoadRangeNode(Node *c, Node *mem, Node *adr, const TypeInt *ti = TypeInt::POS)
 347     : LoadINode(c, mem, adr, TypeAryPtr::RANGE, ti, MemNode::unordered) {}
 348   virtual int Opcode() const;
 349   virtual const Type *Value( PhaseTransform *phase ) const;
 350   virtual Node *Identity( PhaseTransform *phase );
 351   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 352 };
 353 
 354 //------------------------------LoadLNode--------------------------------------
 355 // Load a long from memory
 356 class LoadLNode : public LoadNode {
 357   virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
 358   virtual uint cmp( const Node &n ) const {
 359     return _require_atomic_access == ((LoadLNode&)n)._require_atomic_access
 360       && LoadNode::cmp(n);
 361   }
 362   virtual uint size_of() const { return sizeof(*this); }
 363   const bool _require_atomic_access;  // is piecewise load forbidden?
 364 
 365 public:
 366   LoadLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeLong *tl,
 367             MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
 368     : LoadNode(c, mem, adr, at, tl, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
 369   virtual int Opcode() const;
 370   virtual uint ideal_reg() const { return Op_RegL; }
 371   virtual int store_Opcode() const { return Op_StoreL; }
 372   virtual BasicType memory_type() const { return T_LONG; }
 373   bool require_atomic_access() const { return _require_atomic_access; }
 374   static LoadLNode* make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type,
 375                                 const Type* rt, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest);
 376 #ifndef PRODUCT
 377   virtual void dump_spec(outputStream *st) const {
 378     LoadNode::dump_spec(st);
 379     if (_require_atomic_access)  st->print(" Atomic!");
 380   }
 381 #endif
 382 };
 383 
 384 //------------------------------LoadL_unalignedNode----------------------------
 385 // Load a long from unaligned memory
 386 class LoadL_unalignedNode : public LoadLNode {
 387 public:
 388   LoadL_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 389     : LoadLNode(c, mem, adr, at, TypeLong::LONG, mo, control_dependency) {}
 390   virtual int Opcode() const;
 391 };
 392 
 393 //------------------------------LoadFNode--------------------------------------
 394 // Load a float (64 bits) from memory
 395 class LoadFNode : public LoadNode {
 396 public:
 397   LoadFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 398     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 399   virtual int Opcode() const;
 400   virtual uint ideal_reg() const { return Op_RegF; }
 401   virtual int store_Opcode() const { return Op_StoreF; }
 402   virtual BasicType memory_type() const { return T_FLOAT; }
 403 };
 404 
 405 //------------------------------LoadDNode--------------------------------------
 406 // Load a double (64 bits) from memory
 407 class LoadDNode : public LoadNode {
 408   virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
 409   virtual uint cmp( const Node &n ) const {
 410     return _require_atomic_access == ((LoadDNode&)n)._require_atomic_access
 411       && LoadNode::cmp(n);
 412   }
 413   virtual uint size_of() const { return sizeof(*this); }
 414   const bool _require_atomic_access;  // is piecewise load forbidden?
 415 
 416 public:
 417   LoadDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t,
 418             MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
 419     : LoadNode(c, mem, adr, at, t, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
 420   virtual int Opcode() const;
 421   virtual uint ideal_reg() const { return Op_RegD; }
 422   virtual int store_Opcode() const { return Op_StoreD; }
 423   virtual BasicType memory_type() const { return T_DOUBLE; }
 424   bool require_atomic_access() const { return _require_atomic_access; }
 425   static LoadDNode* make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type,
 426                                 const Type* rt, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest);
 427 #ifndef PRODUCT
 428   virtual void dump_spec(outputStream *st) const {
 429     LoadNode::dump_spec(st);
 430     if (_require_atomic_access)  st->print(" Atomic!");
 431   }
 432 #endif
 433 };
 434 
 435 //------------------------------LoadD_unalignedNode----------------------------
 436 // Load a double from unaligned memory
 437 class LoadD_unalignedNode : public LoadDNode {
 438 public:
 439   LoadD_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 440     : LoadDNode(c, mem, adr, at, Type::DOUBLE, mo, control_dependency) {}
 441   virtual int Opcode() const;
 442 };
 443 
 444 //------------------------------LoadPNode--------------------------------------
 445 // Load a pointer from memory (either object or array)
 446 class LoadPNode : public LoadNode {
 447 public:
 448   LoadPNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const TypePtr* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 449     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 450   virtual int Opcode() const;
 451   virtual uint ideal_reg() const { return Op_RegP; }
 452   virtual int store_Opcode() const { return Op_StoreP; }
 453   virtual BasicType memory_type() const { return T_ADDRESS; }
 454 };
 455 
 456 
 457 //------------------------------LoadNNode--------------------------------------
 458 // Load a narrow oop from memory (either object or array)
 459 class LoadNNode : public LoadNode {
 460 public:
 461   LoadNNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const Type* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 462     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 463   virtual int Opcode() const;
 464   virtual uint ideal_reg() const { return Op_RegN; }
 465   virtual int store_Opcode() const { return Op_StoreN; }
 466   virtual BasicType memory_type() const { return T_NARROWOOP; }
 467 };
 468 
 469 //------------------------------LoadKlassNode----------------------------------
 470 // Load a Klass from an object
 471 class LoadKlassNode : public LoadPNode {
 472 protected:
 473   // In most cases, LoadKlassNode does not have the control input set. If the control
 474   // input is set, it must not be removed (by LoadNode::Ideal()).
 475   virtual bool can_remove_control() const;
 476 public:
 477   LoadKlassNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const TypeKlassPtr *tk, MemOrd mo)
 478     : LoadPNode(c, mem, adr, at, tk, mo) {}
 479   virtual int Opcode() const;
 480   virtual const Type *Value( PhaseTransform *phase ) const;
 481   virtual Node *Identity( PhaseTransform *phase );
 482   virtual bool depends_only_on_test() const { return true; }
 483 
 484   // Polymorphic factory method:
 485   static Node* make(PhaseGVN& gvn, Node* ctl, Node* mem, Node* adr, const TypePtr* at,
 486                     const TypeKlassPtr* tk = TypeKlassPtr::OBJECT);
 487 };
 488 
 489 //------------------------------LoadNKlassNode---------------------------------
 490 // Load a narrow Klass from an object.
 491 class LoadNKlassNode : public LoadNNode {
 492 public:
 493   LoadNKlassNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const TypeNarrowKlass *tk, MemOrd mo)
 494     : LoadNNode(c, mem, adr, at, tk, mo) {}
 495   virtual int Opcode() const;
 496   virtual uint ideal_reg() const { return Op_RegN; }
 497   virtual int store_Opcode() const { return Op_StoreNKlass; }
 498   virtual BasicType memory_type() const { return T_NARROWKLASS; }
 499 
 500   virtual const Type *Value( PhaseTransform *phase ) const;
 501   virtual Node *Identity( PhaseTransform *phase );
 502   virtual bool depends_only_on_test() const { return true; }
 503 };
 504 
 505 
 506 //------------------------------StoreNode--------------------------------------
 507 // Store value; requires Store, Address and Value
 508 class StoreNode : public MemNode {
 509 private:
 510   // On platforms with weak memory ordering (e.g., PPC, Ia64) we distinguish
 511   // stores that can be reordered, and such requiring release semantics to
 512   // adhere to the Java specification.  The required behaviour is stored in
 513   // this field.
 514   const MemOrd _mo;
 515   // Needed for proper cloning.
 516   virtual uint size_of() const { return sizeof(*this); }
 517 protected:
 518   virtual uint cmp( const Node &n ) const;
 519   virtual bool depends_only_on_test() const { return false; }
 520 
 521   Node *Ideal_masked_input       (PhaseGVN *phase, uint mask);
 522   Node *Ideal_sign_extended_input(PhaseGVN *phase, int  num_bits);
 523 
 524 public:
 525   // We must ensure that stores of object references will be visible
 526   // only after the object's initialization. So the callers of this
 527   // procedure must indicate that the store requires `release'
 528   // semantics, if the stored value is an object reference that might
 529   // point to a new object and may become externally visible.
 530   StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 531     : MemNode(c, mem, adr, at, val), _mo(mo) {
 532     init_class_id(Class_Store);
 533   }
 534   StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store, MemOrd mo)
 535     : MemNode(c, mem, adr, at, val, oop_store), _mo(mo) {
 536     init_class_id(Class_Store);
 537   }
 538 
 539   inline bool is_unordered() const { return !is_release(); }
 540   inline bool is_release() const {
 541     assert((_mo == unordered || _mo == release), "unexpected");
 542     return _mo == release;
 543   }
 544 
 545   // Conservatively release stores of object references in order to
 546   // ensure visibility of object initialization.
 547   static inline MemOrd release_if_reference(const BasicType t) {
 548 #ifdef AARCH64
 549     // AArch64 doesn't need a release store here because object
 550     // initialization contains the necessary barriers.
 551     return unordered;
 552 #else
 553     const MemOrd mo = (t == T_ARRAY ||
 554                        t == T_ADDRESS || // Might be the address of an object reference (`boxing').
 555                        t == T_OBJECT) ? release : unordered;
 556     return mo;
 557 #endif
 558   }
 559 
 560   // Polymorphic factory method
 561   //
 562   // We must ensure that stores of object references will be visible
 563   // only after the object's initialization. So the callers of this
 564   // procedure must indicate that the store requires `release'
 565   // semantics, if the stored value is an object reference that might
 566   // point to a new object and may become externally visible.
 567   static StoreNode* make(PhaseGVN& gvn, Node *c, Node *mem, Node *adr,
 568                          const TypePtr* at, Node *val, BasicType bt, MemOrd mo);
 569 
 570   virtual uint hash() const;    // Check the type
 571 
 572   // If the store is to Field memory and the pointer is non-null, we can
 573   // zero out the control input.
 574   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 575 
 576   // Compute a new Type for this node.  Basically we just do the pre-check,
 577   // then call the virtual add() to set the type.
 578   virtual const Type *Value( PhaseTransform *phase ) const;
 579 
 580   // Check for identity function on memory (Load then Store at same address)
 581   virtual Node *Identity( PhaseTransform *phase );
 582 
 583   // Do not match memory edge
 584   virtual uint match_edge(uint idx) const;
 585 
 586   virtual const Type *bottom_type() const;  // returns Type::MEMORY
 587 
 588   // Map a store opcode to its corresponding own opcode, trivially.
 589   virtual int store_Opcode() const { return Opcode(); }
 590 
 591   // have all possible loads of the value stored been optimized away?
 592   bool value_never_loaded(PhaseTransform *phase) const;
 593 };
 594 
 595 //------------------------------StoreBNode-------------------------------------
 596 // Store byte to memory
 597 class StoreBNode : public StoreNode {
 598 public:
 599   StoreBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 600     : StoreNode(c, mem, adr, at, val, mo) {}
 601   virtual int Opcode() const;
 602   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 603   virtual BasicType memory_type() const { return T_BYTE; }
 604 };
 605 
 606 //------------------------------StoreCNode-------------------------------------
 607 // Store char/short to memory
 608 class StoreCNode : public StoreNode {
 609 public:
 610   StoreCNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 611     : StoreNode(c, mem, adr, at, val, mo) {}
 612   virtual int Opcode() const;
 613   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 614   virtual BasicType memory_type() const { return T_CHAR; }
 615 };
 616 
 617 //------------------------------StoreINode-------------------------------------
 618 // Store int to memory
 619 class StoreINode : public StoreNode {
 620 public:
 621   StoreINode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 622     : StoreNode(c, mem, adr, at, val, mo) {}
 623   virtual int Opcode() const;
 624   virtual BasicType memory_type() const { return T_INT; }
 625 };
 626 
 627 //------------------------------StoreLNode-------------------------------------
 628 // Store long to memory
 629 class StoreLNode : public StoreNode {
 630   virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
 631   virtual uint cmp( const Node &n ) const {
 632     return _require_atomic_access == ((StoreLNode&)n)._require_atomic_access
 633       && StoreNode::cmp(n);
 634   }
 635   virtual uint size_of() const { return sizeof(*this); }
 636   const bool _require_atomic_access;  // is piecewise store forbidden?
 637 
 638 public:
 639   StoreLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo, bool require_atomic_access = false)
 640     : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
 641   virtual int Opcode() const;
 642   virtual BasicType memory_type() const { return T_LONG; }
 643   bool require_atomic_access() const { return _require_atomic_access; }
 644   static StoreLNode* make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo);
 645 #ifndef PRODUCT
 646   virtual void dump_spec(outputStream *st) const {
 647     StoreNode::dump_spec(st);
 648     if (_require_atomic_access)  st->print(" Atomic!");
 649   }
 650 #endif
 651 };
 652 
 653 //------------------------------StoreFNode-------------------------------------
 654 // Store float to memory
 655 class StoreFNode : public StoreNode {
 656 public:
 657   StoreFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 658     : StoreNode(c, mem, adr, at, val, mo) {}
 659   virtual int Opcode() const;
 660   virtual BasicType memory_type() const { return T_FLOAT; }
 661 };
 662 
 663 //------------------------------StoreDNode-------------------------------------
 664 // Store double to memory
 665 class StoreDNode : public StoreNode {
 666   virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
 667   virtual uint cmp( const Node &n ) const {
 668     return _require_atomic_access == ((StoreDNode&)n)._require_atomic_access
 669       && StoreNode::cmp(n);
 670   }
 671   virtual uint size_of() const { return sizeof(*this); }
 672   const bool _require_atomic_access;  // is piecewise store forbidden?
 673 public:
 674   StoreDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val,
 675              MemOrd mo, bool require_atomic_access = false)
 676     : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
 677   virtual int Opcode() const;
 678   virtual BasicType memory_type() const { return T_DOUBLE; }
 679   bool require_atomic_access() const { return _require_atomic_access; }
 680   static StoreDNode* make_atomic(Node* ctl, Node* mem, Node* adr, const TypePtr* adr_type, Node* val, MemOrd mo);
 681 #ifndef PRODUCT
 682   virtual void dump_spec(outputStream *st) const {
 683     StoreNode::dump_spec(st);
 684     if (_require_atomic_access)  st->print(" Atomic!");
 685   }
 686 #endif
 687 
 688 };
 689 
 690 //------------------------------StorePNode-------------------------------------
 691 // Store pointer to memory
 692 class StorePNode : public StoreNode {
 693 public:
 694   StorePNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 695     : StoreNode(c, mem, adr, at, val, mo) {}
 696   virtual int Opcode() const;
 697   virtual BasicType memory_type() const { return T_ADDRESS; }
 698 };
 699 
 700 //------------------------------StoreNNode-------------------------------------
 701 // Store narrow oop to memory
 702 class StoreNNode : public StoreNode {
 703 public:
 704   StoreNNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 705     : StoreNode(c, mem, adr, at, val, mo) {}
 706   virtual int Opcode() const;
 707   virtual BasicType memory_type() const { return T_NARROWOOP; }
 708 };
 709 
 710 //------------------------------StoreNKlassNode--------------------------------------
 711 // Store narrow klass to memory
 712 class StoreNKlassNode : public StoreNNode {
 713 public:
 714   StoreNKlassNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 715     : StoreNNode(c, mem, adr, at, val, mo) {}
 716   virtual int Opcode() const;
 717   virtual BasicType memory_type() const { return T_NARROWKLASS; }
 718 };
 719 
 720 //------------------------------StoreCMNode-----------------------------------
 721 // Store card-mark byte to memory for CM
 722 // The last StoreCM before a SafePoint must be preserved and occur after its "oop" store
 723 // Preceeding equivalent StoreCMs may be eliminated.
 724 class StoreCMNode : public StoreNode {
 725  private:
 726   virtual uint hash() const { return StoreNode::hash() + _oop_alias_idx; }
 727   virtual uint cmp( const Node &n ) const {
 728     return _oop_alias_idx == ((StoreCMNode&)n)._oop_alias_idx
 729       && StoreNode::cmp(n);
 730   }
 731   virtual uint size_of() const { return sizeof(*this); }
 732   int _oop_alias_idx;   // The alias_idx of OopStore
 733 
 734 public:
 735   StoreCMNode( Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store, int oop_alias_idx ) :
 736     StoreNode(c, mem, adr, at, val, oop_store, MemNode::release),
 737     _oop_alias_idx(oop_alias_idx) {
 738     assert(_oop_alias_idx >= Compile::AliasIdxRaw ||
 739            _oop_alias_idx == Compile::AliasIdxBot && Compile::current()->AliasLevel() == 0,
 740            "bad oop alias idx");
 741   }
 742   virtual int Opcode() const;
 743   virtual Node *Identity( PhaseTransform *phase );
 744   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 745   virtual const Type *Value( PhaseTransform *phase ) const;
 746   virtual BasicType memory_type() const { return T_VOID; } // unspecific
 747   int oop_alias_idx() const { return _oop_alias_idx; }
 748 };
 749 
 750 //------------------------------LoadPLockedNode---------------------------------
 751 // Load-locked a pointer from memory (either object or array).
 752 // On Sparc & Intel this is implemented as a normal pointer load.
 753 // On PowerPC and friends it's a real load-locked.
 754 class LoadPLockedNode : public LoadPNode {
 755 public:
 756   LoadPLockedNode(Node *c, Node *mem, Node *adr, MemOrd mo)
 757     : LoadPNode(c, mem, adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, mo) {}
 758   virtual int Opcode() const;
 759   virtual int store_Opcode() const { return Op_StorePConditional; }
 760   virtual bool depends_only_on_test() const { return true; }
 761 };
 762 
 763 //------------------------------SCMemProjNode---------------------------------------
 764 // This class defines a projection of the memory  state of a store conditional node.
 765 // These nodes return a value, but also update memory.
 766 class SCMemProjNode : public ProjNode {
 767 public:
 768   enum {SCMEMPROJCON = (uint)-2};
 769   SCMemProjNode( Node *src) : ProjNode( src, SCMEMPROJCON) { }
 770   virtual int Opcode() const;
 771   virtual bool      is_CFG() const  { return false; }
 772   virtual const Type *bottom_type() const {return Type::MEMORY;}
 773   virtual const TypePtr *adr_type() const {
 774     Node* ctrl = in(0);
 775     if (ctrl == NULL)  return NULL; // node is dead
 776     return ctrl->in(MemNode::Memory)->adr_type();
 777   }
 778   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 779   virtual const Type *Value( PhaseTransform *phase ) const;
 780 #ifndef PRODUCT
 781   virtual void dump_spec(outputStream *st) const {};
 782 #endif
 783 };
 784 
 785 //------------------------------LoadStoreNode---------------------------
 786 // Note: is_Mem() method returns 'true' for this class.
 787 class LoadStoreNode : public Node {
 788 private:
 789   const Type* const _type;      // What kind of value is loaded?
 790   const TypePtr* _adr_type;     // What kind of memory is being addressed?
 791   virtual uint size_of() const; // Size is bigger
 792 public:
 793   LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required );
 794   virtual bool depends_only_on_test() const { return false; }
 795   virtual uint match_edge(uint idx) const { return idx == MemNode::Address || idx == MemNode::ValueIn; }
 796 
 797   virtual const Type *bottom_type() const { return _type; }
 798   virtual uint ideal_reg() const;
 799   virtual const class TypePtr *adr_type() const { return _adr_type; }  // returns bottom_type of address
 800 
 801   bool result_not_used() const;
 802 };
 803 
 804 class LoadStoreConditionalNode : public LoadStoreNode {
 805 public:
 806   enum {
 807     ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
 808   };
 809   LoadStoreConditionalNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex);
 810 };
 811 
 812 //------------------------------StorePConditionalNode---------------------------
 813 // Conditionally store pointer to memory, if no change since prior
 814 // load-locked.  Sets flags for success or failure of the store.
 815 class StorePConditionalNode : public LoadStoreConditionalNode {
 816 public:
 817   StorePConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ll ) : LoadStoreConditionalNode(c, mem, adr, val, ll) { }
 818   virtual int Opcode() const;
 819   // Produces flags
 820   virtual uint ideal_reg() const { return Op_RegFlags; }
 821 };
 822 
 823 //------------------------------StoreIConditionalNode---------------------------
 824 // Conditionally store int to memory, if no change since prior
 825 // load-locked.  Sets flags for success or failure of the store.
 826 class StoreIConditionalNode : public LoadStoreConditionalNode {
 827 public:
 828   StoreIConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ii ) : LoadStoreConditionalNode(c, mem, adr, val, ii) { }
 829   virtual int Opcode() const;
 830   // Produces flags
 831   virtual uint ideal_reg() const { return Op_RegFlags; }
 832 };
 833 
 834 //------------------------------StoreLConditionalNode---------------------------
 835 // Conditionally store long to memory, if no change since prior
 836 // load-locked.  Sets flags for success or failure of the store.
 837 class StoreLConditionalNode : public LoadStoreConditionalNode {
 838 public:
 839   StoreLConditionalNode( Node *c, Node *mem, Node *adr, Node *val, Node *ll ) : LoadStoreConditionalNode(c, mem, adr, val, ll) { }
 840   virtual int Opcode() const;
 841   // Produces flags
 842   virtual uint ideal_reg() const { return Op_RegFlags; }
 843 };
 844 
 845 
 846 //------------------------------CompareAndSwapLNode---------------------------
 847 class CompareAndSwapLNode : public LoadStoreConditionalNode {
 848 public:
 849   CompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreConditionalNode(c, mem, adr, val, ex) { }
 850   virtual int Opcode() const;
 851 };
 852 
 853 
 854 //------------------------------CompareAndSwapINode---------------------------
 855 class CompareAndSwapINode : public LoadStoreConditionalNode {
 856 public:
 857   CompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreConditionalNode(c, mem, adr, val, ex) { }
 858   virtual int Opcode() const;
 859 };
 860 
 861 
 862 //------------------------------CompareAndSwapPNode---------------------------
 863 class CompareAndSwapPNode : public LoadStoreConditionalNode {
 864 public:
 865   CompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreConditionalNode(c, mem, adr, val, ex) { }
 866   virtual int Opcode() const;
 867 };
 868 
 869 //------------------------------CompareAndSwapNNode---------------------------
 870 class CompareAndSwapNNode : public LoadStoreConditionalNode {
 871 public:
 872   CompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex) : LoadStoreConditionalNode(c, mem, adr, val, ex) { }
 873   virtual int Opcode() const;
 874 };
 875 
 876 //------------------------------GetAndAddINode---------------------------
 877 class GetAndAddINode : public LoadStoreNode {
 878 public:
 879   GetAndAddINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
 880   virtual int Opcode() const;
 881 };
 882 
 883 //------------------------------GetAndAddLNode---------------------------
 884 class GetAndAddLNode : public LoadStoreNode {
 885 public:
 886   GetAndAddLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
 887   virtual int Opcode() const;
 888 };
 889 
 890 
 891 //------------------------------GetAndSetINode---------------------------
 892 class GetAndSetINode : public LoadStoreNode {
 893 public:
 894   GetAndSetINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
 895   virtual int Opcode() const;
 896 };
 897 
 898 //------------------------------GetAndSetINode---------------------------
 899 class GetAndSetLNode : public LoadStoreNode {
 900 public:
 901   GetAndSetLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
 902   virtual int Opcode() const;
 903 };
 904 
 905 //------------------------------GetAndSetPNode---------------------------
 906 class GetAndSetPNode : public LoadStoreNode {
 907 public:
 908   GetAndSetPNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
 909   virtual int Opcode() const;
 910 };
 911 
 912 //------------------------------GetAndSetNNode---------------------------
 913 class GetAndSetNNode : public LoadStoreNode {
 914 public:
 915   GetAndSetNNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
 916   virtual int Opcode() const;
 917 };
 918 
 919 //------------------------------ClearArray-------------------------------------
 920 class ClearArrayNode: public Node {
 921 public:
 922   ClearArrayNode( Node *ctrl, Node *arymem, Node *word_cnt, Node *base )
 923     : Node(ctrl,arymem,word_cnt,base) {
 924     init_class_id(Class_ClearArray);
 925   }
 926   virtual int         Opcode() const;
 927   virtual const Type *bottom_type() const { return Type::MEMORY; }
 928   // ClearArray modifies array elements, and so affects only the
 929   // array memory addressed by the bottom_type of its base address.
 930   virtual const class TypePtr *adr_type() const;
 931   virtual Node *Identity( PhaseTransform *phase );
 932   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 933   virtual uint match_edge(uint idx) const;
 934 
 935   // Clear the given area of an object or array.
 936   // The start offset must always be aligned mod BytesPerInt.
 937   // The end offset must always be aligned mod BytesPerLong.
 938   // Return the new memory.
 939   static Node* clear_memory(Node* control, Node* mem, Node* dest,
 940                             intptr_t start_offset,
 941                             intptr_t end_offset,
 942                             PhaseGVN* phase);
 943   static Node* clear_memory(Node* control, Node* mem, Node* dest,
 944                             intptr_t start_offset,
 945                             Node* end_offset,
 946                             PhaseGVN* phase);
 947   static Node* clear_memory(Node* control, Node* mem, Node* dest,
 948                             Node* start_offset,
 949                             Node* end_offset,
 950                             PhaseGVN* phase);
 951   // Return allocation input memory edge if it is different instance
 952   // or itself if it is the one we are looking for.
 953   static bool step_through(Node** np, uint instance_id, PhaseTransform* phase);
 954 };
 955 
 956 //------------------------------MemBar-----------------------------------------
 957 // There are different flavors of Memory Barriers to match the Java Memory
 958 // Model.  Monitor-enter and volatile-load act as Aquires: no following ref
 959 // can be moved to before them.  We insert a MemBar-Acquire after a FastLock or
 960 // volatile-load.  Monitor-exit and volatile-store act as Release: no
 961 // preceding ref can be moved to after them.  We insert a MemBar-Release
 962 // before a FastUnlock or volatile-store.  All volatiles need to be
 963 // serialized, so we follow all volatile-stores with a MemBar-Volatile to
 964 // separate it from any following volatile-load.
 965 class MemBarNode: public MultiNode {
 966   virtual uint hash() const ;                  // { return NO_HASH; }
 967   virtual uint cmp( const Node &n ) const ;    // Always fail, except on self
 968 
 969   virtual uint size_of() const { return sizeof(*this); }
 970   // Memory type this node is serializing.  Usually either rawptr or bottom.
 971   const TypePtr* _adr_type;
 972 
 973 public:
 974   enum {
 975     Precedent = TypeFunc::Parms  // optional edge to force precedence
 976   };
 977   MemBarNode(Compile* C, int alias_idx, Node* precedent);
 978   virtual int Opcode() const = 0;
 979   virtual const class TypePtr *adr_type() const { return _adr_type; }
 980   virtual const Type *Value( PhaseTransform *phase ) const;
 981   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 982   virtual uint match_edge(uint idx) const { return 0; }
 983   virtual const Type *bottom_type() const { return TypeTuple::MEMBAR; }
 984   virtual Node *match( const ProjNode *proj, const Matcher *m );
 985   // Factory method.  Builds a wide or narrow membar.
 986   // Optional 'precedent' becomes an extra edge if not null.
 987   static MemBarNode* make(Compile* C, int opcode,
 988                           int alias_idx = Compile::AliasIdxBot,
 989                           Node* precedent = NULL);
 990 };
 991 
 992 // "Acquire" - no following ref can move before (but earlier refs can
 993 // follow, like an early Load stalled in cache).  Requires multi-cpu
 994 // visibility.  Inserted after a volatile load.
 995 class MemBarAcquireNode: public MemBarNode {
 996 public:
 997   MemBarAcquireNode(Compile* C, int alias_idx, Node* precedent)
 998     : MemBarNode(C, alias_idx, precedent) {}
 999   virtual int Opcode() const;
1000 };
1001 
1002 // "Acquire" - no following ref can move before (but earlier refs can
1003 // follow, like an early Load stalled in cache).  Requires multi-cpu
1004 // visibility.  Inserted independ of any load, as required
1005 // for intrinsic sun.misc.Unsafe.loadFence().
1006 class LoadFenceNode: public MemBarNode {
1007 public:
1008   LoadFenceNode(Compile* C, int alias_idx, Node* precedent)
1009     : MemBarNode(C, alias_idx, precedent) {}
1010   virtual int Opcode() const;
1011 };
1012 
1013 // "Release" - no earlier ref can move after (but later refs can move
1014 // up, like a speculative pipelined cache-hitting Load).  Requires
1015 // multi-cpu visibility.  Inserted before a volatile store.
1016 class MemBarReleaseNode: public MemBarNode {
1017 public:
1018   MemBarReleaseNode(Compile* C, int alias_idx, Node* precedent)
1019     : MemBarNode(C, alias_idx, precedent) {}
1020   virtual int Opcode() const;
1021 };
1022 
1023 // "Release" - no earlier ref can move after (but later refs can move
1024 // up, like a speculative pipelined cache-hitting Load).  Requires
1025 // multi-cpu visibility.  Inserted independent of any store, as required
1026 // for intrinsic sun.misc.Unsafe.storeFence().
1027 class StoreFenceNode: public MemBarNode {
1028 public:
1029   StoreFenceNode(Compile* C, int alias_idx, Node* precedent)
1030     : MemBarNode(C, alias_idx, precedent) {}
1031   virtual int Opcode() const;
1032 };
1033 
1034 // "Acquire" - no following ref can move before (but earlier refs can
1035 // follow, like an early Load stalled in cache).  Requires multi-cpu
1036 // visibility.  Inserted after a FastLock.
1037 class MemBarAcquireLockNode: public MemBarNode {
1038 public:
1039   MemBarAcquireLockNode(Compile* C, int alias_idx, Node* precedent)
1040     : MemBarNode(C, alias_idx, precedent) {}
1041   virtual int Opcode() const;
1042 };
1043 
1044 // "Release" - no earlier ref can move after (but later refs can move
1045 // up, like a speculative pipelined cache-hitting Load).  Requires
1046 // multi-cpu visibility.  Inserted before a FastUnLock.
1047 class MemBarReleaseLockNode: public MemBarNode {
1048 public:
1049   MemBarReleaseLockNode(Compile* C, int alias_idx, Node* precedent)
1050     : MemBarNode(C, alias_idx, precedent) {}
1051   virtual int Opcode() const;
1052 };
1053 
1054 class MemBarStoreStoreNode: public MemBarNode {
1055 public:
1056   MemBarStoreStoreNode(Compile* C, int alias_idx, Node* precedent)
1057     : MemBarNode(C, alias_idx, precedent) {
1058     init_class_id(Class_MemBarStoreStore);
1059   }
1060   virtual int Opcode() const;
1061 };
1062 
1063 // Ordering between a volatile store and a following volatile load.
1064 // Requires multi-CPU visibility?
1065 class MemBarVolatileNode: public MemBarNode {
1066 public:
1067   MemBarVolatileNode(Compile* C, int alias_idx, Node* precedent)
1068     : MemBarNode(C, alias_idx, precedent) {}
1069   virtual int Opcode() const;
1070 };
1071 
1072 // Ordering within the same CPU.  Used to order unsafe memory references
1073 // inside the compiler when we lack alias info.  Not needed "outside" the
1074 // compiler because the CPU does all the ordering for us.
1075 class MemBarCPUOrderNode: public MemBarNode {
1076 public:
1077   MemBarCPUOrderNode(Compile* C, int alias_idx, Node* precedent)
1078     : MemBarNode(C, alias_idx, precedent) {}
1079   virtual int Opcode() const;
1080   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1081 };
1082 
1083 // Isolation of object setup after an AllocateNode and before next safepoint.
1084 // (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
1085 class InitializeNode: public MemBarNode {
1086   friend class AllocateNode;
1087 
1088   enum {
1089     Incomplete    = 0,
1090     Complete      = 1,
1091     WithArraycopy = 2
1092   };
1093   int _is_complete;
1094 
1095   bool _does_not_escape;
1096 
1097 public:
1098   enum {
1099     Control    = TypeFunc::Control,
1100     Memory     = TypeFunc::Memory,     // MergeMem for states affected by this op
1101     RawAddress = TypeFunc::Parms+0,    // the newly-allocated raw address
1102     RawStores  = TypeFunc::Parms+1     // zero or more stores (or TOP)
1103   };
1104 
1105   InitializeNode(Compile* C, int adr_type, Node* rawoop);
1106   virtual int Opcode() const;
1107   virtual uint size_of() const { return sizeof(*this); }
1108   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1109   virtual const RegMask &in_RegMask(uint) const;  // mask for RawAddress
1110 
1111   // Manage incoming memory edges via a MergeMem on in(Memory):
1112   Node* memory(uint alias_idx);
1113 
1114   // The raw memory edge coming directly from the Allocation.
1115   // The contents of this memory are *always* all-zero-bits.
1116   Node* zero_memory() { return memory(Compile::AliasIdxRaw); }
1117 
1118   // Return the corresponding allocation for this initialization (or null if none).
1119   // (Note: Both InitializeNode::allocation and AllocateNode::initialization
1120   // are defined in graphKit.cpp, which sets up the bidirectional relation.)
1121   AllocateNode* allocation();
1122 
1123   // Anything other than zeroing in this init?
1124   bool is_non_zero();
1125 
1126   // An InitializeNode must completed before macro expansion is done.
1127   // Completion requires that the AllocateNode must be followed by
1128   // initialization of the new memory to zero, then to any initializers.
1129   bool is_complete() { return _is_complete != Incomplete; }
1130   bool is_complete_with_arraycopy() { return (_is_complete & WithArraycopy) != 0; }
1131 
1132   // Mark complete.  (Must not yet be complete.)
1133   void set_complete(PhaseGVN* phase);
1134   void set_complete_with_arraycopy() { _is_complete = Complete | WithArraycopy; }
1135 
1136   bool does_not_escape() { return _does_not_escape; }
1137   void set_does_not_escape() { _does_not_escape = true; }
1138 
1139 #ifdef ASSERT
1140   // ensure all non-degenerate stores are ordered and non-overlapping
1141   bool stores_are_sane(PhaseTransform* phase);
1142 #endif //ASSERT
1143 
1144   // See if this store can be captured; return offset where it initializes.
1145   // Return 0 if the store cannot be moved (any sort of problem).
1146   intptr_t can_capture_store(StoreNode* st, PhaseTransform* phase, bool can_reshape);
1147 
1148   // Capture another store; reformat it to write my internal raw memory.
1149   // Return the captured copy, else NULL if there is some sort of problem.
1150   Node* capture_store(StoreNode* st, intptr_t start, PhaseTransform* phase, bool can_reshape);
1151 
1152   // Find captured store which corresponds to the range [start..start+size).
1153   // Return my own memory projection (meaning the initial zero bits)
1154   // if there is no such store.  Return NULL if there is a problem.
1155   Node* find_captured_store(intptr_t start, int size_in_bytes, PhaseTransform* phase);
1156 
1157   // Called when the associated AllocateNode is expanded into CFG.
1158   Node* complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
1159                         intptr_t header_size, Node* size_in_bytes,
1160                         PhaseGVN* phase);
1161 
1162  private:
1163   void remove_extra_zeroes();
1164 
1165   // Find out where a captured store should be placed (or already is placed).
1166   int captured_store_insertion_point(intptr_t start, int size_in_bytes,
1167                                      PhaseTransform* phase);
1168 
1169   static intptr_t get_store_offset(Node* st, PhaseTransform* phase);
1170 
1171   Node* make_raw_address(intptr_t offset, PhaseTransform* phase);
1172 
1173   bool detect_init_independence(Node* n, int& count);
1174 
1175   void coalesce_subword_stores(intptr_t header_size, Node* size_in_bytes,
1176                                PhaseGVN* phase);
1177 
1178   intptr_t find_next_fullword_store(uint i, PhaseGVN* phase);
1179 };
1180 
1181 //------------------------------MergeMem---------------------------------------
1182 // (See comment in memnode.cpp near MergeMemNode::MergeMemNode for semantics.)
1183 class MergeMemNode: public Node {
1184   virtual uint hash() const ;                  // { return NO_HASH; }
1185   virtual uint cmp( const Node &n ) const ;    // Always fail, except on self
1186   friend class MergeMemStream;
1187   MergeMemNode(Node* def);  // clients use MergeMemNode::make
1188 
1189 public:
1190   // If the input is a whole memory state, clone it with all its slices intact.
1191   // Otherwise, make a new memory state with just that base memory input.
1192   // In either case, the result is a newly created MergeMem.
1193   static MergeMemNode* make(Node* base_memory);
1194 
1195   virtual int Opcode() const;
1196   virtual Node *Identity( PhaseTransform *phase );
1197   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1198   virtual uint ideal_reg() const { return NotAMachineReg; }
1199   virtual uint match_edge(uint idx) const { return 0; }
1200   virtual const RegMask &out_RegMask() const;
1201   virtual const Type *bottom_type() const { return Type::MEMORY; }
1202   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1203   // sparse accessors
1204   // Fetch the previously stored "set_memory_at", or else the base memory.
1205   // (Caller should clone it if it is a phi-nest.)
1206   Node* memory_at(uint alias_idx) const;
1207   // set the memory, regardless of its previous value
1208   void set_memory_at(uint alias_idx, Node* n);
1209   // the "base" is the memory that provides the non-finite support
1210   Node* base_memory() const       { return in(Compile::AliasIdxBot); }
1211   // warning: setting the base can implicitly set any of the other slices too
1212   void set_base_memory(Node* def);
1213   // sentinel value which denotes a copy of the base memory:
1214   Node*   empty_memory() const    { return in(Compile::AliasIdxTop); }
1215   static Node* make_empty_memory(); // where the sentinel comes from
1216   bool is_empty_memory(Node* n) const { assert((n == empty_memory()) == n->is_top(), "sanity"); return n->is_top(); }
1217   // hook for the iterator, to perform any necessary setup
1218   void iteration_setup(const MergeMemNode* other = NULL);
1219   // push sentinels until I am at least as long as the other (semantic no-op)
1220   void grow_to_match(const MergeMemNode* other);
1221   bool verify_sparse() const PRODUCT_RETURN0;
1222 #ifndef PRODUCT
1223   virtual void dump_spec(outputStream *st) const;
1224 #endif
1225 };
1226 
1227 class MergeMemStream : public StackObj {
1228  private:
1229   MergeMemNode*       _mm;
1230   const MergeMemNode* _mm2;  // optional second guy, contributes non-empty iterations
1231   Node*               _mm_base;  // loop-invariant base memory of _mm
1232   int                 _idx;
1233   int                 _cnt;
1234   Node*               _mem;
1235   Node*               _mem2;
1236   int                 _cnt2;
1237 
1238   void init(MergeMemNode* mm, const MergeMemNode* mm2 = NULL) {
1239     // subsume_node will break sparseness at times, whenever a memory slice
1240     // folds down to a copy of the base ("fat") memory.  In such a case,
1241     // the raw edge will update to base, although it should be top.
1242     // This iterator will recognize either top or base_memory as an
1243     // "empty" slice.  See is_empty, is_empty2, and next below.
1244     //
1245     // The sparseness property is repaired in MergeMemNode::Ideal.
1246     // As long as access to a MergeMem goes through this iterator
1247     // or the memory_at accessor, flaws in the sparseness will
1248     // never be observed.
1249     //
1250     // Also, iteration_setup repairs sparseness.
1251     assert(mm->verify_sparse(), "please, no dups of base");
1252     assert(mm2==NULL || mm2->verify_sparse(), "please, no dups of base");
1253 
1254     _mm  = mm;
1255     _mm_base = mm->base_memory();
1256     _mm2 = mm2;
1257     _cnt = mm->req();
1258     _idx = Compile::AliasIdxBot-1; // start at the base memory
1259     _mem = NULL;
1260     _mem2 = NULL;
1261   }
1262 
1263 #ifdef ASSERT
1264   Node* check_memory() const {
1265     if (at_base_memory())
1266       return _mm->base_memory();
1267     else if ((uint)_idx < _mm->req() && !_mm->in(_idx)->is_top())
1268       return _mm->memory_at(_idx);
1269     else
1270       return _mm_base;
1271   }
1272   Node* check_memory2() const {
1273     return at_base_memory()? _mm2->base_memory(): _mm2->memory_at(_idx);
1274   }
1275 #endif
1276 
1277   static bool match_memory(Node* mem, const MergeMemNode* mm, int idx) PRODUCT_RETURN0;
1278   void assert_synch() const {
1279     assert(!_mem || _idx >= _cnt || match_memory(_mem, _mm, _idx),
1280            "no side-effects except through the stream");
1281   }
1282 
1283  public:
1284 
1285   // expected usages:
1286   // for (MergeMemStream mms(mem->is_MergeMem()); next_non_empty(); ) { ... }
1287   // for (MergeMemStream mms(mem1, mem2); next_non_empty2(); ) { ... }
1288 
1289   // iterate over one merge
1290   MergeMemStream(MergeMemNode* mm) {
1291     mm->iteration_setup();
1292     init(mm);
1293     debug_only(_cnt2 = 999);
1294   }
1295   // iterate in parallel over two merges
1296   // only iterates through non-empty elements of mm2
1297   MergeMemStream(MergeMemNode* mm, const MergeMemNode* mm2) {
1298     assert(mm2, "second argument must be a MergeMem also");
1299     ((MergeMemNode*)mm2)->iteration_setup();  // update hidden state
1300     mm->iteration_setup(mm2);
1301     init(mm, mm2);
1302     _cnt2 = mm2->req();
1303   }
1304 #ifdef ASSERT
1305   ~MergeMemStream() {
1306     assert_synch();
1307   }
1308 #endif
1309 
1310   MergeMemNode* all_memory() const {
1311     return _mm;
1312   }
1313   Node* base_memory() const {
1314     assert(_mm_base == _mm->base_memory(), "no update to base memory, please");
1315     return _mm_base;
1316   }
1317   const MergeMemNode* all_memory2() const {
1318     assert(_mm2 != NULL, "");
1319     return _mm2;
1320   }
1321   bool at_base_memory() const {
1322     return _idx == Compile::AliasIdxBot;
1323   }
1324   int alias_idx() const {
1325     assert(_mem, "must call next 1st");
1326     return _idx;
1327   }
1328 
1329   const TypePtr* adr_type() const {
1330     return Compile::current()->get_adr_type(alias_idx());
1331   }
1332 
1333   const TypePtr* adr_type(Compile* C) const {
1334     return C->get_adr_type(alias_idx());
1335   }
1336   bool is_empty() const {
1337     assert(_mem, "must call next 1st");
1338     assert(_mem->is_top() == (_mem==_mm->empty_memory()), "correct sentinel");
1339     return _mem->is_top();
1340   }
1341   bool is_empty2() const {
1342     assert(_mem2, "must call next 1st");
1343     assert(_mem2->is_top() == (_mem2==_mm2->empty_memory()), "correct sentinel");
1344     return _mem2->is_top();
1345   }
1346   Node* memory() const {
1347     assert(!is_empty(), "must not be empty");
1348     assert_synch();
1349     return _mem;
1350   }
1351   // get the current memory, regardless of empty or non-empty status
1352   Node* force_memory() const {
1353     assert(!is_empty() || !at_base_memory(), "");
1354     // Use _mm_base to defend against updates to _mem->base_memory().
1355     Node *mem = _mem->is_top() ? _mm_base : _mem;
1356     assert(mem == check_memory(), "");
1357     return mem;
1358   }
1359   Node* memory2() const {
1360     assert(_mem2 == check_memory2(), "");
1361     return _mem2;
1362   }
1363   void set_memory(Node* mem) {
1364     if (at_base_memory()) {
1365       // Note that this does not change the invariant _mm_base.
1366       _mm->set_base_memory(mem);
1367     } else {
1368       _mm->set_memory_at(_idx, mem);
1369     }
1370     _mem = mem;
1371     assert_synch();
1372   }
1373 
1374   // Recover from a side effect to the MergeMemNode.
1375   void set_memory() {
1376     _mem = _mm->in(_idx);
1377   }
1378 
1379   bool next()  { return next(false); }
1380   bool next2() { return next(true); }
1381 
1382   bool next_non_empty()  { return next_non_empty(false); }
1383   bool next_non_empty2() { return next_non_empty(true); }
1384   // next_non_empty2 can yield states where is_empty() is true
1385 
1386  private:
1387   // find the next item, which might be empty
1388   bool next(bool have_mm2) {
1389     assert((_mm2 != NULL) == have_mm2, "use other next");
1390     assert_synch();
1391     if (++_idx < _cnt) {
1392       // Note:  This iterator allows _mm to be non-sparse.
1393       // It behaves the same whether _mem is top or base_memory.
1394       _mem = _mm->in(_idx);
1395       if (have_mm2)
1396         _mem2 = _mm2->in((_idx < _cnt2) ? _idx : Compile::AliasIdxTop);
1397       return true;
1398     }
1399     return false;
1400   }
1401 
1402   // find the next non-empty item
1403   bool next_non_empty(bool have_mm2) {
1404     while (next(have_mm2)) {
1405       if (!is_empty()) {
1406         // make sure _mem2 is filled in sensibly
1407         if (have_mm2 && _mem2->is_top())  _mem2 = _mm2->base_memory();
1408         return true;
1409       } else if (have_mm2 && !is_empty2()) {
1410         return true;   // is_empty() == true
1411       }
1412     }
1413     return false;
1414   }
1415 };
1416 
1417 //------------------------------Prefetch---------------------------------------
1418 
1419 // Allocation prefetch which may fault, TLAB size have to be adjusted.
1420 class PrefetchAllocationNode : public Node {
1421 public:
1422   PrefetchAllocationNode(Node *mem, Node *adr) : Node(0,mem,adr) {}
1423   virtual int Opcode() const;
1424   virtual uint ideal_reg() const { return NotAMachineReg; }
1425   virtual uint match_edge(uint idx) const { return idx==2; }
1426   virtual const Type *bottom_type() const { return ( AllocatePrefetchStyle == 3 ) ? Type::MEMORY : Type::ABIO; }
1427 };
1428 
1429 #endif // SHARE_VM_OPTO_MEMNODE_HPP