5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciMethodData.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "interpreter/linkResolver.hpp"
29 #include "jvm_io.h"
30 #include "memory/resourceArea.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "opto/addnode.hpp"
34 #include "opto/castnode.hpp"
35 #include "opto/convertnode.hpp"
36 #include "opto/divnode.hpp"
37 #include "opto/idealGraphPrinter.hpp"
38 #include "opto/matcher.hpp"
39 #include "opto/memnode.hpp"
40 #include "opto/mulnode.hpp"
41 #include "opto/opaquenode.hpp"
42 #include "opto/parse.hpp"
43 #include "opto/runtime.hpp"
44 #include "opto/subtypenode.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 );
1450 // False branch
1451 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1452 set_control(iffalse);
1453
1454 if (stopped()) { // Path is dead?
1455 NOT_PRODUCT(explicit_null_checks_elided++);
1456 if (C->eliminate_boxing()) {
1457 // Mark the successor block as parsed
1458 next_block->next_path_num();
1459 }
1460 } else { // Path is live.
1461 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1462 }
1463
1464 if (do_stress_trap) {
1465 stress_trap(iff, counter, incr_store);
1466 }
1467 }
1468
1469 //------------------------------------do_if------------------------------------
1470 void Parse::do_if(BoolTest::mask btest, Node* c) {
1471 int target_bci = iter().get_dest();
1472
1473 Block* branch_block = successor_for_bci(target_bci);
1474 Block* next_block = successor_for_bci(iter().next_bci());
1475
1476 float cnt;
1477 float prob = branch_prediction(cnt, btest, target_bci, c);
1478 float untaken_prob = 1.0 - prob;
1479
1480 if (prob == PROB_UNKNOWN) {
1481 if (PrintOpto && Verbose) {
1482 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1483 }
1484 repush_if_args(); // to gather stats on loop
1485 uncommon_trap(Deoptimization::Reason_unreached,
1486 Deoptimization::Action_reinterpret,
1487 nullptr, "cold");
1488 if (C->eliminate_boxing()) {
1489 // Mark the successor blocks as parsed
1490 branch_block->next_path_num();
1541 }
1542
1543 // Generate real control flow
1544 float true_prob = (taken_if_true ? prob : untaken_prob);
1545 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1546 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1547 Node* taken_branch = new IfTrueNode(iff);
1548 Node* untaken_branch = new IfFalseNode(iff);
1549 if (!taken_if_true) { // Finish conversion to canonical form
1550 Node* tmp = taken_branch;
1551 taken_branch = untaken_branch;
1552 untaken_branch = tmp;
1553 }
1554
1555 // Branch is taken:
1556 { PreserveJVMState pjvms(this);
1557 taken_branch = _gvn.transform(taken_branch);
1558 set_control(taken_branch);
1559
1560 if (stopped()) {
1561 if (C->eliminate_boxing()) {
1562 // Mark the successor block as parsed
1563 branch_block->next_path_num();
1564 }
1565 } else {
1566 adjust_map_after_if(taken_btest, c, prob, branch_block);
1567 if (!stopped()) {
1568 merge(target_bci);
1569 }
1570 }
1571 }
1572
1573 untaken_branch = _gvn.transform(untaken_branch);
1574 set_control(untaken_branch);
1575
1576 // Branch not taken.
1577 if (stopped()) {
1578 if (C->eliminate_boxing()) {
1579 // Mark the successor block as parsed
1580 next_block->next_path_num();
1581 }
1582 } else {
1583 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1584 }
1585
1586 if (do_stress_trap) {
1587 stress_trap(iff, counter, incr_store);
1588 }
1589 }
1590
1591 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
1592 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
1593 // then either takes the trap or executes the original, unstable if.
1594 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
1595 // Search for an unstable if trap
1596 CallStaticJavaNode* trap = nullptr;
1597 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
1598 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
1599 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
1600 // No suitable trap found. Remove unused counter load and increment.
1601 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
1602 return;
1603 }
1604
1605 // Remove trap from optimization list since we add another path to the trap.
1606 bool success = C->remove_unstable_if_trap(trap, true);
1607 assert(success, "Trap already modified");
1608
1609 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
1610 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
1643 }
1644
1645 void Parse::maybe_add_predicate_after_if(Block* path) {
1646 if (path->is_SEL_head() && path->preds_parsed() == 0) {
1647 // Add predicates at bci of if dominating the loop so traps can be
1648 // recorded on the if's profile data
1649 int bc_depth = repush_if_args();
1650 add_parse_predicates();
1651 dec_sp(bc_depth);
1652 path->set_has_predicates();
1653 }
1654 }
1655
1656
1657 //----------------------------adjust_map_after_if------------------------------
1658 // Adjust the JVM state to reflect the result of taking this path.
1659 // Basically, it means inspecting the CmpNode controlling this
1660 // branch, seeing how it constrains a tested value, and then
1661 // deciding if it's worth our while to encode this constraint
1662 // as graph nodes in the current abstract interpretation map.
1663 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) {
1664 if (!c->is_Cmp()) {
1665 maybe_add_predicate_after_if(path);
1666 return;
1667 }
1668
1669 if (stopped() || btest == BoolTest::illegal) {
1670 return; // nothing to do
1671 }
1672
1673 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1674
1675 if (path_is_suitable_for_uncommon_trap(prob)) {
1676 repush_if_args();
1677 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
1678 Deoptimization::Action_reinterpret,
1679 nullptr,
1680 (is_fallthrough ? "taken always" : "taken never"));
1681
1682 if (call != nullptr) {
1683 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
1684 }
1685 return;
1686 }
1687
1688 Node* val = c->in(1);
1689 Node* con = c->in(2);
1690 const Type* tcon = _gvn.type(con);
1691 const Type* tval = _gvn.type(val);
1692 bool have_con = tcon->singleton();
1693 if (tval->singleton()) {
1694 if (!have_con) {
1695 // Swap, so constant is in con.
1696 con = val;
1697 tcon = tval;
1698 val = c->in(2);
1699 tval = _gvn.type(val);
1700 btest = BoolTest(btest).commute();
1701 have_con = true;
1702 } else {
1703 // Do we have two constants? Then leave well enough alone.
1704 have_con = false;
1705 }
1706 }
1707 if (!have_con) { // remaining adjustments need a con
1746 // as out parameters.
1747 static bool match_type_check(PhaseGVN& gvn,
1748 BoolTest::mask btest,
1749 Node* con, const Type* tcon,
1750 Node* val, const Type* tval,
1751 Node** obj, const TypeOopPtr** cast_type) { // out-parameters
1752 // Look for opportunities to sharpen the type of a node whose klass is compared with a constant klass.
1753 // The constant klass being tested against can come from many bytecode instructions (implicitly or explicitly),
1754 // and also from profile data used by speculative casts.
1755 if (btest == BoolTest::eq && tcon->isa_klassptr()) {
1756 // Found:
1757 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1758 // or the narrowOop equivalent.
1759 (*obj) = extract_obj_from_klass_load(&gvn, val);
1760 // Some klass comparisons are not directly in the form
1761 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq]),
1762 // e.g. Bool(CmpP(CastPP(LoadKlass(...)), ConP(klass)), [eq]).
1763 // These patterns with nullable klasses arise from example from
1764 // load_array_klass_from_mirror.
1765 if (*obj == nullptr) { return false; }
1766 (*cast_type) = tcon->isa_klassptr()->as_instance_type();
1767 return true; // found
1768 }
1769
1770 // Match an instanceof check.
1771 // During parsing its IR shape is not canonicalized yet.
1772 //
1773 // obj superklass
1774 // | |
1775 // SubTypeCheck
1776 // |
1777 // Bool [eq] / [ne]
1778 // |
1779 // If
1780 // / \
1781 // T F
1782 // \ /
1783 // Region
1784 // \ ConI ConI
1785 // \ | /
1786 // val -> Phi ConI <- con
1797 if (b1 != nullptr && b1->in(1)->isa_SubTypeCheck()) {
1798 assert(b1->_test._test == BoolTest::eq ||
1799 b1->_test._test == BoolTest::ne, "%d", b1->_test._test);
1800
1801 ProjNode* success_proj = if1->proj_out(b1->_test._test == BoolTest::eq ? 1 : 0);
1802 int idx = diamond->find_edge(success_proj);
1803 assert(idx == 1 || idx == 2, "");
1804 Node* vcon = val->in(idx);
1805
1806 if ((btest == BoolTest::eq && vcon == con) || (btest == BoolTest::ne && vcon != con)) {
1807 assert(val->find_edge(con) > 0, "mismatch");
1808 SubTypeCheckNode* sub = b1->in(1)->as_SubTypeCheck();
1809 Node* obj_or_subklass = sub->in(SubTypeCheckNode::ObjOrSubKlass);
1810 Node* superklass = sub->in(SubTypeCheckNode::SuperKlass);
1811
1812 if (gvn.type(obj_or_subklass)->isa_oopptr()) {
1813 const TypeKlassPtr* klass_ptr_type = gvn.type(superklass)->is_klassptr();
1814 const TypeKlassPtr* improved_klass_ptr_type = klass_ptr_type->try_improve();
1815
1816 (*obj) = obj_or_subklass;
1817 (*cast_type) = improved_klass_ptr_type->cast_to_exactness(false)->as_instance_type();
1818 return true; // found
1819 }
1820 }
1821 }
1822 }
1823 return false; // not found
1824 }
1825
1826 void Parse::sharpen_type_after_if(BoolTest::mask btest,
1827 Node* con, const Type* tcon,
1828 Node* val, const Type* tval) {
1829 Node* obj = nullptr;
1830 const TypeOopPtr* cast_type = nullptr;
1831 // Insert a cast node with a narrowed type after a successful type check.
1832 if (match_type_check(_gvn, btest, con, tcon, val, tval,
1833 &obj, &cast_type)) {
1834 assert(obj != nullptr && cast_type != nullptr, "missing type check info");
1835 const Type* obj_type = _gvn.type(obj);
1836 const Type* tboth = obj_type->filter_speculative(cast_type);
1837 assert(tboth->higher_equal(obj_type) && tboth->higher_equal(cast_type), "sanity");
1838 if (tboth == Type::TOP && KillPathsReachableByDeadTypeNode) {
1839 // Let dead type node cleaning logic prune effectively dead path for us.
1840 // CheckCastPP::Value() == TOP and it will trigger the cleanup during GVN.
1841 // Don't materialize the cast when cleanup is disabled, because
1842 // it kills data and control leaving IR in broken state.
1843 tboth = cast_type;
1844 }
1845 if (tboth != Type::TOP && tboth != obj_type) {
1846 int obj_in_map = map()->find_edge(obj);
1847 if (obj_in_map >= 0 &&
1848 (jvms()->is_loc(obj_in_map) || jvms()->is_stk(obj_in_map))) {
1849 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1850 // Delay transform() call to allow recovery of pre-cast value at the control merge.
1851 _gvn.set_type_bottom(ccast);
1852 record_for_igvn(ccast);
1853 // Here's the payoff.
1854 replace_in_map(obj, ccast);
1855 }
1856 }
1857 }
1858
1859 int val_in_map = map()->find_edge(val);
1860 if (val_in_map < 0) return; // replace_in_map would be useless
1861 {
1862 JVMState* jvms = this->jvms();
1863 if (!(jvms->is_loc(val_in_map) ||
1864 jvms->is_stk(val_in_map)))
1865 return; // again, it would be useless
1866 }
1867
1868 // Check for a comparison to a constant, and "know" that the compared
1869 // value is constrained on this path.
1870 assert(tcon->singleton(), "");
1871 ConstraintCastNode* ccast = nullptr;
1872 Node* cast = nullptr;
1936 if (c->Opcode() == Op_CmpP &&
1937 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1938 c->in(2)->is_Con()) {
1939 Node* load_klass = nullptr;
1940 Node* decode = nullptr;
1941 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1942 decode = c->in(1);
1943 load_klass = c->in(1)->in(1);
1944 } else {
1945 load_klass = c->in(1);
1946 }
1947 if (load_klass->in(2)->is_AddP()) {
1948 Node* addp = load_klass->in(2);
1949 Node* obj = addp->in(AddPNode::Address);
1950 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1951 if (obj_type->speculative_type_not_null() != nullptr) {
1952 ciKlass* k = obj_type->speculative_type();
1953 inc_sp(2);
1954 obj = maybe_cast_profiled_obj(obj, k);
1955 dec_sp(2);
1956 // Make the CmpP use the casted obj
1957 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1958 load_klass = load_klass->clone();
1959 load_klass->set_req(2, addp);
1960 load_klass = _gvn.transform(load_klass);
1961 if (decode != nullptr) {
1962 decode = decode->clone();
1963 decode->set_req(1, load_klass);
1964 load_klass = _gvn.transform(decode);
1965 }
1966 c = c->clone();
1967 c->set_req(1, load_klass);
1968 c = _gvn.transform(c);
1969 }
1970 }
1971 }
1972 return c;
1973 }
1974
1975 //------------------------------do_one_bytecode--------------------------------
2678 b = _gvn.transform( new ConvI2DNode(a));
2679 push_pair(b);
2680 break;
2681
2682 case Bytecodes::_iinc: // Increment local
2683 i = iter().get_index(); // Get local index
2684 set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2685 break;
2686
2687 // Exit points of synchronized methods must have an unlock node
2688 case Bytecodes::_return:
2689 return_current(nullptr);
2690 break;
2691
2692 case Bytecodes::_ireturn:
2693 case Bytecodes::_areturn:
2694 case Bytecodes::_freturn:
2695 return_current(pop());
2696 break;
2697 case Bytecodes::_lreturn:
2698 return_current(pop_pair());
2699 break;
2700 case Bytecodes::_dreturn:
2701 return_current(pop_pair());
2702 break;
2703
2704 case Bytecodes::_athrow:
2705 // null exception oop throws null pointer exception
2706 null_check(peek());
2707 if (stopped()) return;
2708 // Hook the thrown exception directly to subsequent handlers.
2709 if (BailoutToInterpreterForThrows) {
2710 // Keep method interpreted from now on.
2711 uncommon_trap(Deoptimization::Reason_unhandled,
2712 Deoptimization::Action_make_not_compilable);
2713 return;
2714 }
2715 if (env()->jvmti_can_post_on_exceptions()) {
2716 // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2717 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2718 }
2719 // Here if either can_post_on_exceptions or should_post_on_exceptions is false
2733 // See if we can get some profile data and hand it off to the next block
2734 Block *target_block = block()->successor_for_bci(target_bci);
2735 if (target_block->pred_count() != 1) break;
2736 ciMethodData* methodData = method()->method_data();
2737 if (!methodData->is_mature()) break;
2738 ciProfileData* data = methodData->bci_to_data(bci());
2739 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2740 int taken = ((ciJumpData*)data)->taken();
2741 taken = method()->scale_count(taken);
2742 target_block->set_count(taken);
2743 break;
2744 }
2745
2746 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
2747 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2748 handle_if_null:
2749 // If this is a backwards branch in the bytecodes, add Safepoint
2750 maybe_add_safepoint(iter().get_dest());
2751 a = null();
2752 b = pop();
2753 if (!_gvn.type(b)->speculative_maybe_null() &&
2754 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2755 inc_sp(1);
2756 Node* null_ctl = top();
2757 b = null_check_oop(b, &null_ctl, true, true, true);
2758 assert(null_ctl->is_top(), "no null control here");
2759 dec_sp(1);
2760 } else if (_gvn.type(b)->speculative_always_null() &&
2761 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2762 inc_sp(1);
2763 b = null_assert(b);
2764 dec_sp(1);
2765 }
2766 c = _gvn.transform( new CmpPNode(b, a) );
2767 do_ifnull(btest, c);
2768 break;
2769
2770 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2771 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2772 handle_if_acmp:
2773 // If this is a backwards branch in the bytecodes, add Safepoint
2774 maybe_add_safepoint(iter().get_dest());
2775 a = pop();
2776 b = pop();
2777 c = _gvn.transform( new CmpPNode(b, a) );
2778 c = optimize_cmp_with_klass(c);
2779 do_if(btest, c);
2780 break;
2781
2782 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2783 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2784 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2785 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2786 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2787 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2788 handle_ifxx:
2789 // If this is a backwards branch in the bytecodes, add Safepoint
2790 maybe_add_safepoint(iter().get_dest());
2791 a = _gvn.intcon(0);
2792 b = pop();
2793 c = _gvn.transform( new CmpINode(b, a) );
2794 do_if(btest, c);
2795 break;
2796
2797 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2798 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2799 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2814 break;
2815
2816 case Bytecodes::_lookupswitch:
2817 do_lookupswitch();
2818 break;
2819
2820 case Bytecodes::_invokestatic:
2821 case Bytecodes::_invokedynamic:
2822 case Bytecodes::_invokespecial:
2823 case Bytecodes::_invokevirtual:
2824 case Bytecodes::_invokeinterface:
2825 do_call();
2826 break;
2827 case Bytecodes::_checkcast:
2828 do_checkcast();
2829 break;
2830 case Bytecodes::_instanceof:
2831 do_instanceof();
2832 break;
2833 case Bytecodes::_anewarray:
2834 do_anewarray();
2835 break;
2836 case Bytecodes::_newarray:
2837 do_newarray((BasicType)iter().get_index());
2838 break;
2839 case Bytecodes::_multianewarray:
2840 do_multianewarray();
2841 break;
2842 case Bytecodes::_new:
2843 do_new();
2844 break;
2845
2846 case Bytecodes::_jsr:
2847 case Bytecodes::_jsr_w:
2848 do_jsr();
2849 break;
2850
2851 case Bytecodes::_ret:
2852 do_ret();
2853 break;
2854
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciInlineKlass.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 "opto/subtypenode.hpp"
49 #include "runtime/arguments.hpp"
50 #include "runtime/deoptimization.hpp"
51 #include "runtime/globals.hpp"
52 #include "runtime/sharedRuntime.hpp"
53
54 #ifndef PRODUCT
55 extern uint explicit_null_checks_inserted,
56 explicit_null_checks_elided;
57 #endif
58
59 Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) {
60 // Feed unused profile data to type speculation
61 if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
62 ciKlass* array_type = nullptr;
63 ciKlass* element_type = nullptr;
64 ProfilePtrKind element_ptr = ProfileMaybeNull;
65 bool flat_array = true;
66 bool null_free_array = true;
67 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
68 if (element_type != nullptr || element_ptr != ProfileMaybeNull) {
69 ld = record_profile_for_speculation(ld, element_type, element_ptr);
70 }
71 }
72 return ld;
73 }
74
75
76 //---------------------------------array_load----------------------------------
77 void Parse::array_load(BasicType bt) {
78 const Type* elemtype = Type::TOP;
79 Node* prep_array = prepare_array_addressing(bt, 0, elemtype);
80 if (stopped()) return; // guaranteed null or range check
81
82 Node* array_index = pop();
83 Node* array = pop();
84
85 // Handle inline type arrays
86 const TypeOopPtr* element_ptr = elemtype->make_oopptr();
87 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
88
89 if (!array_type->is_not_flat()) {
90 // Cannot statically determine if array is a flat array, emit runtime check
91 assert(UseArrayFlattening && is_reference_type(bt) && element_ptr->can_be_inline_type() &&
92 (!element_ptr->is_inlinetypeptr() || element_ptr->inline_klass()->maybe_flat_in_array()), "array can't be flat");
93 IdealKit ideal(this);
94 IdealVariable res(ideal);
95 ideal.declarations_done();
96 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
97 // Non-flat array
98 sync_kit(ideal);
99 if (!array_type->is_flat()) {
100 assert(array_type->is_flat() || control()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
101 // Loading from a non-flat array, casting array to not flat.
102 const TypeAryPtr* ary_type = _gvn.type(prep_array)->is_aryptr();
103 ary_type = ary_type->cast_to_not_flat();
104 Node* not_flat_ary = _gvn.transform(new CheckCastPPNode(control(), prep_array, ary_type));
105 Node* adr = get_ptr_to_array_element(not_flat_ary, array_index, bt, ary_type->size(), control());
106 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
107 DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
108 if (needs_range_check(ary_type->size(), array_index)) {
109 // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
110 // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
111 // possibly float above the range check at any point.
112 decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
113 }
114 Node* ld = access_load_at(not_flat_ary, adr, adr_type, element_ptr, bt, decorator_set);
115 if (element_ptr->is_inlinetypeptr()) {
116 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
117 }
118 ideal.set(res, ld);
119 }
120 ideal.sync_kit(this);
121 } ideal.else_(); {
122 // Flat array
123 sync_kit(ideal);
124 if (!array_type->is_not_flat()) {
125 if (element_ptr->is_inlinetypeptr()) {
126 ciInlineKlass* vk = element_ptr->inline_klass();
127 Node* flat_array = cast_to_flat_array(array, vk);
128 Node* vt = InlineTypeNode::make_from_flat_array(this, vk, flat_array, array_index);
129 ideal.set(res, vt);
130 } else {
131 // Element type is unknown, and thus we cannot statically determine the exact flat array layout. Emit a
132 // runtime call to correctly load the inline type element from the flat array.
133 Node* inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
134 bool is_null_free = array_type->is_null_free() ||
135 (!UseNullableAtomicValueFlattening && !UseNullableNonAtomicValueFlattening);
136 if (is_null_free) {
137 inline_type = cast_not_null(inline_type);
138 }
139 ideal.set(res, inline_type);
140 }
141 }
142 ideal.sync_kit(this);
143 } ideal.end_if();
144 sync_kit(ideal);
145 Node* ld = _gvn.transform(ideal.value(res));
146 ld = record_profile_for_speculation_at_array_load(ld);
147 push_node(bt, ld);
148 return;
149 }
150
151 if (elemtype == TypeInt::BOOL) {
152 bt = T_BOOLEAN;
153 }
154 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
155 Node* adr = get_ptr_to_array_element(prep_array, array_index, bt, array_type->size(), control());
156 Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
157 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
158 ld = record_profile_for_speculation_at_array_load(ld);
159 // Loading an inline type from a non-flat array
160 if (element_ptr != nullptr && element_ptr->is_inlinetypeptr()) {
161 assert(!array_type->is_null_free() || !element_ptr->maybe_null(), "inline type array elements should never be null");
162 ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
163 }
164 push_node(bt, ld);
165 }
166
167 Node* Parse::load_from_unknown_flat_array(Node* array, Node* array_index, const TypeOopPtr* element_ptr) {
168 // Below membars keep this access to an unknown flat array correctly
169 // ordered with other unknown and known flat array accesses.
170 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
171
172 Node* call = nullptr;
173 {
174 // Re-execute flat array load if runtime call triggers deoptimization
175 PreserveReexecuteState preexecs(this);
176 jvms()->set_bci(_bci);
177 jvms()->set_should_reexecute(true);
178 inc_sp(2);
179 kill_dead_locals();
180 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
181 OptoRuntime::load_unknown_inline_Type(),
182 OptoRuntime::load_unknown_inline_Java(),
183 nullptr, TypeRawPtr::BOTTOM,
184 array, array_index);
185 }
186 make_slow_call_ex(call, env()->Throwable_klass(), false);
187 Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
188
189 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
190
191 // Keep track of the information that the inline type is in flat arrays
192 const Type* unknown_value = element_ptr->is_instptr()->cast_to_flat_in_array();
193 return _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
194 }
195
196 //--------------------------------array_store----------------------------------
197 void Parse::array_store(BasicType bt) {
198 const Type* elemtype = Type::TOP;
199 Node* prep_array = prepare_array_addressing(bt, type2size[bt], elemtype);
200 if (stopped()) return; // guaranteed null or range check
201
202 Node* adr = get_ptr_to_array_element(prep_array, /* index */peek(0+type2size[bt]), bt,
203 _gvn.type(prep_array)->is_aryptr()->size(), control());
204
205 Node* stored_value_casted = nullptr;
206 if (bt == T_OBJECT) {
207 stored_value_casted = array_store_check(adr, elemtype);
208 if (stopped()) {
209 return;
210 }
211 }
212 Node* const stored_value = pop_node(bt); // Value to store
213 Node* const array_index = pop(); // Index in the array
214 Node* array = pop(); // The array itself
215
216 const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
217 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
218
219 if (elemtype == TypeInt::BOOL) {
220 bt = T_BOOLEAN;
221 } else if (bt == T_OBJECT) {
222 elemtype = elemtype->make_oopptr();
223 const Type* stored_value_casted_type = _gvn.type(stored_value_casted);
224 // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
225 // This is only legal for non-null stores because the array_store_check always passes for null, even
226 // if the array is null-free. Null stores are handled in GraphKit::inline_array_null_guard().
227 bool not_inline = !stored_value_casted_type->maybe_null() && !stored_value_casted_type->is_oopptr()->can_be_inline_type();
228 bool not_null_free = not_inline;
229 bool not_flat = not_inline || ( stored_value_casted_type->is_inlinetypeptr() &&
230 !stored_value_casted_type->inline_klass()->maybe_flat_in_array());
231 if (!array_type->is_not_null_free() && not_null_free) {
232 // Storing a non-inline type, mark array as not null-free.
233 array_type = array_type->cast_to_not_null_free();
234 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
235 replace_in_map(array, cast);
236 array = cast;
237 }
238 if (!array_type->is_not_flat() && not_flat) {
239 // Storing to a non-flat array, mark array as not flat.
240 array_type = array_type->cast_to_not_flat();
241 Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
242 replace_in_map(array, cast);
243 array = cast;
244 }
245
246 if (array_type->is_null_free() && elemtype->is_inlinetypeptr() && elemtype->inline_klass()->is_empty()) {
247 // Array of null-free empty inline type, there is only 1 state for the elements
248 assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
249 return;
250 }
251
252 if (!array_type->is_not_flat()) {
253 // Array might be a flat array, emit runtime checks (for null, a simple inline_array_null_guard is sufficient).
254 assert(UseArrayFlattening && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
255 (!array_type->klass_is_exact() || array_type->is_flat()), "array can't be a flat array");
256 // TODO 8350865 Depending on the available layouts, we can avoid this check in below flat/not-flat branches. Also the safe_for_replace arg is now always true.
257 array = inline_array_null_guard(array, stored_value_casted, 3, true);
258 // Reload array type which could have been updated by inline_array_null_guard().
259 array_type = _gvn.type(array)->is_aryptr();
260 IdealKit ideal(this);
261 ideal.if_then(flat_array_test(array, /* flat = */ false)); {
262 // Non-flat array
263 if (!array_type->is_flat()) {
264 sync_kit(ideal);
265 assert(array_type->is_not_flat() || ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
266 inc_sp(3);
267 access_store_at(array, adr, adr_type, stored_value_casted, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
268 dec_sp(3);
269 ideal.sync_kit(this);
270 }
271 } ideal.else_(); {
272 // Flat array
273 sync_kit(ideal);
274 if (!array_type->is_not_flat()) {
275 // Try to determine the inline klass type of the stored value
276 ciInlineKlass* vk = nullptr;
277 if (stored_value_casted_type->is_inlinetypeptr()) {
278 vk = stored_value_casted_type->inline_klass();
279 } else if (elemtype->is_inlinetypeptr()) {
280 vk = elemtype->inline_klass();
281 }
282
283 if (vk != nullptr) {
284 // Element type is known, cast and store to flat array layout.
285 Node* flat_array = cast_to_flat_array(array, vk);
286
287 // Re-execute flat array store if buffering triggers deoptimization
288 PreserveReexecuteState preexecs(this);
289 jvms()->set_should_reexecute(true);
290 inc_sp(3);
291
292 if (!stored_value_casted->is_InlineType()) {
293 assert(_gvn.type(stored_value_casted) == TypePtr::NULL_PTR, "Unexpected value");
294 stored_value_casted = InlineTypeNode::make_null(_gvn, vk);
295 }
296
297 stored_value_casted->as_InlineType()->store_flat_array(this, flat_array, array_index);
298 } else {
299 // Element type is unknown, emit a runtime call since the flat array layout is not statically known.
300 store_to_unknown_flat_array(array, array_index, stored_value_casted);
301 }
302 }
303 ideal.sync_kit(this);
304 }
305 ideal.end_if();
306 sync_kit(ideal);
307 return;
308 } else if (!array_type->is_not_null_free()) {
309 // Array is not flat but may be null free
310 assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
311 array = inline_array_null_guard(array, stored_value_casted, 3, true);
312 }
313 }
314 inc_sp(3);
315 access_store_at(array, adr, adr_type, stored_value, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
316 dec_sp(3);
317 }
318
319 // Emit a runtime call to store to a flat array whose element type is either unknown (i.e. we do not know the flat
320 // array layout) or not exact (could have different flat array layouts at runtime).
321 void Parse::store_to_unknown_flat_array(Node* array, Node* const idx, Node* non_null_stored_value) {
322 // Below membars keep this access to an unknown flat array correctly
323 // ordered with other unknown and known flat array accesses.
324 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
325
326 Node* call = nullptr;
327 {
328 // Re-execute flat array store if runtime call triggers deoptimization
329 PreserveReexecuteState preexecs(this);
330 jvms()->set_bci(_bci);
331 jvms()->set_should_reexecute(true);
332 inc_sp(3);
333 kill_dead_locals();
334 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
335 OptoRuntime::store_unknown_inline_Type(),
336 OptoRuntime::store_unknown_inline_Java(),
337 nullptr, TypeRawPtr::BOTTOM,
338 non_null_stored_value, array, idx);
339 }
340 make_slow_call_ex(call, env()->Throwable_klass(), false);
341
342 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
343 }
344
345 //------------------------------array_addressing-------------------------------
346 // Pull array and index from the stack. Compute pointer-to-element.
347 Node* Parse::prepare_array_addressing(BasicType type, int vals, const Type*& elemtype) {
348 Node *idx = peek(0+vals); // Get from stack without popping
349 Node *ary = peek(1+vals); // in case of exception
350
351 // Null check the array base, with correct stack contents
352 ary = null_check(ary, T_ARRAY);
353 // Compile-time detect of null-exception?
354 if (stopped()) return top();
355
356 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr();
357 const TypeInt* sizetype = arytype->size();
358 elemtype = arytype->elem();
359
360 if (UseUniqueSubclasses) {
361 const Type* el = elemtype->make_ptr();
362 if (el && el->isa_instptr()) {
363 const TypeInstPtr* toop = el->is_instptr();
364 if (toop->instance_klass()->unique_concrete_subklass()) {
365 // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
366 const Type* subklass = Type::get_const_type(toop->instance_klass());
367 elemtype = subklass->join_speculative(el);
368 }
369 }
370 }
371
372 if (!arytype->is_loaded()) {
373 // Only fails for some -Xcomp runs
374 // The class is unloaded. We have to run this bytecode in the interpreter.
375 ciKlass* klass = arytype->unloaded_klass();
376
377 uncommon_trap(Deoptimization::Reason_unloaded,
378 Deoptimization::Action_reinterpret,
379 klass, "!loaded array");
380 return top();
381 }
382
383 ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);
384
385 if (needs_range_check(sizetype, idx)) {
386 create_range_check(idx, ary, sizetype);
387 } else if (C->log() != nullptr) {
388 C->log()->elem("observe that='!need_range_check'");
389 }
390
391 // Check for always knowing you are throwing a range-check exception
392 if (stopped()) return top();
393
394 return ary;
395 }
396
397 Node* Parse::get_ptr_to_array_element(Node* array, Node* idx, BasicType elembt, const TypeInt* sizetype, Node* control) {
398 // Make array address computation control dependent to prevent it
399 // from floating above the range check during loop optimizations.
400 Node* ptr = array_element_address(array, idx, elembt, sizetype, control);
401 assert(ptr != top(), "top should go hand-in-hand with stopped");
402
403 return ptr;
404 }
405
406 // 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
407 // be greater or equal the smallest possible array size (i.e. out-of-bounds).
408 bool Parse::needs_range_check(const TypeInt* size_type, const Node* index) const {
409 const TypeInt* index_type = _gvn.type(index)->is_int();
410 return index_type->_hi >= size_type->_lo || index_type->_lo < 0;
411 }
412
413 void Parse::create_range_check(Node* idx, Node* ary, const TypeInt* sizetype) {
414 Node* tst;
415 if (sizetype->_hi <= 0) {
416 // The greatest array bound is negative, so we can conclude that we're
417 // compiling unreachable code, but the unsigned compare trick used below
418 // only works with non-negative lengths. Instead, hack "tst" to be zero so
419 // the uncommon_trap path will always be taken.
420 tst = _gvn.intcon(0);
421 } else {
422 // Range is constant in array-oop, so we can use the original state of mem
423 Node* len = load_array_length(ary);
424
425 // Test length vs index (standard trick using unsigned compare)
426 Node* chk = _gvn.transform(new CmpUNode(idx, len) );
427 BoolTest::mask btest = BoolTest::lt;
428 tst = _gvn.transform(new BoolNode(chk, btest) );
429 }
430 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
431 _gvn.set_type(rc, rc->Value(&_gvn));
432 if (!tst->is_Con()) {
433 record_for_igvn(rc);
434 }
435 set_control(_gvn.transform(new IfTrueNode(rc)));
436 // Branch to failure if out of bounds
437 {
438 PreserveJVMState pjvms(this);
439 set_control(_gvn.transform(new IfFalseNode(rc)));
440 if (C->allow_range_check_smearing()) {
441 // Do not use builtin_throw, since range checks are sometimes
442 // made more stringent by an optimistic transformation.
443 // This creates "tentative" range checks at this point,
444 // which are not guaranteed to throw exceptions.
445 // See IfNode::Ideal, is_range_check, adjust_check.
446 uncommon_trap(Deoptimization::Reason_range_check,
447 Deoptimization::Action_make_not_entrant,
448 nullptr, "range_check");
449 } else {
450 // If we have already recompiled with the range-check-widening
451 // heroic optimization turned off, then we must really be throwing
452 // range check exceptions.
453 builtin_throw(Deoptimization::Reason_range_check);
454 }
455 }
456 }
457
458 // For inline type arrays, we can use the profiling information for array accesses to speculate on the type, flatness,
459 // and null-freeness. We can either prepare the speculative type for later uses or emit explicit speculative checks with
460 // traps now. In the latter case, the speculative type guarantees can avoid additional runtime checks later (e.g.
461 // non-null-free implies non-flat which allows us to remove flatness checks). This makes the graph simpler.
462 Node* Parse::create_speculative_inline_type_array_checks(Node* array, const TypeAryPtr* array_type,
463 const Type*& element_type) {
464 if (!array_type->is_flat() && !array_type->is_not_flat()) {
465 // For arrays that might be flat, speculate that the array has the exact type reported in the profile data such that
466 // we can rely on a fixed memory layout (i.e. either a flat layout or not).
467 array = cast_to_speculative_array_type(array, array_type, element_type);
468 } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
469 // Array is known to be either flat or not flat. If possible, update the speculative type by using the profile data
470 // at this bci.
471 array = cast_to_profiled_array_type(array);
472 }
473
474 // Even though the type does not tell us whether we have an inline type array or not, we can still check the profile data
475 // whether we have a non-null-free or non-flat array. Speculating on a non-null-free array doesn't help aaload but could
476 // be profitable for a subsequent aastore.
477 if (!array_type->is_null_free() && !array_type->is_not_null_free()) {
478 array = speculate_non_null_free_array(array, array_type);
479 }
480 if (!array_type->is_flat() && !array_type->is_not_flat()) {
481 array = speculate_non_flat_array(array, array_type);
482 }
483 return array;
484 }
485
486 // Speculate that the array has the exact type reported in the profile data. We emit a trap when this turns out to be
487 // wrong. On the fast path, we add a CheckCastPP to use the exact type.
488 Node* Parse::cast_to_speculative_array_type(Node* const array, const TypeAryPtr*& array_type, const Type*& element_type) {
489 Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
490 ciKlass* speculative_array_type = array_type->speculative_type();
491 if (too_many_traps_or_recompiles(reason) || speculative_array_type == nullptr) {
492 // No speculative type, check profile data at this bci
493 speculative_array_type = nullptr;
494 reason = Deoptimization::Reason_class_check;
495 if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
496 ciKlass* profiled_element_type = nullptr;
497 ProfilePtrKind element_ptr = ProfileMaybeNull;
498 bool flat_array = true;
499 bool null_free_array = true;
500 method()->array_access_profiled_type(bci(), speculative_array_type, profiled_element_type, element_ptr, flat_array,
501 null_free_array);
502 }
503 }
504 if (speculative_array_type != nullptr) {
505 // Speculate that this array has the exact type reported by profile data
506 Node* casted_array = nullptr;
507 DEBUG_ONLY(Node* old_control = control();)
508 Node* slow_ctl = type_check_receiver(array, speculative_array_type, 1.0, &casted_array);
509 if (stopped()) {
510 // The check always fails and therefore profile information is incorrect. Don't use it.
511 assert(old_control == slow_ctl, "type check should have been removed");
512 set_control(slow_ctl);
513 } else if (!slow_ctl->is_top()) {
514 { PreserveJVMState pjvms(this);
515 set_control(slow_ctl);
516 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
517 }
518 replace_in_map(array, casted_array);
519 array_type = _gvn.type(casted_array)->is_aryptr();
520 element_type = array_type->elem();
521 return casted_array;
522 }
523 }
524 return array;
525 }
526
527 // Create a CheckCastPP when the speculative type can improve the current type.
528 Node* Parse::cast_to_profiled_array_type(Node* const array) {
529 ciKlass* array_type = nullptr;
530 ciKlass* element_type = nullptr;
531 ProfilePtrKind element_ptr = ProfileMaybeNull;
532 bool flat_array = true;
533 bool null_free_array = true;
534 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
535 if (array_type != nullptr) {
536 return record_profile_for_speculation(array, array_type, ProfileMaybeNull);
537 }
538 return array;
539 }
540
541 // Speculate that the array is non-null-free. We emit a trap when this turns out to be
542 // wrong. On the fast path, we add a CheckCastPP to use the non-null-free type.
543 Node* Parse::speculate_non_null_free_array(Node* const array, const TypeAryPtr*& array_type) {
544 bool null_free_array = true;
545 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
546 if (array_type->speculative() != nullptr &&
547 array_type->speculative()->is_aryptr()->is_not_null_free() &&
548 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
549 null_free_array = false;
550 reason = Deoptimization::Reason_speculate_class_check;
551 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
552 ciKlass* profiled_array_type = nullptr;
553 ciKlass* profiled_element_type = nullptr;
554 ProfilePtrKind element_ptr = ProfileMaybeNull;
555 bool flat_array = true;
556 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
557 null_free_array);
558 reason = Deoptimization::Reason_class_check;
559 }
560 if (!null_free_array) {
561 { // Deoptimize if null-free array
562 BuildCutout unless(this, null_free_array_test(array, /* null_free = */ false), PROB_MAX);
563 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
564 }
565 assert(!stopped(), "null-free array should have been caught earlier");
566 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_null_free()));
567 replace_in_map(array, casted_array);
568 array_type = _gvn.type(casted_array)->is_aryptr();
569 return casted_array;
570 }
571 return array;
572 }
573
574 // Speculate that the array is non-flat. We emit a trap when this turns out to be wrong.
575 // On the fast path, we add a CheckCastPP to use the non-flat type.
576 Node* Parse::speculate_non_flat_array(Node* const array, const TypeAryPtr* const array_type) {
577 bool flat_array = true;
578 Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
579 if (array_type->speculative() != nullptr &&
580 array_type->speculative()->is_aryptr()->is_not_flat() &&
581 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
582 flat_array = false;
583 reason = Deoptimization::Reason_speculate_class_check;
584 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
585 ciKlass* profiled_array_type = nullptr;
586 ciKlass* profiled_element_type = nullptr;
587 ProfilePtrKind element_ptr = ProfileMaybeNull;
588 bool null_free_array = true;
589 method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
590 null_free_array);
591 reason = Deoptimization::Reason_class_check;
592 }
593 if (!flat_array) {
594 { // Deoptimize if flat array
595 BuildCutout unless(this, flat_array_test(array, /* flat = */ false), PROB_MAX);
596 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
597 }
598 assert(!stopped(), "flat array should have been caught earlier");
599 Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_flat()));
600 replace_in_map(array, casted_array);
601 return casted_array;
602 }
603 return array;
604 }
605
606 // returns IfNode
607 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
608 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
609 Node *tst = _gvn.transform(new BoolNode(cmp, mask));
610 IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
611 return iff;
612 }
613
614
615 // sentinel value for the target bci to mark never taken branches
616 // (according to profiling)
617 static const int never_reached = INT_MAX;
618
619 //------------------------------helper for tableswitch-------------------------
620 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
621 // True branch, use existing map info
622 { PreserveJVMState pjvms(this);
623 Node *iftrue = _gvn.transform( new IfTrueNode (iff) );
624 set_control( iftrue );
1842 // False branch
1843 Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1844 set_control(iffalse);
1845
1846 if (stopped()) { // Path is dead?
1847 NOT_PRODUCT(explicit_null_checks_elided++);
1848 if (C->eliminate_boxing()) {
1849 // Mark the successor block as parsed
1850 next_block->next_path_num();
1851 }
1852 } else { // Path is live.
1853 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1854 }
1855
1856 if (do_stress_trap) {
1857 stress_trap(iff, counter, incr_store);
1858 }
1859 }
1860
1861 //------------------------------------do_if------------------------------------
1862 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken, Node** mem_taken, Node** io_taken) {
1863 int target_bci = iter().get_dest();
1864
1865 Block* branch_block = successor_for_bci(target_bci);
1866 Block* next_block = successor_for_bci(iter().next_bci());
1867
1868 float cnt;
1869 float prob = branch_prediction(cnt, btest, target_bci, c);
1870 float untaken_prob = 1.0 - prob;
1871
1872 if (prob == PROB_UNKNOWN) {
1873 if (PrintOpto && Verbose) {
1874 tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1875 }
1876 repush_if_args(); // to gather stats on loop
1877 uncommon_trap(Deoptimization::Reason_unreached,
1878 Deoptimization::Action_reinterpret,
1879 nullptr, "cold");
1880 if (C->eliminate_boxing()) {
1881 // Mark the successor blocks as parsed
1882 branch_block->next_path_num();
1933 }
1934
1935 // Generate real control flow
1936 float true_prob = (taken_if_true ? prob : untaken_prob);
1937 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1938 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1939 Node* taken_branch = new IfTrueNode(iff);
1940 Node* untaken_branch = new IfFalseNode(iff);
1941 if (!taken_if_true) { // Finish conversion to canonical form
1942 Node* tmp = taken_branch;
1943 taken_branch = untaken_branch;
1944 untaken_branch = tmp;
1945 }
1946
1947 // Branch is taken:
1948 { PreserveJVMState pjvms(this);
1949 taken_branch = _gvn.transform(taken_branch);
1950 set_control(taken_branch);
1951
1952 if (stopped()) {
1953 if (C->eliminate_boxing() && !new_path) {
1954 // Mark the successor block as parsed (if we haven't created a new path)
1955 branch_block->next_path_num();
1956 }
1957 } else {
1958 adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1959 if (!stopped()) {
1960 if (new_path) {
1961 // Merge by using a new path
1962 merge_new_path(target_bci);
1963 } else if (ctrl_taken != nullptr) {
1964 // Don't merge but save taken branch to be wired by caller
1965 *ctrl_taken = control();
1966 if (mem_taken != nullptr) {
1967 *mem_taken = reset_memory();
1968 }
1969 if (io_taken != nullptr) {
1970 *io_taken = i_o();
1971 }
1972 } else {
1973 merge(target_bci);
1974 }
1975 }
1976 }
1977 }
1978
1979 untaken_branch = _gvn.transform(untaken_branch);
1980 set_control(untaken_branch);
1981
1982 // Branch not taken.
1983 if (stopped() && ctrl_taken == nullptr) {
1984 if (C->eliminate_boxing()) {
1985 // Mark the successor block as parsed (if caller does not re-wire control flow)
1986 next_block->next_path_num();
1987 }
1988 } else {
1989 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
1990 }
1991
1992 if (do_stress_trap) {
1993 stress_trap(iff, counter, incr_store);
1994 }
1995 }
1996
1997
1998 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
1999 if (t->speculative() == nullptr) {
2000 return ProfileUnknownNull;
2001 }
2002 if (t->speculative_always_null()) {
2003 return ProfileAlwaysNull;
2004 }
2005 if (t->speculative_maybe_null()) {
2006 return ProfileMaybeNull;
2007 }
2008 return ProfileNeverNull;
2009 }
2010
2011 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
2012 if (btest == BoolTest::ne) {
2013 {
2014 PreserveJVMState pjvms(this);
2015 inc_sp(2);
2016 null_check_common(input, T_OBJECT, true, nullptr,
2017 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
2018 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
2019 dec_sp(2);
2020 int target_bci = iter().get_dest();
2021 merge(target_bci);
2022 }
2023 record_for_igvn(eq_region);
2024 set_control(_gvn.transform(eq_region));
2025 } else {
2026 inc_sp(2);
2027 null_check_common(input, T_OBJECT, true, nullptr,
2028 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
2029 speculative_ptr_kind(tinput) == ProfileAlwaysNull);
2030 dec_sp(2);
2031 }
2032 }
2033
2034 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
2035 inc_sp(2);
2036 null_ctl = top();
2037 Node* cast = null_check_oop(input, &null_ctl,
2038 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
2039 false,
2040 speculative_ptr_kind(tinput) == ProfileNeverNull &&
2041 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
2042 dec_sp(2);
2043 return cast;
2044 }
2045
2046 void Parse::acmp_type_check_or_trap(Node** non_null_input, ciKlass* input_type, Deoptimization::DeoptReason reason) {
2047 Node* slow_ctl = type_check_receiver(*non_null_input, input_type, 1.0, non_null_input);
2048 {
2049 PreserveJVMState pjvms(this);
2050 inc_sp(2);
2051 set_control(slow_ctl);
2052 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2053 }
2054 }
2055
2056 void Parse::acmp_type_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, ciKlass* input_type, BoolTest::mask btest, Node* eq_region) {
2057 Node* null_ctl;
2058 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2059
2060 if (input_type != nullptr) {
2061 Deoptimization::DeoptReason reason;
2062 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2063 reason = Deoptimization::Reason_speculate_class_check;
2064 } else {
2065 reason = Deoptimization::Reason_class_check;
2066 }
2067 acmp_type_check_or_trap(&cast, input_type, reason);
2068 } else {
2069 // No specific type, check for inline type
2070 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2071 inc_sp(2);
2072 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2073 }
2074
2075 Node* ne_region = new RegionNode(2);
2076 ne_region->add_req(null_ctl);
2077 ne_region->add_req(control());
2078
2079 record_for_igvn(ne_region);
2080 set_control(_gvn.transform(ne_region));
2081 if (btest == BoolTest::ne) {
2082 {
2083 PreserveJVMState pjvms(this);
2084 if (null_ctl == top()) {
2085 replace_in_map(input, cast);
2086 }
2087 int target_bci = iter().get_dest();
2088 merge(target_bci);
2089 }
2090 record_for_igvn(eq_region);
2091 set_control(_gvn.transform(eq_region));
2092 } else {
2093 if (null_ctl == top()) {
2094 replace_in_map(input, cast);
2095 }
2096 set_control(_gvn.transform(ne_region));
2097 }
2098 }
2099
2100 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2101 ciKlass* left_type = nullptr;
2102 ciKlass* right_type = nullptr;
2103 ProfilePtrKind left_ptr = ProfileUnknownNull;
2104 ProfilePtrKind right_ptr = ProfileUnknownNull;
2105 bool left_inline_type = true;
2106 bool right_inline_type = true;
2107
2108 // Leverage profiling at acmp
2109 if (UseACmpProfile) {
2110 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2111 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2112 left_type = nullptr;
2113 right_type = nullptr;
2114 left_inline_type = true;
2115 right_inline_type = true;
2116 }
2117 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2118 left_ptr = ProfileUnknownNull;
2119 right_ptr = ProfileUnknownNull;
2120 }
2121 }
2122
2123 if (UseTypeSpeculation) {
2124 record_profile_for_speculation(left, left_type, left_ptr);
2125 record_profile_for_speculation(right, right_type, right_ptr);
2126 }
2127
2128 if (!Arguments::is_valhalla_enabled()) {
2129 Node* cmp = CmpP(left, right);
2130 cmp = optimize_cmp_with_klass(cmp);
2131 do_if(btest, cmp);
2132 return;
2133 }
2134
2135 // Check for equality before potentially allocating
2136 if (left == right) {
2137 do_if(btest, makecon(TypeInt::CC_EQ));
2138 return;
2139 }
2140
2141 // Allocate inline type operands and re-execute on deoptimization
2142 if (left->is_InlineType()) {
2143 PreserveReexecuteState preexecs(this);
2144 inc_sp(2);
2145 jvms()->set_should_reexecute(true);
2146 left = left->as_InlineType()->buffer(this);
2147 }
2148 if (right->is_InlineType()) {
2149 PreserveReexecuteState preexecs(this);
2150 inc_sp(2);
2151 jvms()->set_should_reexecute(true);
2152 right = right->as_InlineType()->buffer(this);
2153 }
2154
2155 // First, do a normal pointer comparison
2156 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2157 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2158 Node* cmp = CmpP(left, right);
2159 record_for_igvn(cmp);
2160 cmp = optimize_cmp_with_klass(cmp);
2161 if (tleft == nullptr || !tleft->can_be_inline_type() ||
2162 tright == nullptr || !tright->can_be_inline_type()) {
2163 // This is sufficient, if one of the operands can't be an inline type
2164 do_if(btest, cmp);
2165 return;
2166 }
2167
2168 // Don't add traps to unstable if branches because additional checks are required to
2169 // decide if the operands are equal/substitutable and we therefore shouldn't prune
2170 // branches for one if based on the profiling of the acmp branches.
2171 // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2172 // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2173 // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2174 const bool can_trap = true;
2175
2176 Node* eq_region = nullptr;
2177 if (btest == BoolTest::eq) {
2178 do_if(btest, cmp, !can_trap, true);
2179 if (stopped()) {
2180 // Pointers are equal, operands must be equal
2181 return;
2182 }
2183 } else {
2184 assert(btest == BoolTest::ne, "only eq or ne");
2185 Node* is_not_equal = nullptr;
2186 eq_region = new RegionNode(4);
2187 {
2188 PreserveJVMState pjvms(this);
2189 // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2190 do_if(btest, cmp, !can_trap, false, &is_not_equal);
2191 if (!stopped()) {
2192 eq_region->init_req(1, control());
2193 }
2194 }
2195 if (is_not_equal == nullptr || is_not_equal->is_top()) {
2196 record_for_igvn(eq_region);
2197 set_control(_gvn.transform(eq_region));
2198 return;
2199 }
2200 set_control(is_not_equal);
2201 }
2202
2203 // Prefer speculative types if available
2204 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2205 if (tleft->speculative_type() != nullptr) {
2206 left_type = tleft->speculative_type();
2207 }
2208 if (tright->speculative_type() != nullptr) {
2209 right_type = tright->speculative_type();
2210 }
2211 }
2212
2213 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2214 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2215 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2216 left_ptr = speculative_left_ptr;
2217 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2218 left_ptr = speculative_left_ptr;
2219 }
2220 }
2221 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2222 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2223 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2224 right_ptr = speculative_right_ptr;
2225 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2226 right_ptr = speculative_right_ptr;
2227 }
2228 }
2229
2230 if (left_ptr == ProfileAlwaysNull) {
2231 // Comparison with null. Assert the input is indeed null and we're done.
2232 acmp_always_null_input(left, tleft, btest, eq_region);
2233 return;
2234 }
2235 if (right_ptr == ProfileAlwaysNull) {
2236 // Comparison with null. Assert the input is indeed null and we're done.
2237 acmp_always_null_input(right, tright, btest, eq_region);
2238 return;
2239 }
2240 if (left_type != nullptr && !left_type->is_inlinetype()) {
2241 // Comparison with an object of known type
2242 acmp_type_check(left, tleft, left_ptr, left_type, btest, eq_region);
2243 return;
2244 }
2245 if (right_type != nullptr && !right_type->is_inlinetype()) {
2246 // Comparison with an object of known type
2247 acmp_type_check(right, tright, right_ptr, right_type, btest, eq_region);
2248 return;
2249 }
2250 if (!left_inline_type) {
2251 // Comparison with an object known not to be an inline type
2252 acmp_type_check(left, tleft, left_ptr, nullptr, btest, eq_region);
2253 return;
2254 }
2255 if (!right_inline_type) {
2256 // Comparison with an object known not to be an inline type
2257 acmp_type_check(right, tright, right_ptr, nullptr, btest, eq_region);
2258 return;
2259 }
2260
2261 // Pointers are not equal, check if first operand is non-null
2262 Node* ne_region = new RegionNode(7);
2263 Node* null_ctl = nullptr;
2264 Node* not_null_left = nullptr;
2265 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2266 ne_region->init_req(1, null_ctl);
2267
2268 Node* kls_right = nullptr;
2269 if (!stopped()) {
2270 // First operand is non-null, check if it is the speculative inline type if possible
2271 // (which later allows isSubstitutable to be intrinsified), or any inline type if no
2272 // speculation is available.
2273 if (right_type != nullptr && right_type->is_inlinetype()) {
2274 acmp_type_check_or_trap(¬_null_right, right_type, Deoptimization::Reason_speculate_class_check);
2275 } else {
2276 Node* is_value = inline_type_test(not_null_right);
2277 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2278 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2279 ne_region->init_req(2, not_value);
2280 set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2281 }
2282
2283 // The first operand is an inline type, check if the second operand is non-null
2284 not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2285 ne_region->init_req(3, null_ctl);
2286 if (!stopped()) {
2287 // Check if lhs operand is of a specific speculative inline type (see above).
2288 // If not, we don't need to enforce that the lhs is a value object since we know
2289 // it already for the rhs, and must enforce that they have the same type.
2290 if (left_type != nullptr && left_type->is_inlinetype()) {
2291 acmp_type_check_or_trap(¬_null_left, left_type, Deoptimization::Reason_speculate_class_check);
2292 }
2293 if (!stopped()) {
2294 // Check if both operands are of the same class.
2295 Node* kls_left = load_object_klass(not_null_left);
2296 kls_right = load_object_klass(not_null_right);
2297 Node* kls_cmp = CmpP(kls_left, kls_right);
2298 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2299 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2300 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2301 set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2302 ne_region->init_req(4, kls_ne);
2303 }
2304 }
2305 }
2306
2307 if (stopped()) {
2308 record_for_igvn(ne_region);
2309 set_control(_gvn.transform(ne_region));
2310 if (btest == BoolTest::ne) {
2311 {
2312 PreserveJVMState pjvms(this);
2313 int target_bci = iter().get_dest();
2314 merge(target_bci);
2315 }
2316 record_for_igvn(eq_region);
2317 set_control(_gvn.transform(eq_region));
2318 }
2319 return;
2320 }
2321 assert(kls_right != nullptr, "");
2322
2323 IfNode* mask_iff = nullptr;
2324 // If any operand has a precisely known type, isSubstitutable will be intrinsified, so we don't need the fast path
2325 if (UseAcmpFastPath && !_gvn.type(not_null_left)->is_inlinetypeptr() && !_gvn.type(not_null_right)->is_inlinetypeptr()) {
2326 /* Here, we are generating the fast path (the slow path being the call to isSubstitutable)
2327 * See the declarations of _fast_acmp_offset and _fast_acmp_mask in InlineKlass::Members
2328 * for details about the fast path logic, and the meaning of these values.
2329 */
2330 Node* members_addr = off_heap_plus_addr(kls_right, in_bytes(InlineKlass::adr_members_offset()));
2331 Node* members = make_load(control(), members_addr, TypeRawPtr::BOTTOM, T_ADDRESS, MemNode::unordered);
2332 Node* offset_addr = off_heap_plus_addr(members, in_bytes(InlineKlass::fast_acmp_offset_offset()));
2333 Node* offset = make_load(control(), offset_addr, TypeInt::INT, T_INT, MemNode::unordered);
2334
2335 Node* offset_cmp = CmpI(offset, zerocon(T_INT));
2336 Node* offset_bol = _gvn.transform(new BoolNode(offset_cmp, BoolTest::lt));
2337 mask_iff = create_and_map_if(control(), offset_bol, PROB_FAIR, COUNT_UNKNOWN);
2338 Node* slow_path_ctl = _gvn.transform(new IfTrueNode(mask_iff));
2339 Node* fast_path_ctl = _gvn.transform(new IfFalseNode(mask_iff));
2340 set_control(slow_path_ctl);
2341
2342 {
2343 PreserveJVMState jvms(this);
2344 set_control(fast_path_ctl);
2345
2346 Node* offset_l = ConvI2L(offset);
2347 Node* fast_acmp_mask_addr = off_heap_plus_addr(members, in_bytes(InlineKlass::fast_acmp_mask_offset()));
2348 Node* fast_acmp_mask = make_load(control(), fast_acmp_mask_addr, TypeLong::LONG, T_LONG, MemNode::unordered);
2349
2350 // *(left + offset) & mask == *(right + offset) & mask
2351 Node* left_payload_addr = basic_plus_adr(not_null_left, offset_l);
2352 Node* left_payload = make_load(control(), left_payload_addr, TypeLong::LONG, T_LONG, MemNode::unordered, LoadNNode::DependsOnlyOnTest, false, true, true, true);
2353 Node* left_masked = _gvn.transform(new AndLNode(left_payload, fast_acmp_mask));
2354
2355 Node* right_payload_addr = basic_plus_adr(not_null_right, offset_l);
2356 Node* right_payload = make_load(control(), right_payload_addr, TypeLong::LONG, T_LONG, MemNode::unordered, LoadNNode::DependsOnlyOnTest, false, true, true, true);
2357 Node* right_masked = _gvn.transform(new AndLNode(right_payload, fast_acmp_mask));
2358
2359 Node* masked_cmp = CmpL(left_masked, right_masked);
2360
2361 Node* ctl = C->top();
2362 if (btest == BoolTest::eq) {
2363 PreserveJVMState pjvms(this);
2364 do_if(btest, masked_cmp, !can_trap, true, nullptr);
2365 if (!stopped()) {
2366 ctl = control();
2367 }
2368 } else {
2369 assert(btest == BoolTest::ne, "only eq or ne");
2370 PreserveJVMState pjvms(this);
2371 do_if(btest, masked_cmp, !can_trap, false, &ctl);
2372 if (!stopped()) {
2373 eq_region->init_req(3, control());
2374 }
2375 }
2376 ne_region->init_req(6, ctl);
2377 }
2378 }
2379
2380 // Both operands are values types of the same class, we need to perform a
2381 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2382 Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2383 Node* mem = reset_memory();
2384 Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2385
2386 Node* eq_io_phi = nullptr;
2387 Node* eq_mem_phi = nullptr;
2388 if (eq_region != nullptr) {
2389 eq_io_phi = PhiNode::make(eq_region, i_o());
2390 eq_mem_phi = PhiNode::make(eq_region, mem);
2391 }
2392
2393 set_all_memory(mem);
2394
2395 kill_dead_locals();
2396 ciSymbol* subst_method_name = ciSymbols::isSubstitutable_name();
2397 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(subst_method_name, ciSymbols::object_object_boolean_signature());
2398 CallStaticJavaNode* call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2399 call->set_override_symbolic_info(true);
2400 call->init_req(TypeFunc::Parms, not_null_left);
2401 call->init_req(TypeFunc::Parms+1, not_null_right);
2402 inc_sp(2);
2403 set_edges_for_java_call(call, false, false);
2404 Node* ret = set_results_for_java_call(call, false, true);
2405 dec_sp(2);
2406
2407 assert(acmp_fast_path_if_from_substitutable_call(&_gvn, call) == mask_iff, "");
2408
2409 // Test the return value of ValueObjectMethods::isSubstitutable()
2410 // This is the last check, do_if can emit traps now.
2411 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2412 Node* ctl = C->top();
2413 Node* mem_taken = nullptr;
2414 Node* io_taken = nullptr;
2415 if (btest == BoolTest::eq) {
2416 PreserveJVMState pjvms(this);
2417 // Also merges branch block.
2418 do_if(btest, subst_cmp, can_trap, false, nullptr, &mem_taken, &io_taken);
2419 if (!stopped()) {
2420 ctl = control();
2421 mem_taken = reset_memory();
2422 io_taken = i_o();
2423 }
2424 } else {
2425 assert(btest == BoolTest::ne, "only eq or ne");
2426 PreserveJVMState pjvms(this);
2427 do_if(btest, subst_cmp, can_trap, false, &ctl, &mem_taken, &io_taken);
2428 if (!stopped()) {
2429 eq_region->init_req(2, control());
2430 eq_io_phi->init_req(2, i_o());
2431 eq_mem_phi->init_req(2, reset_memory());
2432 }
2433 }
2434 ne_region->init_req(5, ctl);
2435 ne_io_phi->init_req(5, io_taken);
2436 ne_mem_phi->init_req(5, mem_taken);
2437
2438 // BoolTest::eq: ne_region is fall-through block.
2439 // BoolTest::ne: ne_region is branch block -> merge below.
2440 record_for_igvn(ne_region);
2441 set_control(_gvn.transform(ne_region));
2442 set_i_o(_gvn.transform(ne_io_phi));
2443 set_all_memory(_gvn.transform(ne_mem_phi));
2444
2445 if (btest == BoolTest::ne) {
2446 int target_bci = iter().get_dest();
2447 if (!stopped()) {
2448 PreserveJVMState pjvms(this);
2449 merge(target_bci);
2450 } else if (C->eliminate_boxing()) {
2451 // Mark the branch block as parsed.
2452 Block* branch_block = successor_for_bci(target_bci);
2453 branch_block->next_path_num();
2454 }
2455
2456 // Fall-through block.
2457 record_for_igvn(eq_region);
2458 set_control(_gvn.transform(eq_region));
2459 set_i_o(_gvn.transform(eq_io_phi));
2460 set_all_memory(_gvn.transform(eq_mem_phi));
2461 }
2462 }
2463
2464 /* Detects whether a call to isSubstitutable is under an IfNode guarding the fast path for acmp.
2465 * If so, returns the IfNode branching between the call and the fast path. Returns null otherwise.
2466 *
2467 * The fast path is a LOT easier to generate at parsing time, but can be later proven useless if further
2468 * optimization narrows down the type of operands and allows intrinsification of the substitutability
2469 * check. In this case, the fast path might still apply, but it comes with various downsides, such as
2470 * mismatch access that may hinder optimizations, or buffering requirement. So, when intrinsifying the call,
2471 * we try to remove the fast path.
2472 *
2473 * This test isn't so bad. Loading the fast acmp offset is pretty unique to the fast acmp path.
2474 *
2475 * Clearly, this is only a step before a proper solution for acmp, such as a macro node.
2476 */
2477 IfNode* Parse::acmp_fast_path_if_from_substitutable_call(PhaseGVN* phase, CallStaticJavaNode* call) {
2478 auto is_con_offset = [](Node* node, ByteSize n) -> bool {
2479 if (!node->is_Con()) return false;
2480 TypeNode* con = node->as_Type();
2481 assert(con->type()->is_intptr_t(), "");
2482 return con->type()->is_intptr_t()->is_con(in_bytes(n));
2483 };
2484
2485 assert(call->in(TypeFunc::Control) != nullptr, "");
2486 if (!call->in(TypeFunc::Control)->is_IfProj()) return nullptr;
2487 IfProjNode* if_proj = call->in(TypeFunc::Control)->as_IfProj();
2488 if (if_proj->_con != 1) return nullptr;
2489
2490 assert(if_proj->in(0) != nullptr, "");
2491 assert(if_proj->in(0)->is_If(), "");
2492 IfNode* iff = if_proj->in(0)->as_If();
2493
2494 assert(iff->in(1) != nullptr, "");
2495 if (!iff->in(1)->is_Bool()) return nullptr;
2496 BoolNode* lt = iff->in(1)->as_Bool();
2497 if (lt->_test._test != BoolTest::lt) return nullptr;
2498
2499 assert(lt->in(1) != nullptr, "");
2500 if (lt->in(1)->Opcode() != Op_CmpI) return nullptr;
2501 CmpNode* cmp_i = lt->in(1)->as_Cmp();
2502
2503 assert(cmp_i->in(1) != nullptr, "");
2504 assert(cmp_i->in(2) != nullptr, "");
2505
2506 if (cmp_i->in(1)->Opcode() != Op_LoadI) return nullptr;
2507 LoadNode* load_offset = cmp_i->in(1)->as_Load();
2508 if (!cmp_i->in(2)->is_ConI()) return nullptr;
2509 ConINode* zero_i = cmp_i->in(2)->as_ConI();
2510 assert(zero_i->type()->is_int() != nullptr, "");
2511 if (!zero_i->type()->is_int()->is_con(0)) return nullptr;
2512
2513 assert(load_offset->in(2) != nullptr, "");
2514 if (!load_offset->in(2)->is_AddP()) return nullptr;
2515 AddPNode* offset_addr_add = load_offset->in(2)->as_AddP();
2516
2517 assert(offset_addr_add->in(AddPNode::Base) != nullptr, "");
2518 assert(offset_addr_add->in(AddPNode::Address) != nullptr, "");
2519 assert(offset_addr_add->in(AddPNode::Offset) != nullptr, "");
2520 if (!offset_addr_add->in(AddPNode::Base)->is_top()) return nullptr;
2521 if (offset_addr_add->in(AddPNode::Address)->Opcode() != Op_LoadP) return nullptr;
2522 LoadNode* load_members = offset_addr_add->in(AddPNode::Address)->as_Load();
2523 if (!is_con_offset(offset_addr_add->in(AddPNode::Offset), InlineKlass::fast_acmp_offset_offset())) return nullptr;
2524
2525 assert(load_members->in(2) != nullptr, "");
2526 if (!load_members->in(2)->is_AddP()) return nullptr;
2527 AddPNode* members_addr_add = load_members->in(2)->as_AddP();
2528
2529 assert(members_addr_add->in(AddPNode::Base) != nullptr, "");
2530 assert(members_addr_add->in(AddPNode::Address) != nullptr, "");
2531 assert(members_addr_add->in(AddPNode::Offset) != nullptr, "");
2532 if (!members_addr_add->in(AddPNode::Base)->is_top()) return nullptr;
2533 if (!phase->type(members_addr_add->in(AddPNode::Address))->isa_instklassptr()) return nullptr;
2534 if (!is_con_offset(members_addr_add->in(AddPNode::Offset), InlineKlass::adr_members_offset())) return nullptr;
2535
2536 return iff;
2537 }
2538
2539 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
2540 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
2541 // then either takes the trap or executes the original, unstable if.
2542 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
2543 // Search for an unstable if trap
2544 CallStaticJavaNode* trap = nullptr;
2545 assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
2546 ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
2547 if (trap == nullptr || !trap->jvms()->should_reexecute()) {
2548 // No suitable trap found. Remove unused counter load and increment.
2549 C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
2550 return;
2551 }
2552
2553 // Remove trap from optimization list since we add another path to the trap.
2554 bool success = C->remove_unstable_if_trap(trap, true);
2555 assert(success, "Trap already modified");
2556
2557 // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
2558 int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]
2591 }
2592
2593 void Parse::maybe_add_predicate_after_if(Block* path) {
2594 if (path->is_SEL_head() && path->preds_parsed() == 0) {
2595 // Add predicates at bci of if dominating the loop so traps can be
2596 // recorded on the if's profile data
2597 int bc_depth = repush_if_args();
2598 add_parse_predicates();
2599 dec_sp(bc_depth);
2600 path->set_has_predicates();
2601 }
2602 }
2603
2604
2605 //----------------------------adjust_map_after_if------------------------------
2606 // Adjust the JVM state to reflect the result of taking this path.
2607 // Basically, it means inspecting the CmpNode controlling this
2608 // branch, seeing how it constrains a tested value, and then
2609 // deciding if it's worth our while to encode this constraint
2610 // as graph nodes in the current abstract interpretation map.
2611 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2612 if (!c->is_Cmp()) {
2613 maybe_add_predicate_after_if(path);
2614 return;
2615 }
2616
2617 if (stopped() || btest == BoolTest::illegal) {
2618 return; // nothing to do
2619 }
2620
2621 bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2622
2623 if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2624 repush_if_args();
2625 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2626 Deoptimization::Action_reinterpret,
2627 nullptr,
2628 (is_fallthrough ? "taken always" : "taken never"));
2629
2630 if (call != nullptr) {
2631 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2632 }
2633 return;
2634 }
2635
2636 if (c->is_FlatArrayCheck()) {
2637 maybe_add_predicate_after_if(path);
2638 return;
2639 }
2640
2641 Node* val = c->in(1);
2642 Node* con = c->in(2);
2643 const Type* tcon = _gvn.type(con);
2644 const Type* tval = _gvn.type(val);
2645 bool have_con = tcon->singleton();
2646 if (tval->singleton()) {
2647 if (!have_con) {
2648 // Swap, so constant is in con.
2649 con = val;
2650 tcon = tval;
2651 val = c->in(2);
2652 tval = _gvn.type(val);
2653 btest = BoolTest(btest).commute();
2654 have_con = true;
2655 } else {
2656 // Do we have two constants? Then leave well enough alone.
2657 have_con = false;
2658 }
2659 }
2660 if (!have_con) { // remaining adjustments need a con
2699 // as out parameters.
2700 static bool match_type_check(PhaseGVN& gvn,
2701 BoolTest::mask btest,
2702 Node* con, const Type* tcon,
2703 Node* val, const Type* tval,
2704 Node** obj, const TypeOopPtr** cast_type) { // out-parameters
2705 // Look for opportunities to sharpen the type of a node whose klass is compared with a constant klass.
2706 // The constant klass being tested against can come from many bytecode instructions (implicitly or explicitly),
2707 // and also from profile data used by speculative casts.
2708 if (btest == BoolTest::eq && tcon->isa_klassptr()) {
2709 // Found:
2710 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2711 // or the narrowOop equivalent.
2712 (*obj) = extract_obj_from_klass_load(&gvn, val);
2713 // Some klass comparisons are not directly in the form
2714 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq]),
2715 // e.g. Bool(CmpP(CastPP(LoadKlass(...)), ConP(klass)), [eq]).
2716 // These patterns with nullable klasses arise from example from
2717 // load_array_klass_from_mirror.
2718 if (*obj == nullptr) { return false; }
2719 (*cast_type) = tcon->isa_klassptr()->as_exact_instance_type();
2720 return true; // found
2721 }
2722
2723 // Match an instanceof check.
2724 // During parsing its IR shape is not canonicalized yet.
2725 //
2726 // obj superklass
2727 // | |
2728 // SubTypeCheck
2729 // |
2730 // Bool [eq] / [ne]
2731 // |
2732 // If
2733 // / \
2734 // T F
2735 // \ /
2736 // Region
2737 // \ ConI ConI
2738 // \ | /
2739 // val -> Phi ConI <- con
2750 if (b1 != nullptr && b1->in(1)->isa_SubTypeCheck()) {
2751 assert(b1->_test._test == BoolTest::eq ||
2752 b1->_test._test == BoolTest::ne, "%d", b1->_test._test);
2753
2754 ProjNode* success_proj = if1->proj_out(b1->_test._test == BoolTest::eq ? 1 : 0);
2755 int idx = diamond->find_edge(success_proj);
2756 assert(idx == 1 || idx == 2, "");
2757 Node* vcon = val->in(idx);
2758
2759 if ((btest == BoolTest::eq && vcon == con) || (btest == BoolTest::ne && vcon != con)) {
2760 assert(val->find_edge(con) > 0, "mismatch");
2761 SubTypeCheckNode* sub = b1->in(1)->as_SubTypeCheck();
2762 Node* obj_or_subklass = sub->in(SubTypeCheckNode::ObjOrSubKlass);
2763 Node* superklass = sub->in(SubTypeCheckNode::SuperKlass);
2764
2765 if (gvn.type(obj_or_subklass)->isa_oopptr()) {
2766 const TypeKlassPtr* klass_ptr_type = gvn.type(superklass)->is_klassptr();
2767 const TypeKlassPtr* improved_klass_ptr_type = klass_ptr_type->try_improve();
2768
2769 (*obj) = obj_or_subklass;
2770 (*cast_type) = improved_klass_ptr_type->as_subtype_instance_type();
2771 return true; // found
2772 }
2773 }
2774 }
2775 }
2776 return false; // not found
2777 }
2778
2779 void Parse::sharpen_type_after_if(BoolTest::mask btest,
2780 Node* con, const Type* tcon,
2781 Node* val, const Type* tval) {
2782 Node* obj = nullptr;
2783 const TypeOopPtr* cast_type = nullptr;
2784 // Insert a cast node with a narrowed type after a successful type check.
2785 if (match_type_check(_gvn, btest, con, tcon, val, tval,
2786 &obj, &cast_type)) {
2787 assert(obj != nullptr && cast_type != nullptr, "missing type check info");
2788 const Type* obj_type = _gvn.type(obj);
2789 const Type* tboth = obj_type->filter_speculative(cast_type);
2790 assert(tboth->higher_equal(obj_type) && tboth->higher_equal(cast_type), "sanity");
2791 if (tboth == Type::TOP && KillPathsReachableByDeadTypeNode) {
2792 // Let dead type node cleaning logic prune effectively dead path for us.
2793 // CheckCastPP::Value() == TOP and it will trigger the cleanup during GVN.
2794 // Don't materialize the cast when cleanup is disabled, because
2795 // it kills data and control leaving IR in broken state.
2796 tboth = cast_type;
2797 }
2798 if (tboth != Type::TOP && tboth != obj_type) {
2799 int obj_in_map = map()->find_edge(obj);
2800 if (obj_in_map >= 0 &&
2801 (jvms()->is_loc(obj_in_map) || jvms()->is_stk(obj_in_map))) {
2802 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2803 // Delay transform() call to allow recovery of pre-cast value at the control merge.
2804 _gvn.set_type_bottom(ccast);
2805 record_for_igvn(ccast);
2806 if (tboth->is_inlinetypeptr()) {
2807 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->isa_oopptr()->exact_klass(true)->as_inline_klass());
2808 }
2809 // Here's the payoff.
2810 replace_in_map(obj, ccast);
2811 }
2812 }
2813 }
2814
2815 int val_in_map = map()->find_edge(val);
2816 if (val_in_map < 0) return; // replace_in_map would be useless
2817 {
2818 JVMState* jvms = this->jvms();
2819 if (!(jvms->is_loc(val_in_map) ||
2820 jvms->is_stk(val_in_map)))
2821 return; // again, it would be useless
2822 }
2823
2824 // Check for a comparison to a constant, and "know" that the compared
2825 // value is constrained on this path.
2826 assert(tcon->singleton(), "");
2827 ConstraintCastNode* ccast = nullptr;
2828 Node* cast = nullptr;
2892 if (c->Opcode() == Op_CmpP &&
2893 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2894 c->in(2)->is_Con()) {
2895 Node* load_klass = nullptr;
2896 Node* decode = nullptr;
2897 if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2898 decode = c->in(1);
2899 load_klass = c->in(1)->in(1);
2900 } else {
2901 load_klass = c->in(1);
2902 }
2903 if (load_klass->in(2)->is_AddP()) {
2904 Node* addp = load_klass->in(2);
2905 Node* obj = addp->in(AddPNode::Address);
2906 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2907 if (obj_type->speculative_type_not_null() != nullptr) {
2908 ciKlass* k = obj_type->speculative_type();
2909 inc_sp(2);
2910 obj = maybe_cast_profiled_obj(obj, k);
2911 dec_sp(2);
2912 if (obj->is_InlineType()) {
2913 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2914 obj = obj->as_InlineType()->get_oop();
2915 }
2916 // Make the CmpP use the casted obj
2917 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2918 load_klass = load_klass->clone();
2919 load_klass->set_req(2, addp);
2920 load_klass = _gvn.transform(load_klass);
2921 if (decode != nullptr) {
2922 decode = decode->clone();
2923 decode->set_req(1, load_klass);
2924 load_klass = _gvn.transform(decode);
2925 }
2926 c = c->clone();
2927 c->set_req(1, load_klass);
2928 c = _gvn.transform(c);
2929 }
2930 }
2931 }
2932 return c;
2933 }
2934
2935 //------------------------------do_one_bytecode--------------------------------
3638 b = _gvn.transform( new ConvI2DNode(a));
3639 push_pair(b);
3640 break;
3641
3642 case Bytecodes::_iinc: // Increment local
3643 i = iter().get_index(); // Get local index
3644 set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
3645 break;
3646
3647 // Exit points of synchronized methods must have an unlock node
3648 case Bytecodes::_return:
3649 return_current(nullptr);
3650 break;
3651
3652 case Bytecodes::_ireturn:
3653 case Bytecodes::_areturn:
3654 case Bytecodes::_freturn:
3655 return_current(pop());
3656 break;
3657 case Bytecodes::_lreturn:
3658 case Bytecodes::_dreturn:
3659 return_current(pop_pair());
3660 break;
3661
3662 case Bytecodes::_athrow:
3663 // null exception oop throws null pointer exception
3664 null_check(peek());
3665 if (stopped()) return;
3666 // Hook the thrown exception directly to subsequent handlers.
3667 if (BailoutToInterpreterForThrows) {
3668 // Keep method interpreted from now on.
3669 uncommon_trap(Deoptimization::Reason_unhandled,
3670 Deoptimization::Action_make_not_compilable);
3671 return;
3672 }
3673 if (env()->jvmti_can_post_on_exceptions()) {
3674 // check if we must post exception events, take uncommon trap if so (with must_throw = false)
3675 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
3676 }
3677 // Here if either can_post_on_exceptions or should_post_on_exceptions is false
3691 // See if we can get some profile data and hand it off to the next block
3692 Block *target_block = block()->successor_for_bci(target_bci);
3693 if (target_block->pred_count() != 1) break;
3694 ciMethodData* methodData = method()->method_data();
3695 if (!methodData->is_mature()) break;
3696 ciProfileData* data = methodData->bci_to_data(bci());
3697 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3698 int taken = ((ciJumpData*)data)->taken();
3699 taken = method()->scale_count(taken);
3700 target_block->set_count(taken);
3701 break;
3702 }
3703
3704 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null;
3705 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3706 handle_if_null:
3707 // If this is a backwards branch in the bytecodes, add Safepoint
3708 maybe_add_safepoint(iter().get_dest());
3709 a = null();
3710 b = pop();
3711 if (b->is_InlineType()) {
3712 // Null checking a scalarized but nullable inline type. Check the null marker
3713 // input instead of the oop input to avoid keeping buffer allocations alive
3714 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_null_marker(), zerocon(T_INT)));
3715 } else {
3716 if (!_gvn.type(b)->speculative_maybe_null() &&
3717 !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3718 inc_sp(1);
3719 Node* null_ctl = top();
3720 b = null_check_oop(b, &null_ctl, true, true, true);
3721 assert(null_ctl->is_top(), "no null control here");
3722 dec_sp(1);
3723 } else if (_gvn.type(b)->speculative_always_null() &&
3724 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3725 inc_sp(1);
3726 b = null_assert(b);
3727 dec_sp(1);
3728 }
3729 c = _gvn.transform( new CmpPNode(b, a) );
3730 }
3731 do_ifnull(btest, c);
3732 break;
3733
3734 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3735 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3736 handle_if_acmp:
3737 // If this is a backwards branch in the bytecodes, add Safepoint
3738 maybe_add_safepoint(iter().get_dest());
3739 a = pop();
3740 b = pop();
3741 do_acmp(btest, b, a);
3742 break;
3743
3744 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3745 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3746 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3747 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3748 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3749 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3750 handle_ifxx:
3751 // If this is a backwards branch in the bytecodes, add Safepoint
3752 maybe_add_safepoint(iter().get_dest());
3753 a = _gvn.intcon(0);
3754 b = pop();
3755 c = _gvn.transform( new CmpINode(b, a) );
3756 do_if(btest, c);
3757 break;
3758
3759 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3760 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3761 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3776 break;
3777
3778 case Bytecodes::_lookupswitch:
3779 do_lookupswitch();
3780 break;
3781
3782 case Bytecodes::_invokestatic:
3783 case Bytecodes::_invokedynamic:
3784 case Bytecodes::_invokespecial:
3785 case Bytecodes::_invokevirtual:
3786 case Bytecodes::_invokeinterface:
3787 do_call();
3788 break;
3789 case Bytecodes::_checkcast:
3790 do_checkcast();
3791 break;
3792 case Bytecodes::_instanceof:
3793 do_instanceof();
3794 break;
3795 case Bytecodes::_anewarray:
3796 do_newarray();
3797 break;
3798 case Bytecodes::_newarray:
3799 do_newarray((BasicType)iter().get_index());
3800 break;
3801 case Bytecodes::_multianewarray:
3802 do_multianewarray();
3803 break;
3804 case Bytecodes::_new:
3805 do_new();
3806 break;
3807
3808 case Bytecodes::_jsr:
3809 case Bytecodes::_jsr_w:
3810 do_jsr();
3811 break;
3812
3813 case Bytecodes::_ret:
3814 do_ret();
3815 break;
3816
|