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