1 /*
   2  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_VM_C1_C1_LIRGENERATOR_HPP
  27 #define SHARE_VM_C1_C1_LIRGENERATOR_HPP
  28 
  29 #include "c1/c1_Decorators.hpp"
  30 #include "c1/c1_Instruction.hpp"
  31 #include "c1/c1_LIR.hpp"
  32 #include "ci/ciMethodData.hpp"
  33 #include "gc/shared/barrierSet.hpp"
  34 #include "jfr/support/jfrIntrinsics.hpp"
  35 #include "utilities/macros.hpp"
  36 #include "utilities/sizes.hpp"
  37 
  38 class BarrierSetC1;
  39 
  40 // The classes responsible for code emission and register allocation
  41 
  42 
  43 class LIRGenerator;
  44 class LIREmitter;
  45 class Invoke;
  46 class SwitchRange;
  47 class LIRItem;
  48 
  49 typedef GrowableArray<LIRItem*> LIRItemList;
  50 
  51 class SwitchRange: public CompilationResourceObj {
  52  private:
  53   int _low_key;
  54   int _high_key;
  55   BlockBegin* _sux;
  56  public:
  57   SwitchRange(int start_key, BlockBegin* sux): _low_key(start_key), _high_key(start_key), _sux(sux) {}
  58   void set_high_key(int key) { _high_key = key; }
  59 
  60   int high_key() const { return _high_key; }
  61   int low_key() const { return _low_key; }
  62   BlockBegin* sux() const { return _sux; }
  63 };
  64 
  65 typedef GrowableArray<SwitchRange*> SwitchRangeArray;
  66 typedef GrowableArray<SwitchRange*> SwitchRangeList;
  67 
  68 class ResolveNode;
  69 
  70 typedef GrowableArray<ResolveNode*> NodeList;
  71 
  72 // Node objects form a directed graph of LIR_Opr
  73 // Edges between Nodes represent moves from one Node to its destinations
  74 class ResolveNode: public CompilationResourceObj {
  75  private:
  76   LIR_Opr    _operand;       // the source or destinaton
  77   NodeList   _destinations;  // for the operand
  78   bool       _assigned;      // Value assigned to this Node?
  79   bool       _visited;       // Node already visited?
  80   bool       _start_node;    // Start node already visited?
  81 
  82  public:
  83   ResolveNode(LIR_Opr operand)
  84     : _operand(operand)
  85     , _assigned(false)
  86     , _visited(false)
  87     , _start_node(false) {};
  88 
  89   // accessors
  90   LIR_Opr operand() const           { return _operand; }
  91   int no_of_destinations() const    { return _destinations.length(); }
  92   ResolveNode* destination_at(int i)     { return _destinations.at(i); }
  93   bool assigned() const             { return _assigned; }
  94   bool visited() const              { return _visited; }
  95   bool start_node() const           { return _start_node; }
  96 
  97   // modifiers
  98   void append(ResolveNode* dest)         { _destinations.append(dest); }
  99   void set_assigned()               { _assigned = true; }
 100   void set_visited()                { _visited = true; }
 101   void set_start_node()             { _start_node = true; }
 102 };
 103 
 104 
 105 // This is shared state to be used by the PhiResolver so the operand
 106 // arrays don't have to be reallocated for reach resolution.
 107 class PhiResolverState: public CompilationResourceObj {
 108   friend class PhiResolver;
 109 
 110  private:
 111   NodeList _virtual_operands; // Nodes where the operand is a virtual register
 112   NodeList _other_operands;   // Nodes where the operand is not a virtual register
 113   NodeList _vreg_table;       // Mapping from virtual register to Node
 114 
 115  public:
 116   PhiResolverState() {}
 117 
 118   void reset(int max_vregs);
 119 };
 120 
 121 
 122 // class used to move value of phi operand to phi function
 123 class PhiResolver: public CompilationResourceObj {
 124  private:
 125   LIRGenerator*     _gen;
 126   PhiResolverState& _state; // temporary state cached by LIRGenerator
 127 
 128   ResolveNode*   _loop;
 129   LIR_Opr _temp;
 130 
 131   // access to shared state arrays
 132   NodeList& virtual_operands() { return _state._virtual_operands; }
 133   NodeList& other_operands()   { return _state._other_operands;   }
 134   NodeList& vreg_table()       { return _state._vreg_table;       }
 135 
 136   ResolveNode* create_node(LIR_Opr opr, bool source);
 137   ResolveNode* source_node(LIR_Opr opr)      { return create_node(opr, true); }
 138   ResolveNode* destination_node(LIR_Opr opr) { return create_node(opr, false); }
 139 
 140   void emit_move(LIR_Opr src, LIR_Opr dest);
 141   void move_to_temp(LIR_Opr src);
 142   void move_temp_to(LIR_Opr dest);
 143   void move(ResolveNode* src, ResolveNode* dest);
 144 
 145   LIRGenerator* gen() {
 146     return _gen;
 147   }
 148 
 149  public:
 150   PhiResolver(LIRGenerator* _lir_gen, int max_vregs);
 151   ~PhiResolver();
 152 
 153   void move(LIR_Opr src, LIR_Opr dest);
 154 };
 155 
 156 
 157 // only the classes below belong in the same file
 158 class LIRGenerator: public InstructionVisitor, public BlockClosure {
 159  // LIRGenerator should never get instatiated on the heap.
 160  private:
 161   void* operator new(size_t size) throw();
 162   void* operator new[](size_t size) throw();
 163   void operator delete(void* p) { ShouldNotReachHere(); }
 164   void operator delete[](void* p) { ShouldNotReachHere(); }
 165 
 166   Compilation*  _compilation;
 167   ciMethod*     _method;    // method that we are compiling
 168   PhiResolverState  _resolver_state;
 169   BlockBegin*   _block;
 170   int           _virtual_register_number;
 171   Values        _instruction_for_operand;
 172   BitMap2D      _vreg_flags; // flags which can be set on a per-vreg basis
 173   LIR_List*     _lir;
 174 
 175   LIRGenerator* gen() {
 176     return this;
 177   }
 178 
 179   void print_if_not_loaded(const NewInstance* new_instance) PRODUCT_RETURN;
 180 
 181  public:
 182 #ifdef ASSERT
 183   LIR_List* lir(const char * file, int line) const {
 184     _lir->set_file_and_line(file, line);
 185     return _lir;
 186   }
 187 #endif
 188   LIR_List* lir() const {
 189     return _lir;
 190   }
 191 
 192  private:
 193   // a simple cache of constants used within a block
 194   GrowableArray<LIR_Const*>       _constants;
 195   LIR_OprList                     _reg_for_constants;
 196   Values                          _unpinned_constants;
 197 
 198   friend class PhiResolver;
 199 
 200  public:
 201   // unified bailout support
 202   void bailout(const char* msg) const            { compilation()->bailout(msg); }
 203   bool bailed_out() const                        { return compilation()->bailed_out(); }
 204 
 205   void block_do_prolog(BlockBegin* block);
 206   void block_do_epilog(BlockBegin* block);
 207 
 208   // register allocation
 209   LIR_Opr rlock(Value instr);                      // lock a free register
 210   LIR_Opr rlock_result(Value instr);
 211   LIR_Opr rlock_result(Value instr, BasicType type);
 212   LIR_Opr rlock_byte(BasicType type);
 213   LIR_Opr rlock_callee_saved(BasicType type);
 214 
 215   // get a constant into a register and get track of what register was used
 216   LIR_Opr load_constant(Constant* x);
 217   LIR_Opr load_constant(LIR_Const* constant);
 218 
 219   // Given an immediate value, return an operand usable in logical ops.
 220   LIR_Opr load_immediate(int x, BasicType type);
 221 
 222   void  set_result(Value x, LIR_Opr opr)           {
 223     assert(opr->is_valid(), "must set to valid value");
 224     assert(x->operand()->is_illegal(), "operand should never change");
 225     assert(!opr->is_register() || opr->is_virtual(), "should never set result to a physical register");
 226     x->set_operand(opr);
 227     assert(opr == x->operand(), "must be");
 228     if (opr->is_virtual()) {
 229       _instruction_for_operand.at_put_grow(opr->vreg_number(), x, NULL);
 230     }
 231   }
 232   void  set_no_result(Value x)                     { assert(!x->has_uses(), "can't have use"); x->clear_operand(); }
 233 
 234   friend class LIRItem;
 235 
 236   LIR_Opr round_item(LIR_Opr opr);
 237   LIR_Opr force_to_spill(LIR_Opr value, BasicType t);
 238 
 239   PhiResolverState& resolver_state() { return _resolver_state; }
 240 
 241   void  move_to_phi(PhiResolver* resolver, Value cur_val, Value sux_val);
 242   void  move_to_phi(ValueStack* cur_state);
 243 
 244   // platform dependent
 245   LIR_Opr getThreadPointer();
 246 
 247  private:
 248   // code emission
 249   void do_ArithmeticOp_Long(ArithmeticOp* x);
 250   void do_ArithmeticOp_Int (ArithmeticOp* x);
 251   void do_ArithmeticOp_FPU (ArithmeticOp* x);
 252 
 253   void do_RegisterFinalizer(Intrinsic* x);
 254   void do_isInstance(Intrinsic* x);
 255   void do_isPrimitive(Intrinsic* x);
 256   void do_getClass(Intrinsic* x);
 257   void do_currentThread(Intrinsic* x);
 258   void do_FmaIntrinsic(Intrinsic* x);
 259   void do_MathIntrinsic(Intrinsic* x);
 260   void do_LibmIntrinsic(Intrinsic* x);
 261   void do_ArrayCopy(Intrinsic* x);
 262   void do_CompareAndSwap(Intrinsic* x, ValueType* type);
 263   void do_NIOCheckIndex(Intrinsic* x);
 264   void do_FPIntrinsics(Intrinsic* x);
 265   void do_Reference_get(Intrinsic* x);
 266   void do_update_CRC32(Intrinsic* x);
 267   void do_update_CRC32C(Intrinsic* x);
 268 #ifdef AARCH32
 269   void do_update_CRC32_inner(Intrinsic* x, int is_crc32c);
 270 #endif
 271   void do_vectorizedMismatch(Intrinsic* x);
 272 #ifdef AARCH32
 273   void do_aescrypt_block(Intrinsic* x);
 274   void do_aescrypt_cbc(Intrinsic* x);
 275   void do_sha(Intrinsic* x);
 276   void do_montgomery_intrinsic(Intrinsic *x);
 277 #endif
 278 
 279  public:
 280   LIR_Opr call_runtime(BasicTypeArray* signature, LIRItemList* args, address entry, ValueType* result_type, CodeEmitInfo* info);
 281   LIR_Opr call_runtime(BasicTypeArray* signature, LIR_OprList* args, address entry, ValueType* result_type, CodeEmitInfo* info);
 282 
 283   // convenience functions
 284   LIR_Opr call_runtime(Value arg1, address entry, ValueType* result_type, CodeEmitInfo* info);
 285   LIR_Opr call_runtime(Value arg1, Value arg2, address entry, ValueType* result_type, CodeEmitInfo* info);
 286 
 287   // Access API
 288 
 289  private:
 290   BarrierSetC1 *_barrier_set;
 291 
 292  public:
 293   void access_store_at(DecoratorSet decorators, BasicType type,
 294                        LIRItem& base, LIR_Opr offset, LIR_Opr value,
 295                        CodeEmitInfo* patch_info = NULL, CodeEmitInfo* store_emit_info = NULL);
 296 
 297   void access_load_at(DecoratorSet decorators, BasicType type,
 298                       LIRItem& base, LIR_Opr offset, LIR_Opr result,
 299                       CodeEmitInfo* patch_info = NULL, CodeEmitInfo* load_emit_info = NULL);
 300 
 301   LIR_Opr access_atomic_cmpxchg_at(DecoratorSet decorators, BasicType type,
 302                                    LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value);
 303 
 304   LIR_Opr access_atomic_xchg_at(DecoratorSet decorators, BasicType type,
 305                                 LIRItem& base, LIRItem& offset, LIRItem& value);
 306 
 307   LIR_Opr access_atomic_add_at(DecoratorSet decorators, BasicType type,
 308                                LIRItem& base, LIRItem& offset, LIRItem& value);
 309 
 310   // These need to guarantee JMM volatile semantics are preserved on each platform
 311   // and requires one implementation per architecture.
 312   LIR_Opr atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value);
 313   LIR_Opr atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& new_value);
 314   LIR_Opr atomic_add(BasicType type, LIR_Opr addr, LIRItem& new_value);
 315 
 316 #ifdef CARDTABLEBARRIERSET_POST_BARRIER_HELPER
 317   virtual void CardTableBarrierSet_post_barrier_helper(LIR_OprDesc* addr, LIR_Const* card_table_base);
 318 #endif
 319 
 320   // specific implementations
 321   void array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info, ciMethod* profiled_method, int profiled_bci);
 322 
 323   static LIR_Opr result_register_for(ValueType* type, bool callee = false);
 324 #ifdef AARCH32
 325   static LIR_Opr java_result_register_for(ValueType* type, bool callee = false);
 326 #endif
 327 
 328   ciObject* get_jobject_constant(Value value);
 329 
 330   LIRItemList* invoke_visit_arguments(Invoke* x);
 331   void invoke_load_arguments(Invoke* x, LIRItemList* args, const LIR_OprList* arg_list);
 332 
 333   void trace_block_entry(BlockBegin* block);
 334 
 335   // volatile field operations are never patchable because a klass
 336   // must be loaded to know it's volatile which means that the offset
 337   // it always known as well.
 338   void volatile_field_store(LIR_Opr value, LIR_Address* address, CodeEmitInfo* info);
 339   void volatile_field_load(LIR_Address* address, LIR_Opr result, CodeEmitInfo* info);
 340 
 341   void put_Object_unsafe(LIR_Opr src, LIR_Opr offset, LIR_Opr data, BasicType type, bool is_volatile);
 342   void get_Object_unsafe(LIR_Opr dest, LIR_Opr src, LIR_Opr offset, BasicType type, bool is_volatile);
 343 
 344   void arithmetic_call_op (Bytecodes::Code code, LIR_Opr result, LIR_OprList* args);
 345 
 346   void increment_counter(address counter, BasicType type, int step = 1);
 347   void increment_counter(LIR_Address* addr, int step = 1);
 348 
 349   // is_strictfp is only needed for mul and div (and only generates different code on i486)
 350   void arithmetic_op(Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp, CodeEmitInfo* info = NULL);
 351   // machine dependent.  returns true if it emitted code for the multiply
 352   bool strength_reduce_multiply(LIR_Opr left, jint constant, LIR_Opr result, LIR_Opr tmp);
 353 
 354   void store_stack_parameter (LIR_Opr opr, ByteSize offset_from_sp_in_bytes);
 355 
 356   void klass2reg_with_patching(LIR_Opr r, ciMetadata* obj, CodeEmitInfo* info, bool need_resolve = false);
 357 
 358   // this loads the length and compares against the index
 359   void array_range_check          (LIR_Opr array, LIR_Opr index, CodeEmitInfo* null_check_info, CodeEmitInfo* range_check_info);
 360   // For java.nio.Buffer.checkIndex
 361   void nio_range_check            (LIR_Opr buffer, LIR_Opr index, LIR_Opr result, CodeEmitInfo* info);
 362 
 363   void arithmetic_op_int  (Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, LIR_Opr tmp);
 364   void arithmetic_op_long (Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info = NULL);
 365   void arithmetic_op_fpu  (Bytecodes::Code code, LIR_Opr result, LIR_Opr left, LIR_Opr right, bool is_strictfp, LIR_Opr tmp = LIR_OprFact::illegalOpr);
 366 
 367   void shift_op   (Bytecodes::Code code, LIR_Opr dst_reg, LIR_Opr value, LIR_Opr count, LIR_Opr tmp);
 368 
 369   void logic_op   (Bytecodes::Code code, LIR_Opr dst_reg, LIR_Opr left, LIR_Opr right);
 370 
 371   void monitor_enter (LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no, CodeEmitInfo* info_for_exception, CodeEmitInfo* info);
 372   void monitor_exit  (LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no);
 373 
 374   void new_instance    (LIR_Opr  dst, ciInstanceKlass* klass, bool is_unresolved, LIR_Opr  scratch1, LIR_Opr  scratch2, LIR_Opr  scratch3,  LIR_Opr scratch4, LIR_Opr  klass_reg, CodeEmitInfo* info);
 375 
 376   // machine dependent
 377   void cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info);
 378   void cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info);
 379 
 380   void arraycopy_helper(Intrinsic* x, int* flags, ciArrayKlass** expected_type);
 381 
 382   // returns a LIR_Address to address an array location.  May also
 383   // emit some code as part of address calculation.  If
 384   // needs_card_mark is true then compute the full address for use by
 385   // both the store and the card mark.
 386   LIR_Address* generate_address(LIR_Opr base,
 387                                 LIR_Opr index, int shift,
 388                                 int disp,
 389                                 BasicType type);
 390   LIR_Address* generate_address(LIR_Opr base, int disp, BasicType type) {
 391     return generate_address(base, LIR_OprFact::illegalOpr, 0, disp, type);
 392   }
 393   LIR_Address* emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr, BasicType type);
 394 
 395   // the helper for generate_address
 396   void add_large_constant(LIR_Opr src, int c, LIR_Opr dest);
 397 
 398   // machine preferences and characteristics
 399   bool can_inline_as_constant(Value i S390_ONLY(COMMA int bits = 20)) const;
 400   bool can_inline_as_constant(LIR_Const* c) const;
 401   bool can_store_as_constant(Value i, BasicType type) const;
 402 
 403   LIR_Opr safepoint_poll_register();
 404 
 405   void profile_branch(If* if_instr, If::Condition cond);
 406   void increment_event_counter_impl(CodeEmitInfo* info,
 407                                     ciMethod *method, LIR_Opr step, int frequency,
 408                                     int bci, bool backedge, bool notify);
 409   void increment_event_counter(CodeEmitInfo* info, LIR_Opr step, int bci, bool backedge);
 410   void increment_invocation_counter(CodeEmitInfo *info) {
 411     if (compilation()->count_invocations()) {
 412       increment_event_counter(info, LIR_OprFact::intConst(InvocationCounter::count_increment), InvocationEntryBci, false);
 413     }
 414   }
 415   void increment_backedge_counter(CodeEmitInfo* info, int bci) {
 416     if (compilation()->count_backedges()) {
 417       increment_event_counter(info, LIR_OprFact::intConst(InvocationCounter::count_increment), bci, true);
 418     }
 419   }
 420   void increment_backedge_counter_conditionally(LIR_Condition cond, LIR_Opr left, LIR_Opr right, CodeEmitInfo* info, int left_bci, int right_bci, int bci);
 421   void increment_backedge_counter(CodeEmitInfo* info, LIR_Opr step, int bci) {
 422     if (compilation()->count_backedges()) {
 423       increment_event_counter(info, step, bci, true);
 424     }
 425   }
 426   void decrement_age(CodeEmitInfo* info);
 427   CodeEmitInfo* state_for(Instruction* x, ValueStack* state, bool ignore_xhandler = false);
 428   CodeEmitInfo* state_for(Instruction* x);
 429 
 430   // allocates a virtual register for this instruction if
 431   // one isn't already allocated.  Only for Phi and Local.
 432   LIR_Opr operand_for_instruction(Instruction *x);
 433 
 434   void set_block(BlockBegin* block)              { _block = block; }
 435 
 436   void block_prolog(BlockBegin* block);
 437   void block_epilog(BlockBegin* block);
 438 
 439   void do_root (Instruction* instr);
 440   void walk    (Instruction* instr);
 441 
 442   void bind_block_entry(BlockBegin* block);
 443   void start_block(BlockBegin* block);
 444 
 445   LIR_Opr new_register(BasicType type);
 446   LIR_Opr new_register(Value value)              { return new_register(as_BasicType(value->type())); }
 447   LIR_Opr new_register(ValueType* type)          { return new_register(as_BasicType(type)); }
 448 
 449   // returns a register suitable for doing pointer math
 450   LIR_Opr new_pointer_register() {
 451 #ifdef _LP64
 452     return new_register(T_LONG);
 453 #else
 454     return new_register(T_INT);
 455 #endif
 456   }
 457 
 458   static LIR_Condition lir_cond(If::Condition cond) {
 459     LIR_Condition l = lir_cond_unknown;
 460     switch (cond) {
 461     case If::eql: l = lir_cond_equal;        break;
 462     case If::neq: l = lir_cond_notEqual;     break;
 463     case If::lss: l = lir_cond_less;         break;
 464     case If::leq: l = lir_cond_lessEqual;    break;
 465     case If::geq: l = lir_cond_greaterEqual; break;
 466     case If::gtr: l = lir_cond_greater;      break;
 467     case If::aeq: l = lir_cond_aboveEqual;   break;
 468     case If::beq: l = lir_cond_belowEqual;   break;
 469     default: fatal("You must pass valid If::Condition");
 470     };
 471     return l;
 472   }
 473 
 474 #ifdef __SOFTFP__
 475   void do_soft_float_compare(If *x);
 476 #endif // __SOFTFP__
 477 
 478   SwitchRangeArray* create_lookup_ranges(TableSwitch* x);
 479   SwitchRangeArray* create_lookup_ranges(LookupSwitch* x);
 480   void do_SwitchRanges(SwitchRangeArray* x, LIR_Opr value, BlockBegin* default_sux);
 481 
 482 #ifdef JFR_HAVE_INTRINSICS
 483   void do_ClassIDIntrinsic(Intrinsic* x);
 484   void do_getEventWriter(Intrinsic* x);
 485 #endif
 486 
 487   void do_RuntimeCall(address routine, Intrinsic* x);
 488 
 489   ciKlass* profile_type(ciMethodData* md, int md_first_offset, int md_offset, intptr_t profiled_k,
 490                         Value arg, LIR_Opr& mdp, bool not_null, ciKlass* signature_at_call_k,
 491                         ciKlass* callee_signature_k);
 492   void profile_arguments(ProfileCall* x);
 493   void profile_parameters(Base* x);
 494   void profile_parameters_at_call(ProfileCall* x);
 495   LIR_Opr mask_boolean(LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info);
 496   LIR_Opr maybe_mask_boolean(StoreIndexed* x, LIR_Opr array, LIR_Opr value, CodeEmitInfo*& null_check_info);
 497 
 498  public:
 499   Compilation*  compilation() const              { return _compilation; }
 500   FrameMap*     frame_map() const                { return _compilation->frame_map(); }
 501   ciMethod*     method() const                   { return _method; }
 502   BlockBegin*   block() const                    { return _block; }
 503   IRScope*      scope() const                    { return block()->scope(); }
 504 
 505   int max_virtual_register_number() const        { return _virtual_register_number; }
 506 
 507   void block_do(BlockBegin* block);
 508 
 509   // Flags that can be set on vregs
 510   enum VregFlag {
 511       must_start_in_memory = 0  // needs to be assigned a memory location at beginning, but may then be loaded in a register
 512     , callee_saved     = 1    // must be in a callee saved register
 513     , byte_reg         = 2    // must be in a byte register
 514     , num_vreg_flags
 515 
 516   };
 517 
 518   LIRGenerator(Compilation* compilation, ciMethod* method)
 519     : _compilation(compilation)
 520     , _method(method)
 521     , _virtual_register_number(LIR_OprDesc::vreg_base)
 522     , _vreg_flags(num_vreg_flags)
 523     , _barrier_set(BarrierSet::barrier_set()->barrier_set_c1()) {
 524   }
 525 
 526   // for virtual registers, maps them back to Phi's or Local's
 527   Instruction* instruction_for_opr(LIR_Opr opr);
 528   Instruction* instruction_for_vreg(int reg_num);
 529 
 530   void set_vreg_flag   (int vreg_num, VregFlag f);
 531   bool is_vreg_flag_set(int vreg_num, VregFlag f);
 532   void set_vreg_flag   (LIR_Opr opr,  VregFlag f) { set_vreg_flag(opr->vreg_number(), f); }
 533   bool is_vreg_flag_set(LIR_Opr opr,  VregFlag f) { return is_vreg_flag_set(opr->vreg_number(), f); }
 534 
 535   // statics
 536   static LIR_Opr exceptionOopOpr();
 537   static LIR_Opr exceptionPcOpr();
 538   static LIR_Opr divInOpr();
 539   static LIR_Opr divOutOpr();
 540   static LIR_Opr remOutOpr();
 541 #ifdef S390
 542   // On S390 we can do ldiv, lrem without RT call.
 543   static LIR_Opr ldivInOpr();
 544   static LIR_Opr ldivOutOpr();
 545   static LIR_Opr lremOutOpr();
 546 #endif
 547   static LIR_Opr shiftCountOpr();
 548   LIR_Opr syncLockOpr();
 549   LIR_Opr syncTempOpr();
 550   LIR_Opr atomicLockOpr();
 551 
 552   // returns a register suitable for saving the thread in a
 553   // call_runtime_leaf if one is needed.
 554   LIR_Opr getThreadTemp();
 555 
 556   // visitor functionality
 557   virtual void do_Phi            (Phi*             x);
 558   virtual void do_Local          (Local*           x);
 559   virtual void do_Constant       (Constant*        x);
 560   virtual void do_LoadField      (LoadField*       x);
 561   virtual void do_StoreField     (StoreField*      x);
 562   virtual void do_ArrayLength    (ArrayLength*     x);
 563   virtual void do_LoadIndexed    (LoadIndexed*     x);
 564   virtual void do_StoreIndexed   (StoreIndexed*    x);
 565   virtual void do_NegateOp       (NegateOp*        x);
 566   virtual void do_ArithmeticOp   (ArithmeticOp*    x);
 567   virtual void do_ShiftOp        (ShiftOp*         x);
 568   virtual void do_LogicOp        (LogicOp*         x);
 569   virtual void do_CompareOp      (CompareOp*       x);
 570   virtual void do_IfOp           (IfOp*            x);
 571   virtual void do_Convert        (Convert*         x);
 572   virtual void do_NullCheck      (NullCheck*       x);
 573   virtual void do_TypeCast       (TypeCast*        x);
 574   virtual void do_Invoke         (Invoke*          x);
 575   virtual void do_NewInstance    (NewInstance*     x);
 576   virtual void do_NewTypeArray   (NewTypeArray*    x);
 577   virtual void do_NewObjectArray (NewObjectArray*  x);
 578   virtual void do_NewMultiArray  (NewMultiArray*   x);
 579   virtual void do_CheckCast      (CheckCast*       x);
 580   virtual void do_InstanceOf     (InstanceOf*      x);
 581   virtual void do_MonitorEnter   (MonitorEnter*    x);
 582   virtual void do_MonitorExit    (MonitorExit*     x);
 583   virtual void do_Intrinsic      (Intrinsic*       x);
 584   virtual void do_BlockBegin     (BlockBegin*      x);
 585   virtual void do_Goto           (Goto*            x);
 586   virtual void do_If             (If*              x);
 587   virtual void do_IfInstanceOf   (IfInstanceOf*    x);
 588   virtual void do_TableSwitch    (TableSwitch*     x);
 589   virtual void do_LookupSwitch   (LookupSwitch*    x);
 590   virtual void do_Return         (Return*          x);
 591   virtual void do_Throw          (Throw*           x);
 592   virtual void do_Base           (Base*            x);
 593   virtual void do_OsrEntry       (OsrEntry*        x);
 594   virtual void do_ExceptionObject(ExceptionObject* x);
 595   virtual void do_RoundFP        (RoundFP*         x);
 596   virtual void do_UnsafeGetRaw   (UnsafeGetRaw*    x);
 597   virtual void do_UnsafePutRaw   (UnsafePutRaw*    x);
 598   virtual void do_UnsafeGetObject(UnsafeGetObject* x);
 599   virtual void do_UnsafePutObject(UnsafePutObject* x);
 600   virtual void do_UnsafeGetAndSetObject(UnsafeGetAndSetObject* x);
 601   virtual void do_ProfileCall    (ProfileCall*     x);
 602   virtual void do_ProfileReturnType (ProfileReturnType* x);
 603   virtual void do_ProfileInvoke  (ProfileInvoke*   x);
 604   virtual void do_RuntimeCall    (RuntimeCall*     x);
 605   virtual void do_MemBar         (MemBar*          x);
 606   virtual void do_RangeCheckPredicate(RangeCheckPredicate* x);
 607 #ifdef ASSERT
 608   virtual void do_Assert         (Assert*          x);
 609 #endif
 610 
 611 #ifdef C1_LIRGENERATOR_MD_HPP
 612 #include C1_LIRGENERATOR_MD_HPP
 613 #endif
 614 };
 615 
 616 
 617 class LIRItem: public CompilationResourceObj {
 618  private:
 619   Value         _value;
 620   LIRGenerator* _gen;
 621   LIR_Opr       _result;
 622   bool          _destroys_register;
 623   LIR_Opr       _new_result;
 624 
 625   LIRGenerator* gen() const { return _gen; }
 626 
 627  public:
 628   LIRItem(Value value, LIRGenerator* gen) {
 629     _destroys_register = false;
 630     _gen = gen;
 631     set_instruction(value);
 632   }
 633 
 634   LIRItem(LIRGenerator* gen) {
 635     _destroys_register = false;
 636     _gen = gen;
 637     _result = LIR_OprFact::illegalOpr;
 638     set_instruction(NULL);
 639   }
 640 
 641   void set_instruction(Value value) {
 642     _value = value;
 643     _result = LIR_OprFact::illegalOpr;
 644     if (_value != NULL) {
 645       _gen->walk(_value);
 646       _result = _value->operand();
 647     }
 648     _new_result = LIR_OprFact::illegalOpr;
 649   }
 650 
 651   Value value() const          { return _value;          }
 652   ValueType* type() const      { return value()->type(); }
 653   LIR_Opr result()             {
 654     assert(!_destroys_register || (!_result->is_register() || _result->is_virtual()),
 655            "shouldn't use set_destroys_register with physical regsiters");
 656     if (_destroys_register && _result->is_register()) {
 657       if (_new_result->is_illegal()) {
 658         _new_result = _gen->new_register(type());
 659         gen()->lir()->move(_result, _new_result);
 660       }
 661       return _new_result;
 662     } else {
 663       return _result;
 664     }
 665     return _result;
 666   }
 667 
 668   void set_result(LIR_Opr opr);
 669 
 670   void load_item();
 671   void load_byte_item();
 672   void load_nonconstant(S390_ONLY(int bits = 20));
 673   // load any values which can't be expressed as part of a single store instruction
 674   void load_for_store(BasicType store_type);
 675   void load_item_force(LIR_Opr reg);
 676 
 677   void dont_load_item() {
 678     // do nothing
 679   }
 680 
 681   void set_destroys_register() {
 682     _destroys_register = true;
 683   }
 684 
 685   bool is_constant() const { return value()->as_Constant() != NULL; }
 686   bool is_stack()          { return result()->is_stack(); }
 687   bool is_register()       { return result()->is_register(); }
 688 
 689   ciObject* get_jobject_constant() const;
 690   jint      get_jint_constant() const;
 691   jlong     get_jlong_constant() const;
 692   jfloat    get_jfloat_constant() const;
 693   jdouble   get_jdouble_constant() const;
 694   jint      get_address_constant() const;
 695 };
 696 
 697 #endif // SHARE_VM_C1_C1_LIRGENERATOR_HPP