< prev index next >

src/hotspot/share/opto/parse2.cpp

Print this page

   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 "runtime/deoptimization.hpp"
  45 #include "runtime/sharedRuntime.hpp"
  46 
  47 #ifndef PRODUCT
  48 extern uint explicit_null_checks_inserted,
  49             explicit_null_checks_elided;
  50 #endif
  51 

















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




























































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

  73   }

  74 }
  75 




























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

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








































































 102 
 103   access_store_at(array, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);



















 104 }
 105 

























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

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







































































































































































































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

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



1479   }
1480 
1481   // Sanity check the probability value
1482   assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1483 
1484   bool taken_if_true = true;
1485   // Convert BoolTest to canonical form:
1486   if (!BoolTest(btest).is_canonical()) {
1487     btest         = BoolTest(btest).negate();
1488     taken_if_true = false;
1489     // prob is NOT updated here; it remains the probability of the taken
1490     // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1491   }
1492   assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1493 
1494   Node* tst0 = new BoolNode(c, btest);
1495   Node* tst = _gvn.transform(tst0);
1496   BoolTest::mask taken_btest   = BoolTest::illegal;
1497   BoolTest::mask untaken_btest = BoolTest::illegal;
1498 

1519   }
1520 
1521   // Generate real control flow
1522   float true_prob = (taken_if_true ? prob : untaken_prob);
1523   IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1524   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1525   Node* taken_branch   = new IfTrueNode(iff);
1526   Node* untaken_branch = new IfFalseNode(iff);
1527   if (!taken_if_true) {  // Finish conversion to canonical form
1528     Node* tmp      = taken_branch;
1529     taken_branch   = untaken_branch;
1530     untaken_branch = tmp;
1531   }
1532 
1533   // Branch is taken:
1534   { PreserveJVMState pjvms(this);
1535     taken_branch = _gvn.transform(taken_branch);
1536     set_control(taken_branch);
1537 
1538     if (stopped()) {
1539       if (C->eliminate_boxing()) {
1540         // Mark the successor block as parsed
1541         branch_block->next_path_num();
1542       }
1543     } else {
1544       adjust_map_after_if(taken_btest, c, prob, branch_block);
1545       if (!stopped()) {
1546         merge(target_bci);








1547       }
1548     }
1549   }
1550 
1551   untaken_branch = _gvn.transform(untaken_branch);
1552   set_control(untaken_branch);
1553 
1554   // Branch not taken.
1555   if (stopped()) {
1556     if (C->eliminate_boxing()) {
1557       // Mark the successor block as parsed
1558       next_block->next_path_num();
1559     }
1560   } else {
1561     adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1562   }
1563 
1564   if (do_stress_trap) {
1565     stress_trap(iff, counter, incr_store);
1566   }
1567 }
1568 

























































































































































































































































































































































































































1569 // Force unstable if traps to be taken randomly to trigger intermittent bugs such as incorrect debug information.
1570 // Add another if before the unstable if that checks a "random" condition at runtime (a simple shared counter) and
1571 // then either takes the trap or executes the original, unstable if.
1572 void Parse::stress_trap(IfNode* orig_iff, Node* counter, Node* incr_store) {
1573   // Search for an unstable if trap
1574   CallStaticJavaNode* trap = nullptr;
1575   assert(orig_iff->Opcode() == Op_If && orig_iff->outcnt() == 2, "malformed if");
1576   ProjNode* trap_proj = orig_iff->uncommon_trap_proj(trap, Deoptimization::Reason_unstable_if);
1577   if (trap == nullptr || !trap->jvms()->should_reexecute()) {
1578     // No suitable trap found. Remove unused counter load and increment.
1579     C->gvn_replace_by(incr_store, incr_store->in(MemNode::Memory));
1580     return;
1581   }
1582 
1583   // Remove trap from optimization list since we add another path to the trap.
1584   bool success = C->remove_unstable_if_trap(trap, true);
1585   assert(success, "Trap already modified");
1586 
1587   // Add a check before the original if that will trap with a certain frequency and execute the original if otherwise
1588   int freq_log = (C->random() % 31) + 1; // Random logarithmic frequency in [1, 31]

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

1730     if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
1731        // Found:
1732        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1733        // or the narrowOop equivalent.
1734        const Type* obj_type = _gvn.type(obj);
1735        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
1736        if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
1737            tboth->higher_equal(obj_type)) {
1738           // obj has to be of the exact type Foo if the CmpP succeeds.
1739           int obj_in_map = map()->find_edge(obj);
1740           JVMState* jvms = this->jvms();
1741           if (obj_in_map >= 0 &&
1742               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1743             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1744             const Type* tcc = ccast->as_Type()->type();
1745             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1746             // Delay transform() call to allow recovery of pre-cast value
1747             // at the control merge.
1748             _gvn.set_type_bottom(ccast);
1749             record_for_igvn(ccast);



1750             // Here's the payoff.
1751             replace_in_map(obj, ccast);
1752           }
1753        }
1754     }
1755   }
1756 
1757   int val_in_map = map()->find_edge(val);
1758   if (val_in_map < 0)  return;          // replace_in_map would be useless
1759   {
1760     JVMState* jvms = this->jvms();
1761     if (!(jvms->is_loc(val_in_map) ||
1762           jvms->is_stk(val_in_map)))
1763       return;                           // again, it would be useless
1764   }
1765 
1766   // Check for a comparison to a constant, and "know" that the compared
1767   // value is constrained on this path.
1768   assert(tcon->singleton(), "");
1769   ConstraintCastNode* ccast = nullptr;

