1 /*
   2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2024, Alibaba Group Holding Limited. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_OPTO_MEMNODE_HPP
  27 #define SHARE_OPTO_MEMNODE_HPP
  28 
  29 #include "opto/multnode.hpp"
  30 #include "opto/node.hpp"
  31 #include "opto/opcodes.hpp"
  32 #include "opto/type.hpp"
  33 
  34 // Portions of code courtesy of Clifford Click
  35 
  36 class MultiNode;
  37 class PhaseCCP;
  38 class PhaseTransform;
  39 
  40 //------------------------------MemNode----------------------------------------
  41 // Load or Store, possibly throwing a null pointer exception
  42 class MemNode : public Node {
  43 private:
  44   bool _unaligned_access; // Unaligned access from unsafe
  45   bool _mismatched_access; // Mismatched access from unsafe: byte read in integer array for instance
  46   bool _unsafe_access;     // Access of unsafe origin.
  47   uint8_t _barrier_data;   // Bit field with barrier information
  48 
  49 protected:
  50 #ifdef ASSERT
  51   const TypePtr* _adr_type;     // What kind of memory is being addressed?
  52 #endif
  53   virtual uint size_of() const;
  54 public:
  55   enum { Control,               // When is it safe to do this load?
  56          Memory,                // Chunk of memory is being loaded from
  57          Address,               // Actually address, derived from base
  58          ValueIn                // Value to store
  59   };
  60   typedef enum { unordered = 0,
  61                  acquire,       // Load has to acquire or be succeeded by MemBarAcquire.
  62                  release,       // Store has to release or be preceded by MemBarRelease.
  63                  seqcst,        // LoadStore has to have both acquire and release semantics.
  64                  unset          // The memory ordering is not set (used for testing)
  65   } MemOrd;
  66 protected:
  67   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at ) :
  68       Node(c0,c1,c2),
  69       _unaligned_access(false),
  70       _mismatched_access(false),
  71       _unsafe_access(false),
  72       _barrier_data(0) {
  73     init_class_id(Class_Mem);
  74     DEBUG_ONLY(_adr_type=at; adr_type();)
  75   }
  76   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3 ) :
  77       Node(c0,c1,c2,c3),
  78       _unaligned_access(false),
  79       _mismatched_access(false),
  80       _unsafe_access(false),
  81       _barrier_data(0) {
  82     init_class_id(Class_Mem);
  83     DEBUG_ONLY(_adr_type=at; adr_type();)
  84   }
  85   MemNode( Node *c0, Node *c1, Node *c2, const TypePtr* at, Node *c3, Node *c4) :
  86       Node(c0,c1,c2,c3,c4),
  87       _unaligned_access(false),
  88       _mismatched_access(false),
  89       _unsafe_access(false),
  90       _barrier_data(0) {
  91     init_class_id(Class_Mem);
  92     DEBUG_ONLY(_adr_type=at; adr_type();)
  93   }
  94 
  95   virtual Node* find_previous_arraycopy(PhaseValues* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const { return nullptr; }
  96   ArrayCopyNode* find_array_copy_clone(Node* ld_alloc, Node* mem) const;
  97   static bool check_if_adr_maybe_raw(Node* adr);
  98 
  99 public:
 100   // Helpers for the optimizer.  Documented in memnode.cpp.
 101   static bool detect_ptr_independence(Node* p1, AllocateNode* a1,
 102                                       Node* p2, AllocateNode* a2,
 103                                       PhaseTransform* phase);
 104   static bool adr_phi_is_loop_invariant(Node* adr_phi, Node* cast);
 105 
 106   static Node *optimize_simple_memory_chain(Node *mchain, const TypeOopPtr *t_oop, Node *load, PhaseGVN *phase);
 107   static Node *optimize_memory_chain(Node *mchain, const TypePtr *t_adr, Node *load, PhaseGVN *phase);
 108   // The following two should probably be phase-specific functions:
 109   static DomResult maybe_all_controls_dominate(Node* dom, Node* sub);
 110   static bool all_controls_dominate(Node* dom, Node* sub) {
 111     DomResult dom_result = maybe_all_controls_dominate(dom, sub);
 112     return dom_result == DomResult::Dominate;
 113   }
 114 
 115   virtual const class TypePtr *adr_type() const;  // returns bottom_type of address
 116 
 117   // Shared code for Ideal methods:
 118   Node *Ideal_common(PhaseGVN *phase, bool can_reshape);  // Return -1 for short-circuit null.
 119 
 120   // Helper function for adr_type() implementations.
 121   static const TypePtr* calculate_adr_type(const Type* t, const TypePtr* cross_check = nullptr);
 122 
 123   // Raw access function, to allow copying of adr_type efficiently in
 124   // product builds and retain the debug info for debug builds.
 125   const TypePtr *raw_adr_type() const {
 126     return DEBUG_ONLY(_adr_type) NOT_DEBUG(nullptr);
 127   }
 128 
 129   // Return the barrier data of n, if available, or 0 otherwise.
 130   static uint8_t barrier_data(const Node* n);
 131 
 132   // Map a load or store opcode to its corresponding store opcode.
 133   // (Return -1 if unknown.)
 134   virtual int store_Opcode() const { return -1; }
 135 
 136   // What is the type of the value in memory?  (T_VOID mean "unspecified".)
 137   // The returned type is a property of the value that is loaded/stored and
 138   // not the memory that is accessed. For mismatched memory accesses
 139   // they might differ. For instance, a value of type 'short' may be stored
 140   // into an array of elements of type 'long'.
 141   virtual BasicType value_basic_type() const = 0;
 142   virtual int memory_size() const {
 143 #ifdef ASSERT
 144     return type2aelembytes(value_basic_type(), true);
 145 #else
 146     return type2aelembytes(value_basic_type());
 147 #endif
 148   }
 149 
 150   uint8_t barrier_data() { return _barrier_data; }
 151   void set_barrier_data(uint8_t barrier_data) { _barrier_data = barrier_data; }
 152 
 153   // Search through memory states which precede this node (load or store).
 154   // Look for an exact match for the address, with no intervening
 155   // aliased stores.
 156   Node* find_previous_store(PhaseValues* phase);
 157 
 158   // Can this node (load or store) accurately see a stored value in
 159   // the given memory state?  (The state may or may not be in(Memory).)
 160   Node* can_see_stored_value(Node* st, PhaseValues* phase) const;
 161 
 162   void set_unaligned_access() { _unaligned_access = true; }
 163   bool is_unaligned_access() const { return _unaligned_access; }
 164   void set_mismatched_access() { _mismatched_access = true; }
 165   bool is_mismatched_access() const { return _mismatched_access; }
 166   void set_unsafe_access() { _unsafe_access = true; }
 167   bool is_unsafe_access() const { return _unsafe_access; }
 168 
 169 #ifndef PRODUCT
 170   static void dump_adr_type(const TypePtr* adr_type, outputStream* st);
 171   virtual void dump_spec(outputStream *st) const;
 172 #endif
 173 };
 174 
 175 //------------------------------LoadNode---------------------------------------
 176 // Load value; requires Memory and Address
 177 class LoadNode : public MemNode {
 178 public:
 179   // Some loads (from unsafe) should be pinned: they don't depend only
 180   // on the dominating test.  The field _control_dependency below records
 181   // whether that node depends only on the dominating test.
 182   // Pinned and UnknownControl are similar, but differ in that Pinned
 183   // loads are not allowed to float across safepoints, whereas UnknownControl
 184   // loads are allowed to do that. Therefore, Pinned is stricter.
 185   enum ControlDependency {
 186     Pinned,
 187     UnknownControl,
 188     DependsOnlyOnTest
 189   };
 190 
 191 private:
 192   // LoadNode::hash() doesn't take the _control_dependency field
 193   // into account: If the graph already has a non-pinned LoadNode and
 194   // we add a pinned LoadNode with the same inputs, it's safe for GVN
 195   // to replace the pinned LoadNode with the non-pinned LoadNode,
 196   // otherwise it wouldn't be safe to have a non pinned LoadNode with
 197   // those inputs in the first place. If the graph already has a
 198   // pinned LoadNode and we add a non pinned LoadNode with the same
 199   // inputs, it's safe (but suboptimal) for GVN to replace the
 200   // non-pinned LoadNode by the pinned LoadNode.
 201   ControlDependency _control_dependency;
 202 
 203   // On platforms with weak memory ordering (e.g., PPC) we distinguish
 204   // loads that can be reordered, and such requiring acquire semantics to
 205   // adhere to the Java specification.  The required behaviour is stored in
 206   // this field.
 207   const MemOrd _mo;
 208 
 209   AllocateNode* is_new_object_mark_load() const;
 210 
 211 protected:
 212   virtual bool cmp(const Node &n) const;
 213   virtual uint size_of() const; // Size is bigger
 214   // Should LoadNode::Ideal() attempt to remove control edges?
 215   virtual bool can_remove_control() const;
 216   const Type* const _type;      // What kind of value is loaded?
 217 
 218   virtual Node* find_previous_arraycopy(PhaseValues* phase, Node* ld_alloc, Node*& mem, bool can_see_stored_value) const;
 219 public:
 220 
 221   LoadNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *rt, MemOrd mo, ControlDependency control_dependency)
 222     : MemNode(c,mem,adr,at), _control_dependency(control_dependency), _mo(mo), _type(rt) {
 223     init_class_id(Class_Load);
 224   }
 225   inline bool is_unordered() const { return !is_acquire(); }
 226   inline bool is_acquire() const {
 227     assert(_mo == unordered || _mo == acquire, "unexpected");
 228     return _mo == acquire;
 229   }
 230   inline bool is_unsigned() const {
 231     int lop = Opcode();
 232     return (lop == Op_LoadUB) || (lop == Op_LoadUS);
 233   }
 234 
 235   // Polymorphic factory method:
 236   static Node* make(PhaseGVN& gvn, Node* c, Node* mem, Node* adr,
 237                     const TypePtr* at, const Type* rt, BasicType bt,
 238                     MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest,
 239                     bool require_atomic_access = false, bool unaligned = false, bool mismatched = false, bool unsafe = false,
 240                     uint8_t barrier_data = 0);
 241 
 242   virtual uint hash()   const;  // Check the type
 243 
 244   // Handle algebraic identities here.  If we have an identity, return the Node
 245   // we are equivalent to.  We look for Load of a Store.
 246   virtual Node* Identity(PhaseGVN* phase);
 247 
 248   // If the load is from Field memory and the pointer is non-null, it might be possible to
 249   // zero out the control input.
 250   // If the offset is constant and the base is an object allocation,
 251   // try to hook me up to the exact initializing store.
 252   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 253 
 254   // Return true if it's possible to split the Load through a Phi merging the bases
 255   bool can_split_through_phi_base(PhaseGVN *phase);
 256 
 257   // Split instance field load through Phi.
 258   Node* split_through_phi(PhaseGVN *phase, bool ignore_missing_instance_id = false);
 259 
 260   // Recover original value from boxed values
 261   Node *eliminate_autobox(PhaseIterGVN *igvn);
 262 
 263   // Compute a new Type for this node.  Basically we just do the pre-check,
 264   // then call the virtual add() to set the type.
 265   virtual const Type* Value(PhaseGVN* phase) const;
 266 
 267   // Common methods for LoadKlass and LoadNKlass nodes.
 268   const Type* klass_value_common(PhaseGVN* phase) const;
 269   Node* klass_identity_common(PhaseGVN* phase);
 270 
 271   virtual uint ideal_reg() const;
 272   virtual const Type *bottom_type() const;
 273   // Following method is copied from TypeNode:
 274   void set_type(const Type* t) {
 275     assert(t != nullptr, "sanity");
 276     DEBUG_ONLY(uint check_hash = (VerifyHashTableKeys && _hash_lock) ? hash() : NO_HASH);
 277     *(const Type**)&_type = t;   // cast away const-ness
 278     // If this node is in the hash table, make sure it doesn't need a rehash.
 279     assert(check_hash == NO_HASH || check_hash == hash(), "type change must preserve hash code");
 280   }
 281   const Type* type() const { assert(_type != nullptr, "sanity"); return _type; };
 282 
 283   // Do not match memory edge
 284   virtual uint match_edge(uint idx) const;
 285 
 286   // Map a load opcode to its corresponding store opcode.
 287   virtual int store_Opcode() const = 0;
 288 
 289   // Check if the load's memory input is a Phi node with the same control.
 290   bool is_instance_field_load_with_local_phi(Node* ctrl);
 291 
 292   Node* convert_to_unsigned_load(PhaseGVN& gvn);
 293   Node* convert_to_signed_load(PhaseGVN& gvn);
 294 
 295   bool  has_reinterpret_variant(const Type* rt);
 296   Node* convert_to_reinterpret_load(PhaseGVN& gvn, const Type* rt);
 297 
 298   ControlDependency control_dependency() const { return _control_dependency; }
 299   bool has_unknown_control_dependency() const  { return _control_dependency == UnknownControl; }
 300   bool has_pinned_control_dependency() const   { return _control_dependency == Pinned; }
 301 
 302 #ifndef PRODUCT
 303   virtual void dump_spec(outputStream *st) const;
 304 #endif
 305 #ifdef ASSERT
 306   // Helper function to allow a raw load without control edge for some cases
 307   static bool is_immutable_value(Node* adr);
 308 #endif
 309 protected:
 310   const Type* load_array_final_field(const TypeKlassPtr *tkls,
 311                                      ciKlass* klass) const;
 312 
 313   Node* can_see_arraycopy_value(Node* st, PhaseGVN* phase) const;
 314 
 315 private:
 316   // depends_only_on_test is almost always true, and needs to be almost always
 317   // true to enable key hoisting & commoning optimizations.  However, for the
 318   // special case of RawPtr loads from TLS top & end, and other loads performed by
 319   // GC barriers, the control edge carries the dependence preventing hoisting past
 320   // a Safepoint instead of the memory edge.  (An unfortunate consequence of having
 321   // Safepoints not set Raw Memory; itself an unfortunate consequence of having Nodes
 322   // which produce results (new raw memory state) inside of loops preventing all
 323   // manner of other optimizations).  Basically, it's ugly but so is the alternative.
 324   // See comment in macro.cpp, around line 125 expand_allocate_common().
 325   virtual bool depends_only_on_test_impl() const {
 326     return adr_type() != TypeRawPtr::BOTTOM && _control_dependency == DependsOnlyOnTest;
 327   }
 328 
 329   LoadNode* clone_pinned() const;
 330   virtual LoadNode* pin_node_under_control_impl() const;
 331 };
 332 
 333 //------------------------------LoadBNode--------------------------------------
 334 // Load a byte (8bits signed) from memory
 335 class LoadBNode : public LoadNode {
 336 public:
 337   LoadBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 338     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 339   virtual int Opcode() const;
 340   virtual uint ideal_reg() const { return Op_RegI; }
 341   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 342   virtual const Type* Value(PhaseGVN* phase) const;
 343   virtual int store_Opcode() const { return Op_StoreB; }
 344   virtual BasicType value_basic_type() const { return T_BYTE; }
 345 };
 346 
 347 //------------------------------LoadUBNode-------------------------------------
 348 // Load a unsigned byte (8bits unsigned) from memory
 349 class LoadUBNode : public LoadNode {
 350 public:
 351   LoadUBNode(Node* c, Node* mem, Node* adr, const TypePtr* at, const TypeInt* ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 352     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 353   virtual int Opcode() const;
 354   virtual uint ideal_reg() const { return Op_RegI; }
 355   virtual Node* Ideal(PhaseGVN *phase, bool can_reshape);
 356   virtual const Type* Value(PhaseGVN* phase) const;
 357   virtual int store_Opcode() const { return Op_StoreB; }
 358   virtual BasicType value_basic_type() const { return T_BYTE; }
 359 };
 360 
 361 //------------------------------LoadUSNode-------------------------------------
 362 // Load an unsigned short/char (16bits unsigned) from memory
 363 class LoadUSNode : public LoadNode {
 364 public:
 365   LoadUSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 366     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 367   virtual int Opcode() const;
 368   virtual uint ideal_reg() const { return Op_RegI; }
 369   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 370   virtual const Type* Value(PhaseGVN* phase) const;
 371   virtual int store_Opcode() const { return Op_StoreC; }
 372   virtual BasicType value_basic_type() const { return T_CHAR; }
 373 };
 374 
 375 //------------------------------LoadSNode--------------------------------------
 376 // Load a short (16bits signed) from memory
 377 class LoadSNode : public LoadNode {
 378 public:
 379   LoadSNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 380     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 381   virtual int Opcode() const;
 382   virtual uint ideal_reg() const { return Op_RegI; }
 383   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 384   virtual const Type* Value(PhaseGVN* phase) const;
 385   virtual int store_Opcode() const { return Op_StoreC; }
 386   virtual BasicType value_basic_type() const { return T_SHORT; }
 387 };
 388 
 389 //------------------------------LoadINode--------------------------------------
 390 // Load an integer from memory
 391 class LoadINode : public LoadNode {
 392 public:
 393   LoadINode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeInt *ti, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 394     : LoadNode(c, mem, adr, at, ti, mo, control_dependency) {}
 395   virtual int Opcode() const;
 396   virtual uint ideal_reg() const { return Op_RegI; }
 397   virtual int store_Opcode() const { return Op_StoreI; }
 398   virtual BasicType value_basic_type() const { return T_INT; }
 399 };
 400 
 401 //------------------------------LoadRangeNode----------------------------------
 402 // Load an array length from the array
 403 class LoadRangeNode : public LoadINode {
 404 public:
 405   LoadRangeNode(Node *c, Node *mem, Node *adr, const TypeInt *ti = TypeInt::POS)
 406     : LoadINode(c, mem, adr, TypeAryPtr::RANGE, ti, MemNode::unordered) {}
 407   virtual int Opcode() const;
 408   virtual const Type* Value(PhaseGVN* phase) const;
 409   virtual Node* Identity(PhaseGVN* phase);
 410   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 411 };
 412 
 413 //------------------------------LoadLNode--------------------------------------
 414 // Load a long from memory
 415 class LoadLNode : public LoadNode {
 416   virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
 417   virtual bool cmp( const Node &n ) const {
 418     return _require_atomic_access == ((LoadLNode&)n)._require_atomic_access
 419       && LoadNode::cmp(n);
 420   }
 421   virtual uint size_of() const { return sizeof(*this); }
 422   const bool _require_atomic_access;  // is piecewise load forbidden?
 423 
 424 public:
 425   LoadLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const TypeLong *tl,
 426             MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
 427     : LoadNode(c, mem, adr, at, tl, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
 428   virtual int Opcode() const;
 429   virtual uint ideal_reg() const { return Op_RegL; }
 430   virtual int store_Opcode() const { return Op_StoreL; }
 431   virtual BasicType value_basic_type() const { return T_LONG; }
 432   bool require_atomic_access() const { return _require_atomic_access; }
 433 
 434 #ifndef PRODUCT
 435   virtual void dump_spec(outputStream *st) const {
 436     LoadNode::dump_spec(st);
 437     if (_require_atomic_access)  st->print(" Atomic!");
 438   }
 439 #endif
 440 };
 441 
 442 //------------------------------LoadL_unalignedNode----------------------------
 443 // Load a long from unaligned memory
 444 class LoadL_unalignedNode : public LoadLNode {
 445 public:
 446   LoadL_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 447     : LoadLNode(c, mem, adr, at, TypeLong::LONG, mo, control_dependency) {}
 448   virtual int Opcode() const;
 449 };
 450 
 451 //------------------------------LoadFNode--------------------------------------
 452 // Load a float (64 bits) from memory
 453 class LoadFNode : public LoadNode {
 454 public:
 455   LoadFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 456     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 457   virtual int Opcode() const;
 458   virtual uint ideal_reg() const { return Op_RegF; }
 459   virtual int store_Opcode() const { return Op_StoreF; }
 460   virtual BasicType value_basic_type() const { return T_FLOAT; }
 461 };
 462 
 463 //------------------------------LoadDNode--------------------------------------
 464 // Load a double (64 bits) from memory
 465 class LoadDNode : public LoadNode {
 466   virtual uint hash() const { return LoadNode::hash() + _require_atomic_access; }
 467   virtual bool cmp( const Node &n ) const {
 468     return _require_atomic_access == ((LoadDNode&)n)._require_atomic_access
 469       && LoadNode::cmp(n);
 470   }
 471   virtual uint size_of() const { return sizeof(*this); }
 472   const bool _require_atomic_access;  // is piecewise load forbidden?
 473 
 474 public:
 475   LoadDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, const Type *t,
 476             MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest, bool require_atomic_access = false)
 477     : LoadNode(c, mem, adr, at, t, mo, control_dependency), _require_atomic_access(require_atomic_access) {}
 478   virtual int Opcode() const;
 479   virtual uint ideal_reg() const { return Op_RegD; }
 480   virtual int store_Opcode() const { return Op_StoreD; }
 481   virtual BasicType value_basic_type() const { return T_DOUBLE; }
 482   bool require_atomic_access() const { return _require_atomic_access; }
 483 
 484 #ifndef PRODUCT
 485   virtual void dump_spec(outputStream *st) const {
 486     LoadNode::dump_spec(st);
 487     if (_require_atomic_access)  st->print(" Atomic!");
 488   }
 489 #endif
 490 };
 491 
 492 //------------------------------LoadD_unalignedNode----------------------------
 493 // Load a double from unaligned memory
 494 class LoadD_unalignedNode : public LoadDNode {
 495 public:
 496   LoadD_unalignedNode(Node *c, Node *mem, Node *adr, const TypePtr* at, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 497     : LoadDNode(c, mem, adr, at, Type::DOUBLE, mo, control_dependency) {}
 498   virtual int Opcode() const;
 499 };
 500 
 501 //------------------------------LoadPNode--------------------------------------
 502 // Load a pointer from memory (either object or array)
 503 class LoadPNode : public LoadNode {
 504 public:
 505   LoadPNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const TypePtr* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 506     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 507   virtual int Opcode() const;
 508   virtual uint ideal_reg() const { return Op_RegP; }
 509   virtual int store_Opcode() const { return Op_StoreP; }
 510   virtual BasicType value_basic_type() const { return T_ADDRESS; }
 511 };
 512 
 513 
 514 //------------------------------LoadNNode--------------------------------------
 515 // Load a narrow oop from memory (either object or array)
 516 class LoadNNode : public LoadNode {
 517 public:
 518   LoadNNode(Node *c, Node *mem, Node *adr, const TypePtr *at, const Type* t, MemOrd mo, ControlDependency control_dependency = DependsOnlyOnTest)
 519     : LoadNode(c, mem, adr, at, t, mo, control_dependency) {}
 520   virtual int Opcode() const;
 521   virtual uint ideal_reg() const { return Op_RegN; }
 522   virtual int store_Opcode() const { return Op_StoreN; }
 523   virtual BasicType value_basic_type() const { return T_NARROWOOP; }
 524 };
 525 
 526 //------------------------------LoadKlassNode----------------------------------
 527 // Load a Klass from an object
 528 class LoadKlassNode : public LoadPNode {
 529 private:
 530   LoadKlassNode(Node* mem, Node* adr, const TypePtr* at, const TypeKlassPtr* tk, MemOrd mo)
 531     : LoadPNode(nullptr, mem, adr, at, tk, mo) {}
 532 
 533 public:
 534   virtual int Opcode() const;
 535   virtual const Type* Value(PhaseGVN* phase) const;
 536   virtual Node* Identity(PhaseGVN* phase);
 537 
 538   // Polymorphic factory method:
 539   static Node* make(PhaseGVN& gvn, Node* mem, Node* adr, const TypePtr* at,
 540                     const TypeKlassPtr* tk = TypeInstKlassPtr::OBJECT);
 541 };
 542 
 543 //------------------------------LoadNKlassNode---------------------------------
 544 // Load a narrow Klass from an object.
 545 // With compact headers, the input address (adr) does not point at the exact
 546 // header position where the (narrow) class pointer is located, but into the
 547 // middle of the mark word (see oopDesc::klass_offset_in_bytes()). This node
 548 // implicitly shifts the loaded value (markWord::klass_shift_at_offset bits) to
 549 // extract the actual class pointer. C2's type system is agnostic on whether the
 550 // input address directly points into the class pointer.
 551 class LoadNKlassNode : public LoadNNode {
 552 private:
 553   friend Node* LoadKlassNode::make(PhaseGVN&, Node*, Node*, const TypePtr*, const TypeKlassPtr*);
 554   LoadNKlassNode(Node* mem, Node* adr, const TypePtr* at, const TypeNarrowKlass* tk, MemOrd mo)
 555     : LoadNNode(nullptr, mem, adr, at, tk, mo) {}
 556 
 557 public:
 558   virtual int Opcode() const;
 559   virtual uint ideal_reg() const { return Op_RegN; }
 560   virtual int store_Opcode() const { return Op_StoreNKlass; }
 561   virtual BasicType value_basic_type() const { return T_NARROWKLASS; }
 562 
 563   virtual const Type* Value(PhaseGVN* phase) const;
 564   virtual Node* Identity(PhaseGVN* phase);
 565 };
 566 
 567 
 568 //------------------------------StoreNode--------------------------------------
 569 // Store value; requires Store, Address and Value
 570 class StoreNode : public MemNode {
 571 private:
 572   // On platforms with weak memory ordering (e.g., PPC) we distinguish
 573   // stores that can be reordered, and such requiring release semantics to
 574   // adhere to the Java specification.  The required behaviour is stored in
 575   // this field.
 576   const MemOrd _mo;
 577   // Needed for proper cloning.
 578   virtual uint size_of() const { return sizeof(*this); }
 579 protected:
 580   virtual bool cmp( const Node &n ) const;
 581 
 582   Node *Ideal_masked_input       (PhaseGVN *phase, uint mask);
 583   Node* Ideal_sign_extended_input(PhaseGVN* phase, int num_rejected_bits);
 584 
 585 public:
 586   // We must ensure that stores of object references will be visible
 587   // only after the object's initialization. So the callers of this
 588   // procedure must indicate that the store requires `release'
 589   // semantics, if the stored value is an object reference that might
 590   // point to a new object and may become externally visible.
 591   StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 592     : MemNode(c, mem, adr, at, val), _mo(mo) {
 593     init_class_id(Class_Store);
 594   }
 595   StoreNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, Node *oop_store, MemOrd mo)
 596     : MemNode(c, mem, adr, at, val, oop_store), _mo(mo) {
 597     init_class_id(Class_Store);
 598   }
 599 
 600   inline bool is_unordered() const { return !is_release(); }
 601   inline bool is_release() const {
 602     assert((_mo == unordered || _mo == release), "unexpected");
 603     return _mo == release;
 604   }
 605 
 606   // Conservatively release stores of object references in order to
 607   // ensure visibility of object initialization.
 608   static inline MemOrd release_if_reference(const BasicType t) {
 609 #ifdef AARCH64
 610     // AArch64 doesn't need a release store here because object
 611     // initialization contains the necessary barriers.
 612     return unordered;
 613 #else
 614     const MemOrd mo = (t == T_ARRAY ||
 615                        t == T_ADDRESS || // Might be the address of an object reference (`boxing').
 616                        t == T_OBJECT) ? release : unordered;
 617     return mo;
 618 #endif
 619   }
 620 
 621   // Polymorphic factory method
 622   //
 623   // We must ensure that stores of object references will be visible
 624   // only after the object's initialization. So the callers of this
 625   // procedure must indicate that the store requires `release'
 626   // semantics, if the stored value is an object reference that might
 627   // point to a new object and may become externally visible.
 628   static StoreNode* make(PhaseGVN& gvn, Node* c, Node* mem, Node* adr,
 629                          const TypePtr* at, Node* val, BasicType bt,
 630                          MemOrd mo, bool require_atomic_access = false);
 631 
 632   virtual uint hash() const;    // Check the type
 633 
 634   // If the store is to Field memory and the pointer is non-null, we can
 635   // zero out the control input.
 636   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 637 
 638   // Compute a new Type for this node.  Basically we just do the pre-check,
 639   // then call the virtual add() to set the type.
 640   virtual const Type* Value(PhaseGVN* phase) const;
 641 
 642   // Check for identity function on memory (Load then Store at same address)
 643   virtual Node* Identity(PhaseGVN* phase);
 644 
 645   // Do not match memory edge
 646   virtual uint match_edge(uint idx) const;
 647 
 648   virtual const Type *bottom_type() const;  // returns Type::MEMORY
 649 
 650   // Map a store opcode to its corresponding own opcode, trivially.
 651   virtual int store_Opcode() const { return Opcode(); }
 652 
 653   // have all possible loads of the value stored been optimized away?
 654   bool value_never_loaded(PhaseValues* phase) const;
 655 
 656   bool  has_reinterpret_variant(const Type* vt);
 657   Node* convert_to_reinterpret_store(PhaseGVN& gvn, Node* val, const Type* vt);
 658 
 659   MemBarNode* trailing_membar() const;
 660 
 661 #ifndef PRODUCT
 662   virtual void dump_spec(outputStream *st) const {
 663     MemNode::dump_spec(st);
 664     if (is_release())  st->print("is_release");
 665   }
 666 #endif
 667 
 668 private:
 669   virtual bool depends_only_on_test_impl() const { return false; }
 670 };
 671 
 672 //------------------------------StoreBNode-------------------------------------
 673 // Store byte to memory
 674 class StoreBNode : public StoreNode {
 675 public:
 676   StoreBNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 677     : StoreNode(c, mem, adr, at, val, mo) {}
 678   virtual int Opcode() const;
 679   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 680   virtual BasicType value_basic_type() const { return T_BYTE; }
 681 };
 682 
 683 //------------------------------StoreCNode-------------------------------------
 684 // Store char/short to memory
 685 class StoreCNode : public StoreNode {
 686 public:
 687   StoreCNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 688     : StoreNode(c, mem, adr, at, val, mo) {}
 689   virtual int Opcode() const;
 690   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
 691   virtual BasicType value_basic_type() const { return T_CHAR; }
 692 };
 693 
 694 //------------------------------StoreINode-------------------------------------
 695 // Store int to memory
 696 class StoreINode : public StoreNode {
 697 public:
 698   StoreINode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 699     : StoreNode(c, mem, adr, at, val, mo) {}
 700   virtual int Opcode() const;
 701   virtual BasicType value_basic_type() const { return T_INT; }
 702 };
 703 
 704 //------------------------------StoreLNode-------------------------------------
 705 // Store long to memory
 706 class StoreLNode : public StoreNode {
 707   virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
 708   virtual bool cmp( const Node &n ) const {
 709     return _require_atomic_access == ((StoreLNode&)n)._require_atomic_access
 710       && StoreNode::cmp(n);
 711   }
 712   virtual uint size_of() const { return sizeof(*this); }
 713   const bool _require_atomic_access;  // is piecewise store forbidden?
 714 
 715 public:
 716   StoreLNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo, bool require_atomic_access = false)
 717     : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
 718   virtual int Opcode() const;
 719   virtual BasicType value_basic_type() const { return T_LONG; }
 720   bool require_atomic_access() const { return _require_atomic_access; }
 721 
 722 #ifndef PRODUCT
 723   virtual void dump_spec(outputStream *st) const {
 724     StoreNode::dump_spec(st);
 725     if (_require_atomic_access)  st->print(" Atomic!");
 726   }
 727 #endif
 728 };
 729 
 730 //------------------------------StoreFNode-------------------------------------
 731 // Store float to memory
 732 class StoreFNode : public StoreNode {
 733 public:
 734   StoreFNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 735     : StoreNode(c, mem, adr, at, val, mo) {}
 736   virtual int Opcode() const;
 737   virtual BasicType value_basic_type() const { return T_FLOAT; }
 738 };
 739 
 740 //------------------------------StoreDNode-------------------------------------
 741 // Store double to memory
 742 class StoreDNode : public StoreNode {
 743   virtual uint hash() const { return StoreNode::hash() + _require_atomic_access; }
 744   virtual bool cmp( const Node &n ) const {
 745     return _require_atomic_access == ((StoreDNode&)n)._require_atomic_access
 746       && StoreNode::cmp(n);
 747   }
 748   virtual uint size_of() const { return sizeof(*this); }
 749   const bool _require_atomic_access;  // is piecewise store forbidden?
 750 public:
 751   StoreDNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val,
 752              MemOrd mo, bool require_atomic_access = false)
 753     : StoreNode(c, mem, adr, at, val, mo), _require_atomic_access(require_atomic_access) {}
 754   virtual int Opcode() const;
 755   virtual BasicType value_basic_type() const { return T_DOUBLE; }
 756   bool require_atomic_access() const { return _require_atomic_access; }
 757 
 758 #ifndef PRODUCT
 759   virtual void dump_spec(outputStream *st) const {
 760     StoreNode::dump_spec(st);
 761     if (_require_atomic_access)  st->print(" Atomic!");
 762   }
 763 #endif
 764 
 765 };
 766 
 767 //------------------------------StorePNode-------------------------------------
 768 // Store pointer to memory
 769 class StorePNode : public StoreNode {
 770 public:
 771   StorePNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 772     : StoreNode(c, mem, adr, at, val, mo) {}
 773   virtual int Opcode() const;
 774   virtual BasicType value_basic_type() const { return T_ADDRESS; }
 775 };
 776 
 777 //------------------------------StoreNNode-------------------------------------
 778 // Store narrow oop to memory
 779 class StoreNNode : public StoreNode {
 780 public:
 781   StoreNNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 782     : StoreNode(c, mem, adr, at, val, mo) {}
 783   virtual int Opcode() const;
 784   virtual BasicType value_basic_type() const { return T_NARROWOOP; }
 785 };
 786 
 787 //------------------------------StoreNKlassNode--------------------------------------
 788 // Store narrow klass to memory
 789 class StoreNKlassNode : public StoreNNode {
 790 public:
 791   StoreNKlassNode(Node *c, Node *mem, Node *adr, const TypePtr* at, Node *val, MemOrd mo)
 792     : StoreNNode(c, mem, adr, at, val, mo) {}
 793   virtual int Opcode() const;
 794   virtual BasicType value_basic_type() const { return T_NARROWKLASS; }
 795 };
 796 
 797 //------------------------------SCMemProjNode---------------------------------------
 798 // This class defines a projection of the memory  state of a store conditional node.
 799 // These nodes return a value, but also update memory.
 800 class SCMemProjNode : public ProjNode {
 801 public:
 802   enum {SCMEMPROJCON = (uint)-2};
 803   SCMemProjNode( Node *src) : ProjNode( src, SCMEMPROJCON) { }
 804   virtual int Opcode() const;
 805   virtual bool      is_CFG() const  { return false; }
 806   virtual const Type *bottom_type() const {return Type::MEMORY;}
 807   virtual uint ideal_reg() const { return 0;} // memory projections don't have a register
 808   virtual const Type* Value(PhaseGVN* phase) const;
 809 #ifndef PRODUCT
 810   virtual void dump_spec(outputStream *st) const {};
 811 #endif
 812 };
 813 
 814 //------------------------------LoadStoreNode---------------------------
 815 // Note: is_Mem() method returns 'true' for this class.
 816 class LoadStoreNode : public Node {
 817 private:
 818   const Type* const _type;      // What kind of value is loaded?
 819   uint8_t _barrier_data;        // Bit field with barrier information
 820   virtual uint size_of() const; // Size is bigger
 821 #ifdef ASSERT
 822   const TypePtr* _adr_type;     // What kind of memory is being addressed?
 823 #endif // ASSERT
 824 public:
 825   LoadStoreNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* rt, uint required );
 826   virtual uint match_edge(uint idx) const { return idx == MemNode::Address || idx == MemNode::ValueIn; }
 827 
 828   virtual const Type *bottom_type() const { return _type; }
 829   virtual uint ideal_reg() const;
 830   virtual const TypePtr* adr_type() const;
 831   virtual const Type* Value(PhaseGVN* phase) const;
 832 
 833   bool result_not_used() const;
 834   MemBarNode* trailing_membar() const;
 835 
 836   uint8_t barrier_data() { return _barrier_data; }
 837   void set_barrier_data(uint8_t barrier_data) { _barrier_data = barrier_data; }
 838 
 839 private:
 840   virtual bool depends_only_on_test_impl() const { return false; }
 841 };
 842 
 843 class LoadStoreConditionalNode : public LoadStoreNode {
 844 public:
 845   enum {
 846     ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
 847   };
 848   LoadStoreConditionalNode(Node *c, Node *mem, Node *adr, Node *val, Node *ex);
 849   virtual const Type* Value(PhaseGVN* phase) const;
 850 };
 851 
 852 class CompareAndSwapNode : public LoadStoreConditionalNode {
 853 private:
 854   const MemNode::MemOrd _mem_ord;
 855 public:
 856   CompareAndSwapNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : LoadStoreConditionalNode(c, mem, adr, val, ex), _mem_ord(mem_ord) {}
 857   MemNode::MemOrd order() const {
 858     return _mem_ord;
 859   }
 860   virtual uint size_of() const { return sizeof(*this); }
 861 };
 862 
 863 class CompareAndExchangeNode : public LoadStoreNode {
 864 private:
 865   const MemNode::MemOrd _mem_ord;
 866 public:
 867   enum {
 868     ExpectedIn = MemNode::ValueIn+1 // One more input than MemNode
 869   };
 870   CompareAndExchangeNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord, const TypePtr* at, const Type* t) :
 871     LoadStoreNode(c, mem, adr, val, at, t, 5), _mem_ord(mem_ord) {
 872      init_req(ExpectedIn, ex );
 873   }
 874 
 875   MemNode::MemOrd order() const {
 876     return _mem_ord;
 877   }
 878   virtual uint size_of() const { return sizeof(*this); }
 879 };
 880 
 881 //------------------------------CompareAndSwapBNode---------------------------
 882 class CompareAndSwapBNode : public CompareAndSwapNode {
 883 public:
 884   CompareAndSwapBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 885   virtual int Opcode() const;
 886 };
 887 
 888 //------------------------------CompareAndSwapSNode---------------------------
 889 class CompareAndSwapSNode : public CompareAndSwapNode {
 890 public:
 891   CompareAndSwapSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 892   virtual int Opcode() const;
 893 };
 894 
 895 //------------------------------CompareAndSwapINode---------------------------
 896 class CompareAndSwapINode : public CompareAndSwapNode {
 897 public:
 898   CompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 899   virtual int Opcode() const;
 900 };
 901 
 902 //------------------------------CompareAndSwapLNode---------------------------
 903 class CompareAndSwapLNode : public CompareAndSwapNode {
 904 public:
 905   CompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 906   virtual int Opcode() const;
 907 };
 908 
 909 //------------------------------CompareAndSwapPNode---------------------------
 910 class CompareAndSwapPNode : public CompareAndSwapNode {
 911 public:
 912   CompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 913   virtual int Opcode() const;
 914 };
 915 
 916 //------------------------------CompareAndSwapNNode---------------------------
 917 class CompareAndSwapNNode : public CompareAndSwapNode {
 918 public:
 919   CompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 920   virtual int Opcode() const;
 921 };
 922 
 923 //------------------------------WeakCompareAndSwapBNode---------------------------
 924 class WeakCompareAndSwapBNode : public CompareAndSwapNode {
 925 public:
 926   WeakCompareAndSwapBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 927   virtual int Opcode() const;
 928 };
 929 
 930 //------------------------------WeakCompareAndSwapSNode---------------------------
 931 class WeakCompareAndSwapSNode : public CompareAndSwapNode {
 932 public:
 933   WeakCompareAndSwapSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 934   virtual int Opcode() const;
 935 };
 936 
 937 //------------------------------WeakCompareAndSwapINode---------------------------
 938 class WeakCompareAndSwapINode : public CompareAndSwapNode {
 939 public:
 940   WeakCompareAndSwapINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 941   virtual int Opcode() const;
 942 };
 943 
 944 //------------------------------WeakCompareAndSwapLNode---------------------------
 945 class WeakCompareAndSwapLNode : public CompareAndSwapNode {
 946 public:
 947   WeakCompareAndSwapLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 948   virtual int Opcode() const;
 949 };
 950 
 951 //------------------------------WeakCompareAndSwapPNode---------------------------
 952 class WeakCompareAndSwapPNode : public CompareAndSwapNode {
 953 public:
 954   WeakCompareAndSwapPNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 955   virtual int Opcode() const;
 956 };
 957 
 958 //------------------------------WeakCompareAndSwapNNode---------------------------
 959 class WeakCompareAndSwapNNode : public CompareAndSwapNode {
 960 public:
 961   WeakCompareAndSwapNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, MemNode::MemOrd mem_ord) : CompareAndSwapNode(c, mem, adr, val, ex, mem_ord) { }
 962   virtual int Opcode() const;
 963 };
 964 
 965 //------------------------------CompareAndExchangeBNode---------------------------
 966 class CompareAndExchangeBNode : public CompareAndExchangeNode {
 967 public:
 968   CompareAndExchangeBNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::BYTE) { }
 969   virtual int Opcode() const;
 970 };
 971 
 972 
 973 //------------------------------CompareAndExchangeSNode---------------------------
 974 class CompareAndExchangeSNode : public CompareAndExchangeNode {
 975 public:
 976   CompareAndExchangeSNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::SHORT) { }
 977   virtual int Opcode() const;
 978 };
 979 
 980 //------------------------------CompareAndExchangeLNode---------------------------
 981 class CompareAndExchangeLNode : public CompareAndExchangeNode {
 982 public:
 983   CompareAndExchangeLNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeLong::LONG) { }
 984   virtual int Opcode() const;
 985 };
 986 
 987 
 988 //------------------------------CompareAndExchangeINode---------------------------
 989 class CompareAndExchangeINode : public CompareAndExchangeNode {
 990 public:
 991   CompareAndExchangeINode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, TypeInt::INT) { }
 992   virtual int Opcode() const;
 993 };
 994 
 995 
 996 //------------------------------CompareAndExchangePNode---------------------------
 997 class CompareAndExchangePNode : public CompareAndExchangeNode {
 998 public:
 999   CompareAndExchangePNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, t) { }
1000   virtual int Opcode() const;
1001 };
1002 
1003 //------------------------------CompareAndExchangeNNode---------------------------
1004 class CompareAndExchangeNNode : public CompareAndExchangeNode {
1005 public:
1006   CompareAndExchangeNNode( Node *c, Node *mem, Node *adr, Node *val, Node *ex, const TypePtr* at, const Type* t, MemNode::MemOrd mem_ord) : CompareAndExchangeNode(c, mem, adr, val, ex, mem_ord, at, t) { }
1007   virtual int Opcode() const;
1008 };
1009 
1010 //------------------------------GetAndAddBNode---------------------------
1011 class GetAndAddBNode : public LoadStoreNode {
1012 public:
1013   GetAndAddBNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::BYTE, 4) { }
1014   virtual int Opcode() const;
1015 };
1016 
1017 //------------------------------GetAndAddSNode---------------------------
1018 class GetAndAddSNode : public LoadStoreNode {
1019 public:
1020   GetAndAddSNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::SHORT, 4) { }
1021   virtual int Opcode() const;
1022 };
1023 
1024 //------------------------------GetAndAddINode---------------------------
1025 class GetAndAddINode : public LoadStoreNode {
1026 public:
1027   GetAndAddINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
1028   virtual int Opcode() const;
1029 };
1030 
1031 //------------------------------GetAndAddLNode---------------------------
1032 class GetAndAddLNode : public LoadStoreNode {
1033 public:
1034   GetAndAddLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
1035   virtual int Opcode() const;
1036 };
1037 
1038 //------------------------------GetAndSetBNode---------------------------
1039 class GetAndSetBNode : public LoadStoreNode {
1040 public:
1041   GetAndSetBNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::BYTE, 4) { }
1042   virtual int Opcode() const;
1043 };
1044 
1045 //------------------------------GetAndSetSNode---------------------------
1046 class GetAndSetSNode : public LoadStoreNode {
1047 public:
1048   GetAndSetSNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::SHORT, 4) { }
1049   virtual int Opcode() const;
1050 };
1051 
1052 //------------------------------GetAndSetINode---------------------------
1053 class GetAndSetINode : public LoadStoreNode {
1054 public:
1055   GetAndSetINode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeInt::INT, 4) { }
1056   virtual int Opcode() const;
1057 };
1058 
1059 //------------------------------GetAndSetLNode---------------------------
1060 class GetAndSetLNode : public LoadStoreNode {
1061 public:
1062   GetAndSetLNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at ) : LoadStoreNode(c, mem, adr, val, at, TypeLong::LONG, 4) { }
1063   virtual int Opcode() const;
1064 };
1065 
1066 //------------------------------GetAndSetPNode---------------------------
1067 class GetAndSetPNode : public LoadStoreNode {
1068 public:
1069   GetAndSetPNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
1070   virtual int Opcode() const;
1071 };
1072 
1073 //------------------------------GetAndSetNNode---------------------------
1074 class GetAndSetNNode : public LoadStoreNode {
1075 public:
1076   GetAndSetNNode( Node *c, Node *mem, Node *adr, Node *val, const TypePtr* at, const Type* t ) : LoadStoreNode(c, mem, adr, val, at, t, 4) { }
1077   virtual int Opcode() const;
1078 };
1079 
1080 //------------------------------ClearArray-------------------------------------
1081 class ClearArrayNode: public Node {
1082 private:
1083   bool _is_large;
1084   static Node* make_address(Node* dest, Node* offset, bool raw_base, PhaseGVN* phase);
1085 public:
1086   ClearArrayNode( Node *ctrl, Node *arymem, Node *word_cnt, Node *base, bool is_large)
1087     : Node(ctrl,arymem,word_cnt,base), _is_large(is_large) {
1088     init_class_id(Class_ClearArray);
1089   }
1090   virtual int         Opcode() const;
1091   virtual const Type *bottom_type() const { return Type::MEMORY; }
1092   // ClearArray modifies array elements, and so affects only the
1093   // array memory addressed by the bottom_type of its base address.
1094   virtual const class TypePtr *adr_type() const;
1095   virtual Node* Identity(PhaseGVN* phase);
1096   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1097   virtual uint match_edge(uint idx) const;
1098   bool is_large() const { return _is_large; }
1099   virtual uint size_of() const { return sizeof(ClearArrayNode); }
1100   virtual uint hash() const { return Node::hash() + _is_large; }
1101   virtual bool cmp(const Node& n) const {
1102     return Node::cmp(n) && _is_large == ((ClearArrayNode&)n).is_large();
1103   }
1104 
1105   // Clear the given area of an object or array.
1106   // The start offset must always be aligned mod BytesPerInt.
1107   // The end offset must always be aligned mod BytesPerLong.
1108   // Return the new memory.
1109   static Node* clear_memory(Node* control, Node* mem, Node* dest,
1110                             intptr_t start_offset,
1111                             intptr_t end_offset,
1112                             bool raw_base,
1113                             PhaseGVN* phase);
1114   static Node* clear_memory(Node* control, Node* mem, Node* dest,
1115                             intptr_t start_offset,
1116                             Node* end_offset,
1117                             bool raw_base,
1118                             PhaseGVN* phase);
1119   static Node* clear_memory(Node* control, Node* mem, Node* dest,
1120                             Node* start_offset,
1121                             Node* end_offset,
1122                             bool raw_base,
1123                             PhaseGVN* phase);
1124   // Return allocation input memory edge if it is different instance
1125   // or itself if it is the one we are looking for.
1126   static bool step_through(Node** np, uint instance_id, PhaseValues* phase);
1127 
1128 private:
1129   virtual bool depends_only_on_test_impl() const { return false; }
1130 };
1131 
1132 //------------------------------MemBar-----------------------------------------
1133 // There are different flavors of Memory Barriers to match the Java Memory
1134 // Model.  Monitor-enter and volatile-load act as Acquires: no following ref
1135 // can be moved to before them.  We insert a MemBar-Acquire after a FastLock or
1136 // volatile-load.  Monitor-exit and volatile-store act as Release: no
1137 // preceding ref can be moved to after them.  We insert a MemBar-Release
1138 // before a FastUnlock or volatile-store.  All volatiles need to be
1139 // serialized, so we follow all volatile-stores with a MemBar-Volatile to
1140 // separate it from any following volatile-load.
1141 class MemBarNode: public MultiNode {
1142   virtual uint hash() const ;                  // { return NO_HASH; }
1143   virtual bool cmp( const Node &n ) const ;    // Always fail, except on self
1144 
1145   virtual uint size_of() const { return sizeof(*this); }
1146   // Memory type this node is serializing.  Usually either rawptr or bottom.
1147   const TypePtr* _adr_type;
1148 
1149   // How is this membar related to a nearby memory access?
1150   enum {
1151     Standalone,
1152     TrailingLoad,
1153     TrailingStore,
1154     LeadingStore,
1155     TrailingLoadStore,
1156     LeadingLoadStore,
1157     TrailingExpandedArrayCopy
1158   } _kind;
1159 
1160 #ifdef ASSERT
1161   uint _pair_idx;
1162 #endif
1163 
1164 public:
1165   enum {
1166     Precedent = TypeFunc::Parms  // optional edge to force precedence
1167   };
1168   MemBarNode(Compile* C, int alias_idx, Node* precedent);
1169   virtual int Opcode() const = 0;
1170   virtual const class TypePtr *adr_type() const { return _adr_type; }
1171   virtual const Type* Value(PhaseGVN* phase) const;
1172   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1173   virtual uint match_edge(uint idx) const { return 0; }
1174   virtual const Type *bottom_type() const { return TypeTuple::MEMBAR; }
1175   virtual Node *match( const ProjNode *proj, const Matcher *m );
1176   // Factory method.  Builds a wide or narrow membar.
1177   // Optional 'precedent' becomes an extra edge if not null.
1178   static MemBarNode* make(Compile* C, int opcode,
1179                           int alias_idx = Compile::AliasIdxBot,
1180                           Node* precedent = nullptr);
1181 
1182   MemBarNode* trailing_membar() const;
1183   MemBarNode* leading_membar() const;
1184 
1185   void set_trailing_load() { _kind = TrailingLoad; }
1186   bool trailing_load() const { return _kind == TrailingLoad; }
1187   bool trailing_store() const { return _kind == TrailingStore; }
1188   bool leading_store() const { return _kind == LeadingStore; }
1189   bool trailing_load_store() const { return _kind == TrailingLoadStore; }
1190   bool leading_load_store() const { return _kind == LeadingLoadStore; }
1191   bool trailing() const { return _kind == TrailingLoad || _kind == TrailingStore || _kind == TrailingLoadStore; }
1192   bool leading() const { return _kind == LeadingStore || _kind == LeadingLoadStore; }
1193   bool standalone() const { return _kind == Standalone; }
1194   void set_trailing_expanded_array_copy() { _kind = TrailingExpandedArrayCopy; }
1195   bool trailing_expanded_array_copy() const { return _kind == TrailingExpandedArrayCopy; }
1196 
1197   static void set_store_pair(MemBarNode* leading, MemBarNode* trailing);
1198   static void set_load_store_pair(MemBarNode* leading, MemBarNode* trailing);
1199 
1200   void remove(PhaseIterGVN *igvn);
1201 };
1202 
1203 // "Acquire" - no following ref can move before (but earlier refs can
1204 // follow, like an early Load stalled in cache).  Requires multi-cpu
1205 // visibility.  Inserted after a volatile load.
1206 class MemBarAcquireNode: public MemBarNode {
1207 public:
1208   MemBarAcquireNode(Compile* C, int alias_idx, Node* precedent)
1209     : MemBarNode(C, alias_idx, precedent) {}
1210   virtual int Opcode() const;
1211 };
1212 
1213 // "Acquire" - no following ref can move before (but earlier refs can
1214 // follow, like an early Load stalled in cache).  Requires multi-cpu
1215 // visibility.  Inserted independent of any load, as required
1216 // for intrinsic Unsafe.loadFence().
1217 class LoadFenceNode: public MemBarNode {
1218 public:
1219   LoadFenceNode(Compile* C, int alias_idx, Node* precedent)
1220     : MemBarNode(C, alias_idx, precedent) {}
1221   virtual int Opcode() const;
1222 };
1223 
1224 // "Release" - no earlier ref can move after (but later refs can move
1225 // up, like a speculative pipelined cache-hitting Load).  Requires
1226 // multi-cpu visibility.  Inserted before a volatile store.
1227 class MemBarReleaseNode: public MemBarNode {
1228 public:
1229   MemBarReleaseNode(Compile* C, int alias_idx, Node* precedent)
1230     : MemBarNode(C, alias_idx, precedent) {}
1231   virtual int Opcode() const;
1232 };
1233 
1234 // "Release" - no earlier ref can move after (but later refs can move
1235 // up, like a speculative pipelined cache-hitting Load).  Requires
1236 // multi-cpu visibility.  Inserted independent of any store, as required
1237 // for intrinsic Unsafe.storeFence().
1238 class StoreFenceNode: public MemBarNode {
1239 public:
1240   StoreFenceNode(Compile* C, int alias_idx, Node* precedent)
1241     : MemBarNode(C, alias_idx, precedent) {}
1242   virtual int Opcode() const;
1243 };
1244 
1245 // "Acquire" - no following ref can move before (but earlier refs can
1246 // follow, like an early Load stalled in cache).  Requires multi-cpu
1247 // visibility.  Inserted after a FastLock.
1248 class MemBarAcquireLockNode: public MemBarNode {
1249 public:
1250   MemBarAcquireLockNode(Compile* C, int alias_idx, Node* precedent)
1251     : MemBarNode(C, alias_idx, precedent) {}
1252   virtual int Opcode() const;
1253 };
1254 
1255 // "Release" - no earlier ref can move after (but later refs can move
1256 // up, like a speculative pipelined cache-hitting Load).  Requires
1257 // multi-cpu visibility.  Inserted before a FastUnLock.
1258 class MemBarReleaseLockNode: public MemBarNode {
1259 public:
1260   MemBarReleaseLockNode(Compile* C, int alias_idx, Node* precedent)
1261     : MemBarNode(C, alias_idx, precedent) {}
1262   virtual int Opcode() const;
1263 };
1264 
1265 class MemBarStoreStoreNode: public MemBarNode {
1266 public:
1267   MemBarStoreStoreNode(Compile* C, int alias_idx, Node* precedent)
1268     : MemBarNode(C, alias_idx, precedent) {
1269     init_class_id(Class_MemBarStoreStore);
1270   }
1271   virtual int Opcode() const;
1272 };
1273 
1274 class StoreStoreFenceNode: public MemBarNode {
1275 public:
1276   StoreStoreFenceNode(Compile* C, int alias_idx, Node* precedent)
1277     : MemBarNode(C, alias_idx, precedent) {}
1278   virtual int Opcode() const;
1279 };
1280 
1281 // Ordering between a volatile store and a following volatile load.
1282 // Requires multi-CPU visibility?
1283 class MemBarVolatileNode: public MemBarNode {
1284 public:
1285   MemBarVolatileNode(Compile* C, int alias_idx, Node* precedent)
1286     : MemBarNode(C, alias_idx, precedent) {}
1287   virtual int Opcode() const;
1288 };
1289 
1290 // Ordering within the same CPU.  Used to order unsafe memory references
1291 // inside the compiler when we lack alias info.  Not needed "outside" the
1292 // compiler because the CPU does all the ordering for us.
1293 class MemBarCPUOrderNode: public MemBarNode {
1294 public:
1295   MemBarCPUOrderNode(Compile* C, int alias_idx, Node* precedent)
1296     : MemBarNode(C, alias_idx, precedent) {}
1297   virtual int Opcode() const;
1298   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1299 };
1300 
1301 class OnSpinWaitNode: public MemBarNode {
1302 public:
1303   OnSpinWaitNode(Compile* C, int alias_idx, Node* precedent)
1304     : MemBarNode(C, alias_idx, precedent) {}
1305   virtual int Opcode() const;
1306 };
1307 
1308 // Isolation of object setup after an AllocateNode and before next safepoint.
1309 // (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
1310 class InitializeNode: public MemBarNode {
1311   friend class AllocateNode;
1312 
1313   enum {
1314     Incomplete    = 0,
1315     Complete      = 1,
1316     WithArraycopy = 2
1317   };
1318   int _is_complete;
1319 
1320   bool _does_not_escape;
1321 
1322 public:
1323   enum {
1324     Control    = TypeFunc::Control,
1325     Memory     = TypeFunc::Memory,     // MergeMem for states affected by this op
1326     RawAddress = TypeFunc::Parms+0,    // the newly-allocated raw address
1327     RawStores  = TypeFunc::Parms+1     // zero or more stores (or TOP)
1328   };
1329 
1330   InitializeNode(Compile* C, int adr_type, Node* rawoop);
1331   virtual int Opcode() const;
1332   virtual uint size_of() const { return sizeof(*this); }
1333   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1334   virtual const RegMask &in_RegMask(uint) const;  // mask for RawAddress
1335 
1336   // Manage incoming memory edges via a MergeMem on in(Memory):
1337   Node* memory(uint alias_idx);
1338 
1339   // The raw memory edge coming directly from the Allocation.
1340   // The contents of this memory are *always* all-zero-bits.
1341   Node* zero_memory() { return memory(Compile::AliasIdxRaw); }
1342 
1343   // Return the corresponding allocation for this initialization (or null if none).
1344   // (Note: Both InitializeNode::allocation and AllocateNode::initialization
1345   // are defined in graphKit.cpp, which sets up the bidirectional relation.)
1346   AllocateNode* allocation();
1347 
1348   // Anything other than zeroing in this init?
1349   bool is_non_zero();
1350 
1351   // An InitializeNode must completed before macro expansion is done.
1352   // Completion requires that the AllocateNode must be followed by
1353   // initialization of the new memory to zero, then to any initializers.
1354   bool is_complete() { return _is_complete != Incomplete; }
1355   bool is_complete_with_arraycopy() { return (_is_complete & WithArraycopy) != 0; }
1356 
1357   // Mark complete.  (Must not yet be complete.)
1358   void set_complete(PhaseGVN* phase);
1359   void set_complete_with_arraycopy() { _is_complete = Complete | WithArraycopy; }
1360 
1361   bool does_not_escape() { return _does_not_escape; }
1362   void set_does_not_escape() { _does_not_escape = true; }
1363 
1364 #ifdef ASSERT
1365   // ensure all non-degenerate stores are ordered and non-overlapping
1366   bool stores_are_sane(PhaseValues* phase);
1367 #endif //ASSERT
1368 
1369   // See if this store can be captured; return offset where it initializes.
1370   // Return 0 if the store cannot be moved (any sort of problem).
1371   intptr_t can_capture_store(StoreNode* st, PhaseGVN* phase, bool can_reshape);
1372 
1373   // Capture another store; reformat it to write my internal raw memory.
1374   // Return the captured copy, else null if there is some sort of problem.
1375   Node* capture_store(StoreNode* st, intptr_t start, PhaseGVN* phase, bool can_reshape);
1376 
1377   // Find captured store which corresponds to the range [start..start+size).
1378   // Return my own memory projection (meaning the initial zero bits)
1379   // if there is no such store.  Return null if there is a problem.
1380   Node* find_captured_store(intptr_t start, int size_in_bytes, PhaseValues* phase);
1381 
1382   // Called when the associated AllocateNode is expanded into CFG.
1383   Node* complete_stores(Node* rawctl, Node* rawmem, Node* rawptr,
1384                         intptr_t header_size, Node* size_in_bytes,
1385                         PhaseIterGVN* phase);
1386 
1387   // An Initialize node has multiple memory projections. Helper methods used when the node is removed.
1388   // For use at parse time
1389   void replace_mem_projs_by(Node* mem, Compile* C);
1390   // For use with IGVN
1391   void replace_mem_projs_by(Node* mem, PhaseIterGVN* igvn);
1392 
1393   // Does a NarrowMemProj with this adr_type and this node as input already exist?
1394   bool already_has_narrow_mem_proj_with_adr_type(const TypePtr* adr_type) const;
1395 
1396   // Used during matching: find the MachProj memory projection if there's one. Expectation is that there should be at
1397   // most one.
1398   MachProjNode* mem_mach_proj() const;
1399 
1400 private:
1401   void remove_extra_zeroes();
1402 
1403   // Find out where a captured store should be placed (or already is placed).
1404   int captured_store_insertion_point(intptr_t start, int size_in_bytes,
1405                                      PhaseValues* phase);
1406 
1407   static intptr_t get_store_offset(Node* st, PhaseValues* phase);
1408 
1409   Node* make_raw_address(intptr_t offset, PhaseGVN* phase);
1410 
1411   bool detect_init_independence(Node* value, PhaseGVN* phase);
1412 
1413   void coalesce_subword_stores(intptr_t header_size, Node* size_in_bytes,
1414                                PhaseGVN* phase);
1415 
1416   intptr_t find_next_fullword_store(uint i, PhaseGVN* phase);
1417 
1418   // Iterate with i over all NarrowMemProj uses calling callback
1419   template <class Callback, class Iterator> NarrowMemProjNode* apply_to_narrow_mem_projs_any_iterator(Iterator i, Callback callback) const {
1420     auto filter = [&](ProjNode* proj) {
1421       if (proj->is_NarrowMemProj() && callback(proj->as_NarrowMemProj()) == BREAK_AND_RETURN_CURRENT_PROJ) {
1422         return BREAK_AND_RETURN_CURRENT_PROJ;
1423       }
1424       return CONTINUE;
1425     };
1426     ProjNode* res = apply_to_projs_any_iterator(i, filter);
1427     if (res == nullptr) {
1428       return nullptr;
1429     }
1430     return res->as_NarrowMemProj();
1431   }
1432 
1433 public:
1434 
1435   // callback is allowed to add new uses that will then be iterated over
1436   template <class Callback> void for_each_narrow_mem_proj_with_new_uses(Callback callback) const {
1437     auto callback_always_continue = [&](NarrowMemProjNode* proj) {
1438       callback(proj);
1439       return MultiNode::CONTINUE;
1440     };
1441     DUIterator i = outs();
1442     apply_to_narrow_mem_projs_any_iterator(UsesIterator(i, this), callback_always_continue);
1443   }
1444 };
1445 
1446 //------------------------------MergeMem---------------------------------------
1447 // (See comment in memnode.cpp near MergeMemNode::MergeMemNode for semantics.)
1448 class MergeMemNode: public Node {
1449   virtual uint hash() const ;                  // { return NO_HASH; }
1450   virtual bool cmp( const Node &n ) const ;    // Always fail, except on self
1451   friend class MergeMemStream;
1452   MergeMemNode(Node* def);  // clients use MergeMemNode::make
1453 
1454 public:
1455   // If the input is a whole memory state, clone it with all its slices intact.
1456   // Otherwise, make a new memory state with just that base memory input.
1457   // In either case, the result is a newly created MergeMem.
1458   static MergeMemNode* make(Node* base_memory);
1459 
1460   virtual int Opcode() const;
1461   virtual Node* Identity(PhaseGVN* phase);
1462   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
1463   virtual uint ideal_reg() const { return NotAMachineReg; }
1464   virtual uint match_edge(uint idx) const { return 0; }
1465   virtual const RegMask &out_RegMask() const;
1466   virtual const Type *bottom_type() const { return Type::MEMORY; }
1467   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1468   // sparse accessors
1469   // Fetch the previously stored "set_memory_at", or else the base memory.
1470   // (Caller should clone it if it is a phi-nest.)
1471   Node* memory_at(uint alias_idx) const;
1472   // set the memory, regardless of its previous value
1473   void set_memory_at(uint alias_idx, Node* n);
1474   // the "base" is the memory that provides the non-finite support
1475   Node* base_memory() const       { return in(Compile::AliasIdxBot); }
1476   // warning: setting the base can implicitly set any of the other slices too
1477   void set_base_memory(Node* def);
1478   // sentinel value which denotes a copy of the base memory:
1479   Node*   empty_memory() const    { return in(Compile::AliasIdxTop); }
1480   static Node* make_empty_memory(); // where the sentinel comes from
1481   bool is_empty_memory(Node* n) const { assert((n == empty_memory()) == n->is_top(), "sanity"); return n->is_top(); }
1482   // hook for the iterator, to perform any necessary setup
1483   void iteration_setup(const MergeMemNode* other = nullptr);
1484   // push sentinels until I am at least as long as the other (semantic no-op)
1485   void grow_to_match(const MergeMemNode* other);
1486   bool verify_sparse() const PRODUCT_RETURN0;
1487 #ifndef PRODUCT
1488   virtual void dump_spec(outputStream *st) const;
1489 #endif
1490 };
1491 
1492 class MergeMemStream : public StackObj {
1493  private:
1494   MergeMemNode*       _mm;
1495   const MergeMemNode* _mm2;  // optional second guy, contributes non-empty iterations
1496   Node*               _mm_base;  // loop-invariant base memory of _mm
1497   int                 _idx;
1498   int                 _cnt;
1499   Node*               _mem;
1500   Node*               _mem2;
1501   int                 _cnt2;
1502 
1503   void init(MergeMemNode* mm, const MergeMemNode* mm2 = nullptr) {
1504     // subsume_node will break sparseness at times, whenever a memory slice
1505     // folds down to a copy of the base ("fat") memory.  In such a case,
1506     // the raw edge will update to base, although it should be top.
1507     // This iterator will recognize either top or base_memory as an
1508     // "empty" slice.  See is_empty, is_empty2, and next below.
1509     //
1510     // The sparseness property is repaired in MergeMemNode::Ideal.
1511     // As long as access to a MergeMem goes through this iterator
1512     // or the memory_at accessor, flaws in the sparseness will
1513     // never be observed.
1514     //
1515     // Also, iteration_setup repairs sparseness.
1516     assert(mm->verify_sparse(), "please, no dups of base");
1517     assert(mm2==nullptr || mm2->verify_sparse(), "please, no dups of base");
1518 
1519     _mm  = mm;
1520     _mm_base = mm->base_memory();
1521     _mm2 = mm2;
1522     _cnt = mm->req();
1523     _idx = Compile::AliasIdxBot-1; // start at the base memory
1524     _mem = nullptr;
1525     _mem2 = nullptr;
1526   }
1527 
1528 #ifdef ASSERT
1529   Node* check_memory() const {
1530     if (at_base_memory())
1531       return _mm->base_memory();
1532     else if ((uint)_idx < _mm->req() && !_mm->in(_idx)->is_top())
1533       return _mm->memory_at(_idx);
1534     else
1535       return _mm_base;
1536   }
1537   Node* check_memory2() const {
1538     return at_base_memory()? _mm2->base_memory(): _mm2->memory_at(_idx);
1539   }
1540 #endif
1541 
1542   static bool match_memory(Node* mem, const MergeMemNode* mm, int idx) PRODUCT_RETURN0;
1543   void assert_synch() const {
1544     assert(!_mem || _idx >= _cnt || match_memory(_mem, _mm, _idx),
1545            "no side-effects except through the stream");
1546   }
1547 
1548  public:
1549 
1550   // expected usages:
1551   // for (MergeMemStream mms(mem->is_MergeMem()); next_non_empty(); ) { ... }
1552   // for (MergeMemStream mms(mem1, mem2); next_non_empty2(); ) { ... }
1553 
1554   // iterate over one merge
1555   MergeMemStream(MergeMemNode* mm) {
1556     mm->iteration_setup();
1557     init(mm);
1558     DEBUG_ONLY(_cnt2 = 999);
1559   }
1560   // iterate in parallel over two merges
1561   // only iterates through non-empty elements of mm2
1562   MergeMemStream(MergeMemNode* mm, const MergeMemNode* mm2) {
1563     assert(mm2, "second argument must be a MergeMem also");
1564     ((MergeMemNode*)mm2)->iteration_setup();  // update hidden state
1565     mm->iteration_setup(mm2);
1566     init(mm, mm2);
1567     _cnt2 = mm2->req();
1568   }
1569 #ifdef ASSERT
1570   ~MergeMemStream() {
1571     assert_synch();
1572   }
1573 #endif
1574 
1575   MergeMemNode* all_memory() const {
1576     return _mm;
1577   }
1578   Node* base_memory() const {
1579     assert(_mm_base == _mm->base_memory(), "no update to base memory, please");
1580     return _mm_base;
1581   }
1582   const MergeMemNode* all_memory2() const {
1583     assert(_mm2 != nullptr, "");
1584     return _mm2;
1585   }
1586   bool at_base_memory() const {
1587     return _idx == Compile::AliasIdxBot;
1588   }
1589   int alias_idx() const {
1590     assert(_mem, "must call next 1st");
1591     return _idx;
1592   }
1593 
1594   const TypePtr* adr_type() const {
1595     return Compile::current()->get_adr_type(alias_idx());
1596   }
1597 
1598   const TypePtr* adr_type(Compile* C) const {
1599     return C->get_adr_type(alias_idx());
1600   }
1601   bool is_empty() const {
1602     assert(_mem, "must call next 1st");
1603     assert(_mem->is_top() == (_mem==_mm->empty_memory()), "correct sentinel");
1604     return _mem->is_top();
1605   }
1606   bool is_empty2() const {
1607     assert(_mem2, "must call next 1st");
1608     assert(_mem2->is_top() == (_mem2==_mm2->empty_memory()), "correct sentinel");
1609     return _mem2->is_top();
1610   }
1611   Node* memory() const {
1612     assert(!is_empty(), "must not be empty");
1613     assert_synch();
1614     return _mem;
1615   }
1616   // get the current memory, regardless of empty or non-empty status
1617   Node* force_memory() const {
1618     assert(!is_empty() || !at_base_memory(), "");
1619     // Use _mm_base to defend against updates to _mem->base_memory().
1620     Node *mem = _mem->is_top() ? _mm_base : _mem;
1621     assert(mem == check_memory(), "");
1622     return mem;
1623   }
1624   Node* memory2() const {
1625     assert(_mem2 == check_memory2(), "");
1626     return _mem2;
1627   }
1628   void set_memory(Node* mem) {
1629     if (at_base_memory()) {
1630       // Note that this does not change the invariant _mm_base.
1631       _mm->set_base_memory(mem);
1632     } else {
1633       _mm->set_memory_at(_idx, mem);
1634     }
1635     _mem = mem;
1636     assert_synch();
1637   }
1638 
1639   // Recover from a side effect to the MergeMemNode.
1640   void set_memory() {
1641     _mem = _mm->in(_idx);
1642   }
1643 
1644   bool next()  { return next(false); }
1645   bool next2() { return next(true); }
1646 
1647   bool next_non_empty()  { return next_non_empty(false); }
1648   bool next_non_empty2() { return next_non_empty(true); }
1649   // next_non_empty2 can yield states where is_empty() is true
1650 
1651  private:
1652   // find the next item, which might be empty
1653   bool next(bool have_mm2) {
1654     assert((_mm2 != nullptr) == have_mm2, "use other next");
1655     assert_synch();
1656     if (++_idx < _cnt) {
1657       // Note:  This iterator allows _mm to be non-sparse.
1658       // It behaves the same whether _mem is top or base_memory.
1659       _mem = _mm->in(_idx);
1660       if (have_mm2)
1661         _mem2 = _mm2->in((_idx < _cnt2) ? _idx : Compile::AliasIdxTop);
1662       return true;
1663     }
1664     return false;
1665   }
1666 
1667   // find the next non-empty item
1668   bool next_non_empty(bool have_mm2) {
1669     while (next(have_mm2)) {
1670       if (!is_empty()) {
1671         // make sure _mem2 is filled in sensibly
1672         if (have_mm2 && _mem2->is_top())  _mem2 = _mm2->base_memory();
1673         return true;
1674       } else if (have_mm2 && !is_empty2()) {
1675         return true;   // is_empty() == true
1676       }
1677     }
1678     return false;
1679   }
1680 };
1681 
1682 // cachewb node for guaranteeing writeback of the cache line at a
1683 // given address to (non-volatile) RAM
1684 class CacheWBNode : public Node {
1685 public:
1686   CacheWBNode(Node *ctrl, Node *mem, Node *addr) : Node(ctrl, mem, addr) {}
1687   virtual int Opcode() const;
1688   virtual uint ideal_reg() const { return NotAMachineReg; }
1689   virtual uint match_edge(uint idx) const { return (idx == 2); }
1690   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1691   virtual const Type *bottom_type() const { return Type::MEMORY; }
1692 
1693 private:
1694   virtual bool depends_only_on_test_impl() const { return false; }
1695 };
1696 
1697 // cachewb pre sync node for ensuring that writebacks are serialised
1698 // relative to preceding or following stores
1699 class CacheWBPreSyncNode : public Node {
1700 public:
1701   CacheWBPreSyncNode(Node *ctrl, Node *mem) : Node(ctrl, mem) {}
1702   virtual int Opcode() const;
1703   virtual uint ideal_reg() const { return NotAMachineReg; }
1704   virtual uint match_edge(uint idx) const { return false; }
1705   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1706   virtual const Type *bottom_type() const { return Type::MEMORY; }
1707 
1708 private:
1709   virtual bool depends_only_on_test_impl() const { return false; }
1710 };
1711 
1712 // cachewb pre sync node for ensuring that writebacks are serialised
1713 // relative to preceding or following stores
1714 class CacheWBPostSyncNode : public Node {
1715 public:
1716   CacheWBPostSyncNode(Node *ctrl, Node *mem) : Node(ctrl, mem) {}
1717   virtual int Opcode() const;
1718   virtual uint ideal_reg() const { return NotAMachineReg; }
1719   virtual uint match_edge(uint idx) const { return false; }
1720   virtual const TypePtr *adr_type() const { return TypePtr::BOTTOM; }
1721   virtual const Type *bottom_type() const { return Type::MEMORY; }
1722 
1723 private:
1724   virtual bool depends_only_on_test_impl() const { return false; }
1725 };
1726 
1727 //------------------------------Prefetch---------------------------------------
1728 
1729 // Allocation prefetch which may fault, TLAB size have to be adjusted.
1730 class PrefetchAllocationNode : public Node {
1731 public:
1732   PrefetchAllocationNode(Node *mem, Node *adr) : Node(nullptr,mem,adr) {}
1733   virtual int Opcode() const;
1734   virtual uint ideal_reg() const { return NotAMachineReg; }
1735   virtual uint match_edge(uint idx) const { return idx==2; }
1736   virtual const Type *bottom_type() const { return ( AllocatePrefetchStyle == 3 ) ? Type::MEMORY : Type::ABIO; }
1737 
1738 private:
1739   virtual bool depends_only_on_test_impl() const { return false; }
1740 };
1741 
1742 #endif // SHARE_OPTO_MEMNODE_HPP