< prev index next >

src/hotspot/share/opto/parse2.cpp

Print this page

   1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciMethodData.hpp"

  27 #include "classfile/vmSymbols.hpp"
  28 #include "compiler/compileLog.hpp"
  29 #include "interpreter/linkResolver.hpp"
  30 #include "jvm_io.h"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "opto/addnode.hpp"
  35 #include "opto/castnode.hpp"
  36 #include "opto/convertnode.hpp"
  37 #include "opto/divnode.hpp"
  38 #include "opto/idealGraphPrinter.hpp"


  39 #include "opto/matcher.hpp"
  40 #include "opto/memnode.hpp"
  41 #include "opto/mulnode.hpp"
  42 #include "opto/opaquenode.hpp"
  43 #include "opto/parse.hpp"
  44 #include "opto/runtime.hpp"
  45 #include "runtime/deoptimization.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 
  48 #ifndef PRODUCT
  49 extern uint explicit_null_checks_inserted,
  50             explicit_null_checks_elided;
  51 #endif
  52 

















  53 //---------------------------------array_load----------------------------------
  54 void Parse::array_load(BasicType bt) {
  55   const Type* elemtype = Type::TOP;
  56   bool big_val = bt == T_DOUBLE || bt == T_LONG;
  57   Node* adr = array_addressing(bt, 0, elemtype);
  58   if (stopped())  return;     // guaranteed null or range check
  59 
  60   pop();                      // index (already used)
  61   Node* array = pop();        // the array itself


















































































  62 
  63   if (elemtype == TypeInt::BOOL) {
  64     bt = T_BOOLEAN;
  65   }
  66   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
  67 
  68   Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
  69                             IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
  70   if (big_val) {
  71     push_pair(ld);
  72   } else {
  73     push(ld);

  74   }

  75 }
  76 




























  77 
  78 //--------------------------------array_store----------------------------------
  79 void Parse::array_store(BasicType bt) {
  80   const Type* elemtype = Type::TOP;
  81   bool big_val = bt == T_DOUBLE || bt == T_LONG;
  82   Node* adr = array_addressing(bt, big_val ? 2 : 1, elemtype);
  83   if (stopped())  return;     // guaranteed null or range check

  84   if (bt == T_OBJECT) {
  85     array_store_check();
  86     if (stopped()) {
  87       return;
  88     }
  89   }
  90   Node* val;                  // Oop to store
  91   if (big_val) {
  92     val = pop_pair();
  93   } else {
  94     val = pop();
  95   }
  96   pop();                      // index (already used)
  97   Node* array = pop();        // the array itself
  98 
  99   if (elemtype == TypeInt::BOOL) {
 100     bt = T_BOOLEAN;



















































































































 101   }
 102   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
 103 
 104   access_store_at(array, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 105 }
 106 

























 107 
 108 //------------------------------array_addressing-------------------------------
 109 // Pull array and index from the stack.  Compute pointer-to-element.
 110 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
 111   Node *idx   = peek(0+vals);   // Get from stack without popping
 112   Node *ary   = peek(1+vals);   // in case of exception
 113 
 114   // Null check the array base, with correct stack contents
 115   ary = null_check(ary, T_ARRAY);
 116   // Compile-time detect of null-exception?
 117   if (stopped())  return top();
 118 
 119   const TypeAryPtr* arytype  = _gvn.type(ary)->is_aryptr();
 120   const TypeInt*    sizetype = arytype->size();
 121   elemtype = arytype->elem();
 122 
 123   if (UseUniqueSubclasses) {
 124     const Type* el = elemtype->make_ptr();
 125     if (el && el->isa_instptr()) {
 126       const TypeInstPtr* toop = el->is_instptr();
 127       if (toop->instance_klass()->unique_concrete_subklass()) {
 128         // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
 129         const Type* subklass = Type::get_const_type(toop->instance_klass());
 130         elemtype = subklass->join_speculative(el);
 131       }
 132     }
 133   }
 134 
 135   // Check for big class initializers with all constant offsets
 136   // feeding into a known-size array.
 137   const TypeInt* idxtype = _gvn.type(idx)->is_int();
 138   // See if the highest idx value is less than the lowest array bound,
 139   // and if the idx value cannot be negative:
 140   bool need_range_check = true;
 141   if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
 142     need_range_check = false;
 143     if (C->log() != nullptr)   C->log()->elem("observe that='!need_range_check'");
 144   }
 145 
 146   if (!arytype->is_loaded()) {
 147     // Only fails for some -Xcomp runs
 148     // The class is unloaded.  We have to run this bytecode in the interpreter.
 149     ciKlass* klass = arytype->unloaded_klass();
 150 
 151     uncommon_trap(Deoptimization::Reason_unloaded,
 152                   Deoptimization::Action_reinterpret,
 153                   klass, "!loaded array");
 154     return top();
 155   }
 156 
 157   // Do the range check
 158   if (need_range_check) {
 159     Node* tst;
 160     if (sizetype->_hi <= 0) {
 161       // The greatest array bound is negative, so we can conclude that we're
 162       // compiling unreachable code, but the unsigned compare trick used below
 163       // only works with non-negative lengths.  Instead, hack "tst" to be zero so
 164       // the uncommon_trap path will always be taken.
 165       tst = _gvn.intcon(0);
 166     } else {
 167       // Range is constant in array-oop, so we can use the original state of mem
 168       Node* len = load_array_length(ary);
 169 
 170       // Test length vs index (standard trick using unsigned compare)
 171       Node* chk = _gvn.transform( new CmpUNode(idx, len) );
 172       BoolTest::mask btest = BoolTest::lt;
 173       tst = _gvn.transform( new BoolNode(chk, btest) );
 174     }
 175     RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
 176     _gvn.set_type(rc, rc->Value(&_gvn));
 177     if (!tst->is_Con()) {
 178       record_for_igvn(rc);
 179     }
 180     set_control(_gvn.transform(new IfTrueNode(rc)));
 181     // Branch to failure if out of bounds
 182     {
 183       PreserveJVMState pjvms(this);
 184       set_control(_gvn.transform(new IfFalseNode(rc)));
 185       if (C->allow_range_check_smearing()) {
 186         // Do not use builtin_throw, since range checks are sometimes
 187         // made more stringent by an optimistic transformation.
 188         // This creates "tentative" range checks at this point,
 189         // which are not guaranteed to throw exceptions.
 190         // See IfNode::Ideal, is_range_check, adjust_check.
 191         uncommon_trap(Deoptimization::Reason_range_check,
 192                       Deoptimization::Action_make_not_entrant,
 193                       nullptr, "range_check");
 194       } else {
 195         // If we have already recompiled with the range-check-widening
 196         // heroic optimization turned off, then we must really be throwing
 197         // range check exceptions.
 198         builtin_throw(Deoptimization::Reason_range_check);
 199       }
 200     }
 201   }

 202   // Check for always knowing you are throwing a range-check exception
 203   if (stopped())  return top();
 204 
 205   // Make array address computation control dependent to prevent it
 206   // from floating above the range check during loop optimizations.
 207   Node* ptr = array_element_address(ary, idx, type, sizetype, control());
 208   assert(ptr != top(), "top should go hand-in-hand with stopped");
 209 
 210   return ptr;
 211 }
 212 








































































































































































































 213 
 214 // returns IfNode
 215 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
 216   Node   *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
 217   Node   *tst = _gvn.transform(new BoolNode(cmp, mask));
 218   IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
 219   return iff;
 220 }
 221 
 222 
 223 // sentinel value for the target bci to mark never taken branches
 224 // (according to profiling)
 225 static const int never_reached = INT_MAX;
 226 
 227 //------------------------------helper for tableswitch-------------------------
 228 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
 229   // True branch, use existing map info
 230   { PreserveJVMState pjvms(this);
 231     Node *iftrue  = _gvn.transform( new IfTrueNode (iff) );
 232     set_control( iftrue );

1446   // False branch
1447   Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1448   set_control(iffalse);
1449 
1450   if (stopped()) {              // Path is dead?
1451     NOT_PRODUCT(explicit_null_checks_elided++);
1452     if (C->eliminate_boxing()) {
1453       // Mark the successor block as parsed
1454       next_block->next_path_num();
1455     }
1456   } else  {                     // Path is live.
1457     adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1458   }
1459 
1460   if (do_stress_trap) {
1461     stress_trap(iff, counter, incr_store);
1462   }
1463 }
1464 
1465 //------------------------------------do_if------------------------------------
1466 void Parse::do_if(BoolTest::mask btest, Node* c) {
1467   int target_bci = iter().get_dest();
1468 
1469   Block* branch_block = successor_for_bci(target_bci);
1470   Block* next_block   = successor_for_bci(iter().next_bci());
1471 
1472   float cnt;
1473   float prob = branch_prediction(cnt, btest, target_bci, c);
1474   float untaken_prob = 1.0 - prob;
1475 
1476   if (prob == PROB_UNKNOWN) {
1477     if (PrintOpto && Verbose) {
1478       tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1479     }
1480     repush_if_args(); // to gather stats on loop
1481     uncommon_trap(Deoptimization::Reason_unreached,
1482                   Deoptimization::Action_reinterpret,
1483                   nullptr, "cold");
1484     if (C->eliminate_boxing()) {
1485       // Mark the successor blocks as parsed
1486       branch_block->next_path_num();

1537   }
1538 
1539   // Generate real control flow
1540   float true_prob = (taken_if_true ? prob : untaken_prob);
1541   IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1542   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1543   Node* taken_branch   = new IfTrueNode(iff);
1544   Node* untaken_branch = new IfFalseNode(iff);
1545   if (!taken_if_true) {  // Finish conversion to canonical form
1546     Node* tmp      = taken_branch;
1547     taken_branch   = untaken_branch;
1548     untaken_branch = tmp;
1549   }
1550 
1551   // Branch is taken:
1552   { PreserveJVMState pjvms(this);
1553     taken_branch = _gvn.transform(taken_branch);
1554     set_control(taken_branch);
1555 
1556     if (stopped()) {
1557       if (C->eliminate_boxing()) {
1558         // Mark the successor block as parsed
1559         branch_block->next_path_num();
1560       }
1561     } else {
1562       adjust_map_after_if(taken_btest, c, prob, branch_block);
1563       if (!stopped()) {
1564         merge(target_bci);








1565       }
1566     }
1567   }
1568 
1569   untaken_branch = _gvn.transform(untaken_branch);
1570   set_control(untaken_branch);
1571 
1572   // Branch not taken.
1573   if (stopped()) {
1574     if (C->eliminate_boxing()) {
1575       // Mark the successor block as parsed
1576       next_block->next_path_num();
1577     }
1578   } else {
1579     adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1580   }
1581 
1582   if (do_stress_trap) {
1583     stress_trap(iff, counter, incr_store);
1584   }
1585 }
1586 




















































































































































































































































































































































































































1587 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
1588 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
1589 // then either takes the trap or executes the original, unstable if.
1590 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
1591   // Search for an unstable if trap
1592   CallStaticJavaNode* trap = nullptr;
1593   assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
1594   ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
1595   if (trap == nullptr || !trap->jvms()->should_reexecute()) {
1596     // No suitable trap found. Remove unused counter load and increment.
1597     C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
1598     return;
1599   }
1600 
1601   // Remove trap from optimization list since we add another path to the trap.
1602   bool success = C->remove_unstable_if_trap(trap, true);
1603   assert(success, "Trap already modified");
1604 
1605   // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
1606   int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]