1834   if (c->Opcode() == Op_CmpP &&
1835       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1836       c->in(2)->is_Con()) {
1837     Node* load_klass = nullptr;
1838     Node* decode = nullptr;
1839     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1840       decode = c->in(1);
1841       load_klass = c->in(1)->in(1);
1842     } else {
1843       load_klass = c->in(1);
1844     }
1845     if (load_klass->in(2)->is_AddP()) {
1846       Node* addp = load_klass->in(2);
1847       Node* obj = addp->in(AddPNode::Address);
1848       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1849       if (obj_type->speculative_type_not_null() != nullptr) {
1850         ciKlass* k = obj_type->speculative_type();
1851         inc_sp(2);
1852         obj = maybe_cast_profiled_obj(obj, k);
1853         dec_sp(2);




1854         // Make the CmpP use the casted obj
1855         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1856         load_klass = load_klass->clone();
1857         load_klass->set_req(2, addp);
1858         load_klass = _gvn.transform(load_klass);
1859         if (decode != nullptr) {
1860           decode = decode->clone();
1861           decode->set_req(1, load_klass);
1862           load_klass = _gvn.transform(decode);
1863         }
1864         c = c->clone();
1865         c->set_req(1, load_klass);
1866         c = _gvn.transform(c);
1867       }
1868     }
1869   }
1870   return c;
1871 }
1872 
1873 //------------------------------do_one_bytecode--------------------------------

2573 
2574   case Bytecodes::_i2d:
2575     a = pop();
2576     b = _gvn.transform( new ConvI2DNode(a));
2577     push_pair(b);
2578     break;
2579 
2580   case Bytecodes::_iinc:        // Increment local
2581     i = iter().get_index();     // Get local index
2582     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2583     break;
2584 
2585   // Exit points of synchronized methods must have an unlock node
2586   case Bytecodes::_return:
2587     return_current(nullptr);
2588     break;
2589 
2590   case Bytecodes::_ireturn:
2591   case Bytecodes::_areturn:
2592   case Bytecodes::_freturn:
2593     return_current(pop());
2594     break;
2595   case Bytecodes::_lreturn:
2596     return_current(pop_pair());
2597     break;
2598   case Bytecodes::_dreturn:
2599     return_current(pop_pair());
2600     break;
2601 
2602   case Bytecodes::_athrow:
2603     // null exception oop throws null pointer exception
2604     null_check(peek());
2605     if (stopped())  return;
2606     // Hook the thrown exception directly to subsequent handlers.
2607     if (BailoutToInterpreterForThrows) {
2608       // Keep method interpreted from now on.
2609       uncommon_trap(Deoptimization::Reason_unhandled,
2610                     Deoptimization::Action_make_not_compilable);
2611       return;
2612     }
2613     if (env()->jvmti_can_post_on_exceptions()) {
2614       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2615       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2616     }
2617     // Here if either can_post_on_exceptions or should_post_on_exceptions is false

2630 
2631     // See if we can get some profile data and hand it off to the next block
2632     Block *target_block = block()->successor_for_bci(target_bci);
2633     if (target_block->pred_count() != 1)  break;
2634     ciMethodData* methodData = method()->method_data();
2635     if (!methodData->is_mature())  break;
2636     ciProfileData* data = methodData->bci_to_data(bci());
2637     assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2638     int taken = ((ciJumpData*)data)->taken();
2639     taken = method()->scale_count(taken);
2640     target_block->set_count(taken);
2641     break;
2642   }
2643 
2644   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
2645   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2646   handle_if_null:
2647     // If this is a backwards branch in the bytecodes, add Safepoint
2648     maybe_add_safepoint(iter().get_dest());
2649     a = null();
2650     b = pop();
2651     if (!_gvn.type(b)->speculative_maybe_null() &&
2652         !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2653       inc_sp(1);
2654       Node* null_ctl = top();
2655       b = null_check_oop(b, &null_ctl, true, true, true);
2656       assert(null_ctl->is_top(), "no null control here");
2657       dec_sp(1);
2658     } else if (_gvn.type(b)->speculative_always_null() &&
2659                !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2660       inc_sp(1);
2661       b = null_assert(b);
2662       dec_sp(1);
2663     }
2664     c = _gvn.transform( new CmpPNode(b, a) );






