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