1639 }
1640 
1641 void Parse::maybe_add_predicate_after_if(Block* path) {
1642   if (path->is_SEL_head() && path->preds_parsed() == 0) {
1643     // Add predicates at bci of if dominating the loop so traps can be
1644     // recorded on the if's profile data
1645     int bc_depth = repush_if_args();
1646     add_parse_predicates();
1647     dec_sp(bc_depth);
1648     path->set_has_predicates();
1649   }
1650 }
1651 
1652 
1653 //----------------------------adjust_map_after_if------------------------------
1654 // Adjust the JVM state to reflect the result of taking this path.
1655 // Basically, it means inspecting the CmpNode controlling this
1656 // branch, seeing how it constrains a tested value, and then
1657 // deciding if it's worth our while to encode this constraint
1658 // as graph nodes in the current abstract interpretation map.
1659 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) {
1660   if (!c->is_Cmp()) {
1661     maybe_add_predicate_after_if(path);
1662     return;
1663   }
1664 
1665   if (stopped() || btest == BoolTest::illegal) {
1666     return;                             // nothing to do
1667   }
1668 
1669   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1670 
1671   if (path_is_suitable_for_uncommon_trap(prob)) {
1672     repush_if_args();
1673     Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
1674                   Deoptimization::Action_reinterpret,
1675                   nullptr,
1676                   (is_fallthrough ? "taken always" : "taken never"));
1677 
1678     if (call != nullptr) {
1679       C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
1680     }
1681     return;
1682   }
1683 
1684   Node* val = c->in(1);
1685   Node* con = c->in(2);
1686   const Type* tcon = _gvn.type(con);
1687   const Type* tval = _gvn.type(val);
1688   bool have_con = tcon->singleton();
1689   if (tval->singleton()) {
1690     if (!have_con) {
1691       // Swap, so constant is in con.

1748     if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
1749        // Found:
1750        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1751        // or the narrowOop equivalent.
1752        const Type* obj_type = _gvn.type(obj);
1753        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
1754        if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
1755            tboth->higher_equal(obj_type)) {
1756           // obj has to be of the exact type Foo if the CmpP succeeds.
1757           int obj_in_map = map()->find_edge(obj);
1758           JVMState* jvms = this->jvms();
1759           if (obj_in_map >= 0 &&
1760               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1761             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1762             const Type* tcc = ccast->as_Type()->type();
1763             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1764             // Delay transform() call to allow recovery of pre-cast value
1765             // at the control merge.
1766             _gvn.set_type_bottom(ccast);
1767             record_for_igvn(ccast);



1768             // Here's the payoff.
1769             replace_in_map(obj, ccast);
1770           }
1771        }
1772     }
1773   }
1774 
1775   int val_in_map = map()->find_edge(val);
1776   if (val_in_map < 0)  return;          // replace_in_map would be useless
1777   {
1778     JVMState* jvms = this->jvms();
1779     if (!(jvms->is_loc(val_in_map) ||
1780           jvms->is_stk(val_in_map)))
1781       return;                           // again, it would be useless
1782   }
1783 
1784   // Check for a comparison to a constant, and "know" that the compared
1785   // value is constrained on this path.
1786   assert(tcon->singleton(), "");
1787   ConstraintCastNode* ccast = nullptr;

1852   if (c->Opcode() == Op_CmpP &&
1853       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1854       c->in(2)->is_Con()) {
1855     Node* load_klass = nullptr;
1856     Node* decode = nullptr;
1857     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1858       decode = c->in(1);
1859       load_klass = c->in(1)->in(1);
1860     } else {
1861       load_klass = c->in(1);
1862     }
1863     if (load_klass->in(2)->is_AddP()) {
1864       Node* addp = load_klass->in(2);
1865       Node* obj = addp->in(AddPNode::Address);
1866       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1867       if (obj_type->speculative_type_not_null() != nullptr) {
1868         ciKlass* k = obj_type->speculative_type();
1869         inc_sp(2);
1870         obj = maybe_cast_profiled_obj(obj, k);
1871         dec_sp(2);




1872         // Make the CmpP use the casted obj
1873         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1874         load_klass = load_klass->clone();
1875         load_klass->set_req(2, addp);
1876         load_klass = _gvn.transform(load_klass);
1877         if (decode != nullptr) {
1878           decode = decode->clone();
1879           decode->set_req(1, load_klass);
1880           load_klass = _gvn.transform(decode);
1881         }
1882         c = c->clone();
1883         c->set_req(1, load_klass);
1884         c = _gvn.transform(c);
1885       }
1886     }
1887   }
1888   return c;
1889 }
1890 
1891 //------------------------------do_one_bytecode--------------------------------

2685     // See if we can get some profile data and hand it off to the next block
2686     Block *target_block = block()->successor_for_bci(target_bci);
2687     if (target_block->pred_count() != 1)  break;
2688     ciMethodData* methodData = method()->method_data();
2689     if (!methodData->is_mature())  break;
2690     ciProfileData* data = methodData->bci_to_data(bci());
2691     assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2692     int taken = ((ciJumpData*)data)->taken();
2693     taken = method()->scale_count(taken);
2694     target_block->set_count(taken);
2695     break;
2696   }
2697 
2698   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
2699   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2700   handle_if_null:
2701     // If this is a backwards branch in the bytecodes, add Safepoint
2702     maybe_add_safepoint(iter().get_dest());
2703     a = null();
2704     b = pop();
2705     if (!_gvn.type(b)->speculative_maybe_null() &&
2706         !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2707       inc_sp(1);
2708       Node* null_ctl = top();
2709       b = null_check_oop(b, &null_ctl, true, true, true);
2710       assert(null_ctl->is_top(), "no null control here");
2711       dec_sp(1);
2712     } else if (_gvn.type(b)->speculative_always_null() &&
2713                !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2714       inc_sp(1);
2715       b = null_assert(b);
2716       dec_sp(1);
2717     }
2718     c = _gvn.transform( new CmpPNode(b, a) );






2719     do_ifnull(btest, c);
2720     break;
2721 
2722   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2723   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2724   handle_if_acmp:
2725     // If this is a backwards branch in the bytecodes, add Safepoint
2726     maybe_add_safepoint(iter().get_dest());
2727     a = pop();
2728     b = pop();
2729     c = _gvn.transform( new CmpPNode(b, a) );
2730     c = optimize_cmp_with_klass(c);
2731     do_if(btest, c);
2732     break;
2733 
2734   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2735   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2736   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2737   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2738   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2739   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2740   handle_ifxx:
2741     // If this is a backwards branch in the bytecodes, add Safepoint
2742     maybe_add_safepoint(iter().get_dest());
2743     a = _gvn.intcon(0);
2744     b = pop();
2745     c = _gvn.transform( new CmpINode(b, a) );
2746     do_if(btest, c);
2747     break;
2748 
2749   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2750   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2751   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;