2665     do_ifnull(btest, c);
2666     break;
2667 
2668   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2669   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2670   handle_if_acmp:
2671     // If this is a backwards branch in the bytecodes, add Safepoint
2672     maybe_add_safepoint(iter().get_dest());
2673     a = pop();
2674     b = pop();
2675     c = _gvn.transform( new CmpPNode(b, a) );
2676     c = optimize_cmp_with_klass(c);
2677     do_if(btest, c);
2678     break;
2679 
2680   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2681   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2682   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2683   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2684   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2685   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2686   handle_ifxx:
2687     // If this is a backwards branch in the bytecodes, add Safepoint
2688     maybe_add_safepoint(iter().get_dest());
2689     a = _gvn.intcon(0);
2690     b = pop();
2691     c = _gvn.transform( new CmpINode(b, a) );
2692     do_if(btest, c);
2693     break;
2694 
2695   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2696   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2697   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;

2712     break;
2713 
2714   case Bytecodes::_lookupswitch:
2715     do_lookupswitch();
2716     break;
2717 
2718   case Bytecodes::_invokestatic:
2719   case Bytecodes::_invokedynamic:
2720   case Bytecodes::_invokespecial:
2721   case Bytecodes::_invokevirtual:
2722   case Bytecodes::_invokeinterface:
2723     do_call();
2724     break;
2725   case Bytecodes::_checkcast:
2726     do_checkcast();
2727     break;
2728   case Bytecodes::_instanceof:
2729     do_instanceof();
2730     break;
2731   case Bytecodes::_anewarray:
2732     do_anewarray();
2733     break;
2734   case Bytecodes::_newarray:
2735     do_newarray((BasicType)iter().get_index());
2736     break;
2737   case Bytecodes::_multianewarray:
2738     do_multianewarray();
2739     break;
2740   case Bytecodes::_new:
2741     do_new();
2742     break;
2743 
2744   case Bytecodes::_jsr:
2745   case Bytecodes::_jsr_w:
2746     do_jsr();
2747     break;
2748 
2749   case Bytecodes::_ret:
2750     do_ret();
2751     break;
2752 

   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 "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 
  86   if (!array_type->is_not_flat()) {
  87     // Cannot statically determine if array is a flat array, emit runtime check
  88     assert(UseArrayFlattening && is_reference_type(bt) && element_ptr->can_be_inline_type() &&
  89            (!element_ptr->is_inlinetypeptr() || element_ptr->inline_klass()->maybe_flat_in_array()), "array can't be flat");
  90     IdealKit ideal(this);
  91     IdealVariable res(ideal);
  92     ideal.declarations_done();
  93     ideal.if_then(flat_array_test(array, /* flat = */ false)); {
  94       // Non-flat array
  95       sync_kit(ideal);
  96       if (!array_type->is_flat()) {
  97         assert(array_type->is_flat() || control()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
  98         const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
  99         DecoratorSet decorator_set = IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD;
 100         if (needs_range_check(array_type->size(), array_index)) {
 101           // We've emitted a RangeCheck but now insert an additional check between the range check and the actual load.
 102           // We cannot pin the load to two separate nodes. Instead, we pin it conservatively here such that it cannot
 103           // possibly float above the range check at any point.
 104           decorator_set |= C2_UNKNOWN_CONTROL_LOAD;
 105         }
 106         Node* ld = access_load_at(array, adr, adr_type, element_ptr, bt, decorator_set);
 107         if (element_ptr->is_inlinetypeptr()) {
 108           ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
 109         }
 110         ideal.set(res, ld);
 111       }
 112       ideal.sync_kit(this);
 113     } ideal.else_(); {
 114       // Flat array
 115       sync_kit(ideal);
 116       if (!array_type->is_not_flat()) {
 117         if (element_ptr->is_inlinetypeptr()) {
 118           ciInlineKlass* vk = element_ptr->inline_klass();
 119           Node* flat_array = cast_to_flat_array(array, vk, false, false, false);
 120           Node* vt = InlineTypeNode::make_from_flat_array(this, vk, flat_array, array_index);
 121           ideal.set(res, vt);
 122         } else {
 123           // Element type is unknown, and thus we cannot statically determine the exact flat array layout. Emit a
 124           // runtime call to correctly load the inline type element from the flat array.
 125           Node* inline_type = load_from_unknown_flat_array(array, array_index, element_ptr);
 126           bool is_null_free = array_type->is_null_free() || !UseNullableValueFlattening;
 127           if (is_null_free) {
 128             inline_type = cast_not_null(inline_type);
 129           }
 130           ideal.set(res, inline_type);
 131         }
 132       }
 133       ideal.sync_kit(this);
 134     } ideal.end_if();
 135     sync_kit(ideal);
 136     Node* ld = _gvn.transform(ideal.value(res));
 137     ld = record_profile_for_speculation_at_array_load(ld);
 138     push_node(bt, ld);
 139     return;
 140   }
 141 
 142   if (elemtype == TypeInt::BOOL) {
 143     bt = T_BOOLEAN;
 144   }
 145   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);

 146   Node* ld = access_load_at(array, adr, adr_type, elemtype, bt,
 147                             IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
 148   ld = record_profile_for_speculation_at_array_load(ld);
 149   // Loading an inline type from a non-flat array
 150   if (element_ptr != nullptr && element_ptr->is_inlinetypeptr()) {
 151     assert(!array_type->is_null_free() || !element_ptr->maybe_null(), "inline type array elements should never be null");
 152     ld = InlineTypeNode::make_from_oop(this, ld, element_ptr->inline_klass());
 153   }
 154   push_node(bt, ld);
 155 }
 156 
 157 Node* Parse::load_from_unknown_flat_array(Node* array, Node* array_index, const TypeOopPtr* element_ptr) {
 158   // Below membars keep this access to an unknown flat array correctly
 159   // ordered with other unknown and known flat array accesses.
 160   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 161 
 162   Node* call = nullptr;
 163   {
 164     // Re-execute flat array load if runtime call triggers deoptimization
 165     PreserveReexecuteState preexecs(this);
 166     jvms()->set_bci(_bci);
 167     jvms()->set_should_reexecute(true);
 168     inc_sp(2);
 169     kill_dead_locals();
 170     call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 171                              OptoRuntime::load_unknown_inline_Type(),
 172                              OptoRuntime::load_unknown_inline_Java(),
 173                              nullptr, TypeRawPtr::BOTTOM,
 174                              array, array_index);
 175   }
 176   make_slow_call_ex(call, env()->Throwable_klass(), false);
 177   Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
 178 
 179   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 180 
 181   // Keep track of the information that the inline type is in flat arrays
 182   const Type* unknown_value = element_ptr->is_instptr()->cast_to_flat_in_array();
 183   return _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value));
 184 }
 185 
 186 //--------------------------------array_store----------------------------------
 187 void Parse::array_store(BasicType bt) {
 188   const Type* elemtype = Type::TOP;
 189   Node* adr = array_addressing(bt, type2size[bt], elemtype);

 190   if (stopped())  return;     // guaranteed null or range check
 191   Node* stored_value_casted = nullptr;
 192   if (bt == T_OBJECT) {
 193     stored_value_casted = array_store_check(adr, elemtype);
 194     if (stopped()) {
 195       return;
 196     }
 197   }
 198   Node* const stored_value = pop_node(bt); // Value to store
 199   Node* const array_index = pop();         // Index in the array
 200   Node* array = pop();                     // The array itself
 201 
 202   const TypeAryPtr* array_type = _gvn.type(array)->is_aryptr();
 203   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);


 204 
 205   if (elemtype == TypeInt::BOOL) {
 206     bt = T_BOOLEAN;
 207   } else if (bt == T_OBJECT) {
 208     elemtype = elemtype->make_oopptr();
 209     const Type* stored_value_casted_type = _gvn.type(stored_value_casted);
 210     // Based on the value to be stored, try to determine if the array is not null-free and/or not flat.
 211     // This is only legal for non-null stores because the array_store_check always passes for null, even
 212     // if the array is null-free. Null stores are handled in GraphKit::inline_array_null_guard().
 213     bool not_inline = !stored_value_casted_type->maybe_null() && !stored_value_casted_type->is_oopptr()->can_be_inline_type();
 214     bool not_null_free = not_inline;
 215     bool not_flat = not_inline || ( stored_value_casted_type->is_inlinetypeptr() &&
 216                                    !stored_value_casted_type->inline_klass()->maybe_flat_in_array());
 217     if (!array_type->is_not_null_free() && not_null_free) {
 218       // Storing a non-inline type, mark array as not null-free.
 219       array_type = array_type->cast_to_not_null_free();
 220       Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
 221       replace_in_map(array, cast);
 222       array = cast;
 223     }
 224     if (!array_type->is_not_flat() && not_flat) {
 225       // Storing to a non-flat array, mark array as not flat.
 226       array_type = array_type->cast_to_not_flat();
 227       Node* cast = _gvn.transform(new CheckCastPPNode(control(), array, array_type));
 228       replace_in_map(array, cast);
 229       array = cast;
 230     }
 231 
 232     if (!array_type->is_flat() && array_type->is_null_free()) {
 233       // Store to non-flat null-free inline type array (elements can never be null)
 234       assert(!stored_value_casted_type->maybe_null(), "should be guaranteed by array store check");
 235       if (elemtype->is_inlinetypeptr() && elemtype->inline_klass()->is_empty()) {
 236         // Ignore empty inline stores, array is already initialized.
 237         return;
 238       }
 239     } else if (!array_type->is_not_flat()) {
 240       // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient).
 241       assert(UseArrayFlattening && !not_flat && elemtype->is_oopptr()->can_be_inline_type() &&
 242              (!array_type->klass_is_exact() || array_type->is_flat()), "array can't be a flat array");
 243       // 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.
 244       array = inline_array_null_guard(array, stored_value_casted, 3, true);
 245       IdealKit ideal(this);
 246       ideal.if_then(flat_array_test(array, /* flat = */ false)); {
 247         // Non-flat array
 248         if (!array_type->is_flat()) {
 249           sync_kit(ideal);
 250           assert(array_type->is_flat() || ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found");
 251           inc_sp(3);
 252           access_store_at(array, adr, adr_type, stored_value_casted, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false);
 253           dec_sp(3);
 254           ideal.sync_kit(this);
 255         }
 256       } ideal.else_(); {
 257         // Flat array
 258         sync_kit(ideal);
 259         if (!array_type->is_not_flat()) {
 260           // Try to determine the inline klass type of the stored value
 261           ciInlineKlass* vk = nullptr;
 262           if (stored_value_casted_type->is_inlinetypeptr()) {
 263             vk = stored_value_casted_type->inline_klass();
 264           } else if (elemtype->is_inlinetypeptr()) {
 265             vk = elemtype->inline_klass();
 266           }
 267 
 268           if (vk != nullptr) {
 269             // Element type is known, cast and store to flat array layout.
 270             Node* flat_array = cast_to_flat_array(array, vk, false, false, false);
 271 
 272             // Re-execute flat array store if buffering triggers deoptimization
 273             PreserveReexecuteState preexecs(this);
 274             jvms()->set_should_reexecute(true);
 275             inc_sp(3);
 276 
 277             if (!stored_value_casted->is_InlineType()) {
 278               assert(_gvn.type(stored_value_casted) == TypePtr::NULL_PTR, "Unexpected value");
 279               stored_value_casted = InlineTypeNode::make_null(_gvn, vk);
 280             }
 281 
 282             stored_value_casted->as_InlineType()->store_flat_array(this, flat_array, array_index);
 283           } else {
 284             // Element type is unknown, emit a runtime call since the flat array layout is not statically known.
 285             store_to_unknown_flat_array(array, array_index, stored_value_casted);
 286           }
 287         }
 288         ideal.sync_kit(this);
 289       }
 290       ideal.end_if();
 291       sync_kit(ideal);
 292       return;
 293     } else if (!array_type->is_not_null_free()) {
 294       // Array is not flat but may be null free
 295       assert(elemtype->is_oopptr()->can_be_inline_type(), "array can't be null-free");
 296       array = inline_array_null_guard(array, stored_value_casted, 3, true);
 297     }
 298   }
 299   inc_sp(3);
 300   access_store_at(array, adr, adr_type, stored_value, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 301   dec_sp(3);
 302 }
 303 
 304 // Emit a runtime call to store to a flat array whose element type is either unknown (i.e. we do not know the flat
 305 // array layout) or not exact (could have different flat array layouts at runtime).
 306 void Parse::store_to_unknown_flat_array(Node* array, Node* const idx, Node* non_null_stored_value) {
 307   // Below membars keep this access to an unknown flat array correctly
 308   // ordered with other unknown and known flat array accesses.
 309   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 310 
 311   Node* call = nullptr;
 312   {
 313     // Re-execute flat array store if runtime call triggers deoptimization
 314     PreserveReexecuteState preexecs(this);
 315     jvms()->set_bci(_bci);
 316     jvms()->set_should_reexecute(true);
 317     inc_sp(3);
 318     kill_dead_locals();
 319     call = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
 320                       OptoRuntime::store_unknown_inline_Type(),
 321                       OptoRuntime::store_unknown_inline_Java(),
 322                       nullptr, TypeRawPtr::BOTTOM,
 323                       non_null_stored_value, array, idx);
 324   }
 325   make_slow_call_ex(call, env()->Throwable_klass(), false);
 326 
 327   insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES));
 328 }
 329 
 330 //------------------------------array_addressing-------------------------------
 331 // Pull array and index from the stack.  Compute pointer-to-element.
 332 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) {
 333   Node *idx   = peek(0+vals);   // Get from stack without popping
 334   Node *ary   = peek(1+vals);   // in case of exception
 335 
 336   // Null check the array base, with correct stack contents
 337   ary = null_check(ary, T_ARRAY);
 338   // Compile-time detect of null-exception?
 339   if (stopped())  return top();
 340 
 341   const TypeAryPtr* arytype  = _gvn.type(ary)->is_aryptr();
 342   const TypeInt*    sizetype = arytype->size();
 343   elemtype = arytype->elem();
 344 
 345   if (UseUniqueSubclasses) {
 346     const Type* el = elemtype->make_ptr();
 347     if (el && el->isa_instptr()) {
 348       const TypeInstPtr* toop = el->is_instptr();
 349       if (toop->instance_klass()->unique_concrete_subklass()) {
 350         // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
 351         const Type* subklass = Type::get_const_type(toop->instance_klass());
 352         elemtype = subklass->join_speculative(el);
 353       }
 354     }
 355   }
 356 











 357   if (!arytype->is_loaded()) {
 358     // Only fails for some -Xcomp runs
 359     // The class is unloaded.  We have to run this bytecode in the interpreter.
 360     ciKlass* klass = arytype->unloaded_klass();
 361 
 362     uncommon_trap(Deoptimization::Reason_unloaded,
 363                   Deoptimization::Action_reinterpret,
 364                   klass, "!loaded array");
 365     return top();
 366   }
 367 
 368   ary = create_speculative_inline_type_array_checks(ary, arytype, elemtype);











 369 
 370   if (needs_range_check(sizetype, idx)) {
 371     create_range_check(idx, ary, sizetype);
 372   } else if (C->log() != nullptr) {
 373     C->log()->elem("observe that='!need_range_check'");



























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

1802   // False branch
1803   Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1804   set_control(iffalse);
1805 
1806   if (stopped()) {              // Path is dead?
1807     NOT_PRODUCT(explicit_null_checks_elided++);
1808     if (C->eliminate_boxing()) {
1809       // Mark the successor block as parsed
1810       next_block->next_path_num();
1811     }
1812   } else  {                     // Path is live.
1813     adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1814   }
1815 
1816   if (do_stress_trap) {
1817     stress_trap(iff, counter, incr_store);
1818   }
1819 }
1820 
1821 //------------------------------------do_if------------------------------------
1822 void Parse::do_if(BoolTest::mask btest, Node* c, bool can_trap, bool new_path, Node** ctrl_taken, Node** stress_count_mem) {
1823   int target_bci = iter().get_dest();
1824 
1825   Block* branch_block = successor_for_bci(target_bci);
1826   Block* next_block   = successor_for_bci(iter().next_bci());
1827 
1828   float cnt;
1829   float prob = branch_prediction(cnt, btest, target_bci, c);
1830   float untaken_prob = 1.0 - prob;
1831 
1832   if (prob == PROB_UNKNOWN) {
1833     if (PrintOpto && Verbose) {
1834       tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1835     }
1836     repush_if_args(); // to gather stats on loop
1837     uncommon_trap(Deoptimization::Reason_unreached,
1838                   Deoptimization::Action_reinterpret,
1839                   nullptr, "cold");
1840     if (C->eliminate_boxing()) {
1841       // Mark the successor blocks as parsed
1842       branch_block->next_path_num();
1843       next_block->next_path_num();
1844     }
1845     return;
1846   }
1847 
1848   Node* counter = nullptr;
1849   Node* incr_store = nullptr;
1850   bool do_stress_trap = StressUnstableIfTraps && ((C->random() % 2) == 0);
1851   if (do_stress_trap) {
1852     increment_trap_stress_counter(counter, incr_store);
1853     if (stress_count_mem != nullptr) {
1854       *stress_count_mem = incr_store;
1855     }
1856   }
1857 
1858   // Sanity check the probability value
1859   assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1860 
1861   bool taken_if_true = true;
1862   // Convert BoolTest to canonical form:
1863   if (!BoolTest(btest).is_canonical()) {
1864     btest         = BoolTest(btest).negate();
1865     taken_if_true = false;
1866     // prob is NOT updated here; it remains the probability of the taken
1867     // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1868   }
1869   assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1870 
1871   Node* tst0 = new BoolNode(c, btest);
1872   Node* tst = _gvn.transform(tst0);
1873   BoolTest::mask taken_btest   = BoolTest::illegal;
1874   BoolTest::mask untaken_btest = BoolTest::illegal;
1875 

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

2415 }
2416 
2417 void Parse::maybe_add_predicate_after_if(Block* path) {
2418   if (path->is_SEL_head() && path->preds_parsed() == 0) {
2419     // Add predicates at bci of if dominating the loop so traps can be
2420     // recorded on the if's profile data
2421     int bc_depth = repush_if_args();
2422     add_parse_predicates();
2423     dec_sp(bc_depth);
2424     path->set_has_predicates();
2425   }
2426 }
2427 
2428 
2429 //----------------------------adjust_map_after_if------------------------------
2430 // Adjust the JVM state to reflect the result of taking this path.
2431 // Basically, it means inspecting the CmpNode controlling this
2432 // branch, seeing how it constrains a tested value, and then
2433 // deciding if it's worth our while to encode this constraint
2434 // as graph nodes in the current abstract interpretation map.
2435 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2436   if (!c->is_Cmp()) {
2437     maybe_add_predicate_after_if(path);
2438     return;
2439   }
2440 
2441   if (stopped() || btest == BoolTest::illegal) {
2442     return;                             // nothing to do
2443   }
2444 
2445   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2446 
2447   if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2448     repush_if_args();
2449     Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2450                   Deoptimization::Action_reinterpret,
2451                   nullptr,
2452                   (is_fallthrough ? "taken always" : "taken never"));
2453 
2454     if (call != nullptr) {
2455       C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2456     }
2457     return;
2458   }
2459 
2460   Node* val = c->in(1);
2461   Node* con = c->in(2);
2462   const Type* tcon = _gvn.type(con);
2463   const Type* tval = _gvn.type(val);
2464   bool have_con = tcon->singleton();
2465   if (tval->singleton()) {
2466     if (!have_con) {
2467       // Swap, so constant is in con.

2524     if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2525        // Found:
2526        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2527        // or the narrowOop equivalent.
2528        const Type* obj_type = _gvn.type(obj);
2529        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2530        if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type &&
2531            tboth->higher_equal(obj_type)) {
2532           // obj has to be of the exact type Foo if the CmpP succeeds.
2533           int obj_in_map = map()->find_edge(obj);
2534           JVMState* jvms = this->jvms();
2535           if (obj_in_map >= 0 &&
2536               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2537             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2538             const Type* tcc = ccast->as_Type()->type();
2539             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2540             // Delay transform() call to allow recovery of pre-cast value
2541             // at the control merge.
2542             _gvn.set_type_bottom(ccast);
2543             record_for_igvn(ccast);
2544             if (tboth->is_inlinetypeptr()) {
2545               ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2546             }
2547             // Here's the payoff.
2548             replace_in_map(obj, ccast);
2549           }
2550        }
2551     }
2552   }
2553 
2554   int val_in_map = map()->find_edge(val);
2555   if (val_in_map < 0)  return;          // replace_in_map would be useless
2556   {
2557     JVMState* jvms = this->jvms();
2558     if (!(jvms->is_loc(val_in_map) ||
2559           jvms->is_stk(val_in_map)))
2560       return;                           // again, it would be useless
2561   }
2562 
2563   // Check for a comparison to a constant, and "know" that the compared
2564   // value is constrained on this path.
2565   assert(tcon->singleton(), "");
2566   ConstraintCastNode* ccast = nullptr;

