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