1 /*
2 * Copyright (c) 2000, 2026, 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/bcEscapeAnalyzer.hpp"
26 #include "ci/ciCallSite.hpp"
27 #include "ci/ciMemberName.hpp"
28 #include "ci/ciMethodHandle.hpp"
29 #include "ci/ciObjArray.hpp"
30 #include "classfile/javaClasses.hpp"
31 #include "compiler/compileLog.hpp"
32 #include "oops/accessDecorators.hpp"
33 #include "opto/addnode.hpp"
34 #include "opto/callGenerator.hpp"
35 #include "opto/callnode.hpp"
36 #include "opto/castnode.hpp"
37 #include "opto/cfgnode.hpp"
38 #include "opto/inlinetypenode.hpp"
39 #include "opto/parse.hpp"
40 #include "opto/rootnode.hpp"
41 #include "opto/runtime.hpp"
42 #include "opto/subnode.hpp"
43 #include "runtime/os.inline.hpp"
44 #include "runtime/sharedRuntime.hpp"
45 #include "utilities/debug.hpp"
46
47 // Utility function.
48 const TypeFunc* CallGenerator::tf() const {
49 return TypeFunc::make(method());
50 }
51
52 bool CallGenerator::is_inlined_method_handle_intrinsic(JVMState* jvms, ciMethod* m) {
53 return is_inlined_method_handle_intrinsic(jvms->method(), jvms->bci(), m);
54 }
55
56 bool CallGenerator::is_inlined_method_handle_intrinsic(ciMethod* caller, int bci, ciMethod* m) {
57 ciMethod* symbolic_info = caller->get_method_at_bci(bci);
58 return is_inlined_method_handle_intrinsic(symbolic_info, m);
59 }
60
61 bool CallGenerator::is_inlined_method_handle_intrinsic(ciMethod* symbolic_info, ciMethod* m) {
62 return symbolic_info->is_method_handle_intrinsic() && !m->is_method_handle_intrinsic();
63 }
64
65 // If late inlining for this call happens in a dead part of the graph it can leave a dead loop behind
66 void CallGenerator::mark_projs_not_dead_loop_safe(Node* ret) const {
67 if (!is_late_inline()) {
68 return;
69 }
70 CallNode* call = call_node();
71 if (ret->is_Proj() && ret->in(0) == call) {
72 ret->mark_not_dead_loop_safe();
73 } else if (ret->isa_InlineType()) {
74 InlineTypeNode* vt = ret->as_InlineType();
75 Node* oop = vt->get_oop();
76 if (oop->is_Proj() && oop->in(0) == call) {
77 oop->mark_not_dead_loop_safe();
78 }
79 Node* null_marker = vt->get_null_marker();
80 if (null_marker->is_Proj() && null_marker->in(0) == call) {
81 null_marker->mark_not_dead_loop_safe();
82 }
83
84 for (uint i = 0; i < vt->field_count(); i++) {
85 Node* field = vt->field_value(i);
86 if (field->is_Proj() && field->in(0) == call) {
87 field->mark_not_dead_loop_safe();
88 }
89 }
90 }
91 }
92
93 //-----------------------------ParseGenerator---------------------------------
94 // Internal class which handles all direct bytecode traversal.
95 class ParseGenerator : public InlineCallGenerator {
96 private:
97 bool _is_osr;
98 float _expected_uses;
99
100 public:
101 ParseGenerator(ciMethod* method, float expected_uses, bool is_osr = false)
102 : InlineCallGenerator(method)
103 {
104 _is_osr = is_osr;
105 _expected_uses = expected_uses;
106 assert(InlineTree::check_can_parse(method) == nullptr, "parse must be possible");
107 }
108
109 virtual bool is_parse() const { return true; }
110 virtual JVMState* generate(JVMState* jvms);
111 bool is_osr() const { return _is_osr; }
112
113 };
114
115 JVMState* ParseGenerator::generate(JVMState* jvms) {
116 Compile* C = Compile::current();
117
118 if (is_osr()) {
119 // The JVMS for a OSR has a single argument (see its TypeFunc).
120 assert(jvms->depth() == 1, "no inline OSR");
121 }
122
123 if (C->failing()) {
124 return nullptr; // bailing out of the compile; do not try to parse
125 }
126
127 Parse parser(jvms, method(), _expected_uses);
128 if (C->failing()) return nullptr;
129
130 // Grab signature for matching/allocation
131 GraphKit& exits = parser.exits();
132
133 if (C->failing()) {
134 while (exits.pop_exception_state() != nullptr) ;
135 return nullptr;
136 }
137
138 assert(exits.jvms()->same_calls_as(jvms), "sanity");
139
140 // Simply return the exit state of the parser,
141 // augmented by any exceptional states.
142 return exits.transfer_exceptions_into_jvms();
143 }
144
145 //---------------------------DirectCallGenerator------------------------------
146 // Internal class which handles all out-of-line calls w/o receiver type checks.
147 class DirectCallGenerator : public CallGenerator {
148 private:
149 CallStaticJavaNode* _call_node;
150 // Force separate memory and I/O projections for the exceptional
151 // paths to facilitate late inlining.
152 bool _separate_io_proj;
153
154 protected:
155 void set_call_node(CallStaticJavaNode* call) { _call_node = call; }
156
157 public:
158 DirectCallGenerator(ciMethod* method, bool separate_io_proj)
159 : CallGenerator(method),
160 _call_node(nullptr),
161 _separate_io_proj(separate_io_proj)
162 {
163 if (InlineTypeReturnedAsFields && method->is_method_handle_intrinsic()) {
164 // If that call has not been optimized by the time optimizations are over,
165 // we'll need to add a call to create an inline type instance from the klass
166 // returned by the call (see PhaseMacroExpand::expand_mh_intrinsic_return).
167 // Separating memory and I/O projections for exceptions is required to
168 // perform that graph transformation.
169 _separate_io_proj = true;
170 }
171 }
172 virtual JVMState* generate(JVMState* jvms);
173
174 virtual CallNode* call_node() const { return _call_node; }
175 virtual CallGenerator* with_call_node(CallNode* call) {
176 DirectCallGenerator* dcg = new DirectCallGenerator(method(), _separate_io_proj);
177 dcg->set_call_node(call->as_CallStaticJava());
178 return dcg;
179 }
180 };
181
182 JVMState* DirectCallGenerator::generate(JVMState* jvms) {
183 GraphKit kit(jvms);
184 bool is_static = method()->is_static();
185 address target = is_static ? SharedRuntime::get_resolve_static_call_stub()
186 : SharedRuntime::get_resolve_opt_virtual_call_stub();
187
188 if (kit.C->log() != nullptr) {
189 kit.C->log()->elem("direct_call bci='%d'", jvms->bci());
190 }
191
192 CallStaticJavaNode* call = new CallStaticJavaNode(kit.C, tf(), target, method());
193 if (is_inlined_method_handle_intrinsic(jvms, method())) {
194 // To be able to issue a direct call and skip a call to MH.linkTo*/invokeBasic adapter,
195 // additional information about the method being invoked should be attached
196 // to the call site to make resolution logic work
197 // (see SharedRuntime::resolve_static_call_C).
198 call->set_override_symbolic_info(true);
199 }
200 _call_node = call; // Save the call node in case we need it later
201 if (!is_static) {
202 // Make an explicit receiver null_check as part of this call.
203 // Since we share a map with the caller, his JVMS gets adjusted.
204 kit.null_check_receiver_before_call(method());
205 if (kit.stopped()) {
206 // And dump it back to the caller, decorated with any exceptions:
207 return kit.transfer_exceptions_into_jvms();
208 }
209 // Mark the call node as virtual, sort of:
210 call->set_optimized_virtual(true);
211 }
212 kit.set_arguments_for_java_call(call);
213 kit.set_edges_for_java_call(call, false, _separate_io_proj);
214 Node* ret = kit.set_results_for_java_call(call, _separate_io_proj);
215 if (!call->is_boxing_method()) {
216 mark_projs_not_dead_loop_safe(ret);
217 }
218 kit.push_node(method()->return_type()->basic_type(), ret);
219 return kit.transfer_exceptions_into_jvms();
220 }
221
222 //--------------------------VirtualCallGenerator------------------------------
223 // Internal class which handles all out-of-line calls checking receiver type.
224 class VirtualCallGenerator : public CallGenerator {
225 private:
226 int _vtable_index;
227 bool _separate_io_proj;
228 CallDynamicJavaNode* _call_node;
229
230 protected:
231 void set_call_node(CallDynamicJavaNode* call) { _call_node = call; }
232
233 public:
234 VirtualCallGenerator(ciMethod* method, int vtable_index, bool separate_io_proj)
235 : CallGenerator(method), _vtable_index(vtable_index), _separate_io_proj(separate_io_proj), _call_node(nullptr)
236 {
237 assert(vtable_index == Method::invalid_vtable_index ||
238 vtable_index >= 0, "either invalid or usable");
239 }
240 virtual bool is_virtual() const { return true; }
241 virtual JVMState* generate(JVMState* jvms);
242
243 virtual CallNode* call_node() const { return _call_node; }
244 int vtable_index() const { return _vtable_index; }
245
246 virtual CallGenerator* with_call_node(CallNode* call) {
247 VirtualCallGenerator* cg = new VirtualCallGenerator(method(), _vtable_index, _separate_io_proj);
248 cg->set_call_node(call->as_CallDynamicJava());
249 return cg;
250 }
251 };
252
253 JVMState* VirtualCallGenerator::generate(JVMState* jvms) {
254 GraphKit kit(jvms);
255 Node* receiver = kit.argument(0);
256 if (kit.C->log() != nullptr) {
257 kit.C->log()->elem("virtual_call bci='%d'", jvms->bci());
258 }
259
260 // If the receiver is a constant null, do not torture the system
261 // by attempting to call through it. The compile will proceed
262 // correctly, but may bail out in final_graph_reshaping, because
263 // the call instruction will have a seemingly deficient out-count.
264 // (The bailout says something misleading about an "infinite loop".)
265 if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) {
266 assert(Bytecodes::is_invoke(kit.java_bc()), "%d: %s", kit.java_bc(), Bytecodes::name(kit.java_bc()));
267 ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
268 int arg_size = declared_method->signature()->arg_size_for_bc(kit.java_bc());
269 kit.inc_sp(arg_size); // restore arguments
270 kit.uncommon_trap(Deoptimization::Reason_null_check,
271 Deoptimization::Action_none,
272 nullptr, "null receiver");
273 return kit.transfer_exceptions_into_jvms();
274 }
275
276 // Ideally we would unconditionally do a null check here and let it
277 // be converted to an implicit check based on profile information.
278 // However currently the conversion to implicit null checks in
279 // Block::implicit_null_check() only looks for loads and stores, not calls.
280 ciMethod *caller = kit.method();
281 ciMethodData *caller_md = (caller == nullptr) ? nullptr : caller->method_data();
282 if (!UseInlineCaches || !ImplicitNullChecks || !os::zero_page_read_protected() ||
283 ((ImplicitNullCheckThreshold > 0) && caller_md &&
284 (caller_md->trap_count(Deoptimization::Reason_null_check)
285 >= (uint)ImplicitNullCheckThreshold))) {
286 // Make an explicit receiver null_check as part of this call.
287 // Since we share a map with the caller, his JVMS gets adjusted.
288 receiver = kit.null_check_receiver_before_call(method());
289 if (kit.stopped()) {
290 // And dump it back to the caller, decorated with any exceptions:
291 return kit.transfer_exceptions_into_jvms();
292 }
293 }
294
295 assert(!method()->is_static(), "virtual call must not be to static");
296 assert(!method()->is_final(), "virtual call should not be to final");
297 assert(!method()->is_private(), "virtual call should not be to private");
298 assert(_vtable_index == Method::invalid_vtable_index || !UseInlineCaches,
299 "no vtable calls if +UseInlineCaches ");
300 address target = SharedRuntime::get_resolve_virtual_call_stub();
301 // Normal inline cache used for call
302 CallDynamicJavaNode* call = new CallDynamicJavaNode(tf(), target, method(), _vtable_index);
303 if (is_inlined_method_handle_intrinsic(jvms, method())) {
304 // To be able to issue a direct call (optimized virtual or virtual)
305 // and skip a call to MH.linkTo*/invokeBasic adapter, additional information
306 // about the method being invoked should be attached to the call site to
307 // make resolution logic work (see SharedRuntime::resolve_{virtual,opt_virtual}_call_C).
308 call->set_override_symbolic_info(true);
309 }
310 _call_node = call; // Save the call node in case we need it later
311
312 kit.set_arguments_for_java_call(call);
313 kit.set_edges_for_java_call(call, false /*must_throw*/, _separate_io_proj);
314 Node* ret = kit.set_results_for_java_call(call, _separate_io_proj);
315 mark_projs_not_dead_loop_safe(ret);
316 kit.push_node(method()->return_type()->basic_type(), ret);
317
318 // Represent the effect of an implicit receiver null_check
319 // as part of this call. Since we share a map with the caller,
320 // his JVMS gets adjusted.
321 kit.cast_not_null(receiver);
322 return kit.transfer_exceptions_into_jvms();
323 }
324
325 CallGenerator* CallGenerator::for_inline(ciMethod* m, float expected_uses) {
326 if (InlineTree::check_can_parse(m) != nullptr) return nullptr;
327 return new ParseGenerator(m, expected_uses);
328 }
329
330 // As a special case, the JVMS passed to this CallGenerator is
331 // for the method execution already in progress, not just the JVMS
332 // of the caller. Thus, this CallGenerator cannot be mixed with others!
333 CallGenerator* CallGenerator::for_osr(ciMethod* m, int osr_bci) {
334 if (InlineTree::check_can_parse(m) != nullptr) return nullptr;
335 float past_uses = m->interpreter_invocation_count();
336 float expected_uses = past_uses;
337 return new ParseGenerator(m, expected_uses, true);
338 }
339
340 CallGenerator* CallGenerator::for_direct_call(ciMethod* m, bool separate_io_proj) {
341 assert(!m->is_abstract(), "for_direct_call mismatch");
342 return new DirectCallGenerator(m, separate_io_proj);
343 }
344
345 CallGenerator* CallGenerator::for_virtual_call(ciMethod* m, int vtable_index) {
346 assert(!m->is_static(), "for_virtual_call mismatch");
347 assert(!m->is_method_handle_intrinsic(), "should be a direct call");
348 return new VirtualCallGenerator(m, vtable_index, false /*separate_io_projs*/);
349 }
350
351 // Allow inlining decisions to be delayed
352 class LateInlineCallGenerator : public DirectCallGenerator {
353 private:
354 jlong _unique_id; // unique id for log compilation
355 bool _is_pure_call; // a hint that the call doesn't have important side effects to care about
356
357 protected:
358 CallGenerator* _inline_cg;
359 virtual bool do_late_inline_check(Compile* C, JVMState* jvms) { return true; }
360 virtual CallGenerator* inline_cg() const { return _inline_cg; }
361 virtual bool is_pure_call() const { return _is_pure_call; }
362
363 public:
364 LateInlineCallGenerator(ciMethod* method, CallGenerator* inline_cg, bool is_pure_call = false) :
365 DirectCallGenerator(method, true), _unique_id(0), _is_pure_call(is_pure_call), _inline_cg(inline_cg) {}
366
367 virtual bool is_late_inline() const { return true; }
368
369 // Convert the CallStaticJava into an inline
370 virtual void do_late_inline();
371
372 virtual JVMState* generate(JVMState* jvms) {
373 Compile *C = Compile::current();
374
375 C->log_inline_id(this);
376
377 // Record that this call site should be revisited once the main
378 // parse is finished.
379 if (!is_mh_late_inline()) {
380 C->add_late_inline(this);
381 }
382
383 // Emit the CallStaticJava and request separate projections so
384 // that the late inlining logic can distinguish between fall
385 // through and exceptional uses of the memory and io projections
386 // as is done for allocations and macro expansion.
387 return DirectCallGenerator::generate(jvms);
388 }
389
390 virtual void set_unique_id(jlong id) {
391 _unique_id = id;
392 }
393
394 virtual jlong unique_id() const {
395 return _unique_id;
396 }
397
398 virtual CallGenerator* inline_cg() {
399 return _inline_cg;
400 }
401
402 virtual CallGenerator* with_call_node(CallNode* call) {
403 LateInlineCallGenerator* cg = new LateInlineCallGenerator(method(), _inline_cg, _is_pure_call);
404 cg->set_call_node(call->as_CallStaticJava());
405 return cg;
406 }
407 };
408
409 CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) {
410 return new LateInlineCallGenerator(method, inline_cg);
411 }
412
413 class LateInlineMHCallGenerator : public LateInlineCallGenerator {
414 ciMethod* _caller;
415 bool _input_not_const;
416
417 virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
418
419 public:
420 LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) :
421 LateInlineCallGenerator(callee, nullptr), _caller(caller), _input_not_const(input_not_const) {}
422
423 virtual bool is_mh_late_inline() const { return true; }
424
425 // Convert the CallStaticJava into an inline
426 virtual void do_late_inline();
427
428 virtual JVMState* generate(JVMState* jvms) {
429 JVMState* new_jvms = LateInlineCallGenerator::generate(jvms);
430
431 Compile* C = Compile::current();
432 if (_input_not_const) {
433 // inlining won't be possible so no need to enqueue right now.
434 call_node()->set_generator(this);
435 } else {
436 C->add_late_inline(this);
437 }
438 return new_jvms;
439 }
440
441 virtual CallGenerator* with_call_node(CallNode* call) {
442 LateInlineMHCallGenerator* cg = new LateInlineMHCallGenerator(_caller, method(), _input_not_const);
443 cg->set_call_node(call->as_CallStaticJava());
444 return cg;
445 }
446 };
447
448 bool LateInlineMHCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
449 // When inlining a virtual call, the null check at the call and the call itself can throw. These 2 paths have different
450 // expression stacks which causes late inlining to break. The MH invoker is not expected to be called from a method with
451 // exception handlers. When there is no exception handler, GraphKit::builtin_throw() pops the stack which solves the issue
452 // of late inlining with exceptions.
453 assert(!jvms->method()->has_exception_handlers() ||
454 (method()->intrinsic_id() != vmIntrinsics::_linkToVirtual &&
455 method()->intrinsic_id() != vmIntrinsics::_linkToInterface), "no exception handler expected");
456 // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
457 bool allow_inline = C->inlining_incrementally();
458 bool input_not_const = true;
459 CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), allow_inline, input_not_const);
460 assert(!input_not_const, "sanity"); // shouldn't have been scheduled for inlining in the first place
461
462 if (cg != nullptr) {
463 // AlwaysIncrementalInline causes for_method_handle_inline() to
464 // return a LateInlineCallGenerator. Extract the
465 // InlineCallGenerator from it.
466 if (AlwaysIncrementalInline && cg->is_late_inline() && !cg->is_virtual_late_inline()) {
467 cg = cg->inline_cg();
468 assert(cg != nullptr, "inline call generator expected");
469 }
470
471 if (!allow_inline) {
472 C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE,
473 "late method handle call resolution");
474 }
475 assert(!cg->is_late_inline() || cg->is_mh_late_inline() || cg->is_virtual_late_inline() ||
476 AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
477 _inline_cg = cg;
478 return true;
479 } else {
480 // Method handle call which has a constant appendix argument should be either inlined or replaced with a direct call
481 // unless there's a signature mismatch between caller and callee. If the failure occurs, there's not much to be improved later,
482 // so don't reinstall the generator to avoid pushing the generator between IGVN and incremental inlining indefinitely.
483 return false;
484 }
485 }
486
487 CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) {
488 assert(IncrementalInlineMH, "required");
489 Compile::current()->mark_has_mh_late_inlines();
490 CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const);
491 return cg;
492 }
493
494 // Allow inlining decisions to be delayed
495 class LateInlineVirtualCallGenerator : public VirtualCallGenerator {
496 private:
497 jlong _unique_id; // unique id for log compilation
498 CallGenerator* _inline_cg;
499 ciMethod* _callee;
500 bool _is_pure_call;
501 float _prof_factor;
502
503 protected:
504 virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
505 virtual CallGenerator* inline_cg() const { return _inline_cg; }
506 virtual bool is_pure_call() const { return _is_pure_call; }
507
508 public:
509 LateInlineVirtualCallGenerator(ciMethod* method, int vtable_index, float prof_factor)
510 : VirtualCallGenerator(method, vtable_index, true /*separate_io_projs*/),
511 _unique_id(0), _inline_cg(nullptr), _callee(nullptr), _is_pure_call(false), _prof_factor(prof_factor) {
512 assert(IncrementalInlineVirtual, "required");
513 }
514
515 virtual bool is_late_inline() const { return true; }
516
517 virtual bool is_virtual_late_inline() const { return true; }
518
519 // Convert the CallDynamicJava into an inline
520 virtual void do_late_inline();
521
522 virtual ciMethod* callee_method() {
523 return _callee;
524 }
525
526 virtual void set_callee_method(ciMethod* m) {
527 assert(_callee == nullptr || _callee == m, "repeated inline attempt with different callee");
528 _callee = m;
529 }
530
531 virtual JVMState* generate(JVMState* jvms) {
532 // Emit the CallDynamicJava and request separate projections so
533 // that the late inlining logic can distinguish between fall
534 // through and exceptional uses of the memory and io projections
535 // as is done for allocations and macro expansion.
536 JVMState* new_jvms = VirtualCallGenerator::generate(jvms);
537 if (call_node() != nullptr) {
538 call_node()->set_generator(this);
539 }
540 return new_jvms;
541 }
542
543 virtual void set_unique_id(jlong id) {
544 _unique_id = id;
545 }
546
547 virtual jlong unique_id() const {
548 return _unique_id;
549 }
550
551 virtual CallGenerator* with_call_node(CallNode* call) {
552 LateInlineVirtualCallGenerator* cg = new LateInlineVirtualCallGenerator(method(), vtable_index(), _prof_factor);
553 cg->set_call_node(call->as_CallDynamicJava());
554 return cg;
555 }
556 };
557
558 bool LateInlineVirtualCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
559 // Method handle linker case is handled in CallDynamicJavaNode::Ideal().
560 // Unless inlining is performed, _override_symbolic_info bit will be set in DirectCallGenerator::generate().
561
562 // Implicit receiver null checks introduce problems when exception states are combined.
563 Node* receiver = jvms->map()->argument(jvms, 0);
564 const Type* recv_type = C->initial_gvn()->type(receiver);
565 if (recv_type->maybe_null()) {
566 C->inline_printer()->record(method(), call_node()->jvms(), InliningResult::FAILURE,
567 "late call devirtualization failed (receiver may be null)");
568 return false;
569 }
570 // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
571 bool allow_inline = C->inlining_incrementally();
572 if (!allow_inline && _callee->holder()->is_interface()) {
573 // Don't convert the interface call to a direct call guarded by an interface subtype check.
574 C->inline_printer()->record(method(), call_node()->jvms(), InliningResult::FAILURE,
575 "late call devirtualization failed (interface call)");
576 return false;
577 }
578 CallGenerator* cg = C->call_generator(_callee,
579 vtable_index(),
580 false /*call_does_dispatch*/,
581 jvms,
582 allow_inline,
583 _prof_factor,
584 nullptr /*speculative_receiver_type*/,
585 true /*allow_intrinsics*/);
586
587 if (cg != nullptr) {
588 if (!allow_inline) {
589 C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE, "late call devirtualization");
590 }
591 assert(!cg->is_late_inline() || cg->is_mh_late_inline() || AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
592 _inline_cg = cg;
593 return true;
594 } else {
595 // Virtual call which provably doesn't dispatch should be either inlined or replaced with a direct call.
596 assert(false, "no progress");
597 return false;
598 }
599 }
600
601 CallGenerator* CallGenerator::for_late_inline_virtual(ciMethod* m, int vtable_index, float prof_factor) {
602 assert(IncrementalInlineVirtual, "required");
603 assert(!m->is_static(), "for_virtual_call mismatch");
604 assert(!m->is_method_handle_intrinsic(), "should be a direct call");
605 return new LateInlineVirtualCallGenerator(m, vtable_index, prof_factor);
606 }
607
608 void LateInlineCallGenerator::do_late_inline() {
609 CallGenerator::do_late_inline_helper();
610 }
611
612 void LateInlineMHCallGenerator::do_late_inline() {
613 CallGenerator::do_late_inline_helper();
614 }
615
616 void LateInlineVirtualCallGenerator::do_late_inline() {
617 assert(_callee != nullptr, "required"); // set up in CallDynamicJavaNode::Ideal
618 CallGenerator::do_late_inline_helper();
619 }
620
621 void CallGenerator::do_late_inline_helper() {
622 assert(is_late_inline(), "only late inline allowed");
623
624 // Can't inline it
625 CallNode* call = call_node();
626 if (call == nullptr || call->outcnt() == 0 ||
627 call->in(0) == nullptr || call->in(0)->is_top()) {
628 return;
629 }
630
631 const TypeTuple* r = call->tf()->domain_cc();
632 for (uint i1 = TypeFunc::Parms; i1 < r->cnt(); i1++) {
633 if (call->in(i1)->is_top() && r->field_at(i1) != Type::HALF) {
634 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
635 return;
636 }
637 }
638
639 if (call->in(TypeFunc::Memory)->is_top()) {
640 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
641 return;
642 }
643 if (call->in(TypeFunc::Memory)->is_MergeMem()) {
644 MergeMemNode* merge_mem = call->in(TypeFunc::Memory)->as_MergeMem();
645 if (merge_mem->base_memory() == merge_mem->empty_memory()) {
646 return; // dead path
647 }
648 }
649
650 // check for unreachable loop
651 // Similar to incremental inlining, don't assert that all call
652 // projections are still there for post-parse call devirtualization.
653 bool do_asserts = !is_mh_late_inline() && !is_virtual_late_inline();
654 CallProjections* callprojs = call->extract_projections(true, do_asserts);
655 if ((callprojs->fallthrough_catchproj == call->in(0)) ||
656 (callprojs->catchall_catchproj == call->in(0)) ||
657 (callprojs->fallthrough_memproj == call->in(TypeFunc::Memory)) ||
658 (callprojs->catchall_memproj == call->in(TypeFunc::Memory)) ||
659 (callprojs->fallthrough_ioproj == call->in(TypeFunc::I_O)) ||
660 (callprojs->catchall_ioproj == call->in(TypeFunc::I_O)) ||
661 (callprojs->exobj != nullptr && call->find_edge(callprojs->exobj) != -1)) {
662 return;
663 }
664
665 Compile* C = Compile::current();
666
667 uint endoff = call->jvms()->endoff();
668 if (C->inlining_incrementally()) {
669 // No reachability edges should be present when incremental inlining takes place.
670 // Inlining logic doesn't expect any extra edges past debug info and fails with
671 // an assert in SafePointNode::grow_stack.
672 assert(endoff == call->req(), "reachability edges not supported");
673 } else {
674 if (call->req() > endoff) { // reachability edges present
675 assert(OptimizeReachabilityFences, "required");
676 return; // keep the original call node as the holder of reachability info
677 }
678 }
679
680 // Remove inlined methods from Compiler's lists.
681 if (call->is_macro()) {
682 C->remove_macro_node(call);
683 }
684
685
686 bool result_not_used = true;
687 for (uint i = 0; i < callprojs->nb_resproj; i++) {
688 if (callprojs->resproj[i] != nullptr) {
689 if (callprojs->resproj[i]->outcnt() != 0) {
690 result_not_used = false;
691 }
692 if (call->find_edge(callprojs->resproj[i]) != -1) {
693 return;
694 }
695 }
696 }
697
698 if (is_pure_call() && result_not_used) {
699 // The call is marked as pure (no important side effects), but result isn't used.
700 // It's safe to remove the call.
701 GraphKit kit(call->jvms());
702 kit.replace_call(call, C->top(), true, do_asserts);
703 } else {
704 // Make a clone of the JVMState that appropriate to use for driving a parse
705 JVMState* old_jvms = call->jvms();
706 JVMState* jvms = old_jvms->clone_shallow(C);
707 uint size = call->req();
708 SafePointNode* map = new SafePointNode(size, jvms);
709 for (uint i1 = 0; i1 < size; i1++) {
710 map->init_req(i1, call->in(i1));
711 }
712 // Call node has in(ReturnAdr) set to top() node.
713 // We have to set map->in(ReturnAdr) to correct value
714 // because it is used by uncommon traps.
715 Node* ret_adr = C->start()->proj_out_or_null(TypeFunc::ReturnAdr);
716 precond(ret_adr != nullptr);
717 map->set_req(TypeFunc::ReturnAdr, ret_adr);
718
719 PhaseGVN& gvn = *C->initial_gvn();
720 // Make sure the state is a MergeMem for parsing.
721 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
722 Node* mem = MergeMemNode::make(map->in(TypeFunc::Memory));
723 gvn.set_type_bottom(mem);
724 map->set_req(TypeFunc::Memory, mem);
725 }
726
727 // blow away old call arguments
728 for (uint i1 = TypeFunc::Parms; i1 < r->cnt(); i1++) {
729 map->set_req(i1, C->top());
730 }
731 jvms->set_map(map);
732 precond(ret_adr == jvms->map()->returnadr());
733
734 // Make enough space in the expression stack to transfer
735 // the incoming arguments and return value.
736 map->ensure_stack(jvms, jvms->method()->max_stack());
737 const TypeTuple* domain_sig = call->_tf->domain_sig();
738 uint nargs = method()->arg_size();
739 assert(domain_sig->cnt() - TypeFunc::Parms == nargs, "inconsistent signature");
740
741 uint j = TypeFunc::Parms;
742 int arg_num = 0;
743 for (uint i1 = 0; i1 < nargs; i1++) {
744 const Type* t = domain_sig->field_at(TypeFunc::Parms + i1);
745 if (t->is_inlinetypeptr() && !method()->mismatch() && method()->is_scalarized_arg(arg_num)) {
746 // Inline type arguments are not passed by reference: we get an argument per
747 // field of the inline type. Build InlineTypeNodes from the inline type arguments.
748 GraphKit arg_kit(jvms, &gvn);
749 Node* vt = InlineTypeNode::make_from_multi(&arg_kit, call, t->inline_klass(), j, /* in= */ true, /* null_free= */ !t->maybe_null());
750 // GraphKit::access_load_at() may be called from InlineTypeNode::make_from_multi() and it may change the map
751 // that arg_kit uses.
752 map = arg_kit.map();
753 map->set_control(arg_kit.control());
754 map->set_argument(jvms, i1, vt);
755 } else {
756 map->set_argument(jvms, i1, call->in(j++));
757 }
758 if (t != Type::HALF) {
759 arg_num++;
760 }
761 }
762
763 C->log_late_inline(this);
764
765 // JVMState is ready, so time to perform some checks and prepare for inlining attempt.
766 if (!do_late_inline_check(C, jvms)) {
767 map->disconnect_inputs(C);
768 return;
769 }
770
771 // Check if we are late inlining a method handle call that returns an inline type as fields.
772 Node* buffer_oop = nullptr;
773 ciMethod* inline_method = inline_cg()->method();
774 ciType* return_type = inline_method->return_type();
775 if (!call->tf()->returns_inline_type_as_fields() &&
776 return_type->is_inlinetype() && return_type->as_inline_klass()->can_be_returned_as_fields()) {
777 assert(is_mh_late_inline(), "Unexpected return type");
778
779 // Allocate a buffer for the inline type returned as fields because the caller expects an oop return.
780 // Do this before the method handle call in case the buffer allocation triggers deoptimization and
781 // we need to "re-execute" the call in the interpreter (to make sure the call is only executed once).
782 GraphKit arg_kit(jvms, &gvn);
783 {
784 PreserveReexecuteState preexecs(&arg_kit);
785 arg_kit.jvms()->set_should_reexecute(true);
786 arg_kit.inc_sp(nargs);
787 Node* klass_node = arg_kit.makecon(TypeKlassPtr::make(return_type->as_inline_klass()));
788 buffer_oop = arg_kit.new_instance(klass_node, nullptr, nullptr, /* deoptimize_on_exception */ true);
789 }
790 jvms = arg_kit.transfer_exceptions_into_jvms();
791 }
792
793 // Setup default node notes to be picked up by the inlining
794 Node_Notes* old_nn = C->node_notes_at(call->_idx);
795 if (old_nn != nullptr) {
796 Node_Notes* entry_nn = old_nn->clone(C);
797 entry_nn->set_jvms(jvms);
798 C->set_default_node_notes(entry_nn);
799 }
800
801 // Now perform the inlining using the synthesized JVMState
802 JVMState* new_jvms = inline_cg()->generate(jvms);
803 if (new_jvms == nullptr) return; // no change
804 if (C->failing()) return;
805
806 if (is_mh_late_inline()) {
807 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (method handle)");
808 } else if (is_string_late_inline()) {
809 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (string method)");
810 } else if (is_boxing_late_inline()) {
811 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (boxing method)");
812 } else if (is_vector_reboxing_late_inline()) {
813 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (vector reboxing method)");
814 } else {
815 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded");
816 }
817
818 if (inline_cg()->is_inline()) {
819 C->set_has_loops(C->has_loops() || inline_method->has_loops());
820 C->env()->notice_inlined_method(inline_method);
821 }
822 C->set_inlining_progress(true);
823
824 // Find the result object and capture any exceptional control flow.
825 GraphKit kit(new_jvms);
826 Node* result = C->top();
827
828 assert(!C->do_cleanup(), "already set");
829 if (kit.stopped()) {
830 C->set_do_cleanup(true); // path is dead; needs cleanup
831 } else {
832 result = kit.pop_node(method()->return_type()->basic_type());
833 if (result != C->top() && !result_not_used) {
834 if (call->is_CallStaticJava() &&
835 call->as_CallStaticJava()->is_boxing_method()) {
836 result = kit.must_be_not_null(result, false);
837 }
838 // Handle inline type returns
839 InlineTypeNode* vt = result->isa_InlineType();
840 if (vt != nullptr) {
841 if (call->tf()->returns_inline_type_as_fields()) {
842 vt->replace_call_results(&kit, call, C);
843 } else {
844 // Result might still be allocated (for example, if it has been stored to a non-flat field)
845 if (!vt->is_allocated(&kit.gvn())) {
846 assert(buffer_oop != nullptr, "should have allocated a buffer");
847 RegionNode* region = new RegionNode(3);
848
849 // Check if result is null
850 Node* null_ctl = kit.top();
851 kit.null_check_common(vt->get_null_marker(), T_INT, false, &null_ctl);
852 region->init_req(1, null_ctl);
853 PhiNode* oop = PhiNode::make(region, kit.gvn().zerocon(T_OBJECT), TypeInstPtr::make(TypePtr::BotPTR, vt->type()->inline_klass()));
854 Node* init_mem = kit.reset_memory();
855 PhiNode* mem = PhiNode::make(region, init_mem, Type::MEMORY, TypePtr::BOTTOM);
856
857 // Not null, initialize the buffer
858 kit.set_all_memory(init_mem);
859
860 Node* payload_ptr = kit.basic_plus_adr(buffer_oop, kit.gvn().type(vt)->inline_klass()->payload_offset());
861 vt->store_flat(&kit, buffer_oop, payload_ptr, false, true, true, IN_HEAP | MO_UNORDERED);
862 // Do not let stores that initialize this buffer be reordered with a subsequent
863 // store that would make this buffer accessible by other threads.
864 AllocateNode* alloc = AllocateNode::Ideal_allocation(buffer_oop);
865 assert(alloc != nullptr, "must have an allocation node");
866 kit.insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
867 region->init_req(2, kit.control());
868 oop->init_req(2, buffer_oop);
869 mem->init_req(2, kit.merged_memory());
870
871 // Update oop input to buffer
872 kit.gvn().hash_delete(vt);
873 vt->set_oop(kit.gvn(), kit.gvn().transform(oop));
874 vt->set_is_buffered(kit.gvn());
875 vt = kit.gvn().transform(vt)->as_InlineType();
876
877 kit.set_control(kit.gvn().transform(region));
878 kit.set_all_memory(kit.gvn().transform(mem));
879 kit.record_for_igvn(region);
880 kit.record_for_igvn(oop);
881 kit.record_for_igvn(mem);
882 }
883 result = vt;
884 }
885 DEBUG_ONLY(buffer_oop = nullptr);
886 } else {
887 assert(!call->tf()->returns_inline_type_as_fields() || !call->as_CallJava()->method()->return_type()->is_loaded(), "Unexpected return value");
888 }
889 assert(buffer_oop == nullptr, "unused buffer allocation");
890
891 // Note: scalarized results are guarded per projection
892 if (!call->tf()->returns_inline_type_as_fields()) {
893 assert(callprojs->nb_resproj == 1 && callprojs->resproj[0] != nullptr,
894 "single result projection expected");
895 // Limit result type propagation until next IGVN cleanup.
896 const Type* result_type = kit.gvn().type(callprojs->resproj[0]);
897 result = kit.gvn().transform(new OpaqueParseNode(C, result, result_type));
898 }
899 }
900 }
901
902 kit.replace_call(call, result, true, do_asserts);
903 }
904 }
905
906 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
907
908 public:
909 LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
910 LateInlineCallGenerator(method, inline_cg) {}
911
912 virtual JVMState* generate(JVMState* jvms) {
913 Compile *C = Compile::current();
914
915 C->log_inline_id(this);
916
917 C->add_string_late_inline(this);
918
919 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
920 return new_jvms;
921 }
922
923 virtual bool is_string_late_inline() const { return true; }
924
925 virtual CallGenerator* with_call_node(CallNode* call) {
926 LateInlineStringCallGenerator* cg = new LateInlineStringCallGenerator(method(), _inline_cg);
927 cg->set_call_node(call->as_CallStaticJava());
928 return cg;
929 }
930 };
931
932 CallGenerator* CallGenerator::for_string_late_inline(ciMethod* method, CallGenerator* inline_cg) {
933 return new LateInlineStringCallGenerator(method, inline_cg);
934 }
935
936 class LateInlineBoxingCallGenerator : public LateInlineCallGenerator {
937
938 public:
939 LateInlineBoxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
940 LateInlineCallGenerator(method, inline_cg, /*is_pure=*/true) {}
941
942 virtual JVMState* generate(JVMState* jvms) {
943 Compile *C = Compile::current();
944
945 C->log_inline_id(this);
946
947 C->add_boxing_late_inline(this);
948
949 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
950 return new_jvms;
951 }
952
953 virtual bool is_boxing_late_inline() const { return true; }
954
955 virtual CallGenerator* with_call_node(CallNode* call) {
956 LateInlineBoxingCallGenerator* cg = new LateInlineBoxingCallGenerator(method(), _inline_cg);
957 cg->set_call_node(call->as_CallStaticJava());
958 return cg;
959 }
960 };
961
962 CallGenerator* CallGenerator::for_boxing_late_inline(ciMethod* method, CallGenerator* inline_cg) {
963 return new LateInlineBoxingCallGenerator(method, inline_cg);
964 }
965
966 class LateInlineVectorReboxingCallGenerator : public LateInlineCallGenerator {
967
968 public:
969 LateInlineVectorReboxingCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
970 LateInlineCallGenerator(method, inline_cg, /*is_pure=*/true) {}
971
972 virtual JVMState* generate(JVMState* jvms) {
973 Compile *C = Compile::current();
974
975 C->log_inline_id(this);
976
977 C->add_vector_reboxing_late_inline(this);
978
979 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
980 return new_jvms;
981 }
982
983 virtual bool is_vector_reboxing_late_inline() const { return true; }
984
985 virtual CallGenerator* with_call_node(CallNode* call) {
986 LateInlineVectorReboxingCallGenerator* cg = new LateInlineVectorReboxingCallGenerator(method(), _inline_cg);
987 cg->set_call_node(call->as_CallStaticJava());
988 return cg;
989 }
990 };
991
992 // static CallGenerator* for_vector_reboxing_late_inline(ciMethod* m, CallGenerator* inline_cg);
993 CallGenerator* CallGenerator::for_vector_reboxing_late_inline(ciMethod* method, CallGenerator* inline_cg) {
994 return new LateInlineVectorReboxingCallGenerator(method, inline_cg);
995 }
996
997 //------------------------PredictedCallGenerator------------------------------
998 // Internal class which handles all out-of-line calls checking receiver type.
999 class PredictedCallGenerator : public CallGenerator {
1000 ciKlass* _predicted_receiver;
1001 CallGenerator* _if_missed;
1002 CallGenerator* _if_hit;
1003 float _hit_prob;
1004 bool _exact_check;
1005
1006 public:
1007 PredictedCallGenerator(ciKlass* predicted_receiver,
1008 CallGenerator* if_missed,
1009 CallGenerator* if_hit, bool exact_check,
1010 float hit_prob)
1011 : CallGenerator(if_missed->method())
1012 {
1013 // The call profile data may predict the hit_prob as extreme as 0 or 1.
1014 // Remove the extremes values from the range.
1015 if (hit_prob > PROB_MAX) hit_prob = PROB_MAX;
1016 if (hit_prob < PROB_MIN) hit_prob = PROB_MIN;
1017
1018 _predicted_receiver = predicted_receiver;
1019 _if_missed = if_missed;
1020 _if_hit = if_hit;
1021 _hit_prob = hit_prob;
1022 _exact_check = exact_check;
1023 }
1024
1025 virtual bool is_virtual() const { return true; }
1026 virtual bool is_inline() const { return _if_hit->is_inline(); }
1027 virtual bool is_deferred() const { return _if_hit->is_deferred(); }
1028
1029 virtual JVMState* generate(JVMState* jvms);
1030 };
1031
1032
1033 CallGenerator* CallGenerator::for_predicted_call(ciKlass* predicted_receiver,
1034 CallGenerator* if_missed,
1035 CallGenerator* if_hit,
1036 float hit_prob) {
1037 return new PredictedCallGenerator(predicted_receiver, if_missed, if_hit,
1038 /*exact_check=*/true, hit_prob);
1039 }
1040
1041 CallGenerator* CallGenerator::for_guarded_call(ciKlass* guarded_receiver,
1042 CallGenerator* if_missed,
1043 CallGenerator* if_hit) {
1044 return new PredictedCallGenerator(guarded_receiver, if_missed, if_hit,
1045 /*exact_check=*/false, PROB_ALWAYS);
1046 }
1047
1048 JVMState* PredictedCallGenerator::generate(JVMState* jvms) {
1049 GraphKit kit(jvms);
1050 PhaseGVN& gvn = kit.gvn();
1051 // We need an explicit receiver null_check before checking its type.
1052 // We share a map with the caller, so his JVMS gets adjusted.
1053 Node* receiver = kit.argument(0);
1054 CompileLog* log = kit.C->log();
1055 if (log != nullptr) {
1056 log->elem("predicted_call bci='%d' exact='%d' klass='%d'",
1057 jvms->bci(), (_exact_check ? 1 : 0), log->identify(_predicted_receiver));
1058 }
1059
1060 receiver = kit.null_check_receiver_before_call(method());
1061 if (kit.stopped()) {
1062 return kit.transfer_exceptions_into_jvms();
1063 }
1064
1065 // Make a copy of the replaced nodes in case we need to restore them
1066 ReplacedNodes replaced_nodes = kit.map()->replaced_nodes();
1067 replaced_nodes.clone();
1068
1069 Node* casted_receiver = receiver; // will get updated in place...
1070 Node* slow_ctl = nullptr;
1071 if (_exact_check) {
1072 slow_ctl = kit.type_check_receiver(receiver, _predicted_receiver, _hit_prob,
1073 &casted_receiver);
1074 } else {
1075 slow_ctl = kit.subtype_check_receiver(receiver, _predicted_receiver,
1076 &casted_receiver);
1077 }
1078
1079 SafePointNode* slow_map = nullptr;
1080 JVMState* slow_jvms = nullptr;
1081 { PreserveJVMState pjvms(&kit);
1082 kit.set_control(slow_ctl);
1083 if (!kit.stopped()) {
1084 slow_jvms = _if_missed->generate(kit.sync_jvms());
1085 if (kit.failing())
1086 return nullptr; // might happen because of NodeCountInliningCutoff
1087 assert(slow_jvms != nullptr, "must be");
1088 kit.add_exception_states_from(slow_jvms);
1089 kit.set_map(slow_jvms->map());
1090 if (!kit.stopped())
1091 slow_map = kit.stop();
1092 }
1093 }
1094
1095 if (kit.stopped()) {
1096 // Instance does not match the predicted type.
1097 kit.set_jvms(slow_jvms);
1098 return kit.transfer_exceptions_into_jvms();
1099 }
1100
1101 // Fall through if the instance matches the desired type.
1102 kit.replace_in_map(receiver, casted_receiver);
1103
1104 // Make the hot call:
1105 JVMState* new_jvms = _if_hit->generate(kit.sync_jvms());
1106 if (kit.failing()) {
1107 return nullptr;
1108 }
1109 if (new_jvms == nullptr) {
1110 // Inline failed, so make a direct call.
1111 assert(_if_hit->is_inline(), "must have been a failed inline");
1112 CallGenerator* cg = CallGenerator::for_direct_call(_if_hit->method());
1113 new_jvms = cg->generate(kit.sync_jvms());
1114 }
1115 kit.add_exception_states_from(new_jvms);
1116 kit.set_jvms(new_jvms);
1117
1118 // Need to merge slow and fast?
1119 if (slow_map == nullptr) {
1120 // The fast path is the only path remaining.
1121 return kit.transfer_exceptions_into_jvms();
1122 }
1123
1124 if (kit.stopped()) {
1125 // Inlined method threw an exception, so it's just the slow path after all.
1126 kit.set_jvms(slow_jvms);
1127 return kit.transfer_exceptions_into_jvms();
1128 }
1129
1130 // There are 2 branches and the replaced nodes are only valid on
1131 // one: restore the replaced nodes to what they were before the
1132 // branch.
1133 kit.map()->set_replaced_nodes(replaced_nodes);
1134
1135 // Finish the diamond.
1136 kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
1137 RegionNode* region = new RegionNode(3);
1138 region->init_req(1, kit.control());
1139 region->init_req(2, slow_map->control());
1140 kit.set_control(gvn.transform(region));
1141 Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
1142 iophi->set_req(2, slow_map->i_o());
1143 kit.set_i_o(gvn.transform(iophi));
1144 // Merge memory
1145 kit.merge_memory(slow_map->merged_memory(), region, 2);
1146 // Transform new memory Phis.
1147 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1148 Node* phi = mms.memory();
1149 if (phi->is_Phi() && phi->in(0) == region) {
1150 mms.set_memory(gvn.transform(phi));
1151 }
1152 }
1153 uint tos = kit.jvms()->stkoff() + kit.sp();
1154 uint limit = slow_map->req();
1155 for (uint i = TypeFunc::Parms; i < limit; i++) {
1156 // Skip unused stack slots; fast forward to monoff();
1157 if (i == tos) {
1158 i = kit.jvms()->monoff();
1159 if( i >= limit ) break;
1160 }
1161 Node* m = kit.map()->in(i);
1162 Node* n = slow_map->in(i);
1163 if (m != n) {
1164 #ifdef ASSERT
1165 if (m->is_InlineType() != n->is_InlineType()) {
1166 InlineTypeNode* unique_vt = m->is_InlineType() ? m->as_InlineType() : n->as_InlineType();
1167 assert(unique_vt->is_allocated(&gvn), "InlineType can be merged with an oop only if it is allocated");
1168 }
1169 #endif
1170 const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
1171 Node* phi = PhiNode::make(region, m, t);
1172 phi->set_req(2, n);
1173 kit.map()->set_req(i, gvn.transform(phi));
1174 }
1175 }
1176 return kit.transfer_exceptions_into_jvms();
1177 }
1178
1179
1180 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline) {
1181 assert(callee->is_method_handle_intrinsic(), "for_method_handle_call mismatch");
1182 bool input_not_const;
1183 CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, allow_inline, input_not_const);
1184 Compile* C = Compile::current();
1185 bool should_delay = C->should_delay_inlining();
1186 if (cg != nullptr) {
1187 if (should_delay && IncrementalInlineMH) {
1188 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1189 } else {
1190 return cg;
1191 }
1192 }
1193 int bci = jvms->bci();
1194 ciCallProfile profile = caller->call_profile_at_bci(bci);
1195 int call_site_count = caller->scale_count(profile.count());
1196
1197 if (IncrementalInlineMH && (AlwaysIncrementalInline ||
1198 (call_site_count > 0 && (should_delay || input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())))) {
1199 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1200 } else {
1201 // Out-of-line call.
1202 return CallGenerator::for_direct_call(callee);
1203 }
1204 }
1205
1206
1207 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1208 GraphKit kit(jvms);
1209 PhaseGVN& gvn = kit.gvn();
1210 Compile* C = kit.C;
1211 vmIntrinsics::ID iid = callee->intrinsic_id();
1212 input_not_const = true;
1213 if (StressMethodHandleLinkerInlining) {
1214 allow_inline = false;
1215 }
1216 switch (iid) {
1217 case vmIntrinsics::_invokeBasic:
1218 {
1219 // Get MethodHandle receiver:
1220 Node* receiver = kit.argument(0);
1221 if (receiver->Opcode() == Op_ConP) {
1222 input_not_const = false;
1223 const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
1224 if (recv_toop != nullptr) {
1225 ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
1226 const int vtable_index = Method::invalid_vtable_index;
1227
1228 if (!ciMethod::is_consistent_info(callee, target)) {
1229 print_inlining_failure(C, callee, jvms, "signatures mismatch");
1230 return nullptr;
1231 }
1232
1233 CallGenerator *cg = C->call_generator(target, vtable_index,
1234 false /* call_does_dispatch */,
1235 jvms,
1236 allow_inline,
1237 PROB_ALWAYS);
1238 return cg;
1239 } else {
1240 assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
1241 Type::str(receiver->bottom_type()));
1242 print_inlining_failure(C, callee, jvms, "receiver is always null");
1243 }
1244 } else {
1245 print_inlining_failure(C, callee, jvms, "receiver not constant");
1246 }
1247 } break;
1248
1249 case vmIntrinsics::_linkToVirtual:
1250 case vmIntrinsics::_linkToStatic:
1251 case vmIntrinsics::_linkToSpecial:
1252 case vmIntrinsics::_linkToInterface:
1253 {
1254 int nargs = callee->arg_size();
1255 // Get MemberName argument:
1256 Node* member_name = kit.argument(nargs - 1);
1257 if (member_name->Opcode() == Op_ConP) {
1258 input_not_const = false;
1259 const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1260 ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1261
1262 if (!ciMethod::is_consistent_info(callee, target)) {
1263 print_inlining_failure(C, callee, jvms, "signatures mismatch");
1264 return nullptr;
1265 }
1266
1267 // In lambda forms we erase signature types to avoid resolving issues
1268 // involving class loaders. When we optimize a method handle invoke
1269 // to a direct call we must cast the receiver and arguments to its
1270 // actual types.
1271 ciSignature* signature = target->signature();
1272 const int receiver_skip = target->is_static() ? 0 : 1;
1273 // Cast receiver to its type.
1274 if (!target->is_static()) {
1275 Node* recv = kit.argument(0);
1276 Node* casted_recv = kit.maybe_narrow_object_type(recv, signature->accessing_klass(), target->receiver_maybe_larval());
1277 if (casted_recv->is_top()) {
1278 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1279 return nullptr; // FIXME: effectively dead; issue a halt node instead
1280 } else if (casted_recv != recv) {
1281 kit.set_argument(0, casted_recv);
1282 }
1283 }
1284 // Cast reference arguments to its type.
1285 for (int i = 0, j = 0; i < signature->count(); i++) {
1286 ciType* t = signature->type_at(i);
1287 if (t->is_klass()) {
1288 Node* arg = kit.argument(receiver_skip + j);
1289 Node* casted_arg = kit.maybe_narrow_object_type(arg, t->as_klass(), false);
1290 if (casted_arg->is_top()) {
1291 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1292 return nullptr; // FIXME: effectively dead; issue a halt node instead
1293 } else if (casted_arg != arg) {
1294 kit.set_argument(receiver_skip + j, casted_arg);
1295 }
1296 }
1297 j += t->size(); // long and double take two slots
1298 }
1299
1300 // Try to get the most accurate receiver type
1301 const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual);
1302 const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1303 int vtable_index = Method::invalid_vtable_index;
1304 bool call_does_dispatch = false;
1305
1306 ciKlass* speculative_receiver_type = nullptr;
1307 if (is_virtual_or_interface) {
1308 ciInstanceKlass* klass = target->holder();
1309 Node* receiver_node = kit.argument(0);
1310 const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1311 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
1312 // optimize_virtual_call() takes 2 different holder
1313 // arguments for a corner case that doesn't apply here (see
1314 // Parse::do_call())
1315 target = C->optimize_virtual_call(caller, klass, klass,
1316 target, receiver_type, is_virtual,
1317 call_does_dispatch, vtable_index, // out-parameters
1318 false /* check_access */);
1319 // We lack profiling at this call but type speculation may
1320 // provide us with a type
1321 speculative_receiver_type = (receiver_type != nullptr) ? receiver_type->speculative_type() : nullptr;
1322 }
1323 CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1324 allow_inline,
1325 PROB_ALWAYS,
1326 speculative_receiver_type,
1327 true);
1328 return cg;
1329 } else {
1330 print_inlining_failure(C, callee, jvms, "member_name not constant");
1331 }
1332 } break;
1333
1334 case vmIntrinsics::_linkToNative:
1335 print_inlining_failure(C, callee, jvms, "native call");
1336 break;
1337
1338 default:
1339 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1340 break;
1341 }
1342 return nullptr;
1343 }
1344
1345 //------------------------PredicatedIntrinsicGenerator------------------------------
1346 // Internal class which handles all predicated Intrinsic calls.
1347 class PredicatedIntrinsicGenerator : public CallGenerator {
1348 CallGenerator* _intrinsic;
1349 CallGenerator* _cg;
1350
1351 public:
1352 PredicatedIntrinsicGenerator(CallGenerator* intrinsic,
1353 CallGenerator* cg)
1354 : CallGenerator(cg->method())
1355 {
1356 _intrinsic = intrinsic;
1357 _cg = cg;
1358 }
1359
1360 virtual bool is_virtual() const { return true; }
1361 virtual bool is_inline() const { return true; }
1362 virtual bool is_intrinsic() const { return true; }
1363
1364 virtual JVMState* generate(JVMState* jvms);
1365 };
1366
1367
1368 CallGenerator* CallGenerator::for_predicated_intrinsic(CallGenerator* intrinsic,
1369 CallGenerator* cg) {
1370 return new PredicatedIntrinsicGenerator(intrinsic, cg);
1371 }
1372
1373
1374 JVMState* PredicatedIntrinsicGenerator::generate(JVMState* jvms) {
1375 // The code we want to generate here is:
1376 // if (receiver == nullptr)
1377 // uncommon_Trap
1378 // if (predicate(0))
1379 // do_intrinsic(0)
1380 // else
1381 // if (predicate(1))
1382 // do_intrinsic(1)
1383 // ...
1384 // else
1385 // do_java_comp
1386
1387 GraphKit kit(jvms);
1388 PhaseGVN& gvn = kit.gvn();
1389
1390 CompileLog* log = kit.C->log();
1391 if (log != nullptr) {
1392 log->elem("predicated_intrinsic bci='%d' method='%d'",
1393 jvms->bci(), log->identify(method()));
1394 }
1395
1396 if (!method()->is_static()) {
1397 // We need an explicit receiver null_check before checking its type in predicate.
1398 // We share a map with the caller, so his JVMS gets adjusted.
1399 kit.null_check_receiver_before_call(method());
1400 if (kit.stopped()) {
1401 return kit.transfer_exceptions_into_jvms();
1402 }
1403 }
1404
1405 int n_predicates = _intrinsic->predicates_count();
1406 assert(n_predicates > 0, "sanity");
1407
1408 JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1409
1410 // Region for normal compilation code if intrinsic failed.
1411 Node* slow_region = new RegionNode(1);
1412
1413 int results = 0;
1414 for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1415 #ifdef ASSERT
1416 JVMState* old_jvms = kit.jvms();
1417 SafePointNode* old_map = kit.map();
1418 Node* old_io = old_map->i_o();
1419 Node* old_mem = old_map->memory();
1420 Node* old_exc = old_map->next_exception();
1421 #endif
1422 Node* else_ctrl = _intrinsic->generate_predicate(kit.sync_jvms(), predicate);
1423 #ifdef ASSERT
1424 // Assert(no_new_memory && no_new_io && no_new_exceptions) after generate_predicate.
1425 assert(old_jvms == kit.jvms(), "generate_predicate should not change jvm state");
1426 SafePointNode* new_map = kit.map();
1427 assert(old_io == new_map->i_o(), "generate_predicate should not change i_o");
1428 assert(old_mem == new_map->memory(), "generate_predicate should not change memory");
1429 assert(old_exc == new_map->next_exception(), "generate_predicate should not add exceptions");
1430 #endif
1431 if (!kit.stopped()) {
1432 PreserveJVMState pjvms(&kit);
1433 // Generate intrinsic code:
1434 JVMState* new_jvms = _intrinsic->generate(kit.sync_jvms());
1435 if (kit.failing()) {
1436 return nullptr;
1437 }
1438 if (new_jvms == nullptr) {
1439 // Intrinsic failed, use normal compilation path for this predicate.
1440 slow_region->add_req(kit.control());
1441 } else {
1442 kit.add_exception_states_from(new_jvms);
1443 kit.set_jvms(new_jvms);
1444 if (!kit.stopped()) {
1445 result_jvms[results++] = kit.jvms();
1446 }
1447 }
1448 }
1449 if (else_ctrl == nullptr) {
1450 else_ctrl = kit.C->top();
1451 }
1452 kit.set_control(else_ctrl);
1453 }
1454 if (!kit.stopped()) {
1455 // Final 'else' after predicates.
1456 slow_region->add_req(kit.control());
1457 }
1458 if (slow_region->req() > 1) {
1459 PreserveJVMState pjvms(&kit);
1460 // Generate normal compilation code:
1461 kit.set_control(gvn.transform(slow_region));
1462 JVMState* new_jvms = _cg->generate(kit.sync_jvms());
1463 if (kit.failing())
1464 return nullptr; // might happen because of NodeCountInliningCutoff
1465 assert(new_jvms != nullptr, "must be");
1466 kit.add_exception_states_from(new_jvms);
1467 kit.set_jvms(new_jvms);
1468 if (!kit.stopped()) {
1469 result_jvms[results++] = kit.jvms();
1470 }
1471 }
1472
1473 if (results == 0) {
1474 // All paths ended in uncommon traps.
1475 (void) kit.stop();
1476 return kit.transfer_exceptions_into_jvms();
1477 }
1478
1479 if (results == 1) { // Only one path
1480 kit.set_jvms(result_jvms[0]);
1481 return kit.transfer_exceptions_into_jvms();
1482 }
1483
1484 // Merge all paths.
1485 kit.C->set_has_split_ifs(true); // Has chance for split-if optimization
1486 RegionNode* region = new RegionNode(results + 1);
1487 Node* iophi = PhiNode::make(region, kit.i_o(), Type::ABIO);
1488 for (int i = 0; i < results; i++) {
1489 JVMState* jvms = result_jvms[i];
1490 int path = i + 1;
1491 SafePointNode* map = jvms->map();
1492 region->init_req(path, map->control());
1493 iophi->set_req(path, map->i_o());
1494 if (i == 0) {
1495 kit.set_jvms(jvms);
1496 } else {
1497 kit.merge_memory(map->merged_memory(), region, path);
1498 }
1499 }
1500 kit.set_control(gvn.transform(region));
1501 kit.set_i_o(gvn.transform(iophi));
1502 // Transform new memory Phis.
1503 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1504 Node* phi = mms.memory();
1505 if (phi->is_Phi() && phi->in(0) == region) {
1506 mms.set_memory(gvn.transform(phi));
1507 }
1508 }
1509
1510 // Merge debug info.
1511 Node** ins = NEW_RESOURCE_ARRAY(Node*, results);
1512 uint tos = kit.jvms()->stkoff() + kit.sp();
1513 Node* map = kit.map();
1514 uint limit = map->req();
1515 for (uint i = TypeFunc::Parms; i < limit; i++) {
1516 // Skip unused stack slots; fast forward to monoff();
1517 if (i == tos) {
1518 i = kit.jvms()->monoff();
1519 if( i >= limit ) break;
1520 }
1521 Node* n = map->in(i);
1522 ins[0] = n;
1523 const Type* t = gvn.type(n);
1524 bool needs_phi = false;
1525 for (int j = 1; j < results; j++) {
1526 JVMState* jvms = result_jvms[j];
1527 Node* jmap = jvms->map();
1528 Node* m = nullptr;
1529 if (jmap->req() > i) {
1530 m = jmap->in(i);
1531 if (m != n) {
1532 needs_phi = true;
1533 t = t->meet_speculative(gvn.type(m));
1534 }
1535 }
1536 ins[j] = m;
1537 }
1538 if (needs_phi) {
1539 Node* phi = PhiNode::make(region, n, t);
1540 for (int j = 1; j < results; j++) {
1541 phi->set_req(j + 1, ins[j]);
1542 }
1543 map->set_req(i, gvn.transform(phi));
1544 }
1545 }
1546
1547 return kit.transfer_exceptions_into_jvms();
1548 }
1549
1550 //-------------------------UncommonTrapCallGenerator-----------------------------
1551 // Internal class which handles all out-of-line calls checking receiver type.
1552 class UncommonTrapCallGenerator : public CallGenerator {
1553 Deoptimization::DeoptReason _reason;
1554 Deoptimization::DeoptAction _action;
1555
1556 public:
1557 UncommonTrapCallGenerator(ciMethod* m,
1558 Deoptimization::DeoptReason reason,
1559 Deoptimization::DeoptAction action)
1560 : CallGenerator(m)
1561 {
1562 _reason = reason;
1563 _action = action;
1564 }
1565
1566 virtual bool is_virtual() const { ShouldNotReachHere(); return false; }
1567 virtual bool is_trap() const { return true; }
1568
1569 virtual JVMState* generate(JVMState* jvms);
1570 };
1571
1572
1573 CallGenerator*
1574 CallGenerator::for_uncommon_trap(ciMethod* m,
1575 Deoptimization::DeoptReason reason,
1576 Deoptimization::DeoptAction action) {
1577 return new UncommonTrapCallGenerator(m, reason, action);
1578 }
1579
1580
1581 JVMState* UncommonTrapCallGenerator::generate(JVMState* jvms) {
1582 GraphKit kit(jvms);
1583 // Take the trap with arguments pushed on the stack. (Cf. null_check_receiver).
1584 // Callsite signature can be different from actual method being called (i.e _linkTo* sites).
1585 // Use callsite signature always.
1586 ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
1587 int nargs = declared_method->arg_size();
1588 kit.inc_sp(nargs);
1589 assert(nargs <= kit.sp() && kit.sp() <= jvms->stk_size(), "sane sp w/ args pushed");
1590 if (_reason == Deoptimization::Reason_class_check &&
1591 _action == Deoptimization::Action_maybe_recompile) {
1592 // Temp fix for 6529811
1593 // Don't allow uncommon_trap to override our decision to recompile in the event
1594 // of a class cast failure for a monomorphic call as it will never let us convert
1595 // the call to either bi-morphic or megamorphic and can lead to unc-trap loops
1596 bool keep_exact_action = true;
1597 kit.uncommon_trap(_reason, _action, nullptr, "monomorphic vcall checkcast", false, keep_exact_action);
1598 } else {
1599 kit.uncommon_trap(_reason, _action);
1600 }
1601 return kit.transfer_exceptions_into_jvms();
1602 }
1603
1604 // (Note: Moved hook_up_call to GraphKit::set_edges_for_java_call.)
1605
1606 // (Node: Merged hook_up_exits into ParseGenerator::generate.)