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 #include "precompiled.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "compiler/compileLog.hpp"
29 #include "interpreter/linkResolver.hpp"
30 #include "jvm_io.h"
31 #include "memory/resourceArea.hpp"
32 #include "memory/universe.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "opto/addnode.hpp"
35 #include "opto/castnode.hpp"
36 #include "opto/convertnode.hpp"
37 #include "opto/divnode.hpp"
38 #include "opto/idealGraphPrinter.hpp"
39 #include "opto/matcher.hpp"
40 #include "opto/memnode.hpp"
41 #include "opto/mulnode.hpp"
42 #include "opto/opaquenode.hpp"
43 #include "opto/parse.hpp"
44 #include "opto/runtime.hpp"
45 #include "runtime/deoptimization.hpp"
46 #include "runtime/sharedRuntime.hpp"
47
48 #ifndef PRODUCT
49 extern uint explicit_null_checks_inserted,
50 explicit_null_checks_elided;
51 #endif
52
53 //---------------------------------array_load----------------------------------
54 void Parse::array_load(BasicType bt) {
55 const Type* elemtype = Type::TOP;
56 bool big_val = bt == T_DOUBLE || bt == T_LONG;
57 Node* adr = array_addressing(bt, 0, elemtype);
58 if (stopped()) return; // guaranteed null or range check
59
60 pop(); // index (already used)
61 Node* array = pop(); // the array itself
62
63 if (elemtype == TypeInt::BOOL) {
64 bt = T_BOOLEAN;
65 }
66 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
67
68 Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
69 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
70 if (big_val) {
71 push_pair(ld);
72 } else {
73 push(ld);
74 }
75 }
76
77
78 //--------------------------------array_store----------------------------------
79 void Parse::array_store(BasicType bt) {
80 const Type* elemtype = Type::TOP;
81 bool big_val = bt == T_DOUBLE || bt == T_LONG;
82 Node* adr = array_addressing(bt, big_val ? 2 : 1, elemtype);
83 if (stopped()) return; // guaranteed null or range check
84 if (bt == T_OBJECT) {
85 array_store_check();
86 if (stopped()) {
87 return;
88 }
89 }
90 Node* val; // Oop to store
91 if (big_val) {
92 val = pop_pair();
93 } else {
94 val = pop();
95 }
96 pop(); // index (already used)
97 Node* array = pop(); // the array itself
98
99 if (elemtype == TypeInt::BOOL) {
100 bt = T_BOOLEAN;
101 }
102 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
103
104 access_store_at(array, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
105 }
106
107
108 //------------------------------array_addressing-------------------------------
109 // Pull array and index from the stack. Compute pointer-to-element.
110 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
111 Node *idx = peek(0+vals); // Get from stack without popping
112 Node *ary = peek(1+vals); // in case of exception
113
114 // Null check the array base, with correct stack contents
115 ary = null_check(ary, T_ARRAY);
116 // Compile-time detect of null-exception?
117 if (stopped()) return top();
118
119 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
120 const TypeInt* sizetype = arytype->size();
121 elemtype = arytype->elem();
122
123 if (UseUniqueSubclasses) {
124 const Type* el = elemtype->make_ptr();
125 if (el && el->isa_instptr()) {
126 const TypeInstPtr* toop = el->is_instptr();
127 if (toop->instance_klass()->unique_concrete_subklass()) {
128 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
129 const Type* subklass = Type::get_const_type(toop->instance_klass());
130 elemtype = subklass->join_speculative(el);
131 }
132 }
133 }
134
135 // Check for big class initializers with all constant offsets
136 // feeding into a known-size array.
137 const TypeInt* idxtype = _gvn.type(idx)->is_int();
138 // See if the highest idx value is less than the lowest array bound,
139 // and if the idx value cannot be negative:
140 bool need_range_check = true;
141 if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
142 need_range_check = false;
143 if (C->log() != nullptr) C->log()->elem("observe that='!need_range_check'");
144 }
145
146 if (!arytype->is_loaded()) {
147 // Only fails for some -Xcomp runs
148 // The class is unloaded. We have to run this bytecode in the interpreter.
149 ciKlass* klass = arytype->unloaded_klass();
150
151 uncommon_trap(Deoptimization::Reason_unloaded,
152 Deoptimization::Action_reinterpret,
153 klass, "!loaded array");
154 return top();
155 }
156
157 // Do the range check
158 if (need_range_check) {
159 Node* tst;
160 if (sizetype->_hi <= 0) {
161 // The greatest array bound is negative, so we can conclude that we're
162 // compiling unreachable code, but the unsigned compare trick used below
163 // only works with non-negative lengths. Instead, hack "tst" to be zero so
164 // the uncommon_trap path will always be taken.
165 tst = _gvn.intcon(0);
166 } else {
167 // Range is constant in array-oop, so we can use the original state of mem
168 Node* len = load_array_length(ary);
169
170 // Test length vs index (standard trick using unsigned compare)
171 Node* chk = _gvn.transform( new CmpUNode(idx, len) );
172 BoolTest::mask btest = BoolTest::lt;
173 tst = _gvn.transform( new BoolNode(chk, btest) );
174 }
175 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
176 _gvn.set_type(rc, rc->Value(&_gvn));
177 if (!tst->is_Con()) {
178 record_for_igvn(rc);
179 }
180 set_control(_gvn.transform(new IfTrueNode(rc)));
181 // Branch to failure if out of bounds
182 {
183 PreserveJVMState pjvms(this);
184 set_control(_gvn.transform(new IfFalseNode(rc)));
185 if (C->allow_range_check_smearing()) {
186 // Do not use builtin_throw, since range checks are sometimes
187 // made more stringent by an optimistic transformation.
188 // This creates "tentative" range checks at this point,
189 // which are not guaranteed to throw exceptions.
190 // See IfNode::Ideal, is_range_check, adjust_check.
191 uncommon_trap(Deoptimization::Reason_range_check,
192 Deoptimization::Action_make_not_entrant,
193 nullptr, "range_check");
194 } else {
195 // If we have already recompiled with the range-check-widening
196 // heroic optimization turned off, then we must really be throwing
197 // range check exceptions.
198 builtin_throw(Deoptimization::Reason_range_check);
199 }
200 }
201 }
202 // Check for always knowing you are throwing a range-check exception
203 if (stopped()) return top();
204
205 // Make array address computation control dependent to prevent it
206 // from floating above the range check during loop optimizations.
207 Node* ptr = array_element_address(ary, idx, type, sizetype, control());
208 assert(ptr != top(), "top should go hand-in-hand with stopped");
209
210 return ptr;
211 }
212
213
214 // returns IfNode
215 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
216 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
217 Node *tst = _gvn.transform(new BoolNode(cmp, mask));
218 IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
219 return iff;
220 }
221
222
223 // sentinel value for the target bci to mark never taken branches
224 // (according to profiling)
225 static const int never_reached = INT_MAX;
226
227 //------------------------------helper for tableswitch-------------------------
228 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
229 // True branch, use existing map info
230 { PreserveJVMState pjvms(this);
231 Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
232 set_control( iftrue );
1425 }
1426 }
1427 }
1428
1429 // False branch
1430 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1431 set_control(iffalse);
1432
1433 if (stopped()) { // Path is dead?
1434 NOT_PRODUCT(explicit_null_checks_elided++);
1435 if (C->eliminate_boxing()) {
1436 // Mark the successor block as parsed
1437 next_block->next_path_num();
1438 }
1439 } else { // Path is live.
1440 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1441 }
1442 }
1443
1444 //------------------------------------do_if------------------------------------
1445 void Parse::do_if(BoolTest::mask btest, Node* c) {
1446 int target_bci = iter().get_dest();
1447
1448 Block* branch_block = successor_for_bci(target_bci);
1449 Block* next_block = successor_for_bci(iter().next_bci());
1450
1451 float cnt;
1452 float prob = branch_prediction(cnt, btest, target_bci, c);
1453 float untaken_prob = 1.0 - prob;
1454
1455 if (prob == PROB_UNKNOWN) {
1456 if (PrintOpto && Verbose) {
1457 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1458 }
1459 repush_if_args(); // to gather stats on loop
1460 uncommon_trap(Deoptimization::Reason_unreached,
1461 Deoptimization::Action_reinterpret,
1462 nullptr, "cold");
1463 if (C->eliminate_boxing()) {
1464 // Mark the successor blocks as parsed
1465 branch_block->next_path_num();
1509 }
1510
1511 // Generate real control flow
1512 float true_prob = (taken_if_true ? prob : untaken_prob);
1513 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1514 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1515 Node* taken_branch = new IfTrueNode(iff);
1516 Node* untaken_branch = new IfFalseNode(iff);
1517 if (!taken_if_true) { // Finish conversion to canonical form
1518 Node* tmp = taken_branch;
1519 taken_branch = untaken_branch;
1520 untaken_branch = tmp;
1521 }
1522
1523 // Branch is taken:
1524 { PreserveJVMState pjvms(this);
1525 taken_branch = _gvn.transform(taken_branch);
1526 set_control(taken_branch);
1527
1528 if (stopped()) {
1529 if (C->eliminate_boxing()) {
1530 // Mark the successor block as parsed
1531 branch_block->next_path_num();
1532 }
1533 } else {
1534 adjust_map_after_if(taken_btest, c, prob, branch_block);
1535 if (!stopped()) {
1536 merge(target_bci);
1537 }
1538 }
1539 }
1540
1541 untaken_branch = _gvn.transform(untaken_branch);
1542 set_control(untaken_branch);
1543
1544 // Branch not taken.
1545 if (stopped()) {
1546 if (C->eliminate_boxing()) {
1547 // Mark the successor block as parsed
1548 next_block->next_path_num();
1549 }
1550 } else {
1551 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1552 }
1553 }
1554
1555 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const {
1556 // Don't want to speculate on uncommon traps when running with -Xcomp
1557 if (!UseInterpreter) {
1558 return false;
1559 }
1560 return seems_never_taken(prob) &&
1561 !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
1562 }
1563
1564 void Parse::maybe_add_predicate_after_if(Block* path) {
1565 if (path->is_SEL_head() && path->preds_parsed() == 0) {
1566 // Add predicates at bci of if dominating the loop so traps can be
1567 // recorded on the if's profile data
1568 int bc_depth = repush_if_args();
1569 add_parse_predicates();
1570 dec_sp(bc_depth);
1571 path->set_has_predicates();
1572 }
1573 }
1574
1575
1576 //----------------------------adjust_map_after_if------------------------------
1577 // Adjust the JVM state to reflect the result of taking this path.
1578 // Basically, it means inspecting the CmpNode controlling this
1579 // branch, seeing how it constrains a tested value, and then
1580 // deciding if it's worth our while to encode this constraint
1581 // as graph nodes in the current abstract interpretation map.
1582 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) {
1583 if (!c->is_Cmp()) {
1584 maybe_add_predicate_after_if(path);
1585 return;
1586 }
1587
1588 if (stopped() || btest == BoolTest::illegal) {
1589 return; // nothing to do
1590 }
1591
1592 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1593
1594 if (path_is_suitable_for_uncommon_trap(prob)) {
1595 repush_if_args();
1596 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
1597 Deoptimization::Action_reinterpret,
1598 nullptr,
1599 (is_fallthrough ? "taken always" : "taken never"));
1600
1601 if (call != nullptr) {
1602 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
1603 }
1604 return;
1605 }
1606
1607 Node* val = c->in(1);
1608 Node* con = c->in(2);
1609 const Type* tcon = _gvn.type(con);
1610 const Type* tval = _gvn.type(val);
1611 bool have_con = tcon->singleton();
1612 if (tval->singleton()) {
1613 if (!have_con) {
1614 // Swap, so constant is in con.
1671 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
1672 // Found:
1673 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1674 // or the narrowOop equivalent.
1675 const Type* obj_type = _gvn.type(obj);
1676 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
1677 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
1678 tboth->higher_equal(obj_type)) {
1679 // obj has to be of the exact type Foo if the CmpP succeeds.
1680 int obj_in_map = map()->find_edge(obj);
1681 JVMState* jvms = this->jvms();
1682 if (obj_in_map >= 0 &&
1683 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1684 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1685 const Type* tcc = ccast->as_Type()->type();
1686 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1687 // Delay transform() call to allow recovery of pre-cast value
1688 // at the control merge.
1689 _gvn.set_type_bottom(ccast);
1690 record_for_igvn(ccast);
1691 // Here's the payoff.
1692 replace_in_map(obj, ccast);
1693 }
1694 }
1695 }
1696 }
1697
1698 int val_in_map = map()->find_edge(val);
1699 if (val_in_map < 0) return; // replace_in_map would be useless
1700 {
1701 JVMState* jvms = this->jvms();
1702 if (!(jvms->is_loc(val_in_map) ||
1703 jvms->is_stk(val_in_map)))
1704 return; // again, it would be useless
1705 }
1706
1707 // Check for a comparison to a constant, and "know" that the compared
1708 // value is constrained on this path.
1709 assert(tcon->singleton(), "");
1710 ConstraintCastNode* ccast = nullptr;
1775 if (c->Opcode() == Op_CmpP &&
1776 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1777 c->in(2)->is_Con()) {
1778 Node* load_klass = nullptr;
1779 Node* decode = nullptr;
1780 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1781 decode = c->in(1);
1782 load_klass = c->in(1)->in(1);
1783 } else {
1784 load_klass = c->in(1);
1785 }
1786 if (load_klass->in(2)->is_AddP()) {
1787 Node* addp = load_klass->in(2);
1788 Node* obj = addp->in(AddPNode::Address);
1789 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1790 if (obj_type->speculative_type_not_null() != nullptr) {
1791 ciKlass* k = obj_type->speculative_type();
1792 inc_sp(2);
1793 obj = maybe_cast_profiled_obj(obj, k);
1794 dec_sp(2);
1795 // Make the CmpP use the casted obj
1796 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1797 load_klass = load_klass->clone();
1798 load_klass->set_req(2, addp);
1799 load_klass = _gvn.transform(load_klass);
1800 if (decode != nullptr) {
1801 decode = decode->clone();
1802 decode->set_req(1, load_klass);
1803 load_klass = _gvn.transform(decode);
1804 }
1805 c = c->clone();
1806 c->set_req(1, load_klass);
1807 c = _gvn.transform(c);
1808 }
1809 }
1810 }
1811 return c;
1812 }
1813
1814 //------------------------------do_one_bytecode--------------------------------
2621 // See if we can get some profile data and hand it off to the next block
2622 Block *target_block = block()->successor_for_bci(target_bci);
2623 if (target_block->pred_count() != 1) break;
2624 ciMethodData* methodData = method()->method_data();
2625 if (!methodData->is_mature()) break;
2626 ciProfileData* data = methodData->bci_to_data(bci());
2627 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2628 int taken = ((ciJumpData*)data)->taken();
2629 taken = method()->scale_count(taken);
2630 target_block->set_count(taken);
2631 break;
2632 }
2633
2634 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
2635 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2636 handle_if_null:
2637 // If this is a backwards branch in the bytecodes, add Safepoint
2638 maybe_add_safepoint(iter().get_dest());
2639 a = null();
2640 b = pop();
2641 if (!_gvn.type(b)->speculative_maybe_null() &&
2642 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2643 inc_sp(1);
2644 Node* null_ctl = top();
2645 b = null_check_oop(b, &null_ctl, true, true, true);
2646 assert(null_ctl->is_top(), "no null control here");
2647 dec_sp(1);
2648 } else if (_gvn.type(b)->speculative_always_null() &&
2649 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2650 inc_sp(1);
2651 b = null_assert(b);
2652 dec_sp(1);
2653 }
2654 c = _gvn.transform( new CmpPNode(b, a) );
2655 do_ifnull(btest, c);
2656 break;
2657
2658 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2659 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2660 handle_if_acmp:
2661 // If this is a backwards branch in the bytecodes, add Safepoint
2662 maybe_add_safepoint(iter().get_dest());
2663 a = pop();
2664 b = pop();
2665 c = _gvn.transform( new CmpPNode(b, a) );
2666 c = optimize_cmp_with_klass(c);
2667 do_if(btest, c);
2668 break;
2669
2670 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2671 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2672 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2673 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2674 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2675 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2676 handle_ifxx:
2677 // If this is a backwards branch in the bytecodes, add Safepoint
2678 maybe_add_safepoint(iter().get_dest());
2679 a = _gvn.intcon(0);
2680 b = pop();
2681 c = _gvn.transform( new CmpINode(b, a) );
2682 do_if(btest, c);
2683 break;
2684
2685 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2686 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2687 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2702 break;
2703
2704 case Bytecodes::_lookupswitch:
2705 do_lookupswitch();
2706 break;
2707
2708 case Bytecodes::_invokestatic:
2709 case Bytecodes::_invokedynamic:
2710 case Bytecodes::_invokespecial:
2711 case Bytecodes::_invokevirtual:
2712 case Bytecodes::_invokeinterface:
2713 do_call();
2714 break;
2715 case Bytecodes::_checkcast:
2716 do_checkcast();
2717 break;
2718 case Bytecodes::_instanceof:
2719 do_instanceof();
2720 break;
2721 case Bytecodes::_anewarray:
2722 do_anewarray();
2723 break;
2724 case Bytecodes::_newarray:
2725 do_newarray((BasicType)iter().get_index());
2726 break;
2727 case Bytecodes::_multianewarray:
2728 do_multianewarray();
2729 break;
2730 case Bytecodes::_new:
2731 do_new();
2732 break;
2733
2734 case Bytecodes::_jsr:
2735 case Bytecodes::_jsr_w:
2736 do_jsr();
2737 break;
2738
2739 case Bytecodes::_ret:
2740 do_ret();
2741 break;
2742
|
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 #include "precompiled.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "ci/ciSymbols.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileLog.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "jvm_io.h"
32 #include "memory/resourceArea.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "opto/addnode.hpp"
36 #include "opto/castnode.hpp"
37 #include "opto/convertnode.hpp"
38 #include "opto/divnode.hpp"
39 #include "opto/idealGraphPrinter.hpp"
40 #include "opto/idealKit.hpp"
41 #include "opto/inlinetypenode.hpp"
42 #include "opto/matcher.hpp"
43 #include "opto/memnode.hpp"
44 #include "opto/mulnode.hpp"
45 #include "opto/opaquenode.hpp"
46 #include "opto/parse.hpp"
47 #include "opto/runtime.hpp"
48 #include "runtime/deoptimization.hpp"
49 #include "runtime/sharedRuntime.hpp"
50
51 #ifndef PRODUCT
52 extern uint explicit_null_checks_inserted,
53 explicit_null_checks_elided;
54 #endif
55
56 Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) {
57 // Feed unused profile data to type speculation
58 if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
59 ciKlass* array_type = nullptr;
60 ciKlass* element_type = nullptr;
61 ProfilePtrKind element_ptr = ProfileMaybeNull;
62 bool flat_array = true;
63 bool null_free_array = true;
64 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
65 if (element_type != nullptr || element_ptr != ProfileMaybeNull) {
66 ld = record_profile_for_speculation(ld, element_type, element_ptr);
67 }
68 }
69 return ld;
70 }
71
72
73 //---------------------------------array_load----------------------------------
74 void Parse::array_load(BasicType bt) {
75 const Type* elemtype = Type::TOP;
76 Node* adr = array_addressing(bt, 0, elemtype);
77 if (stopped()) return; // guaranteed null or range check
78
79 Node* idx = pop();
80 Node* ary = pop();
81
82 // Handle inline type arrays
83 const TypeOopPtr* elemptr = elemtype->make_oopptr();
84 const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
85 if (ary_t->is_flat()) {
86 // Load from flat inline type array
87 Node* vt = InlineTypeNode::make_from_flat(this, elemtype->inline_klass(), ary, adr);
88 push(vt);
89 return;
90 } else if (ary_t->is_null_free()) {
91 // Load from non-flat inline type array (elements can never be null)
92 bt = T_OBJECT;
93 } else if (!ary_t->is_not_flat()) {
94 // Cannot statically determine if array is a flat array, emit runtime check
95 assert(UseFlatArray && is_reference_type(bt) && elemptr->can_be_inline_type() && !ary_t->is_not_null_free() &&
96 (!elemptr->is_inlinetypeptr() || elemptr->inline_klass()->flat_in_array()), "array can't be flat");
97 IdealKit ideal(this);
98 IdealVariable res(ideal);
99 ideal.declarations_done();
100 ideal.if_then(flat_array_test(ary, /* flat = */ false)); {
101 // non-flat array
102 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
103 sync_kit(ideal);
104 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
105 DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
106 if (needs_range_check(ary_t->size(), idx)) {
107 // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
108 // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
109 // possibly float above the range check at any point.
110 decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
111 }
112 Node* ld = access_load_at(ary, adr, adr_type, elemptr, bt, decorator_set);
113 if (elemptr->is_inlinetypeptr()) {
114 assert(elemptr->maybe_null(), "null free array should be handled above");
115 ld = InlineTypeNode::make_from_oop(this, ld, elemptr->inline_klass(), false);
116 }
117 ideal.sync_kit(this);
118 ideal.set(res, ld);
119 } ideal.else_(); {
120 // flat array
121 sync_kit(ideal);
122 if (elemptr->is_inlinetypeptr()) {
123 // Element type is known, cast and load from flat representation
124 ciInlineKlass* vk = elemptr->inline_klass();
125 assert(vk->flat_in_array() && elemptr->maybe_null(), "never/always flat - should be optimized");
126 ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true);
127 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
128 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, arytype));
129 Node* casted_adr = array_element_address(cast, idx, T_OBJECT, ary_t->size(), control());
130 // Re-execute flat array load if buffering triggers deoptimization
131 PreserveReexecuteState preexecs(this);
132 jvms()->set_should_reexecute(true);
133 inc_sp(2);
134 Node* vt = InlineTypeNode::make_from_flat(this, vk, cast, casted_adr)->buffer(this, false);
135 ideal.set(res, vt);
136 ideal.sync_kit(this);
137 } else {
138 // Element type is unknown, emit runtime call
139
140 // Below membars keep this access to an unknown flat array correctly
141 // ordered with other unknown and known flat array accesses.
142 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
143
144 Node* call = nullptr;
145 {
146 // Re-execute flat array load if runtime call triggers deoptimization
147 PreserveReexecuteState preexecs(this);
148 jvms()->set_bci(_bci);
149 jvms()->set_should_reexecute(true);
150 inc_sp(2);
151 kill_dead_locals();
152 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
153 OptoRuntime::load_unknown_inline_type(),
154 OptoRuntime::load_unknown_inline_Java(),
155 nullptr, TypeRawPtr::BOTTOM,
156 ary, idx);
157 }
158 make_slow_call_ex(call, env()->Throwable_klass(), false);
159 Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
160
161 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
162
163 // Keep track of the information that the inline type is in flat arrays
164 const Type* unknown_value = elemptr->is_instptr()->cast_to_flat_in_array();
165 buffer = _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
166
167 ideal.sync_kit(this);
168 ideal.set(res, buffer);
169 }
170 } ideal.end_if();
171 sync_kit(ideal);
172 Node* ld = _gvn.transform(ideal.value(res));
173 ld = record_profile_for_speculation_at_array_load(ld);
174 push_node(bt, ld);
175 return;
176 }
177
178 if (elemtype == TypeInt::BOOL) {
179 bt = T_BOOLEAN;
180 }
181 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
182 Node* ld = access_load_at(ary, adr, adr_type, elemtype, bt,
183 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
184 ld = record_profile_for_speculation_at_array_load(ld);
185 // Loading an inline type from a non-flat array
186 if (elemptr != nullptr && elemptr->is_inlinetypeptr()) {
187 assert(!ary_t->is_null_free() || !elemptr->maybe_null(), "inline type array elements should never be null");
188 ld = InlineTypeNode::make_from_oop(this, ld, elemptr->inline_klass(), !elemptr->maybe_null());
189 }
190 push_node(bt, ld);
191 }
192
193
194 //--------------------------------array_store----------------------------------
195 void Parse::array_store(BasicType bt) {
196 const Type* elemtype = Type::TOP;
197 Node* adr = array_addressing(bt, type2size[bt], elemtype);
198 if (stopped()) return; // guaranteed null or range check
199 Node* cast_val = nullptr;
200 if (bt == T_OBJECT) {
201 cast_val = array_store_check(adr, elemtype);
202 if (stopped()) return;
203 }
204 Node* val = pop_node(bt); // Value to store
205 Node* idx = pop(); // Index in the array
206 Node* ary = pop(); // The array itself
207
208 const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
209 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
210
211 if (elemtype == TypeInt::BOOL) {
212 bt = T_BOOLEAN;
213 } else if (bt == T_OBJECT) {
214 elemtype = elemtype->make_oopptr();
215 const Type* tval = _gvn.type(cast_val);
216 // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
217 // This is only legal for non-null stores because the array_store_check always passes for null, even
218 // if the array is null-free. Null stores are handled in GraphKit::gen_inline_array_null_guard().
219 bool not_null_free = !tval->maybe_null() && !tval->is_oopptr()->can_be_inline_type();
220 bool not_flat = not_null_free || (tval->is_inlinetypeptr() && !tval->inline_klass()->flat_in_array());
221 if (!ary_t->is_not_null_free() && not_null_free) {
222 // Storing a non-inline type, mark array as not null-free (-> not flat).
223 ary_t = ary_t->cast_to_not_null_free();
224 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t));
225 replace_in_map(ary, cast);
226 ary = cast;
227 } else if (!ary_t->is_not_flat() && not_flat) {
228 // Storing to a non-flat array, mark array as not flat.
229 ary_t = ary_t->cast_to_not_flat();
230 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t));
231 replace_in_map(ary, cast);
232 ary = cast;
233 }
234
235 if (ary_t->is_flat()) {
236 // Store to flat inline type array
237 assert(!tval->maybe_null(), "should be guaranteed by array store check");
238 // Re-execute flat array store if buffering triggers deoptimization
239 PreserveReexecuteState preexecs(this);
240 inc_sp(3);
241 jvms()->set_should_reexecute(true);
242 cast_val->as_InlineType()->store_flat(this, ary, adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
243 return;
244 } else if (ary_t->is_null_free()) {
245 // Store to non-flat inline type array (elements can never be null)
246 assert(!tval->maybe_null(), "should be guaranteed by array store check");
247 if (elemtype->inline_klass()->is_empty()) {
248 // Ignore empty inline stores, array is already initialized.
249 return;
250 }
251 } else if (!ary_t->is_not_flat() && (tval != TypePtr::NULL_PTR || StressReflectiveCode)) {
252 // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient).
253 assert(UseFlatArray && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
254 !ary_t->klass_is_exact() && !ary_t->is_not_null_free(), "array can't be a flat array");
255 IdealKit ideal(this);
256 ideal.if_then(flat_array_test(ary, /* flat = */ false)); {
257 // non-flat array
258 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
259 sync_kit(ideal);
260 Node* cast_ary = inline_array_null_guard(ary, cast_val, 3);
261 inc_sp(3);
262 access_store_at(cast_ary, adr, adr_type, cast_val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
263 dec_sp(3);
264 ideal.sync_kit(this);
265 } ideal.else_(); {
266 sync_kit(ideal);
267 // flat array
268 Node* null_ctl = top();
269 Node* val = null_check_oop(cast_val, &null_ctl);
270 if (null_ctl != top()) {
271 PreserveJVMState pjvms(this);
272 inc_sp(3);
273 set_control(null_ctl);
274 uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
275 dec_sp(3);
276 }
277 // Try to determine the inline klass
278 ciInlineKlass* vk = nullptr;
279 if (tval->is_inlinetypeptr()) {
280 vk = tval->inline_klass();
281 } else if (elemtype->is_inlinetypeptr()) {
282 vk = elemtype->inline_klass();
283 }
284 Node* casted_ary = ary;
285 if (vk != nullptr && !stopped()) {
286 // Element type is known, cast and store to flat representation
287 assert(vk->flat_in_array() && elemtype->maybe_null(), "never/always flat - should be optimized");
288 ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true);
289 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
290 casted_ary = _gvn.transform(new CheckCastPPNode(control(), casted_ary, arytype));
291 Node* casted_adr = array_element_address(casted_ary, idx, T_OBJECT, arytype->size(), control());
292 if (!val->is_InlineType()) {
293 assert(!gvn().type(val)->maybe_null(), "inline type array elements should never be null");
294 val = InlineTypeNode::make_from_oop(this, val, vk);
295 }
296 // Re-execute flat array store if buffering triggers deoptimization
297 PreserveReexecuteState preexecs(this);
298 inc_sp(3);
299 jvms()->set_should_reexecute(true);
300 val->as_InlineType()->store_flat(this, casted_ary, casted_adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
301 } else if (!stopped()) {
302 // Element type is unknown, emit runtime call
303
304 // Below membars keep this access to an unknown flat array correctly
305 // ordered with other unknown and known flat array accesses.
306 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
307
308 make_runtime_call(RC_LEAF,
309 OptoRuntime::store_unknown_inline_type(),
310 CAST_FROM_FN_PTR(address, OptoRuntime::store_unknown_inline),
311 "store_unknown_inline", TypeRawPtr::BOTTOM,
312 val, casted_ary, idx);
313
314 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
315 }
316 ideal.sync_kit(this);
317 }
318 ideal.end_if();
319 sync_kit(ideal);
320 return;
321 } else if (!ary_t->is_not_null_free()) {
322 // Array is not flat but may be null free
323 assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
324 ary = inline_array_null_guard(ary, cast_val, 3, true);
325 }
326 }
327 inc_sp(3);
328 access_store_at(ary, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
329 dec_sp(3);
330 }
331
332
333 //------------------------------array_addressing-------------------------------
334 // Pull array and index from the stack. Compute pointer-to-element.
335 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
336 Node *idx = peek(0+vals); // Get from stack without popping
337 Node *ary = peek(1+vals); // in case of exception
338
339 // Null check the array base, with correct stack contents
340 ary = null_check(ary, T_ARRAY);
341 // Compile-time detect of null-exception?
342 if (stopped()) return top();
343
344 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
345 const TypeInt* sizetype = arytype->size();
346 elemtype = arytype->elem();
347
348 if (UseUniqueSubclasses) {
349 const Type* el = elemtype->make_ptr();
350 if (el && el->isa_instptr()) {
351 const TypeInstPtr* toop = el->is_instptr();
352 if (toop->instance_klass()->unique_concrete_subklass()) {
353 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
354 const Type* subklass = Type::get_const_type(toop->instance_klass());
355 elemtype = subklass->join_speculative(el);
356 }
357 }
358 }
359
360 if (!arytype->is_loaded()) {
361 // Only fails for some -Xcomp runs
362 // The class is unloaded. We have to run this bytecode in the interpreter.
363 ciKlass* klass = arytype->unloaded_klass();
364
365 uncommon_trap(Deoptimization::Reason_unloaded,
366 Deoptimization::Action_reinterpret,
367 klass, "!loaded array");
368 return top();
369 }
370
371 ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);
372
373 if (needs_range_check(sizetype, idx)) {
374 create_range_check(idx, ary, sizetype);
375 } else if (C->log() != nullptr) {
376 C->log()->elem("observe that='!need_range_check'");
377 }
378
379 // Check for always knowing you are throwing a range-check exception
380 if (stopped()) return top();
381
382 // Make array address computation control dependent to prevent it
383 // from floating above the range check during loop optimizations.
384 Node* ptr = array_element_address(ary, idx, type, sizetype, control());
385 assert(ptr != top(), "top should go hand-in-hand with stopped");
386
387 return ptr;
388 }
389
390 // Check if we need a range check for an array access. This is the case if the index is either negative or if it could
391 // be greater or equal the smallest possible array size (i.e. out-of-bounds).
392 bool Parse::needs_range_check(const TypeInt* size_type, const Node* index) const {
393 const TypeInt* index_type = _gvn.type(index)->is_int();
394 return index_type->_hi >= size_type->_lo || index_type->_lo < 0;
395 }
396
397 void Parse::create_range_check(Node* idx, Node* ary, const TypeInt* sizetype) {
398 Node* tst;
399 if (sizetype->_hi <= 0) {
400 // The greatest array bound is negative, so we can conclude that we're
401 // compiling unreachable code, but the unsigned compare trick used below
402 // only works with non-negative lengths. Instead, hack "tst" to be zero so
403 // the uncommon_trap path will always be taken.
404 tst = _gvn.intcon(0);
405 } else {
406 // Range is constant in array-oop, so we can use the original state of mem
407 Node* len = load_array_length(ary);
408
409 // Test length vs index (standard trick using unsigned compare)
410 Node* chk = _gvn.transform(new CmpUNode(idx, len) );
411 BoolTest::mask btest = BoolTest::lt;
412 tst = _gvn.transform(new BoolNode(chk, btest) );
413 }
414 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
415 _gvn.set_type(rc, rc->Value(&_gvn));
416 if (!tst->is_Con()) {
417 record_for_igvn(rc);
418 }
419 set_control(_gvn.transform(new IfTrueNode(rc)));
420 // Branch to failure if out of bounds
421 {
422 PreserveJVMState pjvms(this);
423 set_control(_gvn.transform(new IfFalseNode(rc)));
424 if (C->allow_range_check_smearing()) {
425 // Do not use builtin_throw, since range checks are sometimes
426 // made more stringent by an optimistic transformation.
427 // This creates "tentative" range checks at this point,
428 // which are not guaranteed to throw exceptions.
429 // See IfNode::Ideal, is_range_check, adjust_check.
430 uncommon_trap(Deoptimization::Reason_range_check,
431 Deoptimization::Action_make_not_entrant,
432 nullptr, "range_check");
433 } else {
434 // If we have already recompiled with the range-check-widening
435 // heroic optimization turned off, then we must really be throwing
436 // range check exceptions.
437 builtin_throw(Deoptimization::Reason_range_check);
438 }
439 }
440 }
441
442 // For inline type arrays, we can use the profiling information for array accesses to speculate on the type, flatness,
443 // and null-freeness. We can either prepare the speculative type for later uses or emit explicit speculative checks with
444 // traps now. In the latter case, the speculative type guarantees can avoid additional runtime checks later (e.g.
445 // non-null-free implies non-flat which allows us to remove flatness checks). This makes the graph simpler.
446 Node* Parse::create_speculative_inline_type_array_checks(Node* array, const TypeAryPtr* array_type,
447 const Type*& element_type) {
448 if (!array_type->is_flat() && !array_type->is_not_flat()) {
449 // For arrays that might be flat, speculate that the array has the exact type reported in the profile data such that
450 // we can rely on a fixed memory layout (i.e. either a flat layout or not).
451 array = cast_to_speculative_array_type(array, array_type, element_type);
452 } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
453 // Array is known to be either flat or not flat. If possible, update the speculative type by using the profile data
454 // at this bci.
455 array = cast_to_profiled_array_type(array);
456 }
457
458 // Even though the type does not tell us whether we have an inline type array or not, we can still check the profile data
459 // whether we have a non-null-free or non-flat array. Since non-null-free implies non-flat, we check this first.
460 // Speculating on a non-null-free array doesn't help aaload but could be profitable for a subsequent aastore.
461 if (!array_type->is_null_free() && !array_type->is_not_null_free()) {
462 array = speculate_non_null_free_array(array, array_type);
463 }
464
465 if (!array_type->is_flat() && !array_type->is_not_flat()) {
466 array = speculate_non_flat_array(array, array_type);
467 }
468 return array;
469 }
470
471 // Speculate that the array has the exact type reported in the profile data. We emit a trap when this turns out to be
472 // wrong. On the fast path, we add a CheckCastPP to use the exact type.
473 Node* Parse::cast_to_speculative_array_type(Node* const array, const TypeAryPtr*& array_type, const Type*& element_type) {
474 Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
475 ciKlass* speculative_array_type = array_type->speculative_type();
476 if (too_many_traps_or_recompiles(reason) || speculative_array_type == nullptr) {
477 // No speculative type, check profile data at this bci
478 speculative_array_type = nullptr;
479 reason = Deoptimization::Reason_class_check;
480 if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
481 ciKlass* profiled_element_type = nullptr;
482 ProfilePtrKind element_ptr = ProfileMaybeNull;
483 bool flat_array = true;
484 bool null_free_array = true;
485 method()->array_access_profiled_type(bci(), speculative_array_type, profiled_element_type, element_ptr, flat_array,
486 null_free_array);
487 }
488 }
489 if (speculative_array_type != nullptr) {
490 // Speculate that this array has the exact type reported by profile data
491 Node* casted_array = nullptr;
492 DEBUG_ONLY(Node* old_control = control();)
493 Node* slow_ctl = type_check_receiver(array, speculative_array_type, 1.0, &casted_array);
494 if (stopped()) {
495 // The check always fails and therefore profile information is incorrect. Don't use it.
496 assert(old_control == slow_ctl, "type check should have been removed");
497 set_control(slow_ctl);
498 } else if (!slow_ctl->is_top()) {
499 { PreserveJVMState pjvms(this);
500 set_control(slow_ctl);
501 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
502 }
503 replace_in_map(array, casted_array);
504 array_type = _gvn.type(casted_array)->is_aryptr();
505 element_type = array_type->elem();
506 return casted_array;
507 }
508 }
509 return array;
510 }
511
512 // Create a CheckCastPP when the speculative type can improve the current type.
513 Node* Parse::cast_to_profiled_array_type(Node* const array) {
514 ciKlass* array_type = nullptr;
515 ciKlass* element_type = nullptr;
516 ProfilePtrKind element_ptr = ProfileMaybeNull;
517 bool flat_array = true;
518 bool null_free_array = true;
519 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
520 if (array_type != nullptr) {
521 return record_profile_for_speculation(array, array_type, ProfileMaybeNull);
522 }
523 return array;
524 }
525
526 // Speculate that the array is non-null-free. This will imply non-flatness. We emit a trap when this turns out to be
527 // wrong. On the fast path, we add a CheckCastPP to use the non-null-free type.
528 Node* Parse::speculate_non_null_free_array(Node* const array, const TypeAryPtr*& array_type) {
529 bool null_free_array = true;
530 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
531 if (array_type->speculative() != nullptr &&
532 array_type->speculative()->is_aryptr()->is_not_null_free() &&
533 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
534 null_free_array = false;
535 reason = Deoptimization::Reason_speculate_class_check;
536 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
537 ciKlass* profiled_array_type = nullptr;
538 ciKlass* profiled_element_type = nullptr;
539 ProfilePtrKind element_ptr = ProfileMaybeNull;
540 bool flat_array = true;
541 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
542 null_free_array);
543 reason = Deoptimization::Reason_class_check;
544 }
545 if (!null_free_array) {
546 { // Deoptimize if null-free array
547 BuildCutout unless(this, null_free_array_test(array, /* null_free = */ false), PROB_MAX);
548 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
549 }
550 assert(!stopped(), "null-free array should have been caught earlier");
551 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_null_free()));
552 replace_in_map(array, casted_array);
553 array_type = _gvn.type(casted_array)->is_aryptr();
554 return casted_array;
555 }
556 return array;
557 }
558
559 // Speculate that the array is non-flat. We emit a trap when this turns out to be wrong. On the fast path, we add a
560 // CheckCastPP to use the non-flat type.
561 Node* Parse::speculate_non_flat_array(Node* const array, const TypeAryPtr* const array_type) {
562 bool flat_array = true;
563 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
564 if (array_type->speculative() != nullptr &&
565 array_type->speculative()->is_aryptr()->is_not_flat() &&
566 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
567 flat_array = false;
568 reason = Deoptimization::Reason_speculate_class_check;
569 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
570 ciKlass* profiled_array_type = nullptr;
571 ciKlass* profiled_element_type = nullptr;
572 ProfilePtrKind element_ptr = ProfileMaybeNull;
573 bool null_free_array = true;
574 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
575 null_free_array);
576 reason = Deoptimization::Reason_class_check;
577 }
578 if (!flat_array) {
579 { // Deoptimize if flat array
580 BuildCutout unless(this, flat_array_test(array, /* flat = */ false), PROB_MAX);
581 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
582 }
583 assert(!stopped(), "flat array should have been caught earlier");
584 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_flat()));
585 replace_in_map(array, casted_array);
586 return casted_array;
587 }
588 return array;
589 }
590
591 // returns IfNode
592 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
593 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
594 Node *tst = _gvn.transform(new BoolNode(cmp, mask));
595 IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
596 return iff;
597 }
598
599
600 // sentinel value for the target bci to mark never taken branches
601 // (according to profiling)
602 static const int never_reached = INT_MAX;
603
604 //------------------------------helper for tableswitch-------------------------
605 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
606 // True branch, use existing map info
607 { PreserveJVMState pjvms(this);
608 Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
609 set_control( iftrue );
1802 }
1803 }
1804 }
1805
1806 // False branch
1807 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1808 set_control(iffalse);
1809
1810 if (stopped()) { // Path is dead?
1811 NOT_PRODUCT(explicit_null_checks_elided++);
1812 if (C->eliminate_boxing()) {
1813 // Mark the successor block as parsed
1814 next_block->next_path_num();
1815 }
1816 } else { // Path is live.
1817 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1818 }
1819 }
1820
1821 //------------------------------------do_if------------------------------------
1822 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken) {
1823 int target_bci = iter().get_dest();
1824
1825 Block* branch_block = successor_for_bci(target_bci);
1826 Block* next_block = successor_for_bci(iter().next_bci());
1827
1828 float cnt;
1829 float prob = branch_prediction(cnt, btest, target_bci, c);
1830 float untaken_prob = 1.0 - prob;
1831
1832 if (prob == PROB_UNKNOWN) {
1833 if (PrintOpto && Verbose) {
1834 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1835 }
1836 repush_if_args(); // to gather stats on loop
1837 uncommon_trap(Deoptimization::Reason_unreached,
1838 Deoptimization::Action_reinterpret,
1839 nullptr, "cold");
1840 if (C->eliminate_boxing()) {
1841 // Mark the successor blocks as parsed
1842 branch_block->next_path_num();
1886 }
1887
1888 // Generate real control flow
1889 float true_prob = (taken_if_true ? prob : untaken_prob);
1890 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1891 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1892 Node* taken_branch = new IfTrueNode(iff);
1893 Node* untaken_branch = new IfFalseNode(iff);
1894 if (!taken_if_true) { // Finish conversion to canonical form
1895 Node* tmp = taken_branch;
1896 taken_branch = untaken_branch;
1897 untaken_branch = tmp;
1898 }
1899
1900 // Branch is taken:
1901 { PreserveJVMState pjvms(this);
1902 taken_branch = _gvn.transform(taken_branch);
1903 set_control(taken_branch);
1904
1905 if (stopped()) {
1906 if (C->eliminate_boxing() && !new_path) {
1907 // Mark the successor block as parsed (if we haven't created a new path)
1908 branch_block->next_path_num();
1909 }
1910 } else {
1911 adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1912 if (!stopped()) {
1913 if (new_path) {
1914 // Merge by using a new path
1915 merge_new_path(target_bci);
1916 } else if (ctrl_taken != nullptr) {
1917 // Don't merge but save taken branch to be wired by caller
1918 *ctrl_taken = control();
1919 } else {
1920 merge(target_bci);
1921 }
1922 }
1923 }
1924 }
1925
1926 untaken_branch = _gvn.transform(untaken_branch);
1927 set_control(untaken_branch);
1928
1929 // Branch not taken.
1930 if (stopped() && ctrl_taken == nullptr) {
1931 if (C->eliminate_boxing()) {
1932 // Mark the successor block as parsed (if caller does not re-wire control flow)
1933 next_block->next_path_num();
1934 }
1935 } else {
1936 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
1937 }
1938 }
1939
1940
1941 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
1942 if (t->speculative() == nullptr) {
1943 return ProfileUnknownNull;
1944 }
1945 if (t->speculative_always_null()) {
1946 return ProfileAlwaysNull;
1947 }
1948 if (t->speculative_maybe_null()) {
1949 return ProfileMaybeNull;
1950 }
1951 return ProfileNeverNull;
1952 }
1953
1954 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
1955 inc_sp(2);
1956 Node* cast = null_check_common(input, T_OBJECT, true, nullptr,
1957 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
1958 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
1959 dec_sp(2);
1960 if (btest == BoolTest::ne) {
1961 {
1962 PreserveJVMState pjvms(this);
1963 replace_in_map(input, cast);
1964 int target_bci = iter().get_dest();
1965 merge(target_bci);
1966 }
1967 record_for_igvn(eq_region);
1968 set_control(_gvn.transform(eq_region));
1969 } else {
1970 replace_in_map(input, cast);
1971 }
1972 }
1973
1974 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
1975 inc_sp(2);
1976 null_ctl = top();
1977 Node* cast = null_check_oop(input, &null_ctl,
1978 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
1979 false,
1980 speculative_ptr_kind(tinput) == ProfileNeverNull &&
1981 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
1982 dec_sp(2);
1983 assert(!stopped(), "null input should have been caught earlier");
1984 return cast;
1985 }
1986
1987 void Parse::acmp_known_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, ciKlass* input_type, BoolTest::mask btest, Node* eq_region) {
1988 Node* ne_region = new RegionNode(1);
1989 Node* null_ctl;
1990 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
1991 ne_region->add_req(null_ctl);
1992
1993 Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast);
1994 {
1995 PreserveJVMState pjvms(this);
1996 inc_sp(2);
1997 set_control(slow_ctl);
1998 Deoptimization::DeoptReason reason;
1999 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2000 reason = Deoptimization::Reason_speculate_class_check;
2001 } else {
2002 reason = Deoptimization::Reason_class_check;
2003 }
2004 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2005 }
2006 ne_region->add_req(control());
2007
2008 record_for_igvn(ne_region);
2009 set_control(_gvn.transform(ne_region));
2010 if (btest == BoolTest::ne) {
2011 {
2012 PreserveJVMState pjvms(this);
2013 if (null_ctl == top()) {
2014 replace_in_map(input, cast);
2015 }
2016 int target_bci = iter().get_dest();
2017 merge(target_bci);
2018 }
2019 record_for_igvn(eq_region);
2020 set_control(_gvn.transform(eq_region));
2021 } else {
2022 if (null_ctl == top()) {
2023 replace_in_map(input, cast);
2024 }
2025 set_control(_gvn.transform(ne_region));
2026 }
2027 }
2028
2029 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) {
2030 Node* ne_region = new RegionNode(1);
2031 Node* null_ctl;
2032 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2033 ne_region->add_req(null_ctl);
2034
2035 {
2036 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2037 inc_sp(2);
2038 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2039 }
2040
2041 ne_region->add_req(control());
2042
2043 record_for_igvn(ne_region);
2044 set_control(_gvn.transform(ne_region));
2045 if (btest == BoolTest::ne) {
2046 {
2047 PreserveJVMState pjvms(this);
2048 if (null_ctl == top()) {
2049 replace_in_map(input, cast);
2050 }
2051 int target_bci = iter().get_dest();
2052 merge(target_bci);
2053 }
2054 record_for_igvn(eq_region);
2055 set_control(_gvn.transform(eq_region));
2056 } else {
2057 if (null_ctl == top()) {
2058 replace_in_map(input, cast);
2059 }
2060 set_control(_gvn.transform(ne_region));
2061 }
2062 }
2063
2064 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2065 ciKlass* left_type = nullptr;
2066 ciKlass* right_type = nullptr;
2067 ProfilePtrKind left_ptr = ProfileUnknownNull;
2068 ProfilePtrKind right_ptr = ProfileUnknownNull;
2069 bool left_inline_type = true;
2070 bool right_inline_type = true;
2071
2072 // Leverage profiling at acmp
2073 if (UseACmpProfile) {
2074 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2075 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2076 left_type = nullptr;
2077 right_type = nullptr;
2078 left_inline_type = true;
2079 right_inline_type = true;
2080 }
2081 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2082 left_ptr = ProfileUnknownNull;
2083 right_ptr = ProfileUnknownNull;
2084 }
2085 }
2086
2087 if (UseTypeSpeculation) {
2088 record_profile_for_speculation(left, left_type, left_ptr);
2089 record_profile_for_speculation(right, right_type, right_ptr);
2090 }
2091
2092 if (!EnableValhalla) {
2093 Node* cmp = CmpP(left, right);
2094 cmp = optimize_cmp_with_klass(cmp);
2095 do_if(btest, cmp);
2096 return;
2097 }
2098
2099 // Check for equality before potentially allocating
2100 if (left == right) {
2101 do_if(btest, makecon(TypeInt::CC_EQ));
2102 return;
2103 }
2104
2105 // Allocate inline type operands and re-execute on deoptimization
2106 if (left->is_InlineType()) {
2107 if (_gvn.type(right)->is_zero_type() ||
2108 (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_is_init())->is_zero_type())) {
2109 // Null checking a scalarized but nullable inline type. Check the IsInit
2110 // input instead of the oop input to avoid keeping buffer allocations alive.
2111 Node* cmp = CmpI(left->as_InlineType()->get_is_init(), intcon(0));
2112 do_if(btest, cmp);
2113 return;
2114 } else {
2115 PreserveReexecuteState preexecs(this);
2116 inc_sp(2);
2117 jvms()->set_should_reexecute(true);
2118 left = left->as_InlineType()->buffer(this)->get_oop();
2119 }
2120 }
2121 if (right->is_InlineType()) {
2122 PreserveReexecuteState preexecs(this);
2123 inc_sp(2);
2124 jvms()->set_should_reexecute(true);
2125 right = right->as_InlineType()->buffer(this)->get_oop();
2126 }
2127
2128 // First, do a normal pointer comparison
2129 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2130 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2131 Node* cmp = CmpP(left, right);
2132 cmp = optimize_cmp_with_klass(cmp);
2133 if (tleft == nullptr || !tleft->can_be_inline_type() ||
2134 tright == nullptr || !tright->can_be_inline_type()) {
2135 // This is sufficient, if one of the operands can't be an inline type
2136 do_if(btest, cmp);
2137 return;
2138 }
2139
2140 // Don't add traps to unstable if branches because additional checks are required to
2141 // decide if the operands are equal/substitutable and we therefore shouldn't prune
2142 // branches for one if based on the profiling of the acmp branches.
2143 // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2144 // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2145 // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2146 const bool can_trap = true;
2147
2148 Node* eq_region = nullptr;
2149 if (btest == BoolTest::eq) {
2150 do_if(btest, cmp, !can_trap, true);
2151 if (stopped()) {
2152 // Pointers are equal, operands must be equal
2153 return;
2154 }
2155 } else {
2156 assert(btest == BoolTest::ne, "only eq or ne");
2157 Node* is_not_equal = nullptr;
2158 eq_region = new RegionNode(3);
2159 {
2160 PreserveJVMState pjvms(this);
2161 // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2162 do_if(btest, cmp, !can_trap, false, &is_not_equal);
2163 if (!stopped()) {
2164 eq_region->init_req(1, control());
2165 }
2166 }
2167 if (is_not_equal == nullptr || is_not_equal->is_top()) {
2168 record_for_igvn(eq_region);
2169 set_control(_gvn.transform(eq_region));
2170 return;
2171 }
2172 set_control(is_not_equal);
2173 }
2174
2175 // Prefer speculative types if available
2176 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2177 if (tleft->speculative_type() != nullptr) {
2178 left_type = tleft->speculative_type();
2179 }
2180 if (tright->speculative_type() != nullptr) {
2181 right_type = tright->speculative_type();
2182 }
2183 }
2184
2185 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2186 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2187 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2188 left_ptr = speculative_left_ptr;
2189 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2190 left_ptr = speculative_left_ptr;
2191 }
2192 }
2193 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2194 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2195 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2196 right_ptr = speculative_right_ptr;
2197 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2198 right_ptr = speculative_right_ptr;
2199 }
2200 }
2201
2202 if (left_ptr == ProfileAlwaysNull) {
2203 // Comparison with null. Assert the input is indeed null and we're done.
2204 acmp_always_null_input(left, tleft, btest, eq_region);
2205 return;
2206 }
2207 if (right_ptr == ProfileAlwaysNull) {
2208 // Comparison with null. Assert the input is indeed null and we're done.
2209 acmp_always_null_input(right, tright, btest, eq_region);
2210 return;
2211 }
2212 if (left_type != nullptr && !left_type->is_inlinetype()) {
2213 // Comparison with an object of known type
2214 acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region);
2215 return;
2216 }
2217 if (right_type != nullptr && !right_type->is_inlinetype()) {
2218 // Comparison with an object of known type
2219 acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region);
2220 return;
2221 }
2222 if (!left_inline_type) {
2223 // Comparison with an object known not to be an inline type
2224 acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region);
2225 return;
2226 }
2227 if (!right_inline_type) {
2228 // Comparison with an object known not to be an inline type
2229 acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region);
2230 return;
2231 }
2232
2233 // Pointers are not equal, check if first operand is non-null
2234 Node* ne_region = new RegionNode(6);
2235 Node* null_ctl;
2236 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2237 ne_region->init_req(1, null_ctl);
2238
2239 // First operand is non-null, check if it is an inline type
2240 Node* is_value = inline_type_test(not_null_right);
2241 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2242 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2243 ne_region->init_req(2, not_value);
2244 set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2245
2246 // The first operand is an inline type, check if the second operand is non-null
2247 Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2248 ne_region->init_req(3, null_ctl);
2249
2250 // Check if both operands are of the same class.
2251 Node* kls_left = load_object_klass(not_null_left);
2252 Node* kls_right = load_object_klass(not_null_right);
2253 Node* kls_cmp = CmpP(kls_left, kls_right);
2254 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2255 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2256 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2257 set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2258 ne_region->init_req(4, kls_ne);
2259
2260 if (stopped()) {
2261 record_for_igvn(ne_region);
2262 set_control(_gvn.transform(ne_region));
2263 if (btest == BoolTest::ne) {
2264 {
2265 PreserveJVMState pjvms(this);
2266 int target_bci = iter().get_dest();
2267 merge(target_bci);
2268 }
2269 record_for_igvn(eq_region);
2270 set_control(_gvn.transform(eq_region));
2271 }
2272 return;
2273 }
2274
2275 // Both operands are values types of the same class, we need to perform a
2276 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2277 Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2278 Node* mem = reset_memory();
2279 Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2280
2281 Node* eq_io_phi = nullptr;
2282 Node* eq_mem_phi = nullptr;
2283 if (eq_region != nullptr) {
2284 eq_io_phi = PhiNode::make(eq_region, i_o());
2285 eq_mem_phi = PhiNode::make(eq_region, mem);
2286 }
2287
2288 set_all_memory(mem);
2289
2290 kill_dead_locals();
2291 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(ciSymbols::isSubstitutable_name(), ciSymbols::object_object_boolean_signature());
2292 CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2293 call->set_override_symbolic_info(true);
2294 call->init_req(TypeFunc::Parms, not_null_left);
2295 call->init_req(TypeFunc::Parms+1, not_null_right);
2296 inc_sp(2);
2297 set_edges_for_java_call(call, false, false);
2298 Node* ret = set_results_for_java_call(call, false, true);
2299 dec_sp(2);
2300
2301 // Test the return value of ValueObjectMethods::isSubstitutable()
2302 // This is the last check, do_if can emit traps now.
2303 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2304 Node* ctl = C->top();
2305 if (btest == BoolTest::eq) {
2306 PreserveJVMState pjvms(this);
2307 do_if(btest, subst_cmp, can_trap);
2308 if (!stopped()) {
2309 ctl = control();
2310 }
2311 } else {
2312 assert(btest == BoolTest::ne, "only eq or ne");
2313 PreserveJVMState pjvms(this);
2314 do_if(btest, subst_cmp, can_trap, false, &ctl);
2315 if (!stopped()) {
2316 eq_region->init_req(2, control());
2317 eq_io_phi->init_req(2, i_o());
2318 eq_mem_phi->init_req(2, reset_memory());
2319 }
2320 }
2321 ne_region->init_req(5, ctl);
2322 ne_io_phi->init_req(5, i_o());
2323 ne_mem_phi->init_req(5, reset_memory());
2324
2325 record_for_igvn(ne_region);
2326 set_control(_gvn.transform(ne_region));
2327 set_i_o(_gvn.transform(ne_io_phi));
2328 set_all_memory(_gvn.transform(ne_mem_phi));
2329
2330 if (btest == BoolTest::ne) {
2331 {
2332 PreserveJVMState pjvms(this);
2333 int target_bci = iter().get_dest();
2334 merge(target_bci);
2335 }
2336
2337 record_for_igvn(eq_region);
2338 set_control(_gvn.transform(eq_region));
2339 set_i_o(_gvn.transform(eq_io_phi));
2340 set_all_memory(_gvn.transform(eq_mem_phi));
2341 }
2342 }
2343
2344 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const {
2345 // Don't want to speculate on uncommon traps when running with -Xcomp
2346 if (!UseInterpreter) {
2347 return false;
2348 }
2349 return seems_never_taken(prob) &&
2350 !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
2351 }
2352
2353 void Parse::maybe_add_predicate_after_if(Block* path) {
2354 if (path->is_SEL_head() && path->preds_parsed() == 0) {
2355 // Add predicates at bci of if dominating the loop so traps can be
2356 // recorded on the if's profile data
2357 int bc_depth = repush_if_args();
2358 add_parse_predicates();
2359 dec_sp(bc_depth);
2360 path->set_has_predicates();
2361 }
2362 }
2363
2364
2365 //----------------------------adjust_map_after_if------------------------------
2366 // Adjust the JVM state to reflect the result of taking this path.
2367 // Basically, it means inspecting the CmpNode controlling this
2368 // branch, seeing how it constrains a tested value, and then
2369 // deciding if it's worth our while to encode this constraint
2370 // as graph nodes in the current abstract interpretation map.
2371 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2372 if (!c->is_Cmp()) {
2373 maybe_add_predicate_after_if(path);
2374 return;
2375 }
2376
2377 if (stopped() || btest == BoolTest::illegal) {
2378 return; // nothing to do
2379 }
2380
2381 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2382
2383 if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2384 repush_if_args();
2385 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2386 Deoptimization::Action_reinterpret,
2387 nullptr,
2388 (is_fallthrough ? "taken always" : "taken never"));
2389
2390 if (call != nullptr) {
2391 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2392 }
2393 return;
2394 }
2395
2396 Node* val = c->in(1);
2397 Node* con = c->in(2);
2398 const Type* tcon = _gvn.type(con);
2399 const Type* tval = _gvn.type(val);
2400 bool have_con = tcon->singleton();
2401 if (tval->singleton()) {
2402 if (!have_con) {
2403 // Swap, so constant is in con.
2460 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2461 // Found:
2462 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2463 // or the narrowOop equivalent.
2464 const Type* obj_type = _gvn.type(obj);
2465 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2466 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2467 tboth->higher_equal(obj_type)) {
2468 // obj has to be of the exact type Foo if the CmpP succeeds.
2469 int obj_in_map = map()->find_edge(obj);
2470 JVMState* jvms = this->jvms();
2471 if (obj_in_map >= 0 &&
2472 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2473 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2474 const Type* tcc = ccast->as_Type()->type();
2475 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2476 // Delay transform() call to allow recovery of pre-cast value
2477 // at the control merge.
2478 _gvn.set_type_bottom(ccast);
2479 record_for_igvn(ccast);
2480 if (tboth->is_inlinetypeptr()) {
2481 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2482 }
2483 // Here's the payoff.
2484 replace_in_map(obj, ccast);
2485 }
2486 }
2487 }
2488 }
2489
2490 int val_in_map = map()->find_edge(val);
2491 if (val_in_map < 0) return; // replace_in_map would be useless
2492 {
2493 JVMState* jvms = this->jvms();
2494 if (!(jvms->is_loc(val_in_map) ||
2495 jvms->is_stk(val_in_map)))
2496 return; // again, it would be useless
2497 }
2498
2499 // Check for a comparison to a constant, and "know" that the compared
2500 // value is constrained on this path.
2501 assert(tcon->singleton(), "");
2502 ConstraintCastNode* ccast = nullptr;
2567 if (c->Opcode() == Op_CmpP &&
2568 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2569 c->in(2)->is_Con()) {
2570 Node* load_klass = nullptr;
2571 Node* decode = nullptr;
2572 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2573 decode = c->in(1);
2574 load_klass = c->in(1)->in(1);
2575 } else {
2576 load_klass = c->in(1);
2577 }
2578 if (load_klass->in(2)->is_AddP()) {
2579 Node* addp = load_klass->in(2);
2580 Node* obj = addp->in(AddPNode::Address);
2581 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2582 if (obj_type->speculative_type_not_null() != nullptr) {
2583 ciKlass* k = obj_type->speculative_type();
2584 inc_sp(2);
2585 obj = maybe_cast_profiled_obj(obj, k);
2586 dec_sp(2);
2587 if (obj->is_InlineType()) {
2588 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2589 obj = obj->as_InlineType()->get_oop();
2590 }
2591 // Make the CmpP use the casted obj
2592 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2593 load_klass = load_klass->clone();
2594 load_klass->set_req(2, addp);
2595 load_klass = _gvn.transform(load_klass);
2596 if (decode != nullptr) {
2597 decode = decode->clone();
2598 decode->set_req(1, load_klass);
2599 load_klass = _gvn.transform(decode);
2600 }
2601 c = c->clone();
2602 c->set_req(1, load_klass);
2603 c = _gvn.transform(c);
2604 }
2605 }
2606 }
2607 return c;
2608 }
2609
2610 //------------------------------do_one_bytecode--------------------------------
3417 // See if we can get some profile data and hand it off to the next block
3418 Block *target_block = block()->successor_for_bci(target_bci);
3419 if (target_block->pred_count() != 1) break;
3420 ciMethodData* methodData = method()->method_data();
3421 if (!methodData->is_mature()) break;
3422 ciProfileData* data = methodData->bci_to_data(bci());
3423 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3424 int taken = ((ciJumpData*)data)->taken();
3425 taken = method()->scale_count(taken);
3426 target_block->set_count(taken);
3427 break;
3428 }
3429
3430 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
3431 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3432 handle_if_null:
3433 // If this is a backwards branch in the bytecodes, add Safepoint
3434 maybe_add_safepoint(iter().get_dest());
3435 a = null();
3436 b = pop();
3437 if (b->is_InlineType()) {
3438 // Null checking a scalarized but nullable inline type. Check the IsInit
3439 // input instead of the oop input to avoid keeping buffer allocations alive
3440 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_is_init(), zerocon(T_INT)));
3441 } else {
3442 if (!_gvn.type(b)->speculative_maybe_null() &&
3443 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3444 inc_sp(1);
3445 Node* null_ctl = top();
3446 b = null_check_oop(b, &null_ctl, true, true, true);
3447 assert(null_ctl->is_top(), "no null control here");
3448 dec_sp(1);
3449 } else if (_gvn.type(b)->speculative_always_null() &&
3450 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3451 inc_sp(1);
3452 b = null_assert(b);
3453 dec_sp(1);
3454 }
3455 c = _gvn.transform( new CmpPNode(b, a) );
3456 }
3457 do_ifnull(btest, c);
3458 break;
3459
3460 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3461 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3462 handle_if_acmp:
3463 // If this is a backwards branch in the bytecodes, add Safepoint
3464 maybe_add_safepoint(iter().get_dest());
3465 a = pop();
3466 b = pop();
3467 do_acmp(btest, b, a);
3468 break;
3469
3470 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3471 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3472 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3473 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3474 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3475 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3476 handle_ifxx:
3477 // If this is a backwards branch in the bytecodes, add Safepoint
3478 maybe_add_safepoint(iter().get_dest());
3479 a = _gvn.intcon(0);
3480 b = pop();
3481 c = _gvn.transform( new CmpINode(b, a) );
3482 do_if(btest, c);
3483 break;
3484
3485 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3486 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3487 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3502 break;
3503
3504 case Bytecodes::_lookupswitch:
3505 do_lookupswitch();
3506 break;
3507
3508 case Bytecodes::_invokestatic:
3509 case Bytecodes::_invokedynamic:
3510 case Bytecodes::_invokespecial:
3511 case Bytecodes::_invokevirtual:
3512 case Bytecodes::_invokeinterface:
3513 do_call();
3514 break;
3515 case Bytecodes::_checkcast:
3516 do_checkcast();
3517 break;
3518 case Bytecodes::_instanceof:
3519 do_instanceof();
3520 break;
3521 case Bytecodes::_anewarray:
3522 do_newarray();
3523 break;
3524 case Bytecodes::_newarray:
3525 do_newarray((BasicType)iter().get_index());
3526 break;
3527 case Bytecodes::_multianewarray:
3528 do_multianewarray();
3529 break;
3530 case Bytecodes::_new:
3531 do_new();
3532 break;
3533
3534 case Bytecodes::_jsr:
3535 case Bytecodes::_jsr_w:
3536 do_jsr();
3537 break;
3538
3539 case Bytecodes::_ret:
3540 do_ret();
3541 break;
3542
|