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