< 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 "opto/subtypenode.hpp"

  45 #include "runtime/deoptimization.hpp"

  46 #include "runtime/sharedRuntime.hpp"
  47 
  48 #ifndef PRODUCT
  49 extern uint explicit_null_checks_inserted,
  50             explicit_null_checks_elided;
  51 #endif
  52 

















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





























































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

  74   }

  75 }
  76 




























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

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




























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






























































 105 }
 106 

























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

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







































































































































































































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

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



1501   }
1502 
1503   // Sanity check the probability value
1504   assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1505 
1506   bool taken_if_true = true;
1507   // Convert BoolTest to canonical form:
1508   if (!BoolTest(btest).is_canonical()) {
1509     btest         = BoolTest(btest).negate();
1510     taken_if_true = false;
1511     // prob is NOT updated here; it remains the probability of the taken
1512     // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1513   }
1514   assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1515 
1516   Node* tst0 = new BoolNode(c, btest);
1517   Node* tst = _gvn.transform(tst0);
1518   BoolTest::mask taken_btest   = BoolTest::illegal;
1519   BoolTest::mask untaken_btest = BoolTest::illegal;
1520 

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








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














































































































































































































































































































































































































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

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





1688   Node* val = c->in(1);
1689   Node* con = c->in(2);
1690   const Type* tcon = _gvn.type(con);
1691   const Type* tval = _gvn.type(val);
1692   bool have_con = tcon->singleton();
1693   if (tval->singleton()) {
1694     if (!have_con) {
1695       // Swap, so constant is in con.
1696       con  = val;
1697       tcon = tval;
1698       val  = c->in(2);
1699       tval = _gvn.type(val);
1700       btest = BoolTest(btest).commute();
1701       have_con = true;
1702     } else {
1703       // Do we have two constants?  Then leave well enough alone.
1704       have_con = false;
1705     }
1706   }
1707   if (!have_con) {                        // remaining adjustments need a con

1829   Node* obj = nullptr;
1830   const TypeOopPtr* cast_type = nullptr;
1831   // Insert a cast node with a narrowed type after a successful type check.
1832   if (match_type_check(_gvn, btest, con, tcon, val, tval,
1833                        &obj, &cast_type)) {
1834     assert(obj != nullptr && cast_type != nullptr, "missing type check info");
1835     const Type* obj_type = _gvn.type(obj);
1836     const TypeOopPtr* tboth = obj_type->join_speculative(cast_type)->isa_oopptr();
1837     if (tboth != nullptr && tboth != obj_type && tboth->higher_equal(obj_type)) {
1838       int obj_in_map = map()->find_edge(obj);
1839       JVMState* jvms = this->jvms();
1840       if (obj_in_map >= 0 &&
1841           (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1842         TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1843         const Type* tcc = ccast->as_Type()->type();
1844         assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1845         // Delay transform() call to allow recovery of pre-cast value
1846         // at the control merge.
1847         _gvn.set_type_bottom(ccast);
1848         record_for_igvn(ccast);



1849         // Here's the payoff.
1850         replace_in_map(obj, ccast);
1851       }
1852     }
1853   }
1854 
1855   int val_in_map = map()->find_edge(val);
1856   if (val_in_map < 0)  return;          // replace_in_map would be useless
1857   {
1858     JVMState* jvms = this->jvms();
1859     if (!(jvms->is_loc(val_in_map) ||
1860           jvms->is_stk(val_in_map)))
1861       return;                           // again, it would be useless
1862   }
1863 
1864   // Check for a comparison to a constant, and "know" that the compared
1865   // value is constrained on this path.
1866   assert(tcon->singleton(), "");
1867   ConstraintCastNode* ccast = nullptr;
1868   Node* cast = nullptr;

1932   if (c->Opcode() == Op_CmpP &&
1933       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1934       c->in(2)->is_Con()) {
1935     Node* load_klass = nullptr;
1936     Node* decode = nullptr;
1937     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1938       decode = c->in(1);
1939       load_klass = c->in(1)->in(1);
1940     } else {
1941       load_klass = c->in(1);
1942     }
1943     if (load_klass->in(2)->is_AddP()) {
1944       Node* addp = load_klass->in(2);
1945       Node* obj = addp->in(AddPNode::Address);
1946       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1947       if (obj_type->speculative_type_not_null() != nullptr) {
1948         ciKlass* k = obj_type->speculative_type();
1949         inc_sp(2);
1950         obj = maybe_cast_profiled_obj(obj, k);
1951         dec_sp(2);




1952         // Make the CmpP use the casted obj
1953         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1954         load_klass = load_klass->clone();
1955         load_klass->set_req(2, addp);
1956         load_klass = _gvn.transform(load_klass);
1957         if (decode != nullptr) {
1958           decode = decode->clone();
1959           decode->set_req(1, load_klass);
1960           load_klass = _gvn.transform(decode);
1961         }
1962         c = c->clone();
1963         c->set_req(1, load_klass);
1964         c = _gvn.transform(c);
1965       }
1966     }
1967   }
1968   return c;
1969 }
1970 
1971 //------------------------------do_one_bytecode--------------------------------

2674     b = _gvn.transform( new ConvI2DNode(a));
2675     push_pair(b);
2676     break;
2677 
2678   case Bytecodes::_iinc:        // Increment local
2679     i = iter().get_index();     // Get local index
2680     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2681     break;
2682 
2683   // Exit points of synchronized methods must have an unlock node
2684   case Bytecodes::_return:
2685     return_current(nullptr);
2686     break;
2687 
2688   case Bytecodes::_ireturn:
2689   case Bytecodes::_areturn:
2690   case Bytecodes::_freturn:
2691     return_current(pop());
2692     break;
2693   case Bytecodes::_lreturn:
2694     return_current(pop_pair());
2695     break;
2696   case Bytecodes::_dreturn:
2697     return_current(pop_pair());
2698     break;
2699 
2700   case Bytecodes::_athrow:
2701     // null exception oop throws null pointer exception
2702     null_check(peek());
2703     if (stopped())  return;
2704     // Hook the thrown exception directly to subsequent handlers.
2705     if (BailoutToInterpreterForThrows) {
2706       // Keep method interpreted from now on.
2707       uncommon_trap(Deoptimization::Reason_unhandled,
2708                     Deoptimization::Action_make_not_compilable);
2709       return;
2710     }
2711     if (env()->jvmti_can_post_on_exceptions()) {
2712       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2713       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2714     }
2715     // Here if either can_post_on_exceptions or should_post_on_exceptions is false

2729     // See if we can get some profile data and hand it off to the next block
2730     Block *target_block = block()->successor_for_bci(target_bci);
2731     if (target_block->pred_count() != 1)  break;
2732     ciMethodData* methodData = method()->method_data();
2733     if (!methodData->is_mature())  break;
2734     ciProfileData* data = methodData->bci_to_data(bci());
2735     assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch");
2736     int taken = ((ciJumpData*)data)->taken();
2737     taken = method()->scale_count(taken);
2738     target_block->set_count(taken);
2739     break;
2740   }
2741 
2742   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
2743   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2744   handle_if_null:
2745     // If this is a backwards branch in the bytecodes, add Safepoint
2746     maybe_add_safepoint(iter().get_dest());
2747     a = null();
2748     b = pop();
2749     if (!_gvn.type(b)->speculative_maybe_null() &&
2750         !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2751       inc_sp(1);
2752       Node* null_ctl = top();
2753       b = null_check_oop(b, &null_ctl, true, true, true);
2754       assert(null_ctl->is_top(), "no null control here");
2755       dec_sp(1);
2756     } else if (_gvn.type(b)->speculative_always_null() &&
2757                !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2758       inc_sp(1);
2759       b = null_assert(b);
2760       dec_sp(1);
2761     }
2762     c = _gvn.transform( new CmpPNode(b, a) );






