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 );
1446 // False branch
1447 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1448 set_control(iffalse);
1449
1450 if (stopped()) { // Path is dead?
1451 NOT_PRODUCT(explicit_null_checks_elided++);
1452 if (C->eliminate_boxing()) {
1453 // Mark the successor block as parsed
1454 next_block->next_path_num();
1455 }
1456 } else { // Path is live.
1457 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1458 }
1459
1460 if (do_stress_trap) {
1461 stress_trap(iff, counter, incr_store);
1462 }
1463 }
1464
1465 //------------------------------------do_if------------------------------------
1466 void Parse::do_if(BoolTest::mask btest, Node* c) {
1467 int target_bci = iter().get_dest();
1468
1469 Block* branch_block = successor_for_bci(target_bci);
1470 Block* next_block = successor_for_bci(iter().next_bci());
1471
1472 float cnt;
1473 float prob = branch_prediction(cnt, btest, target_bci, c);
1474 float untaken_prob = 1.0 - prob;
1475
1476 if (prob == PROB_UNKNOWN) {
1477 if (PrintOpto && Verbose) {
1478 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1479 }
1480 repush_if_args(); // to gather stats on loop
1481 uncommon_trap(Deoptimization::Reason_unreached,
1482 Deoptimization::Action_reinterpret,
1483 nullptr, "cold");
1484 if (C->eliminate_boxing()) {
1485 // Mark the successor blocks as parsed
1486 branch_block->next_path_num();
1537 }
1538
1539 // Generate real control flow
1540 float true_prob = (taken_if_true ? prob : untaken_prob);
1541 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1542 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1543 Node* taken_branch = new IfTrueNode(iff);
1544 Node* untaken_branch = new IfFalseNode(iff);
1545 if (!taken_if_true) { // Finish conversion to canonical form
1546 Node* tmp = taken_branch;
1547 taken_branch = untaken_branch;
1548 untaken_branch = tmp;
1549 }
1550
1551 // Branch is taken:
1552 { PreserveJVMState pjvms(this);
1553 taken_branch = _gvn.transform(taken_branch);
1554 set_control(taken_branch);
1555
1556 if (stopped()) {
1557 if (C->eliminate_boxing()) {
1558 // Mark the successor block as parsed
1559 branch_block->next_path_num();
1560 }
1561 } else {
1562 adjust_map_after_if(taken_btest, c, prob, branch_block);
1563 if (!stopped()) {
1564 merge(target_bci);
1565 }
1566 }
1567 }
1568
1569 untaken_branch = _gvn.transform(untaken_branch);
1570 set_control(untaken_branch);
1571
1572 // Branch not taken.
1573 if (stopped()) {
1574 if (C->eliminate_boxing()) {
1575 // Mark the successor block as parsed
1576 next_block->next_path_num();
1577 }
1578 } else {
1579 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1580 }
1581
1582 if (do_stress_trap) {
1583 stress_trap(iff, counter, incr_store);
1584 }
1585 }
1586
1587 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
1588 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
1589 // then either takes the trap or executes the original, unstable if.
1590 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
1591 // Search for an unstable if trap
1592 CallStaticJavaNode* trap = nullptr;
1593 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
1594 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
1595 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
1596 // No suitable trap found. Remove unused counter load and increment.
1597 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
1598 return;
1599 }
1600
1601 // Remove trap from optimization list since we add another path to the trap.
1602 bool success = C->remove_unstable_if_trap(trap, true);
1603 assert(success, "Trap already modified");
1604
1605 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
1606 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
1639 }
1640
1641 void Parse::maybe_add_predicate_after_if(Block* path) {
1642 if (path->is_SEL_head() && path->preds_parsed() == 0) {
1643 // Add predicates at bci of if dominating the loop so traps can be
1644 // recorded on the if's profile data
1645 int bc_depth = repush_if_args();
1646 add_parse_predicates();
1647 dec_sp(bc_depth);
1648 path->set_has_predicates();
1649 }
1650 }
1651
1652
1653 //----------------------------adjust_map_after_if------------------------------
1654 // Adjust the JVM state to reflect the result of taking this path.
1655 // Basically, it means inspecting the CmpNode controlling this
1656 // branch, seeing how it constrains a tested value, and then
1657 // deciding if it's worth our while to encode this constraint
1658 // as graph nodes in the current abstract interpretation map.
1659 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) {
1660 if (!c->is_Cmp()) {
1661 maybe_add_predicate_after_if(path);
1662 return;
1663 }
1664
1665 if (stopped() || btest == BoolTest::illegal) {
1666 return; // nothing to do
1667 }
1668
1669 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1670
1671 if (path_is_suitable_for_uncommon_trap(prob)) {
1672 repush_if_args();
1673 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
1674 Deoptimization::Action_reinterpret,
1675 nullptr,
1676 (is_fallthrough ? "taken always" : "taken never"));
1677
1678 if (call != nullptr) {
1679 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
1680 }
1681 return;
1682 }
1683
1684 Node* val = c->in(1);
1685 Node* con = c->in(2);
1686 const Type* tcon = _gvn.type(con);
1687 const Type* tval = _gvn.type(val);
1688 bool have_con = tcon->singleton();
1689 if (tval->singleton()) {
1690 if (!have_con) {
1691 // Swap, so constant is in con.
1748 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
1749 // Found:
1750 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1751 // or the narrowOop equivalent.
1752 const Type* obj_type = _gvn.type(obj);
1753 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
1754 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
1755 tboth->higher_equal(obj_type)) {
1756 // obj has to be of the exact type Foo if the CmpP succeeds.
1757 int obj_in_map = map()->find_edge(obj);
1758 JVMState* jvms = this->jvms();
1759 if (obj_in_map >= 0 &&
1760 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1761 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1762 const Type* tcc = ccast->as_Type()->type();
1763 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1764 // Delay transform() call to allow recovery of pre-cast value
1765 // at the control merge.
1766 _gvn.set_type_bottom(ccast);
1767 record_for_igvn(ccast);
1768 // Here's the payoff.
1769 replace_in_map(obj, ccast);
1770 }
1771 }
1772 }
1773 }
1774
1775 int val_in_map = map()->find_edge(val);
1776 if (val_in_map < 0) return; // replace_in_map would be useless
1777 {
1778 JVMState* jvms = this->jvms();
1779 if (!(jvms->is_loc(val_in_map) ||
1780 jvms->is_stk(val_in_map)))
1781 return; // again, it would be useless
1782 }
1783
1784 // Check for a comparison to a constant, and "know" that the compared
1785 // value is constrained on this path.
1786 assert(tcon->singleton(), "");
1787 ConstraintCastNode* ccast = nullptr;
1852 if (c->Opcode() == Op_CmpP &&
1853 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1854 c->in(2)->is_Con()) {
1855 Node* load_klass = nullptr;
1856 Node* decode = nullptr;
1857 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1858 decode = c->in(1);
1859 load_klass = c->in(1)->in(1);
1860 } else {
1861 load_klass = c->in(1);
1862 }
1863 if (load_klass->in(2)->is_AddP()) {
1864 Node* addp = load_klass->in(2);
1865 Node* obj = addp->in(AddPNode::Address);
1866 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1867 if (obj_type->speculative_type_not_null() != nullptr) {
1868 ciKlass* k = obj_type->speculative_type();
1869 inc_sp(2);
1870 obj = maybe_cast_profiled_obj(obj, k);
1871 dec_sp(2);
1872 // Make the CmpP use the casted obj
1873 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1874 load_klass = load_klass->clone();
1875 load_klass->set_req(2, addp);
1876 load_klass = _gvn.transform(load_klass);
1877 if (decode != nullptr) {
1878 decode = decode->clone();
1879 decode->set_req(1, load_klass);
1880 load_klass = _gvn.transform(decode);
1881 }
1882 c = c->clone();
1883 c->set_req(1, load_klass);
1884 c = _gvn.transform(c);
1885 }
1886 }
1887 }
1888 return c;
1889 }
1890
1891 //------------------------------do_one_bytecode--------------------------------
2698 // See if we can get some profile data and hand it off to the next block
2699 Block *target_block = block()->successor_for_bci(target_bci);
2700 if (target_block->pred_count() != 1) break;
2701 ciMethodData* methodData = method()->method_data();
2702 if (!methodData->is_mature()) break;
2703 ciProfileData* data = methodData->bci_to_data(bci());
2704 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2705 int taken = ((ciJumpData*)data)->taken();
2706 taken = method()->scale_count(taken);
2707 target_block->set_count(taken);
2708 break;
2709 }
2710
2711 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
2712 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2713 handle_if_null:
2714 // If this is a backwards branch in the bytecodes, add Safepoint
2715 maybe_add_safepoint(iter().get_dest());
2716 a = null();
2717 b = pop();
2718 if (!_gvn.type(b)->speculative_maybe_null() &&
2719 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2720 inc_sp(1);
2721 Node* null_ctl = top();
2722 b = null_check_oop(b, &null_ctl, true, true, true);
2723 assert(null_ctl->is_top(), "no null control here");
2724 dec_sp(1);
2725 } else if (_gvn.type(b)->speculative_always_null() &&
2726 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2727 inc_sp(1);
2728 b = null_assert(b);
2729 dec_sp(1);
2730 }
2731 c = _gvn.transform( new CmpPNode(b, a) );
2732 do_ifnull(btest, c);
2733 break;
2734
2735 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2736 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2737 handle_if_acmp:
2738 // If this is a backwards branch in the bytecodes, add Safepoint
2739 maybe_add_safepoint(iter().get_dest());
2740 a = pop();
2741 b = pop();
2742 c = _gvn.transform( new CmpPNode(b, a) );
2743 c = optimize_cmp_with_klass(c);
2744 do_if(btest, c);
2745 break;
2746
2747 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2748 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2749 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2750 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2751 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2752 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2753 handle_ifxx:
2754 // If this is a backwards branch in the bytecodes, add Safepoint
2755 maybe_add_safepoint(iter().get_dest());
2756 a = _gvn.intcon(0);
2757 b = pop();
2758 c = _gvn.transform( new CmpINode(b, a) );
2759 do_if(btest, c);
2760 break;
2761
2762 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2763 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2764 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2779 break;
2780
2781 case Bytecodes::_lookupswitch:
2782 do_lookupswitch();
2783 break;
2784
2785 case Bytecodes::_invokestatic:
2786 case Bytecodes::_invokedynamic:
2787 case Bytecodes::_invokespecial:
2788 case Bytecodes::_invokevirtual:
2789 case Bytecodes::_invokeinterface:
2790 do_call();
2791 break;
2792 case Bytecodes::_checkcast:
2793 do_checkcast();
2794 break;
2795 case Bytecodes::_instanceof:
2796 do_instanceof();
2797 break;
2798 case Bytecodes::_anewarray:
2799 do_anewarray();
2800 break;
2801 case Bytecodes::_newarray:
2802 do_newarray((BasicType)iter().get_index());
2803 break;
2804 case Bytecodes::_multianewarray:
2805 do_multianewarray();
2806 break;
2807 case Bytecodes::_new:
2808 do_new();
2809 break;
2810
2811 case Bytecodes::_jsr:
2812 case Bytecodes::_jsr_w:
2813 do_jsr();
2814 break;
2815
2816 case Bytecodes::_ret:
2817 do_ret();
2818 break;
2819
|
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_C),
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 );
1823 // False branch
1824 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1825 set_control(iffalse);
1826
1827 if (stopped()) { // Path is dead?
1828 NOT_PRODUCT(explicit_null_checks_elided++);
1829 if (C->eliminate_boxing()) {
1830 // Mark the successor block as parsed
1831 next_block->next_path_num();
1832 }
1833 } else { // Path is live.
1834 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1835 }
1836
1837 if (do_stress_trap) {
1838 stress_trap(iff, counter, incr_store);
1839 }
1840 }
1841
1842 //------------------------------------do_if------------------------------------
1843 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken) {
1844 int target_bci = iter().get_dest();
1845
1846 Block* branch_block = successor_for_bci(target_bci);
1847 Block* next_block = successor_for_bci(iter().next_bci());
1848
1849 float cnt;
1850 float prob = branch_prediction(cnt, btest, target_bci, c);
1851 float untaken_prob = 1.0 - prob;
1852
1853 if (prob == PROB_UNKNOWN) {
1854 if (PrintOpto && Verbose) {
1855 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1856 }
1857 repush_if_args(); // to gather stats on loop
1858 uncommon_trap(Deoptimization::Reason_unreached,
1859 Deoptimization::Action_reinterpret,
1860 nullptr, "cold");
1861 if (C->eliminate_boxing()) {
1862 // Mark the successor blocks as parsed
1863 branch_block->next_path_num();
1914 }
1915
1916 // Generate real control flow
1917 float true_prob = (taken_if_true ? prob : untaken_prob);
1918 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1919 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1920 Node* taken_branch = new IfTrueNode(iff);
1921 Node* untaken_branch = new IfFalseNode(iff);
1922 if (!taken_if_true) { // Finish conversion to canonical form
1923 Node* tmp = taken_branch;
1924 taken_branch = untaken_branch;
1925 untaken_branch = tmp;
1926 }
1927
1928 // Branch is taken:
1929 { PreserveJVMState pjvms(this);
1930 taken_branch = _gvn.transform(taken_branch);
1931 set_control(taken_branch);
1932
1933 if (stopped()) {
1934 if (C->eliminate_boxing() && !new_path) {
1935 // Mark the successor block as parsed (if we haven't created a new path)
1936 branch_block->next_path_num();
1937 }
1938 } else {
1939 adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1940 if (!stopped()) {
1941 if (new_path) {
1942 // Merge by using a new path
1943 merge_new_path(target_bci);
1944 } else if (ctrl_taken != nullptr) {
1945 // Don't merge but save taken branch to be wired by caller
1946 *ctrl_taken = control();
1947 } else {
1948 merge(target_bci);
1949 }
1950 }
1951 }
1952 }
1953
1954 untaken_branch = _gvn.transform(untaken_branch);
1955 set_control(untaken_branch);
1956
1957 // Branch not taken.
1958 if (stopped() && ctrl_taken == nullptr) {
1959 if (C->eliminate_boxing()) {
1960 // Mark the successor block as parsed (if caller does not re-wire control flow)
1961 next_block->next_path_num();
1962 }
1963 } else {
1964 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
1965 }
1966
1967 if (do_stress_trap) {
1968 stress_trap(iff, counter, incr_store);
1969 }
1970 }
1971
1972
1973 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
1974 if (t->speculative() == nullptr) {
1975 return ProfileUnknownNull;
1976 }
1977 if (t->speculative_always_null()) {
1978 return ProfileAlwaysNull;
1979 }
1980 if (t->speculative_maybe_null()) {
1981 return ProfileMaybeNull;
1982 }
1983 return ProfileNeverNull;
1984 }
1985
1986 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
1987 inc_sp(2);
1988 Node* cast = null_check_common(input, T_OBJECT, true, nullptr,
1989 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
1990 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
1991 dec_sp(2);
1992 if (btest == BoolTest::ne) {
1993 {
1994 PreserveJVMState pjvms(this);
1995 replace_in_map(input, cast);
1996 int target_bci = iter().get_dest();
1997 merge(target_bci);
1998 }
1999 record_for_igvn(eq_region);
2000 set_control(_gvn.transform(eq_region));
2001 } else {
2002 replace_in_map(input, cast);
2003 }
2004 }
2005
2006 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
2007 inc_sp(2);
2008 null_ctl = top();
2009 Node* cast = null_check_oop(input, &null_ctl,
2010 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
2011 false,
2012 speculative_ptr_kind(tinput) == ProfileNeverNull &&
2013 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
2014 dec_sp(2);
2015 assert(!stopped(), "null input should have been caught earlier");
2016 return cast;
2017 }
2018
2019 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) {
2020 Node* ne_region = new RegionNode(1);
2021 Node* null_ctl;
2022 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2023 ne_region->add_req(null_ctl);
2024
2025 Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast);
2026 {
2027 PreserveJVMState pjvms(this);
2028 inc_sp(2);
2029 set_control(slow_ctl);
2030 Deoptimization::DeoptReason reason;
2031 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2032 reason = Deoptimization::Reason_speculate_class_check;
2033 } else {
2034 reason = Deoptimization::Reason_class_check;
2035 }
2036 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2037 }
2038 ne_region->add_req(control());
2039
2040 record_for_igvn(ne_region);
2041 set_control(_gvn.transform(ne_region));
2042 if (btest == BoolTest::ne) {
2043 {
2044 PreserveJVMState pjvms(this);
2045 if (null_ctl == top()) {
2046 replace_in_map(input, cast);
2047 }
2048 int target_bci = iter().get_dest();
2049 merge(target_bci);
2050 }
2051 record_for_igvn(eq_region);
2052 set_control(_gvn.transform(eq_region));
2053 } else {
2054 if (null_ctl == top()) {
2055 replace_in_map(input, cast);
2056 }
2057 set_control(_gvn.transform(ne_region));
2058 }
2059 }
2060
2061 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) {
2062 Node* ne_region = new RegionNode(1);
2063 Node* null_ctl;
2064 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2065 ne_region->add_req(null_ctl);
2066
2067 {
2068 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2069 inc_sp(2);
2070 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2071 }
2072
2073 ne_region->add_req(control());
2074
2075 record_for_igvn(ne_region);
2076 set_control(_gvn.transform(ne_region));
2077 if (btest == BoolTest::ne) {
2078 {
2079 PreserveJVMState pjvms(this);
2080 if (null_ctl == top()) {
2081 replace_in_map(input, cast);
2082 }
2083 int target_bci = iter().get_dest();
2084 merge(target_bci);
2085 }
2086 record_for_igvn(eq_region);
2087 set_control(_gvn.transform(eq_region));
2088 } else {
2089 if (null_ctl == top()) {
2090 replace_in_map(input, cast);
2091 }
2092 set_control(_gvn.transform(ne_region));
2093 }
2094 }
2095
2096 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2097 ciKlass* left_type = nullptr;
2098 ciKlass* right_type = nullptr;
2099 ProfilePtrKind left_ptr = ProfileUnknownNull;
2100 ProfilePtrKind right_ptr = ProfileUnknownNull;
2101 bool left_inline_type = true;
2102 bool right_inline_type = true;
2103
2104 // Leverage profiling at acmp
2105 if (UseACmpProfile) {
2106 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2107 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2108 left_type = nullptr;
2109 right_type = nullptr;
2110 left_inline_type = true;
2111 right_inline_type = true;
2112 }
2113 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2114 left_ptr = ProfileUnknownNull;
2115 right_ptr = ProfileUnknownNull;
2116 }
2117 }
2118
2119 if (UseTypeSpeculation) {
2120 record_profile_for_speculation(left, left_type, left_ptr);
2121 record_profile_for_speculation(right, right_type, right_ptr);
2122 }
2123
2124 if (!EnableValhalla) {
2125 Node* cmp = CmpP(left, right);
2126 cmp = optimize_cmp_with_klass(cmp);
2127 do_if(btest, cmp);
2128 return;
2129 }
2130
2131 // Check for equality before potentially allocating
2132 if (left == right) {
2133 do_if(btest, makecon(TypeInt::CC_EQ));
2134 return;
2135 }
2136
2137 // Allocate inline type operands and re-execute on deoptimization
2138 if (left->is_InlineType()) {
2139 if (_gvn.type(right)->is_zero_type() ||
2140 (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_is_init())->is_zero_type())) {
2141 // Null checking a scalarized but nullable inline type. Check the IsInit
2142 // input instead of the oop input to avoid keeping buffer allocations alive.
2143 Node* cmp = CmpI(left->as_InlineType()->get_is_init(), intcon(0));
2144 do_if(btest, cmp);
2145 return;
2146 } else {
2147 PreserveReexecuteState preexecs(this);
2148 inc_sp(2);
2149 jvms()->set_should_reexecute(true);
2150 left = left->as_InlineType()->buffer(this)->get_oop();
2151 }
2152 }
2153 if (right->is_InlineType()) {
2154 PreserveReexecuteState preexecs(this);
2155 inc_sp(2);
2156 jvms()->set_should_reexecute(true);
2157 right = right->as_InlineType()->buffer(this)->get_oop();
2158 }
2159
2160 // First, do a normal pointer comparison
2161 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2162 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2163 Node* cmp = CmpP(left, right);
2164 cmp = optimize_cmp_with_klass(cmp);
2165 if (tleft == nullptr || !tleft->can_be_inline_type() ||
2166 tright == nullptr || !tright->can_be_inline_type()) {
2167 // This is sufficient, if one of the operands can't be an inline type
2168 do_if(btest, cmp);
2169 return;
2170 }
2171
2172 // Don't add traps to unstable if branches because additional checks are required to
2173 // decide if the operands are equal/substitutable and we therefore shouldn't prune
2174 // branches for one if based on the profiling of the acmp branches.
2175 // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2176 // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2177 // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2178 const bool can_trap = true;
2179
2180 Node* eq_region = nullptr;
2181 if (btest == BoolTest::eq) {
2182 do_if(btest, cmp, !can_trap, true);
2183 if (stopped()) {
2184 // Pointers are equal, operands must be equal
2185 return;
2186 }
2187 } else {
2188 assert(btest == BoolTest::ne, "only eq or ne");
2189 Node* is_not_equal = nullptr;
2190 eq_region = new RegionNode(3);
2191 {
2192 PreserveJVMState pjvms(this);
2193 // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2194 do_if(btest, cmp, !can_trap, false, &is_not_equal);
2195 if (!stopped()) {
2196 eq_region->init_req(1, control());
2197 }
2198 }
2199 if (is_not_equal == nullptr || is_not_equal->is_top()) {
2200 record_for_igvn(eq_region);
2201 set_control(_gvn.transform(eq_region));
2202 return;
2203 }
2204 set_control(is_not_equal);
2205 }
2206
2207 // Prefer speculative types if available
2208 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2209 if (tleft->speculative_type() != nullptr) {
2210 left_type = tleft->speculative_type();
2211 }
2212 if (tright->speculative_type() != nullptr) {
2213 right_type = tright->speculative_type();
2214 }
2215 }
2216
2217 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2218 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2219 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2220 left_ptr = speculative_left_ptr;
2221 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2222 left_ptr = speculative_left_ptr;
2223 }
2224 }
2225 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2226 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2227 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2228 right_ptr = speculative_right_ptr;
2229 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2230 right_ptr = speculative_right_ptr;
2231 }
2232 }
2233
2234 if (left_ptr == ProfileAlwaysNull) {
2235 // Comparison with null. Assert the input is indeed null and we're done.
2236 acmp_always_null_input(left, tleft, btest, eq_region);
2237 return;
2238 }
2239 if (right_ptr == ProfileAlwaysNull) {
2240 // Comparison with null. Assert the input is indeed null and we're done.
2241 acmp_always_null_input(right, tright, btest, eq_region);
2242 return;
2243 }
2244 if (left_type != nullptr && !left_type->is_inlinetype()) {
2245 // Comparison with an object of known type
2246 acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region);
2247 return;
2248 }
2249 if (right_type != nullptr && !right_type->is_inlinetype()) {
2250 // Comparison with an object of known type
2251 acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region);
2252 return;
2253 }
2254 if (!left_inline_type) {
2255 // Comparison with an object known not to be an inline type
2256 acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region);
2257 return;
2258 }
2259 if (!right_inline_type) {
2260 // Comparison with an object known not to be an inline type
2261 acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region);
2262 return;
2263 }
2264
2265 // Pointers are not equal, check if first operand is non-null
2266 Node* ne_region = new RegionNode(6);
2267 Node* null_ctl;
2268 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2269 ne_region->init_req(1, null_ctl);
2270
2271 // First operand is non-null, check if it is an inline type
2272 Node* is_value = inline_type_test(not_null_right);
2273 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2274 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2275 ne_region->init_req(2, not_value);
2276 set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2277
2278 // The first operand is an inline type, check if the second operand is non-null
2279 Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2280 ne_region->init_req(3, null_ctl);
2281
2282 // Check if both operands are of the same class.
2283 Node* kls_left = load_object_klass(not_null_left);
2284 Node* kls_right = load_object_klass(not_null_right);
2285 Node* kls_cmp = CmpP(kls_left, kls_right);
2286 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2287 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2288 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2289 set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2290 ne_region->init_req(4, kls_ne);
2291
2292 if (stopped()) {
2293 record_for_igvn(ne_region);
2294 set_control(_gvn.transform(ne_region));
2295 if (btest == BoolTest::ne) {
2296 {
2297 PreserveJVMState pjvms(this);
2298 int target_bci = iter().get_dest();
2299 merge(target_bci);
2300 }
2301 record_for_igvn(eq_region);
2302 set_control(_gvn.transform(eq_region));
2303 }
2304 return;
2305 }
2306
2307 // Both operands are values types of the same class, we need to perform a
2308 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2309 Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2310 Node* mem = reset_memory();
2311 Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2312
2313 Node* eq_io_phi = nullptr;
2314 Node* eq_mem_phi = nullptr;
2315 if (eq_region != nullptr) {
2316 eq_io_phi = PhiNode::make(eq_region, i_o());
2317 eq_mem_phi = PhiNode::make(eq_region, mem);
2318 }
2319
2320 set_all_memory(mem);
2321
2322 kill_dead_locals();
2323 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(ciSymbols::isSubstitutable_name(), ciSymbols::object_object_boolean_signature());
2324 CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2325 call->set_override_symbolic_info(true);
2326 call->init_req(TypeFunc::Parms, not_null_left);
2327 call->init_req(TypeFunc::Parms+1, not_null_right);
2328 inc_sp(2);
2329 set_edges_for_java_call(call, false, false);
2330 Node* ret = set_results_for_java_call(call, false, true);
2331 dec_sp(2);
2332
2333 // Test the return value of ValueObjectMethods::isSubstitutable()
2334 // This is the last check, do_if can emit traps now.
2335 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2336 Node* ctl = C->top();
2337 if (btest == BoolTest::eq) {
2338 PreserveJVMState pjvms(this);
2339 do_if(btest, subst_cmp, can_trap);
2340 if (!stopped()) {
2341 ctl = control();
2342 }
2343 } else {
2344 assert(btest == BoolTest::ne, "only eq or ne");
2345 PreserveJVMState pjvms(this);
2346 do_if(btest, subst_cmp, can_trap, false, &ctl);
2347 if (!stopped()) {
2348 eq_region->init_req(2, control());
2349 eq_io_phi->init_req(2, i_o());
2350 eq_mem_phi->init_req(2, reset_memory());
2351 }
2352 }
2353 ne_region->init_req(5, ctl);
2354 ne_io_phi->init_req(5, i_o());
2355 ne_mem_phi->init_req(5, reset_memory());
2356
2357 record_for_igvn(ne_region);
2358 set_control(_gvn.transform(ne_region));
2359 set_i_o(_gvn.transform(ne_io_phi));
2360 set_all_memory(_gvn.transform(ne_mem_phi));
2361
2362 if (btest == BoolTest::ne) {
2363 {
2364 PreserveJVMState pjvms(this);
2365 int target_bci = iter().get_dest();
2366 merge(target_bci);
2367 }
2368
2369 record_for_igvn(eq_region);
2370 set_control(_gvn.transform(eq_region));
2371 set_i_o(_gvn.transform(eq_io_phi));
2372 set_all_memory(_gvn.transform(eq_mem_phi));
2373 }
2374 }
2375
2376 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
2377 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
2378 // then either takes the trap or executes the original, unstable if.
2379 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
2380 // Search for an unstable if trap
2381 CallStaticJavaNode* trap = nullptr;
2382 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
2383 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
2384 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
2385 // No suitable trap found. Remove unused counter load and increment.
2386 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
2387 return;
2388 }
2389
2390 // Remove trap from optimization list since we add another path to the trap.
2391 bool success = C->remove_unstable_if_trap(trap, true);
2392 assert(success, "Trap already modified");
2393
2394 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
2395 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
2428 }
2429
2430 void Parse::maybe_add_predicate_after_if(Block* path) {
2431 if (path->is_SEL_head() && path->preds_parsed() == 0) {
2432 // Add predicates at bci of if dominating the loop so traps can be
2433 // recorded on the if's profile data
2434 int bc_depth = repush_if_args();
2435 add_parse_predicates();
2436 dec_sp(bc_depth);
2437 path->set_has_predicates();
2438 }
2439 }
2440
2441
2442 //----------------------------adjust_map_after_if------------------------------
2443 // Adjust the JVM state to reflect the result of taking this path.
2444 // Basically, it means inspecting the CmpNode controlling this
2445 // branch, seeing how it constrains a tested value, and then
2446 // deciding if it's worth our while to encode this constraint
2447 // as graph nodes in the current abstract interpretation map.
2448 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2449 if (!c->is_Cmp()) {
2450 maybe_add_predicate_after_if(path);
2451 return;
2452 }
2453
2454 if (stopped() || btest == BoolTest::illegal) {
2455 return; // nothing to do
2456 }
2457
2458 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2459
2460 if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2461 repush_if_args();
2462 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2463 Deoptimization::Action_reinterpret,
2464 nullptr,
2465 (is_fallthrough ? "taken always" : "taken never"));
2466
2467 if (call != nullptr) {
2468 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2469 }
2470 return;
2471 }
2472
2473 Node* val = c->in(1);
2474 Node* con = c->in(2);
2475 const Type* tcon = _gvn.type(con);
2476 const Type* tval = _gvn.type(val);
2477 bool have_con = tcon->singleton();
2478 if (tval->singleton()) {
2479 if (!have_con) {
2480 // Swap, so constant is in con.
2537 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2538 // Found:
2539 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2540 // or the narrowOop equivalent.
2541 const Type* obj_type = _gvn.type(obj);
2542 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2543 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2544 tboth->higher_equal(obj_type)) {
2545 // obj has to be of the exact type Foo if the CmpP succeeds.
2546 int obj_in_map = map()->find_edge(obj);
2547 JVMState* jvms = this->jvms();
2548 if (obj_in_map >= 0 &&
2549 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2550 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2551 const Type* tcc = ccast->as_Type()->type();
2552 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2553 // Delay transform() call to allow recovery of pre-cast value
2554 // at the control merge.
2555 _gvn.set_type_bottom(ccast);
2556 record_for_igvn(ccast);
2557 if (tboth->is_inlinetypeptr()) {
2558 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2559 }
2560 // Here's the payoff.
2561 replace_in_map(obj, ccast);
2562 }
2563 }
2564 }
2565 }
2566
2567 int val_in_map = map()->find_edge(val);
2568 if (val_in_map < 0) return; // replace_in_map would be useless
2569 {
2570 JVMState* jvms = this->jvms();
2571 if (!(jvms->is_loc(val_in_map) ||
2572 jvms->is_stk(val_in_map)))
2573 return; // again, it would be useless
2574 }
2575
2576 // Check for a comparison to a constant, and "know" that the compared
2577 // value is constrained on this path.
2578 assert(tcon->singleton(), "");
2579 ConstraintCastNode* ccast = nullptr;
2644 if (c->Opcode() == Op_CmpP &&
2645 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2646 c->in(2)->is_Con()) {
2647 Node* load_klass = nullptr;
2648 Node* decode = nullptr;
2649 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2650 decode = c->in(1);
2651 load_klass = c->in(1)->in(1);
2652 } else {
2653 load_klass = c->in(1);
2654 }
2655 if (load_klass->in(2)->is_AddP()) {
2656 Node* addp = load_klass->in(2);
2657 Node* obj = addp->in(AddPNode::Address);
2658 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2659 if (obj_type->speculative_type_not_null() != nullptr) {
2660 ciKlass* k = obj_type->speculative_type();
2661 inc_sp(2);
2662 obj = maybe_cast_profiled_obj(obj, k);
2663 dec_sp(2);
2664 if (obj->is_InlineType()) {
2665 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2666 obj = obj->as_InlineType()->get_oop();
2667 }
2668 // Make the CmpP use the casted obj
2669 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2670 load_klass = load_klass->clone();
2671 load_klass->set_req(2, addp);
2672 load_klass = _gvn.transform(load_klass);
2673 if (decode != nullptr) {
2674 decode = decode->clone();
2675 decode->set_req(1, load_klass);
2676 load_klass = _gvn.transform(decode);
2677 }
2678 c = c->clone();
2679 c->set_req(1, load_klass);
2680 c = _gvn.transform(c);
2681 }
2682 }
2683 }
2684 return c;
2685 }
2686
2687 //------------------------------do_one_bytecode--------------------------------
3494 // See if we can get some profile data and hand it off to the next block
3495 Block *target_block = block()->successor_for_bci(target_bci);
3496 if (target_block->pred_count() != 1) break;
3497 ciMethodData* methodData = method()->method_data();
3498 if (!methodData->is_mature()) break;
3499 ciProfileData* data = methodData->bci_to_data(bci());
3500 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3501 int taken = ((ciJumpData*)data)->taken();
3502 taken = method()->scale_count(taken);
3503 target_block->set_count(taken);
3504 break;
3505 }
3506
3507 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
3508 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3509 handle_if_null:
3510 // If this is a backwards branch in the bytecodes, add Safepoint
3511 maybe_add_safepoint(iter().get_dest());
3512 a = null();
3513 b = pop();
3514 if (b->is_InlineType()) {
3515 // Null checking a scalarized but nullable inline type. Check the IsInit
3516 // input instead of the oop input to avoid keeping buffer allocations alive
3517 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_is_init(), zerocon(T_INT)));
3518 } else {
3519 if (!_gvn.type(b)->speculative_maybe_null() &&
3520 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3521 inc_sp(1);
3522 Node* null_ctl = top();
3523 b = null_check_oop(b, &null_ctl, true, true, true);
3524 assert(null_ctl->is_top(), "no null control here");
3525 dec_sp(1);
3526 } else if (_gvn.type(b)->speculative_always_null() &&
3527 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3528 inc_sp(1);
3529 b = null_assert(b);
3530 dec_sp(1);
3531 }
3532 c = _gvn.transform( new CmpPNode(b, a) );
3533 }
3534 do_ifnull(btest, c);
3535 break;
3536
3537 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3538 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3539 handle_if_acmp:
3540 // If this is a backwards branch in the bytecodes, add Safepoint
3541 maybe_add_safepoint(iter().get_dest());
3542 a = pop();
3543 b = pop();
3544 do_acmp(btest, b, a);
3545 break;
3546
3547 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3548 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3549 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3550 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3551 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3552 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3553 handle_ifxx:
3554 // If this is a backwards branch in the bytecodes, add Safepoint
3555 maybe_add_safepoint(iter().get_dest());
3556 a = _gvn.intcon(0);
3557 b = pop();
3558 c = _gvn.transform( new CmpINode(b, a) );
3559 do_if(btest, c);
3560 break;
3561
3562 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3563 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3564 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3579 break;
3580
3581 case Bytecodes::_lookupswitch:
3582 do_lookupswitch();
3583 break;
3584
3585 case Bytecodes::_invokestatic:
3586 case Bytecodes::_invokedynamic:
3587 case Bytecodes::_invokespecial:
3588 case Bytecodes::_invokevirtual:
3589 case Bytecodes::_invokeinterface:
3590 do_call();
3591 break;
3592 case Bytecodes::_checkcast:
3593 do_checkcast();
3594 break;
3595 case Bytecodes::_instanceof:
3596 do_instanceof();
3597 break;
3598 case Bytecodes::_anewarray:
3599 do_newarray();
3600 break;
3601 case Bytecodes::_newarray:
3602 do_newarray((BasicType)iter().get_index());
3603 break;
3604 case Bytecodes::_multianewarray:
3605 do_multianewarray();
3606 break;
3607 case Bytecodes::_new:
3608 do_new();
3609 break;
3610
3611 case Bytecodes::_jsr:
3612 case Bytecodes::_jsr_w:
3613 do_jsr();
3614 break;
3615
3616 case Bytecodes::_ret:
3617 do_ret();
3618 break;
3619
|