2766     break;
2767 
2768   case Bytecodes::_lookupswitch:
2769     do_lookupswitch();
2770     break;
2771 
2772   case Bytecodes::_invokestatic:
2773   case Bytecodes::_invokedynamic:
2774   case Bytecodes::_invokespecial:
2775   case Bytecodes::_invokevirtual:
2776   case Bytecodes::_invokeinterface:
2777     do_call();
2778     break;
2779   case Bytecodes::_checkcast:
2780     do_checkcast();
2781     break;
2782   case Bytecodes::_instanceof:
2783     do_instanceof();
2784     break;
2785   case Bytecodes::_anewarray:
2786     do_anewarray();
2787     break;
2788   case Bytecodes::_newarray:
2789     do_newarray((BasicType)iter().get_index());
2790     break;
2791   case Bytecodes::_multianewarray:
2792     do_multianewarray();
2793     break;
2794   case Bytecodes::_new:
2795     do_new();
2796     break;
2797 
2798   case Bytecodes::_jsr:
2799   case Bytecodes::_jsr_w:
2800     do_jsr();
2801     break;
2802 
2803   case Bytecodes::_ret:
2804     do_ret();
2805     break;
2806 

   1 /*
   2  * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciMethodData.hpp"
  27 #include "ci/ciSymbols.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compileLog.hpp"
  30 #include "interpreter/linkResolver.hpp"
  31 #include "jvm_io.h"
  32 #include "memory/resourceArea.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "opto/addnode.hpp"
  36 #include "opto/castnode.hpp"
  37 #include "opto/convertnode.hpp"
  38 #include "opto/divnode.hpp"
  39 #include "opto/idealGraphPrinter.hpp"
  40 #include "opto/idealKit.hpp"
  41 #include "opto/inlinetypenode.hpp"
  42 #include "opto/matcher.hpp"
  43 #include "opto/memnode.hpp"
  44 #include "opto/mulnode.hpp"
  45 #include "opto/opaquenode.hpp"
  46 #include "opto/parse.hpp"
  47 #include "opto/runtime.hpp"
  48 #include "runtime/deoptimization.hpp"
  49 #include "runtime/sharedRuntime.hpp"
  50 
  51 #ifndef PRODUCT
  52 extern uint explicit_null_checks_inserted,
  53             explicit_null_checks_elided;
  54 #endif
  55 
  56 Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) {
  57   // Feed unused profile data to type speculation
  58   if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
  59     ciKlass* array_type = nullptr;
  60     ciKlass* element_type = nullptr;
  61     ProfilePtrKind element_ptr = ProfileMaybeNull;
  62     bool flat_array = true;
  63     bool null_free_array = true;
  64     method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
  65     if (element_type != nullptr || element_ptr != ProfileMaybeNull) {
  66       ld = record_profile_for_speculation(ld, element_type, element_ptr);
  67     }
  68   }
  69   return ld;
  70 }
  71 
  72 
  73 //---------------------------------array_load----------------------------------
  74 void Parse::array_load(BasicType bt) {
  75   const Type* elemtype = Type::TOP;

  76   Node* adr = array_addressing(bt, 0, elemtype);
  77   if (stopped())  return;     // guaranteed null or range check
  78 
  79   Node* array_index = pop();
  80   Node* array = pop();
  81 
  82   // Handle inline type arrays
  83   const TypeOopPtr* element_ptr = elemtype->make_oopptr();
  84   const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
  85   if (array_type->is_flat()) {
  86     // Load from flat inline type array
  87     Node* inline_type;
  88     if (element_ptr->klass_is_exact()) {
  89       inline_type = InlineTypeNode::make_from_flat(this, elemtype->inline_klass(), array, adr);
  90     } else {
  91       // Element type of flat array is not exact. Therefore, we cannot determine the flat array layout statically.
  92       // Emit a runtime call to load the element from the flat array.
  93       inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
  94       inline_type = record_profile_for_speculation_at_array_load(inline_type);
  95     }
  96     push(inline_type);
  97     return;
  98   }
  99 
 100   if (!array_type->is_not_flat()) {
 101     // Cannot statically determine if array is a flat array, emit runtime check
 102     assert(UseFlatArray && is_reference_type(bt) && element_ptr->can_be_inline_type() && !array_type->is_not_null_free() &&
 103            (!element_ptr->is_inlinetypeptr() || element_ptr->inline_klass()->flat_in_array()), "array can't be flat");
 104     IdealKit ideal(this);
 105     IdealVariable res(ideal);
 106     ideal.declarations_done();
 107     ideal.if_then(flat_array_test(array, /* flat = */ false)); {
 108       // Non-flat array
 109       assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
 110       sync_kit(ideal);
 111       const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
 112       DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
 113       if (needs_range_check(array_type->size(), array_index)) {
 114         // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
 115         // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
 116         // possibly float above the range check at any point.
 117         decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
 118       }
 119       Node* ld = access_load_at(array, adr, adr_type, element_ptr, bt, decorator_set);
 120       if (element_ptr->is_inlinetypeptr()) {
 121         assert(element_ptr->maybe_null(), "null free array should be handled above");
 122         ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass(), false);
 123       }
 124       ideal.sync_kit(this);
 125       ideal.set(res, ld);
 126     } ideal.else_(); {
 127       // Flat array
 128       sync_kit(ideal);
 129       if (element_ptr->is_inlinetypeptr()) {
 130         // Element type is known, cast and load from flat array layout.
 131         ciInlineKlass* vk = element_ptr->inline_klass();
 132         assert(vk->flat_in_array() && element_ptr->maybe_null(), "never/always flat - should be optimized");
 133         ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true);
 134         const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
 135         Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, arytype));
 136         Node* casted_adr = array_element_address(cast, array_index, T_OBJECT, array_type->size(), control());
 137         // Re-execute flat array load if buffering triggers deoptimization
 138         PreserveReexecuteState preexecs(this);
 139         jvms()->set_should_reexecute(true);
 140         inc_sp(2);
 141         Node* vt = InlineTypeNode::make_from_flat(this, vk, cast, casted_adr)->buffer(this, false);
 142         ideal.set(res, vt);
 143         ideal.sync_kit(this);
 144       } else {
 145         // Element type is unknown, and thus we cannot statically determine the exact flat array layout. Emit a
 146         // runtime call to correctly load the inline type element from the flat array.
 147         Node* inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
 148         ideal.sync_kit(this);
 149         ideal.set(res, inline_type);
 150       }
 151     } ideal.end_if();
 152     sync_kit(ideal);
 153     Node* ld = _gvn.transform(ideal.value(res));
 154     ld = record_profile_for_speculation_at_array_load(ld);
 155     push_node(bt, ld);
 156     return;
 157   }
 158 
 159   if (array_type->is_null_free()) {
 160     // Load from non-flat inline type array (elements can never be null)
 161     bt = T_OBJECT;
 162   }
 163 
 164   if (elemtype == TypeInt::BOOL) {
 165     bt = T_BOOLEAN;
 166   }
 167   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);

 168   Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
 169                             IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
 170   ld = record_profile_for_speculation_at_array_load(ld);
 171   // Loading an inline type from a non-flat array
 172   if (element_ptr != nullptr && element_ptr->is_inlinetypeptr()) {
 173     assert(!array_type->is_null_free() || !element_ptr->maybe_null(), "inline type array elements should never be null");
 174     ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass(), !element_ptr->maybe_null());
 175   }
 176   push_node(bt, ld);
 177 }
 178 
 179 Node* Parse::load_from_unknown_flat_array(Node* array, Node* array_index, const TypeOopPtr* element_ptr) {
 180   // Below membars keep this access to an unknown flat array correctly
 181   // ordered with other unknown and known flat array accesses.
 182   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 183 
 184   Node* call = nullptr;
 185   {
 186     // Re-execute flat array load if runtime call triggers deoptimization
 187     PreserveReexecuteState preexecs(this);
 188     jvms()->set_bci(_bci);
 189     jvms()->set_should_reexecute(true);
 190     inc_sp(2);
 191     kill_dead_locals();
 192     call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 193                              OptoRuntime::load_unknown_inline_Type(),
 194                              OptoRuntime::load_unknown_inline_Java(),
 195                              nullptr, TypeRawPtr::BOTTOM,
 196                              array, array_index);
 197   }
 198   make_slow_call_ex(call, env()->Throwable_klass(), false);
 199   Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
 200 
 201   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 202 
 203   // Keep track of the information that the inline type is in flat arrays
 204   const Type* unknown_value = element_ptr->is_instptr()->cast_to_flat_in_array();
 205   return _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
 206 }
 207 
 208 //--------------------------------array_store----------------------------------
 209 void Parse::array_store(BasicType bt) {
 210   const Type* elemtype = Type::TOP;
 211   Node* adr = array_addressing(bt, type2size[bt], elemtype);

 212   if (stopped())  return;     // guaranteed null or range check
 213   Node* stored_value_casted = nullptr;
 214   if (bt == T_OBJECT) {
 215     stored_value_casted = array_store_check(adr, elemtype);
 216     if (stopped()) {
 217       return;
 218     }
 219   }
 220   Node* const stored_value = pop_node(bt); // Value to store
 221   Node* const array_index = pop();         // Index in the array
 222   Node* array = pop();                     // The array itself
 223 
 224   const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
 225   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);


 226 
 227   if (elemtype == TypeInt::BOOL) {
 228     bt = T_BOOLEAN;
 229   } else if (bt == T_OBJECT) {
 230     elemtype = elemtype->make_oopptr();
 231     const Type* stored_value_casted_type = _gvn.type(stored_value_casted);
 232     // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
 233     // This is only legal for non-null stores because the array_store_check always passes for null, even
 234     // if the array is null-free. Null stores are handled in GraphKit::inline_array_null_guard().
 235     bool not_null_free = !stored_value_casted_type->maybe_null() &&
 236                          !stored_value_casted_type->is_oopptr()->can_be_inline_type();
 237     bool not_flat = not_null_free || (stored_value_casted_type->is_inlinetypeptr() &&
 238                                       !stored_value_casted_type->inline_klass()->flat_in_array());
 239     if (!array_type->is_not_null_free() && not_null_free) {
 240       // Storing a non-inline type, mark array as not null-free (-> not flat).
 241       array_type = array_type->cast_to_not_null_free();
 242       Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
 243       replace_in_map(array, cast);
 244       array = cast;
 245     } else if (!array_type->is_not_flat() && not_flat) {
 246       // Storing to a non-flat array, mark array as not flat.
 247       array_type = array_type->cast_to_not_flat();
 248       Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
 249       replace_in_map(array, cast);
 250       array = cast;
 251     }
 252 
 253     if (array_type->is_flat()) {
 254       // Store to flat inline type array
 255       assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
 256       if (array_type->klass_is_exact()) {
 257         // Store to exact flat inline type array where we know the flat array layout statically.
 258         // Re-execute flat array store if buffering triggers deoptimization
 259         PreserveReexecuteState preexecs(this);
 260         inc_sp(3);
 261         jvms()->set_should_reexecute(true);
 262         stored_value_casted->as_InlineType()->store_flat(this, array, adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 263       } else {
 264         // Element type of flat array is not exact. Therefore, we cannot determine the flat array layout statically.
 265         // Emit a runtime call to store the element to the flat array.
 266         store_to_unknown_flat_array(array, array_index, stored_value_casted);
 267       }
 268       return;
 269     }
 270     if (array_type->is_null_free()) {
 271       // Store to non-flat null-free inline type array (elements can never be null)
 272       assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
 273       if (elemtype->inline_klass()->is_empty()) {
 274         // Ignore empty inline stores, array is already initialized.
 275         return;
 276       }
 277     } else if (!array_type->is_not_flat() && (stored_value_casted_type != TypePtr::NULL_PTR || StressReflectiveCode)) {
 278       // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient).
 279       assert(UseFlatArray && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
 280              !array_type->klass_is_exact() && !array_type->is_not_null_free(), "array can't be a flat array");
 281       IdealKit ideal(this);
 282       ideal.if_then(flat_array_test(array, /* flat = */ false)); {
 283         // Non-flat array
 284         assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
 285         sync_kit(ideal);
 286         Node* cast_array = inline_array_null_guard(array, stored_value_casted, 3);
 287         inc_sp(3);
 288         access_store_at(cast_array, adr, adr_type, stored_value_casted, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
 289         dec_sp(3);
 290         ideal.sync_kit(this);
 291       } ideal.else_(); {
 292         sync_kit(ideal);
 293         // flat array
 294         Node* null_ctl = top();
 295         Node* null_checked_stored_value_casted = null_check_oop(stored_value_casted, &null_ctl);
 296         if (null_ctl != top()) {
 297           PreserveJVMState pjvms(this);
 298           inc_sp(3);
 299           set_control(null_ctl);
 300           uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
 301           dec_sp(3);
 302         }
 303         // Try to determine the inline klass
 304         ciInlineKlass* inline_Klass = nullptr;
 305         if (stored_value_casted_type->is_inlinetypeptr()) {
 306           inline_Klass = stored_value_casted_type->inline_klass();
 307         } else if (elemtype->is_inlinetypeptr()) {
 308           inline_Klass = elemtype->inline_klass();
 309         }
 310         if (!stopped()) {
 311           if (inline_Klass != nullptr) {
 312             // Element type is known, cast and store to flat array layout.
 313             assert(inline_Klass->flat_in_array() && elemtype->maybe_null(), "never/always flat - should be optimized");
 314             ciArrayKlass* array_klass = ciArrayKlass::make(inline_Klass, /* null_free */ true);
 315             const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
 316             Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, arytype));
 317             Node* casted_adr = array_element_address(casted_array, array_index, T_OBJECT, arytype->size(), control());
 318             if (!null_checked_stored_value_casted->is_InlineType()) {
 319               assert(!gvn().type(null_checked_stored_value_casted)->maybe_null(),
 320                      "inline type array elements should never be null");
 321               null_checked_stored_value_casted = InlineTypeNode::make_from_oop(this, null_checked_stored_value_casted,
 322                                                                                inline_Klass);
 323             }
 324             // Re-execute flat array store if buffering triggers deoptimization
 325             PreserveReexecuteState preexecs(this);
 326             inc_sp(3);
 327             jvms()->set_should_reexecute(true);
 328             null_checked_stored_value_casted->as_InlineType()->store_flat(this, casted_array, casted_adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 329           } else {
 330             // Element type is unknown, emit a runtime call since the flat array layout is not statically known.
 331             store_to_unknown_flat_array(array, array_index, null_checked_stored_value_casted);
 332           }
 333         }
 334         ideal.sync_kit(this);
 335       }
 336       ideal.end_if();
 337       sync_kit(ideal);
 338       return;
 339     } else if (!array_type->is_not_null_free()) {
 340       // Array is not flat but may be null free
 341       assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
 342       array = inline_array_null_guard(array, stored_value_casted, 3, true);
 343     }
 344   }
 345   inc_sp(3);
 346   access_store_at(array, adr, adr_type, stored_value, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 347   dec_sp(3);
 348 }
 349 
 350 // Emit a runtime call to store to a flat array whose element type is either unknown (i.e. we do not know the flat
 351 // array layout) or not exact (could have different flat array layouts at runtime).
 352 void Parse::store_to_unknown_flat_array(Node* array, Node* const idx, Node* non_null_stored_value) {
 353   // Below membars keep this access to an unknown flat array correctly
 354   // ordered with other unknown and known flat array accesses.
 355   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 356 
 357   Node* call = nullptr;
 358   {
 359     // Re-execute flat array store if runtime call triggers deoptimization
 360     PreserveReexecuteState preexecs(this);
 361     jvms()->set_bci(_bci);
 362     jvms()->set_should_reexecute(true);
 363     inc_sp(3);
 364     kill_dead_locals();
 365     call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 366                       OptoRuntime::store_unknown_inline_Type(),
 367                       OptoRuntime::store_unknown_inline_Java(),
 368                       nullptr, TypeRawPtr::BOTTOM,
 369                       non_null_stored_value, array, idx);
 370   }
 371   make_slow_call_ex(call, env()->Throwable_klass(), false);
 372 
 373   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 374 }
 375 
 376 //------------------------------array_addressing-------------------------------
 377 // Pull array and index from the stack.  Compute pointer-to-element.
 378 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
 379   Node *idx   = peek(0+vals);   // Get from stack without popping
 380   Node *ary   = peek(1+vals);   // in case of exception
 381 
 382   // Null check the array base, with correct stack contents
 383   ary = null_check(ary, T_ARRAY);
 384   // Compile-time detect of null-exception?
 385   if (stopped())  return top();
 386 
 387   const TypeAryPtr* arytype  = _gvn.type(ary)->is_aryptr();
 388   const TypeInt*    sizetype = arytype->size();
 389   elemtype = arytype->elem();
 390 
 391   if (UseUniqueSubclasses) {
 392     const Type* el = elemtype->make_ptr();
 393     if (el && el->isa_instptr()) {
 394       const TypeInstPtr* toop = el->is_instptr();
 395       if (toop->instance_klass()->unique_concrete_subklass()) {
 396         // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
 397         const Type* subklass = Type::get_const_type(toop->instance_klass());
 398         elemtype = subklass->join_speculative(el);
 399       }
 400     }
 401   }
 402 











 403   if (!arytype->is_loaded()) {
 404     // Only fails for some -Xcomp runs
 405     // The class is unloaded.  We have to run this bytecode in the interpreter.
 406     ciKlass* klass = arytype->unloaded_klass();
 407 
 408     uncommon_trap(Deoptimization::Reason_unloaded,
 409                   Deoptimization::Action_reinterpret,
 410                   klass, "!loaded array");
 411     return top();
 412   }
 413 
 414   ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);











 415 
 416   if (needs_range_check(sizetype, idx)) {
 417     create_range_check(idx, ary, sizetype);
 418   } else if (C->log() != nullptr) {
 419     C->log()->elem("observe that='!need_range_check'");



























 420   }
 421 
 422   // Check for always knowing you are throwing a range-check exception
 423   if (stopped())  return top();
 424 
 425   // Make array address computation control dependent to prevent it
 426   // from floating above the range check during loop optimizations.
 427   Node* ptr = array_element_address(ary, idx, type, sizetype, control());
 428   assert(ptr != top(), "top should go hand-in-hand with stopped");
 429 
 430   return ptr;
 431 }
 432 
 433 // 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
 434 // be greater or equal the smallest possible array size (i.e. out-of-bounds).
 435 bool Parse::needs_range_check(const TypeInt* size_type, const Node* index) const {
 436   const TypeInt* index_type = _gvn.type(index)->is_int();
 437   return index_type->_hi >= size_type->_lo || index_type->_lo < 0;
 438 }
 439 
 440 void Parse::create_range_check(Node* idx, Node* ary, const TypeInt* sizetype) {
 441   Node* tst;
 442   if (sizetype->_hi <= 0) {
 443     // The greatest array bound is negative, so we can conclude that we're
 444     // compiling unreachable code, but the unsigned compare trick used below
 445     // only works with non-negative lengths.  Instead, hack "tst" to be zero so
 446     // the uncommon_trap path will always be taken.
 447     tst = _gvn.intcon(0);
 448   } else {
 449     // Range is constant in array-oop, so we can use the original state of mem
 450     Node* len = load_array_length(ary);
 451 
 452     // Test length vs index (standard trick using unsigned compare)
 453     Node* chk = _gvn.transform(new CmpUNode(idx, len) );
 454     BoolTest::mask btest = BoolTest::lt;
 455     tst = _gvn.transform(new BoolNode(chk, btest) );
 456   }
 457   RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
 458   _gvn.set_type(rc, rc->Value(&_gvn));
 459   if (!tst->is_Con()) {
 460     record_for_igvn(rc);
 461   }
 462   set_control(_gvn.transform(new IfTrueNode(rc)));
 463   // Branch to failure if out of bounds
 464   {
 465     PreserveJVMState pjvms(this);
 466     set_control(_gvn.transform(new IfFalseNode(rc)));
 467     if (C->allow_range_check_smearing()) {
 468       // Do not use builtin_throw, since range checks are sometimes
 469       // made more stringent by an optimistic transformation.
 470       // This creates "tentative" range checks at this point,
 471       // which are not guaranteed to throw exceptions.
 472       // See IfNode::Ideal, is_range_check, adjust_check.
 473       uncommon_trap(Deoptimization::Reason_range_check,
 474                     Deoptimization::Action_make_not_entrant,
 475                     nullptr, "range_check");
 476     } else {
 477       // If we have already recompiled with the range-check-widening
 478       // heroic optimization turned off, then we must really be throwing
 479       // range check exceptions.
 480       builtin_throw(Deoptimization::Reason_range_check);
 481     }
 482   }
 483 }
 484 
 485 // For inline type arrays, we can use the profiling information for array accesses to speculate on the type, flatness,
 486 // and null-freeness. We can either prepare the speculative type for later uses or emit explicit speculative checks with
 487 // traps now. In the latter case, the speculative type guarantees can avoid additional runtime checks later (e.g.
 488 // non-null-free implies non-flat which allows us to remove flatness checks). This makes the graph simpler.
 489 Node* Parse::create_speculative_inline_type_array_checks(Node* array, const TypeAryPtr* array_type,
 490                                                          const Type*& element_type) {
 491   if (!array_type->is_flat() && !array_type->is_not_flat()) {
 492     // For arrays that might be flat, speculate that the array has the exact type reported in the profile data such that
 493     // we can rely on a fixed memory layout (i.e. either a flat layout or not).
 494     array = cast_to_speculative_array_type(array, array_type, element_type);
 495   } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) {
 496     // Array is known to be either flat or not flat. If possible, update the speculative type by using the profile data
 497     // at this bci.
 498     array = cast_to_profiled_array_type(array);
 499   }
 500 
 501   // Even though the type does not tell us whether we have an inline type array or not, we can still check the profile data
 502   // whether we have a non-null-free or non-flat array. Since non-null-free implies non-flat, we check this first.
 503   // Speculating on a non-null-free array doesn't help aaload but could be profitable for a subsequent aastore.
 504   if (!array_type->is_null_free() && !array_type->is_not_null_free()) {
 505     array = speculate_non_null_free_array(array, array_type);
 506   }
 507 
 508   if (!array_type->is_flat() && !array_type->is_not_flat()) {
 509     array = speculate_non_flat_array(array, array_type);
 510   }
 511   return array;
 512 }
 513 
 514 // Speculate that the array has the exact type reported in the profile data. We emit a trap when this turns out to be
 515 // wrong. On the fast path, we add a CheckCastPP to use the exact type.
 516 Node* Parse::cast_to_speculative_array_type(Node* const array, const TypeAryPtr*& array_type, const Type*& element_type) {
 517   Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check;
 518   ciKlass* speculative_array_type = array_type->speculative_type();
 519   if (too_many_traps_or_recompiles(reason) || speculative_array_type == nullptr) {
 520     // No speculative type, check profile data at this bci
 521     speculative_array_type = nullptr;
 522     reason = Deoptimization::Reason_class_check;
 523     if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
 524       ciKlass* profiled_element_type = nullptr;
 525       ProfilePtrKind element_ptr = ProfileMaybeNull;
 526       bool flat_array = true;
 527       bool null_free_array = true;
 528       method()->array_access_profiled_type(bci(), speculative_array_type, profiled_element_type, element_ptr, flat_array,
 529                                            null_free_array);
 530     }
 531   }
 532   if (speculative_array_type != nullptr) {
 533     // Speculate that this array has the exact type reported by profile data
 534     Node* casted_array = nullptr;
 535     DEBUG_ONLY(Node* old_control = control();)
 536     Node* slow_ctl = type_check_receiver(array, speculative_array_type, 1.0, &casted_array);
 537     if (stopped()) {
 538       // The check always fails and therefore profile information is incorrect. Don't use it.
 539       assert(old_control == slow_ctl, "type check should have been removed");
 540       set_control(slow_ctl);
 541     } else if (!slow_ctl->is_top()) {
 542       { PreserveJVMState pjvms(this);
 543         set_control(slow_ctl);
 544         uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
 545       }
 546       replace_in_map(array, casted_array);
 547       array_type = _gvn.type(casted_array)->is_aryptr();
 548       element_type = array_type->elem();
 549       return casted_array;
 550     }
 551   }
 552   return array;
 553 }
 554 
 555 // Create a CheckCastPP when the speculative type can improve the current type.
 556 Node* Parse::cast_to_profiled_array_type(Node* const array) {
 557   ciKlass* array_type = nullptr;
 558   ciKlass* element_type = nullptr;
 559   ProfilePtrKind element_ptr = ProfileMaybeNull;
 560   bool flat_array = true;
 561   bool null_free_array = true;
 562   method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array);
 563   if (array_type != nullptr) {
 564     return record_profile_for_speculation(array, array_type, ProfileMaybeNull);
 565   }
 566   return array;
 567 }
 568 
 569 // Speculate that the array is non-null-free. This will imply non-flatness. We emit a trap when this turns out to be
 570 // wrong. On the fast path, we add a CheckCastPP to use the non-null-free type.
 571 Node* Parse::speculate_non_null_free_array(Node* const array, const TypeAryPtr*& array_type) {
 572   bool null_free_array = true;
 573   Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
 574   if (array_type->speculative() != nullptr &&
 575       array_type->speculative()->is_aryptr()->is_not_null_free() &&
 576       !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
 577     null_free_array = false;
 578     reason = Deoptimization::Reason_speculate_class_check;
 579   } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
 580     ciKlass* profiled_array_type = nullptr;
 581     ciKlass* profiled_element_type = nullptr;
 582     ProfilePtrKind element_ptr = ProfileMaybeNull;
 583     bool flat_array = true;
 584     method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
 585                                          null_free_array);
 586     reason = Deoptimization::Reason_class_check;
 587   }
 588   if (!null_free_array) {
 589     { // Deoptimize if null-free array
 590       BuildCutout unless(this, null_free_array_test(array, /* null_free = */ false), PROB_MAX);
 591       uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
 592     }
 593     assert(!stopped(), "null-free array should have been caught earlier");
 594     Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_null_free()));
 595     replace_in_map(array, casted_array);
 596     array_type = _gvn.type(casted_array)->is_aryptr();
 597     return casted_array;
 598   }
 599   return array;
 600 }
 601 
 602 // Speculate that the array is non-flat. We emit a trap when this turns out to be wrong. On the fast path, we add a
 603 // CheckCastPP to use the non-flat type.
 604 Node* Parse::speculate_non_flat_array(Node* const array, const TypeAryPtr* const array_type) {
 605   bool flat_array = true;
 606   Deoptimization::DeoptReason reason = Deoptimization::Reason_none;
 607   if (array_type->speculative() != nullptr &&
 608       array_type->speculative()->is_aryptr()->is_not_flat() &&
 609       !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
 610     flat_array = false;
 611     reason = Deoptimization::Reason_speculate_class_check;
 612   } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) {
 613     ciKlass* profiled_array_type = nullptr;
 614     ciKlass* profiled_element_type = nullptr;
 615     ProfilePtrKind element_ptr = ProfileMaybeNull;
 616     bool null_free_array = true;
 617     method()->array_access_profiled_type(bci(), profiled_array_type, profiled_element_type, element_ptr, flat_array,
 618                                          null_free_array);
 619     reason = Deoptimization::Reason_class_check;
 620   }
 621   if (!flat_array) {
 622     { // Deoptimize if flat array
 623       BuildCutout unless(this, flat_array_test(array, /* flat = */ false), PROB_MAX);
 624       uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
 625     }
 626     assert(!stopped(), "flat array should have been caught earlier");
 627     Node* casted_array = _gvn.transform(new CheckCastPPNode(control(), array, array_type->cast_to_not_flat()));
 628     replace_in_map(array, casted_array);
 629     return casted_array;
 630   }
 631   return array;
 632 }
 633 
 634 // returns IfNode
 635 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
 636   Node   *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
 637   Node   *tst = _gvn.transform(new BoolNode(cmp, mask));
 638   IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
 639   return iff;
 640 }
 641 
 642 
 643 // sentinel value for the target bci to mark never taken branches
 644 // (according to profiling)
 645 static const int never_reached = INT_MAX;
 646 
 647 //------------------------------helper for tableswitch-------------------------
 648 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) {
 649   // True branch, use existing map info
 650   { PreserveJVMState pjvms(this);
 651     Node *iftrue  = _gvn.transform( new IfTrueNode (iff) );
 652     set_control( iftrue );

1866   // False branch
1867   Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1868   set_control(iffalse);
1869 
1870   if (stopped()) {              // Path is dead?
1871     NOT_PRODUCT(explicit_null_checks_elided++);
1872     if (C->eliminate_boxing()) {
1873       // Mark the successor block as parsed
1874       next_block->next_path_num();
1875     }
1876   } else  {                     // Path is live.
1877     adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1878   }
1879 
1880   if (do_stress_trap) {
1881     stress_trap(iff, counter, incr_store);
1882   }
1883 }
1884 
1885 //------------------------------------do_if------------------------------------
1886 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken) {
1887   int target_bci = iter().get_dest();
1888 
1889   Block* branch_block = successor_for_bci(target_bci);
1890   Block* next_block   = successor_for_bci(iter().next_bci());
1891 
1892   float cnt;
1893   float prob = branch_prediction(cnt, btest, target_bci, c);
1894   float untaken_prob = 1.0 - prob;
1895 
1896   if (prob == PROB_UNKNOWN) {
1897     if (PrintOpto && Verbose) {
1898       tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1899     }
1900     repush_if_args(); // to gather stats on loop
1901     uncommon_trap(Deoptimization::Reason_unreached,
1902                   Deoptimization::Action_reinterpret,
1903                   nullptr, "cold");
1904     if (C->eliminate_boxing()) {
1905       // Mark the successor blocks as parsed
1906       branch_block->next_path_num();

1957   }
1958 
1959   // Generate real control flow
1960   float true_prob = (taken_if_true ? prob : untaken_prob);
1961   IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1962   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1963   Node* taken_branch   = new IfTrueNode(iff);
1964   Node* untaken_branch = new IfFalseNode(iff);
1965   if (!taken_if_true) {  // Finish conversion to canonical form
1966     Node* tmp      = taken_branch;
1967     taken_branch   = untaken_branch;
1968     untaken_branch = tmp;
1969   }
1970 
1971   // Branch is taken:
1972   { PreserveJVMState pjvms(this);
1973     taken_branch = _gvn.transform(taken_branch);
1974     set_control(taken_branch);
1975 
1976     if (stopped()) {
1977       if (C->eliminate_boxing() && !new_path) {
1978         // Mark the successor block as parsed (if we haven't created a new path)
1979         branch_block->next_path_num();
1980       }
1981     } else {
1982       adjust_map_after_if(taken_btest, c, prob, branch_block, can_trap);
1983       if (!stopped()) {
1984         if (new_path) {
1985           // Merge by using a new path
1986           merge_new_path(target_bci);
1987         } else if (ctrl_taken != nullptr) {
1988           // Don't merge but save taken branch to be wired by caller
1989           *ctrl_taken = control();
1990         } else {
1991           merge(target_bci);
1992         }
1993       }
1994     }
1995   }
1996 
1997   untaken_branch = _gvn.transform(untaken_branch);
1998   set_control(untaken_branch);
1999 
2000   // Branch not taken.
2001   if (stopped() && ctrl_taken == nullptr) {
2002     if (C->eliminate_boxing()) {
2003       // Mark the successor block as parsed (if caller does not re-wire control flow)
2004       next_block->next_path_num();
2005     }
2006   } else {
2007     adjust_map_after_if(untaken_btest, c, untaken_prob, next_block, can_trap);
2008   }
2009 
2010   if (do_stress_trap) {
2011     stress_trap(iff, counter, incr_store);
2012   }
2013 }
2014 
2015 
2016 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) {
2017   if (t->speculative() == nullptr) {
2018     return ProfileUnknownNull;
2019   }
2020   if (t->speculative_always_null()) {
2021     return ProfileAlwaysNull;
2022   }
2023   if (t->speculative_maybe_null()) {
2024     return ProfileMaybeNull;
2025   }
2026   return ProfileNeverNull;
2027 }
2028 
2029 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) {
2030   inc_sp(2);
2031   Node* cast = null_check_common(input, T_OBJECT, true, nullptr,
2032                                  !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) &&
2033                                  speculative_ptr_kind(tinput) == ProfileAlwaysNull);
2034   dec_sp(2);
2035   if (btest == BoolTest::ne) {
2036     {
2037       PreserveJVMState pjvms(this);
2038       replace_in_map(input, cast);
2039       int target_bci = iter().get_dest();
2040       merge(target_bci);
2041     }
2042     record_for_igvn(eq_region);
2043     set_control(_gvn.transform(eq_region));
2044   } else {
2045     replace_in_map(input, cast);
2046   }
2047 }
2048 
2049 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) {
2050   inc_sp(2);
2051   null_ctl = top();
2052   Node* cast = null_check_oop(input, &null_ctl,
2053                               input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)),
2054                               false,
2055                               speculative_ptr_kind(tinput) == ProfileNeverNull &&
2056                               !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check));
2057   dec_sp(2);
2058   assert(!stopped(), "null input should have been caught earlier");
2059   return cast;
2060 }
2061 
2062 void Parse::acmp_known_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, ciKlass* input_type, BoolTest::mask btest, Node* eq_region) {
2063   Node* ne_region = new RegionNode(1);
2064   Node* null_ctl;
2065   Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2066   ne_region->add_req(null_ctl);
2067 
2068   Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast);
2069   {
2070     PreserveJVMState pjvms(this);
2071     inc_sp(2);
2072     set_control(slow_ctl);
2073     Deoptimization::DeoptReason reason;
2074     if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2075       reason = Deoptimization::Reason_speculate_class_check;
2076     } else {
2077       reason = Deoptimization::Reason_class_check;
2078     }
2079     uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile);
2080   }
2081   ne_region->add_req(control());
2082 
2083   record_for_igvn(ne_region);
2084   set_control(_gvn.transform(ne_region));
2085   if (btest == BoolTest::ne) {
2086     {
2087       PreserveJVMState pjvms(this);
2088       if (null_ctl == top()) {
2089         replace_in_map(input, cast);
2090       }
2091       int target_bci = iter().get_dest();
2092       merge(target_bci);
2093     }
2094     record_for_igvn(eq_region);
2095     set_control(_gvn.transform(eq_region));
2096   } else {
2097     if (null_ctl == top()) {
2098       replace_in_map(input, cast);
2099     }
2100     set_control(_gvn.transform(ne_region));
2101   }
2102 }
2103 
2104 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) {
2105   Node* ne_region = new RegionNode(1);
2106   Node* null_ctl;
2107   Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl);
2108   ne_region->add_req(null_ctl);
2109 
2110   {
2111     BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX);
2112     inc_sp(2);
2113     uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile);
2114   }
2115 
2116   ne_region->add_req(control());
2117 
2118   record_for_igvn(ne_region);
2119   set_control(_gvn.transform(ne_region));
2120   if (btest == BoolTest::ne) {
2121     {
2122       PreserveJVMState pjvms(this);
2123       if (null_ctl == top()) {
2124         replace_in_map(input, cast);
2125       }
2126       int target_bci = iter().get_dest();
2127       merge(target_bci);
2128     }
2129     record_for_igvn(eq_region);
2130     set_control(_gvn.transform(eq_region));
2131   } else {
2132     if (null_ctl == top()) {
2133       replace_in_map(input, cast);
2134     }
2135     set_control(_gvn.transform(ne_region));
2136   }
2137 }
2138 
2139 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) {
2140   ciKlass* left_type = nullptr;
2141   ciKlass* right_type = nullptr;
2142   ProfilePtrKind left_ptr = ProfileUnknownNull;
2143   ProfilePtrKind right_ptr = ProfileUnknownNull;
2144   bool left_inline_type = true;
2145   bool right_inline_type = true;
2146 
2147   // Leverage profiling at acmp
2148   if (UseACmpProfile) {
2149     method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type);
2150     if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) {
2151       left_type = nullptr;
2152       right_type = nullptr;
2153       left_inline_type = true;
2154       right_inline_type = true;
2155     }
2156     if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) {
2157       left_ptr = ProfileUnknownNull;
2158       right_ptr = ProfileUnknownNull;
2159     }
2160   }
2161 
2162   if (UseTypeSpeculation) {
2163     record_profile_for_speculation(left, left_type, left_ptr);
2164     record_profile_for_speculation(right, right_type, right_ptr);
2165   }
2166 
2167   if (!EnableValhalla) {
2168     Node* cmp = CmpP(left, right);
2169     cmp = optimize_cmp_with_klass(cmp);
2170     do_if(btest, cmp);
2171     return;
2172   }
2173 
2174   // Check for equality before potentially allocating
2175   if (left == right) {
2176     do_if(btest, makecon(TypeInt::CC_EQ));
2177     return;
2178   }
2179 
2180   // Allocate inline type operands and re-execute on deoptimization
2181   if (left->is_InlineType()) {
2182     if (_gvn.type(right)->is_zero_type() ||
2183         (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_is_init())->is_zero_type())) {
2184       // Null checking a scalarized but nullable inline type. Check the IsInit
2185       // input instead of the oop input to avoid keeping buffer allocations alive.
2186       Node* cmp = CmpI(left->as_InlineType()->get_is_init(), intcon(0));
2187       do_if(btest, cmp);
2188       return;
2189     } else {
2190       PreserveReexecuteState preexecs(this);
2191       inc_sp(2);
2192       jvms()->set_should_reexecute(true);
2193       left = left->as_InlineType()->buffer(this)->get_oop();
2194     }
2195   }
2196   if (right->is_InlineType()) {
2197     PreserveReexecuteState preexecs(this);
2198     inc_sp(2);
2199     jvms()->set_should_reexecute(true);
2200     right = right->as_InlineType()->buffer(this)->get_oop();
2201   }
2202 
2203   // First, do a normal pointer comparison
2204   const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr();
2205   const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr();
2206   Node* cmp = CmpP(left, right);
2207   cmp = optimize_cmp_with_klass(cmp);
2208   if (tleft == nullptr || !tleft->can_be_inline_type() ||
2209       tright == nullptr || !tright->can_be_inline_type()) {
2210     // This is sufficient, if one of the operands can't be an inline type
2211     do_if(btest, cmp);
2212     return;
2213   }
2214 
2215   // Don't add traps to unstable if branches because additional checks are required to
2216   // decide if the operands are equal/substitutable and we therefore shouldn't prune
2217   // branches for one if based on the profiling of the acmp branches.
2218   // Also, OptimizeUnstableIf would set an incorrect re-rexecution state because it
2219   // assumes that there is a 1-1 mapping between the if and the acmp branches and that
2220   // hitting a trap means that we will take the corresponding acmp branch on re-execution.
2221   const bool can_trap = true;
2222 
2223   Node* eq_region = nullptr;
2224   if (btest == BoolTest::eq) {
2225     do_if(btest, cmp, !can_trap, true);
2226     if (stopped()) {
2227       // Pointers are equal, operands must be equal
2228       return;
2229     }
2230   } else {
2231     assert(btest == BoolTest::ne, "only eq or ne");
2232     Node* is_not_equal = nullptr;
2233     eq_region = new RegionNode(3);
2234     {
2235       PreserveJVMState pjvms(this);
2236       // Pointers are not equal, but more checks are needed to determine if the operands are (not) substitutable
2237       do_if(btest, cmp, !can_trap, false, &is_not_equal);
2238       if (!stopped()) {
2239         eq_region->init_req(1, control());
2240       }
2241     }
2242     if (is_not_equal == nullptr || is_not_equal->is_top()) {
2243       record_for_igvn(eq_region);
2244       set_control(_gvn.transform(eq_region));
2245       return;
2246     }
2247     set_control(is_not_equal);
2248   }
2249 
2250   // Prefer speculative types if available
2251   if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
2252     if (tleft->speculative_type() != nullptr) {
2253       left_type = tleft->speculative_type();
2254     }
2255     if (tright->speculative_type() != nullptr) {
2256       right_type = tright->speculative_type();
2257     }
2258   }
2259 
2260   if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) {
2261     ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft);
2262     if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2263       left_ptr = speculative_left_ptr;
2264     } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2265       left_ptr = speculative_left_ptr;
2266     }
2267   }
2268   if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) {
2269     ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright);
2270     if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) {
2271       right_ptr = speculative_right_ptr;
2272     } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) {
2273       right_ptr = speculative_right_ptr;
2274     }
2275   }
2276 
2277   if (left_ptr == ProfileAlwaysNull) {
2278     // Comparison with null. Assert the input is indeed null and we're done.
2279     acmp_always_null_input(left, tleft, btest, eq_region);
2280     return;
2281   }
2282   if (right_ptr == ProfileAlwaysNull) {
2283     // Comparison with null. Assert the input is indeed null and we're done.
2284     acmp_always_null_input(right, tright, btest, eq_region);
2285     return;
2286   }
2287   if (left_type != nullptr && !left_type->is_inlinetype()) {
2288     // Comparison with an object of known type
2289     acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region);
2290     return;
2291   }
2292   if (right_type != nullptr && !right_type->is_inlinetype()) {
2293     // Comparison with an object of known type
2294     acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region);
2295     return;
2296   }
2297   if (!left_inline_type) {
2298     // Comparison with an object known not to be an inline type
2299     acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region);
2300     return;
2301   }
2302   if (!right_inline_type) {
2303     // Comparison with an object known not to be an inline type
2304     acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region);
2305     return;
2306   }
2307 
2308   // Pointers are not equal, check if first operand is non-null
2309   Node* ne_region = new RegionNode(6);
2310   Node* null_ctl;
2311   Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl);
2312   ne_region->init_req(1, null_ctl);
2313 
2314   // First operand is non-null, check if it is an inline type
2315   Node* is_value = inline_type_test(not_null_right);
2316   IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN);
2317   Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff));
2318   ne_region->init_req(2, not_value);
2319   set_control(_gvn.transform(new IfTrueNode(is_value_iff)));
2320 
2321   // The first operand is an inline type, check if the second operand is non-null
2322   Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl);
2323   ne_region->init_req(3, null_ctl);
2324 
2325   // Check if both operands are of the same class.
2326   Node* kls_left = load_object_klass(not_null_left);
2327   Node* kls_right = load_object_klass(not_null_right);
2328   Node* kls_cmp = CmpP(kls_left, kls_right);
2329   Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2330   IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2331   Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2332   set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2333   ne_region->init_req(4, kls_ne);
2334 
2335   if (stopped()) {
2336     record_for_igvn(ne_region);
2337     set_control(_gvn.transform(ne_region));
2338     if (btest == BoolTest::ne) {
2339       {
2340         PreserveJVMState pjvms(this);
2341         int target_bci = iter().get_dest();
2342         merge(target_bci);
2343       }
2344       record_for_igvn(eq_region);
2345       set_control(_gvn.transform(eq_region));
2346     }
2347     return;
2348   }
2349 
2350   // Both operands are values types of the same class, we need to perform a
2351   // substitutability test. Delegate to ValueObjectMethods::isSubstitutable().
2352   Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2353   Node* mem = reset_memory();
2354   Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2355 
2356   Node* eq_io_phi = nullptr;
2357   Node* eq_mem_phi = nullptr;
2358   if (eq_region != nullptr) {
2359     eq_io_phi = PhiNode::make(eq_region, i_o());
2360     eq_mem_phi = PhiNode::make(eq_region, mem);
2361   }
2362 
2363   set_all_memory(mem);
2364 
2365   kill_dead_locals();
2366   ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(ciSymbols::isSubstitutable_name(), ciSymbols::object_object_boolean_signature());
2367   CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method);
2368   call->set_override_symbolic_info(true);
2369   call->init_req(TypeFunc::Parms, not_null_left);
2370   call->init_req(TypeFunc::Parms+1, not_null_right);
2371   inc_sp(2);
2372   set_edges_for_java_call(call, false, false);
2373   Node* ret = set_results_for_java_call(call, false, true);
2374   dec_sp(2);
2375 
2376   // Test the return value of ValueObjectMethods::isSubstitutable()
2377   // This is the last check, do_if can emit traps now.
2378   Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2379   Node* ctl = C->top();
2380   if (btest == BoolTest::eq) {
2381     PreserveJVMState pjvms(this);
2382     do_if(btest, subst_cmp, can_trap);
2383     if (!stopped()) {
2384       ctl = control();
2385     }
2386   } else {
2387     assert(btest == BoolTest::ne, "only eq or ne");
2388     PreserveJVMState pjvms(this);
2389     do_if(btest, subst_cmp, can_trap, false, &ctl);
2390     if (!stopped()) {
2391       eq_region->init_req(2, control());
2392       eq_io_phi->init_req(2, i_o());
2393       eq_mem_phi->init_req(2, reset_memory());
2394     }
2395   }
2396   ne_region->init_req(5, ctl);
2397   ne_io_phi->init_req(5, i_o());
2398   ne_mem_phi->init_req(5, reset_memory());
2399 
2400   record_for_igvn(ne_region);
2401   set_control(_gvn.transform(ne_region));
2402   set_i_o(_gvn.transform(ne_io_phi));
2403   set_all_memory(_gvn.transform(ne_mem_phi));
2404 
2405   if (btest == BoolTest::ne) {
2406     {
2407       PreserveJVMState pjvms(this);
2408       int target_bci = iter().get_dest();
2409       merge(target_bci);
2410     }
2411 
2412     record_for_igvn(eq_region);
2413     set_control(_gvn.transform(eq_region));
2414     set_i_o(_gvn.transform(eq_io_phi));
2415     set_all_memory(_gvn.transform(eq_mem_phi));
2416   }
2417 }
2418 
2419 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
2420 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
2421 // then either takes the trap or executes the original, unstable if.
2422 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
2423   // Search for an unstable if trap
2424   CallStaticJavaNode* trap = nullptr;
2425   assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
2426   ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
2427   if (trap == nullptr || !trap->jvms()->should_reexecute()) {
2428     // No suitable trap found. Remove unused counter load and increment.
2429     C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
2430     return;
2431   }
2432 
2433   // Remove trap from optimization list since we add another path to the trap.
2434   bool success = C->remove_unstable_if_trap(trap, true);
2435   assert(success, "Trap already modified");
2436 
2437   // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
2438   int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]

