1 /*
   2  * Copyright (c) 1998, 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/ciCallSite.hpp"
  27 #include "ci/ciMethodHandle.hpp"
  28 #include "ci/ciSymbols.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "compiler/compileLog.hpp"
  32 #include "interpreter/linkResolver.hpp"
  33 #include "opto/addnode.hpp"
  34 #include "opto/callGenerator.hpp"
  35 #include "opto/castnode.hpp"
  36 #include "opto/cfgnode.hpp"
  37 #include "opto/inlinetypenode.hpp"
  38 #include "opto/mulnode.hpp"
  39 #include "opto/parse.hpp"
  40 #include "opto/rootnode.hpp"
  41 #include "opto/runtime.hpp"
  42 #include "opto/subnode.hpp"
  43 #include "prims/methodHandles.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 
  46 void trace_type_profile(Compile* C, ciMethod *method, int depth, int bci, ciMethod *prof_method, ciKlass *prof_klass, int site_count, int receiver_count) {
  47   if (TraceTypeProfile || C->print_inlining()) {
  48     outputStream* out = tty;
  49     if (!C->print_inlining()) {
  50       if (!PrintOpto && !PrintCompilation) {
  51         method->print_short_name();
  52         tty->cr();
  53       }
  54       CompileTask::print_inlining_tty(prof_method, depth, bci);
  55     } else {
  56       out = C->print_inlining_stream();
  57     }
  58     CompileTask::print_inline_indent(depth, out);
  59     out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
  60     stringStream ss;
  61     prof_klass->name()->print_symbol_on(&ss);
  62     out->print("%s", ss.as_string());
  63     out->cr();
  64   }
  65 }
  66 
  67 CallGenerator* Compile::call_generator(ciMethod* callee, int vtable_index, bool call_does_dispatch,
  68                                        JVMState* jvms, bool allow_inline,
  69                                        float prof_factor, ciKlass* speculative_receiver_type,
  70                                        bool allow_intrinsics) {
  71   ciMethod*       caller   = jvms->method();
  72   int             bci      = jvms->bci();
  73   Bytecodes::Code bytecode = caller->java_code_at_bci(bci);
  74   guarantee(callee != NULL, "failed method resolution");
  75 
  76   const bool is_virtual_or_interface = (bytecode == Bytecodes::_invokevirtual) ||
  77                                        (bytecode == Bytecodes::_invokeinterface);
  78 
  79   // Dtrace currently doesn't work unless all calls are vanilla
  80   if (env()->dtrace_method_probes()) {
  81     allow_inline = false;
  82   }
  83 
  84   // Note: When we get profiling during stage-1 compiles, we want to pull
  85   // from more specific profile data which pertains to this inlining.
  86   // Right now, ignore the information in jvms->caller(), and do method[bci].
  87   ciCallProfile profile = caller->call_profile_at_bci(bci);
  88 
  89   // See how many times this site has been invoked.
  90   int site_count = profile.count();
  91   int receiver_count = -1;
  92   if (call_does_dispatch && UseTypeProfile && profile.has_receiver(0)) {
  93     // Receivers in the profile structure are ordered by call counts
  94     // so that the most called (major) receiver is profile.receiver(0).
  95     receiver_count = profile.receiver_count(0);
  96   }
  97 
  98   CompileLog* log = this->log();
  99   if (log != NULL) {
 100     int rid = (receiver_count >= 0)? log->identify(profile.receiver(0)): -1;
 101     int r2id = (rid != -1 && profile.has_receiver(1))? log->identify(profile.receiver(1)):-1;
 102     log->begin_elem("call method='%d' count='%d' prof_factor='%f'",
 103                     log->identify(callee), site_count, prof_factor);
 104     if (call_does_dispatch)  log->print(" virtual='1'");
 105     if (allow_inline)     log->print(" inline='1'");
 106     if (receiver_count >= 0) {
 107       log->print(" receiver='%d' receiver_count='%d'", rid, receiver_count);
 108       if (profile.has_receiver(1)) {
 109         log->print(" receiver2='%d' receiver2_count='%d'", r2id, profile.receiver_count(1));
 110       }
 111     }
 112     if (callee->is_method_handle_intrinsic()) {
 113       log->print(" method_handle_intrinsic='1'");
 114     }
 115     log->end_elem();
 116   }
 117 
 118   // Special case the handling of certain common, profitable library
 119   // methods.  If these methods are replaced with specialized code,
 120   // then we return it as the inlined version of the call.
 121   CallGenerator* cg_intrinsic = NULL;
 122   if (allow_inline && allow_intrinsics) {
 123     CallGenerator* cg = find_intrinsic(callee, call_does_dispatch);
 124     if (cg != NULL) {
 125       if (cg->is_predicated()) {
 126         // Code without intrinsic but, hopefully, inlined.
 127         CallGenerator* inline_cg = this->call_generator(callee,
 128               vtable_index, call_does_dispatch, jvms, allow_inline, prof_factor, speculative_receiver_type, false);
 129         if (inline_cg != NULL) {
 130           cg = CallGenerator::for_predicated_intrinsic(cg, inline_cg);
 131         }
 132       }
 133 
 134       // If intrinsic does the virtual dispatch, we try to use the type profile
 135       // first, and hopefully inline it as the regular virtual call below.
 136       // We will retry the intrinsic if nothing had claimed it afterwards.
 137       if (cg->does_virtual_dispatch()) {
 138         cg_intrinsic = cg;
 139         cg = NULL;
 140       } else if (IncrementalInline && should_delay_vector_inlining(callee, jvms)) {
 141         return CallGenerator::for_late_inline(callee, cg);
 142       } else {
 143         return cg;
 144       }
 145     }
 146   }
 147 
 148   // Do method handle calls.
 149   // NOTE: This must happen before normal inlining logic below since
 150   // MethodHandle.invoke* are native methods which obviously don't
 151   // have bytecodes and so normal inlining fails.
 152   if (callee->is_method_handle_intrinsic()) {
 153     CallGenerator* cg = CallGenerator::for_method_handle_call(jvms, caller, callee, allow_inline);
 154     return cg;
 155   }
 156 
 157   // Attempt to inline...
 158   if (allow_inline) {
 159     // The profile data is only partly attributable to this caller,
 160     // scale back the call site information.
 161     float past_uses = jvms->method()->scale_count(site_count, prof_factor);
 162     // This is the number of times we expect the call code to be used.
 163     float expected_uses = past_uses;
 164 
 165     // Try inlining a bytecoded method:
 166     if (!call_does_dispatch) {
 167       InlineTree* ilt = InlineTree::find_subtree_from_root(this->ilt(), jvms->caller(), jvms->method());
 168       bool should_delay = false;
 169       if (ilt->ok_to_inline(callee, jvms, profile, should_delay)) {
 170         CallGenerator* cg = CallGenerator::for_inline(callee, expected_uses);
 171         // For optimized virtual calls assert at runtime that receiver object
 172         // is a subtype of the inlined method holder. CHA can report a method
 173         // as a unique target under an abstract method, but receiver type
 174         // sometimes has a broader type. Similar scenario is possible with
 175         // default methods when type system loses information about implemented
 176         // interfaces.
 177         if (cg != NULL && is_virtual_or_interface && !callee->is_static()) {
 178           CallGenerator* trap_cg = CallGenerator::for_uncommon_trap(callee,
 179               Deoptimization::Reason_receiver_constraint, Deoptimization::Action_none);
 180 
 181           cg = CallGenerator::for_guarded_call(callee->holder(), trap_cg, cg);
 182         }
 183         if (cg != NULL) {
 184           // Delay the inlining of this method to give us the
 185           // opportunity to perform some high level optimizations
 186           // first.
 187           if (should_delay_string_inlining(callee, jvms)) {
 188             return CallGenerator::for_string_late_inline(callee, cg);
 189           } else if (should_delay_boxing_inlining(callee, jvms)) {
 190             return CallGenerator::for_boxing_late_inline(callee, cg);
 191           } else if (should_delay_vector_reboxing_inlining(callee, jvms)) {
 192             return CallGenerator::for_vector_reboxing_late_inline(callee, cg);
 193           } else if ((should_delay || AlwaysIncrementalInline)) {
 194             return CallGenerator::for_late_inline(callee, cg);
 195           } else {
 196             return cg;
 197           }
 198         }
 199       }
 200     }
 201 
 202     // Try using the type profile.
 203     if (call_does_dispatch && site_count > 0 && UseTypeProfile) {
 204       // The major receiver's count >= TypeProfileMajorReceiverPercent of site_count.
 205       bool have_major_receiver = profile.has_receiver(0) && (100.*profile.receiver_prob(0) >= (float)TypeProfileMajorReceiverPercent);
 206       ciMethod* receiver_method = NULL;
 207 
 208       int morphism = profile.morphism();
 209       if (speculative_receiver_type != NULL) {
 210         if (!too_many_traps_or_recompiles(caller, bci, Deoptimization::Reason_speculate_class_check)) {
 211           // We have a speculative type, we should be able to resolve
 212           // the call. We do that before looking at the profiling at
 213           // this invoke because it may lead to bimorphic inlining which
 214           // a speculative type should help us avoid.
 215           receiver_method = callee->resolve_invoke(jvms->method()->holder(),
 216                                                    speculative_receiver_type);
 217           if (receiver_method == NULL) {
 218             speculative_receiver_type = NULL;
 219           } else {
 220             morphism = 1;
 221           }
 222         } else {
 223           // speculation failed before. Use profiling at the call
 224           // (could allow bimorphic inlining for instance).
 225           speculative_receiver_type = NULL;
 226         }
 227       }
 228       if (receiver_method == NULL &&
 229           (have_major_receiver || morphism == 1 ||
 230            (morphism == 2 && UseBimorphicInlining))) {
 231         // receiver_method = profile.method();
 232         // Profiles do not suggest methods now.  Look it up in the major receiver.
 233         receiver_method = callee->resolve_invoke(jvms->method()->holder(),
 234                                                       profile.receiver(0));
 235       }
 236       if (receiver_method != NULL) {
 237         // The single majority receiver sufficiently outweighs the minority.
 238         CallGenerator* hit_cg = this->call_generator(receiver_method,
 239               vtable_index, !call_does_dispatch, jvms, allow_inline, prof_factor);
 240         if (hit_cg != NULL) {
 241           // Look up second receiver.
 242           CallGenerator* next_hit_cg = NULL;
 243           ciMethod* next_receiver_method = NULL;
 244           if (morphism == 2 && UseBimorphicInlining) {
 245             next_receiver_method = callee->resolve_invoke(jvms->method()->holder(),
 246                                                                profile.receiver(1));
 247             if (next_receiver_method != NULL) {
 248               next_hit_cg = this->call_generator(next_receiver_method,
 249                                   vtable_index, !call_does_dispatch, jvms,
 250                                   allow_inline, prof_factor);
 251               if (next_hit_cg != NULL && !next_hit_cg->is_inline() &&
 252                   have_major_receiver && UseOnlyInlinedBimorphic) {
 253                   // Skip if we can't inline second receiver's method
 254                   next_hit_cg = NULL;
 255               }
 256             }
 257           }
 258           CallGenerator* miss_cg;
 259           Deoptimization::DeoptReason reason = (morphism == 2
 260                                                ? Deoptimization::Reason_bimorphic
 261                                                : Deoptimization::reason_class_check(speculative_receiver_type != NULL));
 262           if ((morphism == 1 || (morphism == 2 && next_hit_cg != NULL)) &&
 263               !too_many_traps_or_recompiles(caller, bci, reason)
 264              ) {
 265             // Generate uncommon trap for class check failure path
 266             // in case of monomorphic or bimorphic virtual call site.
 267             miss_cg = CallGenerator::for_uncommon_trap(callee, reason,
 268                         Deoptimization::Action_maybe_recompile);
 269           } else {
 270             // Generate virtual call for class check failure path
 271             // in case of polymorphic virtual call site.
 272             miss_cg = (IncrementalInlineVirtual ? CallGenerator::for_late_inline_virtual(callee, vtable_index, prof_factor)
 273                                                 : CallGenerator::for_virtual_call(callee, vtable_index));
 274           }
 275           if (miss_cg != NULL) {
 276             if (next_hit_cg != NULL) {
 277               assert(speculative_receiver_type == NULL, "shouldn't end up here if we used speculation");
 278               trace_type_profile(C, jvms->method(), jvms->depth() - 1, jvms->bci(), next_receiver_method, profile.receiver(1), site_count, profile.receiver_count(1));
 279               // We don't need to record dependency on a receiver here and below.
 280               // Whenever we inline, the dependency is added by Parse::Parse().
 281               miss_cg = CallGenerator::for_predicted_call(profile.receiver(1), miss_cg, next_hit_cg, PROB_MAX);
 282             }
 283             if (miss_cg != NULL) {
 284               ciKlass* k = speculative_receiver_type != NULL ? speculative_receiver_type : profile.receiver(0);
 285               trace_type_profile(C, jvms->method(), jvms->depth() - 1, jvms->bci(), receiver_method, k, site_count, receiver_count);
 286               float hit_prob = speculative_receiver_type != NULL ? 1.0 : profile.receiver_prob(0);
 287               CallGenerator* cg = CallGenerator::for_predicted_call(k, miss_cg, hit_cg, hit_prob);
 288               if (cg != NULL)  return cg;
 289             }
 290           }
 291         }
 292       }
 293     }
 294 
 295     // If there is only one implementor of this interface then we
 296     // may be able to bind this invoke directly to the implementing
 297     // klass but we need both a dependence on the single interface
 298     // and on the method we bind to. Additionally since all we know
 299     // about the receiver type is that it's supposed to implement the
 300     // interface we have to insert a check that it's the class we
 301     // expect.  Interface types are not checked by the verifier so
 302     // they are roughly equivalent to Object.
 303     // The number of implementors for declared_interface is less or
 304     // equal to the number of implementors for target->holder() so
 305     // if number of implementors of target->holder() == 1 then
 306     // number of implementors for decl_interface is 0 or 1. If
 307     // it's 0 then no class implements decl_interface and there's
 308     // no point in inlining.
 309     if (call_does_dispatch && bytecode == Bytecodes::_invokeinterface) {
 310       ciInstanceKlass* declared_interface =
 311           caller->get_declared_method_holder_at_bci(bci)->as_instance_klass();
 312       ciInstanceKlass* singleton = declared_interface->unique_implementor();
 313 
 314       if (singleton != NULL) {
 315         assert(singleton != declared_interface, "not a unique implementor");
 316 
 317         ciMethod* cha_monomorphic_target =
 318             callee->find_monomorphic_target(caller->holder(), declared_interface, singleton);
 319 
 320         if (cha_monomorphic_target != NULL &&
 321             cha_monomorphic_target->holder() != env()->Object_klass()) { // subtype check against Object is useless
 322           ciKlass* holder = cha_monomorphic_target->holder();
 323 
 324           // Try to inline the method found by CHA. Inlined method is guarded by the type check.
 325           CallGenerator* hit_cg = call_generator(cha_monomorphic_target,
 326               vtable_index, !call_does_dispatch, jvms, allow_inline, prof_factor);
 327 
 328           // Deoptimize on type check fail. The interpreter will throw ICCE for us.
 329           CallGenerator* miss_cg = CallGenerator::for_uncommon_trap(callee,
 330               Deoptimization::Reason_class_check, Deoptimization::Action_none);
 331 
 332           CallGenerator* cg = CallGenerator::for_guarded_call(holder, miss_cg, hit_cg);
 333           if (hit_cg != NULL && cg != NULL) {
 334             dependencies()->assert_unique_concrete_method(declared_interface, cha_monomorphic_target, declared_interface, callee);
 335             return cg;
 336           }
 337         }
 338       }
 339     } // call_does_dispatch && bytecode == Bytecodes::_invokeinterface
 340 
 341     // Nothing claimed the intrinsic, we go with straight-forward inlining
 342     // for already discovered intrinsic.
 343     if (allow_intrinsics && cg_intrinsic != NULL) {
 344       assert(cg_intrinsic->does_virtual_dispatch(), "sanity");
 345       return cg_intrinsic;
 346     }
 347   } // allow_inline
 348 
 349   // There was no special inlining tactic, or it bailed out.
 350   // Use a more generic tactic, like a simple call.
 351   if (call_does_dispatch) {
 352     const char* msg = "virtual call";
 353     if (C->print_inlining()) {
 354       print_inlining(callee, jvms->depth() - 1, jvms->bci(), msg);
 355     }
 356     C->log_inline_failure(msg);
 357     if (IncrementalInlineVirtual && allow_inline) {
 358       return CallGenerator::for_late_inline_virtual(callee, vtable_index, prof_factor); // attempt to inline through virtual call later
 359     } else {
 360       return CallGenerator::for_virtual_call(callee, vtable_index);
 361     }
 362   } else {
 363     // Class Hierarchy Analysis or Type Profile reveals a unique target, or it is a static or special call.
 364     CallGenerator* cg = CallGenerator::for_direct_call(callee, should_delay_inlining(callee, jvms));
 365     // For optimized virtual calls assert at runtime that receiver object
 366     // is a subtype of the method holder.
 367     if (cg != NULL && is_virtual_or_interface && !callee->is_static()) {
 368       CallGenerator* trap_cg = CallGenerator::for_uncommon_trap(callee,
 369           Deoptimization::Reason_receiver_constraint, Deoptimization::Action_none);
 370       cg = CallGenerator::for_guarded_call(callee->holder(), trap_cg, cg);
 371     }
 372     return cg;
 373   }
 374 }
 375 
 376 // Return true for methods that shouldn't be inlined early so that
 377 // they are easier to analyze and optimize as intrinsics.
 378 bool Compile::should_delay_string_inlining(ciMethod* call_method, JVMState* jvms) {
 379   if (has_stringbuilder()) {
 380 
 381     if ((call_method->holder() == C->env()->StringBuilder_klass() ||
 382          call_method->holder() == C->env()->StringBuffer_klass()) &&
 383         (jvms->method()->holder() == C->env()->StringBuilder_klass() ||
 384          jvms->method()->holder() == C->env()->StringBuffer_klass())) {
 385       // Delay SB calls only when called from non-SB code
 386       return false;
 387     }
 388 
 389     switch (call_method->intrinsic_id()) {
 390       case vmIntrinsics::_StringBuilder_void:
 391       case vmIntrinsics::_StringBuilder_int:
 392       case vmIntrinsics::_StringBuilder_String:
 393       case vmIntrinsics::_StringBuilder_append_char:
 394       case vmIntrinsics::_StringBuilder_append_int:
 395       case vmIntrinsics::_StringBuilder_append_String:
 396       case vmIntrinsics::_StringBuilder_toString:
 397       case vmIntrinsics::_StringBuffer_void:
 398       case vmIntrinsics::_StringBuffer_int:
 399       case vmIntrinsics::_StringBuffer_String:
 400       case vmIntrinsics::_StringBuffer_append_char:
 401       case vmIntrinsics::_StringBuffer_append_int:
 402       case vmIntrinsics::_StringBuffer_append_String:
 403       case vmIntrinsics::_StringBuffer_toString:
 404       case vmIntrinsics::_Integer_toString:
 405         return true;
 406 
 407       case vmIntrinsics::_String_String:
 408         {
 409           Node* receiver = jvms->map()->in(jvms->argoff() + 1);
 410           if (receiver->is_Proj() && receiver->in(0)->is_CallStaticJava()) {
 411             CallStaticJavaNode* csj = receiver->in(0)->as_CallStaticJava();
 412             ciMethod* m = csj->method();
 413             if (m != NULL &&
 414                 (m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString ||
 415                  m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString))
 416               // Delay String.<init>(new SB())
 417               return true;
 418           }
 419           return false;
 420         }
 421 
 422       default:
 423         return false;
 424     }
 425   }
 426   return false;
 427 }
 428 
 429 bool Compile::should_delay_boxing_inlining(ciMethod* call_method, JVMState* jvms) {
 430   if (eliminate_boxing() && call_method->is_boxing_method()) {
 431     set_has_boxed_value(true);
 432     return aggressive_unboxing();
 433   }
 434   return false;
 435 }
 436 
 437 bool Compile::should_delay_vector_inlining(ciMethod* call_method, JVMState* jvms) {
 438   return EnableVectorSupport && call_method->is_vector_method();
 439 }
 440 
 441 bool Compile::should_delay_vector_reboxing_inlining(ciMethod* call_method, JVMState* jvms) {
 442   return EnableVectorSupport && (call_method->intrinsic_id() == vmIntrinsics::_VectorRebox);
 443 }
 444 
 445 // uncommon-trap call-sites where callee is unloaded, uninitialized or will not link
 446 bool Parse::can_not_compile_call_site(ciMethod *dest_method, ciInstanceKlass* klass) {
 447   // Additional inputs to consider...
 448   // bc      = bc()
 449   // caller  = method()
 450   // iter().get_method_holder_index()
 451   assert( dest_method->is_loaded(), "ciTypeFlow should not let us get here" );
 452   // Interface classes can be loaded & linked and never get around to
 453   // being initialized.  Uncommon-trap for not-initialized static or
 454   // v-calls.  Let interface calls happen.
 455   ciInstanceKlass* holder_klass = dest_method->holder();
 456   if (!holder_klass->is_being_initialized() &&
 457       !holder_klass->is_initialized() &&
 458       !holder_klass->is_interface()) {
 459     uncommon_trap(Deoptimization::Reason_uninitialized,
 460                   Deoptimization::Action_reinterpret,
 461                   holder_klass);
 462     return true;
 463   }
 464 
 465   assert(dest_method->is_loaded(), "dest_method: typeflow responsibility");
 466   return false;
 467 }
 468 
 469 #ifdef ASSERT
 470 static bool check_call_consistency(JVMState* jvms, CallGenerator* cg) {
 471   ciMethod* symbolic_info = jvms->method()->get_method_at_bci(jvms->bci());
 472   ciMethod* resolved_method = cg->method();
 473   if (!ciMethod::is_consistent_info(symbolic_info, resolved_method)) {
 474     tty->print_cr("JVMS:");
 475     jvms->dump();
 476     tty->print_cr("Bytecode info:");
 477     jvms->method()->get_method_at_bci(jvms->bci())->print(); tty->cr();
 478     tty->print_cr("Resolved method:");
 479     cg->method()->print(); tty->cr();
 480     return false;
 481   }
 482   return true;
 483 }
 484 #endif // ASSERT
 485 
 486 //------------------------------do_call----------------------------------------
 487 // Handle your basic call.  Inline if we can & want to, else just setup call.
 488 void Parse::do_call() {
 489   // It's likely we are going to add debug info soon.
 490   // Also, if we inline a guy who eventually needs debug info for this JVMS,
 491   // our contribution to it is cleaned up right here.
 492   kill_dead_locals();
 493 
 494   C->print_inlining_assert_ready();
 495 
 496   // Set frequently used booleans
 497   const bool is_virtual = bc() == Bytecodes::_invokevirtual;
 498   const bool is_virtual_or_interface = is_virtual || bc() == Bytecodes::_invokeinterface;
 499   const bool has_receiver = Bytecodes::has_receiver(bc());
 500 
 501   // Find target being called
 502   bool             will_link;
 503   ciSignature*     declared_signature = NULL;
 504   ciMethod*        orig_callee  = iter().get_method(will_link, &declared_signature);  // callee in the bytecode
 505   ciInstanceKlass* holder_klass = orig_callee->holder();
 506   ciKlass*         holder       = iter().get_declared_method_holder();
 507   ciInstanceKlass* klass = ciEnv::get_instance_klass_for_declared_method_holder(holder);
 508   assert(declared_signature != NULL, "cannot be null");
 509 
 510   // Bump max node limit for JSR292 users
 511   if (bc() == Bytecodes::_invokedynamic || orig_callee->is_method_handle_intrinsic()) {
 512     C->set_max_node_limit(3*MaxNodeLimit);
 513   }
 514 
 515   // uncommon-trap when callee is unloaded, uninitialized or will not link
 516   // bailout when too many arguments for register representation
 517   if (!will_link || can_not_compile_call_site(orig_callee, klass)) {
 518     if (PrintOpto && (Verbose || WizardMode)) {
 519       method()->print_name(); tty->print_cr(" can not compile call at bci %d to:", bci());
 520       orig_callee->print_name(); tty->cr();
 521     }
 522     return;
 523   }
 524   assert(holder_klass->is_loaded(), "");
 525   //assert((bc_callee->is_static() || is_invokedynamic) == !has_receiver , "must match bc");  // XXX invokehandle (cur_bc_raw)
 526   // Note: this takes into account invokeinterface of methods declared in java/lang/Object,
 527   // which should be invokevirtuals but according to the VM spec may be invokeinterfaces
 528   assert(holder_klass->is_interface() || holder_klass->super() == NULL || (bc() != Bytecodes::_invokeinterface), "must match bc");
 529   // Note:  In the absence of miranda methods, an abstract class K can perform
 530   // an invokevirtual directly on an interface method I.m if K implements I.
 531 
 532   // orig_callee is the resolved callee which's signature includes the
 533   // appendix argument.
 534   const int nargs = orig_callee->arg_size();
 535   const bool is_signature_polymorphic = MethodHandles::is_signature_polymorphic(orig_callee->intrinsic_id());
 536 
 537   // Push appendix argument (MethodType, CallSite, etc.), if one.
 538   if (iter().has_appendix()) {
 539     ciObject* appendix_arg = iter().get_appendix();
 540     const TypeOopPtr* appendix_arg_type = TypeOopPtr::make_from_constant(appendix_arg, /* require_const= */ true);
 541     Node* appendix_arg_node = _gvn.makecon(appendix_arg_type);
 542     push(appendix_arg_node);
 543   }
 544 
 545   // ---------------------
 546   // Does Class Hierarchy Analysis reveal only a single target of a v-call?
 547   // Then we may inline or make a static call, but become dependent on there being only 1 target.
 548   // Does the call-site type profile reveal only one receiver?
 549   // Then we may introduce a run-time check and inline on the path where it succeeds.
 550   // The other path may uncommon_trap, check for another receiver, or do a v-call.
 551 
 552   // Try to get the most accurate receiver type
 553   ciMethod* callee             = orig_callee;
 554   int       vtable_index       = Method::invalid_vtable_index;
 555   bool      call_does_dispatch = false;
 556 
 557   // Speculative type of the receiver if any
 558   ciKlass* speculative_receiver_type = NULL;
 559   if (is_virtual_or_interface) {
 560     Node* receiver_node             = stack(sp() - nargs);
 561     const TypeOopPtr* receiver_type = NULL;
 562     if (receiver_node->is_InlineType()) {
 563       receiver_type = TypeInstPtr::make(TypePtr::NotNull, _gvn.type(receiver_node)->inline_klass());
 564     } else {
 565       receiver_type = _gvn.type(receiver_node)->isa_oopptr();
 566     }
 567     // call_does_dispatch and vtable_index are out-parameters.  They might be changed.
 568     // For arrays, klass below is Object. When vtable calls are used,
 569     // resolving the call with Object would allow an illegal call to
 570     // finalize() on an array. We use holder instead: illegal calls to
 571     // finalize() won't be compiled as vtable calls (IC call
 572     // resolution will catch the illegal call) and the few legal calls
 573     // on array types won't be either.
 574     callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
 575                                       receiver_type, is_virtual,
 576                                       call_does_dispatch, vtable_index);  // out-parameters
 577     speculative_receiver_type = receiver_type != NULL ? receiver_type->speculative_type() : NULL;
 578   }
 579 
 580   // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
 581   ciKlass* receiver_constraint = NULL;
 582   if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_constructor()) {
 583     ciInstanceKlass* calling_klass = method()->holder();
 584     ciInstanceKlass* sender_klass = calling_klass;
 585     if (sender_klass->is_interface()) {
 586       receiver_constraint = sender_klass;
 587     }
 588   } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
 589     assert(holder->is_interface(), "How did we get a non-interface method here!");
 590     receiver_constraint = holder;
 591   }
 592 
 593   if (receiver_constraint != NULL) {
 594     Node* receiver_node = stack(sp() - nargs);
 595     Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint));
 596     Node* bad_type_ctrl = NULL;
 597     Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl);
 598     if (bad_type_ctrl != NULL) {
 599       PreserveJVMState pjvms(this);
 600       set_control(bad_type_ctrl);
 601       uncommon_trap(Deoptimization::Reason_class_check,
 602                     Deoptimization::Action_none);
 603     }
 604     if (stopped()) {
 605       return; // MUST uncommon-trap?
 606     }
 607     set_stack(sp() - nargs, casted_receiver);
 608   }
 609 
 610   // Note:  It's OK to try to inline a virtual call.
 611   // The call generator will not attempt to inline a polymorphic call
 612   // unless it knows how to optimize the receiver dispatch.
 613   bool try_inline = (C->do_inlining() || InlineAccessors);
 614 
 615   // ---------------------
 616   dec_sp(nargs);              // Temporarily pop args for JVM state of call
 617   JVMState* jvms = sync_jvms();
 618 
 619   // ---------------------
 620   // Decide call tactic.
 621   // This call checks with CHA, the interpreter profile, intrinsics table, etc.
 622   // It decides whether inlining is desirable or not.
 623   CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
 624 
 625   // NOTE:  Don't use orig_callee and callee after this point!  Use cg->method() instead.
 626   orig_callee = callee = NULL;
 627 
 628   // ---------------------
 629   // Round double arguments before call
 630   round_double_arguments(cg->method());
 631 
 632   // Feed profiling data for arguments to the type system so it can
 633   // propagate it as speculative types
 634   record_profiled_arguments_for_speculation(cg->method(), bc());
 635 
 636 #ifndef PRODUCT
 637   // bump global counters for calls
 638   count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
 639 
 640   // Record first part of parsing work for this call
 641   parse_histogram()->record_change();
 642 #endif // not PRODUCT
 643 
 644   assert(jvms == this->jvms(), "still operating on the right JVMS");
 645   assert(jvms_in_sync(),       "jvms must carry full info into CG");
 646 
 647   // save across call, for a subsequent cast_not_null.
 648   Node* receiver = has_receiver ? argument(0) : NULL;
 649 
 650   // The extra CheckCastPPs for speculative types mess with PhaseStringOpts
 651   if (receiver != NULL && !receiver->is_InlineType() && !call_does_dispatch && !cg->is_string_late_inline()) {
 652     // Feed profiling data for a single receiver to the type system so
 653     // it can propagate it as a speculative type
 654     receiver = record_profiled_receiver_for_speculation(receiver);
 655   }
 656 
 657   JVMState* new_jvms = cg->generate(jvms);
 658   if (new_jvms == NULL) {
 659     // When inlining attempt fails (e.g., too many arguments),
 660     // it may contaminate the current compile state, making it
 661     // impossible to pull back and try again.  Once we call
 662     // cg->generate(), we are committed.  If it fails, the whole
 663     // compilation task is compromised.
 664     if (failing())  return;
 665 
 666     // This can happen if a library intrinsic is available, but refuses
 667     // the call site, perhaps because it did not match a pattern the
 668     // intrinsic was expecting to optimize. Should always be possible to
 669     // get a normal java call that may inline in that case
 670     cg = C->call_generator(cg->method(), vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type, /* allow_intrinsics= */ false);
 671     new_jvms = cg->generate(jvms);
 672     if (new_jvms == NULL) {
 673       guarantee(failing(), "call failed to generate:  calls should work");
 674       return;
 675     }
 676   }
 677 
 678   if (cg->is_inline()) {
 679     // Accumulate has_loops estimate
 680     C->env()->notice_inlined_method(cg->method());
 681   }
 682 
 683   // Reset parser state from [new_]jvms, which now carries results of the call.
 684   // Return value (if any) is already pushed on the stack by the cg.
 685   add_exception_states_from(new_jvms);
 686   if (new_jvms->map()->control() == top()) {
 687     stop_and_kill_map();
 688   } else {
 689     assert(new_jvms->same_calls_as(jvms), "method/bci left unchanged");
 690     set_jvms(new_jvms);
 691   }
 692 
 693   assert(check_call_consistency(jvms, cg), "inconsistent info");
 694 
 695   if (!stopped()) {
 696     // This was some sort of virtual call, which did a null check for us.
 697     // Now we can assert receiver-not-null, on the normal return path.
 698     if (receiver != NULL && cg->is_virtual()) {
 699       Node* cast = cast_not_null(receiver);
 700       // %%% assert(receiver == cast, "should already have cast the receiver");
 701     }
 702 
 703     ciType* rtype = cg->method()->return_type();
 704     ciType* ctype = declared_signature->return_type();
 705 
 706     if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
 707       // Be careful here with return types.
 708       if (ctype != rtype) {
 709         BasicType rt = rtype->basic_type();
 710         BasicType ct = ctype->basic_type();
 711         if (ct == T_VOID) {
 712           // It's OK for a method to return a value that is discarded.
 713           // The discarding does not require any special action from the caller.
 714           // The Java code knows this, at VerifyType.isNullConversion.
 715           pop_node(rt);  // whatever it was, pop it
 716         } else if (rt == T_INT || is_subword_type(rt)) {
 717           // Nothing.  These cases are handled in lambda form bytecode.
 718           assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
 719         } else if (is_reference_type(rt)) {
 720           assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
 721           if (ctype->is_loaded()) {
 722             const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
 723             const Type*       sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
 724             if (declared_signature->returns_null_free_inline_type()) {
 725               sig_type = sig_type->join_speculative(TypePtr::NOTNULL);
 726             }
 727             if (arg_type != NULL && !arg_type->higher_equal(sig_type) && !peek()->is_InlineType()) {
 728               Node* retnode = pop();
 729               Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
 730               push(cast_obj);
 731             }
 732           }
 733         } else {
 734           assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
 735           // push a zero; it's better than getting an oop/int mismatch
 736           pop_node(rt);
 737           Node* retnode = zerocon(ct);
 738           push_node(ct, retnode);
 739         }
 740         // Now that the value is well-behaved, continue with the call-site type.
 741         rtype = ctype;
 742       }
 743     } else {
 744       // Symbolic resolution enforces the types to be the same.
 745       // NOTE: We must relax the assert for unloaded types because two
 746       // different ciType instances of the same unloaded class type
 747       // can appear to be "loaded" by different loaders (depending on
 748       // the accessing class).
 749       assert(!rtype->is_loaded() || !ctype->is_loaded() || rtype == ctype,
 750              "mismatched return types: rtype=%s, ctype=%s", rtype->name(), ctype->name());
 751     }
 752 
 753     if (rtype->basic_type() == T_INLINE_TYPE && !peek()->is_InlineType() && !gvn().type(peek())->maybe_null()) {
 754       Node* retnode = pop();
 755       retnode = InlineTypeNode::make_from_oop(this, retnode, rtype->as_inline_klass());
 756       push_node(T_INLINE_TYPE, retnode);
 757     }
 758 
 759     // If the return type of the method is not loaded, assert that the
 760     // value we got is a null.  Otherwise, we need to recompile.
 761     if (!rtype->is_loaded()) {
 762       if (PrintOpto && (Verbose || WizardMode)) {
 763         method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
 764         cg->method()->print_name(); tty->cr();
 765       }
 766       if (C->log() != NULL) {
 767         C->log()->elem("assert_null reason='return' klass='%d'",
 768                        C->log()->identify(rtype));
 769       }
 770       // If there is going to be a trap, put it at the next bytecode:
 771       set_bci(iter().next_bci());
 772       null_assert(peek());
 773       set_bci(iter().cur_bci()); // put it back
 774     }
 775     BasicType ct = ctype->basic_type();
 776     if (is_reference_type(ct)) {
 777       record_profiled_return_for_speculation();
 778     }
 779   }
 780 
 781   // Restart record of parsing work after possible inlining of call
 782 #ifndef PRODUCT
 783   parse_histogram()->set_initial_state(bc());
 784 #endif
 785 }
 786 
 787 //---------------------------catch_call_exceptions-----------------------------
 788 // Put a Catch and CatchProj nodes behind a just-created call.
 789 // Send their caught exceptions to the proper handler.
 790 // This may be used after a call to the rethrow VM stub,
 791 // when it is needed to process unloaded exception classes.
 792 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
 793   // Exceptions are delivered through this channel:
 794   Node* i_o = this->i_o();
 795 
 796   // Add a CatchNode.
 797   GrowableArray<int>* bcis = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, -1);
 798   GrowableArray<const Type*>* extypes = new (C->node_arena()) GrowableArray<const Type*>(C->node_arena(), 8, 0, NULL);
 799   GrowableArray<int>* saw_unloaded = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, 0);
 800 
 801   bool default_handler = false;
 802   for (; !handlers.is_done(); handlers.next()) {
 803     ciExceptionHandler* h        = handlers.handler();
 804     int                 h_bci    = h->handler_bci();
 805     ciInstanceKlass*    h_klass  = h->is_catch_all() ? env()->Throwable_klass() : h->catch_klass();
 806     // Do not introduce unloaded exception types into the graph:
 807     if (!h_klass->is_loaded()) {
 808       if (saw_unloaded->contains(h_bci)) {
 809         /* We've already seen an unloaded exception with h_bci,
 810            so don't duplicate. Duplication will cause the CatchNode to be
 811            unnecessarily large. See 4713716. */
 812         continue;
 813       } else {
 814         saw_unloaded->append(h_bci);
 815       }
 816     }
 817     const Type*         h_extype = TypeOopPtr::make_from_klass(h_klass);
 818     // (We use make_from_klass because it respects UseUniqueSubclasses.)
 819     h_extype = h_extype->join(TypeInstPtr::NOTNULL);
 820     assert(!h_extype->empty(), "sanity");
 821     // Note:  It's OK if the BCIs repeat themselves.
 822     bcis->append(h_bci);
 823     extypes->append(h_extype);
 824     if (h_bci == -1) {
 825       default_handler = true;
 826     }
 827   }
 828 
 829   if (!default_handler) {
 830     bcis->append(-1);
 831     extypes->append(TypeOopPtr::make_from_klass(env()->Throwable_klass())->is_instptr());
 832   }
 833 
 834   int len = bcis->length();
 835   CatchNode *cn = new CatchNode(control(), i_o, len+1);
 836   Node *catch_ = _gvn.transform(cn);
 837 
 838   // now branch with the exception state to each of the (potential)
 839   // handlers
 840   for(int i=0; i < len; i++) {
 841     // Setup JVM state to enter the handler.
 842     PreserveJVMState pjvms(this);
 843     // Locals are just copied from before the call.
 844     // Get control from the CatchNode.
 845     int handler_bci = bcis->at(i);
 846     Node* ctrl = _gvn.transform( new CatchProjNode(catch_, i+1,handler_bci));
 847     // This handler cannot happen?
 848     if (ctrl == top())  continue;
 849     set_control(ctrl);
 850 
 851     // Create exception oop
 852     const TypeInstPtr* extype = extypes->at(i)->is_instptr();
 853     Node *ex_oop = _gvn.transform(new CreateExNode(extypes->at(i), ctrl, i_o));
 854 
 855     // Handle unloaded exception classes.
 856     if (saw_unloaded->contains(handler_bci)) {
 857       // An unloaded exception type is coming here.  Do an uncommon trap.
 858 #ifndef PRODUCT
 859       // We do not expect the same handler bci to take both cold unloaded
 860       // and hot loaded exceptions.  But, watch for it.
 861       if ((Verbose || WizardMode) && extype->is_loaded()) {
 862         tty->print("Warning: Handler @%d takes mixed loaded/unloaded exceptions in ", bci());
 863         method()->print_name(); tty->cr();
 864       } else if (PrintOpto && (Verbose || WizardMode)) {
 865         tty->print("Bailing out on unloaded exception type ");
 866         extype->klass()->print_name();
 867         tty->print(" at bci:%d in ", bci());
 868         method()->print_name(); tty->cr();
 869       }
 870 #endif
 871       // Emit an uncommon trap instead of processing the block.
 872       set_bci(handler_bci);
 873       push_ex_oop(ex_oop);
 874       uncommon_trap(Deoptimization::Reason_unloaded,
 875                     Deoptimization::Action_reinterpret,
 876                     extype->klass(), "!loaded exception");
 877       set_bci(iter().cur_bci()); // put it back
 878       continue;
 879     }
 880 
 881     // go to the exception handler
 882     if (handler_bci < 0) {     // merge with corresponding rethrow node
 883       throw_to_exit(make_exception_state(ex_oop));
 884     } else {                      // Else jump to corresponding handle
 885       push_ex_oop(ex_oop);        // Clear stack and push just the oop.
 886       merge_exception(handler_bci);
 887     }
 888   }
 889 
 890   // The first CatchProj is for the normal return.
 891   // (Note:  If this is a call to rethrow_Java, this node goes dead.)
 892   set_control(_gvn.transform( new CatchProjNode(catch_, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci)));
 893 }
 894 
 895 
 896 //----------------------------catch_inline_exceptions--------------------------
 897 // Handle all exceptions thrown by an inlined method or individual bytecode.
 898 // Common case 1: we have no handler, so all exceptions merge right into
 899 // the rethrow case.
 900 // Case 2: we have some handlers, with loaded exception klasses that have
 901 // no subklasses.  We do a Deutsch-Shiffman style type-check on the incoming
 902 // exception oop and branch to the handler directly.
 903 // Case 3: We have some handlers with subklasses or are not loaded at
 904 // compile-time.  We have to call the runtime to resolve the exception.
 905 // So we insert a RethrowCall and all the logic that goes with it.
 906 void Parse::catch_inline_exceptions(SafePointNode* ex_map) {
 907   // Caller is responsible for saving away the map for normal control flow!
 908   assert(stopped(), "call set_map(NULL) first");
 909   assert(method()->has_exception_handlers(), "don't come here w/o work to do");
 910 
 911   Node* ex_node = saved_ex_oop(ex_map);
 912   if (ex_node == top()) {
 913     // No action needed.
 914     return;
 915   }
 916   const TypeInstPtr* ex_type = _gvn.type(ex_node)->isa_instptr();
 917   NOT_PRODUCT(if (ex_type==NULL) tty->print_cr("*** Exception not InstPtr"));
 918   if (ex_type == NULL)
 919     ex_type = TypeOopPtr::make_from_klass(env()->Throwable_klass())->is_instptr();
 920 
 921   // determine potential exception handlers
 922   ciExceptionHandlerStream handlers(method(), bci(),
 923                                     ex_type->klass()->as_instance_klass(),
 924                                     ex_type->klass_is_exact());
 925 
 926   // Start executing from the given throw state.  (Keep its stack, for now.)
 927   // Get the exception oop as known at compile time.
 928   ex_node = use_exception_state(ex_map);
 929 
 930   // Get the exception oop klass from its header
 931   Node* ex_klass_node = NULL;
 932   if (has_ex_handler() && !ex_type->klass_is_exact()) {
 933     Node* p = basic_plus_adr( ex_node, ex_node, oopDesc::klass_offset_in_bytes());
 934     ex_klass_node = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
 935 
 936     // Compute the exception klass a little more cleverly.
 937     // Obvious solution is to simple do a LoadKlass from the 'ex_node'.
 938     // However, if the ex_node is a PhiNode, I'm going to do a LoadKlass for
 939     // each arm of the Phi.  If I know something clever about the exceptions
 940     // I'm loading the class from, I can replace the LoadKlass with the
 941     // klass constant for the exception oop.
 942     if (ex_node->is_Phi()) {
 943       ex_klass_node = new PhiNode(ex_node->in(0), TypeInstKlassPtr::OBJECT);
 944       for (uint i = 1; i < ex_node->req(); i++) {
 945         Node* ex_in = ex_node->in(i);
 946         if (ex_in == top() || ex_in == NULL) {
 947           // This path was not taken.
 948           ex_klass_node->init_req(i, top());
 949           continue;
 950         }
 951         Node* p = basic_plus_adr(ex_in, ex_in, oopDesc::klass_offset_in_bytes());
 952         Node* k = _gvn.transform( LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
 953         ex_klass_node->init_req( i, k );
 954       }
 955       _gvn.set_type(ex_klass_node, TypeInstKlassPtr::OBJECT);
 956 
 957     }
 958   }
 959 
 960   // Scan the exception table for applicable handlers.
 961   // If none, we can call rethrow() and be done!
 962   // If precise (loaded with no subklasses), insert a D.S. style
 963   // pointer compare to the correct handler and loop back.
 964   // If imprecise, switch to the Rethrow VM-call style handling.
 965 
 966   int remaining = handlers.count_remaining();
 967 
 968   // iterate through all entries sequentially
 969   for (;!handlers.is_done(); handlers.next()) {
 970     ciExceptionHandler* handler = handlers.handler();
 971 
 972     if (handler->is_rethrow()) {
 973       // If we fell off the end of the table without finding an imprecise
 974       // exception klass (and without finding a generic handler) then we
 975       // know this exception is not handled in this method.  We just rethrow
 976       // the exception into the caller.
 977       throw_to_exit(make_exception_state(ex_node));
 978       return;
 979     }
 980 
 981     // exception handler bci range covers throw_bci => investigate further
 982     int handler_bci = handler->handler_bci();
 983 
 984     if (remaining == 1) {
 985       push_ex_oop(ex_node);        // Push exception oop for handler
 986       if (PrintOpto && WizardMode) {
 987         tty->print_cr("  Catching every inline exception bci:%d -> handler_bci:%d", bci(), handler_bci);
 988       }
 989       merge_exception(handler_bci); // jump to handler
 990       return;                   // No more handling to be done here!
 991     }
 992 
 993     // Get the handler's klass
 994     ciInstanceKlass* klass = handler->catch_klass();
 995 
 996     if (!klass->is_loaded()) {  // klass is not loaded?
 997       // fall through into catch_call_exceptions which will emit a
 998       // handler with an uncommon trap.
 999       break;
1000     }
1001 
1002     if (klass->is_interface())  // should not happen, but...
1003       break;                    // bail out
1004 
1005     // Check the type of the exception against the catch type
1006     const TypeKlassPtr *tk = TypeKlassPtr::make(klass);
1007     Node* con = _gvn.makecon(tk);
1008     Node* not_subtype_ctrl = gen_subtype_check(ex_klass_node, con);
1009     if (!stopped()) {
1010       PreserveJVMState pjvms(this);
1011       const TypeInstPtr* tinst = TypeOopPtr::make_from_klass_unique(klass)->cast_to_ptr_type(TypePtr::NotNull)->is_instptr();
1012       assert(klass->has_subklass() || tinst->klass_is_exact(), "lost exactness");
1013       Node* ex_oop = _gvn.transform(new CheckCastPPNode(control(), ex_node, tinst));
1014       push_ex_oop(ex_oop);      // Push exception oop for handler
1015       if (PrintOpto && WizardMode) {
1016         tty->print("  Catching inline exception bci:%d -> handler_bci:%d -- ", bci(), handler_bci);
1017         klass->print_name();
1018         tty->cr();
1019       }
1020       merge_exception(handler_bci);
1021     }
1022     set_control(not_subtype_ctrl);
1023 
1024     // Come here if exception does not match handler.
1025     // Carry on with more handler checks.
1026     --remaining;
1027   }
1028 
1029   assert(!stopped(), "you should return if you finish the chain");
1030 
1031   // Oops, need to call into the VM to resolve the klasses at runtime.
1032   // Note:  This call must not deoptimize, since it is not a real at this bci!
1033   kill_dead_locals();
1034 
1035   make_runtime_call(RC_NO_LEAF | RC_MUST_THROW,
1036                     OptoRuntime::rethrow_Type(),
1037                     OptoRuntime::rethrow_stub(),
1038                     NULL, NULL,
1039                     ex_node);
1040 
1041   // Rethrow is a pure call, no side effects, only a result.
1042   // The result cannot be allocated, so we use I_O
1043 
1044   // Catch exceptions from the rethrow
1045   catch_call_exceptions(handlers);
1046 }
1047 
1048 
1049 // (Note:  Moved add_debug_info into GraphKit::add_safepoint_edges.)
1050 
1051 
1052 #ifndef PRODUCT
1053 void Parse::count_compiled_calls(bool at_method_entry, bool is_inline) {
1054   if( CountCompiledCalls ) {
1055     if( at_method_entry ) {
1056       // bump invocation counter if top method (for statistics)
1057       if (CountCompiledCalls && depth() == 1) {
1058         const TypePtr* addr_type = TypeMetadataPtr::make(method());
1059         Node* adr1 = makecon(addr_type);
1060         Node* adr2 = basic_plus_adr(adr1, adr1, in_bytes(Method::compiled_invocation_counter_offset()));
1061         increment_counter(adr2);
1062       }
1063     } else if (is_inline) {
1064       switch (bc()) {
1065       case Bytecodes::_invokevirtual:   increment_counter(SharedRuntime::nof_inlined_calls_addr()); break;
1066       case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_inlined_interface_calls_addr()); break;
1067       case Bytecodes::_invokestatic:
1068       case Bytecodes::_invokedynamic:
1069       case Bytecodes::_invokespecial:   increment_counter(SharedRuntime::nof_inlined_static_calls_addr()); break;
1070       default: fatal("unexpected call bytecode");
1071       }
1072     } else {
1073       switch (bc()) {
1074       case Bytecodes::_invokevirtual:   increment_counter(SharedRuntime::nof_normal_calls_addr()); break;
1075       case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_interface_calls_addr()); break;
1076       case Bytecodes::_invokestatic:
1077       case Bytecodes::_invokedynamic:
1078       case Bytecodes::_invokespecial:   increment_counter(SharedRuntime::nof_static_calls_addr()); break;
1079       default: fatal("unexpected call bytecode");
1080       }
1081     }
1082   }
1083 }
1084 #endif //PRODUCT
1085 
1086 
1087 ciMethod* Compile::optimize_virtual_call(ciMethod* caller, ciInstanceKlass* klass,
1088                                          ciKlass* holder, ciMethod* callee,
1089                                          const TypeOopPtr* receiver_type, bool is_virtual,
1090                                          bool& call_does_dispatch, int& vtable_index,
1091                                          bool check_access) {
1092   // Set default values for out-parameters.
1093   call_does_dispatch = true;
1094   vtable_index       = Method::invalid_vtable_index;
1095 
1096   // Choose call strategy.
1097   ciMethod* optimized_virtual_method = optimize_inlining(caller, klass, holder, callee,
1098                                                          receiver_type, check_access);
1099 
1100   // Have the call been sufficiently improved such that it is no longer a virtual?
1101   if (optimized_virtual_method != NULL) {
1102     callee             = optimized_virtual_method;
1103     call_does_dispatch = false;
1104   } else if (!UseInlineCaches && is_virtual && callee->is_loaded()) {
1105     // We can make a vtable call at this site
1106     vtable_index = callee->resolve_vtable_index(caller->holder(), holder);
1107   }
1108   return callee;
1109 }
1110 
1111 // Identify possible target method and inlining style
1112 ciMethod* Compile::optimize_inlining(ciMethod* caller, ciInstanceKlass* klass, ciKlass* holder,
1113                                      ciMethod* callee, const TypeOopPtr* receiver_type,
1114                                      bool check_access) {
1115   // only use for virtual or interface calls
1116 
1117   // If it is obviously final, do not bother to call find_monomorphic_target,
1118   // because the class hierarchy checks are not needed, and may fail due to
1119   // incompletely loaded classes.  Since we do our own class loading checks
1120   // in this module, we may confidently bind to any method.
1121   if (callee->can_be_statically_bound()) {
1122     return callee;
1123   }
1124 
1125   if (receiver_type == NULL) {
1126     return NULL; // no receiver type info
1127   }
1128 
1129   // Attempt to improve the receiver
1130   bool actual_receiver_is_exact = false;
1131   ciInstanceKlass* actual_receiver = klass;
1132   // Array methods are all inherited from Object, and are monomorphic.
1133   // finalize() call on array is not allowed.
1134   if (receiver_type->isa_aryptr() &&
1135       callee->holder() == env()->Object_klass() &&
1136       callee->name() != ciSymbols::finalize_method_name()) {
1137     return callee;
1138   }
1139 
1140   // All other interesting cases are instance klasses.
1141   if (!receiver_type->isa_instptr()) {
1142     return NULL;
1143   }
1144 
1145   ciInstanceKlass* receiver_klass = receiver_type->klass()->as_instance_klass();
1146   if (receiver_klass->is_loaded() && receiver_klass->is_initialized() && !receiver_klass->is_interface() &&
1147       (receiver_klass == actual_receiver || receiver_klass->is_subtype_of(actual_receiver))) {
1148     // ikl is a same or better type than the original actual_receiver,
1149     // e.g. static receiver from bytecodes.
1150     actual_receiver = receiver_klass;
1151     // Is the actual_receiver exact?
1152     actual_receiver_is_exact = receiver_type->klass_is_exact();
1153   }
1154 
1155   ciInstanceKlass*   calling_klass = caller->holder();
1156   ciMethod* cha_monomorphic_target = callee->find_monomorphic_target(calling_klass, klass, actual_receiver, check_access);
1157 
1158   if (cha_monomorphic_target != NULL) {
1159     // Hardwiring a virtual.
1160     assert(!callee->can_be_statically_bound(), "should have been handled earlier");
1161     assert(!cha_monomorphic_target->is_abstract(), "");
1162     if (!cha_monomorphic_target->can_be_statically_bound(actual_receiver)) {
1163       // If we inlined because CHA revealed only a single target method,
1164       // then we are dependent on that target method not getting overridden
1165       // by dynamic class loading.  Be sure to test the "static" receiver
1166       // dest_method here, as opposed to the actual receiver, which may
1167       // falsely lead us to believe that the receiver is final or private.
1168       dependencies()->assert_unique_concrete_method(actual_receiver, cha_monomorphic_target, holder, callee);
1169     }
1170     return cha_monomorphic_target;
1171   }
1172 
1173   // If the type is exact, we can still bind the method w/o a vcall.
1174   // (This case comes after CHA so we can see how much extra work it does.)
1175   if (actual_receiver_is_exact) {
1176     // In case of evolution, there is a dependence on every inlined method, since each
1177     // such method can be changed when its class is redefined.
1178     ciMethod* exact_method = callee->resolve_invoke(calling_klass, actual_receiver);
1179     if (exact_method != NULL) {
1180       return exact_method;
1181     }
1182   }
1183 
1184   return NULL;
1185 }