2763     do_ifnull(btest, c);
2764     break;
2765 
2766   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2767   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2768   handle_if_acmp:
2769     // If this is a backwards branch in the bytecodes, add Safepoint
2770     maybe_add_safepoint(iter().get_dest());
2771     a = pop();
2772     b = pop();
2773     c = _gvn.transform( new CmpPNode(b, a) );
2774     c = optimize_cmp_with_klass(c);
2775     do_if(btest, c);
2776     break;
2777 
2778   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2779   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2780   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2781   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2782   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2783   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2784   handle_ifxx:
2785     // If this is a backwards branch in the bytecodes, add Safepoint
2786     maybe_add_safepoint(iter().get_dest());
2787     a = _gvn.intcon(0);
2788     b = pop();
2789     c = _gvn.transform( new CmpINode(b, a) );
2790     do_if(btest, c);
2791     break;
2792 
2793   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2794   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2795   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;

2810     break;
2811 
2812   case Bytecodes::_lookupswitch:
2813     do_lookupswitch();
2814     break;
2815 
2816   case Bytecodes::_invokestatic:
2817   case Bytecodes::_invokedynamic:
2818   case Bytecodes::_invokespecial:
2819   case Bytecodes::_invokevirtual:
2820   case Bytecodes::_invokeinterface:
2821     do_call();
2822     break;
2823   case Bytecodes::_checkcast:
2824     do_checkcast();
2825     break;
2826   case Bytecodes::_instanceof:
2827     do_instanceof();
2828     break;
2829   case Bytecodes::_anewarray:
2830     do_anewarray();
2831     break;
2832   case Bytecodes::_newarray:
2833     do_newarray((BasicType)iter().get_index());
2834     break;
2835   case Bytecodes::_multianewarray:
2836     do_multianewarray();
2837     break;
2838   case Bytecodes::_new:
2839     do_new();
2840     break;
2841 
2842   case Bytecodes::_jsr:
2843   case Bytecodes::_jsr_w:
2844     do_jsr();
2845     break;
2846 
2847   case Bytecodes::_ret:
2848     do_ret();
2849     break;
2850 

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

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

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

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


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











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











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



























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

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

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

