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