2631   if (c->Opcode() == Op_CmpP &&
2632       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2633       c->in(2)->is_Con()) {
2634     Node* load_klass = nullptr;
2635     Node* decode = nullptr;
2636     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2637       decode = c->in(1);
2638       load_klass = c->in(1)->in(1);
2639     } else {
2640       load_klass = c->in(1);
2641     }
2642     if (load_klass->in(2)->is_AddP()) {
2643       Node* addp = load_klass->in(2);
2644       Node* obj = addp->in(AddPNode::Address);
2645       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2646       if (obj_type->speculative_type_not_null() != nullptr) {
2647         ciKlass* k = obj_type->speculative_type();
2648         inc_sp(2);
2649         obj = maybe_cast_profiled_obj(obj, k);
2650         dec_sp(2);
2651         if (obj->is_InlineType()) {
2652           assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2653           obj = obj->as_InlineType()->get_oop();
2654         }
2655         // Make the CmpP use the casted obj
2656         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2657         load_klass = load_klass->clone();
2658         load_klass->set_req(2, addp);
2659         load_klass = _gvn.transform(load_klass);
2660         if (decode != nullptr) {
2661           decode = decode->clone();
2662           decode->set_req(1, load_klass);
2663           load_klass = _gvn.transform(decode);
2664         }
2665         c = c->clone();
2666         c->set_req(1, load_klass);
2667         c = _gvn.transform(c);
2668       }
2669     }
2670   }
2671   return c;
2672 }
2673 
2674 //------------------------------do_one_bytecode--------------------------------

