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