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/sharedRuntime.hpp"
  43 #include "ci/ciNativeEntryPoint.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 class NativeCallGenerator : public CallGenerator {
1007 private:
1008   address _call_addr;
1009   ciNativeEntryPoint* _nep;
1010 public:
1011   NativeCallGenerator(ciMethod* m, address call_addr, ciNativeEntryPoint* nep)
1012    : CallGenerator(m), _call_addr(call_addr), _nep(nep) {}
1013 
1014   virtual JVMState* generate(JVMState* jvms);
1015 };
1016 
1017 JVMState* NativeCallGenerator::generate(JVMState* jvms) {
1018   GraphKit kit(jvms);
1019 
1020   Node* call = kit.make_native_call(_call_addr, tf(), method()->arg_size(), _nep); // -fallback, - nep
1021   if (call == NULL) return NULL;
1022 
1023   kit.C->print_inlining_update(this);
1024   if (kit.C->log() != NULL) {
1025     kit.C->log()->elem("l2n_intrinsification_success bci='%d' entry_point='" INTPTR_FORMAT "'", jvms->bci(), p2i(_call_addr));
1026   }
1027 
1028   return kit.transfer_exceptions_into_jvms();
1029 }
1030 
1031 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1032   GraphKit kit(jvms);
1033   PhaseGVN& gvn = kit.gvn();
1034   Compile* C = kit.C;
1035   vmIntrinsics::ID iid = callee->intrinsic_id();
1036   input_not_const = true;
1037   if (StressMethodHandleLinkerInlining) {
1038     allow_inline = false;
1039   }
1040   switch (iid) {
1041   case vmIntrinsics::_invokeBasic:
1042     {
1043       // Get MethodHandle receiver:
1044       Node* receiver = kit.argument(0);
1045       if (receiver->Opcode() == Op_ConP) {
1046         input_not_const = false;
1047         const TypeOopPtr* oop_ptr = receiver->bottom_type()->is_oopptr();
1048         ciMethod* target = oop_ptr->const_oop()->as_method_handle()->get_vmtarget();
1049         const int vtable_index = Method::invalid_vtable_index;
1050 
1051         if (!ciMethod::is_consistent_info(callee, target)) {
1052           print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1053                                  "signatures mismatch");
1054           return NULL;
1055         }
1056 
1057         CallGenerator* cg = C->call_generator(target, vtable_index,
1058                                               false /* call_does_dispatch */,
1059                                               jvms,
1060                                               allow_inline,
1061                                               PROB_ALWAYS);
1062         return cg;
1063       } else {
1064         print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1065                                "receiver not constant");
1066       }
1067     }
1068     break;
1069 
1070   case vmIntrinsics::_linkToVirtual:
1071   case vmIntrinsics::_linkToStatic:
1072   case vmIntrinsics::_linkToSpecial:
1073   case vmIntrinsics::_linkToInterface:
1074     {
1075       // Get MemberName argument:
1076       Node* member_name = kit.argument(callee->arg_size() - 1);
1077       if (member_name->Opcode() == Op_ConP) {
1078         input_not_const = false;
1079         const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1080         ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1081 
1082         if (!ciMethod::is_consistent_info(callee, target)) {
1083           print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1084                                  "signatures mismatch");
1085           return NULL;
1086         }
1087 
1088         // In lambda forms we erase signature types to avoid resolving issues
1089         // involving class loaders.  When we optimize a method handle invoke
1090         // to a direct call we must cast the receiver and arguments to its
1091         // actual types.
1092         ciSignature* signature = target->signature();
1093         const int receiver_skip = target->is_static() ? 0 : 1;
1094         // Cast receiver to its type.
1095         if (!target->is_static()) {
1096           Node* arg = kit.argument(0);
1097           const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr();
1098           const Type*       sig_type = TypeOopPtr::make_from_klass(signature->accessing_klass());
1099           if (arg_type != NULL && !arg_type->higher_equal(sig_type)) {
1100             const Type* recv_type = arg_type->filter_speculative(sig_type); // keep speculative part
1101             Node* cast_obj = gvn.transform(new CheckCastPPNode(kit.control(), arg, recv_type));
1102             kit.set_argument(0, cast_obj);
1103           }
1104         }
1105         // Cast reference arguments to its type.
1106         for (int i = 0, j = 0; i < signature->count(); i++) {
1107           ciType* t = signature->type_at(i);
1108           if (t->is_klass()) {
1109             Node* arg = kit.argument(receiver_skip + j);
1110             const TypeOopPtr* arg_type = arg->bottom_type()->isa_oopptr();
1111             const Type*       sig_type = TypeOopPtr::make_from_klass(t->as_klass());
1112             if (arg_type != NULL && !arg_type->higher_equal(sig_type)) {
1113               const Type* narrowed_arg_type = arg_type->filter_speculative(sig_type); // keep speculative part
1114               Node* cast_obj = gvn.transform(new CheckCastPPNode(kit.control(), arg, narrowed_arg_type));
1115               kit.set_argument(receiver_skip + j, cast_obj);
1116             }
1117           }
1118           j += t->size();  // long and double take two slots
1119         }
1120 
1121         // Try to get the most accurate receiver type
1122         const bool is_virtual              = (iid == vmIntrinsics::_linkToVirtual);
1123         const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1124         int  vtable_index       = Method::invalid_vtable_index;
1125         bool call_does_dispatch = false;
1126 
1127         ciKlass* speculative_receiver_type = NULL;
1128         if (is_virtual_or_interface) {
1129           ciInstanceKlass* klass = target->holder();
1130           Node*             receiver_node = kit.argument(0);
1131           const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1132           // call_does_dispatch and vtable_index are out-parameters.  They might be changed.
1133           // optimize_virtual_call() takes 2 different holder
1134           // arguments for a corner case that doesn't apply here (see
1135           // Parse::do_call())
1136           target = C->optimize_virtual_call(caller, klass, klass,
1137                                             target, receiver_type, is_virtual,
1138                                             call_does_dispatch, vtable_index, // out-parameters
1139                                             false /* check_access */);
1140           // We lack profiling at this call but type speculation may
1141           // provide us with a type
1142           speculative_receiver_type = (receiver_type != NULL) ? receiver_type->speculative_type() : NULL;
1143         }
1144         CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1145                                               allow_inline,
1146                                               PROB_ALWAYS,
1147                                               speculative_receiver_type);
1148         return cg;
1149       } else {
1150         print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1151                                "member_name not constant");
1152       }
1153     }
1154     break;
1155 
1156     case vmIntrinsics::_linkToNative:
1157     {
1158       Node* addr_n = kit.argument(0); // target address
1159       Node* nep_n = kit.argument(callee->arg_size() - 1); // NativeEntryPoint
1160       // This check needs to be kept in sync with the one in CallStaticJavaNode::Ideal
1161       if (addr_n->Opcode() == Op_ConL && nep_n->Opcode() == Op_ConP) {
1162         input_not_const = false;
1163 
1164         const TypeOopPtr* nep_t = nep_n->bottom_type()->is_oopptr();
1165         ciNativeEntryPoint* nep = nep_t->const_oop()->as_native_entry_point();
1166 
1167         if (!nep->need_transition()) {
1168           const TypeLong* addr_t = addr_n->bottom_type()->is_long();
1169           address addr = (address) addr_t->get_con();
1170 
1171           return new NativeCallGenerator(callee, addr, nep);
1172         } else {
1173           print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1174                         "Not inlining non-trivial call");
1175         }
1176       } else {
1177         print_inlining_failure(C, callee, jvms->depth() - 1, jvms->bci(),
1178                                "NativeEntryPoint or address not constant");
1179       }
1180     }
1181     break;
1182 
1183   default:
1184     fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1185     break;
1186   }
1187   return NULL;
1188 }
1189 
1190 
1191 //------------------------PredicatedIntrinsicGenerator------------------------------
1192 // Internal class which handles all predicated Intrinsic calls.
1193 class PredicatedIntrinsicGenerator : public CallGenerator {
1194   CallGenerator* _intrinsic;
1195   CallGenerator* _cg;
1196 
1197 public:
1198   PredicatedIntrinsicGenerator(CallGenerator* intrinsic,
1199                                CallGenerator* cg)
1200     : CallGenerator(cg->method())
1201   {
1202     _intrinsic = intrinsic;
1203     _cg        = cg;
1204   }
1205 
1206   virtual bool      is_virtual()   const    { return true; }
1207   virtual bool      is_inline()    const    { return true; }
1208   virtual bool      is_intrinsic() const    { return true; }
1209 
1210   virtual JVMState* generate(JVMState* jvms);
1211 };
1212 
1213 
1214 CallGenerator* CallGenerator::for_predicated_intrinsic(CallGenerator* intrinsic,
1215                                                        CallGenerator* cg) {
1216   return new PredicatedIntrinsicGenerator(intrinsic, cg);
1217 }
1218 
1219 
1220 JVMState* PredicatedIntrinsicGenerator::generate(JVMState* jvms) {
1221   // The code we want to generate here is:
1222   //    if (receiver == NULL)
1223   //        uncommon_Trap
1224   //    if (predicate(0))
1225   //        do_intrinsic(0)
1226   //    else
1227   //    if (predicate(1))
1228   //        do_intrinsic(1)
1229   //    ...
1230   //    else
1231   //        do_java_comp
1232 
1233   GraphKit kit(jvms);
1234   PhaseGVN& gvn = kit.gvn();
1235 
1236   CompileLog* log = kit.C->log();
1237   if (log != NULL) {
1238     log->elem("predicated_intrinsic bci='%d' method='%d'",
1239               jvms->bci(), log->identify(method()));
1240   }
1241 
1242   if (!method()->is_static()) {
1243     // We need an explicit receiver null_check before checking its type in predicate.
1244     // We share a map with the caller, so his JVMS gets adjusted.
1245     Node* receiver = kit.null_check_receiver_before_call(method());
1246     if (kit.stopped()) {
1247       return kit.transfer_exceptions_into_jvms();
1248     }
1249   }
1250 
1251   int n_predicates = _intrinsic->predicates_count();
1252   assert(n_predicates > 0, "sanity");
1253 
1254   JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1255 
1256   // Region for normal compilation code if intrinsic failed.
1257   Node* slow_region = new RegionNode(1);
1258 
1259   int results = 0;
1260   for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1261 #ifdef ASSERT
1262     JVMState* old_jvms = kit.jvms();
1263     SafePointNode* old_map = kit.map();
1264     Node* old_io  = old_map->i_o();
1265     Node* old_mem = old_map->memory();
1266     Node* old_exc = old_map->next_exception();
1267 #endif
1268     Node* else_ctrl = _intrinsic->generate_predicate(kit.sync_jvms(), predicate);
1269 #ifdef ASSERT
1270     // Assert(no_new_memory && no_new_io && no_new_exceptions) after generate_predicate.
1271     assert(old_jvms == kit.jvms(), "generate_predicate should not change jvm state");
1272     SafePointNode* new_map = kit.map();
1273     assert(old_io  == new_map->i_o(), "generate_predicate should not change i_o");
1274     assert(old_mem == new_map->memory(), "generate_predicate should not change memory");
1275     assert(old_exc == new_map->next_exception(), "generate_predicate should not add exceptions");
1276 #endif
1277     if (!kit.stopped()) {
1278       PreserveJVMState pjvms(&kit);
1279       // Generate intrinsic code:
1280       JVMState* new_jvms = _intrinsic->generate(kit.sync_jvms());
1281       if (new_jvms == NULL) {
1282         // Intrinsic failed, use normal compilation path for this predicate.
1283         slow_region->add_req(kit.control());
1284       } else {
1285         kit.add_exception_states_from(new_jvms);
1286         kit.set_jvms(new_jvms);
1287         if (!kit.stopped()) {
1288           result_jvms[results++] = kit.jvms();
1289         }
1290       }
1291     }
1292     if (else_ctrl == NULL) {
1293       else_ctrl = kit.C->top();
1294     }
1295     kit.set_control(else_ctrl);
1296   }
1297   if (!kit.stopped()) {
1298     // Final 'else' after predicates.
1299     slow_region->add_req(kit.control());
1300   }
1301   if (slow_region->req() > 1) {
1302     PreserveJVMState pjvms(&kit);
1303     // Generate normal compilation code:
1304     kit.set_control(gvn.transform(slow_region));
1305     JVMState* new_jvms = _cg->generate(kit.sync_jvms());
1306     if (kit.failing())
1307       return NULL;  // might happen because of NodeCountInliningCutoff
1308     assert(new_jvms != NULL, "must be");
1309     kit.add_exception_states_from(new_jvms);
1310     kit.set_jvms(new_jvms);
1311     if (!kit.stopped()) {
1312       result_jvms[results++] = kit.jvms();
1313     }
1314   }
1315 
1316   if (results == 0) {
1317     // All paths ended in uncommon traps.
1318     (void) kit.stop();
1319     return kit.transfer_exceptions_into_jvms();
1320   }
1321 
1322   if (results == 1) { // Only one path
1323     kit.set_jvms(result_jvms[0]);
1324     return kit.transfer_exceptions_into_jvms();
1325   }
1326 
1327   // Merge all paths.
1328   kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
1329   RegionNode* region = new RegionNode(results + 1);
1330   Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
1331   for (int i = 0; i < results; i++) {
1332     JVMState* jvms = result_jvms[i];
1333     int path = i + 1;
1334     SafePointNode* map = jvms->map();
1335     region->init_req(path, map->control());
1336     iophi->set_req(path, map->i_o());
1337     if (i == 0) {
1338       kit.set_jvms(jvms);
1339     } else {
1340       kit.merge_memory(map->merged_memory(), region, path);
1341     }
1342   }
1343   kit.set_control(gvn.transform(region));
1344   kit.set_i_o(gvn.transform(iophi));
1345   // Transform new memory Phis.
1346   for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1347     Node* phi = mms.memory();
1348     if (phi->is_Phi() && phi->in(0) == region) {
1349       mms.set_memory(gvn.transform(phi));
1350     }
1351   }
1352 
1353   // Merge debug info.
1354   Node** ins = NEW_RESOURCE_ARRAY(Node*, results);
1355   uint tos = kit.jvms()->stkoff() + kit.sp();
1356   Node* map = kit.map();
1357   uint limit = map->req();
1358   for (uint i = TypeFunc::Parms; i < limit; i++) {
1359     // Skip unused stack slots; fast forward to monoff();
1360     if (i == tos) {
1361       i = kit.jvms()->monoff();
1362       if( i >= limit ) break;
1363     }
1364     Node* n = map->in(i);
1365     ins[0] = n;
1366     const Type* t = gvn.type(n);
1367     bool needs_phi = false;
1368     for (int j = 1; j < results; j++) {
1369       JVMState* jvms = result_jvms[j];
1370       Node* jmap = jvms->map();
1371       Node* m = NULL;
1372       if (jmap->req() > i) {
1373         m = jmap->in(i);
1374         if (m != n) {
1375           needs_phi = true;
1376           t = t->meet_speculative(gvn.type(m));
1377         }
1378       }
1379       ins[j] = m;
1380     }
1381     if (needs_phi) {
1382       Node* phi = PhiNode::make(region, n, t);
1383       for (int j = 1; j < results; j++) {
1384         phi->set_req(j + 1, ins[j]);
1385       }
1386       map->set_req(i, gvn.transform(phi));
1387     }
1388   }
1389 
1390   return kit.transfer_exceptions_into_jvms();
1391 }
1392 
1393 //-------------------------UncommonTrapCallGenerator-----------------------------
1394 // Internal class which handles all out-of-line calls checking receiver type.
1395 class UncommonTrapCallGenerator : public CallGenerator {
1396   Deoptimization::DeoptReason _reason;
1397   Deoptimization::DeoptAction _action;
1398 
1399 public:
1400   UncommonTrapCallGenerator(ciMethod* m,
1401                             Deoptimization::DeoptReason reason,
1402                             Deoptimization::DeoptAction action)
1403     : CallGenerator(m)
1404   {
1405     _reason = reason;
1406     _action = action;
1407   }
1408 
1409   virtual bool      is_virtual() const          { ShouldNotReachHere(); return false; }
1410   virtual bool      is_trap() const             { return true; }
1411 
1412   virtual JVMState* generate(JVMState* jvms);
1413 };
1414 
1415 
1416 CallGenerator*
1417 CallGenerator::for_uncommon_trap(ciMethod* m,
1418                                  Deoptimization::DeoptReason reason,
1419                                  Deoptimization::DeoptAction action) {
1420   return new UncommonTrapCallGenerator(m, reason, action);
1421 }
1422 
1423 
1424 JVMState* UncommonTrapCallGenerator::generate(JVMState* jvms) {
1425   GraphKit kit(jvms);
1426   kit.C->print_inlining_update(this);
1427   // Take the trap with arguments pushed on the stack.  (Cf. null_check_receiver).
1428   // Callsite signature can be different from actual method being called (i.e _linkTo* sites).
1429   // Use callsite signature always.
1430   ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
1431   int nargs = declared_method->arg_size();
1432   kit.inc_sp(nargs);
1433   assert(nargs <= kit.sp() && kit.sp() <= jvms->stk_size(), "sane sp w/ args pushed");
1434   if (_reason == Deoptimization::Reason_class_check &&
1435       _action == Deoptimization::Action_maybe_recompile) {
1436     // Temp fix for 6529811
1437     // Don't allow uncommon_trap to override our decision to recompile in the event
1438     // of a class cast failure for a monomorphic call as it will never let us convert
1439     // the call to either bi-morphic or megamorphic and can lead to unc-trap loops
1440     bool keep_exact_action = true;
1441     kit.uncommon_trap(_reason, _action, NULL, "monomorphic vcall checkcast", false, keep_exact_action);
1442   } else {
1443     kit.uncommon_trap(_reason, _action);
1444   }
1445   return kit.transfer_exceptions_into_jvms();
1446 }
1447 
1448 // (Note:  Moved hook_up_call to GraphKit::set_edges_for_java_call.)
1449 
1450 // (Node:  Merged hook_up_exits into ParseGenerator::generate.)