2428 }
2429 
2430 void Parse::maybe_add_predicate_after_if(Block* path) {
2431   if (path->is_SEL_head() && path->preds_parsed() == 0) {
2432     // Add predicates at bci of if dominating the loop so traps can be
2433     // recorded on the if's profile data
2434     int bc_depth = repush_if_args();
2435     add_parse_predicates();
2436     dec_sp(bc_depth);
2437     path->set_has_predicates();
2438   }
2439 }
2440 
2441 
2442 //----------------------------adjust_map_after_if------------------------------
2443 // Adjust the JVM state to reflect the result of taking this path.
2444 // Basically, it means inspecting the CmpNode controlling this
2445 // branch, seeing how it constrains a tested value, and then
2446 // deciding if it's worth our while to encode this constraint
2447 // as graph nodes in the current abstract interpretation map.
2448 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path, bool can_trap) {
2449   if (!c->is_Cmp()) {
2450     maybe_add_predicate_after_if(path);
2451     return;
2452   }
2453 
2454   if (stopped() || btest == BoolTest::illegal) {
2455     return;                             // nothing to do
2456   }
2457 
2458   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2459 
2460   if (can_trap && path_is_suitable_for_uncommon_trap(prob)) {
2461     repush_if_args();
2462     Node* call = uncommon_trap(Deoptimization::Reason_unstable_if,
2463                   Deoptimization::Action_reinterpret,
2464                   nullptr,
2465                   (is_fallthrough ? "taken always" : "taken never"));
2466 
2467     if (call != nullptr) {
2468       C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path));
2469     }
2470     return;
2471   }
2472 
2473   if (c->is_FlatArrayCheck()) {
2474     maybe_add_predicate_after_if(path);
2475     return;
2476   }
2477 
2478   Node* val = c->in(1);
2479   Node* con = c->in(2);
2480   const Type* tcon = _gvn.type(con);
2481   const Type* tval = _gvn.type(val);
2482   bool have_con = tcon->singleton();
2483   if (tval->singleton()) {
2484     if (!have_con) {
2485       // Swap, so constant is in con.
2486       con  = val;
2487       tcon = tval;
2488       val  = c->in(2);
2489       tval = _gvn.type(val);
2490       btest = BoolTest(btest).commute();
2491       have_con = true;
2492     } else {
2493       // Do we have two constants?  Then leave well enough alone.
2494       have_con = false;
2495     }
2496   }
2497   if (!have_con) {                        // remaining adjustments need a con

2619   Node* obj = nullptr;
2620   const TypeOopPtr* cast_type = nullptr;
2621   // Insert a cast node with a narrowed type after a successful type check.
2622   if (match_type_check(_gvn, btest, con, tcon, val, tval,
2623                        &obj, &cast_type)) {
2624     assert(obj != nullptr && cast_type != nullptr, "missing type check info");
2625     const Type* obj_type = _gvn.type(obj);
2626     const TypeOopPtr* tboth = obj_type->join_speculative(cast_type)->isa_oopptr();
2627     if (tboth != nullptr && tboth != obj_type && tboth->higher_equal(obj_type)) {
2628       int obj_in_map = map()->find_edge(obj);
2629       JVMState* jvms = this->jvms();
2630       if (obj_in_map >= 0 &&
2631           (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2632         TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2633         const Type* tcc = ccast->as_Type()->type();
2634         assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2635         // Delay transform() call to allow recovery of pre-cast value
2636         // at the control merge.
2637         _gvn.set_type_bottom(ccast);
2638         record_for_igvn(ccast);
2639         if (tboth->is_inlinetypeptr()) {
2640           ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass());
2641         }
2642         // Here's the payoff.
2643         replace_in_map(obj, ccast);
2644       }
2645     }
2646   }
2647 
2648   int val_in_map = map()->find_edge(val);
2649   if (val_in_map < 0)  return;          // replace_in_map would be useless
2650   {
2651     JVMState* jvms = this->jvms();
2652     if (!(jvms->is_loc(val_in_map) ||
2653           jvms->is_stk(val_in_map)))
2654       return;                           // again, it would be useless
2655   }
2656 
2657   // Check for a comparison to a constant, and "know" that the compared
2658   // value is constrained on this path.
2659   assert(tcon->singleton(), "");
2660   ConstraintCastNode* ccast = nullptr;
2661   Node* cast = nullptr;

2725   if (c->Opcode() == Op_CmpP &&
2726       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2727       c->in(2)->is_Con()) {
2728     Node* load_klass = nullptr;
2729     Node* decode = nullptr;
2730     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2731       decode = c->in(1);
2732       load_klass = c->in(1)->in(1);
2733     } else {
2734       load_klass = c->in(1);
2735     }
2736     if (load_klass->in(2)->is_AddP()) {
2737       Node* addp = load_klass->in(2);
2738       Node* obj = addp->in(AddPNode::Address);
2739       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2740       if (obj_type->speculative_type_not_null() != nullptr) {
2741         ciKlass* k = obj_type->speculative_type();
2742         inc_sp(2);
2743         obj = maybe_cast_profiled_obj(obj, k);
2744         dec_sp(2);
2745         if (obj->is_InlineType()) {
2746           assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated");
2747           obj = obj->as_InlineType()->get_oop();
2748         }
2749         // Make the CmpP use the casted obj
2750         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2751         load_klass = load_klass->clone();
2752         load_klass->set_req(2, addp);
2753         load_klass = _gvn.transform(load_klass);
2754         if (decode != nullptr) {
2755           decode = decode->clone();
2756           decode->set_req(1, load_klass);
2757           load_klass = _gvn.transform(decode);
2758         }
2759         c = c->clone();
2760         c->set_req(1, load_klass);
2761         c = _gvn.transform(c);
2762       }
2763     }
2764   }
2765   return c;
2766 }
2767 
2768 //------------------------------do_one_bytecode--------------------------------

