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