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