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