2471 }
2472 
2473 void Parse::maybe_add_predicate_after_if(Block* path) {
2474   if (path->is_SEL_head() && path->preds_parsed() == 0) {
2475     // Add predicates at bci of if dominating the loop so traps can be
2476     // recorded on the if's profile data
2477     int bc_depth = repush_if_args();
2478     add_parse_predicates();
2479     dec_sp(bc_depth);
2480     path->set_has_predicates();
2481   }
2482 }
2483 
2484 
2485 //----------------------------adjust_map_after_if------------------------------
2486 // Adjust the JVM state to reflect the result of taking this path.
2487 // Basically, it means inspecting the CmpNode controlling this
2488 // branch, seeing how it constrains a tested value, and then
2489 // deciding if it's worth our while to encode this constraint
2490 // as graph nodes in the current abstract interpretation map.
2491 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2492   if (!c->is_Cmp()) {
2493     maybe_add_predicate_after_if(path);
2494     return;
2495   }
2496 
2497   if (stopped() || btest == BoolTest::illegal) {
2498     return;                             // nothing to do
2499   }
2500 
2501   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2502 
2503   if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2504     repush_if_args();
2505     Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2506                   Deoptimization::Action_reinterpret,
2507                   nullptr,
2508                   (is_fallthrough ? "taken always" : "taken never"));
2509 
2510     if (call != nullptr) {
2511       C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2512     }
2513     return;
2514   }
2515 
2516   Node* val = c->in(1);
2517   Node* con = c->in(2);
2518   const Type* tcon = _gvn.type(con);
2519   const Type* tval = _gvn.type(val);
2520   bool have_con = tcon->singleton();
2521   if (tval->singleton()) {
2522     if (!have_con) {
2523       // Swap, so constant is in con.

2580     if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2581        // Found:
2582        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2583        // or the narrowOop equivalent.
2584        const Type* obj_type = _gvn.type(obj);
2585        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2586        if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2587            tboth->higher_equal(obj_type)) {
2588           // obj has to be of the exact type Foo if the CmpP succeeds.
2589           int obj_in_map = map()->find_edge(obj);
2590           JVMState* jvms = this->jvms();
2591           if (obj_in_map >= 0 &&
2592               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2593             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2594             const Type* tcc = ccast->as_Type()->type();
2595             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2596             // Delay transform() call to allow recovery of pre-cast value
2597             // at the control merge.
2598             _gvn.set_type_bottom(ccast);
2599             record_for_igvn(ccast);
2600             if (tboth->is_inlinetypeptr()) {
2601               ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2602             }
2603             // Here's the payoff.
2604             replace_in_map(obj, ccast);
2605           }
2606        }
2607     }
2608   }
2609 
2610   int val_in_map = map()->find_edge(val);
2611   if (val_in_map < 0)  return;          // replace_in_map would be useless
2612   {
2613     JVMState* jvms = this->jvms();
2614     if (!(jvms->is_loc(val_in_map) ||
2615           jvms->is_stk(val_in_map)))
2616       return;                           // again, it would be useless
2617   }
2618 
2619   // Check for a comparison to a constant, and "know" that the compared
2620   // value is constrained on this path.
2621   assert(tcon->singleton(), "");
2622   ConstraintCastNode* ccast = nullptr;