3374 
3375   case Bytecodes::_i2d:
3376     a = pop();
3377     b = _gvn.transform( new ConvI2DNode(a));
3378     push_pair(b);
3379     break;
3380 
3381   case Bytecodes::_iinc:        // Increment local
3382     i = iter().get_index();     // Get local index
3383     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
3384     break;
3385 
3386   // Exit points of synchronized methods must have an unlock node
3387   case Bytecodes::_return:
3388     return_current(nullptr);
3389     break;
3390 
3391   case Bytecodes::_ireturn:
3392   case Bytecodes::_areturn:
3393   case Bytecodes::_freturn:
3394     return_current(cast_to_non_larval(pop()));
3395     break;
3396   case Bytecodes::_lreturn:


3397   case Bytecodes::_dreturn:
3398     return_current(pop_pair());
3399     break;
3400 
3401   case Bytecodes::_athrow:
3402     // null exception oop throws null pointer exception
3403     null_check(peek());
3404     if (stopped())  return;
3405     // Hook the thrown exception directly to subsequent handlers.
3406     if (BailoutToInterpreterForThrows) {
3407       // Keep method interpreted from now on.
3408       uncommon_trap(Deoptimization::Reason_unhandled,
3409                     Deoptimization::Action_make_not_compilable);
3410       return;
3411     }
3412     if (env()->jvmti_can_post_on_exceptions()) {
3413       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
3414       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
3415     }
3416     // Here if either can_post_on_exceptions or should_post_on_exceptions is false

