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--------------------------------
2685 // See if we can get some profile data and hand it off to the next block
2686 Block *target_block = block()->successor_for_bci(target_bci);
2687 if (target_block->pred_count() != 1) break;
2688 ciMethodData* methodData = method()->method_data();
2689 if (!methodData->is_mature()) break;
2690 ciProfileData* data = methodData->bci_to_data(bci());
2691 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2692 int taken = ((ciJumpData*)data)->taken();
2693 taken = method()->scale_count(taken);
2694 target_block->set_count(taken);
2695 break;
2696 }
2697
2698 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
2699 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2700 handle_if_null:
2701 // If this is a backwards branch in the bytecodes, add Safepoint
2702 maybe_add_safepoint(iter().get_dest());
2703 a = null();
2704 b = pop();
2705 if (!_gvn.type(b)->speculative_maybe_null() &&
2706 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2707 inc_sp(1);
2708 Node* null_ctl = top();
2709 b = null_check_oop(b, &null_ctl, true, true, true);
2710 assert(null_ctl->is_top(), "no null control here");
2711 dec_sp(1);
2712 } else if (_gvn.type(b)->speculative_always_null() &&
2713 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2714 inc_sp(1);
2715 b = null_assert(b);
2716 dec_sp(1);
2717 }
2718 c = _gvn.transform( new CmpPNode(b, a) );
2719 do_ifnull(btest, c);
2720 break;
2721
2722 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2723 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2724 handle_if_acmp:
2725 // If this is a backwards branch in the bytecodes, add Safepoint
2726 maybe_add_safepoint(iter().get_dest());
2727 a = pop();
2728 b = pop();
2729 c = _gvn.transform( new CmpPNode(b, a) );
2730 c = optimize_cmp_with_klass(c);
2731 do_if(btest, c);
2732 break;
2733
2734 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2735 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2736 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2737 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2738 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2739 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2740 handle_ifxx:
2741 // If this is a backwards branch in the bytecodes, add Safepoint
2742 maybe_add_safepoint(iter().get_dest());
2743 a = _gvn.intcon(0);
2744 b = pop();
2745 c = _gvn.transform( new CmpINode(b, a) );
2746 do_if(btest, c);
2747 break;
2748
2749 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2750 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2751 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2766 break;
2767
2768 case Bytecodes::_lookupswitch:
2769 do_lookupswitch();
2770 break;
2771
2772 case Bytecodes::_invokestatic:
2773 case Bytecodes::_invokedynamic:
2774 case Bytecodes::_invokespecial:
2775 case Bytecodes::_invokevirtual:
2776 case Bytecodes::_invokeinterface:
2777 do_call();
2778 break;
2779 case Bytecodes::_checkcast:
2780 do_checkcast();
2781 break;
2782 case Bytecodes::_instanceof:
2783 do_instanceof();
2784 break;
2785 case Bytecodes::_anewarray:
2786 do_anewarray();
2787 break;
2788 case Bytecodes::_newarray:
2789 do_newarray((BasicType)iter().get_index());
2790 break;
2791 case Bytecodes::_multianewarray:
2792 do_multianewarray();
2793 break;
2794 case Bytecodes::_new:
2795 do_new();
2796 break;
2797
2798 case Bytecodes::_jsr:
2799 case Bytecodes::_jsr_w:
2800 do_jsr();
2801 break;
2802
2803 case Bytecodes::_ret:
2804 do_ret();
2805 break;
2806
|
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* array_index = pop();
80 Node* array = pop();
81
82 // Handle inline type arrays
83 const TypeOopPtr* element_ptr = elemtype->make_oopptr();
84 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
85 if (array_type->is_flat()) {
86 // Load from flat inline type array
87 Node* inline_type;
88 if (element_ptr->klass_is_exact()) {
89 inline_type = InlineTypeNode::make_from_flat(this, elemtype->inline_klass(), array, adr);
90 } else {
91 // Element type of flat array is not exact. Therefore, we cannot determine the flat array layout statically.
92 // Emit a runtime call to load the element from the flat array.
93 inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
94 inline_type = record_profile_for_speculation_at_array_load(inline_type);
95 }
96 push(inline_type);
97 return;
98 }
99
100 if (!array_type->is_not_flat()) {
101 // Cannot statically determine if array is a flat array, emit runtime check
102 assert(UseFlatArray && is_reference_type(bt) && element_ptr->can_be_inline_type() && !array_type->is_not_null_free() &&
103 (!element_ptr->is_inlinetypeptr() || element_ptr->inline_klass()->flat_in_array()), "array can't be flat");
104 IdealKit ideal(this);
105 IdealVariable res(ideal);
106 ideal.declarations_done();
107 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
108 // Non-flat array
109 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
110 sync_kit(ideal);
111 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
112 DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
113 if (needs_range_check(array_type->size(), array_index)) {
114 // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
115 // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
116 // possibly float above the range check at any point.
117 decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
118 }
119 Node* ld = access_load_at(array, adr, adr_type, element_ptr, bt, decorator_set);
120 if (element_ptr->is_inlinetypeptr()) {
121 assert(element_ptr->maybe_null(), "null free array should be handled above");
122 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass(), false);
123 }
124 ideal.sync_kit(this);
125 ideal.set(res, ld);
126 } ideal.else_(); {
127 // Flat array
128 sync_kit(ideal);
129 if (element_ptr->is_inlinetypeptr()) {
130 // Element type is known, cast and load from flat array layout.
131 ciInlineKlass* vk = element_ptr->inline_klass();
132 assert(vk->flat_in_array() && element_ptr->maybe_null(), "never/always flat - should be optimized");
133 ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true);
134 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
135 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, arytype));
136 Node* casted_adr = array_element_address(cast, array_index, T_OBJECT, array_type->size(), control());
137 // Re-execute flat array load if buffering triggers deoptimization
138 PreserveReexecuteState preexecs(this);
139 jvms()->set_should_reexecute(true);
140 inc_sp(2);
141 Node* vt = InlineTypeNode::make_from_flat(this, vk, cast, casted_adr)->buffer(this, false);
142 ideal.set(res, vt);
143 ideal.sync_kit(this);
144 } else {
145 // Element type is unknown, and thus we cannot statically determine the exact flat array layout. Emit a
146 // runtime call to correctly load the inline type element from the flat array.
147 Node* inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
148 ideal.sync_kit(this);
149 ideal.set(res, inline_type);
150 }
151 } ideal.end_if();
152 sync_kit(ideal);
153 Node* ld = _gvn.transform(ideal.value(res));
154 ld = record_profile_for_speculation_at_array_load(ld);
155 push_node(bt, ld);
156 return;
157 }
158
159 if (array_type->is_null_free()) {
160 // Load from non-flat inline type array (elements can never be null)
161 bt = T_OBJECT;
162 }
163
164 if (elemtype == TypeInt::BOOL) {
165 bt = T_BOOLEAN;
166 }
167 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
168 Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
169 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
170 ld = record_profile_for_speculation_at_array_load(ld);
171 // Loading an inline type from a non-flat array
172 if (element_ptr != nullptr && element_ptr->is_inlinetypeptr()) {
173 assert(!array_type->is_null_free() || !element_ptr->maybe_null(), "inline type array elements should never be null");
174 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass(), !element_ptr->maybe_null());
175 }
176 push_node(bt, ld);
177 }
178
179 Node* Parse::load_from_unknown_flat_array(Node* array, Node* array_index, const TypeOopPtr* element_ptr) {
180 // Below membars keep this access to an unknown flat array correctly
181 // ordered with other unknown and known flat array accesses.
182 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
183
184 Node* call = nullptr;
185 {
186 // Re-execute flat array load if runtime call triggers deoptimization
187 PreserveReexecuteState preexecs(this);
188 jvms()->set_bci(_bci);
189 jvms()->set_should_reexecute(true);
190 inc_sp(2);
191 kill_dead_locals();
192 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
193 OptoRuntime::load_unknown_inline_Type(),
194 OptoRuntime::load_unknown_inline_Java(),
195 nullptr, TypeRawPtr::BOTTOM,
196 array, array_index);
197 }
198 make_slow_call_ex(call, env()->Throwable_klass(), false);
199 Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
200
201 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
202
203 // Keep track of the information that the inline type is in flat arrays
204 const Type* unknown_value = element_ptr->is_instptr()->cast_to_flat_in_array();
205 return _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
206 }
207
208 //--------------------------------array_store----------------------------------
209 void Parse::array_store(BasicType bt) {
210 const Type* elemtype = Type::TOP;
211 Node* adr = array_addressing(bt, type2size[bt], elemtype);
212 if (stopped()) return; // guaranteed null or range check
213 Node* stored_value_casted = nullptr;
214 if (bt == T_OBJECT) {
215 stored_value_casted = array_store_check(adr, elemtype);
216 if (stopped()) {
217 return;
218 }
219 }
220 Node* const stored_value = pop_node(bt); // Value to store
221 Node* const array_index = pop(); // Index in the array
222 Node* array = pop(); // The array itself
223
224 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
225 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
226
227 if (elemtype == TypeInt::BOOL) {
228 bt = T_BOOLEAN;
229 } else if (bt == T_OBJECT) {
230 elemtype = elemtype->make_oopptr();
231 const Type* stored_value_casted_type = _gvn.type(stored_value_casted);
232 // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
233 // This is only legal for non-null stores because the array_store_check always passes for null, even
234 // if the array is null-free. Null stores are handled in GraphKit::inline_array_null_guard().
235 bool not_null_free = !stored_value_casted_type->maybe_null() &&
236 !stored_value_casted_type->is_oopptr()->can_be_inline_type();
237 bool not_flat = not_null_free || (stored_value_casted_type->is_inlinetypeptr() &&
238 !stored_value_casted_type->inline_klass()->flat_in_array());
239 if (!array_type->is_not_null_free() && not_null_free) {
240 // Storing a non-inline type, mark array as not null-free (-> not flat).
241 array_type = array_type->cast_to_not_null_free();
242 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
243 replace_in_map(array, cast);
244 array = cast;
245 } else if (!array_type->is_not_flat() && not_flat) {
246 // Storing to a non-flat array, mark array as not flat.
247 array_type = array_type->cast_to_not_flat();
248 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
249 replace_in_map(array, cast);
250 array = cast;
251 }
252
253 if (array_type->is_flat()) {
254 // Store to flat inline type array
255 assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
256 if (array_type->klass_is_exact()) {
257 // Store to exact flat inline type array where we know the flat array layout statically.
258 // Re-execute flat array store if buffering triggers deoptimization
259 PreserveReexecuteState preexecs(this);
260 inc_sp(3);
261 jvms()->set_should_reexecute(true);
262 stored_value_casted->as_InlineType()->store_flat(this, array, adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
263 } else {
264 // Element type of flat array is not exact. Therefore, we cannot determine the flat array layout statically.
265 // Emit a runtime call to store the element to the flat array.
266 store_to_unknown_flat_array(array, array_index, stored_value_casted);
267 }
268 return;
269 }
270 if (array_type->is_null_free()) {
271 // Store to non-flat null-free inline type array (elements can never be null)
272 assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
273 if (elemtype->inline_klass()->is_empty()) {
274 // Ignore empty inline stores, array is already initialized.
275 return;
276 }
277 } else if (!array_type->is_not_flat() && (stored_value_casted_type != TypePtr::NULL_PTR || StressReflectiveCode)) {
278 // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient).
279 assert(UseFlatArray && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
280 !array_type->klass_is_exact() && !array_type->is_not_null_free(), "array can't be a flat array");
281 IdealKit ideal(this);
282 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
283 // Non-flat array
284 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
285 sync_kit(ideal);
286 Node* cast_array = inline_array_null_guard(array, stored_value_casted, 3);
287 inc_sp(3);
288 access_store_at(cast_array, adr, adr_type, stored_value_casted, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
289 dec_sp(3);
290 ideal.sync_kit(this);
291 } ideal.else_(); {
292 sync_kit(ideal);
293 // flat array
294 Node* null_ctl = top();
295 Node* null_checked_stored_value_casted = null_check_oop(stored_value_casted, &null_ctl);
296 if (null_ctl != top()) {
297 PreserveJVMState pjvms(this);
298 inc_sp(3);
299 set_control(null_ctl);
300 uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
301 dec_sp(3);
302 }
303 // Try to determine the inline klass
304 ciInlineKlass* inline_Klass = nullptr;
305 if (stored_value_casted_type->is_inlinetypeptr()) {
306 inline_Klass = stored_value_casted_type->inline_klass();
307 } else if (elemtype->is_inlinetypeptr()) {
308 inline_Klass = elemtype->inline_klass();
309 }
310 if (!stopped()) {
311 if (inline_Klass != nullptr) {
312 // Element type is known, cast and store to flat array layout.
313 assert(inline_Klass->flat_in_array() && elemtype->maybe_null(), "never/always flat - should be optimized");
314 ciArrayKlass* array_klass = ciArrayKlass::make(inline_Klass, /* null_free */ true);
315 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
316 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, arytype));
317 Node* casted_adr = array_element_address(casted_array, array_index, T_OBJECT, arytype->size(), control());
318 if (!null_checked_stored_value_casted->is_InlineType()) {
319 assert(!gvn().type(null_checked_stored_value_casted)->maybe_null(),
320 "inline type array elements should never be null");
321 null_checked_stored_value_casted = InlineTypeNode::make_from_oop(this, null_checked_stored_value_casted,
322 inline_Klass);
323 }
324 // Re-execute flat array store if buffering triggers deoptimization
325 PreserveReexecuteState preexecs(this);
326 inc_sp(3);
327 jvms()->set_should_reexecute(true);
328 null_checked_stored_value_casted->as_InlineType()->store_flat(this, casted_array, casted_adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
329 } else {
330 // Element type is unknown, emit a runtime call since the flat array layout is not statically known.
331 store_to_unknown_flat_array(array, array_index, null_checked_stored_value_casted);
332 }
333 }
334 ideal.sync_kit(this);
335 }
336 ideal.end_if();
337 sync_kit(ideal);
338 return;
339 } else if (!array_type->is_not_null_free()) {
340 // Array is not flat but may be null free
341 assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
342 array = inline_array_null_guard(array, stored_value_casted, 3, true);
343 }
344 }
345 inc_sp(3);
346 access_store_at(array, adr, adr_type, stored_value, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
347 dec_sp(3);
348 }
349
350 // Emit a runtime call to store to a flat array whose element type is either unknown (i.e. we do not know the flat
351 // array layout) or not exact (could have different flat array layouts at runtime).
352 void Parse::store_to_unknown_flat_array(Node* array, Node* const idx, Node* non_null_stored_value) {
353 // Below membars keep this access to an unknown flat array correctly
354 // ordered with other unknown and known flat array accesses.
355 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
356
357 make_runtime_call(RC_LEAF,
358 OptoRuntime::store_unknown_inline_Type(),
359 CAST_FROM_FN_PTR(address, OptoRuntime::store_unknown_inline_C),
360 "store_unknown_inline", TypeRawPtr::BOTTOM,
361 non_null_stored_value, array, idx);
362
363 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
364 }
365
366 //------------------------------array_addressing-------------------------------
367 // Pull array and index from the stack. Compute pointer-to-element.
368 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
369 Node *idx = peek(0+vals); // Get from stack without popping
370 Node *ary = peek(1+vals); // in case of exception
371
372 // Null check the array base, with correct stack contents
373 ary = null_check(ary, T_ARRAY);
374 // Compile-time detect of null-exception?
375 if (stopped()) return top();
376
377 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
378 const TypeInt* sizetype = arytype->size();
379 elemtype = arytype->elem();
380
381 if (UseUniqueSubclasses) {
382 const Type* el = elemtype->make_ptr();
383 if (el && el->isa_instptr()) {
384 const TypeInstPtr* toop = el->is_instptr();
385 if (toop->instance_klass()->unique_concrete_subklass()) {
386 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
387 const Type* subklass = Type::get_const_type(toop->instance_klass());
388 elemtype = subklass->join_speculative(el);
389 }
390 }
391 }
392
393 if (!arytype->is_loaded()) {
394 // Only fails for some -Xcomp runs
395 // The class is unloaded. We have to run this bytecode in the interpreter.
396 ciKlass* klass = arytype->unloaded_klass();
397
398 uncommon_trap(Deoptimization::Reason_unloaded,
399 Deoptimization::Action_reinterpret,
400 klass, "!loaded array");
401 return top();
402 }
403
404 ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);
405
406 if (needs_range_check(sizetype, idx)) {
407 create_range_check(idx, ary, sizetype);
408 } else if (C->log() != nullptr) {
409 C->log()->elem("observe that='!need_range_check'");
410 }
411
412 // Check for always knowing you are throwing a range-check exception
413 if (stopped()) return top();
414
415 // Make array address computation control dependent to prevent it
416 // from floating above the range check during loop optimizations.
417 Node* ptr = array_element_address(ary, idx, type, sizetype, control());
418 assert(ptr != top(), "top should go hand-in-hand with stopped");
419
420 return ptr;
421 }
422
423 // 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
424 // be greater or equal the smallest possible array size (i.e. out-of-bounds).
425 bool Parse::needs_range_check(const TypeInt* size_type, const Node* index) const {
426 const TypeInt* index_type = _gvn.type(index)->is_int();
427 return index_type->_hi >= size_type->_lo || index_type->_lo < 0;
428 }
429
430 void Parse::create_range_check(Node* idx, Node* ary, const TypeInt* sizetype) {
431 Node* tst;
432 if (sizetype->_hi <= 0) {
433 // The greatest array bound is negative, so we can conclude that we're
434 // compiling unreachable code, but the unsigned compare trick used below
435 // only works with non-negative lengths. Instead, hack "tst" to be zero so
436 // the uncommon_trap path will always be taken.
437 tst = _gvn.intcon(0);
438 } else {
439 // Range is constant in array-oop, so we can use the original state of mem
440 Node* len = load_array_length(ary);
441
442 // Test length vs index (standard trick using unsigned compare)
443 Node* chk = _gvn.transform(new CmpUNode(idx, len) );
444 BoolTest::mask btest = BoolTest::lt;
445 tst = _gvn.transform(new BoolNode(chk, btest) );
446 }
447 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
448 _gvn.set_type(rc, rc->Value(&_gvn));
449 if (!tst->is_Con()) {
450 record_for_igvn(rc);
451 }
452 set_control(_gvn.transform(new IfTrueNode(rc)));
453 // Branch to failure if out of bounds
454 {
455 PreserveJVMState pjvms(this);
456 set_control(_gvn.transform(new IfFalseNode(rc)));
457 if (C->allow_range_check_smearing()) {
458 // Do not use builtin_throw, since range checks are sometimes
459 // made more stringent by an optimistic transformation.
460 // This creates "tentative" range checks at this point,
461 // which are not guaranteed to throw exceptions.
462 // See IfNode::Ideal, is_range_check, adjust_check.
463 uncommon_trap(Deoptimization::Reason_range_check,
464 Deoptimization::Action_make_not_entrant,
465 nullptr, "range_check");
466 } else {
467 // If we have already recompiled with the range-check-widening
468 // heroic optimization turned off, then we must really be throwing
469 // range check exceptions.
470 builtin_throw(Deoptimization::Reason_range_check);
471 }
472 }
473 }
474
475 // For inline type arrays, we can use the profiling information for array accesses to speculate on the type, flatness,
476 // and null-freeness. We can either prepare the speculative type for later uses or emit explicit speculative checks with
477 // traps now. In the latter case, the speculative type guarantees can avoid additional runtime checks later (e.g.
478 // non-null-free implies non-flat which allows us to remove flatness checks). This makes the graph simpler.
479 Node* Parse::create_speculative_inline_type_array_checks(Node* array, const TypeAryPtr* array_type,
480 const Type*& element_type) {
481 if (!array_type->is_flat() && !array_type->is_not_flat()) {
482 // For arrays that might be flat, speculate that the array has the exact type reported in the profile data such that
483 // we can rely on a fixed memory layout (i.e. either a flat layout or not).
484 array = cast_to_speculative_array_type(array, array_type, element_type);
485 } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
486 // Array is known to be either flat or not flat. If possible, update the speculative type by using the profile data
487 // at this bci.
488 array = cast_to_profiled_array_type(array);
489 }
490
491 // Even though the type does not tell us whether we have an inline type array or not, we can still check the profile data
492 // whether we have a non-null-free or non-flat array. Since non-null-free implies non-flat, we check this first.
493 // Speculating on a non-null-free array doesn't help aaload but could be profitable for a subsequent aastore.
494 if (!array_type->is_null_free() && !array_type->is_not_null_free()) {
495 array = speculate_non_null_free_array(array, array_type);
496 }
497
498 if (!array_type->is_flat() && !array_type->is_not_flat()) {
499 array = speculate_non_flat_array(array, array_type);
500 }
501 return array;
502 }
503
504 // Speculate that the array has the exact type reported in the profile data. We emit a trap when this turns out to be
505 // wrong. On the fast path, we add a CheckCastPP to use the exact type.
506 Node* Parse::cast_to_speculative_array_type(Node* const array, const TypeAryPtr*& array_type, const Type*& element_type) {
507 Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
508 ciKlass* speculative_array_type = array_type->speculative_type();
509 if (too_many_traps_or_recompiles(reason) || speculative_array_type == nullptr) {
510 // No speculative type, check profile data at this bci
511 speculative_array_type = nullptr;
512 reason = Deoptimization::Reason_class_check;
513 if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
514 ciKlass* profiled_element_type = nullptr;
515 ProfilePtrKind element_ptr = ProfileMaybeNull;
516 bool flat_array = true;
517 bool null_free_array = true;
518 method()->array_access_profiled_type(bci(), speculative_array_type, profiled_element_type, element_ptr, flat_array,
519 null_free_array);
520 }
521 }
522 if (speculative_array_type != nullptr) {
523 // Speculate that this array has the exact type reported by profile data
524 Node* casted_array = nullptr;
525 DEBUG_ONLY(Node* old_control = control();)
526 Node* slow_ctl = type_check_receiver(array, speculative_array_type, 1.0, &casted_array);
527 if (stopped()) {
528 // The check always fails and therefore profile information is incorrect. Don't use it.
529 assert(old_control == slow_ctl, "type check should have been removed");
530 set_control(slow_ctl);
531 } else if (!slow_ctl->is_top()) {
532 { PreserveJVMState pjvms(this);
533 set_control(slow_ctl);
534 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
535 }
536 replace_in_map(array, casted_array);
537 array_type = _gvn.type(casted_array)->is_aryptr();
538 element_type = array_type->elem();
539 return casted_array;
540 }
541 }
542 return array;
543 }
544
545 // Create a CheckCastPP when the speculative type can improve the current type.
546 Node* Parse::cast_to_profiled_array_type(Node* const array) {
547 ciKlass* array_type = nullptr;
548 ciKlass* element_type = nullptr;
549 ProfilePtrKind element_ptr = ProfileMaybeNull;
550 bool flat_array = true;
551 bool null_free_array = true;
552 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
553 if (array_type != nullptr) {
554 return record_profile_for_speculation(array, array_type, ProfileMaybeNull);
555 }
556 return array;
557 }
558
559 // Speculate that the array is non-null-free. This will imply non-flatness. We emit a trap when this turns out to be
560 // wrong. On the fast path, we add a CheckCastPP to use the non-null-free type.
561 Node* Parse::speculate_non_null_free_array(Node* const array, const TypeAryPtr*& array_type) {
562 bool null_free_array = true;
563 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
564 if (array_type->speculative() != nullptr &&
565 array_type->speculative()->is_aryptr()->is_not_null_free() &&
566 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
567 null_free_array = false;
568 reason = Deoptimization::Reason_speculate_class_check;
569 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
570 ciKlass* profiled_array_type = nullptr;
571 ciKlass* profiled_element_type = nullptr;
572 ProfilePtrKind element_ptr = ProfileMaybeNull;
573 bool flat_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 (!null_free_array) {
579 { // Deoptimize if null-free array
580 BuildCutout unless(this, null_free_array_test(array, /* null_free = */ false), PROB_MAX);
581 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
582 }
583 assert(!stopped(), "null-free array should have been caught earlier");
584 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_null_free()));
585 replace_in_map(array, casted_array);
586 array_type = _gvn.type(casted_array)->is_aryptr();
587 return casted_array;
588 }
589 return array;
590 }
591
592 // 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
593 // CheckCastPP to use the non-flat type.
594 Node* Parse::speculate_non_flat_array(Node* const array, const TypeAryPtr* const array_type) {
595 bool flat_array = true;
596 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
597 if (array_type->speculative() != nullptr &&
598 array_type->speculative()->is_aryptr()->is_not_flat() &&
599 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
600 flat_array = false;
601 reason = Deoptimization::Reason_speculate_class_check;
602 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
603 ciKlass* profiled_array_type = nullptr;
604 ciKlass* profiled_element_type = nullptr;
605 ProfilePtrKind element_ptr = ProfileMaybeNull;
606 bool null_free_array = true;
607 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
608 null_free_array);
609 reason = Deoptimization::Reason_class_check;
610 }
611 if (!flat_array) {
612 { // Deoptimize if flat array
613 BuildCutout unless(this, flat_array_test(array, /* flat = */ false), PROB_MAX);
614 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
615 }
616 assert(!stopped(), "flat array should have been caught earlier");
617 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_flat()));
618 replace_in_map(array, casted_array);
619 return casted_array;
620 }
621 return array;
622 }
623
624 // returns IfNode
625 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
626 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
627 Node *tst = _gvn.transform(new BoolNode(cmp, mask));
628 IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
629 return iff;
630 }
631
632
633 // sentinel value for the target bci to mark never taken branches
634 // (according to profiling)
635 static const int never_reached = INT_MAX;
636
637 //------------------------------helper for tableswitch-------------------------
638 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
639 // True branch, use existing map info
640 { PreserveJVMState pjvms(this);
641 Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
642 set_control( iftrue );
1856 // False branch
1857 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1858 set_control(iffalse);
1859
1860 if (stopped()) { // Path is dead?
1861 NOT_PRODUCT(explicit_null_checks_elided++);
1862 if (C->eliminate_boxing()) {
1863 // Mark the successor block as parsed
1864 next_block->next_path_num();
1865 }
1866 } else { // Path is live.
1867 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1868 }
1869
1870 if (do_stress_trap) {
1871 stress_trap(iff, counter, incr_store);
1872 }
1873 }
1874
1875 //------------------------------------do_if------------------------------------
1876 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken) {
1877 int target_bci = iter().get_dest();
1878
1879 Block* branch_block = successor_for_bci(target_bci);
1880 Block* next_block = successor_for_bci(iter().next_bci());
1881
1882 float cnt;
1883 float prob = branch_prediction(cnt, btest, target_bci, c);
1884 float untaken_prob = 1.0 - prob;
1885
1886 if (prob == PROB_UNKNOWN) {
1887 if (PrintOpto && Verbose) {
1888 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1889 }
1890 repush_if_args(); // to gather stats on loop
1891 uncommon_trap(Deoptimization::Reason_unreached,
1892 Deoptimization::Action_reinterpret,
1893 nullptr, "cold");
1894 if (C->eliminate_boxing()) {
1895 // Mark the successor blocks as parsed
1896 branch_block->next_path_num();
1947 }
1948
1949 // Generate real control flow
1950 float true_prob = (taken_if_true ? prob : untaken_prob);
1951 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1952 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1953 Node* taken_branch = new IfTrueNode(iff);
1954 Node* untaken_branch = new IfFalseNode(iff);
1955 if (!taken_if_true) { // Finish conversion to canonical form
1956 Node* tmp = taken_branch;
1957 taken_branch = untaken_branch;
1958 untaken_branch = tmp;
1959 }
1960
1961 // Branch is taken:
1962 { PreserveJVMState pjvms(this);
1963 taken_branch = _gvn.transform(taken_branch);
1964 set_control(taken_branch);
1965
1966 if (stopped()) {
1967 if (C->eliminate_boxing() && !new_path) {
1968 // Mark the successor block as parsed (if we haven't created a new path)
1969 branch_block->next_path_num();
1970 }
1971 } else {
1972 adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1973 if (!stopped()) {
1974 if (new_path) {
1975 // Merge by using a new path
1976 merge_new_path(target_bci);
1977 } else if (ctrl_taken != nullptr) {
1978 // Don't merge but save taken branch to be wired by caller
1979 *ctrl_taken = control();
1980 } else {
1981 merge(target_bci);
1982 }
1983 }
1984 }
1985 }
1986
1987 untaken_branch = _gvn.transform(untaken_branch);
1988 set_control(untaken_branch);
1989
1990 // Branch not taken.
1991 if (stopped() && ctrl_taken == nullptr) {
1992 if (C->eliminate_boxing()) {
1993 // Mark the successor block as parsed (if caller does not re-wire control flow)
1994 next_block->next_path_num();
1995 }
1996 } else {
1997 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
1998 }
1999
2000 if (do_stress_trap) {
2001 stress_trap(iff, counter, incr_store);
2002 }
2003 }
2004
2005
2006 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
2007 if (t->speculative() == nullptr) {
2008 return ProfileUnknownNull;
2009 }
2010 if (t->speculative_always_null()) {
2011 return ProfileAlwaysNull;
2012 }
2013 if (t->speculative_maybe_null()) {
2014 return ProfileMaybeNull;
2015 }
2016 return ProfileNeverNull;
2017 }
2018
2019 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
2020 inc_sp(2);
2021 Node* cast = null_check_common(input, T_OBJECT, true, nullptr,
2022 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
2023 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
2024 dec_sp(2);
2025 if (btest == BoolTest::ne) {
2026 {
2027 PreserveJVMState pjvms(this);
2028 replace_in_map(input, cast);
2029 int target_bci = iter().get_dest();
2030 merge(target_bci);
2031 }
2032 record_for_igvn(eq_region);
2033 set_control(_gvn.transform(eq_region));
2034 } else {
2035 replace_in_map(input, cast);
2036 }
2037 }
2038
2039 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
2040 inc_sp(2);
2041 null_ctl = top();
2042 Node* cast = null_check_oop(input, &null_ctl,
2043 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
2044 false,
2045 speculative_ptr_kind(tinput) == ProfileNeverNull &&
2046 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
2047 dec_sp(2);
2048 assert(!stopped(), "null input should have been caught earlier");
2049 return cast;
2050 }
2051
2052 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) {
2053 Node* ne_region = new RegionNode(1);
2054 Node* null_ctl;
2055 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2056 ne_region->add_req(null_ctl);
2057
2058 Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast);
2059 {
2060 PreserveJVMState pjvms(this);
2061 inc_sp(2);
2062 set_control(slow_ctl);
2063 Deoptimization::DeoptReason reason;
2064 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2065 reason = Deoptimization::Reason_speculate_class_check;
2066 } else {
2067 reason = Deoptimization::Reason_class_check;
2068 }
2069 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2070 }
2071 ne_region->add_req(control());
2072
2073 record_for_igvn(ne_region);
2074 set_control(_gvn.transform(ne_region));
2075 if (btest == BoolTest::ne) {
2076 {
2077 PreserveJVMState pjvms(this);
2078 if (null_ctl == top()) {
2079 replace_in_map(input, cast);
2080 }
2081 int target_bci = iter().get_dest();
2082 merge(target_bci);
2083 }
2084 record_for_igvn(eq_region);
2085 set_control(_gvn.transform(eq_region));
2086 } else {
2087 if (null_ctl == top()) {
2088 replace_in_map(input, cast);
2089 }
2090 set_control(_gvn.transform(ne_region));
2091 }
2092 }
2093
2094 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) {
2095 Node* ne_region = new RegionNode(1);
2096 Node* null_ctl;
2097 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2098 ne_region->add_req(null_ctl);
2099
2100 {
2101 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2102 inc_sp(2);
2103 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2104 }
2105
2106 ne_region->add_req(control());
2107
2108 record_for_igvn(ne_region);
2109 set_control(_gvn.transform(ne_region));
2110 if (btest == BoolTest::ne) {
2111 {
2112 PreserveJVMState pjvms(this);
2113 if (null_ctl == top()) {
2114 replace_in_map(input, cast);
2115 }
2116 int target_bci = iter().get_dest();
2117 merge(target_bci);
2118 }
2119 record_for_igvn(eq_region);
2120 set_control(_gvn.transform(eq_region));
2121 } else {
2122 if (null_ctl == top()) {
2123 replace_in_map(input, cast);
2124 }
2125 set_control(_gvn.transform(ne_region));
2126 }
2127 }
2128
2129 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2130 ciKlass* left_type = nullptr;
2131 ciKlass* right_type = nullptr;
2132 ProfilePtrKind left_ptr = ProfileUnknownNull;
2133 ProfilePtrKind right_ptr = ProfileUnknownNull;
2134 bool left_inline_type = true;
2135 bool right_inline_type = true;
2136
2137 // Leverage profiling at acmp
2138 if (UseACmpProfile) {
2139 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2140 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2141 left_type = nullptr;
2142 right_type = nullptr;
2143 left_inline_type = true;
2144 right_inline_type = true;
2145 }
2146 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2147 left_ptr = ProfileUnknownNull;
2148 right_ptr = ProfileUnknownNull;
2149 }
2150 }
2151
2152 if (UseTypeSpeculation) {
2153 record_profile_for_speculation(left, left_type, left_ptr);
2154 record_profile_for_speculation(right, right_type, right_ptr);
2155 }
2156
2157 if (!EnableValhalla) {
2158 Node* cmp = CmpP(left, right);
2159 cmp = optimize_cmp_with_klass(cmp);
2160 do_if(btest, cmp);
2161 return;
2162 }
2163
2164 // Check for equality before potentially allocating
2165 if (left == right) {
2166 do_if(btest, makecon(TypeInt::CC_EQ));
2167 return;
2168 }
2169
2170 // Allocate inline type operands and re-execute on deoptimization
2171 if (left->is_InlineType()) {
2172 if (_gvn.type(right)->is_zero_type() ||
2173 (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_is_init())->is_zero_type())) {
2174 // Null checking a scalarized but nullable inline type. Check the IsInit
2175 // input instead of the oop input to avoid keeping buffer allocations alive.
2176 Node* cmp = CmpI(left->as_InlineType()->get_is_init(), intcon(0));
2177 do_if(btest, cmp);
2178 return;
2179 } else {
2180 PreserveReexecuteState preexecs(this);
2181 inc_sp(2);
2182 jvms()->set_should_reexecute(true);
2183 left = left->as_InlineType()->buffer(this)->get_oop();
2184 }
2185 }
2186 if (right->is_InlineType()) {
2187 PreserveReexecuteState preexecs(this);
2188 inc_sp(2);
2189 jvms()->set_should_reexecute(true);
2190 right = right->as_InlineType()->buffer(this)->get_oop();
2191 }
2192
2193 // First, do a normal pointer comparison
2194 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2195 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2196 Node* cmp = CmpP(left, right);
2197 cmp = optimize_cmp_with_klass(cmp);
2198 if (tleft == nullptr || !tleft->can_be_inline_type() ||
2199 tright == nullptr || !tright->can_be_inline_type()) {
2200 // This is sufficient, if one of the operands can't be an inline type
2201 do_if(btest, cmp);
2202 return;
2203 }
2204
2205 // Don't add traps to unstable if branches because additional checks are required to
2206 // decide if the operands are equal/substitutable and we therefore shouldn't prune
2207 // branches for one if based on the profiling of the acmp branches.
2208 // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2209 // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2210 // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2211 const bool can_trap = true;
2212
2213 Node* eq_region = nullptr;
2214 if (btest == BoolTest::eq) {
2215 do_if(btest, cmp, !can_trap, true);
2216 if (stopped()) {
2217 // Pointers are equal, operands must be equal
2218 return;
2219 }
2220 } else {
2221 assert(btest == BoolTest::ne, "only eq or ne");
2222 Node* is_not_equal = nullptr;
2223 eq_region = new RegionNode(3);
2224 {
2225 PreserveJVMState pjvms(this);
2226 // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2227 do_if(btest, cmp, !can_trap, false, &is_not_equal);
2228 if (!stopped()) {
2229 eq_region->init_req(1, control());
2230 }
2231 }
2232 if (is_not_equal == nullptr || is_not_equal->is_top()) {
2233 record_for_igvn(eq_region);
2234 set_control(_gvn.transform(eq_region));
2235 return;
2236 }
2237 set_control(is_not_equal);
2238 }
2239
2240 // Prefer speculative types if available
2241 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2242 if (tleft->speculative_type() != nullptr) {
2243 left_type = tleft->speculative_type();
2244 }
2245 if (tright->speculative_type() != nullptr) {
2246 right_type = tright->speculative_type();
2247 }
2248 }
2249
2250 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2251 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2252 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2253 left_ptr = speculative_left_ptr;
2254 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2255 left_ptr = speculative_left_ptr;
2256 }
2257 }
2258 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2259 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2260 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2261 right_ptr = speculative_right_ptr;
2262 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2263 right_ptr = speculative_right_ptr;
2264 }
2265 }
2266
2267 if (left_ptr == ProfileAlwaysNull) {
2268 // Comparison with null. Assert the input is indeed null and we're done.
2269 acmp_always_null_input(left, tleft, btest, eq_region);
2270 return;
2271 }
2272 if (right_ptr == ProfileAlwaysNull) {
2273 // Comparison with null. Assert the input is indeed null and we're done.
2274 acmp_always_null_input(right, tright, btest, eq_region);
2275 return;
2276 }
2277 if (left_type != nullptr && !left_type->is_inlinetype()) {
2278 // Comparison with an object of known type
2279 acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region);
2280 return;
2281 }
2282 if (right_type != nullptr && !right_type->is_inlinetype()) {
2283 // Comparison with an object of known type
2284 acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region);
2285 return;
2286 }
2287 if (!left_inline_type) {
2288 // Comparison with an object known not to be an inline type
2289 acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region);
2290 return;
2291 }
2292 if (!right_inline_type) {
2293 // Comparison with an object known not to be an inline type
2294 acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region);
2295 return;
2296 }
2297
2298 // Pointers are not equal, check if first operand is non-null
2299 Node* ne_region = new RegionNode(6);
2300 Node* null_ctl;
2301 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2302 ne_region->init_req(1, null_ctl);
2303
2304 // First operand is non-null, check if it is an inline type
2305 Node* is_value = inline_type_test(not_null_right);
2306 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2307 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2308 ne_region->init_req(2, not_value);
2309 set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2310
2311 // The first operand is an inline type, check if the second operand is non-null
2312 Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2313 ne_region->init_req(3, null_ctl);
2314
2315 // Check if both operands are of the same class.
2316 Node* kls_left = load_object_klass(not_null_left);
2317 Node* kls_right = load_object_klass(not_null_right);
2318 Node* kls_cmp = CmpP(kls_left, kls_right);
2319 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2320 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2321 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2322 set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2323 ne_region->init_req(4, kls_ne);
2324
2325 if (stopped()) {
2326 record_for_igvn(ne_region);
2327 set_control(_gvn.transform(ne_region));
2328 if (btest == BoolTest::ne) {
2329 {
2330 PreserveJVMState pjvms(this);
2331 int target_bci = iter().get_dest();
2332 merge(target_bci);
2333 }
2334 record_for_igvn(eq_region);
2335 set_control(_gvn.transform(eq_region));
2336 }
2337 return;
2338 }
2339
2340 // Both operands are values types of the same class, we need to perform a
2341 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2342 Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2343 Node* mem = reset_memory();
2344 Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2345
2346 Node* eq_io_phi = nullptr;
2347 Node* eq_mem_phi = nullptr;
2348 if (eq_region != nullptr) {
2349 eq_io_phi = PhiNode::make(eq_region, i_o());
2350 eq_mem_phi = PhiNode::make(eq_region, mem);
2351 }
2352
2353 set_all_memory(mem);
2354
2355 kill_dead_locals();
2356 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(ciSymbols::isSubstitutable_name(), ciSymbols::object_object_boolean_signature());
2357 CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2358 call->set_override_symbolic_info(true);
2359 call->init_req(TypeFunc::Parms, not_null_left);
2360 call->init_req(TypeFunc::Parms+1, not_null_right);
2361 inc_sp(2);
2362 set_edges_for_java_call(call, false, false);
2363 Node* ret = set_results_for_java_call(call, false, true);
2364 dec_sp(2);
2365
2366 // Test the return value of ValueObjectMethods::isSubstitutable()
2367 // This is the last check, do_if can emit traps now.
2368 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2369 Node* ctl = C->top();
2370 if (btest == BoolTest::eq) {
2371 PreserveJVMState pjvms(this);
2372 do_if(btest, subst_cmp, can_trap);
2373 if (!stopped()) {
2374 ctl = control();
2375 }
2376 } else {
2377 assert(btest == BoolTest::ne, "only eq or ne");
2378 PreserveJVMState pjvms(this);
2379 do_if(btest, subst_cmp, can_trap, false, &ctl);
2380 if (!stopped()) {
2381 eq_region->init_req(2, control());
2382 eq_io_phi->init_req(2, i_o());
2383 eq_mem_phi->init_req(2, reset_memory());
2384 }
2385 }
2386 ne_region->init_req(5, ctl);
2387 ne_io_phi->init_req(5, i_o());
2388 ne_mem_phi->init_req(5, reset_memory());
2389
2390 record_for_igvn(ne_region);
2391 set_control(_gvn.transform(ne_region));
2392 set_i_o(_gvn.transform(ne_io_phi));
2393 set_all_memory(_gvn.transform(ne_mem_phi));
2394
2395 if (btest == BoolTest::ne) {
2396 {
2397 PreserveJVMState pjvms(this);
2398 int target_bci = iter().get_dest();
2399 merge(target_bci);
2400 }
2401
2402 record_for_igvn(eq_region);
2403 set_control(_gvn.transform(eq_region));
2404 set_i_o(_gvn.transform(eq_io_phi));
2405 set_all_memory(_gvn.transform(eq_mem_phi));
2406 }
2407 }
2408
2409 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
2410 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
2411 // then either takes the trap or executes the original, unstable if.
2412 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
2413 // Search for an unstable if trap
2414 CallStaticJavaNode* trap = nullptr;
2415 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
2416 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
2417 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
2418 // No suitable trap found. Remove unused counter load and increment.
2419 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
2420 return;
2421 }
2422
2423 // Remove trap from optimization list since we add another path to the trap.
2424 bool success = C->remove_unstable_if_trap(trap, true);
2425 assert(success, "Trap already modified");
2426
2427 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
2428 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
2461 }
2462
2463 void Parse::maybe_add_predicate_after_if(Block* path) {
2464 if (path->is_SEL_head() && path->preds_parsed() == 0) {
2465 // Add predicates at bci of if dominating the loop so traps can be
2466 // recorded on the if's profile data
2467 int bc_depth = repush_if_args();
2468 add_parse_predicates();
2469 dec_sp(bc_depth);
2470 path->set_has_predicates();
2471 }
2472 }
2473
2474
2475 //----------------------------adjust_map_after_if------------------------------
2476 // Adjust the JVM state to reflect the result of taking this path.
2477 // Basically, it means inspecting the CmpNode controlling this
2478 // branch, seeing how it constrains a tested value, and then
2479 // deciding if it's worth our while to encode this constraint
2480 // as graph nodes in the current abstract interpretation map.
2481 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2482 if (!c->is_Cmp()) {
2483 maybe_add_predicate_after_if(path);
2484 return;
2485 }
2486
2487 if (stopped() || btest == BoolTest::illegal) {
2488 return; // nothing to do
2489 }
2490
2491 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2492
2493 if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2494 repush_if_args();
2495 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2496 Deoptimization::Action_reinterpret,
2497 nullptr,
2498 (is_fallthrough ? "taken always" : "taken never"));
2499
2500 if (call != nullptr) {
2501 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2502 }
2503 return;
2504 }
2505
2506 Node* val = c->in(1);
2507 Node* con = c->in(2);
2508 const Type* tcon = _gvn.type(con);
2509 const Type* tval = _gvn.type(val);
2510 bool have_con = tcon->singleton();
2511 if (tval->singleton()) {
2512 if (!have_con) {
2513 // Swap, so constant is in con.
2570 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2571 // Found:
2572 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2573 // or the narrowOop equivalent.
2574 const Type* obj_type = _gvn.type(obj);
2575 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2576 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2577 tboth->higher_equal(obj_type)) {
2578 // obj has to be of the exact type Foo if the CmpP succeeds.
2579 int obj_in_map = map()->find_edge(obj);
2580 JVMState* jvms = this->jvms();
2581 if (obj_in_map >= 0 &&
2582 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2583 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2584 const Type* tcc = ccast->as_Type()->type();
2585 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2586 // Delay transform() call to allow recovery of pre-cast value
2587 // at the control merge.
2588 _gvn.set_type_bottom(ccast);
2589 record_for_igvn(ccast);
2590 if (tboth->is_inlinetypeptr()) {
2591 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2592 }
2593 // Here's the payoff.
2594 replace_in_map(obj, ccast);
2595 }
2596 }
2597 }
2598 }
2599
2600 int val_in_map = map()->find_edge(val);
2601 if (val_in_map < 0) return; // replace_in_map would be useless
2602 {
2603 JVMState* jvms = this->jvms();
2604 if (!(jvms->is_loc(val_in_map) ||
2605 jvms->is_stk(val_in_map)))
2606 return; // again, it would be useless
2607 }
2608
2609 // Check for a comparison to a constant, and "know" that the compared
2610 // value is constrained on this path.
2611 assert(tcon->singleton(), "");
2612 ConstraintCastNode* ccast = nullptr;
2677 if (c->Opcode() == Op_CmpP &&
2678 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2679 c->in(2)->is_Con()) {
2680 Node* load_klass = nullptr;
2681 Node* decode = nullptr;
2682 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2683 decode = c->in(1);
2684 load_klass = c->in(1)->in(1);
2685 } else {
2686 load_klass = c->in(1);
2687 }
2688 if (load_klass->in(2)->is_AddP()) {
2689 Node* addp = load_klass->in(2);
2690 Node* obj = addp->in(AddPNode::Address);
2691 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2692 if (obj_type->speculative_type_not_null() != nullptr) {
2693 ciKlass* k = obj_type->speculative_type();
2694 inc_sp(2);
2695 obj = maybe_cast_profiled_obj(obj, k);
2696 dec_sp(2);
2697 if (obj->is_InlineType()) {
2698 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2699 obj = obj->as_InlineType()->get_oop();
2700 }
2701 // Make the CmpP use the casted obj
2702 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2703 load_klass = load_klass->clone();
2704 load_klass->set_req(2, addp);
2705 load_klass = _gvn.transform(load_klass);
2706 if (decode != nullptr) {
2707 decode = decode->clone();
2708 decode->set_req(1, load_klass);
2709 load_klass = _gvn.transform(decode);
2710 }
2711 c = c->clone();
2712 c->set_req(1, load_klass);
2713 c = _gvn.transform(c);
2714 }
2715 }
2716 }
2717 return c;
2718 }
2719
2720 //------------------------------do_one_bytecode--------------------------------
3514 // See if we can get some profile data and hand it off to the next block
3515 Block *target_block = block()->successor_for_bci(target_bci);
3516 if (target_block->pred_count() != 1) break;
3517 ciMethodData* methodData = method()->method_data();
3518 if (!methodData->is_mature()) break;
3519 ciProfileData* data = methodData->bci_to_data(bci());
3520 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3521 int taken = ((ciJumpData*)data)->taken();
3522 taken = method()->scale_count(taken);
3523 target_block->set_count(taken);
3524 break;
3525 }
3526
3527 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
3528 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3529 handle_if_null:
3530 // If this is a backwards branch in the bytecodes, add Safepoint
3531 maybe_add_safepoint(iter().get_dest());
3532 a = null();
3533 b = pop();
3534 if (b->is_InlineType()) {
3535 // Null checking a scalarized but nullable inline type. Check the IsInit
3536 // input instead of the oop input to avoid keeping buffer allocations alive
3537 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_is_init(), zerocon(T_INT)));
3538 } else {
3539 if (!_gvn.type(b)->speculative_maybe_null() &&
3540 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3541 inc_sp(1);
3542 Node* null_ctl = top();
3543 b = null_check_oop(b, &null_ctl, true, true, true);
3544 assert(null_ctl->is_top(), "no null control here");
3545 dec_sp(1);
3546 } else if (_gvn.type(b)->speculative_always_null() &&
3547 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3548 inc_sp(1);
3549 b = null_assert(b);
3550 dec_sp(1);
3551 }
3552 c = _gvn.transform( new CmpPNode(b, a) );
3553 }
3554 do_ifnull(btest, c);
3555 break;
3556
3557 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3558 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3559 handle_if_acmp:
3560 // If this is a backwards branch in the bytecodes, add Safepoint
3561 maybe_add_safepoint(iter().get_dest());
3562 a = pop();
3563 b = pop();
3564 do_acmp(btest, b, a);
3565 break;
3566
3567 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3568 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3569 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3570 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3571 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3572 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3573 handle_ifxx:
3574 // If this is a backwards branch in the bytecodes, add Safepoint
3575 maybe_add_safepoint(iter().get_dest());
3576 a = _gvn.intcon(0);
3577 b = pop();
3578 c = _gvn.transform( new CmpINode(b, a) );
3579 do_if(btest, c);
3580 break;
3581
3582 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3583 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3584 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3599 break;
3600
3601 case Bytecodes::_lookupswitch:
3602 do_lookupswitch();
3603 break;
3604
3605 case Bytecodes::_invokestatic:
3606 case Bytecodes::_invokedynamic:
3607 case Bytecodes::_invokespecial:
3608 case Bytecodes::_invokevirtual:
3609 case Bytecodes::_invokeinterface:
3610 do_call();
3611 break;
3612 case Bytecodes::_checkcast:
3613 do_checkcast();
3614 break;
3615 case Bytecodes::_instanceof:
3616 do_instanceof();
3617 break;
3618 case Bytecodes::_anewarray:
3619 do_newarray();
3620 break;
3621 case Bytecodes::_newarray:
3622 do_newarray((BasicType)iter().get_index());
3623 break;
3624 case Bytecodes::_multianewarray:
3625 do_multianewarray();
3626 break;
3627 case Bytecodes::_new:
3628 do_new();
3629 break;
3630
3631 case Bytecodes::_jsr:
3632 case Bytecodes::_jsr_w:
3633 do_jsr();
3634 break;
3635
3636 case Bytecodes::_ret:
3637 do_ret();
3638 break;
3639
|