3471     b = _gvn.transform( new ConvI2DNode(a));
3472     push_pair(b);
3473     break;
3474 
3475   case Bytecodes::_iinc:        // Increment local
3476     i = iter().get_index();     // Get local index
3477     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
3478     break;
3479 
3480   // Exit points of synchronized methods must have an unlock node
3481   case Bytecodes::_return:
3482     return_current(nullptr);
3483     break;
3484 
3485   case Bytecodes::_ireturn:
3486   case Bytecodes::_areturn:
3487   case Bytecodes::_freturn:
3488     return_current(pop());
3489     break;
3490   case Bytecodes::_lreturn:


3491   case Bytecodes::_dreturn:
3492     return_current(pop_pair());
3493     break;
3494 
3495   case Bytecodes::_athrow:
3496     // null exception oop throws null pointer exception
3497     null_check(peek());
3498     if (stopped())  return;
3499     // Hook the thrown exception directly to subsequent handlers.
3500     if (BailoutToInterpreterForThrows) {
3501       // Keep method interpreted from now on.
3502       uncommon_trap(Deoptimization::Reason_unhandled,
3503                     Deoptimization::Action_make_not_compilable);
3504       return;
3505     }
3506     if (env()->jvmti_can_post_on_exceptions()) {
3507       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
3508       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
3509     }
3510     // Here if either can_post_on_exceptions or should_post_on_exceptions is false

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


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

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