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