3429 
3430     // See if we can get some profile data and hand it off to the next block
3431     Block *target_block = block()->successor_for_bci(target_bci);
3432     if (target_block->pred_count() != 1)  break;
3433     ciMethodData* methodData = method()->method_data();
3434     if (!methodData->is_mature())  break;
3435     ciProfileData* data = methodData->bci_to_data(bci());
3436     assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
3437     int taken = ((ciJumpData*)data)->taken();
3438     taken = method()->scale_count(taken);
3439     target_block->set_count(taken);
3440     break;
3441   }
3442 
3443   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
3444   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3445   handle_if_null:
3446     // If this is a backwards branch in the bytecodes, add Safepoint
3447     maybe_add_safepoint(iter().get_dest());
3448     a = null();
3449     b = cast_to_non_larval(pop());
3450     if (b->is_InlineType()) {
3451       // Null checking a scalarized but nullable inline type. Check the null marker
3452       // input instead of the oop input to avoid keeping buffer allocations alive
3453       c = _gvn.transform(new CmpINode(b->as_InlineType()->get_null_marker(), zerocon(T_INT)));
3454     } else {
3455       if (!_gvn.type(b)->speculative_maybe_null() &&
3456           !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3457         inc_sp(1);
3458         Node* null_ctl = top();
3459         b = null_check_oop(b, &null_ctl, true, true, true);
3460         assert(null_ctl->is_top(), "no null control here");
3461         dec_sp(1);
3462       } else if (_gvn.type(b)->speculative_always_null() &&
3463                  !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3464         inc_sp(1);
3465         b = null_assert(b);
3466         dec_sp(1);
3467       }
3468       c = _gvn.transform( new CmpPNode(b, a) );
3469     }
3470     do_ifnull(btest, c);
3471     break;
3472 
3473   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3474   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3475   handle_if_acmp:
3476     // If this is a backwards branch in the bytecodes, add Safepoint
3477     maybe_add_safepoint(iter().get_dest());
3478     a = cast_to_non_larval(pop());
3479     b = cast_to_non_larval(pop());
3480     do_acmp(btest, b, a);


