1 /*
   2  * Copyright (c) 1999, 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_C1_C1_INSTRUCTION_HPP
  26 #define SHARE_C1_C1_INSTRUCTION_HPP
  27 
  28 #include "c1/c1_Compilation.hpp"
  29 #include "c1/c1_LIR.hpp"
  30 #include "c1/c1_ValueType.hpp"
  31 #include "ci/ciField.hpp"
  32 
  33 // Predefined classes
  34 class ciField;
  35 class ValueStack;
  36 class InstructionPrinter;
  37 class IRScope;
  38 class LIR_OprDesc;
  39 typedef LIR_OprDesc* LIR_Opr;
  40 
  41 
  42 // Instruction class hierarchy
  43 //
  44 // All leaf classes in the class hierarchy are concrete classes
  45 // (i.e., are instantiated). All other classes are abstract and
  46 // serve factoring.
  47 
  48 class Instruction;
  49 class   Phi;
  50 class   Local;
  51 class   Constant;
  52 class   AccessField;
  53 class     LoadField;
  54 class     StoreField;
  55 class   AccessArray;
  56 class     ArrayLength;
  57 class     AccessIndexed;
  58 class       LoadIndexed;
  59 class       StoreIndexed;
  60 class   NegateOp;
  61 class   Op2;
  62 class     ArithmeticOp;
  63 class     ShiftOp;
  64 class     LogicOp;
  65 class     CompareOp;
  66 class     IfOp;
  67 class   Convert;
  68 class   NullCheck;
  69 class   TypeCast;
  70 class   OsrEntry;
  71 class   ExceptionObject;
  72 class   StateSplit;
  73 class     Invoke;
  74 class     NewInstance;
  75 class     NewValueTypeInstance;
  76 class     NewArray;
  77 class       NewTypeArray;
  78 class       NewObjectArray;
  79 class       NewMultiArray;
  80 class     TypeCheck;
  81 class       CheckCast;
  82 class       InstanceOf;
  83 class     AccessMonitor;
  84 class       MonitorEnter;
  85 class       MonitorExit;
  86 class     Intrinsic;
  87 class     BlockBegin;
  88 class     BlockEnd;
  89 class       Goto;
  90 class       If;
  91 class       IfInstanceOf;
  92 class       Switch;
  93 class         TableSwitch;
  94 class         LookupSwitch;
  95 class       Return;
  96 class       Throw;
  97 class       Base;
  98 class   RoundFP;
  99 class   UnsafeOp;
 100 class     UnsafeRawOp;
 101 class       UnsafeGetRaw;
 102 class       UnsafePutRaw;
 103 class     UnsafeObjectOp;
 104 class       UnsafeGetObject;
 105 class       UnsafePutObject;
 106 class         UnsafeGetAndSetObject;
 107 class   ProfileCall;
 108 class   ProfileReturnType;
 109 class   ProfileInvoke;
 110 class   RuntimeCall;
 111 class   MemBar;
 112 class   RangeCheckPredicate;
 113 #ifdef ASSERT
 114 class   Assert;
 115 #endif
 116 
 117 // A Value is a reference to the instruction creating the value
 118 typedef Instruction* Value;
 119 typedef GrowableArray<Value> Values;
 120 typedef GrowableArray<ValueStack*> ValueStackStack;
 121 
 122 // BlockClosure is the base class for block traversal/iteration.
 123 
 124 class BlockClosure: public CompilationResourceObj {
 125  public:
 126   virtual void block_do(BlockBegin* block)       = 0;
 127 };
 128 
 129 
 130 // A simple closure class for visiting the values of an Instruction
 131 class ValueVisitor: public StackObj {
 132  public:
 133   virtual void visit(Value* v) = 0;
 134 };
 135 
 136 
 137 // Some array and list classes
 138 typedef GrowableArray<BlockBegin*> BlockBeginArray;
 139 
 140 class BlockList: public GrowableArray<BlockBegin*> {
 141  public:
 142   BlockList(): GrowableArray<BlockBegin*>() {}
 143   BlockList(const int size): GrowableArray<BlockBegin*>(size) {}
 144   BlockList(const int size, BlockBegin* init): GrowableArray<BlockBegin*>(size, size, init) {}
 145 
 146   void iterate_forward(BlockClosure* closure);
 147   void iterate_backward(BlockClosure* closure);
 148   void blocks_do(void f(BlockBegin*));
 149   void values_do(ValueVisitor* f);
 150   void print(bool cfg_only = false, bool live_only = false) PRODUCT_RETURN;
 151 };
 152 
 153 
 154 // InstructionVisitors provide type-based dispatch for instructions.
 155 // For each concrete Instruction class X, a virtual function do_X is
 156 // provided. Functionality that needs to be implemented for all classes
 157 // (e.g., printing, code generation) is factored out into a specialised
 158 // visitor instead of added to the Instruction classes itself.
 159 
 160 class InstructionVisitor: public StackObj {
 161  public:
 162   virtual void do_Phi            (Phi*             x) = 0;
 163   virtual void do_Local          (Local*           x) = 0;
 164   virtual void do_Constant       (Constant*        x) = 0;
 165   virtual void do_LoadField      (LoadField*       x) = 0;
 166   virtual void do_StoreField     (StoreField*      x) = 0;
 167   virtual void do_ArrayLength    (ArrayLength*     x) = 0;
 168   virtual void do_LoadIndexed    (LoadIndexed*     x) = 0;
 169   virtual void do_StoreIndexed   (StoreIndexed*    x) = 0;
 170   virtual void do_NegateOp       (NegateOp*        x) = 0;
 171   virtual void do_ArithmeticOp   (ArithmeticOp*    x) = 0;
 172   virtual void do_ShiftOp        (ShiftOp*         x) = 0;
 173   virtual void do_LogicOp        (LogicOp*         x) = 0;
 174   virtual void do_CompareOp      (CompareOp*       x) = 0;
 175   virtual void do_IfOp           (IfOp*            x) = 0;
 176   virtual void do_Convert        (Convert*         x) = 0;
 177   virtual void do_NullCheck      (NullCheck*       x) = 0;
 178   virtual void do_TypeCast       (TypeCast*        x) = 0;
 179   virtual void do_Invoke         (Invoke*          x) = 0;
 180   virtual void do_NewInstance    (NewInstance*     x) = 0;
 181   virtual void do_NewValueTypeInstance(NewValueTypeInstance* x) = 0;
 182   virtual void do_NewTypeArray   (NewTypeArray*    x) = 0;
 183   virtual void do_NewObjectArray (NewObjectArray*  x) = 0;
 184   virtual void do_NewMultiArray  (NewMultiArray*   x) = 0;
 185   virtual void do_CheckCast      (CheckCast*       x) = 0;
 186   virtual void do_InstanceOf     (InstanceOf*      x) = 0;
 187   virtual void do_MonitorEnter   (MonitorEnter*    x) = 0;
 188   virtual void do_MonitorExit    (MonitorExit*     x) = 0;
 189   virtual void do_Intrinsic      (Intrinsic*       x) = 0;
 190   virtual void do_BlockBegin     (BlockBegin*      x) = 0;
 191   virtual void do_Goto           (Goto*            x) = 0;
 192   virtual void do_If             (If*              x) = 0;
 193   virtual void do_IfInstanceOf   (IfInstanceOf*    x) = 0;
 194   virtual void do_TableSwitch    (TableSwitch*     x) = 0;
 195   virtual void do_LookupSwitch   (LookupSwitch*    x) = 0;
 196   virtual void do_Return         (Return*          x) = 0;
 197   virtual void do_Throw          (Throw*           x) = 0;
 198   virtual void do_Base           (Base*            x) = 0;
 199   virtual void do_OsrEntry       (OsrEntry*        x) = 0;
 200   virtual void do_ExceptionObject(ExceptionObject* x) = 0;
 201   virtual void do_RoundFP        (RoundFP*         x) = 0;
 202   virtual void do_UnsafeGetRaw   (UnsafeGetRaw*    x) = 0;
 203   virtual void do_UnsafePutRaw   (UnsafePutRaw*    x) = 0;
 204   virtual void do_UnsafeGetObject(UnsafeGetObject* x) = 0;
 205   virtual void do_UnsafePutObject(UnsafePutObject* x) = 0;
 206   virtual void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x) = 0;
 207   virtual void do_ProfileCall    (ProfileCall*     x) = 0;
 208   virtual void do_ProfileReturnType (ProfileReturnType*  x) = 0;
 209   virtual void do_ProfileInvoke  (ProfileInvoke*   x) = 0;
 210   virtual void do_RuntimeCall    (RuntimeCall*     x) = 0;
 211   virtual void do_MemBar         (MemBar*          x) = 0;
 212   virtual void do_RangeCheckPredicate(RangeCheckPredicate* x) = 0;
 213 #ifdef ASSERT
 214   virtual void do_Assert         (Assert*          x) = 0;
 215 #endif
 216 };
 217 
 218 
 219 // Hashing support
 220 //
 221 // Note: This hash functions affect the performance
 222 //       of ValueMap - make changes carefully!
 223 
 224 #define HASH1(x1            )                    ((intx)(x1))
 225 #define HASH2(x1, x2        )                    ((HASH1(x1        ) << 7) ^ HASH1(x2))
 226 #define HASH3(x1, x2, x3    )                    ((HASH2(x1, x2    ) << 7) ^ HASH1(x3))
 227 #define HASH4(x1, x2, x3, x4)                    ((HASH3(x1, x2, x3) << 7) ^ HASH1(x4))
 228 
 229 
 230 // The following macros are used to implement instruction-specific hashing.
 231 // By default, each instruction implements hash() and is_equal(Value), used
 232 // for value numbering/common subexpression elimination. The default imple-
 233 // mentation disables value numbering. Each instruction which can be value-
 234 // numbered, should define corresponding hash() and is_equal(Value) functions
 235 // via the macros below. The f arguments specify all the values/op codes, etc.
 236 // that need to be identical for two instructions to be identical.
 237 //
 238 // Note: The default implementation of hash() returns 0 in order to indicate
 239 //       that the instruction should not be considered for value numbering.
 240 //       The currently used hash functions do not guarantee that never a 0
 241 //       is produced. While this is still correct, it may be a performance
 242 //       bug (no value numbering for that node). However, this situation is
 243 //       so unlikely, that we are not going to handle it specially.
 244 
 245 #define HASHING1(class_name, enabled, f1)             \
 246   virtual intx hash() const {                         \
 247     return (enabled) ? HASH2(name(), f1) : 0;         \
 248   }                                                   \
 249   virtual bool is_equal(Value v) const {              \
 250     if (!(enabled)  ) return false;                   \
 251     class_name* _v = v->as_##class_name();            \
 252     if (_v == NULL  ) return false;                   \
 253     if (f1 != _v->f1) return false;                   \
 254     return true;                                      \
 255   }                                                   \
 256 
 257 
 258 #define HASHING2(class_name, enabled, f1, f2)         \
 259   virtual intx hash() const {                         \
 260     return (enabled) ? HASH3(name(), f1, f2) : 0;     \
 261   }                                                   \
 262   virtual bool is_equal(Value v) const {              \
 263     if (!(enabled)  ) return false;                   \
 264     class_name* _v = v->as_##class_name();            \
 265     if (_v == NULL  ) return false;                   \
 266     if (f1 != _v->f1) return false;                   \
 267     if (f2 != _v->f2) return false;                   \
 268     return true;                                      \
 269   }                                                   \
 270 
 271 
 272 #define HASHING3(class_name, enabled, f1, f2, f3)     \
 273   virtual intx hash() const {                          \
 274     return (enabled) ? HASH4(name(), f1, f2, f3) : 0; \
 275   }                                                   \
 276   virtual bool is_equal(Value v) const {              \
 277     if (!(enabled)  ) return false;                   \
 278     class_name* _v = v->as_##class_name();            \
 279     if (_v == NULL  ) return false;                   \
 280     if (f1 != _v->f1) return false;                   \
 281     if (f2 != _v->f2) return false;                   \
 282     if (f3 != _v->f3) return false;                   \
 283     return true;                                      \
 284   }                                                   \
 285 
 286 
 287 // The mother of all instructions...
 288 
 289 class Instruction: public CompilationResourceObj {
 290  private:
 291   int          _id;                              // the unique instruction id
 292 #ifndef PRODUCT
 293   int          _printable_bci;                   // the bci of the instruction for printing
 294 #endif
 295   int          _use_count;                       // the number of instructions refering to this value (w/o prev/next); only roots can have use count = 0 or > 1
 296   int          _pin_state;                       // set of PinReason describing the reason for pinning
 297   ValueType*   _type;                            // the instruction value type
 298   Instruction* _next;                            // the next instruction if any (NULL for BlockEnd instructions)
 299   Instruction* _subst;                           // the substitution instruction if any
 300   LIR_Opr      _operand;                         // LIR specific information
 301   unsigned int _flags;                           // Flag bits
 302 
 303   ValueStack*  _state_before;                    // Copy of state with input operands still on stack (or NULL)
 304   ValueStack*  _exception_state;                 // Copy of state for exception handling
 305   XHandlers*   _exception_handlers;              // Flat list of exception handlers covering this instruction
 306 
 307   friend class UseCountComputer;
 308   friend class BlockBegin;
 309 
 310   void update_exception_state(ValueStack* state);
 311 
 312  protected:
 313   BlockBegin*  _block;                           // Block that contains this instruction
 314 
 315   void set_type(ValueType* type) {
 316     assert(type != NULL, "type must exist");
 317     _type = type;
 318   }
 319 
 320   // Helper class to keep track of which arguments need a null check
 321   class ArgsNonNullState {
 322   private:
 323     int _nonnull_state; // mask identifying which args are nonnull
 324   public:
 325     ArgsNonNullState()
 326       : _nonnull_state(AllBits) {}
 327 
 328     // Does argument number i needs a null check?
 329     bool arg_needs_null_check(int i) const {
 330       // No data is kept for arguments starting at position 33 so
 331       // conservatively assume that they need a null check.
 332       if (i >= 0 && i < (int)sizeof(_nonnull_state) * BitsPerByte) {
 333         return is_set_nth_bit(_nonnull_state, i);
 334       }
 335       return true;
 336     }
 337 
 338     // Set whether argument number i needs a null check or not
 339     void set_arg_needs_null_check(int i, bool check) {
 340       if (i >= 0 && i < (int)sizeof(_nonnull_state) * BitsPerByte) {
 341         if (check) {
 342           _nonnull_state |= nth_bit(i);
 343         } else {
 344           _nonnull_state &= ~(nth_bit(i));
 345         }
 346       }
 347     }
 348   };
 349 
 350  public:
 351   void* operator new(size_t size) throw() {
 352     Compilation* c = Compilation::current();
 353     void* res = c->arena()->Amalloc(size);
 354     ((Instruction*)res)->_id = c->get_next_id();
 355     return res;
 356   }
 357 
 358   static const int no_bci = -99;
 359 
 360   enum InstructionFlag {
 361     NeedsNullCheckFlag = 0,
 362     NeverNullFlag,          // For "Q" signatures
 363     CanTrapFlag,
 364     DirectCompareFlag,
 365     IsEliminatedFlag,
 366     IsSafepointFlag,
 367     IsStaticFlag,
 368     IsStrictfpFlag,
 369     NeedsStoreCheckFlag,
 370     NeedsWriteBarrierFlag,
 371     PreservesStateFlag,
 372     TargetIsFinalFlag,
 373     TargetIsLoadedFlag,
 374     TargetIsStrictfpFlag,
 375     UnorderedIsTrueFlag,
 376     NeedsPatchingFlag,
 377     ThrowIncompatibleClassChangeErrorFlag,
 378     InvokeSpecialReceiverCheckFlag,
 379     ProfileMDOFlag,
 380     IsLinkedInBlockFlag,
 381     NeedsRangeCheckFlag,
 382     InWorkListFlag,
 383     DeoptimizeOnException,
 384     InstructionLastFlag
 385   };
 386 
 387  public:
 388   bool check_flag(InstructionFlag id) const      { return (_flags & (1 << id)) != 0;    }
 389   void set_flag(InstructionFlag id, bool f)      { _flags = f ? (_flags | (1 << id)) : (_flags & ~(1 << id)); };
 390 
 391   // 'globally' used condition values
 392   enum Condition {
 393     eql, neq, lss, leq, gtr, geq, aeq, beq
 394   };
 395 
 396   // Instructions may be pinned for many reasons and under certain conditions
 397   // with enough knowledge it's possible to safely unpin them.
 398   enum PinReason {
 399       PinUnknown           = 1 << 0
 400     , PinExplicitNullCheck = 1 << 3
 401     , PinStackForStateSplit= 1 << 12
 402     , PinStateSplitConstructor= 1 << 13
 403     , PinGlobalValueNumbering= 1 << 14
 404   };
 405 
 406   static Condition mirror(Condition cond);
 407   static Condition negate(Condition cond);
 408 
 409   // initialization
 410   static int number_of_instructions() {
 411     return Compilation::current()->number_of_instructions();
 412   }
 413 
 414   // creation
 415   Instruction(ValueType* type, ValueStack* state_before = NULL, bool type_is_constant = false)
 416   :
 417 #ifndef PRODUCT
 418   _printable_bci(-99),
 419 #endif
 420     _use_count(0)
 421   , _pin_state(0)
 422   , _type(type)
 423   , _next(NULL)
 424   , _subst(NULL)
 425   , _operand(LIR_OprFact::illegalOpr)
 426   , _flags(0)
 427   , _state_before(state_before)
 428   , _exception_handlers(NULL)
 429   , _block(NULL)
 430   {
 431     check_state(state_before);
 432     assert(type != NULL && (!type->is_constant() || type_is_constant), "type must exist");
 433     update_exception_state(_state_before);
 434   }
 435 
 436   // accessors
 437   int id() const                                 { return _id; }
 438 #ifndef PRODUCT
 439   bool has_printable_bci() const                 { return _printable_bci != -99; }
 440   int printable_bci() const                      { assert(has_printable_bci(), "_printable_bci should have been set"); return _printable_bci; }
 441   void set_printable_bci(int bci)                { _printable_bci = bci; }
 442 #endif
 443   int dominator_depth();
 444   int use_count() const                          { return _use_count; }
 445   int pin_state() const                          { return _pin_state; }
 446   bool is_pinned() const                         { return _pin_state != 0 || PinAllInstructions; }
 447   ValueType* type() const                        { return _type; }
 448   BlockBegin *block() const                      { return _block; }
 449   Instruction* prev();                           // use carefully, expensive operation
 450   Instruction* next() const                      { return _next; }
 451   bool has_subst() const                         { return _subst != NULL; }
 452   Instruction* subst()                           { return _subst == NULL ? this : _subst->subst(); }
 453   LIR_Opr operand() const                        { return _operand; }
 454 
 455   void set_needs_null_check(bool f)              { set_flag(NeedsNullCheckFlag, f); }
 456   bool needs_null_check() const                  { return check_flag(NeedsNullCheckFlag); }
 457   void set_never_null(bool f)                    { set_flag(NeverNullFlag, f); }
 458   bool is_never_null() const                     { return check_flag(NeverNullFlag); }
 459   bool is_linked() const                         { return check_flag(IsLinkedInBlockFlag); }
 460   bool can_be_linked()                           { return as_Local() == NULL && as_Phi() == NULL; }
 461 
 462   bool has_uses() const                          { return use_count() > 0; }
 463   ValueStack* state_before() const               { return _state_before; }
 464   ValueStack* exception_state() const            { return _exception_state; }
 465   virtual bool needs_exception_state() const     { return true; }
 466   XHandlers* exception_handlers() const          { return _exception_handlers; }
 467   ciKlass* as_loaded_klass_or_null() const;
 468 
 469   // manipulation
 470   void pin(PinReason reason)                     { _pin_state |= reason; }
 471   void pin()                                     { _pin_state |= PinUnknown; }
 472   // DANGEROUS: only used by EliminateStores
 473   void unpin(PinReason reason)                   { assert((reason & PinUnknown) == 0, "can't unpin unknown state"); _pin_state &= ~reason; }
 474 
 475   Instruction* set_next(Instruction* next) {
 476     assert(next->has_printable_bci(), "_printable_bci should have been set");
 477     assert(next != NULL, "must not be NULL");
 478     assert(as_BlockEnd() == NULL, "BlockEnd instructions must have no next");
 479     assert(next->can_be_linked(), "shouldn't link these instructions into list");
 480 
 481     BlockBegin *block = this->block();
 482     next->_block = block;
 483 
 484     next->set_flag(Instruction::IsLinkedInBlockFlag, true);
 485     _next = next;
 486     return next;
 487   }
 488 
 489   Instruction* set_next(Instruction* next, int bci) {
 490 #ifndef PRODUCT
 491     next->set_printable_bci(bci);
 492 #endif
 493     return set_next(next);
 494   }
 495 
 496   // when blocks are merged
 497   void fixup_block_pointers() {
 498     Instruction *cur = next()->next(); // next()'s block is set in set_next
 499     while (cur && cur->_block != block()) {
 500       cur->_block = block();
 501       cur = cur->next();
 502     }
 503   }
 504 
 505   Instruction *insert_after(Instruction *i) {
 506     Instruction* n = _next;
 507     set_next(i);
 508     i->set_next(n);
 509     return _next;
 510   }
 511 
 512   bool is_loaded_flattened_array() const;
 513   bool maybe_flattened_array();
 514   bool maybe_null_free_array();
 515 
 516   Instruction *insert_after_same_bci(Instruction *i) {
 517 #ifndef PRODUCT
 518     i->set_printable_bci(printable_bci());
 519 #endif
 520     return insert_after(i);
 521   }
 522 
 523   void set_subst(Instruction* subst)             {
 524     assert(subst == NULL ||
 525            type()->base() == subst->type()->base() ||
 526            subst->type()->base() == illegalType, "type can't change");
 527     _subst = subst;
 528   }
 529   void set_exception_handlers(XHandlers *xhandlers) { _exception_handlers = xhandlers; }
 530   void set_exception_state(ValueStack* s)        { check_state(s); _exception_state = s; }
 531   void set_state_before(ValueStack* s)           { check_state(s); _state_before = s; }
 532 
 533   // machine-specifics
 534   void set_operand(LIR_Opr operand)              { assert(operand != LIR_OprFact::illegalOpr, "operand must exist"); _operand = operand; }
 535   void clear_operand()                           { _operand = LIR_OprFact::illegalOpr; }
 536 
 537   // generic
 538   virtual Instruction*      as_Instruction()     { return this; } // to satisfy HASHING1 macro
 539   virtual Phi*              as_Phi()             { return NULL; }
 540   virtual Local*            as_Local()           { return NULL; }
 541   virtual Constant*         as_Constant()        { return NULL; }
 542   virtual AccessField*      as_AccessField()     { return NULL; }
 543   virtual LoadField*        as_LoadField()       { return NULL; }
 544   virtual StoreField*       as_StoreField()      { return NULL; }
 545   virtual AccessArray*      as_AccessArray()     { return NULL; }
 546   virtual ArrayLength*      as_ArrayLength()     { return NULL; }
 547   virtual AccessIndexed*    as_AccessIndexed()   { return NULL; }
 548   virtual LoadIndexed*      as_LoadIndexed()     { return NULL; }
 549   virtual StoreIndexed*     as_StoreIndexed()    { return NULL; }
 550   virtual NegateOp*         as_NegateOp()        { return NULL; }
 551   virtual Op2*              as_Op2()             { return NULL; }
 552   virtual ArithmeticOp*     as_ArithmeticOp()    { return NULL; }
 553   virtual ShiftOp*          as_ShiftOp()         { return NULL; }
 554   virtual LogicOp*          as_LogicOp()         { return NULL; }
 555   virtual CompareOp*        as_CompareOp()       { return NULL; }
 556   virtual IfOp*             as_IfOp()            { return NULL; }
 557   virtual Convert*          as_Convert()         { return NULL; }
 558   virtual NullCheck*        as_NullCheck()       { return NULL; }
 559   virtual OsrEntry*         as_OsrEntry()        { return NULL; }
 560   virtual StateSplit*       as_StateSplit()      { return NULL; }
 561   virtual Invoke*           as_Invoke()          { return NULL; }
 562   virtual NewInstance*      as_NewInstance()     { return NULL; }
 563   virtual NewValueTypeInstance* as_NewValueTypeInstance() { return NULL; }
 564   virtual NewArray*         as_NewArray()        { return NULL; }
 565   virtual NewTypeArray*     as_NewTypeArray()    { return NULL; }
 566   virtual NewObjectArray*   as_NewObjectArray()  { return NULL; }
 567   virtual NewMultiArray*    as_NewMultiArray()   { return NULL; }
 568   virtual TypeCheck*        as_TypeCheck()       { return NULL; }
 569   virtual CheckCast*        as_CheckCast()       { return NULL; }
 570   virtual InstanceOf*       as_InstanceOf()      { return NULL; }
 571   virtual TypeCast*         as_TypeCast()        { return NULL; }
 572   virtual AccessMonitor*    as_AccessMonitor()   { return NULL; }
 573   virtual MonitorEnter*     as_MonitorEnter()    { return NULL; }
 574   virtual MonitorExit*      as_MonitorExit()     { return NULL; }
 575   virtual Intrinsic*        as_Intrinsic()       { return NULL; }
 576   virtual BlockBegin*       as_BlockBegin()      { return NULL; }
 577   virtual BlockEnd*         as_BlockEnd()        { return NULL; }
 578   virtual Goto*             as_Goto()            { return NULL; }
 579   virtual If*               as_If()              { return NULL; }
 580   virtual IfInstanceOf*     as_IfInstanceOf()    { return NULL; }
 581   virtual TableSwitch*      as_TableSwitch()     { return NULL; }
 582   virtual LookupSwitch*     as_LookupSwitch()    { return NULL; }
 583   virtual Return*           as_Return()          { return NULL; }
 584   virtual Throw*            as_Throw()           { return NULL; }
 585   virtual Base*             as_Base()            { return NULL; }
 586   virtual RoundFP*          as_RoundFP()         { return NULL; }
 587   virtual ExceptionObject*  as_ExceptionObject() { return NULL; }
 588   virtual UnsafeOp*         as_UnsafeOp()        { return NULL; }
 589   virtual ProfileInvoke*    as_ProfileInvoke()   { return NULL; }
 590   virtual RangeCheckPredicate* as_RangeCheckPredicate() { return NULL; }
 591 
 592 #ifdef ASSERT
 593   virtual Assert*           as_Assert()          { return NULL; }
 594 #endif
 595 
 596   virtual void visit(InstructionVisitor* v)      = 0;
 597 
 598   virtual bool can_trap() const                  { return false; }
 599 
 600   virtual void input_values_do(ValueVisitor* f)   = 0;
 601   virtual void state_values_do(ValueVisitor* f);
 602   virtual void other_values_do(ValueVisitor* f)   { /* usually no other - override on demand */ }
 603           void       values_do(ValueVisitor* f)   { input_values_do(f); state_values_do(f); other_values_do(f); }
 604 
 605   virtual ciType* exact_type() const;
 606   virtual ciType* declared_type() const          { return NULL; }
 607 
 608   // hashing
 609   virtual const char* name() const               = 0;
 610   HASHING1(Instruction, false, id())             // hashing disabled by default
 611 
 612   // debugging
 613   static void check_state(ValueStack* state)     PRODUCT_RETURN;
 614   void print()                                   PRODUCT_RETURN;
 615   void print_line()                              PRODUCT_RETURN;
 616   void print(InstructionPrinter& ip)             PRODUCT_RETURN;
 617 };
 618 
 619 
 620 // The following macros are used to define base (i.e., non-leaf)
 621 // and leaf instruction classes. They define class-name related
 622 // generic functionality in one place.
 623 
 624 #define BASE(class_name, super_class_name)       \
 625   class class_name: public super_class_name {    \
 626    public:                                       \
 627     virtual class_name* as_##class_name()        { return this; }              \
 628 
 629 
 630 #define LEAF(class_name, super_class_name)       \
 631   BASE(class_name, super_class_name)             \
 632    public:                                       \
 633     virtual const char* name() const             { return #class_name; }       \
 634     virtual void visit(InstructionVisitor* v)    { v->do_##class_name(this); } \
 635 
 636 
 637 // Debugging support
 638 
 639 
 640 #ifdef ASSERT
 641 class AssertValues: public ValueVisitor {
 642   void visit(Value* x)             { assert((*x) != NULL, "value must exist"); }
 643 };
 644   #define ASSERT_VALUES                          { AssertValues assert_value; values_do(&assert_value); }
 645 #else
 646   #define ASSERT_VALUES
 647 #endif // ASSERT
 648 
 649 
 650 // A Phi is a phi function in the sense of SSA form. It stands for
 651 // the value of a local variable at the beginning of a join block.
 652 // A Phi consists of n operands, one for every incoming branch.
 653 
 654 LEAF(Phi, Instruction)
 655  private:
 656   int         _pf_flags; // the flags of the phi function
 657   int         _index;    // to value on operand stack (index < 0) or to local
 658  public:
 659   // creation
 660   Phi(ValueType* type, BlockBegin* b, int index)
 661   : Instruction(type->base())
 662   , _pf_flags(0)
 663   , _index(index)
 664   {
 665     _block = b;
 666     NOT_PRODUCT(set_printable_bci(Value(b)->printable_bci()));
 667     if (type->is_illegal()) {
 668       make_illegal();
 669     }
 670   }
 671 
 672   // flags
 673   enum Flag {
 674     no_flag         = 0,
 675     visited         = 1 << 0,
 676     cannot_simplify = 1 << 1
 677   };
 678 
 679   // accessors
 680   bool  is_local() const          { return _index >= 0; }
 681   bool  is_on_stack() const       { return !is_local(); }
 682   int   local_index() const       { assert(is_local(), ""); return _index; }
 683   int   stack_index() const       { assert(is_on_stack(), ""); return -(_index+1); }
 684 
 685   Value operand_at(int i) const;
 686   int   operand_count() const;
 687 
 688   void   set(Flag f)              { _pf_flags |=  f; }
 689   void   clear(Flag f)            { _pf_flags &= ~f; }
 690   bool   is_set(Flag f) const     { return (_pf_flags & f) != 0; }
 691 
 692   // Invalidates phis corresponding to merges of locals of two different types
 693   // (these should never be referenced, otherwise the bytecodes are illegal)
 694   void   make_illegal() {
 695     set(cannot_simplify);
 696     set_type(illegalType);
 697   }
 698 
 699   bool is_illegal() const {
 700     return type()->is_illegal();
 701   }
 702 
 703   // generic
 704   virtual void input_values_do(ValueVisitor* f) {
 705   }
 706 };
 707 
 708 
 709 // A local is a placeholder for an incoming argument to a function call.
 710 LEAF(Local, Instruction)
 711  private:
 712   int      _java_index;                          // the local index within the method to which the local belongs
 713   bool     _is_receiver;                         // if local variable holds the receiver: "this" for non-static methods
 714   ciType*  _declared_type;
 715  public:
 716   // creation
 717   Local(ciType* declared, ValueType* type, int index, bool receiver, bool never_null)
 718     : Instruction(type)
 719     , _java_index(index)
 720     , _is_receiver(receiver)
 721     , _declared_type(declared)
 722   {
 723     set_never_null(never_null);
 724     NOT_PRODUCT(set_printable_bci(-1));
 725   }
 726 
 727   // accessors
 728   int java_index() const                         { return _java_index; }
 729   bool is_receiver() const                       { return _is_receiver; }
 730 
 731   virtual ciType* declared_type() const          { return _declared_type; }
 732 
 733   // generic
 734   virtual void input_values_do(ValueVisitor* f)   { /* no values */ }
 735 };
 736 
 737 
 738 LEAF(Constant, Instruction)
 739  public:
 740   // creation
 741   Constant(ValueType* type):
 742       Instruction(type, NULL, /*type_is_constant*/ true)
 743   {
 744     assert(type->is_constant(), "must be a constant");
 745   }
 746 
 747   Constant(ValueType* type, ValueStack* state_before):
 748     Instruction(type, state_before, /*type_is_constant*/ true)
 749   {
 750     assert(state_before != NULL, "only used for constants which need patching");
 751     assert(type->is_constant(), "must be a constant");
 752     // since it's patching it needs to be pinned
 753     pin();
 754   }
 755 
 756   // generic
 757   virtual bool can_trap() const                  { return state_before() != NULL; }
 758   virtual void input_values_do(ValueVisitor* f)   { /* no values */ }
 759 
 760   virtual intx hash() const;
 761   virtual bool is_equal(Value v) const;
 762 
 763   virtual ciType* exact_type() const;
 764 
 765   enum CompareResult { not_comparable = -1, cond_false, cond_true };
 766 
 767   virtual CompareResult compare(Instruction::Condition condition, Value right) const;
 768   BlockBegin* compare(Instruction::Condition cond, Value right,
 769                       BlockBegin* true_sux, BlockBegin* false_sux) const {
 770     switch (compare(cond, right)) {
 771     case not_comparable:
 772       return NULL;
 773     case cond_false:
 774       return false_sux;
 775     case cond_true:
 776       return true_sux;
 777     default:
 778       ShouldNotReachHere();
 779       return NULL;
 780     }
 781   }
 782 };
 783 
 784 
 785 BASE(AccessField, Instruction)
 786  private:
 787   Value       _obj;
 788   int         _offset;
 789   ciField*    _field;
 790   NullCheck*  _explicit_null_check;              // For explicit null check elimination
 791 
 792  public:
 793   // creation
 794   AccessField(Value obj, int offset, ciField* field, bool is_static,
 795               ValueStack* state_before, bool needs_patching)
 796   : Instruction(as_ValueType(field->type()->basic_type()), state_before)
 797   , _obj(obj)
 798   , _offset(offset)
 799   , _field(field)
 800   , _explicit_null_check(NULL)
 801   {
 802     set_needs_null_check(!is_static);
 803     set_flag(IsStaticFlag, is_static);
 804     set_flag(NeedsPatchingFlag, needs_patching);
 805     ASSERT_VALUES
 806     // pin of all instructions with memory access
 807     pin();
 808   }
 809 
 810   // accessors
 811   Value obj() const                              { return _obj; }
 812   int offset() const                             { return _offset; }
 813   ciField* field() const                         { return _field; }
 814   BasicType field_type() const                   { return _field->type()->basic_type(); }
 815   bool is_static() const                         { return check_flag(IsStaticFlag); }
 816   NullCheck* explicit_null_check() const         { return _explicit_null_check; }
 817   bool needs_patching() const                    { return check_flag(NeedsPatchingFlag); }
 818 
 819   // Unresolved getstatic and putstatic can cause initialization.
 820   // Technically it occurs at the Constant that materializes the base
 821   // of the static fields but it's simpler to model it here.
 822   bool is_init_point() const                     { return is_static() && (needs_patching() || !_field->holder()->is_initialized()); }
 823 
 824   // manipulation
 825 
 826   // Under certain circumstances, if a previous NullCheck instruction
 827   // proved the target object non-null, we can eliminate the explicit
 828   // null check and do an implicit one, simply specifying the debug
 829   // information from the NullCheck. This field should only be consulted
 830   // if needs_null_check() is true.
 831   void set_explicit_null_check(NullCheck* check) { _explicit_null_check = check; }
 832 
 833   // generic
 834   virtual bool can_trap() const                  { return needs_null_check() || needs_patching(); }
 835   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_obj); }
 836 };
 837 
 838 
 839 LEAF(LoadField, AccessField)
 840  public:
 841   // creation
 842   LoadField(Value obj, int offset, ciField* field, bool is_static,
 843             ValueStack* state_before, bool needs_patching,
 844             ciValueKlass* value_klass = NULL, Value default_value = NULL )
 845   : AccessField(obj, offset, field, is_static, state_before, needs_patching)
 846   {}
 847 
 848   ciType* declared_type() const;
 849 
 850   // generic
 851   HASHING2(LoadField, !needs_patching() && !field()->is_volatile(), obj()->subst(), offset())  // cannot be eliminated if needs patching or if volatile
 852 };
 853 
 854 
 855 LEAF(StoreField, AccessField)
 856  private:
 857   Value _value;
 858 
 859  public:
 860   // creation
 861   StoreField(Value obj, int offset, ciField* field, Value value, bool is_static,
 862              ValueStack* state_before, bool needs_patching)
 863   : AccessField(obj, offset, field, is_static, state_before, needs_patching)
 864   , _value(value)
 865   {
 866     set_flag(NeedsWriteBarrierFlag, as_ValueType(field_type())->is_object());
 867     ASSERT_VALUES
 868     pin();
 869   }
 870 
 871   // accessors
 872   Value value() const                            { return _value; }
 873   bool needs_write_barrier() const               { return check_flag(NeedsWriteBarrierFlag); }
 874 
 875   // generic
 876   virtual void input_values_do(ValueVisitor* f)   { AccessField::input_values_do(f); f->visit(&_value); }
 877 };
 878 
 879 
 880 BASE(AccessArray, Instruction)
 881  private:
 882   Value       _array;
 883 
 884  public:
 885   // creation
 886   AccessArray(ValueType* type, Value array, ValueStack* state_before)
 887   : Instruction(type, state_before)
 888   , _array(array)
 889   {
 890     set_needs_null_check(true);
 891     ASSERT_VALUES
 892     pin(); // instruction with side effect (null exception or range check throwing)
 893   }
 894 
 895   Value array() const                            { return _array; }
 896 
 897   // generic
 898   virtual bool can_trap() const                  { return needs_null_check(); }
 899   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_array); }
 900 };
 901 
 902 
 903 LEAF(ArrayLength, AccessArray)
 904  private:
 905   NullCheck*  _explicit_null_check;              // For explicit null check elimination
 906 
 907  public:
 908   // creation
 909   ArrayLength(Value array, ValueStack* state_before)
 910   : AccessArray(intType, array, state_before)
 911   , _explicit_null_check(NULL) {}
 912 
 913   // accessors
 914   NullCheck* explicit_null_check() const         { return _explicit_null_check; }
 915 
 916   // setters
 917   // See LoadField::set_explicit_null_check for documentation
 918   void set_explicit_null_check(NullCheck* check) { _explicit_null_check = check; }
 919 
 920   // generic
 921   HASHING1(ArrayLength, true, array()->subst())
 922 };
 923 
 924 
 925 BASE(AccessIndexed, AccessArray)
 926  private:
 927   Value     _index;
 928   Value     _length;
 929   BasicType _elt_type;
 930   bool      _mismatched;
 931 
 932  public:
 933   // creation
 934   AccessIndexed(Value array, Value index, Value length, BasicType elt_type, ValueStack* state_before, bool mismatched)
 935   : AccessArray(as_ValueType(elt_type), array, state_before)
 936   , _index(index)
 937   , _length(length)
 938   , _elt_type(elt_type)
 939   , _mismatched(mismatched)
 940   {
 941     set_flag(Instruction::NeedsRangeCheckFlag, true);
 942     ASSERT_VALUES
 943   }
 944 
 945   // accessors
 946   Value index() const                            { return _index; }
 947   Value length() const                           { return _length; }
 948   BasicType elt_type() const                     { return _elt_type; }
 949   bool mismatched() const                        { return _mismatched; }
 950 
 951   void clear_length()                            { _length = NULL; }
 952   // perform elimination of range checks involving constants
 953   bool compute_needs_range_check();
 954 
 955   // generic
 956   virtual void input_values_do(ValueVisitor* f)   { AccessArray::input_values_do(f); f->visit(&_index); if (_length != NULL) f->visit(&_length); }
 957 };
 958 
 959 
 960 LEAF(LoadIndexed, AccessIndexed)
 961  private:
 962   NullCheck*  _explicit_null_check;              // For explicit null check elimination
 963   NewValueTypeInstance* _vt;
 964 
 965  public:
 966   // creation
 967   LoadIndexed(Value array, Value index, Value length, BasicType elt_type, ValueStack* state_before, bool mismatched = false)
 968   : AccessIndexed(array, index, length, elt_type, state_before, mismatched)
 969   , _explicit_null_check(NULL), _vt(NULL) {}
 970 
 971   // accessors
 972   NullCheck* explicit_null_check() const         { return _explicit_null_check; }
 973 
 974   // setters
 975   // See LoadField::set_explicit_null_check for documentation
 976   void set_explicit_null_check(NullCheck* check) { _explicit_null_check = check; }
 977 
 978   ciType* exact_type() const;
 979   ciType* declared_type() const;
 980 
 981   NewValueTypeInstance* vt() { return _vt; }
 982   void set_vt(NewValueTypeInstance* vt) { _vt = vt; }
 983 
 984   // generic
 985   HASHING2(LoadIndexed, true, array()->subst(), index()->subst())
 986 };
 987 
 988 
 989 LEAF(StoreIndexed, AccessIndexed)
 990  private:
 991   Value       _value;
 992 
 993   ciMethod* _profiled_method;
 994   int       _profiled_bci;
 995   bool      _check_boolean;
 996 
 997  public:
 998   // creation
 999   StoreIndexed(Value array, Value index, Value length, BasicType elt_type, Value value, ValueStack* state_before,
1000                bool check_boolean, bool mismatched = false)
1001   : AccessIndexed(array, index, length, elt_type, state_before, mismatched)
1002   , _value(value), _profiled_method(NULL), _profiled_bci(0), _check_boolean(check_boolean)
1003   {
1004     set_flag(NeedsWriteBarrierFlag, (as_ValueType(elt_type)->is_object()));
1005     set_flag(NeedsStoreCheckFlag, (as_ValueType(elt_type)->is_object()));
1006     ASSERT_VALUES
1007     pin();
1008   }
1009 
1010   // accessors
1011   Value value() const                            { return _value; }
1012   bool needs_write_barrier() const               { return check_flag(NeedsWriteBarrierFlag); }
1013   bool needs_store_check() const                 { return check_flag(NeedsStoreCheckFlag); }
1014   bool check_boolean() const                     { return _check_boolean; }
1015   // Helpers for MethodData* profiling
1016   void set_should_profile(bool value)                { set_flag(ProfileMDOFlag, value); }
1017   void set_profiled_method(ciMethod* method)         { _profiled_method = method;   }
1018   void set_profiled_bci(int bci)                     { _profiled_bci = bci;         }
1019   bool      should_profile() const                   { return check_flag(ProfileMDOFlag); }
1020   ciMethod* profiled_method() const                  { return _profiled_method;     }
1021   int       profiled_bci() const                     { return _profiled_bci;        }
1022   // Flattened array support
1023   bool is_exact_flattened_array_store() const;
1024   // generic
1025   virtual void input_values_do(ValueVisitor* f)   { AccessIndexed::input_values_do(f); f->visit(&_value); }
1026 };
1027 
1028 
1029 LEAF(NegateOp, Instruction)
1030  private:
1031   Value _x;
1032 
1033  public:
1034   // creation
1035   NegateOp(Value x) : Instruction(x->type()->base()), _x(x) {
1036     ASSERT_VALUES
1037   }
1038 
1039   // accessors
1040   Value x() const                                { return _x; }
1041 
1042   // generic
1043   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_x); }
1044 };
1045 
1046 
1047 BASE(Op2, Instruction)
1048  private:
1049   Bytecodes::Code _op;
1050   Value           _x;
1051   Value           _y;
1052 
1053  public:
1054   // creation
1055   Op2(ValueType* type, Bytecodes::Code op, Value x, Value y, ValueStack* state_before = NULL)
1056   : Instruction(type, state_before)
1057   , _op(op)
1058   , _x(x)
1059   , _y(y)
1060   {
1061     ASSERT_VALUES
1062   }
1063 
1064   // accessors
1065   Bytecodes::Code op() const                     { return _op; }
1066   Value x() const                                { return _x; }
1067   Value y() const                                { return _y; }
1068 
1069   // manipulators
1070   void swap_operands() {
1071     assert(is_commutative(), "operation must be commutative");
1072     Value t = _x; _x = _y; _y = t;
1073   }
1074 
1075   // generic
1076   virtual bool is_commutative() const            { return false; }
1077   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_x); f->visit(&_y); }
1078 };
1079 
1080 
1081 LEAF(ArithmeticOp, Op2)
1082  public:
1083   // creation
1084   ArithmeticOp(Bytecodes::Code op, Value x, Value y, bool is_strictfp, ValueStack* state_before)
1085   : Op2(x->type()->meet(y->type()), op, x, y, state_before)
1086   {
1087     set_flag(IsStrictfpFlag, is_strictfp);
1088     if (can_trap()) pin();
1089   }
1090 
1091   // accessors
1092   bool        is_strictfp() const                { return check_flag(IsStrictfpFlag); }
1093 
1094   // generic
1095   virtual bool is_commutative() const;
1096   virtual bool can_trap() const;
1097   HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1098 };
1099 
1100 
1101 LEAF(ShiftOp, Op2)
1102  public:
1103   // creation
1104   ShiftOp(Bytecodes::Code op, Value x, Value s) : Op2(x->type()->base(), op, x, s) {}
1105 
1106   // generic
1107   HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1108 };
1109 
1110 
1111 LEAF(LogicOp, Op2)
1112  public:
1113   // creation
1114   LogicOp(Bytecodes::Code op, Value x, Value y) : Op2(x->type()->meet(y->type()), op, x, y) {}
1115 
1116   // generic
1117   virtual bool is_commutative() const;
1118   HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1119 };
1120 
1121 
1122 LEAF(CompareOp, Op2)
1123  public:
1124   // creation
1125   CompareOp(Bytecodes::Code op, Value x, Value y, ValueStack* state_before)
1126   : Op2(intType, op, x, y, state_before)
1127   {}
1128 
1129   // generic
1130   HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1131 };
1132 
1133 
1134 LEAF(IfOp, Op2)
1135  private:
1136   Value _tval;
1137   Value _fval;
1138   bool _substitutability_check;
1139 
1140  public:
1141   // creation
1142   IfOp(Value x, Condition cond, Value y, Value tval, Value fval, ValueStack* state_before, bool substitutability_check)
1143   : Op2(tval->type()->meet(fval->type()), (Bytecodes::Code)cond, x, y)
1144   , _tval(tval)
1145   , _fval(fval)
1146   , _substitutability_check(substitutability_check)
1147   {
1148     ASSERT_VALUES
1149     assert(tval->type()->tag() == fval->type()->tag(), "types must match");
1150     set_state_before(state_before);
1151   }
1152 
1153   // accessors
1154   virtual bool is_commutative() const;
1155   Bytecodes::Code op() const                     { ShouldNotCallThis(); return Bytecodes::_illegal; }
1156   Condition cond() const                         { return (Condition)Op2::op(); }
1157   Value tval() const                             { return _tval; }
1158   Value fval() const                             { return _fval; }
1159   bool substitutability_check() const             { return _substitutability_check; }
1160   // generic
1161   virtual void input_values_do(ValueVisitor* f)   { Op2::input_values_do(f); f->visit(&_tval); f->visit(&_fval); }
1162 };
1163 
1164 
1165 LEAF(Convert, Instruction)
1166  private:
1167   Bytecodes::Code _op;
1168   Value           _value;
1169 
1170  public:
1171   // creation
1172   Convert(Bytecodes::Code op, Value value, ValueType* to_type) : Instruction(to_type), _op(op), _value(value) {
1173     ASSERT_VALUES
1174   }
1175 
1176   // accessors
1177   Bytecodes::Code op() const                     { return _op; }
1178   Value value() const                            { return _value; }
1179 
1180   // generic
1181   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_value); }
1182   HASHING2(Convert, true, op(), value()->subst())
1183 };
1184 
1185 
1186 LEAF(NullCheck, Instruction)
1187  private:
1188   Value       _obj;
1189 
1190  public:
1191   // creation
1192   NullCheck(Value obj, ValueStack* state_before)
1193   : Instruction(obj->type()->base(), state_before)
1194   , _obj(obj)
1195   {
1196     ASSERT_VALUES
1197     set_can_trap(true);
1198     assert(_obj->type()->is_object(), "null check must be applied to objects only");
1199     pin(Instruction::PinExplicitNullCheck);
1200   }
1201 
1202   // accessors
1203   Value obj() const                              { return _obj; }
1204 
1205   // setters
1206   void set_can_trap(bool can_trap)               { set_flag(CanTrapFlag, can_trap); }
1207 
1208   // generic
1209   virtual bool can_trap() const                  { return check_flag(CanTrapFlag); /* null-check elimination sets to false */ }
1210   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_obj); }
1211   HASHING1(NullCheck, true, obj()->subst())
1212 };
1213 
1214 
1215 // This node is supposed to cast the type of another node to a more precise
1216 // declared type.
1217 LEAF(TypeCast, Instruction)
1218  private:
1219   ciType* _declared_type;
1220   Value   _obj;
1221 
1222  public:
1223   // The type of this node is the same type as the object type (and it might be constant).
1224   TypeCast(ciType* type, Value obj, ValueStack* state_before)
1225   : Instruction(obj->type(), state_before, obj->type()->is_constant()),
1226     _declared_type(type),
1227     _obj(obj) {}
1228 
1229   // accessors
1230   ciType* declared_type() const                  { return _declared_type; }
1231   Value   obj() const                            { return _obj; }
1232 
1233   // generic
1234   virtual void input_values_do(ValueVisitor* f)  { f->visit(&_obj); }
1235 };
1236 
1237 
1238 BASE(StateSplit, Instruction)
1239  private:
1240   ValueStack* _state;
1241 
1242  protected:
1243   static void substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block);
1244 
1245  public:
1246   // creation
1247   StateSplit(ValueType* type, ValueStack* state_before = NULL)
1248   : Instruction(type, state_before)
1249   , _state(NULL)
1250   {
1251     pin(PinStateSplitConstructor);
1252   }
1253 
1254   // accessors
1255   ValueStack* state() const                      { return _state; }
1256   IRScope* scope() const;                        // the state's scope
1257 
1258   // manipulation
1259   void set_state(ValueStack* state)              { assert(_state == NULL, "overwriting existing state"); check_state(state); _state = state; }
1260 
1261   // generic
1262   virtual void input_values_do(ValueVisitor* f)   { /* no values */ }
1263   virtual void state_values_do(ValueVisitor* f);
1264 };
1265 
1266 
1267 LEAF(Invoke, StateSplit)
1268  private:
1269   Bytecodes::Code _code;
1270   Value           _recv;
1271   Values*         _args;
1272   BasicTypeList*  _signature;
1273   int             _vtable_index;
1274   ciMethod*       _target;
1275 
1276  public:
1277   // creation
1278   Invoke(Bytecodes::Code code, ValueType* result_type, Value recv, Values* args,
1279          int vtable_index, ciMethod* target, ValueStack* state_before, bool never_null);
1280 
1281   // accessors
1282   Bytecodes::Code code() const                   { return _code; }
1283   Value receiver() const                         { return _recv; }
1284   bool has_receiver() const                      { return receiver() != NULL; }
1285   int number_of_arguments() const                { return _args->length(); }
1286   Value argument_at(int i) const                 { return _args->at(i); }
1287   int vtable_index() const                       { return _vtable_index; }
1288   BasicTypeList* signature() const               { return _signature; }
1289   ciMethod* target() const                       { return _target; }
1290 
1291   ciType* declared_type() const;
1292 
1293   // Returns false if target is not loaded
1294   bool target_is_final() const                   { return check_flag(TargetIsFinalFlag); }
1295   bool target_is_loaded() const                  { return check_flag(TargetIsLoadedFlag); }
1296   // Returns false if target is not loaded
1297   bool target_is_strictfp() const                { return check_flag(TargetIsStrictfpFlag); }
1298 
1299   // JSR 292 support
1300   bool is_invokedynamic() const                  { return code() == Bytecodes::_invokedynamic; }
1301   bool is_method_handle_intrinsic() const        { return target()->is_method_handle_intrinsic(); }
1302 
1303   virtual bool needs_exception_state() const     { return false; }
1304 
1305   // generic
1306   virtual bool can_trap() const                  { return true; }
1307   virtual void input_values_do(ValueVisitor* f) {
1308     StateSplit::input_values_do(f);
1309     if (has_receiver()) f->visit(&_recv);
1310     for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
1311   }
1312   virtual void state_values_do(ValueVisitor *f);
1313 };
1314 
1315 
1316 LEAF(NewInstance, StateSplit)
1317  private:
1318   ciInstanceKlass* _klass;
1319   bool _is_unresolved;
1320 
1321  public:
1322   // creation
1323   NewInstance(ciInstanceKlass* klass, ValueStack* state_before, bool is_unresolved)
1324   : StateSplit(instanceType, state_before)
1325   , _klass(klass), _is_unresolved(is_unresolved)
1326   {}
1327 
1328   // accessors
1329   ciInstanceKlass* klass() const                 { return _klass; }
1330   bool is_unresolved() const                     { return _is_unresolved; }
1331 
1332   virtual bool needs_exception_state() const     { return false; }
1333 
1334   // generic
1335   virtual bool can_trap() const                  { return true; }
1336   ciType* exact_type() const;
1337   ciType* declared_type() const;
1338 };
1339 
1340 LEAF(NewValueTypeInstance, StateSplit)
1341   bool _is_unresolved;
1342   ciValueKlass* _klass;
1343   Value _depends_on;      // Link to instance on with withfield was called on
1344 
1345 public:
1346 
1347   // Default creation, always allocated for now
1348   NewValueTypeInstance(ciValueKlass* klass, ValueStack* state_before, bool is_unresolved, Value depends_on = NULL)
1349   : StateSplit(instanceType, state_before)
1350    , _is_unresolved(is_unresolved)
1351    , _klass(klass)
1352   {
1353     if (depends_on == NULL) {
1354       _depends_on = this;
1355     } else {
1356       _depends_on = depends_on;
1357     }
1358     set_never_null(true);
1359   }
1360 
1361   // accessors
1362   bool is_unresolved() const                     { return _is_unresolved; }
1363   Value depends_on();
1364 
1365   ciValueKlass* klass() const { return _klass; }
1366 
1367   virtual bool needs_exception_state() const     { return false; }
1368 
1369   // generic
1370   virtual bool can_trap() const                  { return true; }
1371   ciType* exact_type() const;
1372   ciType* declared_type() const;
1373 
1374   // Only done in LIR Generator -> map everything to object
1375   void set_to_object_type() { set_type(instanceType); }
1376 };
1377 
1378 BASE(NewArray, StateSplit)
1379  private:
1380   Value       _length;
1381 
1382  public:
1383   // creation
1384   NewArray(Value length, ValueStack* state_before)
1385   : StateSplit(objectType, state_before)
1386   , _length(length)
1387   {
1388     // Do not ASSERT_VALUES since length is NULL for NewMultiArray
1389   }
1390 
1391   // accessors
1392   Value length() const                           { return _length; }
1393 
1394   virtual bool needs_exception_state() const     { return false; }
1395 
1396   ciType* exact_type() const                     { return NULL; }
1397   ciType* declared_type() const;
1398 
1399   // generic
1400   virtual bool can_trap() const                  { return true; }
1401   virtual void input_values_do(ValueVisitor* f)   { StateSplit::input_values_do(f); f->visit(&_length); }
1402 };
1403 
1404 
1405 LEAF(NewTypeArray, NewArray)
1406  private:
1407   BasicType _elt_type;
1408 
1409  public:
1410   // creation
1411   NewTypeArray(Value length, BasicType elt_type, ValueStack* state_before)
1412   : NewArray(length, state_before)
1413   , _elt_type(elt_type)
1414   {}
1415 
1416   // accessors
1417   BasicType elt_type() const                     { return _elt_type; }
1418   ciType* exact_type() const;
1419 };
1420 
1421 
1422 LEAF(NewObjectArray, NewArray)
1423  private:
1424   ciKlass* _klass;
1425 
1426  public:
1427   // creation
1428   NewObjectArray(ciKlass* klass, Value length, ValueStack* state_before, bool never_null)
1429   : NewArray(length, state_before), _klass(klass) {
1430     set_never_null(never_null);
1431   }
1432 
1433   // accessors
1434   ciKlass* klass() const                         { return _klass; }
1435   ciType* exact_type() const;
1436 };
1437 
1438 
1439 LEAF(NewMultiArray, NewArray)
1440  private:
1441   ciKlass* _klass;
1442   Values*  _dims;
1443 
1444  public:
1445   // creation
1446   NewMultiArray(ciKlass* klass, Values* dims, ValueStack* state_before) : NewArray(NULL, state_before), _klass(klass), _dims(dims) {
1447     ASSERT_VALUES
1448   }
1449 
1450   // accessors
1451   ciKlass* klass() const                         { return _klass; }
1452   Values* dims() const                           { return _dims; }
1453   int rank() const                               { return dims()->length(); }
1454 
1455   // generic
1456   virtual void input_values_do(ValueVisitor* f) {
1457     // NOTE: we do not call NewArray::input_values_do since "length"
1458     // is meaningless for a multi-dimensional array; passing the
1459     // zeroth element down to NewArray as its length is a bad idea
1460     // since there will be a copy in the "dims" array which doesn't
1461     // get updated, and the value must not be traversed twice. Was bug
1462     // - kbr 4/10/2001
1463     StateSplit::input_values_do(f);
1464     for (int i = 0; i < _dims->length(); i++) f->visit(_dims->adr_at(i));
1465   }
1466 
1467   ciType* exact_type() const;
1468 };
1469 
1470 
1471 BASE(TypeCheck, StateSplit)
1472  private:
1473   ciKlass*    _klass;
1474   Value       _obj;
1475 
1476   ciMethod* _profiled_method;
1477   int       _profiled_bci;
1478 
1479  public:
1480   // creation
1481   TypeCheck(ciKlass* klass, Value obj, ValueType* type, ValueStack* state_before)
1482   : StateSplit(type, state_before), _klass(klass), _obj(obj),
1483     _profiled_method(NULL), _profiled_bci(0) {
1484     ASSERT_VALUES
1485     set_direct_compare(false);
1486   }
1487 
1488   // accessors
1489   ciKlass* klass() const                         { return _klass; }
1490   Value obj() const                              { return _obj; }
1491   bool is_loaded() const                         { return klass() != NULL; }
1492   bool direct_compare() const                    { return check_flag(DirectCompareFlag); }
1493 
1494   // manipulation
1495   void set_direct_compare(bool flag)             { set_flag(DirectCompareFlag, flag); }
1496 
1497   // generic
1498   virtual bool can_trap() const                  { return true; }
1499   virtual void input_values_do(ValueVisitor* f)   { StateSplit::input_values_do(f); f->visit(&_obj); }
1500 
1501   // Helpers for MethodData* profiling
1502   void set_should_profile(bool value)                { set_flag(ProfileMDOFlag, value); }
1503   void set_profiled_method(ciMethod* method)         { _profiled_method = method;   }
1504   void set_profiled_bci(int bci)                     { _profiled_bci = bci;         }
1505   bool      should_profile() const                   { return check_flag(ProfileMDOFlag); }
1506   ciMethod* profiled_method() const                  { return _profiled_method;     }
1507   int       profiled_bci() const                     { return _profiled_bci;        }
1508 };
1509 
1510 
1511 LEAF(CheckCast, TypeCheck)
1512  public:
1513   // creation
1514   CheckCast(ciKlass* klass, Value obj, ValueStack* state_before, bool never_null = false)
1515   : TypeCheck(klass, obj, objectType, state_before) {
1516     set_never_null(never_null);
1517   }
1518 
1519   void set_incompatible_class_change_check() {
1520     set_flag(ThrowIncompatibleClassChangeErrorFlag, true);
1521   }
1522   bool is_incompatible_class_change_check() const {
1523     return check_flag(ThrowIncompatibleClassChangeErrorFlag);
1524   }
1525   void set_invokespecial_receiver_check() {
1526     set_flag(InvokeSpecialReceiverCheckFlag, true);
1527   }
1528   bool is_invokespecial_receiver_check() const {
1529     return check_flag(InvokeSpecialReceiverCheckFlag);
1530   }
1531 
1532   virtual bool needs_exception_state() const {
1533     return !is_invokespecial_receiver_check();
1534   }
1535 
1536   ciType* declared_type() const;
1537 };
1538 
1539 
1540 LEAF(InstanceOf, TypeCheck)
1541  public:
1542   // creation
1543   InstanceOf(ciKlass* klass, Value obj, ValueStack* state_before) : TypeCheck(klass, obj, intType, state_before) {}
1544 
1545   virtual bool needs_exception_state() const     { return false; }
1546 };
1547 
1548 
1549 BASE(AccessMonitor, StateSplit)
1550  private:
1551   Value       _obj;
1552   int         _monitor_no;
1553 
1554  public:
1555   // creation
1556   AccessMonitor(Value obj, int monitor_no, ValueStack* state_before = NULL)
1557   : StateSplit(illegalType, state_before)
1558   , _obj(obj)
1559   , _monitor_no(monitor_no)
1560   {
1561     set_needs_null_check(true);
1562     ASSERT_VALUES
1563   }
1564 
1565   // accessors
1566   Value obj() const                              { return _obj; }
1567   int monitor_no() const                         { return _monitor_no; }
1568 
1569   // generic
1570   virtual void input_values_do(ValueVisitor* f)   { StateSplit::input_values_do(f); f->visit(&_obj); }
1571 };
1572 
1573 
1574 LEAF(MonitorEnter, AccessMonitor)
1575   bool _maybe_valuetype;
1576  public:
1577   // creation
1578   MonitorEnter(Value obj, int monitor_no, ValueStack* state_before, bool maybe_valuetype)
1579   : AccessMonitor(obj, monitor_no, state_before)
1580   , _maybe_valuetype(maybe_valuetype)
1581   {
1582     ASSERT_VALUES
1583   }
1584 
1585   // accessors
1586   bool maybe_valuetype() const                   { return _maybe_valuetype; }
1587 
1588   // generic
1589   virtual bool can_trap() const                  { return true; }
1590 };
1591 
1592 
1593 LEAF(MonitorExit, AccessMonitor)
1594  public:
1595   // creation
1596   MonitorExit(Value obj, int monitor_no)
1597   : AccessMonitor(obj, monitor_no, NULL)
1598   {
1599     ASSERT_VALUES
1600   }
1601 };
1602 
1603 
1604 LEAF(Intrinsic, StateSplit)
1605  private:
1606   vmIntrinsics::ID _id;
1607   Values*          _args;
1608   Value            _recv;
1609   ArgsNonNullState _nonnull_state;
1610 
1611  public:
1612   // preserves_state can be set to true for Intrinsics
1613   // which are guaranteed to preserve register state across any slow
1614   // cases; setting it to true does not mean that the Intrinsic can
1615   // not trap, only that if we continue execution in the same basic
1616   // block after the Intrinsic, all of the registers are intact. This
1617   // allows load elimination and common expression elimination to be
1618   // performed across the Intrinsic.  The default value is false.
1619   Intrinsic(ValueType* type,
1620             vmIntrinsics::ID id,
1621             Values* args,
1622             bool has_receiver,
1623             ValueStack* state_before,
1624             bool preserves_state,
1625             bool cantrap = true)
1626   : StateSplit(type, state_before)
1627   , _id(id)
1628   , _args(args)
1629   , _recv(NULL)
1630   {
1631     assert(args != NULL, "args must exist");
1632     ASSERT_VALUES
1633     set_flag(PreservesStateFlag, preserves_state);
1634     set_flag(CanTrapFlag,        cantrap);
1635     if (has_receiver) {
1636       _recv = argument_at(0);
1637     }
1638     set_needs_null_check(has_receiver);
1639 
1640     // some intrinsics can't trap, so don't force them to be pinned
1641     if (!can_trap() && !vmIntrinsics::should_be_pinned(_id)) {
1642       unpin(PinStateSplitConstructor);
1643     }
1644   }
1645 
1646   // accessors
1647   vmIntrinsics::ID id() const                    { return _id; }
1648   int number_of_arguments() const                { return _args->length(); }
1649   Value argument_at(int i) const                 { return _args->at(i); }
1650 
1651   bool has_receiver() const                      { return (_recv != NULL); }
1652   Value receiver() const                         { assert(has_receiver(), "must have receiver"); return _recv; }
1653   bool preserves_state() const                   { return check_flag(PreservesStateFlag); }
1654 
1655   bool arg_needs_null_check(int i) const {
1656     return _nonnull_state.arg_needs_null_check(i);
1657   }
1658 
1659   void set_arg_needs_null_check(int i, bool check) {
1660     _nonnull_state.set_arg_needs_null_check(i, check);
1661   }
1662 
1663   // generic
1664   virtual bool can_trap() const                  { return check_flag(CanTrapFlag); }
1665   virtual void input_values_do(ValueVisitor* f) {
1666     StateSplit::input_values_do(f);
1667     for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
1668   }
1669 };
1670 
1671 
1672 class LIR_List;
1673 
1674 LEAF(BlockBegin, StateSplit)
1675  private:
1676   int        _block_id;                          // the unique block id
1677   int        _bci;                               // start-bci of block
1678   int        _depth_first_number;                // number of this block in a depth-first ordering
1679   int        _linear_scan_number;                // number of this block in linear-scan ordering
1680   int        _dominator_depth;
1681   int        _loop_depth;                        // the loop nesting level of this block
1682   int        _loop_index;                        // number of the innermost loop of this block
1683   int        _flags;                             // the flags associated with this block
1684 
1685   // fields used by BlockListBuilder
1686   int            _total_preds;                   // number of predecessors found by BlockListBuilder
1687   ResourceBitMap _stores_to_locals;              // bit is set when a local variable is stored in the block
1688 
1689   // SSA specific fields: (factor out later)
1690   BlockList   _successors;                       // the successors of this block
1691   BlockList   _predecessors;                     // the predecessors of this block
1692   BlockList   _dominates;                        // list of blocks that are dominated by this block
1693   BlockBegin* _dominator;                        // the dominator of this block
1694   // SSA specific ends
1695   BlockEnd*  _end;                               // the last instruction of this block
1696   BlockList  _exception_handlers;                // the exception handlers potentially invoked by this block
1697   ValueStackStack* _exception_states;            // only for xhandler entries: states of all instructions that have an edge to this xhandler
1698   int        _exception_handler_pco;             // if this block is the start of an exception handler,
1699                                                  // this records the PC offset in the assembly code of the
1700                                                  // first instruction in this block
1701   Label      _label;                             // the label associated with this block
1702   LIR_List*  _lir;                               // the low level intermediate representation for this block
1703 
1704   ResourceBitMap _live_in;                       // set of live LIR_Opr registers at entry to this block
1705   ResourceBitMap _live_out;                      // set of live LIR_Opr registers at exit from this block
1706   ResourceBitMap _live_gen;                      // set of registers used before any redefinition in this block
1707   ResourceBitMap _live_kill;                     // set of registers defined in this block
1708 
1709   ResourceBitMap _fpu_register_usage;
1710   intArray*      _fpu_stack_state;               // For x86 FPU code generation with UseLinearScan
1711   int            _first_lir_instruction_id;      // ID of first LIR instruction in this block
1712   int            _last_lir_instruction_id;       // ID of last LIR instruction in this block
1713 
1714   void iterate_preorder (boolArray& mark, BlockClosure* closure);
1715   void iterate_postorder(boolArray& mark, BlockClosure* closure);
1716 
1717   friend class SuxAndWeightAdjuster;
1718 
1719  public:
1720    void* operator new(size_t size) throw() {
1721     Compilation* c = Compilation::current();
1722     void* res = c->arena()->Amalloc(size);
1723     ((BlockBegin*)res)->_id = c->get_next_id();
1724     ((BlockBegin*)res)->_block_id = c->get_next_block_id();
1725     return res;
1726   }
1727 
1728   // initialization/counting
1729   static int  number_of_blocks() {
1730     return Compilation::current()->number_of_blocks();
1731   }
1732 
1733   // creation
1734   BlockBegin(int bci)
1735   : StateSplit(illegalType)
1736   , _bci(bci)
1737   , _depth_first_number(-1)
1738   , _linear_scan_number(-1)
1739   , _dominator_depth(-1)
1740   , _loop_depth(0)
1741   , _loop_index(-1)
1742   , _flags(0)
1743   , _total_preds(0)
1744   , _stores_to_locals()
1745   , _successors(2)
1746   , _predecessors(2)
1747   , _dominates(2)
1748   , _dominator(NULL)
1749   , _end(NULL)
1750   , _exception_handlers(1)
1751   , _exception_states(NULL)
1752   , _exception_handler_pco(-1)
1753   , _lir(NULL)
1754   , _live_in()
1755   , _live_out()
1756   , _live_gen()
1757   , _live_kill()
1758   , _fpu_register_usage()
1759   , _fpu_stack_state(NULL)
1760   , _first_lir_instruction_id(-1)
1761   , _last_lir_instruction_id(-1)
1762   {
1763     _block = this;
1764 #ifndef PRODUCT
1765     set_printable_bci(bci);
1766 #endif
1767   }
1768 
1769   // accessors
1770   int block_id() const                           { return _block_id; }
1771   int bci() const                                { return _bci; }
1772   BlockList* successors()                        { return &_successors; }
1773   BlockList* dominates()                         { return &_dominates; }
1774   BlockBegin* dominator() const                  { return _dominator; }
1775   int loop_depth() const                         { return _loop_depth; }
1776   int dominator_depth() const                    { return _dominator_depth; }
1777   int depth_first_number() const                 { return _depth_first_number; }
1778   int linear_scan_number() const                 { return _linear_scan_number; }
1779   BlockEnd* end() const                          { return _end; }
1780   Label* label()                                 { return &_label; }
1781   LIR_List* lir() const                          { return _lir; }
1782   int exception_handler_pco() const              { return _exception_handler_pco; }
1783   ResourceBitMap& live_in()                      { return _live_in;        }
1784   ResourceBitMap& live_out()                     { return _live_out;       }
1785   ResourceBitMap& live_gen()                     { return _live_gen;       }
1786   ResourceBitMap& live_kill()                    { return _live_kill;      }
1787   ResourceBitMap& fpu_register_usage()           { return _fpu_register_usage; }
1788   intArray* fpu_stack_state() const              { return _fpu_stack_state;    }
1789   int first_lir_instruction_id() const           { return _first_lir_instruction_id; }
1790   int last_lir_instruction_id() const            { return _last_lir_instruction_id; }
1791   int total_preds() const                        { return _total_preds; }
1792   BitMap& stores_to_locals()                     { return _stores_to_locals; }
1793 
1794   // manipulation
1795   void set_dominator(BlockBegin* dom)            { _dominator = dom; }
1796   void set_loop_depth(int d)                     { _loop_depth = d; }
1797   void set_dominator_depth(int d)                { _dominator_depth = d; }
1798   void set_depth_first_number(int dfn)           { _depth_first_number = dfn; }
1799   void set_linear_scan_number(int lsn)           { _linear_scan_number = lsn; }
1800   void set_end(BlockEnd* end);
1801   void clear_end();
1802   void disconnect_from_graph();
1803   static void disconnect_edge(BlockBegin* from, BlockBegin* to);
1804   BlockBegin* insert_block_between(BlockBegin* sux);
1805   void substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux);
1806   void set_lir(LIR_List* lir)                    { _lir = lir; }
1807   void set_exception_handler_pco(int pco)        { _exception_handler_pco = pco; }
1808   void set_live_in  (const ResourceBitMap& map)  { _live_in = map;   }
1809   void set_live_out (const ResourceBitMap& map)  { _live_out = map;  }
1810   void set_live_gen (const ResourceBitMap& map)  { _live_gen = map;  }
1811   void set_live_kill(const ResourceBitMap& map)  { _live_kill = map; }
1812   void set_fpu_register_usage(const ResourceBitMap& map) { _fpu_register_usage = map; }
1813   void set_fpu_stack_state(intArray* state)      { _fpu_stack_state = state;  }
1814   void set_first_lir_instruction_id(int id)      { _first_lir_instruction_id = id;  }
1815   void set_last_lir_instruction_id(int id)       { _last_lir_instruction_id = id;  }
1816   void increment_total_preds(int n = 1)          { _total_preds += n; }
1817   void init_stores_to_locals(int locals_count)   { _stores_to_locals.initialize(locals_count); }
1818 
1819   // generic
1820   virtual void state_values_do(ValueVisitor* f);
1821 
1822   // successors and predecessors
1823   int number_of_sux() const;
1824   BlockBegin* sux_at(int i) const;
1825   void add_successor(BlockBegin* sux);
1826   void remove_successor(BlockBegin* pred);
1827   bool is_successor(BlockBegin* sux) const       { return _successors.contains(sux); }
1828 
1829   void add_predecessor(BlockBegin* pred);
1830   void remove_predecessor(BlockBegin* pred);
1831   bool is_predecessor(BlockBegin* pred) const    { return _predecessors.contains(pred); }
1832   int number_of_preds() const                    { return _predecessors.length(); }
1833   BlockBegin* pred_at(int i) const               { return _predecessors.at(i); }
1834 
1835   // exception handlers potentially invoked by this block
1836   void add_exception_handler(BlockBegin* b);
1837   bool is_exception_handler(BlockBegin* b) const { return _exception_handlers.contains(b); }
1838   int  number_of_exception_handlers() const      { return _exception_handlers.length(); }
1839   BlockBegin* exception_handler_at(int i) const  { return _exception_handlers.at(i); }
1840 
1841   // states of the instructions that have an edge to this exception handler
1842   int number_of_exception_states()               { assert(is_set(exception_entry_flag), "only for xhandlers"); return _exception_states == NULL ? 0 : _exception_states->length(); }
1843   ValueStack* exception_state_at(int idx) const  { assert(is_set(exception_entry_flag), "only for xhandlers"); return _exception_states->at(idx); }
1844   int add_exception_state(ValueStack* state);
1845 
1846   // flags
1847   enum Flag {
1848     no_flag                       = 0,
1849     std_entry_flag                = 1 << 0,
1850     osr_entry_flag                = 1 << 1,
1851     exception_entry_flag          = 1 << 2,
1852     subroutine_entry_flag         = 1 << 3,
1853     backward_branch_target_flag   = 1 << 4,
1854     is_on_work_list_flag          = 1 << 5,
1855     was_visited_flag              = 1 << 6,
1856     parser_loop_header_flag       = 1 << 7,  // set by parser to identify blocks where phi functions can not be created on demand
1857     critical_edge_split_flag      = 1 << 8, // set for all blocks that are introduced when critical edges are split
1858     linear_scan_loop_header_flag  = 1 << 9, // set during loop-detection for LinearScan
1859     linear_scan_loop_end_flag     = 1 << 10, // set during loop-detection for LinearScan
1860     donot_eliminate_range_checks  = 1 << 11  // Should be try to eliminate range checks in this block
1861   };
1862 
1863   void set(Flag f)                               { _flags |= f; }
1864   void clear(Flag f)                             { _flags &= ~f; }
1865   bool is_set(Flag f) const                      { return (_flags & f) != 0; }
1866   bool is_entry_block() const {
1867     const int entry_mask = std_entry_flag | osr_entry_flag | exception_entry_flag;
1868     return (_flags & entry_mask) != 0;
1869   }
1870 
1871   // iteration
1872   void iterate_preorder   (BlockClosure* closure);
1873   void iterate_postorder  (BlockClosure* closure);
1874 
1875   void block_values_do(ValueVisitor* f);
1876 
1877   // loops
1878   void set_loop_index(int ix)                    { _loop_index = ix;        }
1879   int  loop_index() const                        { return _loop_index;      }
1880 
1881   // merging
1882   bool try_merge(ValueStack* state);             // try to merge states at block begin
1883   void merge(ValueStack* state)                  { bool b = try_merge(state); assert(b, "merge failed"); }
1884 
1885   // debugging
1886   void print_block()                             PRODUCT_RETURN;
1887   void print_block(InstructionPrinter& ip, bool live_only = false) PRODUCT_RETURN;
1888 };
1889 
1890 
1891 BASE(BlockEnd, StateSplit)
1892  private:
1893   BlockList*  _sux;
1894 
1895  protected:
1896   BlockList* sux() const                         { return _sux; }
1897 
1898   void set_sux(BlockList* sux) {
1899 #ifdef ASSERT
1900     assert(sux != NULL, "sux must exist");
1901     for (int i = sux->length() - 1; i >= 0; i--) assert(sux->at(i) != NULL, "sux must exist");
1902 #endif
1903     _sux = sux;
1904   }
1905 
1906  public:
1907   // creation
1908   BlockEnd(ValueType* type, ValueStack* state_before, bool is_safepoint)
1909   : StateSplit(type, state_before)
1910   , _sux(NULL)
1911   {
1912     set_flag(IsSafepointFlag, is_safepoint);
1913   }
1914 
1915   // accessors
1916   bool is_safepoint() const                      { return check_flag(IsSafepointFlag); }
1917   // For compatibility with old code, for new code use block()
1918   BlockBegin* begin() const                      { return _block; }
1919 
1920   // manipulation
1921   void set_begin(BlockBegin* begin);
1922 
1923   // successors
1924   int number_of_sux() const                      { return _sux != NULL ? _sux->length() : 0; }
1925   BlockBegin* sux_at(int i) const                { return _sux->at(i); }
1926   BlockBegin* default_sux() const                { return sux_at(number_of_sux() - 1); }
1927   BlockBegin** addr_sux_at(int i) const          { return _sux->adr_at(i); }
1928   int sux_index(BlockBegin* sux) const           { return _sux->find(sux); }
1929   void substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux);
1930 };
1931 
1932 
1933 LEAF(Goto, BlockEnd)
1934  public:
1935   enum Direction {
1936     none,            // Just a regular goto
1937     taken, not_taken // Goto produced from If
1938   };
1939  private:
1940   ciMethod*   _profiled_method;
1941   int         _profiled_bci;
1942   Direction   _direction;
1943  public:
1944   // creation
1945   Goto(BlockBegin* sux, ValueStack* state_before, bool is_safepoint = false)
1946     : BlockEnd(illegalType, state_before, is_safepoint)
1947     , _profiled_method(NULL)
1948     , _profiled_bci(0)
1949     , _direction(none) {
1950     BlockList* s = new BlockList(1);
1951     s->append(sux);
1952     set_sux(s);
1953   }
1954 
1955   Goto(BlockBegin* sux, bool is_safepoint) : BlockEnd(illegalType, NULL, is_safepoint)
1956                                            , _profiled_method(NULL)
1957                                            , _profiled_bci(0)
1958                                            , _direction(none) {
1959     BlockList* s = new BlockList(1);
1960     s->append(sux);
1961     set_sux(s);
1962   }
1963 
1964   bool should_profile() const                    { return check_flag(ProfileMDOFlag); }
1965   ciMethod* profiled_method() const              { return _profiled_method; } // set only for profiled branches
1966   int profiled_bci() const                       { return _profiled_bci; }
1967   Direction direction() const                    { return _direction; }
1968 
1969   void set_should_profile(bool value)            { set_flag(ProfileMDOFlag, value); }
1970   void set_profiled_method(ciMethod* method)     { _profiled_method = method; }
1971   void set_profiled_bci(int bci)                 { _profiled_bci = bci; }
1972   void set_direction(Direction d)                { _direction = d; }
1973 };
1974 
1975 #ifdef ASSERT
1976 LEAF(Assert, Instruction)
1977   private:
1978   Value       _x;
1979   Condition   _cond;
1980   Value       _y;
1981   char        *_message;
1982 
1983  public:
1984   // creation
1985   // unordered_is_true is valid for float/double compares only
1986    Assert(Value x, Condition cond, bool unordered_is_true, Value y);
1987 
1988   // accessors
1989   Value x() const                                { return _x; }
1990   Condition cond() const                         { return _cond; }
1991   bool unordered_is_true() const                 { return check_flag(UnorderedIsTrueFlag); }
1992   Value y() const                                { return _y; }
1993   const char *message() const                    { return _message; }
1994 
1995   // generic
1996   virtual void input_values_do(ValueVisitor* f)  { f->visit(&_x); f->visit(&_y); }
1997 };
1998 #endif
1999 
2000 LEAF(RangeCheckPredicate, StateSplit)
2001  private:
2002   Value       _x;
2003   Condition   _cond;
2004   Value       _y;
2005 
2006   void check_state();
2007 
2008  public:
2009   // creation
2010   // unordered_is_true is valid for float/double compares only
2011    RangeCheckPredicate(Value x, Condition cond, bool unordered_is_true, Value y, ValueStack* state) : StateSplit(illegalType)
2012   , _x(x)
2013   , _cond(cond)
2014   , _y(y)
2015   {
2016     ASSERT_VALUES
2017     set_flag(UnorderedIsTrueFlag, unordered_is_true);
2018     assert(x->type()->tag() == y->type()->tag(), "types must match");
2019     this->set_state(state);
2020     check_state();
2021   }
2022 
2023   // Always deoptimize
2024   RangeCheckPredicate(ValueStack* state) : StateSplit(illegalType)
2025   {
2026     this->set_state(state);
2027     _x = _y = NULL;
2028     check_state();
2029   }
2030 
2031   // accessors
2032   Value x() const                                { return _x; }
2033   Condition cond() const                         { return _cond; }
2034   bool unordered_is_true() const                 { return check_flag(UnorderedIsTrueFlag); }
2035   Value y() const                                { return _y; }
2036 
2037   void always_fail()                             { _x = _y = NULL; }
2038 
2039   // generic
2040   virtual void input_values_do(ValueVisitor* f)  { StateSplit::input_values_do(f); f->visit(&_x); f->visit(&_y); }
2041   HASHING3(RangeCheckPredicate, true, x()->subst(), y()->subst(), cond())
2042 };
2043 
2044 LEAF(If, BlockEnd)
2045  private:
2046   Value       _x;
2047   Condition   _cond;
2048   Value       _y;
2049   ciMethod*   _profiled_method;
2050   int         _profiled_bci; // Canonicalizer may alter bci of If node
2051   bool        _swapped;      // Is the order reversed with respect to the original If in the
2052                              // bytecode stream?
2053   bool        _substitutability_check;
2054  public:
2055   // creation
2056   // unordered_is_true is valid for float/double compares only
2057   If(Value x, Condition cond, bool unordered_is_true, Value y, BlockBegin* tsux, BlockBegin* fsux, ValueStack* state_before, bool is_safepoint, bool substitutability_check=false)
2058     : BlockEnd(illegalType, state_before, is_safepoint)
2059   , _x(x)
2060   , _cond(cond)
2061   , _y(y)
2062   , _profiled_method(NULL)
2063   , _profiled_bci(0)
2064   , _swapped(false)
2065   , _substitutability_check(substitutability_check)
2066   {
2067     ASSERT_VALUES
2068     set_flag(UnorderedIsTrueFlag, unordered_is_true);
2069     assert(x->type()->tag() == y->type()->tag(), "types must match");
2070     BlockList* s = new BlockList(2);
2071     s->append(tsux);
2072     s->append(fsux);
2073     set_sux(s);
2074   }
2075 
2076   // accessors
2077   Value x() const                                { return _x; }
2078   Condition cond() const                         { return _cond; }
2079   bool unordered_is_true() const                 { return check_flag(UnorderedIsTrueFlag); }
2080   Value y() const                                { return _y; }
2081   BlockBegin* sux_for(bool is_true) const        { return sux_at(is_true ? 0 : 1); }
2082   BlockBegin* tsux() const                       { return sux_for(true); }
2083   BlockBegin* fsux() const                       { return sux_for(false); }
2084   BlockBegin* usux() const                       { return sux_for(unordered_is_true()); }
2085   bool should_profile() const                    { return check_flag(ProfileMDOFlag); }
2086   ciMethod* profiled_method() const              { return _profiled_method; } // set only for profiled branches
2087   int profiled_bci() const                       { return _profiled_bci; }    // set for profiled branches and tiered
2088   bool is_swapped() const                        { return _swapped; }
2089 
2090   // manipulation
2091   void swap_operands() {
2092     Value t = _x; _x = _y; _y = t;
2093     _cond = mirror(_cond);
2094   }
2095 
2096   void swap_sux() {
2097     assert(number_of_sux() == 2, "wrong number of successors");
2098     BlockList* s = sux();
2099     BlockBegin* t = s->at(0); s->at_put(0, s->at(1)); s->at_put(1, t);
2100     _cond = negate(_cond);
2101     set_flag(UnorderedIsTrueFlag, !check_flag(UnorderedIsTrueFlag));
2102   }
2103 
2104   void set_should_profile(bool value)             { set_flag(ProfileMDOFlag, value); }
2105   void set_profiled_method(ciMethod* method)      { _profiled_method = method; }
2106   void set_profiled_bci(int bci)                  { _profiled_bci = bci;       }
2107   void set_swapped(bool value)                    { _swapped = value;         }
2108   bool substitutability_check() const              { return _substitutability_check; }
2109   // generic
2110   virtual void input_values_do(ValueVisitor* f)   { BlockEnd::input_values_do(f); f->visit(&_x); f->visit(&_y); }
2111 };
2112 
2113 
2114 LEAF(IfInstanceOf, BlockEnd)
2115  private:
2116   ciKlass* _klass;
2117   Value    _obj;
2118   bool     _test_is_instance;                    // jump if instance
2119   int      _instanceof_bci;
2120 
2121  public:
2122   IfInstanceOf(ciKlass* klass, Value obj, bool test_is_instance, int instanceof_bci, BlockBegin* tsux, BlockBegin* fsux)
2123   : BlockEnd(illegalType, NULL, false) // temporary set to false
2124   , _klass(klass)
2125   , _obj(obj)
2126   , _test_is_instance(test_is_instance)
2127   , _instanceof_bci(instanceof_bci)
2128   {
2129     ASSERT_VALUES
2130     assert(instanceof_bci >= 0, "illegal bci");
2131     BlockList* s = new BlockList(2);
2132     s->append(tsux);
2133     s->append(fsux);
2134     set_sux(s);
2135   }
2136 
2137   // accessors
2138   //
2139   // Note 1: If test_is_instance() is true, IfInstanceOf tests if obj *is* an
2140   //         instance of klass; otherwise it tests if it is *not* and instance
2141   //         of klass.
2142   //
2143   // Note 2: IfInstanceOf instructions are created by combining an InstanceOf
2144   //         and an If instruction. The IfInstanceOf bci() corresponds to the
2145   //         bci that the If would have had; the (this->) instanceof_bci() is
2146   //         the bci of the original InstanceOf instruction.
2147   ciKlass* klass() const                         { return _klass; }
2148   Value obj() const                              { return _obj; }
2149   int instanceof_bci() const                     { return _instanceof_bci; }
2150   bool test_is_instance() const                  { return _test_is_instance; }
2151   BlockBegin* sux_for(bool is_true) const        { return sux_at(is_true ? 0 : 1); }
2152   BlockBegin* tsux() const                       { return sux_for(true); }
2153   BlockBegin* fsux() const                       { return sux_for(false); }
2154 
2155   // manipulation
2156   void swap_sux() {
2157     assert(number_of_sux() == 2, "wrong number of successors");
2158     BlockList* s = sux();
2159     BlockBegin* t = s->at(0); s->at_put(0, s->at(1)); s->at_put(1, t);
2160     _test_is_instance = !_test_is_instance;
2161   }
2162 
2163   // generic
2164   virtual void input_values_do(ValueVisitor* f)   { BlockEnd::input_values_do(f); f->visit(&_obj); }
2165 };
2166 
2167 
2168 BASE(Switch, BlockEnd)
2169  private:
2170   Value       _tag;
2171 
2172  public:
2173   // creation
2174   Switch(Value tag, BlockList* sux, ValueStack* state_before, bool is_safepoint)
2175   : BlockEnd(illegalType, state_before, is_safepoint)
2176   , _tag(tag) {
2177     ASSERT_VALUES
2178     set_sux(sux);
2179   }
2180 
2181   // accessors
2182   Value tag() const                              { return _tag; }
2183   int length() const                             { return number_of_sux() - 1; }
2184 
2185   virtual bool needs_exception_state() const     { return false; }
2186 
2187   // generic
2188   virtual void input_values_do(ValueVisitor* f)   { BlockEnd::input_values_do(f); f->visit(&_tag); }
2189 };
2190 
2191 
2192 LEAF(TableSwitch, Switch)
2193  private:
2194   int _lo_key;
2195 
2196  public:
2197   // creation
2198   TableSwitch(Value tag, BlockList* sux, int lo_key, ValueStack* state_before, bool is_safepoint)
2199     : Switch(tag, sux, state_before, is_safepoint)
2200   , _lo_key(lo_key) { assert(_lo_key <= hi_key(), "integer overflow"); }
2201 
2202   // accessors
2203   int lo_key() const                             { return _lo_key; }
2204   int hi_key() const                             { return _lo_key + (length() - 1); }
2205 };
2206 
2207 
2208 LEAF(LookupSwitch, Switch)
2209  private:
2210   intArray* _keys;
2211 
2212  public:
2213   // creation
2214   LookupSwitch(Value tag, BlockList* sux, intArray* keys, ValueStack* state_before, bool is_safepoint)
2215   : Switch(tag, sux, state_before, is_safepoint)
2216   , _keys(keys) {
2217     assert(keys != NULL, "keys must exist");
2218     assert(keys->length() == length(), "sux & keys have incompatible lengths");
2219   }
2220 
2221   // accessors
2222   int key_at(int i) const                        { return _keys->at(i); }
2223 };
2224 
2225 
2226 LEAF(Return, BlockEnd)
2227  private:
2228   Value _result;
2229 
2230  public:
2231   // creation
2232   Return(Value result) :
2233     BlockEnd(result == NULL ? voidType : result->type()->base(), NULL, true),
2234     _result(result) {}
2235 
2236   // accessors
2237   Value result() const                           { return _result; }
2238   bool has_result() const                        { return result() != NULL; }
2239 
2240   // generic
2241   virtual void input_values_do(ValueVisitor* f) {
2242     BlockEnd::input_values_do(f);
2243     if (has_result()) f->visit(&_result);
2244   }
2245 };
2246 
2247 
2248 LEAF(Throw, BlockEnd)
2249  private:
2250   Value _exception;
2251 
2252  public:
2253   // creation
2254   Throw(Value exception, ValueStack* state_before) : BlockEnd(illegalType, state_before, true), _exception(exception) {
2255     ASSERT_VALUES
2256   }
2257 
2258   // accessors
2259   Value exception() const                        { return _exception; }
2260 
2261   // generic
2262   virtual bool can_trap() const                  { return true; }
2263   virtual void input_values_do(ValueVisitor* f)   { BlockEnd::input_values_do(f); f->visit(&_exception); }
2264 };
2265 
2266 
2267 LEAF(Base, BlockEnd)
2268  public:
2269   // creation
2270   Base(BlockBegin* std_entry, BlockBegin* osr_entry) : BlockEnd(illegalType, NULL, false) {
2271     assert(std_entry->is_set(BlockBegin::std_entry_flag), "std entry must be flagged");
2272     assert(osr_entry == NULL || osr_entry->is_set(BlockBegin::osr_entry_flag), "osr entry must be flagged");
2273     BlockList* s = new BlockList(2);
2274     if (osr_entry != NULL) s->append(osr_entry);
2275     s->append(std_entry); // must be default sux!
2276     set_sux(s);
2277   }
2278 
2279   // accessors
2280   BlockBegin* std_entry() const                  { return default_sux(); }
2281   BlockBegin* osr_entry() const                  { return number_of_sux() < 2 ? NULL : sux_at(0); }
2282 };
2283 
2284 
2285 LEAF(OsrEntry, Instruction)
2286  public:
2287   // creation
2288 #ifdef _LP64
2289   OsrEntry() : Instruction(longType) { pin(); }
2290 #else
2291   OsrEntry() : Instruction(intType)  { pin(); }
2292 #endif
2293 
2294   // generic
2295   virtual void input_values_do(ValueVisitor* f)   { }
2296 };
2297 
2298 
2299 // Models the incoming exception at a catch site
2300 LEAF(ExceptionObject, Instruction)
2301  public:
2302   // creation
2303   ExceptionObject() : Instruction(objectType) {
2304     pin();
2305   }
2306 
2307   // generic
2308   virtual void input_values_do(ValueVisitor* f)   { }
2309 };
2310 
2311 
2312 // Models needed rounding for floating-point values on Intel.
2313 // Currently only used to represent rounding of double-precision
2314 // values stored into local variables, but could be used to model
2315 // intermediate rounding of single-precision values as well.
2316 LEAF(RoundFP, Instruction)
2317  private:
2318   Value _input;             // floating-point value to be rounded
2319 
2320  public:
2321   RoundFP(Value input)
2322   : Instruction(input->type()) // Note: should not be used for constants
2323   , _input(input)
2324   {
2325     ASSERT_VALUES
2326   }
2327 
2328   // accessors
2329   Value input() const                            { return _input; }
2330 
2331   // generic
2332   virtual void input_values_do(ValueVisitor* f)   { f->visit(&_input); }
2333 };
2334 
2335 
2336 BASE(UnsafeOp, Instruction)
2337  private:
2338   BasicType _basic_type;    // ValueType can not express byte-sized integers
2339 
2340  protected:
2341   // creation
2342   UnsafeOp(BasicType basic_type, bool is_put)
2343   : Instruction(is_put ? voidType : as_ValueType(basic_type))
2344   , _basic_type(basic_type)
2345   {
2346     //Note:  Unsafe ops are not not guaranteed to throw NPE.
2347     // Convservatively, Unsafe operations must be pinned though we could be
2348     // looser about this if we wanted to..
2349     pin();
2350   }
2351 
2352  public:
2353   // accessors
2354   BasicType basic_type()                         { return _basic_type; }
2355 
2356   // generic
2357   virtual void input_values_do(ValueVisitor* f)   { }
2358 };
2359 
2360 
2361 BASE(UnsafeRawOp, UnsafeOp)
2362  private:
2363   Value _base;                                   // Base address (a Java long)
2364   Value _index;                                  // Index if computed by optimizer; initialized to NULL
2365   int   _log2_scale;                             // Scale factor: 0, 1, 2, or 3.
2366                                                  // Indicates log2 of number of bytes (1, 2, 4, or 8)
2367                                                  // to scale index by.
2368 
2369  protected:
2370   UnsafeRawOp(BasicType basic_type, Value addr, bool is_put)
2371   : UnsafeOp(basic_type, is_put)
2372   , _base(addr)
2373   , _index(NULL)
2374   , _log2_scale(0)
2375   {
2376     // Can not use ASSERT_VALUES because index may be NULL
2377     assert(addr != NULL && addr->type()->is_long(), "just checking");
2378   }
2379 
2380   UnsafeRawOp(BasicType basic_type, Value base, Value index, int log2_scale, bool is_put)
2381   : UnsafeOp(basic_type, is_put)
2382   , _base(base)
2383   , _index(index)
2384   , _log2_scale(log2_scale)
2385   {
2386   }
2387 
2388  public:
2389   // accessors
2390   Value base()                                   { return _base; }
2391   Value index()                                  { return _index; }
2392   bool  has_index()                              { return (_index != NULL); }
2393   int   log2_scale()                             { return _log2_scale; }
2394 
2395   // setters
2396   void set_base (Value base)                     { _base  = base; }
2397   void set_index(Value index)                    { _index = index; }
2398   void set_log2_scale(int log2_scale)            { _log2_scale = log2_scale; }
2399 
2400   // generic
2401   virtual void input_values_do(ValueVisitor* f)   { UnsafeOp::input_values_do(f);
2402                                                    f->visit(&_base);
2403                                                    if (has_index()) f->visit(&_index); }
2404 };
2405 
2406 
2407 LEAF(UnsafeGetRaw, UnsafeRawOp)
2408  private:
2409  bool _may_be_unaligned, _is_wide;  // For OSREntry
2410 
2411  public:
2412  UnsafeGetRaw(BasicType basic_type, Value addr, bool may_be_unaligned, bool is_wide = false)
2413   : UnsafeRawOp(basic_type, addr, false) {
2414     _may_be_unaligned = may_be_unaligned;
2415     _is_wide = is_wide;
2416   }
2417 
2418  UnsafeGetRaw(BasicType basic_type, Value base, Value index, int log2_scale, bool may_be_unaligned, bool is_wide = false)
2419   : UnsafeRawOp(basic_type, base, index, log2_scale, false) {
2420     _may_be_unaligned = may_be_unaligned;
2421     _is_wide = is_wide;
2422   }
2423 
2424   bool may_be_unaligned()                         { return _may_be_unaligned; }
2425   bool is_wide()                                  { return _is_wide; }
2426 };
2427 
2428 
2429 LEAF(UnsafePutRaw, UnsafeRawOp)
2430  private:
2431   Value _value;                                  // Value to be stored
2432 
2433  public:
2434   UnsafePutRaw(BasicType basic_type, Value addr, Value value)
2435   : UnsafeRawOp(basic_type, addr, true)
2436   , _value(value)
2437   {
2438     assert(value != NULL, "just checking");
2439     ASSERT_VALUES
2440   }
2441 
2442   UnsafePutRaw(BasicType basic_type, Value base, Value index, int log2_scale, Value value)
2443   : UnsafeRawOp(basic_type, base, index, log2_scale, true)
2444   , _value(value)
2445   {
2446     assert(value != NULL, "just checking");
2447     ASSERT_VALUES
2448   }
2449 
2450   // accessors
2451   Value value()                                  { return _value; }
2452 
2453   // generic
2454   virtual void input_values_do(ValueVisitor* f)   { UnsafeRawOp::input_values_do(f);
2455                                                    f->visit(&_value); }
2456 };
2457 
2458 
2459 BASE(UnsafeObjectOp, UnsafeOp)
2460  private:
2461   Value _object;                                 // Object to be fetched from or mutated
2462   Value _offset;                                 // Offset within object
2463   bool  _is_volatile;                            // true if volatile - dl/JSR166
2464  public:
2465   UnsafeObjectOp(BasicType basic_type, Value object, Value offset, bool is_put, bool is_volatile)
2466     : UnsafeOp(basic_type, is_put), _object(object), _offset(offset), _is_volatile(is_volatile)
2467   {
2468   }
2469 
2470   // accessors
2471   Value object()                                 { return _object; }
2472   Value offset()                                 { return _offset; }
2473   bool  is_volatile()                            { return _is_volatile; }
2474   // generic
2475   virtual void input_values_do(ValueVisitor* f)   { UnsafeOp::input_values_do(f);
2476                                                    f->visit(&_object);
2477                                                    f->visit(&_offset); }
2478 };
2479 
2480 
2481 LEAF(UnsafeGetObject, UnsafeObjectOp)
2482  public:
2483   UnsafeGetObject(BasicType basic_type, Value object, Value offset, bool is_volatile)
2484   : UnsafeObjectOp(basic_type, object, offset, false, is_volatile)
2485   {
2486     ASSERT_VALUES
2487   }
2488 };
2489 
2490 
2491 LEAF(UnsafePutObject, UnsafeObjectOp)
2492  private:
2493   Value _value;                                  // Value to be stored
2494  public:
2495   UnsafePutObject(BasicType basic_type, Value object, Value offset, Value value, bool is_volatile)
2496   : UnsafeObjectOp(basic_type, object, offset, true, is_volatile)
2497     , _value(value)
2498   {
2499     ASSERT_VALUES
2500   }
2501 
2502   // accessors
2503   Value value()                                  { return _value; }
2504 
2505   // generic
2506   virtual void input_values_do(ValueVisitor* f)   { UnsafeObjectOp::input_values_do(f);
2507                                                    f->visit(&_value); }
2508 };
2509 
2510 LEAF(UnsafeGetAndSetObject, UnsafeObjectOp)
2511  private:
2512   Value _value;                                  // Value to be stored
2513   bool  _is_add;
2514  public:
2515   UnsafeGetAndSetObject(BasicType basic_type, Value object, Value offset, Value value, bool is_add)
2516   : UnsafeObjectOp(basic_type, object, offset, false, false)
2517     , _value(value)
2518     , _is_add(is_add)
2519   {
2520     ASSERT_VALUES
2521   }
2522 
2523   // accessors
2524   bool is_add() const                            { return _is_add; }
2525   Value value()                                  { return _value; }
2526 
2527   // generic
2528   virtual void input_values_do(ValueVisitor* f)   { UnsafeObjectOp::input_values_do(f);
2529                                                    f->visit(&_value); }
2530 };
2531 
2532 LEAF(ProfileCall, Instruction)
2533  private:
2534   ciMethod*        _method;
2535   int              _bci_of_invoke;
2536   ciMethod*        _callee;         // the method that is called at the given bci
2537   Value            _recv;
2538   ciKlass*         _known_holder;
2539   Values*          _obj_args;       // arguments for type profiling
2540   ArgsNonNullState _nonnull_state;  // Do we know whether some arguments are never null?
2541   bool             _inlined;        // Are we profiling a call that is inlined
2542 
2543  public:
2544   ProfileCall(ciMethod* method, int bci, ciMethod* callee, Value recv, ciKlass* known_holder, Values* obj_args, bool inlined)
2545     : Instruction(voidType)
2546     , _method(method)
2547     , _bci_of_invoke(bci)
2548     , _callee(callee)
2549     , _recv(recv)
2550     , _known_holder(known_holder)
2551     , _obj_args(obj_args)
2552     , _inlined(inlined)
2553   {
2554     // The ProfileCall has side-effects and must occur precisely where located
2555     pin();
2556   }
2557 
2558   ciMethod* method()             const { return _method; }
2559   int bci_of_invoke()            const { return _bci_of_invoke; }
2560   ciMethod* callee()             const { return _callee; }
2561   Value recv()                   const { return _recv; }
2562   ciKlass* known_holder()        const { return _known_holder; }
2563   int nb_profiled_args()         const { return _obj_args == NULL ? 0 : _obj_args->length(); }
2564   Value profiled_arg_at(int i)   const { return _obj_args->at(i); }
2565   bool arg_needs_null_check(int i) const {
2566     return _nonnull_state.arg_needs_null_check(i);
2567   }
2568   bool inlined()                 const { return _inlined; }
2569 
2570   void set_arg_needs_null_check(int i, bool check) {
2571     _nonnull_state.set_arg_needs_null_check(i, check);
2572   }
2573 
2574   virtual void input_values_do(ValueVisitor* f)   {
2575     if (_recv != NULL) {
2576       f->visit(&_recv);
2577     }
2578     for (int i = 0; i < nb_profiled_args(); i++) {
2579       f->visit(_obj_args->adr_at(i));
2580     }
2581   }
2582 };
2583 
2584 LEAF(ProfileReturnType, Instruction)
2585  private:
2586   ciMethod*        _method;
2587   ciMethod*        _callee;
2588   int              _bci_of_invoke;
2589   Value            _ret;
2590 
2591  public:
2592   ProfileReturnType(ciMethod* method, int bci, ciMethod* callee, Value ret)
2593     : Instruction(voidType)
2594     , _method(method)
2595     , _callee(callee)
2596     , _bci_of_invoke(bci)
2597     , _ret(ret)
2598   {
2599     set_needs_null_check(true);
2600     // The ProfileType has side-effects and must occur precisely where located
2601     pin();
2602   }
2603 
2604   ciMethod* method()             const { return _method; }
2605   ciMethod* callee()             const { return _callee; }
2606   int bci_of_invoke()            const { return _bci_of_invoke; }
2607   Value ret()                    const { return _ret; }
2608 
2609   virtual void input_values_do(ValueVisitor* f)   {
2610     if (_ret != NULL) {
2611       f->visit(&_ret);
2612     }
2613   }
2614 };
2615 
2616 // Call some C runtime function that doesn't safepoint,
2617 // optionally passing the current thread as the first argument.
2618 LEAF(RuntimeCall, Instruction)
2619  private:
2620   const char* _entry_name;
2621   address     _entry;
2622   Values*     _args;
2623   bool        _pass_thread;  // Pass the JavaThread* as an implicit first argument
2624 
2625  public:
2626   RuntimeCall(ValueType* type, const char* entry_name, address entry, Values* args, bool pass_thread = true)
2627     : Instruction(type)
2628     , _entry_name(entry_name)
2629     , _entry(entry)
2630     , _args(args)
2631     , _pass_thread(pass_thread) {
2632     ASSERT_VALUES
2633     pin();
2634   }
2635 
2636   const char* entry_name() const  { return _entry_name; }
2637   address entry() const           { return _entry; }
2638   int number_of_arguments() const { return _args->length(); }
2639   Value argument_at(int i) const  { return _args->at(i); }
2640   bool pass_thread() const        { return _pass_thread; }
2641 
2642   virtual void input_values_do(ValueVisitor* f)   {
2643     for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
2644   }
2645 };
2646 
2647 // Use to trip invocation counter of an inlined method
2648 
2649 LEAF(ProfileInvoke, Instruction)
2650  private:
2651   ciMethod*   _inlinee;
2652   ValueStack* _state;
2653 
2654  public:
2655   ProfileInvoke(ciMethod* inlinee,  ValueStack* state)
2656     : Instruction(voidType)
2657     , _inlinee(inlinee)
2658     , _state(state)
2659   {
2660     // The ProfileInvoke has side-effects and must occur precisely where located QQQ???
2661     pin();
2662   }
2663 
2664   ciMethod* inlinee()      { return _inlinee; }
2665   ValueStack* state()      { return _state; }
2666   virtual void input_values_do(ValueVisitor*)   {}
2667   virtual void state_values_do(ValueVisitor*);
2668 };
2669 
2670 LEAF(MemBar, Instruction)
2671  private:
2672   LIR_Code _code;
2673 
2674  public:
2675   MemBar(LIR_Code code)
2676     : Instruction(voidType)
2677     , _code(code)
2678   {
2679     pin();
2680   }
2681 
2682   LIR_Code code()           { return _code; }
2683 
2684   virtual void input_values_do(ValueVisitor*)   {}
2685 };
2686 
2687 class BlockPair: public CompilationResourceObj {
2688  private:
2689   BlockBegin* _from;
2690   BlockBegin* _to;
2691  public:
2692   BlockPair(BlockBegin* from, BlockBegin* to): _from(from), _to(to) {}
2693   BlockBegin* from() const { return _from; }
2694   BlockBegin* to() const   { return _to;   }
2695   bool is_same(BlockBegin* from, BlockBegin* to) const { return  _from == from && _to == to; }
2696   bool is_same(BlockPair* p) const { return  _from == p->from() && _to == p->to(); }
2697   void set_to(BlockBegin* b)   { _to = b; }
2698   void set_from(BlockBegin* b) { _from = b; }
2699 };
2700 
2701 typedef GrowableArray<BlockPair*> BlockPairList;
2702 
2703 inline int         BlockBegin::number_of_sux() const            { assert(_end == NULL || _end->number_of_sux() == _successors.length(), "mismatch"); return _successors.length(); }
2704 inline BlockBegin* BlockBegin::sux_at(int i) const              { assert(_end == NULL || _end->sux_at(i) == _successors.at(i), "mismatch");          return _successors.at(i); }
2705 inline void        BlockBegin::add_successor(BlockBegin* sux)   { assert(_end == NULL, "Would create mismatch with successors of BlockEnd");         _successors.append(sux); }
2706 
2707 #undef ASSERT_VALUES
2708 
2709 #endif // SHARE_C1_C1_INSTRUCTION_HPP