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