3481     break;
3482 
3483   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3484   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3485   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3486   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3487   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3488   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3489   handle_ifxx:
3490     // If this is a backwards branch in the bytecodes, add Safepoint
3491     maybe_add_safepoint(iter().get_dest());
3492     a = _gvn.intcon(0);
3493     b = pop();
3494     c = _gvn.transform( new CmpINode(b, a) );
3495     do_if(btest, c);
3496     break;
3497 
3498   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3499   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3500   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;

3515     break;
3516 
3517   case Bytecodes::_lookupswitch:
3518     do_lookupswitch();
3519     break;
3520 
3521   case Bytecodes::_invokestatic:
3522   case Bytecodes::_invokedynamic:
3523   case Bytecodes::_invokespecial:
3524   case Bytecodes::_invokevirtual:
3525   case Bytecodes::_invokeinterface:
3526     do_call();
3527     break;
3528   case Bytecodes::_checkcast:
3529     do_checkcast();
3530     break;
3531   case Bytecodes::_instanceof:
3532     do_instanceof();
3533     break;
3534   case Bytecodes::_anewarray:
3535     do_newarray();
3536     break;
3537   case Bytecodes::_newarray:
3538     do_newarray((BasicType)iter().get_index());
3539     break;
3540   case Bytecodes::_multianewarray:
3541     do_multianewarray();
3542     break;
3543   case Bytecodes::_new:
3544     do_new();
3545     break;
3546 
3547   case Bytecodes::_jsr:
3548   case Bytecodes::_jsr_w:
3549     do_jsr();
3550     break;
3551 
3552   case Bytecodes::_ret:
3553     do_ret();
3554     break;
3555 
< prev index next >