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