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