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