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