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