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