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