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