2687   if (c->Opcode() == Op_CmpP &&
2688       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2689       c->in(2)->is_Con()) {
2690     Node* load_klass = nullptr;
2691     Node* decode = nullptr;
2692     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2693       decode = c->in(1);
2694       load_klass = c->in(1)->in(1);
2695     } else {
2696       load_klass = c->in(1);
2697     }
2698     if (load_klass->in(2)->is_AddP()) {
2699       Node* addp = load_klass->in(2);
2700       Node* obj = addp->in(AddPNode::Address);
2701       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2702       if (obj_type->speculative_type_not_null() != nullptr) {
2703         ciKlass* k = obj_type->speculative_type();
2704         inc_sp(2);
2705         obj = maybe_cast_profiled_obj(obj, k);
2706         dec_sp(2);
2707         if (obj->is_InlineType()) {
2708           assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2709           obj = obj->as_InlineType()->get_oop();
2710         }
2711         // Make the CmpP use the casted obj
2712         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2713         load_klass = load_klass->clone();
2714         load_klass->set_req(2, addp);
2715         load_klass = _gvn.transform(load_klass);
2716         if (decode != nullptr) {
2717           decode = decode->clone();
2718           decode->set_req(1, load_klass);
2719           load_klass = _gvn.transform(decode);
2720         }
2721         c = c->clone();
2722         c->set_req(1, load_klass);
2723         c = _gvn.transform(c);
2724       }
2725     }
2726   }
2727   return c;
2728 }
2729 
2730 //------------------------------do_one_bytecode--------------------------------

