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