1 /*
   2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compileLog.hpp"
  27 #include "interpreter/linkResolver.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/method.hpp"
  30 #include "opto/addnode.hpp"
  31 #include "opto/c2compiler.hpp"
  32 #include "opto/castnode.hpp"
  33 #include "opto/idealGraphPrinter.hpp"
  34 #include "opto/locknode.hpp"
  35 #include "opto/memnode.hpp"
  36 #include "opto/opaquenode.hpp"
  37 #include "opto/parse.hpp"
  38 #include "opto/rootnode.hpp"
  39 #include "opto/runtime.hpp"
  40 #include "opto/type.hpp"
  41 #include "runtime/handles.inline.hpp"
  42 #include "runtime/safepointMechanism.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "utilities/bitMap.inline.hpp"
  45 #include "utilities/copy.hpp"
  46 
  47 // Static array so we can figure out which bytecodes stop us from compiling
  48 // the most. Some of the non-static variables are needed in bytecodeInfo.cpp
  49 // and eventually should be encapsulated in a proper class (gri 8/18/98).
  50 
  51 #ifndef PRODUCT
  52 uint nodes_created             = 0;
  53 uint methods_parsed            = 0;
  54 uint methods_seen              = 0;
  55 uint blocks_parsed             = 0;
  56 uint blocks_seen               = 0;
  57 
  58 uint explicit_null_checks_inserted = 0;
  59 uint explicit_null_checks_elided   = 0;
  60 uint all_null_checks_found         = 0;
  61 uint implicit_null_checks          = 0;
  62 
  63 bool Parse::BytecodeParseHistogram::_initialized = false;
  64 uint Parse::BytecodeParseHistogram::_bytecodes_parsed [Bytecodes::number_of_codes];
  65 uint Parse::BytecodeParseHistogram::_nodes_constructed[Bytecodes::number_of_codes];
  66 uint Parse::BytecodeParseHistogram::_nodes_transformed[Bytecodes::number_of_codes];
  67 uint Parse::BytecodeParseHistogram::_new_values       [Bytecodes::number_of_codes];
  68 
  69 //------------------------------print_statistics-------------------------------
  70 void Parse::print_statistics() {
  71   tty->print_cr("--- Compiler Statistics ---");
  72   tty->print("Methods seen: %u  Methods parsed: %u", methods_seen, methods_parsed);
  73   tty->print("  Nodes created: %u", nodes_created);
  74   tty->cr();
  75   if (methods_seen != methods_parsed) {
  76     tty->print_cr("Reasons for parse failures (NOT cumulative):");
  77   }
  78   tty->print_cr("Blocks parsed: %u  Blocks seen: %u", blocks_parsed, blocks_seen);
  79 
  80   if (explicit_null_checks_inserted) {
  81     tty->print_cr("%u original null checks - %u elided (%2u%%); optimizer leaves %u,",
  82                   explicit_null_checks_inserted, explicit_null_checks_elided,
  83                   (100*explicit_null_checks_elided)/explicit_null_checks_inserted,
  84                   all_null_checks_found);
  85   }
  86   if (all_null_checks_found) {
  87     tty->print_cr("%u made implicit (%2u%%)", implicit_null_checks,
  88                   (100*implicit_null_checks)/all_null_checks_found);
  89   }
  90   if (SharedRuntime::_implicit_null_throws) {
  91     tty->print_cr("%u implicit null exceptions at runtime",
  92                   SharedRuntime::_implicit_null_throws);
  93   }
  94 
  95   if (PrintParseStatistics && BytecodeParseHistogram::initialized()) {
  96     BytecodeParseHistogram::print();
  97   }
  98 
  99   if (DoPartialEscapeAnalysis) {
 100     printPeaStatistics();
 101   }
 102 }
 103 #endif
 104 
 105 //------------------------------ON STACK REPLACEMENT---------------------------
 106 
 107 // Construct a node which can be used to get incoming state for
 108 // on stack replacement.
 109 Node *Parse::fetch_interpreter_state(int index,
 110                                      BasicType bt,
 111                                      Node *local_addrs,
 112                                      Node *local_addrs_base) {
 113   Node *mem = memory(Compile::AliasIdxRaw);
 114   Node *adr = basic_plus_adr( local_addrs_base, local_addrs, -index*wordSize );
 115   Node *ctl = control();
 116 
 117   // Very similar to LoadNode::make, except we handle un-aligned longs and
 118   // doubles on Sparc.  Intel can handle them just fine directly.
 119   Node *l = nullptr;
 120   switch (bt) {                // Signature is flattened
 121   case T_INT:     l = new LoadINode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeInt::INT,        MemNode::unordered); break;
 122   case T_FLOAT:   l = new LoadFNode(ctl, mem, adr, TypeRawPtr::BOTTOM, Type::FLOAT,         MemNode::unordered); break;
 123   case T_ADDRESS: l = new LoadPNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM,  MemNode::unordered); break;
 124   case T_OBJECT:  l = new LoadPNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeInstPtr::BOTTOM, MemNode::unordered); break;
 125   case T_LONG:
 126   case T_DOUBLE: {
 127     // Since arguments are in reverse order, the argument address 'adr'
 128     // refers to the back half of the long/double.  Recompute adr.
 129     adr = basic_plus_adr(local_addrs_base, local_addrs, -(index+1)*wordSize);
 130     if (Matcher::misaligned_doubles_ok) {
 131       l = (bt == T_DOUBLE)
 132         ? (Node*)new LoadDNode(ctl, mem, adr, TypeRawPtr::BOTTOM, Type::DOUBLE, MemNode::unordered)
 133         : (Node*)new LoadLNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeLong::LONG, MemNode::unordered);
 134     } else {
 135       l = (bt == T_DOUBLE)
 136         ? (Node*)new LoadD_unalignedNode(ctl, mem, adr, TypeRawPtr::BOTTOM, MemNode::unordered)
 137         : (Node*)new LoadL_unalignedNode(ctl, mem, adr, TypeRawPtr::BOTTOM, MemNode::unordered);
 138     }
 139     break;
 140   }
 141   default: ShouldNotReachHere();
 142   }
 143   return _gvn.transform(l);
 144 }
 145 
 146 // Helper routine to prevent the interpreter from handing
 147 // unexpected typestate to an OSR method.
 148 // The Node l is a value newly dug out of the interpreter frame.
 149 // The type is the type predicted by ciTypeFlow.  Note that it is
 150 // not a general type, but can only come from Type::get_typeflow_type.
 151 // The safepoint is a map which will feed an uncommon trap.
 152 Node* Parse::check_interpreter_type(Node* l, const Type* type,
 153                                     SafePointNode* &bad_type_exit) {
 154 
 155   const TypeOopPtr* tp = type->isa_oopptr();
 156 
 157   // TypeFlow may assert null-ness if a type appears unloaded.
 158   if (type == TypePtr::NULL_PTR ||
 159       (tp != nullptr && !tp->is_loaded())) {
 160     // Value must be null, not a real oop.
 161     Node* chk = _gvn.transform( new CmpPNode(l, null()) );
 162     Node* tst = _gvn.transform( new BoolNode(chk, BoolTest::eq) );
 163     IfNode* iff = create_and_map_if(control(), tst, PROB_MAX, COUNT_UNKNOWN);
 164     set_control(_gvn.transform( new IfTrueNode(iff) ));
 165     Node* bad_type = _gvn.transform( new IfFalseNode(iff) );
 166     bad_type_exit->control()->add_req(bad_type);
 167     l = null();
 168   }
 169 
 170   // Typeflow can also cut off paths from the CFG, based on
 171   // types which appear unloaded, or call sites which appear unlinked.
 172   // When paths are cut off, values at later merge points can rise
 173   // toward more specific classes.  Make sure these specific classes
 174   // are still in effect.
 175   if (tp != nullptr && !tp->is_same_java_type_as(TypeInstPtr::BOTTOM)) {
 176     // TypeFlow asserted a specific object type.  Value must have that type.
 177     Node* bad_type_ctrl = nullptr;
 178     l = gen_checkcast(l, makecon(tp->as_klass_type()->cast_to_exactness(true)), &bad_type_ctrl);
 179     bad_type_exit->control()->add_req(bad_type_ctrl);
 180   }
 181 
 182   assert(_gvn.type(l)->higher_equal(type), "must constrain OSR typestate");
 183   return l;
 184 }
 185 
 186 // Helper routine which sets up elements of the initial parser map when
 187 // performing a parse for on stack replacement.  Add values into map.
 188 // The only parameter contains the address of a interpreter arguments.
 189 void Parse::load_interpreter_state(Node* osr_buf) {
 190   int index;
 191   int max_locals = jvms()->loc_size();
 192   int max_stack  = jvms()->stk_size();
 193 
 194 
 195   // Mismatch between method and jvms can occur since map briefly held
 196   // an OSR entry state (which takes up one RawPtr word).
 197   assert(max_locals == method()->max_locals(), "sanity");
 198   assert(max_stack  >= method()->max_stack(),  "sanity");
 199   assert((int)jvms()->endoff() == TypeFunc::Parms + max_locals + max_stack, "sanity");
 200   assert((int)jvms()->endoff() == (int)map()->req(), "sanity");
 201 
 202   // Find the start block.
 203   Block* osr_block = start_block();
 204   assert(osr_block->start() == osr_bci(), "sanity");
 205 
 206   // Set initial BCI.
 207   set_parse_bci(osr_block->start());
 208 
 209   // Set initial stack depth.
 210   set_sp(osr_block->start_sp());
 211 
 212   // Check bailouts.  We currently do not perform on stack replacement
 213   // of loops in catch blocks or loops which branch with a non-empty stack.
 214   if (sp() != 0) {
 215     C->record_method_not_compilable("OSR starts with non-empty stack");
 216     return;
 217   }
 218   // Do not OSR inside finally clauses:
 219   if (osr_block->has_trap_at(osr_block->start())) {
 220     assert(false, "OSR starts with an immediate trap");
 221     C->record_method_not_compilable("OSR starts with an immediate trap");
 222     return;
 223   }
 224 
 225   // Commute monitors from interpreter frame to compiler frame.
 226   assert(jvms()->monitor_depth() == 0, "should be no active locks at beginning of osr");
 227   int mcnt = osr_block->flow()->monitor_count();
 228   Node *monitors_addr = basic_plus_adr(osr_buf, osr_buf, (max_locals+mcnt*2-1)*wordSize);
 229   for (index = 0; index < mcnt; index++) {
 230     // Make a BoxLockNode for the monitor.
 231     Node *box = _gvn.transform(new BoxLockNode(next_monitor()));
 232 
 233 
 234     // Displaced headers and locked objects are interleaved in the
 235     // temp OSR buffer.  We only copy the locked objects out here.
 236     // Fetch the locked object from the OSR temp buffer and copy to our fastlock node.
 237     Node *lock_object = fetch_interpreter_state(index*2, T_OBJECT, monitors_addr, osr_buf);
 238     // Try and copy the displaced header to the BoxNode
 239     Node *displaced_hdr = fetch_interpreter_state((index*2) + 1, T_ADDRESS, monitors_addr, osr_buf);
 240 
 241 
 242     store_to_memory(control(), box, displaced_hdr, T_ADDRESS, Compile::AliasIdxRaw, MemNode::unordered);
 243 
 244     // Build a bogus FastLockNode (no code will be generated) and push the
 245     // monitor into our debug info.
 246     const FastLockNode *flock = _gvn.transform(new FastLockNode( 0, lock_object, box ))->as_FastLock();
 247     map()->push_monitor(flock);
 248 
 249     // If the lock is our method synchronization lock, tuck it away in
 250     // _sync_lock for return and rethrow exit paths.
 251     if (index == 0 && method()->is_synchronized()) {
 252       _synch_lock = flock;
 253     }
 254   }
 255 
 256   // Use the raw liveness computation to make sure that unexpected
 257   // values don't propagate into the OSR frame.
 258   MethodLivenessResult live_locals = method()->liveness_at_bci(osr_bci());
 259   if (!live_locals.is_valid()) {
 260     // Degenerate or breakpointed method.
 261     assert(false, "OSR in empty or breakpointed method");
 262     C->record_method_not_compilable("OSR in empty or breakpointed method");
 263     return;
 264   }
 265 
 266   // Extract the needed locals from the interpreter frame.
 267   Node *locals_addr = basic_plus_adr(osr_buf, osr_buf, (max_locals-1)*wordSize);
 268 
 269   // find all the locals that the interpreter thinks contain live oops
 270   const ResourceBitMap live_oops = method()->live_local_oops_at_bci(osr_bci());
 271   for (index = 0; index < max_locals; index++) {
 272 
 273     if (!live_locals.at(index)) {
 274       continue;
 275     }
 276 
 277     const Type *type = osr_block->local_type_at(index);
 278 
 279     if (type->isa_oopptr() != nullptr) {
 280 
 281       // 6403625: Verify that the interpreter oopMap thinks that the oop is live
 282       // else we might load a stale oop if the MethodLiveness disagrees with the
 283       // result of the interpreter. If the interpreter says it is dead we agree
 284       // by making the value go to top.
 285       //
 286 
 287       if (!live_oops.at(index)) {
 288         if (C->log() != nullptr) {
 289           C->log()->elem("OSR_mismatch local_index='%d'",index);
 290         }
 291         set_local(index, null());
 292         // and ignore it for the loads
 293         continue;
 294       }
 295     }
 296 
 297     // Filter out TOP, HALF, and BOTTOM.  (Cf. ensure_phi.)
 298     if (type == Type::TOP || type == Type::HALF) {
 299       continue;
 300     }
 301     // If the type falls to bottom, then this must be a local that
 302     // is mixing ints and oops or some such.  Forcing it to top
 303     // makes it go dead.
 304     if (type == Type::BOTTOM) {
 305       continue;
 306     }
 307     // Construct code to access the appropriate local.
 308     BasicType bt = type->basic_type();
 309     if (type == TypePtr::NULL_PTR) {
 310       // Ptr types are mixed together with T_ADDRESS but null is
 311       // really for T_OBJECT types so correct it.
 312       bt = T_OBJECT;
 313     }
 314     Node *value = fetch_interpreter_state(index, bt, locals_addr, osr_buf);
 315     set_local(index, value);
 316   }
 317 
 318   // Extract the needed stack entries from the interpreter frame.
 319   for (index = 0; index < sp(); index++) {
 320     const Type *type = osr_block->stack_type_at(index);
 321     if (type != Type::TOP) {
 322       // Currently the compiler bails out when attempting to on stack replace
 323       // at a bci with a non-empty stack.  We should not reach here.
 324       ShouldNotReachHere();
 325     }
 326   }
 327 
 328   // End the OSR migration
 329   make_runtime_call(RC_LEAF, OptoRuntime::osr_end_Type(),
 330                     CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end),
 331                     "OSR_migration_end", TypeRawPtr::BOTTOM,
 332                     osr_buf);
 333 
 334   // Now that the interpreter state is loaded, make sure it will match
 335   // at execution time what the compiler is expecting now:
 336   SafePointNode* bad_type_exit = clone_map();
 337   bad_type_exit->set_control(new RegionNode(1));
 338 
 339   assert(osr_block->flow()->jsrs()->size() == 0, "should be no jsrs live at osr point");
 340   for (index = 0; index < max_locals; index++) {
 341     if (stopped())  break;
 342     Node* l = local(index);
 343     if (l->is_top())  continue;  // nothing here
 344     const Type *type = osr_block->local_type_at(index);
 345     if (type->isa_oopptr() != nullptr) {
 346       if (!live_oops.at(index)) {
 347         // skip type check for dead oops
 348         continue;
 349       }
 350     }
 351     if (osr_block->flow()->local_type_at(index)->is_return_address()) {
 352       // In our current system it's illegal for jsr addresses to be
 353       // live into an OSR entry point because the compiler performs
 354       // inlining of jsrs.  ciTypeFlow has a bailout that detect this
 355       // case and aborts the compile if addresses are live into an OSR
 356       // entry point.  Because of that we can assume that any address
 357       // locals at the OSR entry point are dead.  Method liveness
 358       // isn't precise enough to figure out that they are dead in all
 359       // cases so simply skip checking address locals all
 360       // together. Any type check is guaranteed to fail since the
 361       // interpreter type is the result of a load which might have any
 362       // value and the expected type is a constant.
 363       continue;
 364     }
 365     set_local(index, check_interpreter_type(l, type, bad_type_exit));
 366   }
 367 
 368   for (index = 0; index < sp(); index++) {
 369     if (stopped())  break;
 370     Node* l = stack(index);
 371     if (l->is_top())  continue;  // nothing here
 372     const Type *type = osr_block->stack_type_at(index);
 373     set_stack(index, check_interpreter_type(l, type, bad_type_exit));
 374   }
 375 
 376   if (bad_type_exit->control()->req() > 1) {
 377     // Build an uncommon trap here, if any inputs can be unexpected.
 378     bad_type_exit->set_control(_gvn.transform( bad_type_exit->control() ));
 379     record_for_igvn(bad_type_exit->control());
 380     SafePointNode* types_are_good = map();
 381     set_map(bad_type_exit);
 382     // The unexpected type happens because a new edge is active
 383     // in the CFG, which typeflow had previously ignored.
 384     // E.g., Object x = coldAtFirst() && notReached()? "str": new Integer(123).
 385     // This x will be typed as Integer if notReached is not yet linked.
 386     // It could also happen due to a problem in ciTypeFlow analysis.
 387     uncommon_trap(Deoptimization::Reason_constraint,
 388                   Deoptimization::Action_reinterpret);
 389     set_map(types_are_good);
 390   }
 391 }
 392 
 393 //------------------------------Parse------------------------------------------
 394 // Main parser constructor.
 395 Parse::Parse(JVMState* caller, ciMethod* parse_method, float expected_uses, PEAState* caller_state)
 396   : _exits(caller)
 397 {
 398   // Init some variables
 399   _caller = caller;
 400   _caller_state = caller_state;
 401   _method = parse_method;
 402   _expected_uses = expected_uses;
 403   _depth = 1 + (caller->has_method() ? caller->depth() : 0);
 404   _wrote_final = false;
 405   _wrote_volatile = false;
 406   _wrote_stable = false;
 407   _wrote_fields = false;
 408   _alloc_with_final = nullptr;
 409   _block = nullptr;
 410   _first_return = 0;
 411   _replaced_nodes_for_exceptions = false;
 412   _new_idx = C->unique();
 413   DEBUG_ONLY(_entry_bci = UnknownBci);
 414   DEBUG_ONLY(_block_count = -1);
 415   DEBUG_ONLY(_blocks = (Block*)-1);
 416 #ifndef PRODUCT
 417   if (PrintCompilation || PrintOpto) {
 418     // Make sure I have an inline tree, so I can print messages about it.
 419     InlineTree::find_subtree_from_root(C->ilt(), caller, parse_method);
 420   }
 421   _max_switch_depth = 0;
 422   _est_switch_depth = 0;
 423 
 424   if (TraceOptoParse) {
 425     tty->print_raw("Parsing method ");
 426     parse_method->print_name(tty);
 427     tty->print_cr(" {");
 428   }
 429 #endif
 430 
 431   if (parse_method->has_reserved_stack_access()) {
 432     C->set_has_reserved_stack_access(true);
 433   }
 434 
 435   if (parse_method->is_synchronized() || parse_method->has_monitor_bytecodes()) {
 436     C->set_has_monitors(true);
 437   }
 438 
 439   _iter.reset_to_method(method());
 440   C->set_has_loops(C->has_loops() || method()->has_loops());
 441 
 442   if (_expected_uses <= 0) {
 443     _prof_factor = 1;
 444   } else {
 445     float prof_total = parse_method->interpreter_invocation_count();
 446     if (prof_total <= _expected_uses) {
 447       _prof_factor = 1;
 448     } else {
 449       _prof_factor = _expected_uses / prof_total;
 450     }
 451   }
 452 
 453   CompileLog* log = C->log();
 454   if (log != nullptr) {
 455     log->begin_head("parse method='%d' uses='%f'",
 456                     log->identify(parse_method), expected_uses);
 457     if (depth() == 1 && C->is_osr_compilation()) {
 458       log->print(" osr_bci='%d'", C->entry_bci());
 459     }
 460     log->stamp();
 461     log->end_head();
 462   }
 463 
 464   // Accumulate deoptimization counts.
 465   // (The range_check and store_check counts are checked elsewhere.)
 466   ciMethodData* md = method()->method_data();
 467   for (uint reason = 0; reason < md->trap_reason_limit(); reason++) {
 468     uint md_count = md->trap_count(reason);
 469     if (md_count != 0) {
 470       if (md_count >= md->trap_count_limit()) {
 471         md_count = md->trap_count_limit() + md->overflow_trap_count();
 472       }
 473       uint total_count = C->trap_count(reason);
 474       uint old_count   = total_count;
 475       total_count += md_count;
 476       // Saturate the add if it overflows.
 477       if (total_count < old_count || total_count < md_count)
 478         total_count = (uint)-1;
 479       C->set_trap_count(reason, total_count);
 480       if (log != nullptr)
 481         log->elem("observe trap='%s' count='%d' total='%d'",
 482                   Deoptimization::trap_reason_name(reason),
 483                   md_count, total_count);
 484     }
 485   }
 486   // Accumulate total sum of decompilations, also.
 487   C->set_decompile_count(C->decompile_count() + md->decompile_count());
 488 
 489   if (log != nullptr && method()->has_exception_handlers()) {
 490     log->elem("observe that='has_exception_handlers'");
 491   }
 492 
 493   assert(InlineTree::check_can_parse(method()) == nullptr, "Can not parse this method, cutout earlier");
 494   assert(method()->has_balanced_monitors(), "Can not parse unbalanced monitors, cutout earlier");
 495 
 496   // Always register dependence if JVMTI is enabled, because
 497   // either breakpoint setting or hotswapping of methods may
 498   // cause deoptimization.
 499   if (C->env()->jvmti_can_hotswap_or_post_breakpoint()) {
 500     C->dependencies()->assert_evol_method(method());
 501   }
 502 
 503   NOT_PRODUCT(methods_seen++);
 504 
 505   // Do some special top-level things.
 506   if (depth() == 1 && C->is_osr_compilation()) {
 507     _tf = C->tf();     // the OSR entry type is different
 508     _entry_bci = C->entry_bci();
 509     _flow = method()->get_osr_flow_analysis(osr_bci());
 510   } else {
 511     _tf = TypeFunc::make(method());
 512     _entry_bci = InvocationEntryBci;
 513     _flow = method()->get_flow_analysis();
 514   }
 515 
 516   if (_flow->failing()) {
 517     assert(false, "type flow analysis failed during parsing");
 518     C->record_method_not_compilable(_flow->failure_reason());
 519 #ifndef PRODUCT
 520       if (PrintOpto && (Verbose || WizardMode)) {
 521         if (is_osr_parse()) {
 522           tty->print_cr("OSR @%d type flow bailout: %s", _entry_bci, _flow->failure_reason());
 523         } else {
 524           tty->print_cr("type flow bailout: %s", _flow->failure_reason());
 525         }
 526         if (Verbose) {
 527           method()->print();
 528           method()->print_codes();
 529           _flow->print();
 530         }
 531       }
 532 #endif
 533   }
 534 
 535 #ifdef ASSERT
 536   if (depth() == 1) {
 537     assert(C->is_osr_compilation() == this->is_osr_parse(), "OSR in sync");
 538   } else {
 539     assert(!this->is_osr_parse(), "no recursive OSR");
 540   }
 541 #endif
 542 
 543 #ifndef PRODUCT
 544   // Dump CFG in RPO order before Parsing.
 545   if (Verbose && !CITraceTypeFlow) {
 546     _flow->rpo_print_on(tty);
 547   }
 548 
 549   if (_flow->has_irreducible_entry()) {
 550     C->set_parsed_irreducible_loop(true);
 551   }
 552 
 553   methods_parsed++;
 554   // add method size here to guarantee that inlined methods are added too
 555   if (CITime)
 556     _total_bytes_compiled += method()->code_size();
 557 
 558   show_parse_info();
 559 #endif
 560 
 561   if (failing()) {
 562     if (log)  log->done("parse");
 563     return;
 564   }
 565 
 566   gvn().transform(top());
 567 
 568   // Import the results of the ciTypeFlow.
 569   init_blocks();
 570 
 571   // Merge point for all normal exits
 572   build_exits();
 573 
 574   // Setup the initial JVM state map.
 575   SafePointNode* entry_map = create_entry_map();
 576 
 577   // Check for bailouts during map initialization
 578   if (failing() || entry_map == nullptr) {
 579     if (log)  log->done("parse");
 580     return;
 581   }
 582 
 583   Node_Notes* caller_nn = C->default_node_notes();
 584   // Collect debug info for inlined calls unless -XX:-DebugInlinedCalls.
 585   if (DebugInlinedCalls || depth() == 1) {
 586     C->set_default_node_notes(make_node_notes(caller_nn));
 587   }
 588 
 589   if (is_osr_parse()) {
 590     Node* osr_buf = entry_map->in(TypeFunc::Parms+0);
 591     entry_map->set_req(TypeFunc::Parms+0, top());
 592     set_map(entry_map);
 593     load_interpreter_state(osr_buf);
 594   } else {
 595     set_map(entry_map);
 596     do_method_entry();
 597   }
 598 
 599   if (depth() == 1 && !failing()) {
 600     if (C->clinit_barrier_on_entry()) {
 601       // Add check to deoptimize the nmethod once the holder class is fully initialized
 602       clinit_deopt();
 603     }
 604 
 605     // Add check to deoptimize the nmethod if RTM state was changed
 606     rtm_deopt();
 607   }
 608 
 609   // Check for bailouts during method entry or RTM state check setup.
 610   if (failing()) {
 611     if (log)  log->done("parse");
 612     C->set_default_node_notes(caller_nn);
 613     return;
 614   }
 615 
 616   entry_map = map();  // capture any changes performed by method setup code
 617   assert(jvms()->endoff() == map()->req(), "map matches JVMS layout");
 618 
 619   // We begin parsing as if we have just encountered a jump to the
 620   // method entry.
 621   Block* entry_block = start_block();
 622   assert(entry_block->start() == (is_osr_parse() ? osr_bci() : 0), "");
 623   set_map_clone(entry_map);
 624 
 625   merge_common(entry_block, entry_block->next_path_num());
 626 #ifndef PRODUCT
 627   BytecodeParseHistogram *parse_histogram_obj = new (C->env()->arena()) BytecodeParseHistogram(this, C);
 628   set_parse_histogram( parse_histogram_obj );
 629 #endif
 630 
 631   // Parse all the basic blocks.
 632   do_all_blocks();
 633 
 634   // Check for bailouts during conversion to graph
 635   if (failing()) {
 636     if (log)  log->done("parse");
 637     return;
 638   }
 639 
 640   // Fix up all exiting control flow.
 641   set_map(entry_map);
 642   do_exits();
 643 
 644   // Only reset this now, to make sure that debug information emitted
 645   // for exiting control flow still refers to the inlined method.
 646   C->set_default_node_notes(caller_nn);
 647 
 648   if (log)  log->done("parse nodes='%d' live='%d' memory='" SIZE_FORMAT "'",
 649                       C->unique(), C->live_nodes(), C->node_arena()->used());
 650 }
 651 
 652 #ifndef PRODUCT
 653 Parse::~Parse() {
 654   if (TraceOptoParse) {
 655     tty->print("} // ");
 656     method()->print_short_name(tty);
 657     tty->cr();
 658   }
 659 
 660   if (DoPartialEscapeAnalysis && PEAVerbose) {
 661     PEAState& as = _exits.jvms()->alloc_state();
 662     auto objs = PEA()->all_objects();
 663     for (int i = 0; i < objs.length(); ++i) {
 664       ObjID obj = objs.at(i);
 665 
 666       if (as.contains(obj)) {
 667         ObjectState* os = as.get_object_state(obj);
 668         tty->print("%4d | Obj%d\t", i, obj->_idx);
 669 
 670         if (os->is_virtual()) {
 671           tty->print_cr("V");
 672         } else {
 673           tty->print_cr("M");
 674         }
 675       }
 676     }
 677   }
 678 }
 679 #endif
 680 //---------------------------do_all_blocks-------------------------------------
 681 void Parse::do_all_blocks() {
 682   bool has_irreducible = flow()->has_irreducible_entry();
 683 
 684   // Walk over all blocks in Reverse Post-Order.
 685   while (true) {
 686     bool progress = false;
 687     for (int rpo = 0; rpo < block_count(); rpo++) {
 688       Block* block = rpo_at(rpo);
 689 
 690       if (block->is_parsed()) continue;
 691 
 692       if (!block->is_merged()) {
 693         // Dead block, no state reaches this block
 694         continue;
 695       }
 696 
 697       // Prepare to parse this block.
 698       load_state_from(block);
 699 
 700       if (stopped()) {
 701         // Block is dead.
 702         continue;
 703       }
 704 
 705       NOT_PRODUCT(blocks_parsed++);
 706 
 707       progress = true;
 708       if (block->is_loop_head() || block->is_handler() || (has_irreducible && !block->is_ready())) {
 709         // mark live objects 'Escaped' in map before mounting phi nodes.
 710         if (DoPartialEscapeAnalysis && block->is_loop_head()) {
 711           PEAState& as = jvms()->alloc_state();
 712           as.mark_all_live_objects_escaped(PEA(), map());
 713         }
 714         // Not all preds have been parsed.  We must build phis everywhere.
 715         // (Note that dead locals do not get phis built, ever.)
 716         ensure_phis_everywhere();
 717 
 718         if (block->is_SEL_head()) {
 719           // Add predicate to single entry (not irreducible) loop head.
 720           assert(!block->has_merged_backedge(), "only entry paths should be merged for now");
 721           // Predicates may have been added after a dominating if
 722           if (!block->has_predicates()) {
 723             // Need correct bci for predicate.
 724             // It is fine to set it here since do_one_block() will set it anyway.
 725             set_parse_bci(block->start());
 726             add_parse_predicates();
 727           }
 728           // Add new region for back branches.
 729           int edges = block->pred_count() - block->preds_parsed() + 1; // +1 for original region
 730           RegionNode *r = new RegionNode(edges+1);
 731           _gvn.set_type(r, Type::CONTROL);
 732           record_for_igvn(r);
 733           r->init_req(edges, control());
 734           set_control(r);
 735           block->copy_irreducible_status_to(r, jvms());
 736           // Add new phis.
 737           ensure_phis_everywhere();
 738         }
 739 
 740         // Leave behind an undisturbed copy of the map, for future merges.
 741         set_map(clone_map());
 742       }
 743 
 744       if (control()->is_Region() && !block->is_loop_head() && !has_irreducible && !block->is_handler()) {
 745         // In the absence of irreducible loops, the Region and Phis
 746         // associated with a merge that doesn't involve a backedge can
 747         // be simplified now since the RPO parsing order guarantees
 748         // that any path which was supposed to reach here has already
 749         // been parsed or must be dead.
 750         Node* c = control();
 751         Node* result = _gvn.transform(control());
 752         if (c != result && TraceOptoParse) {
 753           tty->print_cr("Block #%d replace %d with %d", block->rpo(), c->_idx, result->_idx);
 754         }
 755         if (result != top()) {
 756           record_for_igvn(result);
 757         }
 758       }
 759 
 760       // Parse the block.
 761       do_one_block();
 762 
 763       // Check for bailouts.
 764       if (failing())  return;
 765     }
 766 
 767     // with irreducible loops multiple passes might be necessary to parse everything
 768     if (!has_irreducible || !progress) {
 769       break;
 770     }
 771   }
 772 
 773 #ifndef PRODUCT
 774   blocks_seen += block_count();
 775 
 776   // Make sure there are no half-processed blocks remaining.
 777   // Every remaining unprocessed block is dead and may be ignored now.
 778   for (int rpo = 0; rpo < block_count(); rpo++) {
 779     Block* block = rpo_at(rpo);
 780     if (!block->is_parsed()) {
 781       if (TraceOptoParse) {
 782         tty->print_cr("Skipped dead block %d at bci:%d", rpo, block->start());
 783       }
 784       assert(!block->is_merged(), "no half-processed blocks");
 785     }
 786   }
 787 #endif
 788 }
 789 
 790 static Node* mask_int_value(Node* v, BasicType bt, PhaseGVN* gvn) {
 791   switch (bt) {
 792   case T_BYTE:
 793     v = gvn->transform(new LShiftINode(v, gvn->intcon(24)));
 794     v = gvn->transform(new RShiftINode(v, gvn->intcon(24)));
 795     break;
 796   case T_SHORT:
 797     v = gvn->transform(new LShiftINode(v, gvn->intcon(16)));
 798     v = gvn->transform(new RShiftINode(v, gvn->intcon(16)));
 799     break;
 800   case T_CHAR:
 801     v = gvn->transform(new AndINode(v, gvn->intcon(0xFFFF)));
 802     break;
 803   case T_BOOLEAN:
 804     v = gvn->transform(new AndINode(v, gvn->intcon(0x1)));
 805     break;
 806   default:
 807     break;
 808   }
 809   return v;
 810 }
 811 
 812 //-------------------------------build_exits----------------------------------
 813 // Build normal and exceptional exit merge points.
 814 void Parse::build_exits() {
 815   // make a clone of caller to prevent sharing of side-effects
 816   _exits.set_map(_exits.clone_map());
 817   _exits.clean_stack(_exits.sp());
 818   _exits.sync_jvms();
 819 
 820   RegionNode* region = new RegionNode(1);
 821   record_for_igvn(region);
 822   gvn().set_type_bottom(region);
 823   _exits.set_control(region);
 824 
 825   // Note:  iophi and memphi are not transformed until do_exits.
 826   Node* iophi  = new PhiNode(region, Type::ABIO);
 827   Node* memphi = new PhiNode(region, Type::MEMORY, TypePtr::BOTTOM);
 828   gvn().set_type_bottom(iophi);
 829   gvn().set_type_bottom(memphi);
 830   _exits.set_i_o(iophi);
 831   _exits.set_all_memory(memphi);
 832 
 833   // Add a return value to the exit state.  (Do not push it yet.)
 834   if (tf()->range()->cnt() > TypeFunc::Parms) {
 835     const Type* ret_type = tf()->range()->field_at(TypeFunc::Parms);
 836     if (ret_type->isa_int()) {
 837       BasicType ret_bt = method()->return_type()->basic_type();
 838       if (ret_bt == T_BOOLEAN ||
 839           ret_bt == T_CHAR ||
 840           ret_bt == T_BYTE ||
 841           ret_bt == T_SHORT) {
 842         ret_type = TypeInt::INT;
 843       }
 844     }
 845 
 846     // Don't "bind" an unloaded return klass to the ret_phi. If the klass
 847     // becomes loaded during the subsequent parsing, the loaded and unloaded
 848     // types will not join when we transform and push in do_exits().
 849     const TypeOopPtr* ret_oop_type = ret_type->isa_oopptr();
 850     if (ret_oop_type && !ret_oop_type->is_loaded()) {
 851       ret_type = TypeOopPtr::BOTTOM;
 852     }
 853     int         ret_size = type2size[ret_type->basic_type()];
 854     Node*       ret_phi  = new PhiNode(region, ret_type);
 855     gvn().set_type_bottom(ret_phi);
 856     _exits.ensure_stack(ret_size);
 857     assert((int)(tf()->range()->cnt() - TypeFunc::Parms) == ret_size, "good tf range");
 858     assert(method()->return_type()->size() == ret_size, "tf agrees w/ method");
 859     _exits.set_argument(0, ret_phi);  // here is where the parser finds it
 860     // Note:  ret_phi is not yet pushed, until do_exits.
 861   }
 862 }
 863 
 864 
 865 //----------------------------build_start_state-------------------------------
 866 // Construct a state which contains only the incoming arguments from an
 867 // unknown caller.  The method & bci will be null & InvocationEntryBci.
 868 JVMState* Compile::build_start_state(StartNode* start, const TypeFunc* tf) {
 869   int        arg_size = tf->domain()->cnt();
 870   int        max_size = MAX2(arg_size, (int)tf->range()->cnt());
 871   JVMState*  jvms     = new (this) JVMState(max_size - TypeFunc::Parms);
 872   SafePointNode* map  = new SafePointNode(max_size, jvms);
 873   record_for_igvn(map);
 874   assert(arg_size == TypeFunc::Parms + (is_osr_compilation() ? 1 : method()->arg_size()), "correct arg_size");
 875   Node_Notes* old_nn = default_node_notes();
 876   if (old_nn != nullptr && has_method()) {
 877     Node_Notes* entry_nn = old_nn->clone(this);
 878     JVMState* entry_jvms = new(this) JVMState(method(), old_nn->jvms());
 879     entry_jvms->set_offsets(0);
 880     entry_jvms->set_bci(entry_bci());
 881     entry_nn->set_jvms(entry_jvms);
 882     set_default_node_notes(entry_nn);
 883   }
 884   uint i;
 885   for (i = 0; i < (uint)arg_size; i++) {
 886     Node* parm = initial_gvn()->transform(new ParmNode(start, i));
 887     map->init_req(i, parm);
 888     // Record all these guys for later GVN.
 889     record_for_igvn(parm);
 890   }
 891   for (; i < map->req(); i++) {
 892     map->init_req(i, top());
 893   }
 894   assert(jvms->argoff() == TypeFunc::Parms, "parser gets arguments here");
 895   set_default_node_notes(old_nn);
 896   jvms->set_map(map);
 897   return jvms;
 898 }
 899 
 900 //-----------------------------make_node_notes---------------------------------
 901 Node_Notes* Parse::make_node_notes(Node_Notes* caller_nn) {
 902   if (caller_nn == nullptr)  return nullptr;
 903   Node_Notes* nn = caller_nn->clone(C);
 904   JVMState* caller_jvms = nn->jvms();
 905   JVMState* jvms = new (C) JVMState(method(), caller_jvms);
 906   jvms->set_offsets(0);
 907   jvms->set_bci(_entry_bci);
 908   nn->set_jvms(jvms);
 909   return nn;
 910 }
 911 
 912 
 913 //--------------------------return_values--------------------------------------
 914 void Compile::return_values(JVMState* jvms) {
 915   GraphKit kit(jvms);
 916   Node* ret = new ReturnNode(TypeFunc::Parms,
 917                              kit.control(),
 918                              kit.i_o(),
 919                              kit.reset_memory(),
 920                              kit.frameptr(),
 921                              kit.returnadr());
 922   // Add zero or 1 return values
 923   int ret_size = tf()->range()->cnt() - TypeFunc::Parms;
 924   if (ret_size > 0) {
 925     kit.inc_sp(-ret_size);  // pop the return value(s)
 926     kit.sync_jvms();
 927     ret->add_req(kit.argument(0));
 928     // Note:  The second dummy edge is not needed by a ReturnNode.
 929   }
 930   // bind it to root
 931   root()->add_req(ret);
 932   record_for_igvn(ret);
 933   initial_gvn()->transform(ret);
 934 }
 935 
 936 //------------------------rethrow_exceptions-----------------------------------
 937 // Bind all exception states in the list into a single RethrowNode.
 938 void Compile::rethrow_exceptions(JVMState* jvms) {
 939   GraphKit kit(jvms);
 940   if (!kit.has_exceptions())  return;  // nothing to generate
 941   // Load my combined exception state into the kit, with all phis transformed:
 942   SafePointNode* ex_map = kit.combine_and_pop_all_exception_states();
 943   Node* ex_oop = kit.use_exception_state(ex_map);
 944   RethrowNode* exit = new RethrowNode(kit.control(),
 945                                       kit.i_o(), kit.reset_memory(),
 946                                       kit.frameptr(), kit.returnadr(),
 947                                       // like a return but with exception input
 948                                       ex_oop);
 949   // bind to root
 950   root()->add_req(exit);
 951   record_for_igvn(exit);
 952   initial_gvn()->transform(exit);
 953 }
 954 
 955 //---------------------------do_exceptions-------------------------------------
 956 // Process exceptions arising from the current bytecode.
 957 // Send caught exceptions to the proper handler within this method.
 958 // Unhandled exceptions feed into _exit.
 959 void Parse::do_exceptions() {
 960   if (!has_exceptions())  return;
 961 
 962   if (failing()) {
 963     // Pop them all off and throw them away.
 964     while (pop_exception_state() != nullptr) ;
 965     return;
 966   }
 967 
 968   PreserveJVMState pjvms(this, false);
 969 
 970   SafePointNode* ex_map;
 971   while ((ex_map = pop_exception_state()) != nullptr) {
 972     if (!method()->has_exception_handlers()) {
 973       // Common case:  Transfer control outward.
 974       // Doing it this early allows the exceptions to common up
 975       // even between adjacent method calls.
 976       throw_to_exit(ex_map);
 977     } else {
 978       // Have to look at the exception first.
 979       assert(stopped(), "catch_inline_exceptions trashes the map");
 980       catch_inline_exceptions(ex_map);
 981       stop_and_kill_map();      // we used up this exception state; kill it
 982     }
 983   }
 984 
 985   // We now return to our regularly scheduled program:
 986 }
 987 
 988 //---------------------------throw_to_exit-------------------------------------
 989 // Merge the given map into an exception exit from this method.
 990 // The exception exit will handle any unlocking of receiver.
 991 // The ex_oop must be saved within the ex_map, unlike merge_exception.
 992 void Parse::throw_to_exit(SafePointNode* ex_map) {
 993   // Pop the JVMS to (a copy of) the caller.
 994   GraphKit caller;
 995   caller.set_map_clone(_caller->map());
 996   caller.set_bci(_caller->bci());
 997   caller.set_sp(_caller->sp());
 998   // Copy out the standard machine state:
 999   for (uint i = 0; i < TypeFunc::Parms; i++) {
1000     caller.map()->set_req(i, ex_map->in(i));
1001   }
1002   if (ex_map->has_replaced_nodes()) {
1003     _replaced_nodes_for_exceptions = true;
1004   }
1005   caller.map()->transfer_replaced_nodes_from(ex_map, _new_idx);
1006   // ...and the exception:
1007   Node*          ex_oop        = saved_ex_oop(ex_map);
1008   SafePointNode* caller_ex_map = caller.make_exception_state(ex_oop);
1009   // Finally, collect the new exception state in my exits:
1010   _exits.add_exception_state(caller_ex_map);
1011 }
1012 
1013 //------------------------------do_exits---------------------------------------
1014 void Parse::do_exits() {
1015   set_parse_bci(InvocationEntryBci);
1016 
1017   // Now peephole on the return bits
1018   Node* region = _exits.control();
1019   _exits.set_control(gvn().transform(region));
1020 
1021   Node* iophi = _exits.i_o();
1022   _exits.set_i_o(gvn().transform(iophi));
1023 
1024   // Figure out if we need to emit the trailing barrier. The barrier is only
1025   // needed in the constructors, and only in three cases:
1026   //
1027   // 1. The constructor wrote a final. The effects of all initializations
1028   //    must be committed to memory before any code after the constructor
1029   //    publishes the reference to the newly constructed object. Rather
1030   //    than wait for the publication, we simply block the writes here.
1031   //    Rather than put a barrier on only those writes which are required
1032   //    to complete, we force all writes to complete.
1033   //
1034   // 2. Experimental VM option is used to force the barrier if any field
1035   //    was written out in the constructor.
1036   //
1037   // 3. On processors which are not CPU_MULTI_COPY_ATOMIC (e.g. PPC64),
1038   //    support_IRIW_for_not_multiple_copy_atomic_cpu selects that
1039   //    MemBarVolatile is used before volatile load instead of after volatile
1040   //    store, so there's no barrier after the store.
1041   //    We want to guarantee the same behavior as on platforms with total store
1042   //    order, although this is not required by the Java memory model.
1043   //    In this case, we want to enforce visibility of volatile field
1044   //    initializations which are performed in constructors.
1045   //    So as with finals, we add a barrier here.
1046   //
1047   // "All bets are off" unless the first publication occurs after a
1048   // normal return from the constructor.  We do not attempt to detect
1049   // such unusual early publications.  But no barrier is needed on
1050   // exceptional returns, since they cannot publish normally.
1051   //
1052   if (method()->is_initializer() &&
1053        (wrote_final() ||
1054          (AlwaysSafeConstructors && wrote_fields()) ||
1055          (support_IRIW_for_not_multiple_copy_atomic_cpu && wrote_volatile()))) {
1056     if (!DoPartialEscapeAnalysis) {
1057       // If Memory barrier is created for final fields write
1058       // and allocation node does not escape the initialize method,
1059       // then barrier introduced by allocation node can be removed.
1060       if (DoEscapeAnalysis && alloc_with_final()) {
1061         AllocateNode *alloc = AllocateNode::Ideal_allocation(alloc_with_final());
1062         alloc->compute_MemBar_redundancy(method());
1063       }
1064     } else {
1065       // in PEA, alloc_with_final stores ObjID
1066       AllocateNode* alloc = (ObjID)alloc_with_final();
1067 
1068       if (DoEscapeAnalysis && alloc != nullptr) {
1069         Node* obj = _exits.jvms()->alloc_state().get_java_oop(alloc);
1070         _exits.insert_mem_bar(Op_MemBarRelease, obj);
1071         alloc->compute_MemBar_redundancy(method());
1072       }
1073     }
1074 
1075     if (PrintOpto && (Verbose || WizardMode)) {
1076       method()->print_name();
1077       tty->print_cr(" writes finals and needs a memory barrier");
1078     }
1079   }
1080 
1081   // Any method can write a @Stable field; insert memory barriers
1082   // after those also. Can't bind predecessor allocation node (if any)
1083   // with barrier because allocation doesn't always dominate
1084   // MemBarRelease.
1085   if (wrote_stable()) {
1086     _exits.insert_mem_bar(Op_MemBarRelease);
1087     if (PrintOpto && (Verbose || WizardMode)) {
1088       method()->print_name();
1089       tty->print_cr(" writes @Stable and needs a memory barrier");
1090     }
1091   }
1092 
1093   for (MergeMemStream mms(_exits.merged_memory()); mms.next_non_empty(); ) {
1094     // transform each slice of the original memphi:
1095     mms.set_memory(_gvn.transform(mms.memory()));
1096   }
1097   // Clean up input MergeMems created by transforming the slices
1098   _gvn.transform(_exits.merged_memory());
1099 
1100   if (tf()->range()->cnt() > TypeFunc::Parms) {
1101     const Type* ret_type = tf()->range()->field_at(TypeFunc::Parms);
1102     Node* const ret_phi_old = _exits.argument(0);
1103     Node*       ret_phi  = _gvn.transform(ret_phi_old);
1104     if (DoPartialEscapeAnalysis && ret_phi_old != ret_phi) {
1105       PEAState& as = _exits.jvms()->alloc_state();
1106       EscapedState* es = as.as_escaped(PEA(), ret_phi_old);
1107 
1108       if (es != nullptr && es->merged_value() == ret_phi_old) {
1109         es->update(ret_phi);
1110         ObjID obj = PEA()->is_alias(ret_phi_old);
1111         PEA()->add_alias(obj, ret_phi);
1112       }
1113     }
1114     if (!_exits.control()->is_top() && _gvn.type(ret_phi)->empty()) {
1115       // If the type we set for the ret_phi in build_exits() is too optimistic and
1116       // the ret_phi is top now, there's an extremely small chance that it may be due to class
1117       // loading.  It could also be due to an error, so mark this method as not compilable because
1118       // otherwise this could lead to an infinite compile loop.
1119       // In any case, this code path is rarely (and never in my testing) reached.
1120 #ifdef ASSERT
1121       tty->print_cr("# Can't determine return type.");
1122       tty->print_cr("# exit control");
1123       _exits.control()->dump(2);
1124       tty->print_cr("# ret phi type");
1125       _gvn.type(ret_phi)->dump();
1126       tty->print_cr("# ret phi");
1127       ret_phi->dump(2);
1128 #endif // ASSERT
1129       assert(false, "Can't determine return type.");
1130       C->record_method_not_compilable("Can't determine return type.");
1131       return;
1132     }
1133     if (ret_type->isa_int()) {
1134       BasicType ret_bt = method()->return_type()->basic_type();
1135       ret_phi = mask_int_value(ret_phi, ret_bt, &_gvn);
1136     }
1137     _exits.push_node(ret_type->basic_type(), ret_phi);
1138   }
1139 
1140   // Note:  Logic for creating and optimizing the ReturnNode is in Compile.
1141 
1142   // Unlock along the exceptional paths.
1143   // This is done late so that we can common up equivalent exceptions
1144   // (e.g., null checks) arising from multiple points within this method.
1145   // See GraphKit::add_exception_state, which performs the commoning.
1146   bool do_synch = method()->is_synchronized() && GenerateSynchronizationCode;
1147 
1148   // record exit from a method if compiled while Dtrace is turned on.
1149   if (do_synch || C->env()->dtrace_method_probes() || _replaced_nodes_for_exceptions) {
1150     // First move the exception list out of _exits:
1151     GraphKit kit(_exits.transfer_exceptions_into_jvms());
1152     SafePointNode* normal_map = kit.map();  // keep this guy safe
1153     // Now re-collect the exceptions into _exits:
1154     SafePointNode* ex_map;
1155     while ((ex_map = kit.pop_exception_state()) != nullptr) {
1156       Node* ex_oop = kit.use_exception_state(ex_map);
1157       // Force the exiting JVM state to have this method at InvocationEntryBci.
1158       // The exiting JVM state is otherwise a copy of the calling JVMS.
1159       JVMState* caller = kit.jvms();
1160       JVMState* ex_jvms = caller->clone_shallow(C);
1161       ex_jvms->bind_map(kit.clone_map());
1162       ex_jvms->set_bci(   InvocationEntryBci);
1163       kit.set_jvms(ex_jvms);
1164       if (do_synch) {
1165         // Add on the synchronized-method box/object combo
1166         kit.map()->push_monitor(_synch_lock);
1167         // Unlock!
1168         kit.shared_unlock(_synch_lock->box_node(), _synch_lock->obj_node());
1169       }
1170       if (C->env()->dtrace_method_probes()) {
1171         kit.make_dtrace_method_exit(method());
1172       }
1173       if (_replaced_nodes_for_exceptions) {
1174         kit.map()->apply_replaced_nodes(_new_idx);
1175       }
1176       // Done with exception-path processing.
1177       ex_map = kit.make_exception_state(ex_oop);
1178       assert(ex_jvms->same_calls_as(ex_map->jvms()), "sanity");
1179       // Pop the last vestige of this method:
1180       caller->clone_shallow(C)->bind_map(ex_map);
1181       _exits.push_exception_state(ex_map);
1182     }
1183     assert(_exits.map() == normal_map, "keep the same return state");
1184   }
1185 
1186   {
1187     // Capture very early exceptions (receiver null checks) from caller JVMS
1188     GraphKit caller(_caller);
1189     SafePointNode* ex_map;
1190     while ((ex_map = caller.pop_exception_state()) != nullptr) {
1191       _exits.add_exception_state(ex_map);
1192     }
1193   }
1194 
1195   _exits.map()->apply_replaced_nodes(_new_idx);
1196   // don't trust replace list. return_current() may mess it up.
1197   // use AllocationState to update it.
1198   if (DoPartialEscapeAnalysis) {
1199     PEAState& as = _exits.jvms()->alloc_state();
1200     SafePointNode* map = _exits.map();
1201     backfill_materialized(map, TypeFunc::Parms, map->req(), as);
1202   }
1203 }
1204 
1205 //-----------------------------create_entry_map-------------------------------
1206 // Initialize our parser map to contain the types at method entry.
1207 // For OSR, the map contains a single RawPtr parameter.
1208 // Initial monitor locking for sync. methods is performed by do_method_entry.
1209 SafePointNode* Parse::create_entry_map() {
1210   // Check for really stupid bail-out cases.
1211   uint len = TypeFunc::Parms + method()->max_locals() + method()->max_stack();
1212   if (len >= 32760) {
1213     // Bailout expected, this is a very rare edge case.
1214     C->record_method_not_compilable("too many local variables");
1215     return nullptr;
1216   }
1217 
1218   // clear current replaced nodes that are of no use from here on (map was cloned in build_exits).
1219   _caller->map()->delete_replaced_nodes();
1220 
1221   // If this is an inlined method, we may have to do a receiver null check.
1222   if (_caller->has_method() && is_normal_parse() && !method()->is_static()) {
1223     GraphKit kit(_caller);
1224     kit.null_check_receiver_before_call(method());
1225     _caller = kit.transfer_exceptions_into_jvms();
1226     if (kit.stopped()) {
1227       _exits.add_exception_states_from(_caller);
1228       _exits.set_jvms(_caller);
1229       return nullptr;
1230     }
1231   }
1232 
1233   assert(method() != nullptr, "parser must have a method");
1234 
1235   // Create an initial safepoint to hold JVM state during parsing
1236   JVMState* jvms = new (C) JVMState(method(), _caller->has_method() ? _caller : nullptr);
1237   if (_caller != nullptr && DoPartialEscapeAnalysis) {
1238     jvms->alloc_state() = _caller->alloc_state();
1239   }
1240 
1241   set_map(new SafePointNode(len, jvms));
1242   jvms->set_map(map());
1243   record_for_igvn(map());
1244   assert(jvms->endoff() == len, "correct jvms sizing");
1245 
1246   SafePointNode* inmap = _caller->map();
1247   assert(inmap != nullptr, "must have inmap");
1248   // In case of null check on receiver above
1249   map()->transfer_replaced_nodes_from(inmap, _new_idx);
1250 
1251   uint i;
1252 
1253   // Pass thru the predefined input parameters.
1254   for (i = 0; i < TypeFunc::Parms; i++) {
1255     map()->init_req(i, inmap->in(i));
1256   }
1257 
1258   if (depth() == 1) {
1259     assert(map()->memory()->Opcode() == Op_Parm, "");
1260     // Insert the memory aliasing node
1261     set_all_memory(reset_memory());
1262   }
1263   assert(merged_memory(), "");
1264 
1265   // Now add the locals which are initially bound to arguments:
1266   uint arg_size = tf()->domain()->cnt();
1267   ensure_stack(arg_size - TypeFunc::Parms);  // OSR methods have funny args
1268   for (i = TypeFunc::Parms; i < arg_size; i++) {
1269     map()->init_req(i, inmap->argument(_caller, i - TypeFunc::Parms));
1270   }
1271 
1272   // Clear out the rest of the map (locals and stack)
1273   for (i = arg_size; i < len; i++) {
1274     map()->init_req(i, top());
1275   }
1276 
1277   SafePointNode* entry_map = stop();
1278   return entry_map;
1279 }
1280 
1281 //-----------------------------do_method_entry--------------------------------
1282 // Emit any code needed in the pseudo-block before BCI zero.
1283 // The main thing to do is lock the receiver of a synchronized method.
1284 void Parse::do_method_entry() {
1285   set_parse_bci(InvocationEntryBci); // Pseudo-BCP
1286   set_sp(0);                         // Java Stack Pointer
1287 
1288   NOT_PRODUCT( count_compiled_calls(true/*at_method_entry*/, false/*is_inline*/); )
1289 
1290   if (C->env()->dtrace_method_probes()) {
1291     make_dtrace_method_entry(method());
1292   }
1293 
1294 #ifdef ASSERT
1295   // Narrow receiver type when it is too broad for the method being parsed.
1296   if (!method()->is_static()) {
1297     ciInstanceKlass* callee_holder = method()->holder();
1298     const Type* holder_type = TypeInstPtr::make(TypePtr::BotPTR, callee_holder, Type::trust_interfaces);
1299 
1300     Node* receiver_obj = local(0);
1301     const TypeInstPtr* receiver_type = _gvn.type(receiver_obj)->isa_instptr();
1302 
1303     if (receiver_type != nullptr && !receiver_type->higher_equal(holder_type)) {
1304       // Receiver should always be a subtype of callee holder.
1305       // But, since C2 type system doesn't properly track interfaces,
1306       // the invariant can't be expressed in the type system for default methods.
1307       // Example: for unrelated C <: I and D <: I, (C `meet` D) = Object </: I.
1308       assert(callee_holder->is_interface(), "missing subtype check");
1309 
1310       // Perform dynamic receiver subtype check against callee holder class w/ a halt on failure.
1311       Node* holder_klass = _gvn.makecon(TypeKlassPtr::make(callee_holder, Type::trust_interfaces));
1312       Node* not_subtype_ctrl = gen_subtype_check(receiver_obj, holder_klass);
1313       assert(!stopped(), "not a subtype");
1314 
1315       Node* halt = _gvn.transform(new HaltNode(not_subtype_ctrl, frameptr(), "failed receiver subtype check"));
1316       C->root()->add_req(halt);
1317     }
1318   }
1319 #endif // ASSERT
1320 
1321   // If the method is synchronized, we need to construct a lock node, attach
1322   // it to the Start node, and pin it there.
1323   if (method()->is_synchronized()) {
1324     // Insert a FastLockNode right after the Start which takes as arguments
1325     // the current thread pointer, the "this" pointer & the address of the
1326     // stack slot pair used for the lock.  The "this" pointer is a projection
1327     // off the start node, but the locking spot has to be constructed by
1328     // creating a ConLNode of 0, and boxing it with a BoxLockNode.  The BoxLockNode
1329     // becomes the second argument to the FastLockNode call.  The
1330     // FastLockNode becomes the new control parent to pin it to the start.
1331 
1332     // Setup Object Pointer
1333     Node *lock_obj = nullptr;
1334     if (method()->is_static()) {
1335       ciInstance* mirror = _method->holder()->java_mirror();
1336       const TypeInstPtr *t_lock = TypeInstPtr::make(mirror);
1337       lock_obj = makecon(t_lock);
1338     } else {                  // Else pass the "this" pointer,
1339       lock_obj = local(0);    // which is Parm0 from StartNode
1340     }
1341     // Clear out dead values from the debug info.
1342     kill_dead_locals();
1343     // Build the FastLockNode
1344     _synch_lock = shared_lock(lock_obj);
1345   }
1346 
1347   // Feed profiling data for parameters to the type system so it can
1348   // propagate it as speculative types
1349   record_profiled_parameters_for_speculation();
1350 }
1351 
1352 //------------------------------init_blocks------------------------------------
1353 // Initialize our parser map to contain the types/monitors at method entry.
1354 void Parse::init_blocks() {
1355   // Create the blocks.
1356   _block_count = flow()->block_count();
1357   _blocks = NEW_RESOURCE_ARRAY(Block, _block_count);
1358 
1359   // Initialize the structs.
1360   for (int rpo = 0; rpo < block_count(); rpo++) {
1361     Block* block = rpo_at(rpo);
1362     new(block) Block(this, rpo);
1363   }
1364 
1365   // Collect predecessor and successor information.
1366   for (int rpo = 0; rpo < block_count(); rpo++) {
1367     Block* block = rpo_at(rpo);
1368     block->init_graph(this);
1369   }
1370 }
1371 
1372 //-------------------------------init_node-------------------------------------
1373 Parse::Block::Block(Parse* outer, int rpo) : _live_locals() {
1374   _flow = outer->flow()->rpo_at(rpo);
1375   _pred_count = 0;
1376   _preds_parsed = 0;
1377   _count = 0;
1378   _is_parsed = false;
1379   _is_handler = false;
1380   _has_merged_backedge = false;
1381   _start_map = nullptr;
1382   _has_predicates = false;
1383   _num_successors = 0;
1384   _all_successors = 0;
1385   _successors = nullptr;
1386   assert(pred_count() == 0 && preds_parsed() == 0, "sanity");
1387   assert(!(is_merged() || is_parsed() || is_handler() || has_merged_backedge()), "sanity");
1388   assert(!_live_locals.is_valid(), "sanity");
1389 
1390   // entry point has additional predecessor
1391   if (flow()->is_start())  _pred_count++;
1392   assert(flow()->is_start() == (this == outer->start_block()), "");
1393 }
1394 
1395 //-------------------------------init_graph------------------------------------
1396 void Parse::Block::init_graph(Parse* outer) {
1397   // Create the successor list for this parser block.
1398   GrowableArray<ciTypeFlow::Block*>* tfs = flow()->successors();
1399   GrowableArray<ciTypeFlow::Block*>* tfe = flow()->exceptions();
1400   int ns = tfs->length();
1401   int ne = tfe->length();
1402   _num_successors = ns;
1403   _all_successors = ns+ne;
1404   _successors = (ns+ne == 0) ? nullptr: NEW_RESOURCE_ARRAY(Block*, ns+ne);
1405   for (int i = 0; i < ns+ne; i++) {
1406     ciTypeFlow::Block* tf2 = (i < ns) ? tfs->at(i) : tfe->at(i-ns);
1407     Block* block2 = outer->rpo_at(tf2->rpo());
1408     _successors[i] = block2;
1409 
1410     // Accumulate pred info for the other block, too.
1411     // Note: We also need to set _pred_count for exception blocks since they could
1412     // also have normal predecessors (reached without athrow by an explicit jump).
1413     // This also means that next_path_num can be called along exception paths.
1414     block2->_pred_count++;
1415     if (i >= ns) {
1416       block2->_is_handler = true;
1417     }
1418 
1419     #ifdef ASSERT
1420     // A block's successors must be distinguishable by BCI.
1421     // That is, no bytecode is allowed to branch to two different
1422     // clones of the same code location.
1423     for (int j = 0; j < i; j++) {
1424       Block* block1 = _successors[j];
1425       if (block1 == block2)  continue;  // duplicates are OK
1426       assert(block1->start() != block2->start(), "successors have unique bcis");
1427     }
1428     #endif
1429   }
1430 
1431   if (DoPartialEscapeAnalysis) {
1432     GrowableArray<ciTypeFlow::Block*>* tfp = flow()->predecessors();
1433     int np = tfp->length();
1434     _predecessors = np > 0 ? NEW_RESOURCE_ARRAY(Block*, np) : nullptr;
1435     for (int i = 0; i < np; ++i) {
1436       ciTypeFlow::Block* tf2 = tfp->at(i);
1437       Block* block2 = outer->rpo_at(tf2->rpo());
1438       _predecessors[i] = block2;
1439     }
1440   }
1441 }
1442 
1443 //---------------------------successor_for_bci---------------------------------
1444 Parse::Block* Parse::Block::successor_for_bci(int bci) {
1445   for (int i = 0; i < all_successors(); i++) {
1446     Block* block2 = successor_at(i);
1447     if (block2->start() == bci)  return block2;
1448   }
1449   // We can actually reach here if ciTypeFlow traps out a block
1450   // due to an unloaded class, and concurrently with compilation the
1451   // class is then loaded, so that a later phase of the parser is
1452   // able to see more of the bytecode CFG.  Or, the flow pass and
1453   // the parser can have a minor difference of opinion about executability
1454   // of bytecodes.  For example, "obj.field = null" is executable even
1455   // if the field's type is an unloaded class; the flow pass used to
1456   // make a trap for such code.
1457   return nullptr;
1458 }
1459 
1460 
1461 //-----------------------------stack_type_at-----------------------------------
1462 const Type* Parse::Block::stack_type_at(int i) const {
1463   return get_type(flow()->stack_type_at(i));
1464 }
1465 
1466 
1467 //-----------------------------local_type_at-----------------------------------
1468 const Type* Parse::Block::local_type_at(int i) const {
1469   // This bitmap can be zero length if we saw a breakpoint.
1470   // In such cases, pretend they are all live.
1471   auto live_locals = liveness();
1472   if (live_locals.size() > 0 && !live_locals.at(i))
1473     return Type::BOTTOM;
1474 
1475   return get_type(flow()->local_type_at(i));
1476 }
1477 
1478 
1479 #ifndef PRODUCT
1480 
1481 //----------------------------name_for_bc--------------------------------------
1482 // helper method for BytecodeParseHistogram
1483 static const char* name_for_bc(int i) {
1484   return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx";
1485 }
1486 
1487 //----------------------------BytecodeParseHistogram------------------------------------
1488 Parse::BytecodeParseHistogram::BytecodeParseHistogram(Parse *p, Compile *c) {
1489   _parser   = p;
1490   _compiler = c;
1491   if( ! _initialized ) { _initialized = true; reset(); }
1492 }
1493 
1494 //----------------------------current_count------------------------------------
1495 int Parse::BytecodeParseHistogram::current_count(BPHType bph_type) {
1496   switch( bph_type ) {
1497   case BPH_transforms: { return _parser->gvn().made_progress(); }
1498   case BPH_values:     { return _parser->gvn().made_new_values(); }
1499   default: { ShouldNotReachHere(); return 0; }
1500   }
1501 }
1502 
1503 //----------------------------initialized--------------------------------------
1504 bool Parse::BytecodeParseHistogram::initialized() { return _initialized; }
1505 
1506 //----------------------------reset--------------------------------------------
1507 void Parse::BytecodeParseHistogram::reset() {
1508   int i = Bytecodes::number_of_codes;
1509   while (i-- > 0) { _bytecodes_parsed[i] = 0; _nodes_constructed[i] = 0; _nodes_transformed[i] = 0; _new_values[i] = 0; }
1510 }
1511 
1512 //----------------------------set_initial_state--------------------------------
1513 // Record info when starting to parse one bytecode
1514 void Parse::BytecodeParseHistogram::set_initial_state( Bytecodes::Code bc ) {
1515   if( PrintParseStatistics && !_parser->is_osr_parse() ) {
1516     _initial_bytecode    = bc;
1517     _initial_node_count  = _compiler->unique();
1518     _initial_transforms  = current_count(BPH_transforms);
1519     _initial_values      = current_count(BPH_values);
1520   }
1521 }
1522 
1523 //----------------------------record_change--------------------------------
1524 // Record results of parsing one bytecode
1525 void Parse::BytecodeParseHistogram::record_change() {
1526   if( PrintParseStatistics && !_parser->is_osr_parse() ) {
1527     ++_bytecodes_parsed[_initial_bytecode];
1528     _nodes_constructed [_initial_bytecode] += (_compiler->unique() - _initial_node_count);
1529     _nodes_transformed [_initial_bytecode] += (current_count(BPH_transforms) - _initial_transforms);
1530     _new_values        [_initial_bytecode] += (current_count(BPH_values)     - _initial_values);
1531   }
1532 }
1533 
1534 
1535 //----------------------------print--------------------------------------------
1536 void Parse::BytecodeParseHistogram::print(float cutoff) {
1537   ResourceMark rm;
1538   // print profile
1539   int total  = 0;
1540   int i      = 0;
1541   for( i = 0; i < Bytecodes::number_of_codes; ++i ) { total += _bytecodes_parsed[i]; }
1542   int abs_sum = 0;
1543   tty->cr();   //0123456789012345678901234567890123456789012345678901234567890123456789
1544   tty->print_cr("Histogram of %d parsed bytecodes:", total);
1545   if( total == 0 ) { return; }
1546   tty->cr();
1547   tty->print_cr("absolute:  count of compiled bytecodes of this type");
1548   tty->print_cr("relative:  percentage contribution to compiled nodes");
1549   tty->print_cr("nodes   :  Average number of nodes constructed per bytecode");
1550   tty->print_cr("rnodes  :  Significance towards total nodes constructed, (nodes*relative)");
1551   tty->print_cr("transforms: Average amount of transform progress per bytecode compiled");
1552   tty->print_cr("values  :  Average number of node values improved per bytecode");
1553   tty->print_cr("name    :  Bytecode name");
1554   tty->cr();
1555   tty->print_cr("  absolute  relative   nodes  rnodes  transforms  values   name");
1556   tty->print_cr("----------------------------------------------------------------------");
1557   while (--i > 0) {
1558     int       abs = _bytecodes_parsed[i];
1559     float     rel = abs * 100.0F / total;
1560     float   nodes = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _nodes_constructed[i])/_bytecodes_parsed[i];
1561     float  rnodes = _bytecodes_parsed[i] == 0 ? 0 :  rel * nodes;
1562     float  xforms = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _nodes_transformed[i])/_bytecodes_parsed[i];
1563     float  values = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _new_values       [i])/_bytecodes_parsed[i];
1564     if (cutoff <= rel) {
1565       tty->print_cr("%10d  %7.2f%%  %6.1f  %6.2f   %6.1f   %6.1f     %s", abs, rel, nodes, rnodes, xforms, values, name_for_bc(i));
1566       abs_sum += abs;
1567     }
1568   }
1569   tty->print_cr("----------------------------------------------------------------------");
1570   float rel_sum = abs_sum * 100.0F / total;
1571   tty->print_cr("%10d  %7.2f%%    (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff);
1572   tty->print_cr("----------------------------------------------------------------------");
1573   tty->cr();
1574 }
1575 #endif
1576 
1577 //----------------------------load_state_from----------------------------------
1578 // Load block/map/sp.  But not do not touch iter/bci.
1579 void Parse::load_state_from(Block* block) {
1580   set_block(block);
1581   // load the block's JVM state:
1582   set_map(block->start_map());
1583   set_sp( block->start_sp());
1584 }
1585 
1586 
1587 //-----------------------------record_state------------------------------------
1588 void Parse::Block::record_state(Parse* p, int pnum) {
1589   assert(!is_merged(), "can only record state once, on 1st inflow");
1590   assert(start_sp() == p->sp(), "stack pointer must agree with ciTypeFlow");
1591   set_start_map(p->stop());
1592 
1593   _from_block = p->block();
1594   _init_pnum = pnum;
1595 }
1596 
1597 
1598 //------------------------------do_one_block-----------------------------------
1599 void Parse::do_one_block() {
1600   if (TraceOptoParse) {
1601     Block *b = block();
1602     int ns = b->num_successors();
1603     int nt = b->all_successors();
1604 
1605     tty->print("Parsing block #%d at bci [%d,%d), successors:",
1606                   block()->rpo(), block()->start(), block()->limit());
1607     for (int i = 0; i < nt; i++) {
1608       tty->print((( i < ns) ? " %d" : " %d(exception block)"), b->successor_at(i)->rpo());
1609     }
1610     if (b->is_loop_head()) {
1611       tty->print("  loop head");
1612     }
1613     if (b->is_irreducible_loop_entry()) {
1614       tty->print("  irreducible");
1615     }
1616     tty->cr();
1617   }
1618 
1619 #ifndef PRODUCT
1620   if (PEAVerbose) {
1621     PEAState& as = jvms()->alloc_state();
1622     as.print_on(tty);
1623   }
1624 #endif
1625   assert(block()->is_merged(), "must be merged before being parsed");
1626   block()->mark_parsed();
1627 
1628   // Set iterator to start of block.
1629   iter().reset_to_bci(block()->start());
1630 
1631   if (ProfileExceptionHandlers && block()->is_handler()) {
1632     ciMethodData* methodData = method()->method_data();
1633     if (methodData->is_mature()) {
1634       ciBitData data = methodData->exception_handler_bci_to_data(block()->start());
1635       if (!data.exception_handler_entered() || StressPrunedExceptionHandlers) {
1636         // dead catch block
1637         // Emit an uncommon trap instead of processing the block.
1638         set_parse_bci(block()->start());
1639         uncommon_trap(Deoptimization::Reason_unreached,
1640                       Deoptimization::Action_reinterpret,
1641                       nullptr, "dead catch block");
1642         return;
1643       }
1644     }
1645   }
1646 
1647   CompileLog* log = C->log();
1648 
1649   // Parse bytecodes
1650   while (!stopped() && !failing()) {
1651     iter().next();
1652 
1653     // Learn the current bci from the iterator:
1654     set_parse_bci(iter().cur_bci());
1655 
1656     if (bci() == block()->limit()) {
1657       // Do not walk into the next block until directed by do_all_blocks.
1658       merge(bci());
1659       break;
1660     }
1661     assert(bci() < block()->limit(), "bci still in block");
1662 
1663     if (log != nullptr) {
1664       // Output an optional context marker, to help place actions
1665       // that occur during parsing of this BC.  If there is no log
1666       // output until the next context string, this context string
1667       // will be silently ignored.
1668       log->set_context("bc code='%d' bci='%d'", (int)bc(), bci());
1669     }
1670 
1671     if (block()->has_trap_at(bci())) {
1672       // We must respect the flow pass's traps, because it will refuse
1673       // to produce successors for trapping blocks.
1674       int trap_index = block()->flow()->trap_index();
1675       assert(trap_index != 0, "trap index must be valid");
1676       uncommon_trap(trap_index);
1677       break;
1678     }
1679 
1680     NOT_PRODUCT( parse_histogram()->set_initial_state(bc()); );
1681 
1682 #ifdef ASSERT
1683     int pre_bc_sp = sp();
1684     int inputs, depth;
1685     bool have_se = !stopped() && compute_stack_effects(inputs, depth);
1686     assert(!have_se || pre_bc_sp >= inputs, "have enough stack to execute this BC: pre_bc_sp=%d, inputs=%d", pre_bc_sp, inputs);
1687 #endif //ASSERT
1688 
1689     do_one_bytecode();
1690     if (failing()) return;
1691 
1692     assert(!have_se || stopped() || failing() || (sp() - pre_bc_sp) == depth,
1693            "incorrect depth prediction: sp=%d, pre_bc_sp=%d, depth=%d", sp(), pre_bc_sp, depth);
1694 
1695     do_exceptions();
1696 
1697     NOT_PRODUCT( parse_histogram()->record_change(); );
1698 
1699     if (log != nullptr)
1700       log->clear_context();  // skip marker if nothing was printed
1701 
1702     // Fall into next bytecode.  Each bytecode normally has 1 sequential
1703     // successor which is typically made ready by visiting this bytecode.
1704     // If the successor has several predecessors, then it is a merge
1705     // point, starts a new basic block, and is handled like other basic blocks.
1706   }
1707 }
1708 
1709 
1710 //------------------------------merge------------------------------------------
1711 void Parse::set_parse_bci(int bci) {
1712   set_bci(bci);
1713   Node_Notes* nn = C->default_node_notes();
1714   if (nn == nullptr)  return;
1715 
1716   // Collect debug info for inlined calls unless -XX:-DebugInlinedCalls.
1717   if (!DebugInlinedCalls && depth() > 1) {
1718     return;
1719   }
1720 
1721   // Update the JVMS annotation, if present.
1722   JVMState* jvms = nn->jvms();
1723   if (jvms != nullptr && jvms->bci() != bci) {
1724     // Update the JVMS.
1725     jvms = jvms->clone_shallow(C);
1726     jvms->set_bci(bci);
1727     nn->set_jvms(jvms);
1728   }
1729 }
1730 
1731 //------------------------------merge------------------------------------------
1732 // Merge the current mapping into the basic block starting at bci
1733 void Parse::merge(int target_bci) {
1734   Block* target = successor_for_bci(target_bci);
1735   if (target == nullptr) { handle_missing_successor(target_bci); return; }
1736   assert(!target->is_ready(), "our arrival must be expected");
1737   int pnum = target->next_path_num();
1738   merge_common(target, pnum);
1739 }
1740 
1741 //-------------------------merge_new_path--------------------------------------
1742 // Merge the current mapping into the basic block, using a new path
1743 void Parse::merge_new_path(int target_bci) {
1744   Block* target = successor_for_bci(target_bci);
1745   if (target == nullptr) { handle_missing_successor(target_bci); return; }
1746   assert(!target->is_ready(), "new path into frozen graph");
1747   int pnum = target->add_new_path();
1748   merge_common(target, pnum);
1749 }
1750 
1751 //-------------------------merge_exception-------------------------------------
1752 // Merge the current mapping into the basic block starting at bci
1753 // The ex_oop must be pushed on the stack, unlike throw_to_exit.
1754 void Parse::merge_exception(int target_bci) {
1755 #ifdef ASSERT
1756   if (target_bci < bci()) {
1757     C->set_exception_backedge();
1758   }
1759 #endif
1760   assert(sp() == 1, "must have only the throw exception on the stack");
1761   Block* target = successor_for_bci(target_bci);
1762   if (target == nullptr) { handle_missing_successor(target_bci); return; }
1763   assert(target->is_handler(), "exceptions are handled by special blocks");
1764   int pnum = target->add_new_path();
1765   merge_common(target, pnum);
1766 }
1767 
1768 //--------------------handle_missing_successor---------------------------------
1769 void Parse::handle_missing_successor(int target_bci) {
1770 #ifndef PRODUCT
1771   Block* b = block();
1772   int trap_bci = b->flow()->has_trap()? b->flow()->trap_bci(): -1;
1773   tty->print_cr("### Missing successor at bci:%d for block #%d (trap_bci:%d)", target_bci, b->rpo(), trap_bci);
1774 #endif
1775   ShouldNotReachHere();
1776 }
1777 
1778 //--------------------------merge_common---------------------------------------
1779 void Parse::merge_common(Parse::Block* target, int pnum) {
1780   if (TraceOptoParse) {
1781     tty->print("Merging state at block #%d bci:%d", target->rpo(), target->start());
1782     if (!target->is_merged()) {
1783       tty->print(" with empty state");
1784     } else {
1785       tty->print(" with previous state");
1786     }
1787     tty->print_cr(" on path %d", pnum);
1788   }
1789 
1790   // Zap extra stack slots to top
1791   assert(sp() == target->start_sp(), "");
1792   clean_stack(sp());
1793 
1794   if (!target->is_merged()) {   // No prior mapping at this bci
1795     // If this path is dead, do not bother capturing it as a merge.
1796     // It is "as if" we had 1 fewer predecessors from the beginning.
1797     if (stopped()) {
1798       if (TraceOptoParse)  tty->print_cr(", but path is dead and doesn't count");
1799       return;
1800     }
1801 
1802     // Make a region if we know there are multiple or unpredictable inputs.
1803     // (Also, if this is a plain fall-through, we might see another region,
1804     // which must not be allowed into this block's map.)
1805     if (pnum > PhiNode::Input         // Known multiple inputs.
1806         || target->is_handler()       // These have unpredictable inputs.
1807         || target->is_loop_head()     // Known multiple inputs
1808         || control()->is_Region()) {  // We must hide this guy.
1809 
1810       int current_bci = bci();
1811       set_parse_bci(target->start()); // Set target bci
1812       if (target->is_SEL_head()) {
1813         DEBUG_ONLY( target->mark_merged_backedge(block()); )
1814         if (target->start() == 0) {
1815           // Add Parse Predicates for the special case when
1816           // there are backbranches to the method entry.
1817           add_parse_predicates();
1818         }
1819       }
1820       // Add a Region to start the new basic block.  Phis will be added
1821       // later lazily.
1822       int edges = target->pred_count();
1823       if (edges < pnum)  edges = pnum;  // might be a new path!
1824       RegionNode *r = new RegionNode(edges+1);
1825       gvn().set_type(r, Type::CONTROL);
1826       record_for_igvn(r);
1827       // zap all inputs to null for debugging (done in Node(uint) constructor)
1828       // for (int j = 1; j < edges+1; j++) { r->init_req(j, nullptr); }
1829       r->init_req(pnum, control());
1830       set_control(r);
1831       target->copy_irreducible_status_to(r, jvms());
1832       set_parse_bci(current_bci); // Restore bci
1833     }
1834 
1835     // Convert the existing Parser mapping into a mapping at this bci.
1836     store_state_to(target, pnum);
1837     assert(target->is_merged(), "do not come here twice");
1838 #ifdef ASSERT
1839     target->state().validate();
1840 #endif
1841   } else {                      // Prior mapping at this bci
1842 
1843 #ifdef ASSERT
1844     if (target->is_SEL_head()) {
1845       target->mark_merged_backedge(block());
1846     }
1847 #endif
1848     // We must not manufacture more phis if the target is already parsed.
1849     bool nophi = target->is_parsed();
1850 
1851     SafePointNode* newin = map();// Hang on to incoming mapping
1852     Block* save_block = block(); // Hang on to incoming block;
1853     load_state_from(target);    // Get prior mapping
1854 
1855     assert(newin->jvms()->locoff() == jvms()->locoff(), "JVMS layouts agree");
1856     assert(newin->jvms()->stkoff() == jvms()->stkoff(), "JVMS layouts agree");
1857     assert(newin->jvms()->monoff() == jvms()->monoff(), "JVMS layouts agree");
1858     assert(newin->jvms()->endoff() == jvms()->endoff(), "JVMS layouts agree");
1859 
1860     // Iterate over my current mapping and the old mapping.
1861     // Where different, insert Phi functions.
1862     // Use any existing Phi functions.
1863     assert(control()->is_Region(), "must be merging to a region");
1864     RegionNode* r = control()->as_Region();
1865 
1866     // Compute where to merge into
1867     // Merge incoming control path
1868     r->init_req(pnum, newin->control());
1869 
1870     if (pnum == 1) {            // Last merge for this Region?
1871       if (!block()->flow()->is_irreducible_loop_secondary_entry()) {
1872         Node* result = _gvn.transform(r);
1873         if (r != result && TraceOptoParse) {
1874           tty->print_cr("Block #%d replace %d with %d", block()->rpo(), r->_idx, result->_idx);
1875         }
1876       }
1877       record_for_igvn(r);
1878     }
1879 
1880     // Update all the non-control inputs to map:
1881     assert(TypeFunc::Parms == newin->jvms()->locoff(), "parser map should contain only youngest jvms");
1882     bool check_elide_phi = target->is_SEL_backedge(save_block);
1883     PEAState& pred_as = newin->jvms()->alloc_state();
1884     PEAState& as = block()->state();
1885     AllocationStateMerger as_merger(as);
1886 
1887     for (uint j = 1; j < newin->req(); ++j) {
1888       Node* m = map()->in(j);   // Current state of target.
1889       Node* n = newin->in(j);   // Incoming change to target state.
1890       PhiNode* phi;
1891       if (m->is_Phi() && m->as_Phi()->region() == r)
1892         phi = m->as_Phi();
1893       else
1894         phi = nullptr;
1895       if (m != n) {             // Different; must merge
1896         switch (j) {
1897         // Frame pointer and Return Address never changes
1898         case TypeFunc::FramePtr:// Drop m, use the original value
1899         case TypeFunc::ReturnAdr:
1900           break;
1901         case TypeFunc::Memory:  // Merge inputs to the MergeMem node
1902           assert(phi == nullptr, "the merge contains phis, not vice versa");
1903           merge_memory_edges(n->as_MergeMem(), pnum, nophi);
1904           continue;
1905         default:                // All normal stuff
1906           if (phi == nullptr) {
1907             const JVMState* jvms = map()->jvms();
1908             if (EliminateNestedLocks &&
1909                 jvms->is_mon(j) && jvms->is_monitor_box(j)) {
1910               // BoxLock nodes are not commoning.
1911               // Use old BoxLock node as merged box.
1912               assert(newin->jvms()->is_monitor_box(j), "sanity");
1913               // This assert also tests that nodes are BoxLock.
1914               assert(BoxLockNode::same_slot(n, m), "sanity");
1915               C->gvn_replace_by(n, m);
1916             } else if (!check_elide_phi || !target->can_elide_SEL_phi(j)) {
1917               phi = ensure_phi(j, nophi);
1918 
1919               // We merges allocation state according to 5.2.4 of the dissertation
1920               // Becase C2 Parse is merging basic blocks, we have to intercept some phi creation or
1921               // PEA MergeProcessor creates duplicated phi nodes.
1922               if (DoPartialEscapeAnalysis && phi != nullptr) {
1923                 PartialEscapeAnalysis* pea = PEA();
1924                 as_merger.merge_at_phi_creation(pea, pred_as, phi, m, n);
1925               } // DoPartialEscapeAnalysis
1926             }
1927           }
1928           break;
1929         }
1930       }
1931       // At this point, n might be top if:
1932       //  - there is no phi (because TypeFlow detected a conflict), or
1933       //  - the corresponding control edges is top (a dead incoming path)
1934       // It is a bug if we create a phi which sees a garbage value on a live path.
1935 
1936       if (phi != nullptr) {
1937         assert(n != top() || r->in(pnum) == top(), "live value must not be garbage");
1938         assert(phi->region() == r, "");
1939 
1940         phi->set_req(pnum, n);  // Then add 'n' to the merge
1941         if (pnum == PhiNode::Input) {
1942           // Last merge for this Phi.
1943           // So far, Phis have had a reasonable type from ciTypeFlow.
1944           // Now _gvn will join that with the meet of current inputs.
1945           // BOTTOM is never permissible here, 'cause pessimistically
1946           // Phis of pointers cannot lose the basic pointer type.
1947           debug_only(const Type* bt1 = phi->bottom_type());
1948           assert(bt1 != Type::BOTTOM, "should not be building conflict phis");
1949           map()->set_req(j, _gvn.transform(phi));
1950           debug_only(const Type* bt2 = phi->bottom_type());
1951           assert(bt2->higher_equal_speculative(bt1), "must be consistent with type-flow");
1952           record_for_igvn(phi);
1953         }
1954       }
1955     } // End of for all values to be merged
1956 
1957 
1958     if (DoPartialEscapeAnalysis) {
1959       as_merger.merge(pred_as, this, r, pnum);
1960     }
1961 
1962     if (pnum == PhiNode::Input &&
1963         !r->in(0)) {         // The occasional useless Region
1964       assert(control() == r, "");
1965       set_control(r->nonnull_req());
1966     }
1967 
1968     map()->merge_replaced_nodes_with(newin);
1969 
1970 #ifdef ASSERT
1971     block()->state().validate();
1972 #endif
1973     // newin has been subsumed into the lazy merge, and is now dead.
1974     set_block(save_block);
1975 
1976     stop();                     // done with this guy, for now
1977   }
1978 
1979   // Done with this parser state.
1980   assert(stopped(), "");
1981 }
1982 
1983 
1984 //--------------------------merge_memory_edges---------------------------------
1985 void Parse::merge_memory_edges(MergeMemNode* n, int pnum, bool nophi) {
1986   // (nophi means we must not create phis, because we already parsed here)
1987   assert(n != nullptr, "");
1988   // Merge the inputs to the MergeMems
1989   MergeMemNode* m = merged_memory();
1990 
1991   assert(control()->is_Region(), "must be merging to a region");
1992   RegionNode* r = control()->as_Region();
1993 
1994   PhiNode* base = nullptr;
1995   MergeMemNode* remerge = nullptr;
1996   for (MergeMemStream mms(m, n); mms.next_non_empty2(); ) {
1997     Node *p = mms.force_memory();
1998     Node *q = mms.memory2();
1999     if (mms.is_empty() && nophi) {
2000       // Trouble:  No new splits allowed after a loop body is parsed.
2001       // Instead, wire the new split into a MergeMem on the backedge.
2002       // The optimizer will sort it out, slicing the phi.
2003       if (remerge == nullptr) {
2004         guarantee(base != nullptr, "");
2005         assert(base->in(0) != nullptr, "should not be xformed away");
2006         remerge = MergeMemNode::make(base->in(pnum));
2007         gvn().set_type(remerge, Type::MEMORY);
2008         base->set_req(pnum, remerge);
2009       }
2010       remerge->set_memory_at(mms.alias_idx(), q);
2011       continue;
2012     }
2013     assert(!q->is_MergeMem(), "");
2014     PhiNode* phi;
2015     if (p != q) {
2016       phi = ensure_memory_phi(mms.alias_idx(), nophi);
2017     } else {
2018       if (p->is_Phi() && p->as_Phi()->region() == r)
2019         phi = p->as_Phi();
2020       else
2021         phi = nullptr;
2022     }
2023     // Insert q into local phi
2024     if (phi != nullptr) {
2025       assert(phi->region() == r, "");
2026       p = phi;
2027       phi->set_req(pnum, q);
2028       if (mms.at_base_memory()) {
2029         base = phi;  // delay transforming it
2030       } else if (pnum == 1) {
2031         record_for_igvn(phi);
2032         p = _gvn.transform(phi);
2033       }
2034       mms.set_memory(p);// store back through the iterator
2035     }
2036   }
2037   // Transform base last, in case we must fiddle with remerging.
2038   if (base != nullptr && pnum == 1) {
2039     record_for_igvn(base);
2040     m->set_base_memory(_gvn.transform(base));
2041   }
2042 }
2043 
2044 
2045 //------------------------ensure_phis_everywhere-------------------------------
2046 void Parse::ensure_phis_everywhere() {
2047   ensure_phi(TypeFunc::I_O);
2048 
2049   // Ensure a phi on all currently known memories.
2050   for (MergeMemStream mms(merged_memory()); mms.next_non_empty(); ) {
2051     ensure_memory_phi(mms.alias_idx());
2052     debug_only(mms.set_memory());  // keep the iterator happy
2053   }
2054 
2055   // Note:  This is our only chance to create phis for memory slices.
2056   // If we miss a slice that crops up later, it will have to be
2057   // merged into the base-memory phi that we are building here.
2058   // Later, the optimizer will comb out the knot, and build separate
2059   // phi-loops for each memory slice that matters.
2060 
2061   // Monitors must nest nicely and not get confused amongst themselves.
2062   // Phi-ify everything up to the monitors, though.
2063   uint monoff = map()->jvms()->monoff();
2064   uint nof_monitors = map()->jvms()->nof_monitors();
2065 
2066   assert(TypeFunc::Parms == map()->jvms()->locoff(), "parser map should contain only youngest jvms");
2067   bool check_elide_phi = block()->is_SEL_head();
2068   for (uint i = TypeFunc::Parms; i < monoff; i++) {
2069     if (!check_elide_phi || !block()->can_elide_SEL_phi(i)) {
2070       ensure_phi(i);
2071     }
2072   }
2073 
2074   // Even monitors need Phis, though they are well-structured.
2075   // This is true for OSR methods, and also for the rare cases where
2076   // a monitor object is the subject of a replace_in_map operation.
2077   // See bugs 4426707 and 5043395.
2078   for (uint m = 0; m < nof_monitors; m++) {
2079     ensure_phi(map()->jvms()->monitor_obj_offset(m));
2080   }
2081 }
2082 
2083 
2084 //-----------------------------add_new_path------------------------------------
2085 // Add a previously unaccounted predecessor to this block.
2086 int Parse::Block::add_new_path() {
2087   // If there is no map, return the lowest unused path number.
2088   if (!is_merged())  return pred_count()+1;  // there will be a map shortly
2089 
2090   SafePointNode* map = start_map();
2091   if (!map->control()->is_Region())
2092     return pred_count()+1;  // there may be a region some day
2093   RegionNode* r = map->control()->as_Region();
2094 
2095   // Add new path to the region.
2096   const uint pnum = r->req();
2097   r->add_req(nullptr);
2098 
2099   for (DUIterator_Fast imax, i = r->fast_outs(imax); i < imax; i++) {
2100     Node* n = r->fast_out(i);
2101 
2102     if (n->is_MergeMem()) {
2103       // Ensure a phi on all currently known memories.
2104       for (MergeMemStream mms(n->as_MergeMem()); mms.next_non_empty(); ) {
2105         Node* phi = mms.memory();
2106         if (phi->is_Phi() && phi->as_Phi()->region() == r && phi->req() <= pnum) {
2107           assert(phi->req() == pnum, "must be same size as region");
2108           phi->add_req(nullptr);
2109         }
2110       }
2111     } else if (n->is_Phi() && n->as_Phi()->region() == r && n->req() <= pnum) {
2112       assert(n->req() == pnum, "must be same size as region");
2113       n->add_req(nullptr);
2114     }
2115   }
2116 
2117   return pnum;
2118 }
2119 
2120 //------------------------------ensure_phi-------------------------------------
2121 // Turn the idx'th entry of the current map into a Phi
2122 PhiNode *Parse::ensure_phi(int idx, bool nocreate) {
2123   SafePointNode* map = this->map();
2124   Node* region = map->control();
2125   assert(region->is_Region(), "");
2126 
2127   Node* o = map->in(idx);
2128   assert(o != nullptr, "");
2129 
2130   if (o == top())  return nullptr; // TOP always merges into TOP
2131 
2132   if (o->is_Phi() && o->as_Phi()->region() == region) {
2133     return o->as_Phi();
2134   }
2135 
2136   // Now use a Phi here for merging
2137   assert(!nocreate, "Cannot build a phi for a block already parsed.");
2138   const JVMState* jvms = map->jvms();
2139   const Type* t = nullptr;
2140   if (jvms->is_loc(idx)) {
2141     t = block()->local_type_at(idx - jvms->locoff());
2142   } else if (jvms->is_stk(idx)) {
2143     t = block()->stack_type_at(idx - jvms->stkoff());
2144   } else if (jvms->is_mon(idx)) {
2145     assert(!jvms->is_monitor_box(idx), "no phis for boxes");
2146     t = TypeInstPtr::BOTTOM; // this is sufficient for a lock object
2147   } else if ((uint)idx < TypeFunc::Parms) {
2148     t = o->bottom_type();  // Type::RETURN_ADDRESS or such-like.
2149   } else {
2150     assert(false, "no type information for this phi");
2151   }
2152 
2153   // If the type falls to bottom, then this must be a local that
2154   // is mixing ints and oops or some such.  Forcing it to top
2155   // makes it go dead.
2156   if (t == Type::BOTTOM) {
2157     map->set_req(idx, top());
2158     return nullptr;
2159   }
2160 
2161   // Do not create phis for top either.
2162   // A top on a non-null control flow must be an unused even after the.phi.
2163   if (t == Type::TOP || t == Type::HALF) {
2164     map->set_req(idx, top());
2165     return nullptr;
2166   }
2167 
2168   PhiNode* phi = PhiNode::make(region, o, t);
2169   gvn().set_type(phi, t);
2170   if (C->do_escape_analysis()) record_for_igvn(phi);
2171   map->set_req(idx, phi);
2172 
2173   return phi;
2174 }
2175 
2176 //--------------------------ensure_memory_phi----------------------------------
2177 // Turn the idx'th slice of the current memory into a Phi
2178 PhiNode *Parse::ensure_memory_phi(int idx, bool nocreate) {
2179   MergeMemNode* mem = merged_memory();
2180   Node* region = control();
2181   assert(region->is_Region(), "");
2182 
2183   Node *o = (idx == Compile::AliasIdxBot)? mem->base_memory(): mem->memory_at(idx);
2184   assert(o != nullptr && o != top(), "");
2185 
2186   PhiNode* phi;
2187   if (o->is_Phi() && o->as_Phi()->region() == region) {
2188     phi = o->as_Phi();
2189     if (phi == mem->base_memory() && idx >= Compile::AliasIdxRaw) {
2190       // clone the shared base memory phi to make a new memory split
2191       assert(!nocreate, "Cannot build a phi for a block already parsed.");
2192       const Type* t = phi->bottom_type();
2193       const TypePtr* adr_type = C->get_adr_type(idx);
2194       phi = phi->slice_memory(adr_type);
2195       gvn().set_type(phi, t);
2196     }
2197     return phi;
2198   }
2199 
2200   // Now use a Phi here for merging
2201   assert(!nocreate, "Cannot build a phi for a block already parsed.");
2202   const Type* t = o->bottom_type();
2203   const TypePtr* adr_type = C->get_adr_type(idx);
2204   phi = PhiNode::make(region, o, t, adr_type);
2205   gvn().set_type(phi, t);
2206   if (idx == Compile::AliasIdxBot)
2207     mem->set_base_memory(phi);
2208   else
2209     mem->set_memory_at(idx, phi);
2210   return phi;
2211 }
2212 
2213 //------------------------------call_register_finalizer-----------------------
2214 // Check the klass of the receiver and call register_finalizer if the
2215 // class need finalization.
2216 void Parse::call_register_finalizer() {
2217   Node* receiver = local(0);
2218   assert(receiver != nullptr && receiver->bottom_type()->isa_instptr() != nullptr,
2219          "must have non-null instance type");
2220 
2221   const TypeInstPtr *tinst = receiver->bottom_type()->isa_instptr();
2222   if (tinst != nullptr && tinst->is_loaded() && !tinst->klass_is_exact()) {
2223     // The type isn't known exactly so see if CHA tells us anything.
2224     ciInstanceKlass* ik = tinst->instance_klass();
2225     if (!Dependencies::has_finalizable_subclass(ik)) {
2226       // No finalizable subclasses so skip the dynamic check.
2227       C->dependencies()->assert_has_no_finalizable_subclasses(ik);
2228       return;
2229     }
2230   }
2231 
2232   // Insert a dynamic test for whether the instance needs
2233   // finalization.  In general this will fold up since the concrete
2234   // class is often visible so the access flags are constant.
2235   Node* klass_addr = basic_plus_adr( receiver, receiver, oopDesc::klass_offset_in_bytes() );
2236   Node* klass = _gvn.transform(LoadKlassNode::make(_gvn, nullptr, immutable_memory(), klass_addr, TypeInstPtr::KLASS));
2237 
2238   Node* access_flags_addr = basic_plus_adr(klass, klass, in_bytes(Klass::access_flags_offset()));
2239   Node* access_flags = make_load(nullptr, access_flags_addr, TypeInt::INT, T_INT, MemNode::unordered);
2240 
2241   Node* mask  = _gvn.transform(new AndINode(access_flags, intcon(JVM_ACC_HAS_FINALIZER)));
2242   Node* check = _gvn.transform(new CmpINode(mask, intcon(0)));
2243   Node* test  = _gvn.transform(new BoolNode(check, BoolTest::ne));
2244 
2245   IfNode* iff = create_and_map_if(control(), test, PROB_MAX, COUNT_UNKNOWN);
2246 
2247   RegionNode* result_rgn = new RegionNode(3);
2248   record_for_igvn(result_rgn);
2249 
2250   Node *skip_register = _gvn.transform(new IfFalseNode(iff));
2251   result_rgn->init_req(1, skip_register);
2252 
2253   Node *needs_register = _gvn.transform(new IfTrueNode(iff));
2254   set_control(needs_register);
2255   if (stopped()) {
2256     // There is no slow path.
2257     result_rgn->init_req(2, top());
2258   } else {
2259     Node *call = make_runtime_call(RC_NO_LEAF,
2260                                    OptoRuntime::register_finalizer_Type(),
2261                                    OptoRuntime::register_finalizer_Java(),
2262                                    nullptr, TypePtr::BOTTOM,
2263                                    receiver);
2264     make_slow_call_ex(call, env()->Throwable_klass(), true);
2265 
2266     Node* fast_io  = call->in(TypeFunc::I_O);
2267     Node* fast_mem = call->in(TypeFunc::Memory);
2268     // These two phis are pre-filled with copies of of the fast IO and Memory
2269     Node* io_phi   = PhiNode::make(result_rgn, fast_io,  Type::ABIO);
2270     Node* mem_phi  = PhiNode::make(result_rgn, fast_mem, Type::MEMORY, TypePtr::BOTTOM);
2271 
2272     result_rgn->init_req(2, control());
2273     io_phi    ->init_req(2, i_o());
2274     mem_phi   ->init_req(2, reset_memory());
2275 
2276     set_all_memory( _gvn.transform(mem_phi) );
2277     set_i_o(        _gvn.transform(io_phi) );
2278   }
2279 
2280   set_control( _gvn.transform(result_rgn) );
2281 }
2282 
2283 // Add check to deoptimize once holder klass is fully initialized.
2284 void Parse::clinit_deopt() {
2285   assert(C->has_method(), "only for normal compilations");
2286   assert(depth() == 1, "only for main compiled method");
2287   assert(is_normal_parse(), "no barrier needed on osr entry");
2288   assert(!method()->holder()->is_not_initialized(), "initialization should have been started");
2289 
2290   set_parse_bci(0);
2291 
2292   Node* holder = makecon(TypeKlassPtr::make(method()->holder(), Type::trust_interfaces));
2293   guard_klass_being_initialized(holder);
2294 }
2295 
2296 // Add check to deoptimize if RTM state is not ProfileRTM
2297 void Parse::rtm_deopt() {
2298 #if INCLUDE_RTM_OPT
2299   if (C->profile_rtm()) {
2300     assert(C->has_method(), "only for normal compilations");
2301     assert(!C->method()->method_data()->is_empty(), "MDO is needed to record RTM state");
2302     assert(depth() == 1, "generate check only for main compiled method");
2303 
2304     // Set starting bci for uncommon trap.
2305     set_parse_bci(is_osr_parse() ? osr_bci() : 0);
2306 
2307     // Load the rtm_state from the MethodData.
2308     const TypePtr* adr_type = TypeMetadataPtr::make(C->method()->method_data());
2309     Node* mdo = makecon(adr_type);
2310     int offset = in_bytes(MethodData::rtm_state_offset());
2311     Node* adr_node = basic_plus_adr(mdo, mdo, offset);
2312     Node* rtm_state = make_load(control(), adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
2313 
2314     // Separate Load from Cmp by Opaque.
2315     // In expand_macro_nodes() it will be replaced either
2316     // with this load when there are locks in the code
2317     // or with ProfileRTM (cmp->in(2)) otherwise so that
2318     // the check will fold.
2319     Node* profile_state = makecon(TypeInt::make(ProfileRTM));
2320     Node* opq   = _gvn.transform( new Opaque3Node(C, rtm_state, Opaque3Node::RTM_OPT) );
2321     Node* chk   = _gvn.transform( new CmpINode(opq, profile_state) );
2322     Node* tst   = _gvn.transform( new BoolNode(chk, BoolTest::eq) );
2323     // Branch to failure if state was changed
2324     { BuildCutout unless(this, tst, PROB_ALWAYS);
2325       uncommon_trap(Deoptimization::Reason_rtm_state_change,
2326                     Deoptimization::Action_make_not_entrant);
2327     }
2328   }
2329 #endif
2330 }
2331 
2332 //------------------------------return_current---------------------------------
2333 // Append current _map to _exit_return
2334 void Parse::return_current(Node* value) {
2335   if (RegisterFinalizersAtInit &&
2336       method()->intrinsic_id() == vmIntrinsics::_Object_init) {
2337     call_register_finalizer();
2338   }
2339 
2340   // Do not set_parse_bci, so that return goo is credited to the return insn.
2341   set_bci(InvocationEntryBci);
2342   if (method()->is_synchronized() && GenerateSynchronizationCode) {
2343     shared_unlock(_synch_lock->box_node(), _synch_lock->obj_node());
2344   }
2345   if (C->env()->dtrace_method_probes()) {
2346     make_dtrace_method_exit(method());
2347   }
2348   SafePointNode* exit_return = _exits.map();
2349   exit_return->in( TypeFunc::Control  )->add_req( control() );
2350   exit_return->in( TypeFunc::I_O      )->add_req( i_o    () );
2351   Node *mem = exit_return->in( TypeFunc::Memory   );
2352   for (MergeMemStream mms(mem->as_MergeMem(), merged_memory()); mms.next_non_empty2(); ) {
2353     if (mms.is_empty()) {
2354       // get a copy of the base memory, and patch just this one input
2355       const TypePtr* adr_type = mms.adr_type(C);
2356       Node* phi = mms.force_memory()->as_Phi()->slice_memory(adr_type);
2357       assert(phi->as_Phi()->region() == mms.base_memory()->in(0), "");
2358       gvn().set_type_bottom(phi);
2359       phi->del_req(phi->req()-1);  // prepare to re-patch
2360       mms.set_memory(phi);
2361     }
2362     mms.memory()->add_req(mms.memory2());
2363   }
2364 
2365   // frame pointer is always same, already captured
2366   if (value != nullptr) {
2367     // If returning oops to an interface-return, there is a silent free
2368     // cast from oop to interface allowed by the Verifier.  Make it explicit
2369     // here.
2370     Node* phi = _exits.argument(0);
2371     phi->add_req(value);
2372   }
2373 
2374   if (_first_return++ == 0) {
2375     _exits.map()->transfer_replaced_nodes_from(map(), _new_idx);
2376     // copy assignment
2377     _exits.jvms()->alloc_state() = jvms()->alloc_state();
2378   } else {
2379     _exits.map()->merge_replaced_nodes_with(map());
2380 
2381     if (DoPartialEscapeAnalysis) {
2382       PEAState& as =_exits.jvms()->alloc_state();
2383       PEAState& newin = jvms()->alloc_state();
2384       AllocationStateMerger mp(as);
2385       // if value is a tracking object and PEA needs to create a phi node to merge it,
2386       // we need to use _exits.argument(0)
2387       ObjID obj = PEA()->is_alias(value);
2388       if (as.contains(obj) && newin.contains(obj)) {
2389         Node* phi = _exits.argument(0);
2390         mp.merge_at_phi_creation(PEA(), newin, phi->as_Phi(), phi->in(_first_return-1), value);
2391       }
2392       mp.merge(newin, &_exits, _exits.control()->as_Region(), _first_return);
2393     }
2394   }
2395 
2396   stop_and_kill_map();          // This CFG path dies here
2397 }
2398 
2399 
2400 //------------------------------add_safepoint----------------------------------
2401 void Parse::add_safepoint() {
2402   uint parms = TypeFunc::Parms+1;
2403 
2404   // Clear out dead values from the debug info.
2405   kill_dead_locals();
2406 
2407   // Clone the JVM State
2408   SafePointNode *sfpnt = new SafePointNode(parms, nullptr);
2409 
2410   // Capture memory state BEFORE a SafePoint.  Since we can block at a
2411   // SafePoint we need our GC state to be safe; i.e. we need all our current
2412   // write barriers (card marks) to not float down after the SafePoint so we
2413   // must read raw memory.  Likewise we need all oop stores to match the card
2414   // marks.  If deopt can happen, we need ALL stores (we need the correct JVM
2415   // state on a deopt).
2416 
2417   // We do not need to WRITE the memory state after a SafePoint.  The control
2418   // edge will keep card-marks and oop-stores from floating up from below a
2419   // SafePoint and our true dependency added here will keep them from floating
2420   // down below a SafePoint.
2421 
2422   // Clone the current memory state
2423   Node* mem = MergeMemNode::make(map()->memory());
2424 
2425   mem = _gvn.transform(mem);
2426 
2427   // Pass control through the safepoint
2428   sfpnt->init_req(TypeFunc::Control  , control());
2429   // Fix edges normally used by a call
2430   sfpnt->init_req(TypeFunc::I_O      , top() );
2431   sfpnt->init_req(TypeFunc::Memory   , mem   );
2432   sfpnt->init_req(TypeFunc::ReturnAdr, top() );
2433   sfpnt->init_req(TypeFunc::FramePtr , top() );
2434 
2435   // Create a node for the polling address
2436   Node *polladr;
2437   Node *thread = _gvn.transform(new ThreadLocalNode());
2438   Node *polling_page_load_addr = _gvn.transform(basic_plus_adr(top(), thread, in_bytes(JavaThread::polling_page_offset())));
2439   polladr = make_load(control(), polling_page_load_addr, TypeRawPtr::BOTTOM, T_ADDRESS, Compile::AliasIdxRaw, MemNode::unordered);
2440   sfpnt->init_req(TypeFunc::Parms+0, _gvn.transform(polladr));
2441 
2442   // Fix up the JVM State edges
2443   add_safepoint_edges(sfpnt);
2444   Node *transformed_sfpnt = _gvn.transform(sfpnt);
2445   set_control(transformed_sfpnt);
2446 
2447   // Provide an edge from root to safepoint.  This makes the safepoint
2448   // appear useful until the parse has completed.
2449   if (transformed_sfpnt->is_SafePoint()) {
2450     assert(C->root() != nullptr, "Expect parse is still valid");
2451     C->root()->add_prec(transformed_sfpnt);
2452   }
2453 }
2454 
2455 #ifndef PRODUCT
2456 //------------------------show_parse_info--------------------------------------
2457 void Parse::show_parse_info() {
2458   InlineTree* ilt = nullptr;
2459   if (C->ilt() != nullptr) {
2460     JVMState* caller_jvms = is_osr_parse() ? caller()->caller() : caller();
2461     ilt = InlineTree::find_subtree_from_root(C->ilt(), caller_jvms, method());
2462   }
2463   if (PrintCompilation && Verbose) {
2464     if (depth() == 1) {
2465       if( ilt->count_inlines() ) {
2466         tty->print("    __inlined %d (%d bytes)", ilt->count_inlines(),
2467                      ilt->count_inline_bcs());
2468         tty->cr();
2469       }
2470     } else {
2471       if (method()->is_synchronized())         tty->print("s");
2472       if (method()->has_exception_handlers())  tty->print("!");
2473       // Check this is not the final compiled version
2474       if (C->trap_can_recompile()) {
2475         tty->print("-");
2476       } else {
2477         tty->print(" ");
2478       }
2479       method()->print_short_name();
2480       if (is_osr_parse()) {
2481         tty->print(" @ %d", osr_bci());
2482       }
2483       tty->print(" (%d bytes)",method()->code_size());
2484       if (ilt->count_inlines()) {
2485         tty->print(" __inlined %d (%d bytes)", ilt->count_inlines(),
2486                    ilt->count_inline_bcs());
2487       }
2488       tty->cr();
2489     }
2490   }
2491   if (PrintOpto && (depth() == 1 || PrintOptoInlining)) {
2492     // Print that we succeeded; suppress this message on the first osr parse.
2493 
2494     if (method()->is_synchronized())         tty->print("s");
2495     if (method()->has_exception_handlers())  tty->print("!");
2496     // Check this is not the final compiled version
2497     if (C->trap_can_recompile() && depth() == 1) {
2498       tty->print("-");
2499     } else {
2500       tty->print(" ");
2501     }
2502     if( depth() != 1 ) { tty->print("   "); }  // missing compile count
2503     for (int i = 1; i < depth(); ++i) { tty->print("  "); }
2504     method()->print_short_name();
2505     if (is_osr_parse()) {
2506       tty->print(" @ %d", osr_bci());
2507     }
2508     if (ilt->caller_bci() != -1) {
2509       tty->print(" @ %d", ilt->caller_bci());
2510     }
2511     tty->print(" (%d bytes)",method()->code_size());
2512     if (ilt->count_inlines()) {
2513       tty->print(" __inlined %d (%d bytes)", ilt->count_inlines(),
2514                  ilt->count_inline_bcs());
2515     }
2516     tty->cr();
2517   }
2518 }
2519 
2520 
2521 //------------------------------dump-------------------------------------------
2522 // Dump information associated with the bytecodes of current _method
2523 void Parse::dump() {
2524   if( method() != nullptr ) {
2525     // Iterate over bytecodes
2526     ciBytecodeStream iter(method());
2527     for( Bytecodes::Code bc = iter.next(); bc != ciBytecodeStream::EOBC() ; bc = iter.next() ) {
2528       dump_bci( iter.cur_bci() );
2529       tty->cr();
2530     }
2531   }
2532 }
2533 
2534 // Dump information associated with a byte code index, 'bci'
2535 void Parse::dump_bci(int bci) {
2536   // Output info on merge-points, cloning, and within _jsr..._ret
2537   // NYI
2538   tty->print(" bci:%d", bci);
2539 }
2540 
2541 #endif