3524     // See if we can get some profile data and hand it off to the next block
3525     Block *target_block = block()->successor_for_bci(target_bci);
3526     if (target_block->pred_count() != 1)  break;
3527     ciMethodData* methodData = method()->method_data();
3528     if (!methodData->is_mature())  break;
3529     ciProfileData* data = methodData->bci_to_data(bci());
3530     assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3531     int taken = ((ciJumpData*)data)->taken();
3532     taken = method()->scale_count(taken);
3533     target_block->set_count(taken);
3534     break;
3535   }
3536 
3537   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
3538   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3539   handle_if_null:
3540     // If this is a backwards branch in the bytecodes, add Safepoint
3541     maybe_add_safepoint(iter().get_dest());
3542     a = null();
3543     b = pop();
3544     if (b->is_InlineType()) {
3545       // Null checking a scalarized but nullable inline type. Check the IsInit
3546       // input instead of the oop input to avoid keeping buffer allocations alive
3547       c = _gvn.transform(new CmpINode(b->as_InlineType()->get_is_init(), zerocon(T_INT)));
3548     } else {
3549       if (!_gvn.type(b)->speculative_maybe_null() &&
3550           !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3551         inc_sp(1);
3552         Node* null_ctl = top();
3553         b = null_check_oop(b, &null_ctl, true, true, true);
3554         assert(null_ctl->is_top(), "no null control here");
3555         dec_sp(1);
3556       } else if (_gvn.type(b)->speculative_always_null() &&
3557                  !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3558         inc_sp(1);
3559         b = null_assert(b);
3560         dec_sp(1);
3561       }
3562       c = _gvn.transform( new CmpPNode(b, a) );
3563     }
3564     do_ifnull(btest, c);
3565     break;
3566 
3567   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3568   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3569   handle_if_acmp:
3570     // If this is a backwards branch in the bytecodes, add Safepoint
3571     maybe_add_safepoint(iter().get_dest());
3572     a = pop();
3573     b = pop();
3574     do_acmp(btest, b, a);


