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