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();
515 bool maybe_null_free_array();
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 NewInstance* _vt;
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), _vt(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 NewInstance* vt() const { return _vt; }
991 void set_vt(NewInstance* vt) { _vt = vt; }
992
993 DelayedLoadIndexed* delayed() const { return _delayed; }
994 void set_delayed(DelayedLoadIndexed* delayed) { _delayed = delayed; }
995
996 // generic;
997 HASHING4(LoadIndexed, delayed() == nullptr && !should_profile(), elt_type(), array()->subst(), index()->subst(), vt())
998 };
999
1000 class DelayedLoadIndexed : public CompilationResourceObj {
1001 private:
1002 LoadIndexed* _load_instr;
1003 ValueStack* _state_before;
1004 ciField* _field;
1005 size_t _offset;
1006 public:
1007 DelayedLoadIndexed(LoadIndexed* load, ValueStack* state_before)
1008 : _load_instr(load)
1009 , _state_before(state_before)
1010 , _field(nullptr)
1011 , _offset(0) { }
1012
1013 void update(ciField* field, int offset) {
1014 assert(offset >= 0, "must be");
1015 _field = field;
1016 _offset += offset;
1017 }
1018
1019 LoadIndexed* load_instr() const { return _load_instr; }
1020 ValueStack* state_before() const { return _state_before; }
1021 ciField* field() const { return _field; }
1022 size_t offset() const { return _offset; }
1023 };
1024
1025 LEAF(StoreIndexed, AccessIndexed)
1026 private:
1027 Value _value;
1028
1029 bool _check_boolean;
1030
1031 public:
1032 // creation
1033 StoreIndexed(Value array, Value index, Value length, BasicType elt_type, Value value, ValueStack* state_before,
1034 bool check_boolean, bool mismatched = false);
1035
1036 // accessors
1037 Value value() const { return _value; }
1038 bool check_boolean() const { return _check_boolean; }
1039
1040 // Flattened array support
1041 bool is_exact_flat_array_store() const;
1042 // generic
1043 virtual void input_values_do(ValueVisitor* f) { AccessIndexed::input_values_do(f); f->visit(&_value); }
1044 };
1045
1046
1047 LEAF(NegateOp, Instruction)
1048 private:
1049 Value _x;
1050
1051 public:
1052 // creation
1053 NegateOp(Value x) : Instruction(x->type()->base()), _x(x) {
1054 ASSERT_VALUES
1055 }
1056
1057 // accessors
1058 Value x() const { return _x; }
1059
1060 // generic
1061 virtual void input_values_do(ValueVisitor* f) { f->visit(&_x); }
1062 };
1063
1064
1065 BASE(Op2, Instruction)
1066 private:
1067 Bytecodes::Code _op;
1068 Value _x;
1069 Value _y;
1070
1071 public:
1072 // creation
1073 Op2(ValueType* type, Bytecodes::Code op, Value x, Value y, ValueStack* state_before = nullptr)
1074 : Instruction(type, state_before)
1075 , _op(op)
1076 , _x(x)
1077 , _y(y)
1078 {
1079 ASSERT_VALUES
1080 }
1081
1082 // accessors
1083 Bytecodes::Code op() const { return _op; }
1084 Value x() const { return _x; }
1085 Value y() const { return _y; }
1086
1087 // manipulators
1088 void swap_operands() {
1089 assert(is_commutative(), "operation must be commutative");
1090 Value t = _x; _x = _y; _y = t;
1091 }
1092
1093 // generic
1094 virtual bool is_commutative() const { return false; }
1095 virtual void input_values_do(ValueVisitor* f) { f->visit(&_x); f->visit(&_y); }
1096 };
1097
1098
1099 LEAF(ArithmeticOp, Op2)
1100 public:
1101 // creation
1102 ArithmeticOp(Bytecodes::Code op, Value x, Value y, ValueStack* state_before)
1103 : Op2(x->type()->meet(y->type()), op, x, y, state_before)
1104 {
1105 if (can_trap()) pin();
1106 }
1107
1108 // generic
1109 virtual bool is_commutative() const;
1110 virtual bool can_trap() const;
1111 HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1112 };
1113
1114
1115 LEAF(ShiftOp, Op2)
1116 public:
1117 // creation
1118 ShiftOp(Bytecodes::Code op, Value x, Value s) : Op2(x->type()->base(), op, x, s) {}
1119
1120 // generic
1121 HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1122 };
1123
1124
1125 LEAF(LogicOp, Op2)
1126 public:
1127 // creation
1128 LogicOp(Bytecodes::Code op, Value x, Value y) : Op2(x->type()->meet(y->type()), op, x, y) {}
1129
1130 // generic
1131 virtual bool is_commutative() const;
1132 HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1133 };
1134
1135
1136 LEAF(CompareOp, Op2)
1137 public:
1138 // creation
1139 CompareOp(Bytecodes::Code op, Value x, Value y, ValueStack* state_before)
1140 : Op2(intType, op, x, y, state_before)
1141 {}
1142
1143 // generic
1144 HASHING3(Op2, true, op(), x()->subst(), y()->subst())
1145 };
1146
1147
1148 LEAF(IfOp, Op2)
1149 private:
1150 Value _tval;
1151 Value _fval;
1152 bool _substitutability_check;
1153
1154 public:
1155 // creation
1156 IfOp(Value x, Condition cond, Value y, Value tval, Value fval, ValueStack* state_before, bool substitutability_check)
1157 : Op2(tval->type()->meet(fval->type()), (Bytecodes::Code)cond, x, y)
1158 , _tval(tval)
1159 , _fval(fval)
1160 , _substitutability_check(substitutability_check)
1161 {
1162 ASSERT_VALUES
1163 assert(tval->type()->tag() == fval->type()->tag(), "types must match");
1164 set_state_before(state_before);
1165 }
1166
1167 // accessors
1168 virtual bool is_commutative() const;
1169 Bytecodes::Code op() const { ShouldNotCallThis(); return Bytecodes::_illegal; }
1170 Condition cond() const { return (Condition)Op2::op(); }
1171 Value tval() const { return _tval; }
1172 Value fval() const { return _fval; }
1173 bool substitutability_check() const { return _substitutability_check; }
1174 // generic
1175 virtual void input_values_do(ValueVisitor* f) { Op2::input_values_do(f); f->visit(&_tval); f->visit(&_fval); }
1176 };
1177
1178
1179 LEAF(Convert, Instruction)
1180 private:
1181 Bytecodes::Code _op;
1182 Value _value;
1183
1184 public:
1185 // creation
1186 Convert(Bytecodes::Code op, Value value, ValueType* to_type) : Instruction(to_type), _op(op), _value(value) {
1187 ASSERT_VALUES
1188 }
1189
1190 // accessors
1191 Bytecodes::Code op() const { return _op; }
1192 Value value() const { return _value; }
1193
1194 // generic
1195 virtual void input_values_do(ValueVisitor* f) { f->visit(&_value); }
1196 HASHING2(Convert, true, op(), value()->subst())
1197 };
1198
1199
1200 LEAF(NullCheck, Instruction)
1201 private:
1202 Value _obj;
1203
1204 public:
1205 // creation
1206 NullCheck(Value obj, ValueStack* state_before)
1207 : Instruction(obj->type()->base(), state_before)
1208 , _obj(obj)
1209 {
1210 ASSERT_VALUES
1211 set_can_trap(true);
1212 assert(_obj->type()->is_object(), "null check must be applied to objects only");
1213 pin(Instruction::PinExplicitNullCheck);
1214 }
1215
1216 // accessors
1217 Value obj() const { return _obj; }
1218
1219 // setters
1220 void set_can_trap(bool can_trap) { set_flag(CanTrapFlag, can_trap); }
1221
1222 // generic
1223 virtual bool can_trap() const { return check_flag(CanTrapFlag); /* null-check elimination sets to false */ }
1224 virtual void input_values_do(ValueVisitor* f) { f->visit(&_obj); }
1225 HASHING1(NullCheck, true, obj()->subst())
1226 };
1227
1228
1229 // This node is supposed to cast the type of another node to a more precise
1230 // declared type.
1231 LEAF(TypeCast, Instruction)
1232 private:
1233 ciType* _declared_type;
1234 Value _obj;
1235
1236 public:
1237 // The type of this node is the same type as the object type (and it might be constant).
1238 TypeCast(ciType* type, Value obj, ValueStack* state_before)
1239 : Instruction(obj->type(), state_before, obj->type()->is_constant()),
1240 _declared_type(type),
1241 _obj(obj) {}
1242
1243 // accessors
1244 ciType* declared_type() const { return _declared_type; }
1245 Value obj() const { return _obj; }
1246
1247 // generic
1248 virtual void input_values_do(ValueVisitor* f) { f->visit(&_obj); }
1249 };
1250
1251
1252 BASE(StateSplit, Instruction)
1253 private:
1254 ValueStack* _state;
1255
1256 protected:
1257 static void substitute(BlockList& list, BlockBegin* old_block, BlockBegin* new_block);
1258
1259 public:
1260 // creation
1261 StateSplit(ValueType* type, ValueStack* state_before = nullptr)
1262 : Instruction(type, state_before)
1263 , _state(nullptr)
1264 {
1265 pin(PinStateSplitConstructor);
1266 }
1267
1268 // accessors
1269 ValueStack* state() const { return _state; }
1270 IRScope* scope() const; // the state's scope
1271
1272 // manipulation
1273 void set_state(ValueStack* state) { assert(_state == nullptr, "overwriting existing state"); check_state(state); _state = state; }
1274
1275 // generic
1276 virtual void input_values_do(ValueVisitor* f) { /* no values */ }
1277 virtual void state_values_do(ValueVisitor* f);
1278 };
1279
1280
1281 LEAF(Invoke, StateSplit)
1282 private:
1283 Bytecodes::Code _code;
1284 Value _recv;
1285 Values* _args;
1286 BasicTypeList* _signature;
1287 ciMethod* _target;
1288 ciType* _return_type;
1289
1290 public:
1291 // creation
1292 Invoke(Bytecodes::Code code, ciType* return_type, Value recv, Values* args,
1293 ciMethod* target, ValueStack* state_before);
1294
1295 // accessors
1296 Bytecodes::Code code() const { return _code; }
1297 Value receiver() const { return _recv; }
1298 bool has_receiver() const { return receiver() != nullptr; }
1299 int number_of_arguments() const { return _args->length(); }
1300 Value argument_at(int i) const { return _args->at(i); }
1301 BasicTypeList* signature() const { return _signature; }
1302 ciMethod* target() const { return _target; }
1303
1304 ciType* declared_type() const;
1305
1306 // Returns false if target is not loaded
1307 bool target_is_final() const { return check_flag(TargetIsFinalFlag); }
1308 bool target_is_loaded() const { return check_flag(TargetIsLoadedFlag); }
1309
1310 // JSR 292 support
1311 bool is_invokedynamic() const { return code() == Bytecodes::_invokedynamic; }
1312 bool is_method_handle_intrinsic() const { return target()->is_method_handle_intrinsic(); }
1313
1314 virtual bool needs_exception_state() const { return false; }
1315
1316 // generic
1317 virtual bool can_trap() const { return true; }
1318 virtual void input_values_do(ValueVisitor* f) {
1319 StateSplit::input_values_do(f);
1320 if (has_receiver()) f->visit(&_recv);
1321 for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
1322 }
1323 virtual void state_values_do(ValueVisitor *f);
1324 };
1325
1326
1327 LEAF(NewInstance, StateSplit)
1328 private:
1329 ciInstanceKlass* _klass;
1330 bool _is_unresolved;
1331 bool _needs_state_before;
1332
1333 public:
1334 // creation
1335 NewInstance(ciInstanceKlass* klass, ValueStack* state_before, bool is_unresolved, bool needs_state_before)
1336 : StateSplit(instanceType, state_before)
1337 , _klass(klass), _is_unresolved(is_unresolved), _needs_state_before(needs_state_before)
1338 {}
1339
1340 // accessors
1341 ciInstanceKlass* klass() const { return _klass; }
1342 bool is_unresolved() const { return _is_unresolved; }
1343 bool needs_state_before() const { return _needs_state_before; }
1344
1345 virtual bool needs_exception_state() const { return false; }
1346
1347 // generic
1348 virtual bool can_trap() const { return true; }
1349 ciType* exact_type() const;
1350 ciType* declared_type() const;
1351 };
1352
1353 BASE(NewArray, StateSplit)
1354 private:
1355 Value _length;
1356
1357 public:
1358 // creation
1359 NewArray(Value length, ValueStack* state_before)
1360 : StateSplit(objectType, state_before)
1361 , _length(length)
1362 {
1363 // Do not ASSERT_VALUES since length is null for NewMultiArray
1364 }
1365
1366 // accessors
1367 Value length() const { return _length; }
1368
1369 virtual bool needs_exception_state() const { return false; }
1370
1371 ciType* exact_type() const { return nullptr; }
1372 ciType* declared_type() const;
1373
1374 // generic
1375 virtual bool can_trap() const { return true; }
1376 virtual void input_values_do(ValueVisitor* f) { StateSplit::input_values_do(f); f->visit(&_length); }
1377 };
1378
1379
1380 LEAF(NewTypeArray, NewArray)
1381 private:
1382 BasicType _elt_type;
1383 bool _zero_array;
1384
1385 public:
1386 // creation
1387 NewTypeArray(Value length, BasicType elt_type, ValueStack* state_before, bool zero_array)
1388 : NewArray(length, state_before)
1389 , _elt_type(elt_type)
1390 , _zero_array(zero_array)
1391 {}
1392
1393 // accessors
1394 BasicType elt_type() const { return _elt_type; }
1395 bool zero_array() const { return _zero_array; }
1396 ciType* exact_type() const;
1397 };
1398
1399
1400 LEAF(NewObjectArray, NewArray)
1401 private:
1402 ciKlass* _klass;
1403
1404 public:
1405 // creation
1406 NewObjectArray(ciKlass* klass, Value length, ValueStack* state_before)
1407 : NewArray(length, state_before), _klass(klass) { }
1408
1409 // accessors
1410 ciKlass* klass() const { return _klass; }
1411 ciType* exact_type() const;
1412 };
1413
1414
1415 LEAF(NewMultiArray, NewArray)
1416 private:
1417 ciKlass* _klass;
1418 Values* _dims;
1419
1420 public:
1421 // creation
1422 NewMultiArray(ciKlass* klass, Values* dims, ValueStack* state_before) : NewArray(nullptr, state_before), _klass(klass), _dims(dims) {
1423 ASSERT_VALUES
1424 }
1425
1426 // accessors
1427 ciKlass* klass() const { return _klass; }
1428 Values* dims() const { return _dims; }
1429 int rank() const { return dims()->length(); }
1430
1431 // generic
1432 virtual void input_values_do(ValueVisitor* f) {
1433 // NOTE: we do not call NewArray::input_values_do since "length"
1434 // is meaningless for a multi-dimensional array; passing the
1435 // zeroth element down to NewArray as its length is a bad idea
1436 // since there will be a copy in the "dims" array which doesn't
1437 // get updated, and the value must not be traversed twice. Was bug
1438 // - kbr 4/10/2001
1439 StateSplit::input_values_do(f);
1440 for (int i = 0; i < _dims->length(); i++) f->visit(_dims->adr_at(i));
1441 }
1442
1443 ciType* exact_type() const;
1444 };
1445
1446
1447 BASE(TypeCheck, StateSplit)
1448 private:
1449 ciKlass* _klass;
1450 Value _obj;
1451
1452 ciMethod* _profiled_method;
1453 int _profiled_bci;
1454
1455 public:
1456 // creation
1457 TypeCheck(ciKlass* klass, Value obj, ValueType* type, ValueStack* state_before)
1458 : StateSplit(type, state_before), _klass(klass), _obj(obj),
1459 _profiled_method(nullptr), _profiled_bci(0) {
1460 ASSERT_VALUES
1461 set_direct_compare(false);
1462 }
1463
1464 // accessors
1465 ciKlass* klass() const { return _klass; }
1466 Value obj() const { return _obj; }
1467 bool is_loaded() const { return klass() != nullptr; }
1468 bool direct_compare() const { return check_flag(DirectCompareFlag); }
1469
1470 // manipulation
1471 void set_direct_compare(bool flag) { set_flag(DirectCompareFlag, flag); }
1472
1473 // generic
1474 virtual bool can_trap() const { return true; }
1475 virtual void input_values_do(ValueVisitor* f) { StateSplit::input_values_do(f); f->visit(&_obj); }
1476
1477 // Helpers for MethodData* profiling
1478 void set_should_profile(bool value) { set_flag(ProfileMDOFlag, value); }
1479 void set_profiled_method(ciMethod* method) { _profiled_method = method; }
1480 void set_profiled_bci(int bci) { _profiled_bci = bci; }
1481 bool should_profile() const { return check_flag(ProfileMDOFlag); }
1482 ciMethod* profiled_method() const { return _profiled_method; }
1483 int profiled_bci() const { return _profiled_bci; }
1484 };
1485
1486
1487 LEAF(CheckCast, TypeCheck)
1488 public:
1489 // creation
1490 CheckCast(ciKlass* klass, Value obj, ValueStack* state_before)
1491 : TypeCheck(klass, obj, objectType, state_before) { }
1492
1493 void set_incompatible_class_change_check() {
1494 set_flag(ThrowIncompatibleClassChangeErrorFlag, true);
1495 }
1496 bool is_incompatible_class_change_check() const {
1497 return check_flag(ThrowIncompatibleClassChangeErrorFlag);
1498 }
1499 void set_invokespecial_receiver_check() {
1500 set_flag(InvokeSpecialReceiverCheckFlag, true);
1501 }
1502 bool is_invokespecial_receiver_check() const {
1503 return check_flag(InvokeSpecialReceiverCheckFlag);
1504 }
1505
1506 virtual bool needs_exception_state() const {
1507 return !is_invokespecial_receiver_check();
1508 }
1509
1510 ciType* declared_type() const;
1511 };
1512
1513
1514 LEAF(InstanceOf, TypeCheck)
1515 public:
1516 // creation
1517 InstanceOf(ciKlass* klass, Value obj, ValueStack* state_before) : TypeCheck(klass, obj, intType, state_before) {}
1518
1519 virtual bool needs_exception_state() const { return false; }
1520 };
1521
1522
1523 BASE(AccessMonitor, StateSplit)
1524 private:
1525 Value _obj;
1526 int _monitor_no;
1527
1528 public:
1529 // creation
1530 AccessMonitor(Value obj, int monitor_no, ValueStack* state_before = nullptr)
1531 : StateSplit(illegalType, state_before)
1532 , _obj(obj)
1533 , _monitor_no(monitor_no)
1534 {
1535 set_needs_null_check(true);
1536 ASSERT_VALUES
1537 }
1538
1539 // accessors
1540 Value obj() const { return _obj; }
1541 int monitor_no() const { return _monitor_no; }
1542
1543 // generic
1544 virtual void input_values_do(ValueVisitor* f) { StateSplit::input_values_do(f); f->visit(&_obj); }
1545 };
1546
1547
1548 LEAF(MonitorEnter, AccessMonitor)
1549 bool _maybe_inlinetype;
1550 public:
1551 // creation
1552 MonitorEnter(Value obj, int monitor_no, ValueStack* state_before, bool maybe_inlinetype)
1553 : AccessMonitor(obj, monitor_no, state_before)
1554 , _maybe_inlinetype(maybe_inlinetype)
1555 {
1556 ASSERT_VALUES
1557 }
1558
1559 // accessors
1560 bool maybe_inlinetype() const { return _maybe_inlinetype; }
1561
1562 // generic
1563 virtual bool can_trap() const { return true; }
1564 };
1565
1566
1567 LEAF(MonitorExit, AccessMonitor)
1568 public:
1569 // creation
1570 MonitorExit(Value obj, int monitor_no)
1571 : AccessMonitor(obj, monitor_no, nullptr)
1572 {
1573 ASSERT_VALUES
1574 }
1575 };
1576
1577
1578 LEAF(Intrinsic, StateSplit)
1579 private:
1580 vmIntrinsics::ID _id;
1581 ArgsNonNullState _nonnull_state;
1582 Values* _args;
1583 Value _recv;
1584
1585 public:
1586 // preserves_state can be set to true for Intrinsics
1587 // which are guaranteed to preserve register state across any slow
1588 // cases; setting it to true does not mean that the Intrinsic can
1589 // not trap, only that if we continue execution in the same basic
1590 // block after the Intrinsic, all of the registers are intact. This
1591 // allows load elimination and common expression elimination to be
1592 // performed across the Intrinsic. The default value is false.
1593 Intrinsic(ValueType* type,
1594 vmIntrinsics::ID id,
1595 Values* args,
1596 bool has_receiver,
1597 ValueStack* state_before,
1598 bool preserves_state,
1599 bool cantrap = true)
1600 : StateSplit(type, state_before)
1601 , _id(id)
1602 , _args(args)
1603 , _recv(nullptr)
1604 {
1605 assert(args != nullptr, "args must exist");
1606 ASSERT_VALUES
1607 set_flag(PreservesStateFlag, preserves_state);
1608 set_flag(CanTrapFlag, cantrap);
1609 if (has_receiver) {
1610 _recv = argument_at(0);
1611 }
1612 set_needs_null_check(has_receiver);
1613
1614 // some intrinsics can't trap, so don't force them to be pinned
1615 if (!can_trap() && !vmIntrinsics::should_be_pinned(_id)) {
1616 unpin(PinStateSplitConstructor);
1617 }
1618 }
1619
1620 // accessors
1621 vmIntrinsics::ID id() const { return _id; }
1622 int number_of_arguments() const { return _args->length(); }
1623 Value argument_at(int i) const { return _args->at(i); }
1624
1625 bool has_receiver() const { return (_recv != nullptr); }
1626 Value receiver() const { assert(has_receiver(), "must have receiver"); return _recv; }
1627 bool preserves_state() const { return check_flag(PreservesStateFlag); }
1628
1629 bool arg_needs_null_check(int i) const {
1630 return _nonnull_state.arg_needs_null_check(i);
1631 }
1632
1633 void set_arg_needs_null_check(int i, bool check) {
1634 _nonnull_state.set_arg_needs_null_check(i, check);
1635 }
1636
1637 // generic
1638 virtual bool can_trap() const { return check_flag(CanTrapFlag); }
1639 virtual void input_values_do(ValueVisitor* f) {
1640 StateSplit::input_values_do(f);
1641 for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
1642 }
1643 };
1644
1645
1646 class LIR_List;
1647
1648 LEAF(BlockBegin, StateSplit)
1649 private:
1650 int _block_id; // the unique block id
1651 int _bci; // start-bci of block
1652 int _depth_first_number; // number of this block in a depth-first ordering
1653 int _linear_scan_number; // number of this block in linear-scan ordering
1654 int _dominator_depth;
1655 int _loop_depth; // the loop nesting level of this block
1656 int _loop_index; // number of the innermost loop of this block
1657 int _flags; // the flags associated with this block
1658
1659 // fields used by BlockListBuilder
1660 int _total_preds; // number of predecessors found by BlockListBuilder
1661 ResourceBitMap _stores_to_locals; // bit is set when a local variable is stored in the block
1662
1663 // SSA specific fields: (factor out later)
1664 BlockList _predecessors; // the predecessors of this block
1665 BlockList _dominates; // list of blocks that are dominated by this block
1666 BlockBegin* _dominator; // the dominator of this block
1667 // SSA specific ends
1668 BlockEnd* _end; // the last instruction of this block
1669 BlockList _exception_handlers; // the exception handlers potentially invoked by this block
1670 ValueStackStack* _exception_states; // only for xhandler entries: states of all instructions that have an edge to this xhandler
1671 int _exception_handler_pco; // if this block is the start of an exception handler,
1672 // this records the PC offset in the assembly code of the
1673 // first instruction in this block
1674 Label _label; // the label associated with this block
1675 LIR_List* _lir; // the low level intermediate representation for this block
1676
1677 ResourceBitMap _live_in; // set of live LIR_Opr registers at entry to this block
1678 ResourceBitMap _live_out; // set of live LIR_Opr registers at exit from this block
1679 ResourceBitMap _live_gen; // set of registers used before any redefinition in this block
1680 ResourceBitMap _live_kill; // set of registers defined in this block
1681
1682 ResourceBitMap _fpu_register_usage;
1683 int _first_lir_instruction_id; // ID of first LIR instruction in this block
1684 int _last_lir_instruction_id; // ID of last LIR instruction in this block
1685
1686 void iterate_preorder (boolArray& mark, BlockClosure* closure);
1687 void iterate_postorder(boolArray& mark, BlockClosure* closure);
1688
1689 friend class SuxAndWeightAdjuster;
1690
1691 public:
1692 void* operator new(size_t size) throw() {
1693 Compilation* c = Compilation::current();
1694 void* res = c->arena()->Amalloc(size);
1695 return res;
1696 }
1697
1698 // initialization/counting
1699 static int number_of_blocks() {
1700 return Compilation::current()->number_of_blocks();
1701 }
1702
1703 // creation
1704 BlockBegin(int bci)
1705 : StateSplit(illegalType)
1706 , _block_id(Compilation::current()->get_next_block_id())
1707 , _bci(bci)
1708 , _depth_first_number(-1)
1709 , _linear_scan_number(-1)
1710 , _dominator_depth(-1)
1711 , _loop_depth(0)
1712 , _loop_index(-1)
1713 , _flags(0)
1714 , _total_preds(0)
1715 , _stores_to_locals()
1716 , _predecessors(2)
1717 , _dominates(2)
1718 , _dominator(nullptr)
1719 , _end(nullptr)
1720 , _exception_handlers(1)
1721 , _exception_states(nullptr)
1722 , _exception_handler_pco(-1)
1723 , _lir(nullptr)
1724 , _live_in()
1725 , _live_out()
1726 , _live_gen()
1727 , _live_kill()
1728 , _fpu_register_usage()
1729 , _first_lir_instruction_id(-1)
1730 , _last_lir_instruction_id(-1)
1731 {
1732 _block = this;
1733 #ifndef PRODUCT
1734 set_printable_bci(bci);
1735 #endif
1736 }
1737
1738 // accessors
1739 int block_id() const { return _block_id; }
1740 int bci() const { return _bci; }
1741 BlockList* dominates() { return &_dominates; }
1742 BlockBegin* dominator() const { return _dominator; }
1743 int loop_depth() const { return _loop_depth; }
1744 int dominator_depth() const { return _dominator_depth; }
1745 int depth_first_number() const { return _depth_first_number; }
1746 int linear_scan_number() const { return _linear_scan_number; }
1747 BlockEnd* end() const { return _end; }
1748 Label* label() { return &_label; }
1749 LIR_List* lir() const { return _lir; }
1750 int exception_handler_pco() const { return _exception_handler_pco; }
1751 ResourceBitMap& live_in() { return _live_in; }
1752 ResourceBitMap& live_out() { return _live_out; }
1753 ResourceBitMap& live_gen() { return _live_gen; }
1754 ResourceBitMap& live_kill() { return _live_kill; }
1755 ResourceBitMap& fpu_register_usage() { return _fpu_register_usage; }
1756 int first_lir_instruction_id() const { return _first_lir_instruction_id; }
1757 int last_lir_instruction_id() const { return _last_lir_instruction_id; }
1758 int total_preds() const { return _total_preds; }
1759 BitMap& stores_to_locals() { return _stores_to_locals; }
1760
1761 // manipulation
1762 void set_dominator(BlockBegin* dom) { _dominator = dom; }
1763 void set_loop_depth(int d) { _loop_depth = d; }
1764 void set_dominator_depth(int d) { _dominator_depth = d; }
1765 void set_depth_first_number(int dfn) { _depth_first_number = dfn; }
1766 void set_linear_scan_number(int lsn) { _linear_scan_number = lsn; }
1767 void set_end(BlockEnd* new_end);
1768 static void disconnect_edge(BlockBegin* from, BlockBegin* to);
1769 BlockBegin* insert_block_between(BlockBegin* sux);
1770 void substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux);
1771 void set_lir(LIR_List* lir) { _lir = lir; }
1772 void set_exception_handler_pco(int pco) { _exception_handler_pco = pco; }
1773 void set_live_in (const ResourceBitMap& map) { _live_in = map; }
1774 void set_live_out (const ResourceBitMap& map) { _live_out = map; }
1775 void set_live_gen (const ResourceBitMap& map) { _live_gen = map; }
1776 void set_live_kill(const ResourceBitMap& map) { _live_kill = map; }
1777 void set_fpu_register_usage(const ResourceBitMap& map) { _fpu_register_usage = map; }
1778 void set_first_lir_instruction_id(int id) { _first_lir_instruction_id = id; }
1779 void set_last_lir_instruction_id(int id) { _last_lir_instruction_id = id; }
1780 void increment_total_preds(int n = 1) { _total_preds += n; }
1781 void init_stores_to_locals(int locals_count) { _stores_to_locals.initialize(locals_count); }
1782
1783 // generic
1784 virtual void state_values_do(ValueVisitor* f);
1785
1786 // successors and predecessors
1787 int number_of_sux() const;
1788 BlockBegin* sux_at(int i) const;
1789 void add_predecessor(BlockBegin* pred);
1790 void remove_predecessor(BlockBegin* pred);
1791 bool is_predecessor(BlockBegin* pred) const { return _predecessors.contains(pred); }
1792 int number_of_preds() const { return _predecessors.length(); }
1793 BlockBegin* pred_at(int i) const { return _predecessors.at(i); }
1794
1795 // exception handlers potentially invoked by this block
1796 void add_exception_handler(BlockBegin* b);
1797 bool is_exception_handler(BlockBegin* b) const { return _exception_handlers.contains(b); }
1798 int number_of_exception_handlers() const { return _exception_handlers.length(); }
1799 BlockBegin* exception_handler_at(int i) const { return _exception_handlers.at(i); }
1800
1801 // states of the instructions that have an edge to this exception handler
1802 int number_of_exception_states() { assert(is_set(exception_entry_flag), "only for xhandlers"); return _exception_states == nullptr ? 0 : _exception_states->length(); }
1803 ValueStack* exception_state_at(int idx) const { assert(is_set(exception_entry_flag), "only for xhandlers"); return _exception_states->at(idx); }
1804 int add_exception_state(ValueStack* state);
1805
1806 // flags
1807 enum Flag {
1808 no_flag = 0,
1809 std_entry_flag = 1 << 0,
1810 osr_entry_flag = 1 << 1,
1811 exception_entry_flag = 1 << 2,
1812 subroutine_entry_flag = 1 << 3,
1813 backward_branch_target_flag = 1 << 4,
1814 is_on_work_list_flag = 1 << 5,
1815 was_visited_flag = 1 << 6,
1816 parser_loop_header_flag = 1 << 7, // set by parser to identify blocks where phi functions can not be created on demand
1817 critical_edge_split_flag = 1 << 8, // set for all blocks that are introduced when critical edges are split
1818 linear_scan_loop_header_flag = 1 << 9, // set during loop-detection for LinearScan
1819 linear_scan_loop_end_flag = 1 << 10, // set during loop-detection for LinearScan
1820 donot_eliminate_range_checks = 1 << 11 // Should be try to eliminate range checks in this block
1821 };
1822
1823 void set(Flag f) { _flags |= f; }
1824 void clear(Flag f) { _flags &= ~f; }
1825 bool is_set(Flag f) const { return (_flags & f) != 0; }
1826 bool is_entry_block() const {
1827 const int entry_mask = std_entry_flag | osr_entry_flag | exception_entry_flag;
1828 return (_flags & entry_mask) != 0;
1829 }
1830
1831 // iteration
1832 void iterate_preorder (BlockClosure* closure);
1833 void iterate_postorder (BlockClosure* closure);
1834
1835 void block_values_do(ValueVisitor* f);
1836
1837 // loops
1838 void set_loop_index(int ix) { _loop_index = ix; }
1839 int loop_index() const { return _loop_index; }
1840
1841 // merging
1842 bool try_merge(ValueStack* state, bool has_irreducible_loops); // try to merge states at block begin
1843 void merge(ValueStack* state, bool has_irreducible_loops) {
1844 bool b = try_merge(state, has_irreducible_loops);
1845 assert(b, "merge failed");
1846 }
1847
1848 // debugging
1849 void print_block() PRODUCT_RETURN;
1850 void print_block(InstructionPrinter& ip, bool live_only = false) PRODUCT_RETURN;
1851
1852 };
1853
1854
1855 BASE(BlockEnd, StateSplit)
1856 private:
1857 BlockList* _sux;
1858
1859 protected:
1860 BlockList* sux() const { return _sux; }
1861
1862 void set_sux(BlockList* sux) {
1863 #ifdef ASSERT
1864 assert(sux != nullptr, "sux must exist");
1865 for (int i = sux->length() - 1; i >= 0; i--) assert(sux->at(i) != nullptr, "sux must exist");
1866 #endif
1867 _sux = sux;
1868 }
1869
1870 public:
1871 // creation
1872 BlockEnd(ValueType* type, ValueStack* state_before, bool is_safepoint)
1873 : StateSplit(type, state_before)
1874 , _sux(nullptr)
1875 {
1876 set_flag(IsSafepointFlag, is_safepoint);
1877 }
1878
1879 // accessors
1880 bool is_safepoint() const { return check_flag(IsSafepointFlag); }
1881 // For compatibility with old code, for new code use block()
1882 BlockBegin* begin() const { return _block; }
1883
1884 // manipulation
1885 inline void remove_sux_at(int i) { _sux->remove_at(i);}
1886 inline int find_sux(BlockBegin* sux) {return _sux->find(sux);}
1887
1888 // successors
1889 int number_of_sux() const { return _sux != nullptr ? _sux->length() : 0; }
1890 BlockBegin* sux_at(int i) const { return _sux->at(i); }
1891 bool is_sux(BlockBegin* sux) const { return _sux == nullptr ? false : _sux->contains(sux); }
1892 BlockBegin* default_sux() const { return sux_at(number_of_sux() - 1); }
1893 void substitute_sux(BlockBegin* old_sux, BlockBegin* new_sux);
1894 };
1895
1896
1897 LEAF(Goto, BlockEnd)
1898 public:
1899 enum Direction {
1900 none, // Just a regular goto
1901 taken, not_taken // Goto produced from If
1902 };
1903 private:
1904 ciMethod* _profiled_method;
1905 int _profiled_bci;
1906 Direction _direction;
1907 public:
1908 // creation
1909 Goto(BlockBegin* sux, ValueStack* state_before, bool is_safepoint = false)
1910 : BlockEnd(illegalType, state_before, is_safepoint)
1911 , _profiled_method(nullptr)
1912 , _profiled_bci(0)
1913 , _direction(none) {
1914 BlockList* s = new BlockList(1);
1915 s->append(sux);
1916 set_sux(s);
1917 }
1918
1919 Goto(BlockBegin* sux, bool is_safepoint) : BlockEnd(illegalType, nullptr, is_safepoint)
1920 , _profiled_method(nullptr)
1921 , _profiled_bci(0)
1922 , _direction(none) {
1923 BlockList* s = new BlockList(1);
1924 s->append(sux);
1925 set_sux(s);
1926 }
1927
1928 bool should_profile() const { return check_flag(ProfileMDOFlag); }
1929 ciMethod* profiled_method() const { return _profiled_method; } // set only for profiled branches
1930 int profiled_bci() const { return _profiled_bci; }
1931 Direction direction() const { return _direction; }
1932
1933 void set_should_profile(bool value) { set_flag(ProfileMDOFlag, value); }
1934 void set_profiled_method(ciMethod* method) { _profiled_method = method; }
1935 void set_profiled_bci(int bci) { _profiled_bci = bci; }
1936 void set_direction(Direction d) { _direction = d; }
1937 };
1938
1939 #ifdef ASSERT
1940 LEAF(Assert, Instruction)
1941 private:
1942 Value _x;
1943 Condition _cond;
1944 Value _y;
1945 char *_message;
1946
1947 public:
1948 // creation
1949 // unordered_is_true is valid for float/double compares only
1950 Assert(Value x, Condition cond, bool unordered_is_true, Value y);
1951
1952 // accessors
1953 Value x() const { return _x; }
1954 Condition cond() const { return _cond; }
1955 bool unordered_is_true() const { return check_flag(UnorderedIsTrueFlag); }
1956 Value y() const { return _y; }
1957 const char *message() const { return _message; }
1958
1959 // generic
1960 virtual void input_values_do(ValueVisitor* f) { f->visit(&_x); f->visit(&_y); }
1961 };
1962 #endif
1963
1964 LEAF(RangeCheckPredicate, StateSplit)
1965 private:
1966 Value _x;
1967 Condition _cond;
1968 Value _y;
1969
1970 void check_state();
1971
1972 public:
1973 // creation
1974 // unordered_is_true is valid for float/double compares only
1975 RangeCheckPredicate(Value x, Condition cond, bool unordered_is_true, Value y, ValueStack* state) : StateSplit(illegalType)
1976 , _x(x)
1977 , _cond(cond)
1978 , _y(y)
1979 {
1980 ASSERT_VALUES
1981 set_flag(UnorderedIsTrueFlag, unordered_is_true);
1982 assert(x->type()->tag() == y->type()->tag(), "types must match");
1983 this->set_state(state);
1984 check_state();
1985 }
1986
1987 // Always deoptimize
1988 RangeCheckPredicate(ValueStack* state) : StateSplit(illegalType)
1989 {
1990 this->set_state(state);
1991 _x = _y = nullptr;
1992 check_state();
1993 }
1994
1995 // accessors
1996 Value x() const { return _x; }
1997 Condition cond() const { return _cond; }
1998 bool unordered_is_true() const { return check_flag(UnorderedIsTrueFlag); }
1999 Value y() const { return _y; }
2000
2001 void always_fail() { _x = _y = nullptr; }
2002
2003 // generic
2004 virtual void input_values_do(ValueVisitor* f) { StateSplit::input_values_do(f); f->visit(&_x); f->visit(&_y); }
2005 HASHING3(RangeCheckPredicate, true, x()->subst(), y()->subst(), cond())
2006 };
2007
2008 LEAF(If, BlockEnd)
2009 private:
2010 Value _x;
2011 Condition _cond;
2012 Value _y;
2013 ciMethod* _profiled_method;
2014 int _profiled_bci; // Canonicalizer may alter bci of If node
2015 bool _swapped; // Is the order reversed with respect to the original If in the
2016 // bytecode stream?
2017 bool _substitutability_check;
2018 public:
2019 // creation
2020 // unordered_is_true is valid for float/double compares only
2021 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)
2022 : BlockEnd(illegalType, state_before, is_safepoint)
2023 , _x(x)
2024 , _cond(cond)
2025 , _y(y)
2026 , _profiled_method(nullptr)
2027 , _profiled_bci(0)
2028 , _swapped(false)
2029 , _substitutability_check(substitutability_check)
2030 {
2031 ASSERT_VALUES
2032 set_flag(UnorderedIsTrueFlag, unordered_is_true);
2033 assert(x->type()->tag() == y->type()->tag(), "types must match");
2034 BlockList* s = new BlockList(2);
2035 s->append(tsux);
2036 s->append(fsux);
2037 set_sux(s);
2038 }
2039
2040 // accessors
2041 Value x() const { return _x; }
2042 Condition cond() const { return _cond; }
2043 bool unordered_is_true() const { return check_flag(UnorderedIsTrueFlag); }
2044 Value y() const { return _y; }
2045 BlockBegin* sux_for(bool is_true) const { return sux_at(is_true ? 0 : 1); }
2046 BlockBegin* tsux() const { return sux_for(true); }
2047 BlockBegin* fsux() const { return sux_for(false); }
2048 BlockBegin* usux() const { return sux_for(unordered_is_true()); }
2049 bool should_profile() const { return check_flag(ProfileMDOFlag); }
2050 ciMethod* profiled_method() const { return _profiled_method; } // set only for profiled branches
2051 int profiled_bci() const { return _profiled_bci; } // set for profiled branches and tiered
2052 bool is_swapped() const { return _swapped; }
2053
2054 // manipulation
2055 void swap_operands() {
2056 Value t = _x; _x = _y; _y = t;
2057 _cond = mirror(_cond);
2058 }
2059
2060 void set_should_profile(bool value) { set_flag(ProfileMDOFlag, value); }
2061 void set_profiled_method(ciMethod* method) { _profiled_method = method; }
2062 void set_profiled_bci(int bci) { _profiled_bci = bci; }
2063 void set_swapped(bool value) { _swapped = value; }
2064 bool substitutability_check() const { return _substitutability_check; }
2065 // generic
2066 virtual void input_values_do(ValueVisitor* f) { BlockEnd::input_values_do(f); f->visit(&_x); f->visit(&_y); }
2067 };
2068
2069
2070 BASE(Switch, BlockEnd)
2071 private:
2072 Value _tag;
2073
2074 public:
2075 // creation
2076 Switch(Value tag, BlockList* sux, ValueStack* state_before, bool is_safepoint)
2077 : BlockEnd(illegalType, state_before, is_safepoint)
2078 , _tag(tag) {
2079 ASSERT_VALUES
2080 set_sux(sux);
2081 }
2082
2083 // accessors
2084 Value tag() const { return _tag; }
2085 int length() const { return number_of_sux() - 1; }
2086
2087 virtual bool needs_exception_state() const { return false; }
2088
2089 // generic
2090 virtual void input_values_do(ValueVisitor* f) { BlockEnd::input_values_do(f); f->visit(&_tag); }
2091 };
2092
2093
2094 LEAF(TableSwitch, Switch)
2095 private:
2096 int _lo_key;
2097
2098 public:
2099 // creation
2100 TableSwitch(Value tag, BlockList* sux, int lo_key, ValueStack* state_before, bool is_safepoint)
2101 : Switch(tag, sux, state_before, is_safepoint)
2102 , _lo_key(lo_key) { assert(_lo_key <= hi_key(), "integer overflow"); }
2103
2104 // accessors
2105 int lo_key() const { return _lo_key; }
2106 int hi_key() const { return _lo_key + (length() - 1); }
2107 };
2108
2109
2110 LEAF(LookupSwitch, Switch)
2111 private:
2112 intArray* _keys;
2113
2114 public:
2115 // creation
2116 LookupSwitch(Value tag, BlockList* sux, intArray* keys, ValueStack* state_before, bool is_safepoint)
2117 : Switch(tag, sux, state_before, is_safepoint)
2118 , _keys(keys) {
2119 assert(keys != nullptr, "keys must exist");
2120 assert(keys->length() == length(), "sux & keys have incompatible lengths");
2121 }
2122
2123 // accessors
2124 int key_at(int i) const { return _keys->at(i); }
2125 };
2126
2127
2128 LEAF(Return, BlockEnd)
2129 private:
2130 Value _result;
2131
2132 public:
2133 // creation
2134 Return(Value result) :
2135 BlockEnd(result == nullptr ? voidType : result->type()->base(), nullptr, true),
2136 _result(result) {}
2137
2138 // accessors
2139 Value result() const { return _result; }
2140 bool has_result() const { return result() != nullptr; }
2141
2142 // generic
2143 virtual void input_values_do(ValueVisitor* f) {
2144 BlockEnd::input_values_do(f);
2145 if (has_result()) f->visit(&_result);
2146 }
2147 };
2148
2149
2150 LEAF(Throw, BlockEnd)
2151 private:
2152 Value _exception;
2153
2154 public:
2155 // creation
2156 Throw(Value exception, ValueStack* state_before) : BlockEnd(illegalType, state_before, true), _exception(exception) {
2157 ASSERT_VALUES
2158 }
2159
2160 // accessors
2161 Value exception() const { return _exception; }
2162
2163 // generic
2164 virtual bool can_trap() const { return true; }
2165 virtual void input_values_do(ValueVisitor* f) { BlockEnd::input_values_do(f); f->visit(&_exception); }
2166 };
2167
2168
2169 LEAF(Base, BlockEnd)
2170 public:
2171 // creation
2172 Base(BlockBegin* std_entry, BlockBegin* osr_entry) : BlockEnd(illegalType, nullptr, false) {
2173 assert(std_entry->is_set(BlockBegin::std_entry_flag), "std entry must be flagged");
2174 assert(osr_entry == nullptr || osr_entry->is_set(BlockBegin::osr_entry_flag), "osr entry must be flagged");
2175 BlockList* s = new BlockList(2);
2176 if (osr_entry != nullptr) s->append(osr_entry);
2177 s->append(std_entry); // must be default sux!
2178 set_sux(s);
2179 }
2180
2181 // accessors
2182 BlockBegin* std_entry() const { return default_sux(); }
2183 BlockBegin* osr_entry() const { return number_of_sux() < 2 ? nullptr : sux_at(0); }
2184 };
2185
2186
2187 LEAF(OsrEntry, Instruction)
2188 public:
2189 // creation
2190 #ifdef _LP64
2191 OsrEntry() : Instruction(longType) { pin(); }
2192 #else
2193 OsrEntry() : Instruction(intType) { pin(); }
2194 #endif
2195
2196 // generic
2197 virtual void input_values_do(ValueVisitor* f) { }
2198 };
2199
2200
2201 // Models the incoming exception at a catch site
2202 LEAF(ExceptionObject, Instruction)
2203 public:
2204 // creation
2205 ExceptionObject() : Instruction(objectType) {
2206 pin();
2207 }
2208
2209 // generic
2210 virtual void input_values_do(ValueVisitor* f) { }
2211 };
2212
2213
2214 BASE(UnsafeOp, Instruction)
2215 private:
2216 Value _object; // Object to be fetched from or mutated
2217 Value _offset; // Offset within object
2218 bool _is_volatile; // true if volatile - dl/JSR166
2219 BasicType _basic_type; // ValueType can not express byte-sized integers
2220
2221 protected:
2222 // creation
2223 UnsafeOp(BasicType basic_type, Value object, Value offset, bool is_put, bool is_volatile)
2224 : Instruction(is_put ? voidType : as_ValueType(basic_type)),
2225 _object(object), _offset(offset), _is_volatile(is_volatile), _basic_type(basic_type)
2226 {
2227 //Note: Unsafe ops are not not guaranteed to throw NPE.
2228 // Convservatively, Unsafe operations must be pinned though we could be
2229 // looser about this if we wanted to..
2230 pin();
2231 }
2232
2233 public:
2234 // accessors
2235 BasicType basic_type() { return _basic_type; }
2236 Value object() { return _object; }
2237 Value offset() { return _offset; }
2238 bool is_volatile() { return _is_volatile; }
2239
2240 // generic
2241 virtual void input_values_do(ValueVisitor* f) { f->visit(&_object);
2242 f->visit(&_offset); }
2243 };
2244
2245 LEAF(UnsafeGet, UnsafeOp)
2246 private:
2247 bool _is_raw;
2248 public:
2249 UnsafeGet(BasicType basic_type, Value object, Value offset, bool is_volatile)
2250 : UnsafeOp(basic_type, object, offset, false, is_volatile)
2251 {
2252 ASSERT_VALUES
2253 _is_raw = false;
2254 }
2255 UnsafeGet(BasicType basic_type, Value object, Value offset, bool is_volatile, bool is_raw)
2256 : UnsafeOp(basic_type, object, offset, false, is_volatile), _is_raw(is_raw)
2257 {
2258 ASSERT_VALUES
2259 }
2260
2261 // accessors
2262 bool is_raw() { return _is_raw; }
2263 };
2264
2265
2266 LEAF(UnsafePut, UnsafeOp)
2267 private:
2268 Value _value; // Value to be stored
2269 public:
2270 UnsafePut(BasicType basic_type, Value object, Value offset, Value value, bool is_volatile)
2271 : UnsafeOp(basic_type, object, offset, true, is_volatile)
2272 , _value(value)
2273 {
2274 ASSERT_VALUES
2275 }
2276
2277 // accessors
2278 Value value() { return _value; }
2279
2280 // generic
2281 virtual void input_values_do(ValueVisitor* f) { UnsafeOp::input_values_do(f);
2282 f->visit(&_value); }
2283 };
2284
2285 LEAF(UnsafeGetAndSet, UnsafeOp)
2286 private:
2287 Value _value; // Value to be stored
2288 bool _is_add;
2289 public:
2290 UnsafeGetAndSet(BasicType basic_type, Value object, Value offset, Value value, bool is_add)
2291 : UnsafeOp(basic_type, object, offset, false, false)
2292 , _value(value)
2293 , _is_add(is_add)
2294 {
2295 ASSERT_VALUES
2296 }
2297
2298 // accessors
2299 bool is_add() const { return _is_add; }
2300 Value value() { return _value; }
2301
2302 // generic
2303 virtual void input_values_do(ValueVisitor* f) { UnsafeOp::input_values_do(f);
2304 f->visit(&_value); }
2305 };
2306
2307 LEAF(ProfileCall, Instruction)
2308 private:
2309 ciMethod* _method;
2310 int _bci_of_invoke;
2311 ciMethod* _callee; // the method that is called at the given bci
2312 Value _recv;
2313 ciKlass* _known_holder;
2314 Values* _obj_args; // arguments for type profiling
2315 ArgsNonNullState _nonnull_state; // Do we know whether some arguments are never null?
2316 bool _inlined; // Are we profiling a call that is inlined
2317
2318 public:
2319 ProfileCall(ciMethod* method, int bci, ciMethod* callee, Value recv, ciKlass* known_holder, Values* obj_args, bool inlined)
2320 : Instruction(voidType)
2321 , _method(method)
2322 , _bci_of_invoke(bci)
2323 , _callee(callee)
2324 , _recv(recv)
2325 , _known_holder(known_holder)
2326 , _obj_args(obj_args)
2327 , _inlined(inlined)
2328 {
2329 // The ProfileCall has side-effects and must occur precisely where located
2330 pin();
2331 }
2332
2333 ciMethod* method() const { return _method; }
2334 int bci_of_invoke() const { return _bci_of_invoke; }
2335 ciMethod* callee() const { return _callee; }
2336 Value recv() const { return _recv; }
2337 ciKlass* known_holder() const { return _known_holder; }
2338 int nb_profiled_args() const { return _obj_args == nullptr ? 0 : _obj_args->length(); }
2339 Value profiled_arg_at(int i) const { return _obj_args->at(i); }
2340 bool arg_needs_null_check(int i) const {
2341 return _nonnull_state.arg_needs_null_check(i);
2342 }
2343 bool inlined() const { return _inlined; }
2344
2345 void set_arg_needs_null_check(int i, bool check) {
2346 _nonnull_state.set_arg_needs_null_check(i, check);
2347 }
2348
2349 virtual void input_values_do(ValueVisitor* f) {
2350 if (_recv != nullptr) {
2351 f->visit(&_recv);
2352 }
2353 for (int i = 0; i < nb_profiled_args(); i++) {
2354 f->visit(_obj_args->adr_at(i));
2355 }
2356 }
2357 };
2358
2359 LEAF(ProfileReturnType, Instruction)
2360 private:
2361 ciMethod* _method;
2362 ciMethod* _callee;
2363 int _bci_of_invoke;
2364 Value _ret;
2365
2366 public:
2367 ProfileReturnType(ciMethod* method, int bci, ciMethod* callee, Value ret)
2368 : Instruction(voidType)
2369 , _method(method)
2370 , _callee(callee)
2371 , _bci_of_invoke(bci)
2372 , _ret(ret)
2373 {
2374 set_needs_null_check(true);
2375 // The ProfileReturnType has side-effects and must occur precisely where located
2376 pin();
2377 }
2378
2379 ciMethod* method() const { return _method; }
2380 ciMethod* callee() const { return _callee; }
2381 int bci_of_invoke() const { return _bci_of_invoke; }
2382 Value ret() const { return _ret; }
2383
2384 virtual void input_values_do(ValueVisitor* f) {
2385 if (_ret != nullptr) {
2386 f->visit(&_ret);
2387 }
2388 }
2389 };
2390
2391 LEAF(ProfileACmpTypes, Instruction)
2392 private:
2393 ciMethod* _method;
2394 int _bci;
2395 Value _left;
2396 Value _right;
2397 bool _left_maybe_null;
2398 bool _right_maybe_null;
2399
2400 public:
2401 ProfileACmpTypes(ciMethod* method, int bci, Value left, Value right)
2402 : Instruction(voidType)
2403 , _method(method)
2404 , _bci(bci)
2405 , _left(left)
2406 , _right(right)
2407 {
2408 // The ProfileACmp has side-effects and must occur precisely where located
2409 pin();
2410 _left_maybe_null = true;
2411 _right_maybe_null = true;
2412 }
2413
2414 ciMethod* method() const { return _method; }
2415 int bci() const { return _bci; }
2416 Value left() const { return _left; }
2417 Value right() const { return _right; }
2418 bool left_maybe_null() const { return _left_maybe_null; }
2419 bool right_maybe_null() const { return _right_maybe_null; }
2420 void set_left_maybe_null(bool v) { _left_maybe_null = v; }
2421 void set_right_maybe_null(bool v) { _right_maybe_null = v; }
2422
2423 virtual void input_values_do(ValueVisitor* f) {
2424 if (_left != nullptr) {
2425 f->visit(&_left);
2426 }
2427 if (_right != nullptr) {
2428 f->visit(&_right);
2429 }
2430 }
2431 };
2432
2433 // Call some C runtime function that doesn't safepoint,
2434 // optionally passing the current thread as the first argument.
2435 LEAF(RuntimeCall, Instruction)
2436 private:
2437 const char* _entry_name;
2438 address _entry;
2439 Values* _args;
2440 bool _pass_thread; // Pass the JavaThread* as an implicit first argument
2441
2442 public:
2443 RuntimeCall(ValueType* type, const char* entry_name, address entry, Values* args, bool pass_thread = true)
2444 : Instruction(type)
2445 , _entry_name(entry_name)
2446 , _entry(entry)
2447 , _args(args)
2448 , _pass_thread(pass_thread) {
2449 ASSERT_VALUES
2450 pin();
2451 }
2452
2453 const char* entry_name() const { return _entry_name; }
2454 address entry() const { return _entry; }
2455 int number_of_arguments() const { return _args->length(); }
2456 Value argument_at(int i) const { return _args->at(i); }
2457 bool pass_thread() const { return _pass_thread; }
2458
2459 virtual void input_values_do(ValueVisitor* f) {
2460 for (int i = 0; i < _args->length(); i++) f->visit(_args->adr_at(i));
2461 }
2462 };
2463
2464 // Use to trip invocation counter of an inlined method
2465
2466 LEAF(ProfileInvoke, Instruction)
2467 private:
2468 ciMethod* _inlinee;
2469 ValueStack* _state;
2470
2471 public:
2472 ProfileInvoke(ciMethod* inlinee, ValueStack* state)
2473 : Instruction(voidType)
2474 , _inlinee(inlinee)
2475 , _state(state)
2476 {
2477 // The ProfileInvoke has side-effects and must occur precisely where located QQQ???
2478 pin();
2479 }
2480
2481 ciMethod* inlinee() { return _inlinee; }
2482 ValueStack* state() { return _state; }
2483 virtual void input_values_do(ValueVisitor*) {}
2484 virtual void state_values_do(ValueVisitor*);
2485 };
2486
2487 LEAF(MemBar, Instruction)
2488 private:
2489 LIR_Code _code;
2490
2491 public:
2492 MemBar(LIR_Code code)
2493 : Instruction(voidType)
2494 , _code(code)
2495 {
2496 pin();
2497 }
2498
2499 LIR_Code code() { return _code; }
2500
2501 virtual void input_values_do(ValueVisitor*) {}
2502 };
2503
2504 class BlockPair: public CompilationResourceObj {
2505 private:
2506 BlockBegin* _from;
2507 int _index; // sux index of 'to' block
2508 public:
2509 BlockPair(BlockBegin* from, int index): _from(from), _index(index) {}
2510 BlockBegin* from() const { return _from; }
2511 int index() const { return _index; }
2512 };
2513
2514 typedef GrowableArray<BlockPair*> BlockPairList;
2515
2516 inline int BlockBegin::number_of_sux() const { assert(_end != nullptr, "need end"); return _end->number_of_sux(); }
2517 inline BlockBegin* BlockBegin::sux_at(int i) const { assert(_end != nullptr , "need end"); return _end->sux_at(i); }
2518
2519 #undef ASSERT_VALUES
2520
2521 #endif // SHARE_C1_C1_INSTRUCTION_HPP