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