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