3575     break;
3576 
3577   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3578   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3579   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3580   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3581   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3582   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3583   handle_ifxx:
3584     // If this is a backwards branch in the bytecodes, add Safepoint
3585     maybe_add_safepoint(iter().get_dest());
3586     a = _gvn.intcon(0);
3587     b = pop();
3588     c = _gvn.transform( new CmpINode(b, a) );
3589     do_if(btest, c);
3590     break;
3591 
3592   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3593   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3594   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;

3609     break;
3610 
3611   case Bytecodes::_lookupswitch:
3612     do_lookupswitch();
3613     break;
3614 
3615   case Bytecodes::_invokestatic:
3616   case Bytecodes::_invokedynamic:
3617   case Bytecodes::_invokespecial:
3618   case Bytecodes::_invokevirtual:
3619   case Bytecodes::_invokeinterface:
3620     do_call();
3621     break;
3622   case Bytecodes::_checkcast:
3623     do_checkcast();
3624     break;
3625   case Bytecodes::_instanceof:
3626     do_instanceof();
3627     break;
3628   case Bytecodes::_anewarray:
3629     do_newarray();
3630     break;
3631   case Bytecodes::_newarray:
3632     do_newarray((BasicType)iter().get_index());
3633     break;
3634   case Bytecodes::_multianewarray:
3635     do_multianewarray();
3636     break;
3637   case Bytecodes::_new:
3638     do_new();
3639     break;
3640 
3641   case Bytecodes::_jsr:
3642   case Bytecodes::_jsr_w:
3643     do_jsr();
3644     break;
3645 
3646   case Bytecodes::_ret:
3647     do_ret();
3648     break;
3649 
< prev index next >