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