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