comparison src/share/vm/opto/doCall.cpp @ 0:a61af66fc99e jdk7-b24

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