1 /*
   2  * Copyright (c) 2000, 2026, 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 "ci/bcEscapeAnalyzer.hpp"
  26 #include "ci/ciCallSite.hpp"
  27 #include "ci/ciMemberName.hpp"
  28 #include "ci/ciMethodHandle.hpp"
  29 #include "ci/ciObjArray.hpp"
  30 #include "classfile/javaClasses.hpp"
  31 #include "compiler/compileLog.hpp"
  32 #include "opto/addnode.hpp"
  33 #include "opto/callGenerator.hpp"
  34 #include "opto/callnode.hpp"
  35 #include "opto/castnode.hpp"
  36 #include "opto/cfgnode.hpp"
  37 #include "opto/parse.hpp"
  38 #include "opto/rootnode.hpp"
  39 #include "opto/runtime.hpp"
  40 #include "opto/subnode.hpp"
  41 #include "runtime/os.inline.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "utilities/debug.hpp"
  44 
  45 // Utility function.
  46 const TypeFunc* CallGenerator::tf() const {
  47   return TypeFunc::make(method());
  48 }
  49 
  50 bool CallGenerator::is_inlined_method_handle_intrinsic(JVMState* jvms, ciMethod* m) {
  51   return is_inlined_method_handle_intrinsic(jvms->method(), jvms->bci(), m);
  52 }
  53 
  54 bool CallGenerator::is_inlined_method_handle_intrinsic(ciMethod* caller, int bci, ciMethod* m) {
  55   ciMethod* symbolic_info = caller->get_method_at_bci(bci);
  56   return is_inlined_method_handle_intrinsic(symbolic_info, m);
  57 }
  58 
  59 bool CallGenerator::is_inlined_method_handle_intrinsic(ciMethod* symbolic_info, ciMethod* m) {
  60   return symbolic_info->is_method_handle_intrinsic() && !m->is_method_handle_intrinsic();
  61 }
  62 
  63 //-----------------------------ParseGenerator---------------------------------
  64 // Internal class which handles all direct bytecode traversal.
  65 class ParseGenerator : public InlineCallGenerator {
  66 private:
  67   bool  _is_osr;
  68   float _expected_uses;
  69 
  70 public:
  71   ParseGenerator(ciMethod* method, float expected_uses, bool is_osr = false)
  72     : InlineCallGenerator(method)
  73   {
  74     _is_osr        = is_osr;
  75     _expected_uses = expected_uses;
  76     assert(InlineTree::check_can_parse(method) == nullptr, "parse must be possible");
  77   }
  78 
  79   virtual bool      is_parse() const           { return true; }
  80   virtual JVMState* generate(JVMState* jvms);
  81   bool              is_osr() const             { return _is_osr; }
  82 
  83 };
  84 
  85 JVMState* ParseGenerator::generate(JVMState* jvms) {
  86   Compile* C = Compile::current();
  87 
  88   if (is_osr()) {
  89     // The JVMS for a OSR has a single argument (see its TypeFunc).
  90     assert(jvms->depth() == 1, "no inline OSR");
  91   }
  92 
  93   if (C->failing()) {
  94     return nullptr;  // bailing out of the compile; do not try to parse
  95   }
  96 
  97   Parse parser(jvms, method(), _expected_uses);
  98   if (C->failing()) return nullptr;
  99 
 100   // Grab signature for matching/allocation
 101   GraphKit& exits = parser.exits();
 102 
 103   if (C->failing()) {
 104     while (exits.pop_exception_state() != nullptr) ;
 105     return nullptr;
 106   }
 107 
 108   assert(exits.jvms()->same_calls_as(jvms), "sanity");
 109 
 110   // Simply return the exit state of the parser,
 111   // augmented by any exceptional states.
 112   return exits.transfer_exceptions_into_jvms();
 113 }
 114 
 115 //---------------------------DirectCallGenerator------------------------------
 116 // Internal class which handles all out-of-line calls w/o receiver type checks.
 117 class DirectCallGenerator : public CallGenerator {
 118  private:
 119   CallStaticJavaNode* _call_node;
 120   // Force separate memory and I/O projections for the exceptional
 121   // paths to facilitate late inlinig.
 122   bool                _separate_io_proj;
 123 
 124 protected:
 125   void set_call_node(CallStaticJavaNode* call) { _call_node = call; }
 126 
 127  public:
 128   DirectCallGenerator(ciMethod* method, bool separate_io_proj)
 129     : CallGenerator(method),
 130       _separate_io_proj(separate_io_proj)
 131   {
 132   }
 133   virtual JVMState* generate(JVMState* jvms);
 134 
 135   virtual CallNode* call_node() const { return _call_node; }
 136   virtual CallGenerator* with_call_node(CallNode* call) {
 137     DirectCallGenerator* dcg = new DirectCallGenerator(method(), _separate_io_proj);
 138     dcg->set_call_node(call->as_CallStaticJava());
 139     return dcg;
 140   }
 141 };
 142 
 143 JVMState* DirectCallGenerator::generate(JVMState* jvms) {
 144   GraphKit kit(jvms);
 145   bool is_static = method()->is_static();
 146   address target = is_static ? SharedRuntime::get_resolve_static_call_stub()
 147                              : SharedRuntime::get_resolve_opt_virtual_call_stub();
 148 
 149   if (kit.C->log() != nullptr) {
 150     kit.C->log()->elem("direct_call bci='%d'", jvms->bci());
 151   }
 152 
 153   CallStaticJavaNode* call = new CallStaticJavaNode(kit.C, tf(), target, method());
 154   if (is_inlined_method_handle_intrinsic(jvms, method())) {
 155     // To be able to issue a direct call and skip a call to MH.linkTo*/invokeBasic adapter,
 156     // additional information about the method being invoked should be attached
 157     // to the call site to make resolution logic work
 158     // (see SharedRuntime::resolve_static_call_C).
 159     call->set_override_symbolic_info(true);
 160   }
 161   _call_node = call;  // Save the call node in case we need it later
 162   if (!is_static) {
 163     // Make an explicit receiver null_check as part of this call.
 164     // Since we share a map with the caller, his JVMS gets adjusted.
 165     kit.null_check_receiver_before_call(method());
 166     if (kit.stopped()) {
 167       // And dump it back to the caller, decorated with any exceptions:
 168       return kit.transfer_exceptions_into_jvms();
 169     }
 170     // Mark the call node as virtual, sort of:
 171     call->set_optimized_virtual(true);
 172   }
 173   kit.set_arguments_for_java_call(call);
 174   kit.set_edges_for_java_call(call, false, _separate_io_proj);
 175   Node* ret = kit.set_results_for_java_call(call, _separate_io_proj);
 176   if (is_late_inline() && !call->is_boxing_method() && ret->is_Proj()) {
 177     // If late inlining for this call happens in a dead part of the graph it can leave a dead loop behind
 178     ret->mark_not_dead_loop_safe();
 179   }
 180   kit.push_node(method()->return_type()->basic_type(), ret);
 181   return kit.transfer_exceptions_into_jvms();
 182 }
 183 
 184 //--------------------------VirtualCallGenerator------------------------------
 185 // Internal class which handles all out-of-line calls checking receiver type.
 186 class VirtualCallGenerator : public CallGenerator {
 187 private:
 188   int _vtable_index;
 189   bool _separate_io_proj;
 190   CallDynamicJavaNode* _call_node;
 191 
 192 protected:
 193   void set_call_node(CallDynamicJavaNode* call) { _call_node = call; }
 194 
 195 public:
 196   VirtualCallGenerator(ciMethod* method, int vtable_index, bool separate_io_proj)
 197     : CallGenerator(method), _vtable_index(vtable_index), _separate_io_proj(separate_io_proj), _call_node(nullptr)
 198   {
 199     assert(vtable_index == Method::invalid_vtable_index ||
 200            vtable_index >= 0, "either invalid or usable");
 201   }
 202   virtual bool      is_virtual() const          { return true; }
 203   virtual JVMState* generate(JVMState* jvms);
 204 
 205   virtual CallNode* call_node() const { return _call_node; }
 206   int vtable_index() const { return _vtable_index; }
 207 
 208   virtual CallGenerator* with_call_node(CallNode* call) {
 209     VirtualCallGenerator* cg = new VirtualCallGenerator(method(), _vtable_index, _separate_io_proj);
 210     cg->set_call_node(call->as_CallDynamicJava());
 211     return cg;
 212   }
 213 };
 214 
 215 JVMState* VirtualCallGenerator::generate(JVMState* jvms) {
 216   GraphKit kit(jvms);
 217   Node* receiver = kit.argument(0);
 218 
 219   if (kit.C->log() != nullptr) {
 220     kit.C->log()->elem("virtual_call bci='%d'", jvms->bci());
 221   }
 222 
 223   // If the receiver is a constant null, do not torture the system
 224   // by attempting to call through it.  The compile will proceed
 225   // correctly, but may bail out in final_graph_reshaping, because
 226   // the call instruction will have a seemingly deficient out-count.
 227   // (The bailout says something misleading about an "infinite loop".)
 228   if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) {
 229     assert(Bytecodes::is_invoke(kit.java_bc()), "%d: %s", kit.java_bc(), Bytecodes::name(kit.java_bc()));
 230     ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
 231     int arg_size = declared_method->signature()->arg_size_for_bc(kit.java_bc());
 232     kit.inc_sp(arg_size);  // restore arguments
 233     kit.uncommon_trap(Deoptimization::Reason_null_check,
 234                       Deoptimization::Action_none,
 235                       nullptr, "null receiver");
 236     return kit.transfer_exceptions_into_jvms();
 237   }
 238 
 239   // Ideally we would unconditionally do a null check here and let it
 240   // be converted to an implicit check based on profile information.
 241   // However currently the conversion to implicit null checks in
 242   // Block::implicit_null_check() only looks for loads and stores, not calls.
 243   ciMethod *caller = kit.method();
 244   ciMethodData *caller_md = (caller == nullptr) ? nullptr : caller->method_data();
 245   if (!UseInlineCaches || !ImplicitNullChecks || !os::zero_page_read_protected() ||
 246        ((ImplicitNullCheckThreshold > 0) && caller_md &&
 247        (caller_md->trap_count(Deoptimization::Reason_null_check)
 248        >= (uint)ImplicitNullCheckThreshold))) {
 249     // Make an explicit receiver null_check as part of this call.
 250     // Since we share a map with the caller, his JVMS gets adjusted.
 251     receiver = kit.null_check_receiver_before_call(method());
 252     if (kit.stopped()) {
 253       // And dump it back to the caller, decorated with any exceptions:
 254       return kit.transfer_exceptions_into_jvms();
 255     }
 256   }
 257 
 258   assert(!method()->is_static(), "virtual call must not be to static");
 259   assert(!method()->is_final(), "virtual call should not be to final");
 260   assert(!method()->is_private(), "virtual call should not be to private");
 261   assert(_vtable_index == Method::invalid_vtable_index || !UseInlineCaches,
 262          "no vtable calls if +UseInlineCaches ");
 263   address target = SharedRuntime::get_resolve_virtual_call_stub();
 264   // Normal inline cache used for call
 265   CallDynamicJavaNode* call = new CallDynamicJavaNode(tf(), target, method(), _vtable_index);
 266   if (is_inlined_method_handle_intrinsic(jvms, method())) {
 267     // To be able to issue a direct call (optimized virtual or virtual)
 268     // and skip a call to MH.linkTo*/invokeBasic adapter, additional information
 269     // about the method being invoked should be attached to the call site to
 270     // make resolution logic work (see SharedRuntime::resolve_{virtual,opt_virtual}_call_C).
 271     call->set_override_symbolic_info(true);
 272   }
 273   _call_node = call;  // Save the call node in case we need it later
 274 
 275   kit.set_arguments_for_java_call(call);
 276   kit.set_edges_for_java_call(call, false /*must_throw*/, _separate_io_proj);
 277   Node* ret = kit.set_results_for_java_call(call, _separate_io_proj);
 278   if (is_late_inline() && ret->is_Proj()) {
 279     // If late inlining for this call happens in a dead part of the graph it can leave a dead loop behind
 280     ret->mark_not_dead_loop_safe();
 281   }
 282   kit.push_node(method()->return_type()->basic_type(), ret);
 283 
 284   // Represent the effect of an implicit receiver null_check
 285   // as part of this call.  Since we share a map with the caller,
 286   // his JVMS gets adjusted.
 287   kit.cast_not_null(receiver);
 288   return kit.transfer_exceptions_into_jvms();
 289 }
 290 
 291 CallGenerator* CallGenerator::for_inline(ciMethod* m, float expected_uses) {
 292   if (InlineTree::check_can_parse(m) != nullptr)  return nullptr;
 293   return new ParseGenerator(m, expected_uses);
 294 }
 295 
 296 // As a special case, the JVMS passed to this CallGenerator is
 297 // for the method execution already in progress, not just the JVMS
 298 // of the caller.  Thus, this CallGenerator cannot be mixed with others!
 299 CallGenerator* CallGenerator::for_osr(ciMethod* m, int osr_bci) {
 300   if (InlineTree::check_can_parse(m) != nullptr)  return nullptr;
 301   float past_uses = m->interpreter_invocation_count();
 302   float expected_uses = past_uses;
 303   return new ParseGenerator(m, expected_uses, true);
 304 }
 305 
 306 CallGenerator* CallGenerator::for_direct_call(ciMethod* m, bool separate_io_proj) {
 307   assert(!m->is_abstract(), "for_direct_call mismatch");
 308   return new DirectCallGenerator(m, separate_io_proj);
 309 }
 310 
 311 CallGenerator* CallGenerator::for_virtual_call(ciMethod* m, int vtable_index) {
 312   assert(!m->is_static(), "for_virtual_call mismatch");
 313   assert(!m->is_method_handle_intrinsic(), "should be a direct call");
 314   return new VirtualCallGenerator(m, vtable_index, false /*separate_io_projs*/);
 315 }
 316 
 317 // Allow inlining decisions to be delayed
 318 class LateInlineCallGenerator : public DirectCallGenerator {
 319  private:
 320   jlong _unique_id;   // unique id for log compilation
 321   bool _is_pure_call; // a hint that the call doesn't have important side effects to care about
 322 
 323  protected:
 324   CallGenerator* _inline_cg;
 325   virtual bool do_late_inline_check(Compile* C, JVMState* jvms) { return true; }
 326   virtual CallGenerator* inline_cg() const { return _inline_cg; }
 327   virtual bool is_pure_call() const { return _is_pure_call; }
 328 
 329  public:
 330   LateInlineCallGenerator(ciMethod* method, CallGenerator* inline_cg, bool is_pure_call = false) :
 331     DirectCallGenerator(method, true), _unique_id(0), _is_pure_call(is_pure_call), _inline_cg(inline_cg) {}
 332 
 333   virtual bool is_late_inline() const { return true; }
 334 
 335   // Convert the CallStaticJava into an inline
 336   virtual void do_late_inline();
 337 
 338   virtual JVMState* generate(JVMState* jvms) {
 339     Compile *C = Compile::current();
 340 
 341     C->log_inline_id(this);
 342 
 343     // Record that this call site should be revisited once the main
 344     // parse is finished.
 345     if (!is_mh_late_inline()) {
 346       C->add_late_inline(this);
 347     }
 348 
 349     // Emit the CallStaticJava and request separate projections so
 350     // that the late inlining logic can distinguish between fall
 351     // through and exceptional uses of the memory and io projections
 352     // as is done for allocations and macro expansion.
 353     return DirectCallGenerator::generate(jvms);
 354   }
 355 
 356   virtual void set_unique_id(jlong id) {
 357     _unique_id = id;
 358   }
 359 
 360   virtual jlong unique_id() const {
 361     return _unique_id;
 362   }
 363 
 364   virtual CallGenerator* with_call_node(CallNode* call) {
 365     LateInlineCallGenerator* cg = new LateInlineCallGenerator(method(), _inline_cg, _is_pure_call);
 366     cg->set_call_node(call->as_CallStaticJava());
 367     return cg;
 368   }
 369 };
 370 
 371 CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) {
 372   return new LateInlineCallGenerator(method, inline_cg);
 373 }
 374 
 375 class LateInlineMHCallGenerator : public LateInlineCallGenerator {
 376   ciMethod* _caller;
 377   bool _input_not_const;
 378 
 379   virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
 380 
 381  public:
 382   LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) :
 383     LateInlineCallGenerator(callee, nullptr), _caller(caller), _input_not_const(input_not_const) {}
 384 
 385   virtual bool is_mh_late_inline() const { return true; }
 386 
 387   // Convert the CallStaticJava into an inline
 388   virtual void do_late_inline();
 389 
 390   virtual JVMState* generate(JVMState* jvms) {
 391     JVMState* new_jvms = LateInlineCallGenerator::generate(jvms);
 392 
 393     Compile* C = Compile::current();
 394     if (_input_not_const) {
 395       // inlining won't be possible so no need to enqueue right now.
 396       call_node()->set_generator(this);
 397     } else {
 398       C->add_late_inline(this);
 399     }
 400     return new_jvms;
 401   }
 402 
 403   virtual CallGenerator* with_call_node(CallNode* call) {
 404     LateInlineMHCallGenerator* cg = new LateInlineMHCallGenerator(_caller, method(), _input_not_const);
 405     cg->set_call_node(call->as_CallStaticJava());
 406     return cg;
 407   }
 408 };
 409 
 410 bool LateInlineMHCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
 411   // When inlining a virtual call, the null check at the call and the call itself can throw. These 2 paths have different
 412   // expression stacks which causes late inlining to break. The MH invoker is not expected to be called from a method with
 413   // exception handlers. When there is no exception handler, GraphKit::builtin_throw() pops the stack which solves the issue
 414   // of late inlining with exceptions.
 415   assert(!jvms->method()->has_exception_handlers() ||
 416          (method()->intrinsic_id() != vmIntrinsics::_linkToVirtual &&
 417           method()->intrinsic_id() != vmIntrinsics::_linkToInterface), "no exception handler expected");
 418   // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
 419   bool allow_inline = C->inlining_incrementally();
 420   bool input_not_const = true;
 421   CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), allow_inline, input_not_const);
 422   assert(!input_not_const, "sanity"); // shouldn't have been scheduled for inlining in the first place
 423 
 424   if (cg != nullptr) {
 425     if (!allow_inline) {
 426       C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE,
 427                                   "late method handle call resolution");
 428     }
 429     assert(!cg->is_late_inline() || cg->is_mh_late_inline() || cg->is_virtual_late_inline() ||
 430            AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
 431     _inline_cg = cg;
 432     return true;
 433   } else {
 434     // Method handle call which has a constant appendix argument should be either inlined or replaced with a direct call
 435     // unless there's a signature mismatch between caller and callee. If the failure occurs, there's not much to be improved later,
 436     // so don't reinstall the generator to avoid pushing the generator between IGVN and incremental inlining indefinitely.
 437     return false;
 438   }
 439 }
 440 
 441 CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) {
 442   assert(IncrementalInlineMH, "required");
 443   Compile::current()->mark_has_mh_late_inlines();
 444   CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const);
 445   return cg;
 446 }
 447 
 448 // Allow inlining decisions to be delayed
 449 class LateInlineVirtualCallGenerator : public VirtualCallGenerator {
 450  private:
 451   jlong          _unique_id;   // unique id for log compilation
 452   CallGenerator* _inline_cg;
 453   ciMethod*      _callee;
 454   bool           _is_pure_call;
 455   float          _prof_factor;
 456 
 457  protected:
 458   virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
 459   virtual CallGenerator* inline_cg() const { return _inline_cg; }
 460   virtual bool is_pure_call() const { return _is_pure_call; }
 461 
 462  public:
 463   LateInlineVirtualCallGenerator(ciMethod* method, int vtable_index, float prof_factor)
 464   : VirtualCallGenerator(method, vtable_index, true /*separate_io_projs*/),
 465     _unique_id(0), _inline_cg(nullptr), _callee(nullptr), _is_pure_call(false), _prof_factor(prof_factor) {
 466     assert(IncrementalInlineVirtual, "required");
 467   }
 468 
 469   virtual bool is_late_inline() const { return true; }
 470 
 471   virtual bool is_virtual_late_inline() const { return true; }
 472 
 473   // Convert the CallDynamicJava into an inline
 474   virtual void do_late_inline();
 475 
 476   virtual ciMethod* callee_method() {
 477     return _callee;
 478   }
 479 
 480   virtual void set_callee_method(ciMethod* m) {
 481     assert(_callee == nullptr || _callee == m, "repeated inline attempt with different callee");
 482     _callee = m;
 483   }
 484 
 485   virtual JVMState* generate(JVMState* jvms) {
 486     // Emit the CallDynamicJava and request separate projections so
 487     // that the late inlining logic can distinguish between fall
 488     // through and exceptional uses of the memory and io projections
 489     // as is done for allocations and macro expansion.
 490     JVMState* new_jvms = VirtualCallGenerator::generate(jvms);
 491     if (call_node() != nullptr) {
 492       call_node()->set_generator(this);
 493     }
 494     return new_jvms;
 495   }
 496 
 497   virtual void set_unique_id(jlong id) {
 498     _unique_id = id;
 499   }
 500 
 501   virtual jlong unique_id() const {
 502     return _unique_id;
 503   }
 504 
 505   virtual CallGenerator* with_call_node(CallNode* call) {
 506     LateInlineVirtualCallGenerator* cg = new LateInlineVirtualCallGenerator(method(), vtable_index(), _prof_factor);
 507     cg->set_call_node(call->as_CallDynamicJava());
 508     return cg;
 509   }
 510 };
 511 
 512 bool LateInlineVirtualCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
 513   // Method handle linker case is handled in CallDynamicJavaNode::Ideal().
 514   // Unless inlining is performed, _override_symbolic_info bit will be set in DirectCallGenerator::generate().
 515 
 516   // Implicit receiver null checks introduce problems when exception states are combined.
 517   Node* receiver = jvms->map()->argument(jvms, 0);
 518   const Type* recv_type = C->initial_gvn()->type(receiver);
 519   if (recv_type->maybe_null()) {
 520     C->inline_printer()->record(method(), call_node()->jvms(), InliningResult::FAILURE,
 521                                 "late call devirtualization failed (receiver may be null)");
 522     return false;
 523   }
 524   // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
 525   bool allow_inline = C->inlining_incrementally();
 526   if (!allow_inline && _callee->holder()->is_interface()) {
 527     // Don't convert the interface call to a direct call guarded by an interface subtype check.
 528     C->inline_printer()->record(method(), call_node()->jvms(), InliningResult::FAILURE,
 529                                 "late call devirtualization failed (interface call)");
 530     return false;
 531   }
 532   CallGenerator* cg = C->call_generator(_callee,
 533                                         vtable_index(),
 534                                         false /*call_does_dispatch*/,
 535                                         jvms,
 536                                         allow_inline,
 537                                         _prof_factor,
 538                                         nullptr /*speculative_receiver_type*/,
 539                                         true /*allow_intrinsics*/);
 540 
 541   if (cg != nullptr) {
 542     if (!allow_inline) {
 543       C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE, "late call devirtualization");
 544     }
 545     assert(!cg->is_late_inline() || cg->is_mh_late_inline() || AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
 546     _inline_cg = cg;
 547     return true;
 548   } else {
 549     // Virtual call which provably doesn't dispatch should be either inlined or replaced with a direct call.
 550     assert(false, "no progress");
 551     return false;
 552   }
 553 }
 554 
 555 CallGenerator* CallGenerator::for_late_inline_virtual(ciMethod* m, int vtable_index, float prof_factor) {
 556   assert(IncrementalInlineVirtual, "required");
 557   assert(!m->is_static(), "for_virtual_call mismatch");
 558   assert(!m->is_method_handle_intrinsic(), "should be a direct call");
 559   return new LateInlineVirtualCallGenerator(m, vtable_index, prof_factor);
 560 }
 561 
 562 void LateInlineCallGenerator::do_late_inline() {
 563   CallGenerator::do_late_inline_helper();
 564 }
 565 
 566 void LateInlineMHCallGenerator::do_late_inline() {
 567   CallGenerator::do_late_inline_helper();
 568 }
 569 
 570 void LateInlineVirtualCallGenerator::do_late_inline() {
 571   assert(_callee != nullptr, "required"); // set up in CallDynamicJavaNode::Ideal
 572   CallGenerator::do_late_inline_helper();
 573 }
 574 
 575 void CallGenerator::do_late_inline_helper() {
 576   assert(is_late_inline(), "only late inline allowed");
 577 
 578   // Can't inline it
 579   CallNode* call = call_node();
 580   if (call == nullptr || call->outcnt() == 0 ||
 581       call->in(0) == nullptr || call->in(0)->is_top()) {
 582     return;
 583   }
 584 
 585   const TypeTuple *r = call->tf()->domain();
 586   for (int i1 = 0; i1 < method()->arg_size(); i1++) {
 587     if (call->in(TypeFunc::Parms + i1)->is_top() && r->field_at(TypeFunc::Parms + i1) != Type::HALF) {
 588       assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
 589       return;
 590     }
 591   }
 592 
 593   if (call->in(TypeFunc::Memory)->is_top()) {
 594     assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
 595     return;
 596   }
 597   if (call->in(TypeFunc::Memory)->is_MergeMem()) {
 598     MergeMemNode* merge_mem = call->in(TypeFunc::Memory)->as_MergeMem();
 599     if (merge_mem->base_memory() == merge_mem->empty_memory()) {
 600       return; // dead path
 601     }
 602   }
 603 
 604   // check for unreachable loop
 605   CallProjections callprojs;
 606   // Similar to incremental inlining, don't assert that all call
 607   // projections are still there for post-parse call devirtualization.
 608   bool do_asserts = !is_mh_late_inline() && !is_virtual_late_inline();
 609   call->extract_projections(&callprojs, true, do_asserts);
 610   if ((callprojs.fallthrough_catchproj == call->in(0)) ||
 611       (callprojs.catchall_catchproj    == call->in(0)) ||
 612       (callprojs.fallthrough_memproj   == call->in(TypeFunc::Memory)) ||
 613       (callprojs.catchall_memproj      == call->in(TypeFunc::Memory)) ||
 614       (callprojs.fallthrough_ioproj    == call->in(TypeFunc::I_O)) ||
 615       (callprojs.catchall_ioproj       == call->in(TypeFunc::I_O)) ||
 616       (callprojs.resproj != nullptr && call->find_edge(callprojs.resproj) != -1) ||
 617       (callprojs.exobj   != nullptr && call->find_edge(callprojs.exobj) != -1)) {
 618     return;
 619   }
 620 
 621   Compile* C = Compile::current();
 622 
 623   uint endoff = call->jvms()->endoff();
 624   if (C->inlining_incrementally()) {
 625     // No reachability edges should be present when incremental inlining takes place.
 626     // Inlining logic doesn't expect any extra edges past debug info and fails with
 627     // an assert in SafePointNode::grow_stack.
 628     assert(endoff == call->req(), "reachability edges not supported");
 629   } else {
 630     if (call->req() > endoff) { // reachability edges present
 631       assert(OptimizeReachabilityFences, "required");
 632       return; // keep the original call node as the holder of reachability info
 633     }
 634   }
 635 
 636   // Remove inlined methods from Compiler's lists.
 637   if (call->is_macro()) {
 638     C->remove_macro_node(call);
 639   }
 640 
 641   // The call is marked as pure (no important side effects), but result isn't used.
 642   // It's safe to remove the call.
 643   bool result_not_used = (callprojs.resproj == nullptr || callprojs.resproj->outcnt() == 0);
 644 
 645   if (is_pure_call() && result_not_used) {
 646     GraphKit kit(call->jvms());
 647     kit.replace_call(call, C->top(), true, do_asserts);
 648   } else {
 649     // Make a clone of the JVMState that appropriate to use for driving a parse
 650     JVMState* old_jvms = call->jvms();
 651     JVMState* jvms = old_jvms->clone_shallow(C);
 652     uint size = call->req();
 653     SafePointNode* map = new SafePointNode(size, jvms);
 654     for (uint i1 = 0; i1 < size; i1++) {
 655       map->init_req(i1, call->in(i1));
 656     }
 657     // Call node has in(ReturnAdr) set to top() node.
 658     // We have to set map->in(ReturnAdr) to correct value
 659     // because it is used by uncommon traps.
 660     Node* ret_adr = C->start()->proj_out_or_null(TypeFunc::ReturnAdr);
 661     precond(ret_adr != nullptr);
 662     map->set_req(TypeFunc::ReturnAdr, ret_adr);
 663 
 664     // Make sure the state is a MergeMem for parsing.
 665     if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
 666       Node* mem = MergeMemNode::make(map->in(TypeFunc::Memory));
 667       C->initial_gvn()->set_type_bottom(mem);
 668       map->set_req(TypeFunc::Memory, mem);
 669     }
 670 
 671     uint nargs = method()->arg_size();
 672     // blow away old call arguments
 673     Node* top = C->top();
 674     for (uint i1 = 0; i1 < nargs; i1++) {
 675       map->set_req(TypeFunc::Parms + i1, top);
 676     }
 677     jvms->set_map(map);
 678     precond(ret_adr == jvms->map()->returnadr());
 679 
 680     // Make enough space in the expression stack to transfer
 681     // the incoming arguments and return value.
 682     map->ensure_stack(jvms, jvms->method()->max_stack());
 683     for (uint i1 = 0; i1 < nargs; i1++) {
 684       map->set_argument(jvms, i1, call->in(TypeFunc::Parms + i1));
 685     }
 686 
 687     C->log_late_inline(this);
 688 
 689     // JVMState is ready, so time to perform some checks and prepare for inlining attempt.
 690     if (!do_late_inline_check(C, jvms)) {
 691       map->disconnect_inputs(C);
 692       return;
 693     }
 694 
 695     // Setup default node notes to be picked up by the inlining
 696     Node_Notes* old_nn = C->node_notes_at(call->_idx);
 697     if (old_nn != nullptr) {
 698       Node_Notes* entry_nn = old_nn->clone(C);
 699       entry_nn->set_jvms(jvms);
 700       C->set_default_node_notes(entry_nn);
 701     }
 702 
 703     // Now perform the inlining using the synthesized JVMState
 704     JVMState* new_jvms = inline_cg()->generate(jvms);
 705     if (new_jvms == nullptr)  return;  // no change
 706     if (C->failing())      return;
 707 
 708     if (is_mh_late_inline()) {
 709       C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (method handle)");
 710     } else if (is_string_late_inline()) {
 711       C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (string method)");
 712     } else if (is_boxing_late_inline()) {
 713       C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (boxing method)");
 714     } else if (is_vector_reboxing_late_inline()) {
 715       C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (vector reboxing method)");
 716     } else {
 717       C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded");
 718     }
 719 
 720     // Capture any exceptional control flow
 721     GraphKit kit(new_jvms);
 722 
 723     // Find the result object
 724     Node* result = C->top();
 725     int   result_size = method()->return_type()->size();
 726     if (result_size != 0 && !kit.stopped()) {
 727       result = (result_size == 1) ? kit.pop() : kit.pop_pair();
 728     }
 729 
 730     if (call->is_CallStaticJava() && call->as_CallStaticJava()->is_boxing_method()) {
 731       result = kit.must_be_not_null(result, false);
 732     }
 733 
 734     if (inline_cg()->is_inline()) {
 735       C->set_has_loops(C->has_loops() || inline_cg()->method()->has_loops());
 736       C->env()->notice_inlined_method(inline_cg()->method());
 737     }
 738     C->set_inlining_progress(true);
 739     C->set_do_cleanup(kit.stopped()); // path is dead; needs cleanup
 740     kit.replace_call(call, result, true, do_asserts);
 741   }
 742 }
 743 
 744 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
 745 
 746  public:
 747   LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
 748     LateInlineCallGenerator(method, inline_cg) {}
 749 
 750   virtual JVMState* generate(JVMState* jvms) {
 751     Compile *C = Compile::current();
 752 
 753     C->log_inline_id(this);
 754 
 755     C->add_string_late_inline(this);
 756 
 757     JVMState* new_jvms = DirectCallGenerator::generate(jvms);
 758     return new_jvms;
 759   }
 760 
 761   virtual bool is_string_late_inline() const { return true; }
 762 
 763   virtual CallGenerator* with_call_node(CallNode* call) {
 764     LateInlineStringCallGenerator* cg = new LateInlineStringCallGenerator(method(), _inline_cg);
 765     cg->set_call_node(call->as_CallStaticJava());
 766     return cg;
 767   }
 768 };
 769 
 770 CallGenerator* CallGenerator::for_string_late_inline(ciMethod* method, CallGenerator* inline_cg) {
 771   return new LateInlineStringCallGenerator(method, inline_cg);
 772 }
 773 
 774 class LateInlineBoxingCallGenerator : public LateInlineCallGenerator {
 775 
 776  public:
 777   LateInlineBoxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
 778     LateInlineCallGenerator(method, inline_cg, /*is_pure=*/true) {}
 779 
 780   virtual JVMState* generate(JVMState* jvms) {
 781     Compile *C = Compile::current();
 782 
 783     C->log_inline_id(this);
 784 
 785     C->add_boxing_late_inline(this);
 786 
 787     JVMState* new_jvms = DirectCallGenerator::generate(jvms);
 788     return new_jvms;
 789   }
 790 
 791   virtual bool is_boxing_late_inline() const { return true; }
 792 
 793   virtual CallGenerator* with_call_node(CallNode* call) {
 794     LateInlineBoxingCallGenerator* cg = new LateInlineBoxingCallGenerator(method(), _inline_cg);
 795     cg->set_call_node(call->as_CallStaticJava());
 796     return cg;
 797   }
 798 };
 799 
 800 CallGenerator* CallGenerator::for_boxing_late_inline(ciMethod* method, CallGenerator* inline_cg) {
 801   return new LateInlineBoxingCallGenerator(method, inline_cg);
 802 }
 803 
 804 class LateInlineVectorReboxingCallGenerator : public LateInlineCallGenerator {
 805 
 806  public:
 807   LateInlineVectorReboxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
 808     LateInlineCallGenerator(method, inline_cg, /*is_pure=*/true) {}
 809 
 810   virtual JVMState* generate(JVMState* jvms) {
 811     Compile *C = Compile::current();
 812 
 813     C->log_inline_id(this);
 814 
 815     C->add_vector_reboxing_late_inline(this);
 816 
 817     JVMState* new_jvms = DirectCallGenerator::generate(jvms);
 818     return new_jvms;
 819   }
 820 
 821   virtual bool is_vector_reboxing_late_inline() const { return true; }
 822 
 823   virtual CallGenerator* with_call_node(CallNode* call) {
 824     LateInlineVectorReboxingCallGenerator* cg = new LateInlineVectorReboxingCallGenerator(method(), _inline_cg);
 825     cg->set_call_node(call->as_CallStaticJava());
 826     return cg;
 827   }
 828 };
 829 
 830 //   static CallGenerator* for_vector_reboxing_late_inline(ciMethod* m, CallGenerator* inline_cg);
 831 CallGenerator* CallGenerator::for_vector_reboxing_late_inline(ciMethod* method, CallGenerator* inline_cg) {
 832   return new LateInlineVectorReboxingCallGenerator(method, inline_cg);
 833 }
 834 
 835 //------------------------PredictedCallGenerator------------------------------
 836 // Internal class which handles all out-of-line calls checking receiver type.
 837 class PredictedCallGenerator : public CallGenerator {
 838   ciKlass*       _predicted_receiver;
 839   CallGenerator* _if_missed;
 840   CallGenerator* _if_hit;
 841   float          _hit_prob;
 842   bool           _exact_check;
 843 
 844 public:
 845   PredictedCallGenerator(ciKlass* predicted_receiver,
 846                          CallGenerator* if_missed,
 847                          CallGenerator* if_hit, bool exact_check,
 848                          float hit_prob)
 849     : CallGenerator(if_missed->method())
 850   {
 851     // The call profile data may predict the hit_prob as extreme as 0 or 1.
 852     // Remove the extremes values from the range.
 853     if (hit_prob > PROB_MAX)   hit_prob = PROB_MAX;
 854     if (hit_prob < PROB_MIN)   hit_prob = PROB_MIN;
 855 
 856     _predicted_receiver = predicted_receiver;
 857     _if_missed          = if_missed;
 858     _if_hit             = if_hit;
 859     _hit_prob           = hit_prob;
 860     _exact_check        = exact_check;
 861   }
 862 
 863   virtual bool      is_virtual()   const    { return true; }
 864   virtual bool      is_inline()    const    { return _if_hit->is_inline(); }
 865   virtual bool      is_deferred()  const    { return _if_hit->is_deferred(); }
 866 
 867   virtual JVMState* generate(JVMState* jvms);
 868 };
 869 
 870 
 871 CallGenerator* CallGenerator::for_predicted_call(ciKlass* predicted_receiver,
 872                                                  CallGenerator* if_missed,
 873                                                  CallGenerator* if_hit,
 874                                                  float hit_prob) {
 875   return new PredictedCallGenerator(predicted_receiver, if_missed, if_hit,
 876                                     /*exact_check=*/true, hit_prob);
 877 }
 878 
 879 CallGenerator* CallGenerator::for_guarded_call(ciKlass* guarded_receiver,
 880                                                CallGenerator* if_missed,
 881                                                CallGenerator* if_hit) {
 882   return new PredictedCallGenerator(guarded_receiver, if_missed, if_hit,
 883                                     /*exact_check=*/false, PROB_ALWAYS);
 884 }
 885 
 886 JVMState* PredictedCallGenerator::generate(JVMState* jvms) {
 887   GraphKit kit(jvms);
 888   PhaseGVN& gvn = kit.gvn();
 889   // We need an explicit receiver null_check before checking its type.
 890   // We share a map with the caller, so his JVMS gets adjusted.
 891   Node* receiver = kit.argument(0);
 892   CompileLog* log = kit.C->log();
 893   if (log != nullptr) {
 894     log->elem("predicted_call bci='%d' exact='%d' klass='%d'",
 895               jvms->bci(), (_exact_check ? 1 : 0), log->identify(_predicted_receiver));
 896   }
 897 
 898   receiver = kit.null_check_receiver_before_call(method());
 899   if (kit.stopped()) {
 900     return kit.transfer_exceptions_into_jvms();
 901   }
 902 
 903   // Make a copy of the replaced nodes in case we need to restore them
 904   ReplacedNodes replaced_nodes = kit.map()->replaced_nodes();
 905   replaced_nodes.clone();
 906 
 907   Node* casted_receiver = receiver;  // will get updated in place...
 908   Node* slow_ctl = nullptr;
 909   if (_exact_check) {
 910     slow_ctl = kit.type_check_receiver(receiver, _predicted_receiver, _hit_prob,
 911                                        &casted_receiver);
 912   } else {
 913     slow_ctl = kit.subtype_check_receiver(receiver, _predicted_receiver,
 914                                           &casted_receiver);
 915   }
 916 
 917   SafePointNode* slow_map = nullptr;
 918   JVMState* slow_jvms = nullptr;
 919   { PreserveJVMState pjvms(&kit);
 920     kit.set_control(slow_ctl);
 921     if (!kit.stopped()) {
 922       slow_jvms = _if_missed->generate(kit.sync_jvms());
 923       if (kit.failing())
 924         return nullptr;  // might happen because of NodeCountInliningCutoff
 925       assert(slow_jvms != nullptr, "must be");
 926       kit.add_exception_states_from(slow_jvms);
 927       kit.set_map(slow_jvms->map());
 928       if (!kit.stopped())
 929         slow_map = kit.stop();
 930     }
 931   }
 932 
 933   if (kit.stopped()) {
 934     // Instance does not match the predicted type.
 935     kit.set_jvms(slow_jvms);
 936     return kit.transfer_exceptions_into_jvms();
 937   }
 938 
 939   // Fall through if the instance matches the desired type.
 940   kit.replace_in_map(receiver, casted_receiver);
 941 
 942   // Make the hot call:
 943   JVMState* new_jvms = _if_hit->generate(kit.sync_jvms());
 944   if (kit.failing()) {
 945     return nullptr;
 946   }
 947   if (new_jvms == nullptr) {
 948     // Inline failed, so make a direct call.
 949     assert(_if_hit->is_inline(), "must have been a failed inline");
 950     CallGenerator* cg = CallGenerator::for_direct_call(_if_hit->method());
 951     new_jvms = cg->generate(kit.sync_jvms());
 952   }
 953   kit.add_exception_states_from(new_jvms);
 954   kit.set_jvms(new_jvms);
 955 
 956   // Need to merge slow and fast?
 957   if (slow_map == nullptr) {
 958     // The fast path is the only path remaining.
 959     return kit.transfer_exceptions_into_jvms();
 960   }
 961 
 962   if (kit.stopped()) {
 963     // Inlined method threw an exception, so it's just the slow path after all.
 964     kit.set_jvms(slow_jvms);
 965     return kit.transfer_exceptions_into_jvms();
 966   }
 967 
 968   // There are 2 branches and the replaced nodes are only valid on
 969   // one: restore the replaced nodes to what they were before the
 970   // branch.
 971   kit.map()->set_replaced_nodes(replaced_nodes);
 972 
 973   // Finish the diamond.
 974   kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
 975   RegionNode* region = new RegionNode(3);
 976   region->init_req(1, kit.control());
 977   region->init_req(2, slow_map->control());
 978   kit.set_control(gvn.transform(region));
 979   Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
 980   iophi->set_req(2, slow_map->i_o());
 981   kit.set_i_o(gvn.transform(iophi));
 982   // Merge memory
 983   kit.merge_memory(slow_map->merged_memory(), region, 2);
 984   // Transform new memory Phis.
 985   for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
 986     Node* phi = mms.memory();
 987     if (phi->is_Phi() && phi->in(0) == region) {
 988       mms.set_memory(gvn.transform(phi));
 989     }
 990   }
 991   uint tos = kit.jvms()->stkoff() + kit.sp();
 992   uint limit = slow_map->req();
 993   for (uint i = TypeFunc::Parms; i < limit; i++) {
 994     // Skip unused stack slots; fast forward to monoff();
 995     if (i == tos) {
 996       i = kit.jvms()->monoff();
 997       if( i >= limit ) break;
 998     }
 999     Node* m = kit.map()->in(i);
1000     Node* n = slow_map->in(i);
1001     if (m != n) {
1002       const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
1003       Node* phi = PhiNode::make(region, m, t);
1004       phi->set_req(2, n);
1005       kit.map()->set_req(i, gvn.transform(phi));
1006     }
1007   }
1008   return kit.transfer_exceptions_into_jvms();
1009 }
1010 
1011 
1012 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline) {
1013   assert(callee->is_method_handle_intrinsic(), "for_method_handle_call mismatch");
1014   bool input_not_const;
1015   CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, allow_inline, input_not_const);
1016   Compile* C = Compile::current();
1017   bool should_delay = C->should_delay_inlining();
1018   if (cg != nullptr) {
1019     if (should_delay && IncrementalInlineMH) {
1020       return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1021     } else {
1022       return cg;
1023     }
1024   }
1025   int bci = jvms->bci();
1026   ciCallProfile profile = caller->call_profile_at_bci(bci);
1027   int call_site_count = caller->scale_count(profile.count());
1028 
1029   if (IncrementalInlineMH && call_site_count > 0 &&
1030       (should_delay || input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())) {
1031     return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1032   } else {
1033     // Out-of-line call.
1034     return CallGenerator::for_direct_call(callee);
1035   }
1036 }
1037 
1038 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1039   GraphKit kit(jvms);
1040   PhaseGVN& gvn = kit.gvn();
1041   Compile* C = kit.C;
1042   vmIntrinsics::ID iid = callee->intrinsic_id();
1043   input_not_const = true;
1044   if (StressMethodHandleLinkerInlining) {
1045     allow_inline = false;
1046   }
1047   switch (iid) {
1048   case vmIntrinsics::_invokeBasic:
1049     {
1050       // Get MethodHandle receiver:
1051       Node* receiver = kit.argument(0);
1052       if (receiver->Opcode() == Op_ConP) {
1053         input_not_const = false;
1054         const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
1055         if (recv_toop != nullptr) {
1056           ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
1057           const int vtable_index = Method::invalid_vtable_index;
1058 
1059           if (!ciMethod::is_consistent_info(callee, target)) {
1060             print_inlining_failure(C, callee, jvms, "signatures mismatch");
1061             return nullptr;
1062           }
1063 
1064           CallGenerator *cg = C->call_generator(target, vtable_index,
1065                                                 false /* call_does_dispatch */,
1066                                                 jvms,
1067                                                 allow_inline,
1068                                                 PROB_ALWAYS);
1069           return cg;
1070         } else {
1071           assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
1072                  Type::str(receiver->bottom_type()));
1073           print_inlining_failure(C, callee, jvms, "receiver is always null");
1074         }
1075       } else {
1076         print_inlining_failure(C, callee, jvms, "receiver not constant");
1077       }
1078   } break;
1079 
1080   case vmIntrinsics::_linkToVirtual:
1081   case vmIntrinsics::_linkToStatic:
1082   case vmIntrinsics::_linkToSpecial:
1083   case vmIntrinsics::_linkToInterface:
1084     {
1085       // Get MemberName argument:
1086       Node* member_name = kit.argument(callee->arg_size() - 1);
1087       if (member_name->Opcode() == Op_ConP) {
1088         input_not_const = false;
1089         const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1090         ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1091 
1092         if (!ciMethod::is_consistent_info(callee, target)) {
1093           print_inlining_failure(C, callee, jvms, "signatures mismatch");
1094           return nullptr;
1095         }
1096 
1097         // In lambda forms we erase signature types to avoid resolving issues
1098         // involving class loaders.  When we optimize a method handle invoke
1099         // to a direct call we must cast the receiver and arguments to its
1100         // actual types.
1101         ciSignature* signature = target->signature();
1102         const int receiver_skip = target->is_static() ? 0 : 1;
1103         // Cast receiver to its type.
1104         if (!target->is_static()) {
1105           Node* recv = kit.argument(0);
1106           Node* casted_recv = kit.maybe_narrow_object_type(recv, signature->accessing_klass());
1107           if (casted_recv->is_top()) {
1108             print_inlining_failure(C, callee, jvms, "argument types mismatch");
1109             return nullptr; // FIXME: effectively dead; issue a halt node instead
1110           } else if (casted_recv != recv) {
1111             kit.set_argument(0, casted_recv);
1112           }
1113         }
1114         // Cast reference arguments to its type.
1115         for (int i = 0, j = 0; i < signature->count(); i++) {
1116           ciType* t = signature->type_at(i);
1117           if (t->is_klass()) {
1118             Node* arg = kit.argument(receiver_skip + j);
1119             Node* casted_arg = kit.maybe_narrow_object_type(arg, t->as_klass());
1120             if (casted_arg->is_top()) {
1121               print_inlining_failure(C, callee, jvms, "argument types mismatch");
1122               return nullptr; // FIXME: effectively dead; issue a halt node instead
1123             } else if (casted_arg != arg) {
1124               kit.set_argument(receiver_skip + j, casted_arg);
1125             }
1126           }
1127           j += t->size();  // long and double take two slots
1128         }
1129 
1130         // Try to get the most accurate receiver type
1131         const bool is_virtual              = (iid == vmIntrinsics::_linkToVirtual);
1132         const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1133         int  vtable_index       = Method::invalid_vtable_index;
1134         bool call_does_dispatch = false;
1135 
1136         ciKlass* speculative_receiver_type = nullptr;
1137         if (is_virtual_or_interface) {
1138           ciInstanceKlass* klass = target->holder();
1139           Node*             receiver_node = kit.argument(0);
1140           const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1141           // call_does_dispatch and vtable_index are out-parameters.  They might be changed.
1142           // optimize_virtual_call() takes 2 different holder
1143           // arguments for a corner case that doesn't apply here (see
1144           // Parse::do_call())
1145           target = C->optimize_virtual_call(caller, klass, klass,
1146                                             target, receiver_type, is_virtual,
1147                                             call_does_dispatch, vtable_index, // out-parameters
1148                                             false /* check_access */);
1149           // We lack profiling at this call but type speculation may
1150           // provide us with a type
1151           speculative_receiver_type = (receiver_type != nullptr) ? receiver_type->speculative_type() : nullptr;
1152         }
1153         CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1154                                               allow_inline,
1155                                               PROB_ALWAYS,
1156                                               speculative_receiver_type);
1157         return cg;
1158       } else {
1159         print_inlining_failure(C, callee, jvms, "member_name not constant");
1160       }
1161   } break;
1162 
1163   case vmIntrinsics::_linkToNative:
1164     print_inlining_failure(C, callee, jvms, "native call");
1165     break;
1166 
1167   default:
1168     fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1169     break;
1170   }
1171   return nullptr;
1172 }
1173 
1174 //------------------------PredicatedIntrinsicGenerator------------------------------
1175 // Internal class which handles all predicated Intrinsic calls.
1176 class PredicatedIntrinsicGenerator : public CallGenerator {
1177   CallGenerator* _intrinsic;
1178   CallGenerator* _cg;
1179 
1180 public:
1181   PredicatedIntrinsicGenerator(CallGenerator* intrinsic,
1182                                CallGenerator* cg)
1183     : CallGenerator(cg->method())
1184   {
1185     _intrinsic = intrinsic;
1186     _cg        = cg;
1187   }
1188 
1189   virtual bool      is_virtual()   const    { return true; }
1190   virtual bool      is_inline()    const    { return true; }
1191   virtual bool      is_intrinsic() const    { return true; }
1192 
1193   virtual JVMState* generate(JVMState* jvms);
1194 };
1195 
1196 
1197 CallGenerator* CallGenerator::for_predicated_intrinsic(CallGenerator* intrinsic,
1198                                                        CallGenerator* cg) {
1199   return new PredicatedIntrinsicGenerator(intrinsic, cg);
1200 }
1201 
1202 
1203 JVMState* PredicatedIntrinsicGenerator::generate(JVMState* jvms) {
1204   // The code we want to generate here is:
1205   //    if (receiver == nullptr)
1206   //        uncommon_Trap
1207   //    if (predicate(0))
1208   //        do_intrinsic(0)
1209   //    else
1210   //    if (predicate(1))
1211   //        do_intrinsic(1)
1212   //    ...
1213   //    else
1214   //        do_java_comp
1215 
1216   GraphKit kit(jvms);
1217   PhaseGVN& gvn = kit.gvn();
1218 
1219   CompileLog* log = kit.C->log();
1220   if (log != nullptr) {
1221     log->elem("predicated_intrinsic bci='%d' method='%d'",
1222               jvms->bci(), log->identify(method()));
1223   }
1224 
1225   if (!method()->is_static()) {
1226     // We need an explicit receiver null_check before checking its type in predicate.
1227     // We share a map with the caller, so his JVMS gets adjusted.
1228     Node* receiver = kit.null_check_receiver_before_call(method());
1229     if (kit.stopped()) {
1230       return kit.transfer_exceptions_into_jvms();
1231     }
1232   }
1233 
1234   int n_predicates = _intrinsic->predicates_count();
1235   assert(n_predicates > 0, "sanity");
1236 
1237   JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1238 
1239   // Region for normal compilation code if intrinsic failed.
1240   Node* slow_region = new RegionNode(1);
1241 
1242   int results = 0;
1243   for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1244 #ifdef ASSERT
1245     JVMState* old_jvms = kit.jvms();
1246     SafePointNode* old_map = kit.map();
1247     Node* old_io  = old_map->i_o();
1248     Node* old_mem = old_map->memory();
1249     Node* old_exc = old_map->next_exception();
1250 #endif
1251     Node* else_ctrl = _intrinsic->generate_predicate(kit.sync_jvms(), predicate);
1252 #ifdef ASSERT
1253     // Assert(no_new_memory && no_new_io && no_new_exceptions) after generate_predicate.
1254     assert(old_jvms == kit.jvms(), "generate_predicate should not change jvm state");
1255     SafePointNode* new_map = kit.map();
1256     assert(old_io  == new_map->i_o(), "generate_predicate should not change i_o");
1257     assert(old_mem == new_map->memory(), "generate_predicate should not change memory");
1258     assert(old_exc == new_map->next_exception(), "generate_predicate should not add exceptions");
1259 #endif
1260     if (!kit.stopped()) {
1261       PreserveJVMState pjvms(&kit);
1262       // Generate intrinsic code:
1263       JVMState* new_jvms = _intrinsic->generate(kit.sync_jvms());
1264       if (kit.failing()) {
1265         return nullptr;
1266       }
1267       if (new_jvms == nullptr) {
1268         // Intrinsic failed, use normal compilation path for this predicate.
1269         slow_region->add_req(kit.control());
1270       } else {
1271         kit.add_exception_states_from(new_jvms);
1272         kit.set_jvms(new_jvms);
1273         if (!kit.stopped()) {
1274           result_jvms[results++] = kit.jvms();
1275         }
1276       }
1277     }
1278     if (else_ctrl == nullptr) {
1279       else_ctrl = kit.C->top();
1280     }
1281     kit.set_control(else_ctrl);
1282   }
1283   if (!kit.stopped()) {
1284     // Final 'else' after predicates.
1285     slow_region->add_req(kit.control());
1286   }
1287   if (slow_region->req() > 1) {
1288     PreserveJVMState pjvms(&kit);
1289     // Generate normal compilation code:
1290     kit.set_control(gvn.transform(slow_region));
1291     JVMState* new_jvms = _cg->generate(kit.sync_jvms());
1292     if (kit.failing())
1293       return nullptr;  // might happen because of NodeCountInliningCutoff
1294     assert(new_jvms != nullptr, "must be");
1295     kit.add_exception_states_from(new_jvms);
1296     kit.set_jvms(new_jvms);
1297     if (!kit.stopped()) {
1298       result_jvms[results++] = kit.jvms();
1299     }
1300   }
1301 
1302   if (results == 0) {
1303     // All paths ended in uncommon traps.
1304     (void) kit.stop();
1305     return kit.transfer_exceptions_into_jvms();
1306   }
1307 
1308   if (results == 1) { // Only one path
1309     kit.set_jvms(result_jvms[0]);
1310     return kit.transfer_exceptions_into_jvms();
1311   }
1312 
1313   // Merge all paths.
1314   kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
1315   RegionNode* region = new RegionNode(results + 1);
1316   Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
1317   for (int i = 0; i < results; i++) {
1318     JVMState* jvms = result_jvms[i];
1319     int path = i + 1;
1320     SafePointNode* map = jvms->map();
1321     region->init_req(path, map->control());
1322     iophi->set_req(path, map->i_o());
1323     if (i == 0) {
1324       kit.set_jvms(jvms);
1325     } else {
1326       kit.merge_memory(map->merged_memory(), region, path);
1327     }
1328   }
1329   kit.set_control(gvn.transform(region));
1330   kit.set_i_o(gvn.transform(iophi));
1331   // Transform new memory Phis.
1332   for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1333     Node* phi = mms.memory();
1334     if (phi->is_Phi() && phi->in(0) == region) {
1335       mms.set_memory(gvn.transform(phi));
1336     }
1337   }
1338 
1339   // Merge debug info.
1340   Node** ins = NEW_RESOURCE_ARRAY(Node*, results);
1341   uint tos = kit.jvms()->stkoff() + kit.sp();
1342   Node* map = kit.map();
1343   uint limit = map->req();
1344   for (uint i = TypeFunc::Parms; i < limit; i++) {
1345     // Skip unused stack slots; fast forward to monoff();
1346     if (i == tos) {
1347       i = kit.jvms()->monoff();
1348       if( i >= limit ) break;
1349     }
1350     Node* n = map->in(i);
1351     ins[0] = n;
1352     const Type* t = gvn.type(n);
1353     bool needs_phi = false;
1354     for (int j = 1; j < results; j++) {
1355       JVMState* jvms = result_jvms[j];
1356       Node* jmap = jvms->map();
1357       Node* m = nullptr;
1358       if (jmap->req() > i) {
1359         m = jmap->in(i);
1360         if (m != n) {
1361           needs_phi = true;
1362           t = t->meet_speculative(gvn.type(m));
1363         }
1364       }
1365       ins[j] = m;
1366     }
1367     if (needs_phi) {
1368       Node* phi = PhiNode::make(region, n, t);
1369       for (int j = 1; j < results; j++) {
1370         phi->set_req(j + 1, ins[j]);
1371       }
1372       map->set_req(i, gvn.transform(phi));
1373     }
1374   }
1375 
1376   return kit.transfer_exceptions_into_jvms();
1377 }
1378 
1379 //-------------------------UncommonTrapCallGenerator-----------------------------
1380 // Internal class which handles all out-of-line calls checking receiver type.
1381 class UncommonTrapCallGenerator : public CallGenerator {
1382   Deoptimization::DeoptReason _reason;
1383   Deoptimization::DeoptAction _action;
1384 
1385 public:
1386   UncommonTrapCallGenerator(ciMethod* m,
1387                             Deoptimization::DeoptReason reason,
1388                             Deoptimization::DeoptAction action)
1389     : CallGenerator(m)
1390   {
1391     _reason = reason;
1392     _action = action;
1393   }
1394 
1395   virtual bool      is_virtual() const          { ShouldNotReachHere(); return false; }
1396   virtual bool      is_trap() const             { return true; }
1397 
1398   virtual JVMState* generate(JVMState* jvms);
1399 };
1400 
1401 
1402 CallGenerator*
1403 CallGenerator::for_uncommon_trap(ciMethod* m,
1404                                  Deoptimization::DeoptReason reason,
1405                                  Deoptimization::DeoptAction action) {
1406   return new UncommonTrapCallGenerator(m, reason, action);
1407 }
1408 
1409 
1410 JVMState* UncommonTrapCallGenerator::generate(JVMState* jvms) {
1411   GraphKit kit(jvms);
1412   // Take the trap with arguments pushed on the stack.  (Cf. null_check_receiver).
1413   // Callsite signature can be different from actual method being called (i.e _linkTo* sites).
1414   // Use callsite signature always.
1415   ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
1416   int nargs = declared_method->arg_size();
1417   kit.inc_sp(nargs);
1418   assert(nargs <= kit.sp() && kit.sp() <= jvms->stk_size(), "sane sp w/ args pushed");
1419   if (_reason == Deoptimization::Reason_class_check &&
1420       _action == Deoptimization::Action_maybe_recompile) {
1421     // Temp fix for 6529811
1422     // Don't allow uncommon_trap to override our decision to recompile in the event
1423     // of a class cast failure for a monomorphic call as it will never let us convert
1424     // the call to either bi-morphic or megamorphic and can lead to unc-trap loops
1425     bool keep_exact_action = true;
1426     kit.uncommon_trap(_reason, _action, nullptr, "monomorphic vcall checkcast", false, keep_exact_action);
1427   } else {
1428     kit.uncommon_trap(_reason, _action);
1429   }
1430   return kit.transfer_exceptions_into_jvms();
1431 }
1432 
1433 // (Note:  Moved hook_up_call to GraphKit::set_edges_for_java_call.)
1434 
1435 // (Node:  Merged hook_up_exits into ParseGenerator::generate.)