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