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 "opto/addnode.hpp"
33 #include "opto/callGenerator.hpp"
34 #include "opto/callnode.hpp"
35 #include "opto/castnode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/parse.hpp"
38 #include "opto/rootnode.hpp"
39 #include "opto/runtime.hpp"
40 #include "opto/subnode.hpp"
41 #include "runtime/os.inline.hpp"
42 #include "runtime/sharedRuntime.hpp"
43 #include "utilities/debug.hpp"
44
45 // Utility function.
46 const TypeFunc* CallGenerator::tf() const {
47 return TypeFunc::make(method());
48 }
49
50 bool CallGenerator::is_inlined_method_handle_intrinsic(JVMState* jvms, ciMethod* m) {
51 return is_inlined_method_handle_intrinsic(jvms->method(), jvms->bci(), m);
52 }
53
54 bool CallGenerator::is_inlined_method_handle_intrinsic(ciMethod* caller, int bci, ciMethod* m) {
55 ciMethod* symbolic_info = caller->get_method_at_bci(bci);
56 return is_inlined_method_handle_intrinsic(symbolic_info, m);
101 GraphKit& exits = parser.exits();
102
103 if (C->failing()) {
104 while (exits.pop_exception_state() != nullptr) ;
105 return nullptr;
106 }
107
108 assert(exits.jvms()->same_calls_as(jvms), "sanity");
109
110 // Simply return the exit state of the parser,
111 // augmented by any exceptional states.
112 return exits.transfer_exceptions_into_jvms();
113 }
114
115 //---------------------------DirectCallGenerator------------------------------
116 // Internal class which handles all out-of-line calls w/o receiver type checks.
117 class DirectCallGenerator : public CallGenerator {
118 private:
119 CallStaticJavaNode* _call_node;
120 // Force separate memory and I/O projections for the exceptional
121 // paths to facilitate late inlinig.
122 bool _separate_io_proj;
123
124 protected:
125 void set_call_node(CallStaticJavaNode* call) { _call_node = call; }
126
127 public:
128 DirectCallGenerator(ciMethod* method, bool separate_io_proj)
129 : CallGenerator(method),
130 _separate_io_proj(separate_io_proj)
131 {
132 }
133 virtual JVMState* generate(JVMState* jvms);
134
135 virtual CallNode* call_node() const { return _call_node; }
136 virtual CallGenerator* with_call_node(CallNode* call) {
137 DirectCallGenerator* dcg = new DirectCallGenerator(method(), _separate_io_proj);
138 dcg->set_call_node(call->as_CallStaticJava());
139 return dcg;
140 }
141 };
142
143 JVMState* DirectCallGenerator::generate(JVMState* jvms) {
144 GraphKit kit(jvms);
145 bool is_static = method()->is_static();
146 address target = is_static ? SharedRuntime::get_resolve_static_call_stub()
147 : SharedRuntime::get_resolve_opt_virtual_call_stub();
148
149 if (kit.C->log() != nullptr) {
150 kit.C->log()->elem("direct_call bci='%d'", jvms->bci());
151 }
194 {
195 assert(vtable_index == Method::invalid_vtable_index ||
196 vtable_index >= 0, "either invalid or usable");
197 }
198 virtual bool is_virtual() const { return true; }
199 virtual JVMState* generate(JVMState* jvms);
200
201 virtual CallNode* call_node() const { return _call_node; }
202 int vtable_index() const { return _vtable_index; }
203
204 virtual CallGenerator* with_call_node(CallNode* call) {
205 VirtualCallGenerator* cg = new VirtualCallGenerator(method(), _vtable_index, _separate_io_proj);
206 cg->set_call_node(call->as_CallDynamicJava());
207 return cg;
208 }
209 };
210
211 JVMState* VirtualCallGenerator::generate(JVMState* jvms) {
212 GraphKit kit(jvms);
213 Node* receiver = kit.argument(0);
214
215 if (kit.C->log() != nullptr) {
216 kit.C->log()->elem("virtual_call bci='%d'", jvms->bci());
217 }
218
219 // If the receiver is a constant null, do not torture the system
220 // by attempting to call through it. The compile will proceed
221 // correctly, but may bail out in final_graph_reshaping, because
222 // the call instruction will have a seemingly deficient out-count.
223 // (The bailout says something misleading about an "infinite loop".)
224 if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) {
225 assert(Bytecodes::is_invoke(kit.java_bc()), "%d: %s", kit.java_bc(), Bytecodes::name(kit.java_bc()));
226 ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
227 int arg_size = declared_method->signature()->arg_size_for_bc(kit.java_bc());
228 kit.inc_sp(arg_size); // restore arguments
229 kit.uncommon_trap(Deoptimization::Reason_null_check,
230 Deoptimization::Action_none,
231 nullptr, "null receiver");
232 return kit.transfer_exceptions_into_jvms();
233 }
234
336 // parse is finished.
337 if (!is_mh_late_inline()) {
338 C->add_late_inline(this);
339 }
340
341 // Emit the CallStaticJava and request separate projections so
342 // that the late inlining logic can distinguish between fall
343 // through and exceptional uses of the memory and io projections
344 // as is done for allocations and macro expansion.
345 return DirectCallGenerator::generate(jvms);
346 }
347
348 virtual void set_unique_id(jlong id) {
349 _unique_id = id;
350 }
351
352 virtual jlong unique_id() const {
353 return _unique_id;
354 }
355
356 virtual CallGenerator* with_call_node(CallNode* call) {
357 LateInlineCallGenerator* cg = new LateInlineCallGenerator(method(), _inline_cg, _is_pure_call);
358 cg->set_call_node(call->as_CallStaticJava());
359 return cg;
360 }
361 };
362
363 CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) {
364 return new LateInlineCallGenerator(method, inline_cg);
365 }
366
367 class LateInlineMHCallGenerator : public LateInlineCallGenerator {
368 ciMethod* _caller;
369 bool _input_not_const;
370
371 virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
372
373 public:
374 LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) :
375 LateInlineCallGenerator(callee, nullptr), _caller(caller), _input_not_const(input_not_const) {}
397 cg->set_call_node(call->as_CallStaticJava());
398 return cg;
399 }
400 };
401
402 bool LateInlineMHCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
403 // When inlining a virtual call, the null check at the call and the call itself can throw. These 2 paths have different
404 // expression stacks which causes late inlining to break. The MH invoker is not expected to be called from a method with
405 // exception handlers. When there is no exception handler, GraphKit::builtin_throw() pops the stack which solves the issue
406 // of late inlining with exceptions.
407 assert(!jvms->method()->has_exception_handlers() ||
408 (method()->intrinsic_id() != vmIntrinsics::_linkToVirtual &&
409 method()->intrinsic_id() != vmIntrinsics::_linkToInterface), "no exception handler expected");
410 // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
411 bool allow_inline = C->inlining_incrementally();
412 bool input_not_const = true;
413 CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), allow_inline, input_not_const);
414 assert(!input_not_const, "sanity"); // shouldn't have been scheduled for inlining in the first place
415
416 if (cg != nullptr) {
417 if (!allow_inline) {
418 C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE,
419 "late method handle call resolution");
420 }
421 assert(!cg->is_late_inline() || cg->is_mh_late_inline() || cg->is_virtual_late_inline() ||
422 AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
423 _inline_cg = cg;
424 return true;
425 } else {
426 // Method handle call which has a constant appendix argument should be either inlined or replaced with a direct call
427 // unless there's a signature mismatch between caller and callee. If the failure occurs, there's not much to be improved later,
428 // so don't reinstall the generator to avoid pushing the generator between IGVN and incremental inlining indefinitely.
429 return false;
430 }
431 }
432
433 CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) {
434 assert(IncrementalInlineMH, "required");
435 Compile::current()->mark_has_mh_late_inlines();
436 CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const);
557
558 void LateInlineMHCallGenerator::do_late_inline() {
559 CallGenerator::do_late_inline_helper();
560 }
561
562 void LateInlineVirtualCallGenerator::do_late_inline() {
563 assert(_callee != nullptr, "required"); // set up in CallDynamicJavaNode::Ideal
564 CallGenerator::do_late_inline_helper();
565 }
566
567 void CallGenerator::do_late_inline_helper() {
568 assert(is_late_inline(), "only late inline allowed");
569
570 // Can't inline it
571 CallNode* call = call_node();
572 if (call == nullptr || call->outcnt() == 0 ||
573 call->in(0) == nullptr || call->in(0)->is_top()) {
574 return;
575 }
576
577 const TypeTuple *r = call->tf()->domain();
578 for (int i1 = 0; i1 < method()->arg_size(); i1++) {
579 if (call->in(TypeFunc::Parms + i1)->is_top() && r->field_at(TypeFunc::Parms + i1) != Type::HALF) {
580 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
581 return;
582 }
583 }
584
585 if (call->in(TypeFunc::Memory)->is_top()) {
586 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
587 return;
588 }
589 if (call->in(TypeFunc::Memory)->is_MergeMem()) {
590 MergeMemNode* merge_mem = call->in(TypeFunc::Memory)->as_MergeMem();
591 if (merge_mem->base_memory() == merge_mem->empty_memory()) {
592 return; // dead path
593 }
594 }
595
596 // check for unreachable loop
597 CallProjections callprojs;
598 // Similar to incremental inlining, don't assert that all call
599 // projections are still there for post-parse call devirtualization.
600 bool do_asserts = !is_mh_late_inline() && !is_virtual_late_inline();
601 call->extract_projections(&callprojs, true, do_asserts);
602 if ((callprojs.fallthrough_catchproj == call->in(0)) ||
603 (callprojs.catchall_catchproj == call->in(0)) ||
604 (callprojs.fallthrough_memproj == call->in(TypeFunc::Memory)) ||
605 (callprojs.catchall_memproj == call->in(TypeFunc::Memory)) ||
606 (callprojs.fallthrough_ioproj == call->in(TypeFunc::I_O)) ||
607 (callprojs.catchall_ioproj == call->in(TypeFunc::I_O)) ||
608 (callprojs.resproj != nullptr && call->find_edge(callprojs.resproj) != -1) ||
609 (callprojs.exobj != nullptr && call->find_edge(callprojs.exobj) != -1)) {
610 return;
611 }
612
613 Compile* C = Compile::current();
614
615 uint endoff = call->jvms()->endoff();
616 if (C->inlining_incrementally()) {
617 // No reachability edges should be present when incremental inlining takes place.
618 // Inlining logic doesn't expect any extra edges past debug info and fails with
619 // an assert in SafePointNode::grow_stack.
620 assert(endoff == call->req(), "reachability edges not supported");
621 } else {
622 if (call->req() > endoff) { // reachability edges present
623 assert(OptimizeReachabilityFences, "required");
624 return; // keep the original call node as the holder of reachability info
625 }
626 }
627
628 // Remove inlined methods from Compiler's lists.
629 if (call->is_macro()) {
630 C->remove_macro_node(call);
631 }
632
633 // The call is marked as pure (no important side effects), but result isn't used.
634 // It's safe to remove the call.
635 bool result_not_used = (callprojs.resproj == nullptr || callprojs.resproj->outcnt() == 0);
636
637 if (is_pure_call() && result_not_used) {
638 GraphKit kit(call->jvms());
639 kit.replace_call(call, C->top(), true, do_asserts);
640 } else {
641 // Make a clone of the JVMState that appropriate to use for driving a parse
642 JVMState* old_jvms = call->jvms();
643 JVMState* jvms = old_jvms->clone_shallow(C);
644 uint size = call->req();
645 SafePointNode* map = new SafePointNode(size, jvms);
646 for (uint i1 = 0; i1 < size; i1++) {
647 map->init_req(i1, call->in(i1));
648 }
649 // Call node has in(ReturnAdr) set to top() node.
650 // We have to set map->in(ReturnAdr) to correct value
651 // because it is used by uncommon traps.
652 Node* ret_adr = C->start()->proj_out_or_null(TypeFunc::ReturnAdr);
653 precond(ret_adr != nullptr);
654 map->set_req(TypeFunc::ReturnAdr, ret_adr);
655
656 // Make sure the state is a MergeMem for parsing.
657 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
658 Node* mem = MergeMemNode::make(map->in(TypeFunc::Memory));
659 C->initial_gvn()->set_type_bottom(mem);
660 map->set_req(TypeFunc::Memory, mem);
661 }
662
663 uint nargs = method()->arg_size();
664 // blow away old call arguments
665 Node* top = C->top();
666 for (uint i1 = 0; i1 < nargs; i1++) {
667 map->set_req(TypeFunc::Parms + i1, top);
668 }
669 jvms->set_map(map);
670 precond(ret_adr == jvms->map()->returnadr());
671
672 // Make enough space in the expression stack to transfer
673 // the incoming arguments and return value.
674 map->ensure_stack(jvms, jvms->method()->max_stack());
675 for (uint i1 = 0; i1 < nargs; i1++) {
676 map->set_argument(jvms, i1, call->in(TypeFunc::Parms + i1));
677 }
678
679 C->log_late_inline(this);
680
681 // JVMState is ready, so time to perform some checks and prepare for inlining attempt.
682 if (!do_late_inline_check(C, jvms)) {
683 map->disconnect_inputs(C);
684 return;
685 }
686
687 // Setup default node notes to be picked up by the inlining
688 Node_Notes* old_nn = C->node_notes_at(call->_idx);
689 if (old_nn != nullptr) {
690 Node_Notes* entry_nn = old_nn->clone(C);
691 entry_nn->set_jvms(jvms);
692 C->set_default_node_notes(entry_nn);
693 }
694
695 // Now perform the inlining using the synthesized JVMState
696 JVMState* new_jvms = inline_cg()->generate(jvms);
697 if (new_jvms == nullptr) return; // no change
698 if (C->failing()) return;
699
700 if (is_mh_late_inline()) {
701 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (method handle)");
702 } else if (is_string_late_inline()) {
703 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (string method)");
704 } else if (is_boxing_late_inline()) {
705 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (boxing method)");
706 } else if (is_vector_reboxing_late_inline()) {
707 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (vector reboxing method)");
708 } else {
709 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded");
710 }
711
712 // Capture any exceptional control flow
713 GraphKit kit(new_jvms);
714
715 // Find the result object
716 Node* result = C->top();
717 int result_size = method()->return_type()->size();
718 if (result_size != 0 && !kit.stopped()) {
719 result = (result_size == 1) ? kit.pop() : kit.pop_pair();
720 }
721
722 if (call->is_CallStaticJava() && call->as_CallStaticJava()->is_boxing_method()) {
723 result = kit.must_be_not_null(result, false);
724 }
725
726 if (inline_cg()->is_inline()) {
727 C->set_has_loops(C->has_loops() || inline_cg()->method()->has_loops());
728 C->env()->notice_inlined_method(inline_cg()->method());
729 }
730 C->set_inlining_progress(true);
731 C->set_do_cleanup(kit.stopped()); // path is dead; needs cleanup
732 kit.replace_call(call, result, true, do_asserts);
733 }
734 }
735
736 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
737
738 public:
739 LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
740 LateInlineCallGenerator(method, inline_cg) {}
741
742 virtual JVMState* generate(JVMState* jvms) {
743 Compile *C = Compile::current();
744
745 C->log_inline_id(this);
746
747 C->add_string_late_inline(this);
748
749 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
750 return new_jvms;
751 }
974 // Merge memory
975 kit.merge_memory(slow_map->merged_memory(), region, 2);
976 // Transform new memory Phis.
977 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
978 Node* phi = mms.memory();
979 if (phi->is_Phi() && phi->in(0) == region) {
980 mms.set_memory(gvn.transform(phi));
981 }
982 }
983 uint tos = kit.jvms()->stkoff() + kit.sp();
984 uint limit = slow_map->req();
985 for (uint i = TypeFunc::Parms; i < limit; i++) {
986 // Skip unused stack slots; fast forward to monoff();
987 if (i == tos) {
988 i = kit.jvms()->monoff();
989 if( i >= limit ) break;
990 }
991 Node* m = kit.map()->in(i);
992 Node* n = slow_map->in(i);
993 if (m != n) {
994 const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
995 Node* phi = PhiNode::make(region, m, t);
996 phi->set_req(2, n);
997 kit.map()->set_req(i, gvn.transform(phi));
998 }
999 }
1000 return kit.transfer_exceptions_into_jvms();
1001 }
1002
1003
1004 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline) {
1005 assert(callee->is_method_handle_intrinsic(), "for_method_handle_call mismatch");
1006 bool input_not_const;
1007 CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, allow_inline, input_not_const);
1008 Compile* C = Compile::current();
1009 bool should_delay = C->should_delay_inlining();
1010 if (cg != nullptr) {
1011 if (should_delay && IncrementalInlineMH) {
1012 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1013 } else {
1014 return cg;
1015 }
1016 }
1017 int bci = jvms->bci();
1018 ciCallProfile profile = caller->call_profile_at_bci(bci);
1019 int call_site_count = caller->scale_count(profile.count());
1020
1021 if (IncrementalInlineMH && call_site_count > 0 &&
1022 (should_delay || input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())) {
1023 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1024 } else {
1025 // Out-of-line call.
1026 return CallGenerator::for_direct_call(callee);
1027 }
1028 }
1029
1030 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1031 GraphKit kit(jvms);
1032 PhaseGVN& gvn = kit.gvn();
1033 Compile* C = kit.C;
1034 vmIntrinsics::ID iid = callee->intrinsic_id();
1035 input_not_const = true;
1036 if (StressMethodHandleLinkerInlining) {
1037 allow_inline = false;
1038 }
1039 switch (iid) {
1040 case vmIntrinsics::_invokeBasic:
1041 {
1042 // Get MethodHandle receiver:
1043 Node* receiver = kit.argument(0);
1044 if (receiver->Opcode() == Op_ConP) {
1045 input_not_const = false;
1046 const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
1047 if (recv_toop != nullptr) {
1048 ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
1049 const int vtable_index = Method::invalid_vtable_index;
1057 false /* call_does_dispatch */,
1058 jvms,
1059 allow_inline,
1060 PROB_ALWAYS);
1061 return cg;
1062 } else {
1063 assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
1064 Type::str(receiver->bottom_type()));
1065 print_inlining_failure(C, callee, jvms, "receiver is always null");
1066 }
1067 } else {
1068 print_inlining_failure(C, callee, jvms, "receiver not constant");
1069 }
1070 } break;
1071
1072 case vmIntrinsics::_linkToVirtual:
1073 case vmIntrinsics::_linkToStatic:
1074 case vmIntrinsics::_linkToSpecial:
1075 case vmIntrinsics::_linkToInterface:
1076 {
1077 // Get MemberName argument:
1078 Node* member_name = kit.argument(callee->arg_size() - 1);
1079 if (member_name->Opcode() == Op_ConP) {
1080 input_not_const = false;
1081 const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1082 ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1083
1084 if (!ciMethod::is_consistent_info(callee, target)) {
1085 print_inlining_failure(C, callee, jvms, "signatures mismatch");
1086 return nullptr;
1087 }
1088
1089 // In lambda forms we erase signature types to avoid resolving issues
1090 // involving class loaders. When we optimize a method handle invoke
1091 // to a direct call we must cast the receiver and arguments to its
1092 // actual types.
1093 ciSignature* signature = target->signature();
1094 const int receiver_skip = target->is_static() ? 0 : 1;
1095 // Cast receiver to its type.
1096 if (!target->is_static()) {
1097 Node* recv = kit.argument(0);
1098 Node* casted_recv = kit.maybe_narrow_object_type(recv, signature->accessing_klass());
1099 if (casted_recv->is_top()) {
1100 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1101 return nullptr; // FIXME: effectively dead; issue a halt node instead
1102 } else if (casted_recv != recv) {
1103 kit.set_argument(0, casted_recv);
1104 }
1105 }
1106 // Cast reference arguments to its type.
1107 for (int i = 0, j = 0; i < signature->count(); i++) {
1108 ciType* t = signature->type_at(i);
1109 if (t->is_klass()) {
1110 Node* arg = kit.argument(receiver_skip + j);
1111 Node* casted_arg = kit.maybe_narrow_object_type(arg, t->as_klass());
1112 if (casted_arg->is_top()) {
1113 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1114 return nullptr; // FIXME: effectively dead; issue a halt node instead
1115 } else if (casted_arg != arg) {
1116 kit.set_argument(receiver_skip + j, casted_arg);
1117 }
1118 }
1119 j += t->size(); // long and double take two slots
1120 }
1121
1122 // Try to get the most accurate receiver type
1123 const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual);
1124 const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1125 int vtable_index = Method::invalid_vtable_index;
1126 bool call_does_dispatch = false;
1127
1128 ciKlass* speculative_receiver_type = nullptr;
1129 if (is_virtual_or_interface) {
1130 ciInstanceKlass* klass = target->holder();
1131 Node* receiver_node = kit.argument(0);
1132 const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1133 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
1134 // optimize_virtual_call() takes 2 different holder
1135 // arguments for a corner case that doesn't apply here (see
1136 // Parse::do_call())
1137 target = C->optimize_virtual_call(caller, klass, klass,
1138 target, receiver_type, is_virtual,
1139 call_does_dispatch, vtable_index, // out-parameters
1140 false /* check_access */);
1141 // We lack profiling at this call but type speculation may
1142 // provide us with a type
1143 speculative_receiver_type = (receiver_type != nullptr) ? receiver_type->speculative_type() : nullptr;
1144 }
1145 CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1146 allow_inline,
1147 PROB_ALWAYS,
1148 speculative_receiver_type);
1149 return cg;
1150 } else {
1151 print_inlining_failure(C, callee, jvms, "member_name not constant");
1152 }
1153 } break;
1154
1155 case vmIntrinsics::_linkToNative:
1156 print_inlining_failure(C, callee, jvms, "native call");
1157 break;
1158
1159 default:
1160 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1161 break;
1162 }
1163 return nullptr;
1164 }
1165
1166 //------------------------PredicatedIntrinsicGenerator------------------------------
1167 // Internal class which handles all predicated Intrinsic calls.
1168 class PredicatedIntrinsicGenerator : public CallGenerator {
1200 // do_intrinsic(0)
1201 // else
1202 // if (predicate(1))
1203 // do_intrinsic(1)
1204 // ...
1205 // else
1206 // do_java_comp
1207
1208 GraphKit kit(jvms);
1209 PhaseGVN& gvn = kit.gvn();
1210
1211 CompileLog* log = kit.C->log();
1212 if (log != nullptr) {
1213 log->elem("predicated_intrinsic bci='%d' method='%d'",
1214 jvms->bci(), log->identify(method()));
1215 }
1216
1217 if (!method()->is_static()) {
1218 // We need an explicit receiver null_check before checking its type in predicate.
1219 // We share a map with the caller, so his JVMS gets adjusted.
1220 Node* receiver = kit.null_check_receiver_before_call(method());
1221 if (kit.stopped()) {
1222 return kit.transfer_exceptions_into_jvms();
1223 }
1224 }
1225
1226 int n_predicates = _intrinsic->predicates_count();
1227 assert(n_predicates > 0, "sanity");
1228
1229 JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1230
1231 // Region for normal compilation code if intrinsic failed.
1232 Node* slow_region = new RegionNode(1);
1233
1234 int results = 0;
1235 for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1236 #ifdef ASSERT
1237 JVMState* old_jvms = kit.jvms();
1238 SafePointNode* old_map = kit.map();
1239 Node* old_io = old_map->i_o();
1240 Node* old_mem = old_map->memory();
|
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);
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 bool is_static = method()->is_static();
157 address target = is_static ? SharedRuntime::get_resolve_static_call_stub()
158 : SharedRuntime::get_resolve_opt_virtual_call_stub();
159
160 if (kit.C->log() != nullptr) {
161 kit.C->log()->elem("direct_call bci='%d'", jvms->bci());
162 }
205 {
206 assert(vtable_index == Method::invalid_vtable_index ||
207 vtable_index >= 0, "either invalid or usable");
208 }
209 virtual bool is_virtual() const { return true; }
210 virtual JVMState* generate(JVMState* jvms);
211
212 virtual CallNode* call_node() const { return _call_node; }
213 int vtable_index() const { return _vtable_index; }
214
215 virtual CallGenerator* with_call_node(CallNode* call) {
216 VirtualCallGenerator* cg = new VirtualCallGenerator(method(), _vtable_index, _separate_io_proj);
217 cg->set_call_node(call->as_CallDynamicJava());
218 return cg;
219 }
220 };
221
222 JVMState* VirtualCallGenerator::generate(JVMState* jvms) {
223 GraphKit kit(jvms);
224 Node* receiver = kit.argument(0);
225 if (kit.C->log() != nullptr) {
226 kit.C->log()->elem("virtual_call bci='%d'", jvms->bci());
227 }
228
229 // If the receiver is a constant null, do not torture the system
230 // by attempting to call through it. The compile will proceed
231 // correctly, but may bail out in final_graph_reshaping, because
232 // the call instruction will have a seemingly deficient out-count.
233 // (The bailout says something misleading about an "infinite loop".)
234 if (kit.gvn().type(receiver)->higher_equal(TypePtr::NULL_PTR)) {
235 assert(Bytecodes::is_invoke(kit.java_bc()), "%d: %s", kit.java_bc(), Bytecodes::name(kit.java_bc()));
236 ciMethod* declared_method = kit.method()->get_method_at_bci(kit.bci());
237 int arg_size = declared_method->signature()->arg_size_for_bc(kit.java_bc());
238 kit.inc_sp(arg_size); // restore arguments
239 kit.uncommon_trap(Deoptimization::Reason_null_check,
240 Deoptimization::Action_none,
241 nullptr, "null receiver");
242 return kit.transfer_exceptions_into_jvms();
243 }
244
346 // parse is finished.
347 if (!is_mh_late_inline()) {
348 C->add_late_inline(this);
349 }
350
351 // Emit the CallStaticJava and request separate projections so
352 // that the late inlining logic can distinguish between fall
353 // through and exceptional uses of the memory and io projections
354 // as is done for allocations and macro expansion.
355 return DirectCallGenerator::generate(jvms);
356 }
357
358 virtual void set_unique_id(jlong id) {
359 _unique_id = id;
360 }
361
362 virtual jlong unique_id() const {
363 return _unique_id;
364 }
365
366 virtual CallGenerator* inline_cg() {
367 return _inline_cg;
368 }
369
370 virtual CallGenerator* with_call_node(CallNode* call) {
371 LateInlineCallGenerator* cg = new LateInlineCallGenerator(method(), _inline_cg, _is_pure_call);
372 cg->set_call_node(call->as_CallStaticJava());
373 return cg;
374 }
375 };
376
377 CallGenerator* CallGenerator::for_late_inline(ciMethod* method, CallGenerator* inline_cg) {
378 return new LateInlineCallGenerator(method, inline_cg);
379 }
380
381 class LateInlineMHCallGenerator : public LateInlineCallGenerator {
382 ciMethod* _caller;
383 bool _input_not_const;
384
385 virtual bool do_late_inline_check(Compile* C, JVMState* jvms);
386
387 public:
388 LateInlineMHCallGenerator(ciMethod* caller, ciMethod* callee, bool input_not_const) :
389 LateInlineCallGenerator(callee, nullptr), _caller(caller), _input_not_const(input_not_const) {}
411 cg->set_call_node(call->as_CallStaticJava());
412 return cg;
413 }
414 };
415
416 bool LateInlineMHCallGenerator::do_late_inline_check(Compile* C, JVMState* jvms) {
417 // When inlining a virtual call, the null check at the call and the call itself can throw. These 2 paths have different
418 // expression stacks which causes late inlining to break. The MH invoker is not expected to be called from a method with
419 // exception handlers. When there is no exception handler, GraphKit::builtin_throw() pops the stack which solves the issue
420 // of late inlining with exceptions.
421 assert(!jvms->method()->has_exception_handlers() ||
422 (method()->intrinsic_id() != vmIntrinsics::_linkToVirtual &&
423 method()->intrinsic_id() != vmIntrinsics::_linkToInterface), "no exception handler expected");
424 // Even if inlining is not allowed, a virtual call can be strength-reduced to a direct call.
425 bool allow_inline = C->inlining_incrementally();
426 bool input_not_const = true;
427 CallGenerator* cg = for_method_handle_inline(jvms, _caller, method(), allow_inline, input_not_const);
428 assert(!input_not_const, "sanity"); // shouldn't have been scheduled for inlining in the first place
429
430 if (cg != nullptr) {
431 // AlwaysIncrementalInline causes for_method_handle_inline() to
432 // return a LateInlineCallGenerator. Extract the
433 // InlineCallGenerator from it.
434 if (AlwaysIncrementalInline && cg->is_late_inline() && !cg->is_virtual_late_inline()) {
435 cg = cg->inline_cg();
436 assert(cg != nullptr, "inline call generator expected");
437 }
438
439 if (!allow_inline) {
440 C->inline_printer()->record(cg->method(), call_node()->jvms(), InliningResult::FAILURE,
441 "late method handle call resolution");
442 }
443 assert(!cg->is_late_inline() || cg->is_mh_late_inline() || cg->is_virtual_late_inline() ||
444 AlwaysIncrementalInline || StressIncrementalInlining, "we're doing late inlining");
445 _inline_cg = cg;
446 return true;
447 } else {
448 // Method handle call which has a constant appendix argument should be either inlined or replaced with a direct call
449 // unless there's a signature mismatch between caller and callee. If the failure occurs, there's not much to be improved later,
450 // so don't reinstall the generator to avoid pushing the generator between IGVN and incremental inlining indefinitely.
451 return false;
452 }
453 }
454
455 CallGenerator* CallGenerator::for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const) {
456 assert(IncrementalInlineMH, "required");
457 Compile::current()->mark_has_mh_late_inlines();
458 CallGenerator* cg = new LateInlineMHCallGenerator(caller, callee, input_not_const);
579
580 void LateInlineMHCallGenerator::do_late_inline() {
581 CallGenerator::do_late_inline_helper();
582 }
583
584 void LateInlineVirtualCallGenerator::do_late_inline() {
585 assert(_callee != nullptr, "required"); // set up in CallDynamicJavaNode::Ideal
586 CallGenerator::do_late_inline_helper();
587 }
588
589 void CallGenerator::do_late_inline_helper() {
590 assert(is_late_inline(), "only late inline allowed");
591
592 // Can't inline it
593 CallNode* call = call_node();
594 if (call == nullptr || call->outcnt() == 0 ||
595 call->in(0) == nullptr || call->in(0)->is_top()) {
596 return;
597 }
598
599 const TypeTuple* r = call->tf()->domain_cc();
600 for (uint i1 = TypeFunc::Parms; i1 < r->cnt(); i1++) {
601 if (call->in(i1)->is_top() && r->field_at(i1) != Type::HALF) {
602 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
603 return;
604 }
605 }
606
607 if (call->in(TypeFunc::Memory)->is_top()) {
608 assert(Compile::current()->inlining_incrementally(), "shouldn't happen during parsing");
609 return;
610 }
611 if (call->in(TypeFunc::Memory)->is_MergeMem()) {
612 MergeMemNode* merge_mem = call->in(TypeFunc::Memory)->as_MergeMem();
613 if (merge_mem->base_memory() == merge_mem->empty_memory()) {
614 return; // dead path
615 }
616 }
617
618 // check for unreachable loop
619 // Similar to incremental inlining, don't assert that all call
620 // projections are still there for post-parse call devirtualization.
621 bool do_asserts = !is_mh_late_inline() && !is_virtual_late_inline();
622 CallProjections* callprojs = call->extract_projections(true, do_asserts);
623 if ((callprojs->fallthrough_catchproj == call->in(0)) ||
624 (callprojs->catchall_catchproj == call->in(0)) ||
625 (callprojs->fallthrough_memproj == call->in(TypeFunc::Memory)) ||
626 (callprojs->catchall_memproj == call->in(TypeFunc::Memory)) ||
627 (callprojs->fallthrough_ioproj == call->in(TypeFunc::I_O)) ||
628 (callprojs->catchall_ioproj == call->in(TypeFunc::I_O)) ||
629 (callprojs->exobj != nullptr && call->find_edge(callprojs->exobj) != -1)) {
630 return;
631 }
632
633 Compile* C = Compile::current();
634
635 uint endoff = call->jvms()->endoff();
636 if (C->inlining_incrementally()) {
637 // No reachability edges should be present when incremental inlining takes place.
638 // Inlining logic doesn't expect any extra edges past debug info and fails with
639 // an assert in SafePointNode::grow_stack.
640 assert(endoff == call->req(), "reachability edges not supported");
641 } else {
642 if (call->req() > endoff) { // reachability edges present
643 assert(OptimizeReachabilityFences, "required");
644 return; // keep the original call node as the holder of reachability info
645 }
646 }
647
648 // Remove inlined methods from Compiler's lists.
649 if (call->is_macro()) {
650 C->remove_macro_node(call);
651 }
652
653
654 bool result_not_used = true;
655 for (uint i = 0; i < callprojs->nb_resproj; i++) {
656 if (callprojs->resproj[i] != nullptr) {
657 if (callprojs->resproj[i]->outcnt() != 0) {
658 result_not_used = false;
659 }
660 if (call->find_edge(callprojs->resproj[i]) != -1) {
661 return;
662 }
663 }
664 }
665
666 if (is_pure_call() && result_not_used) {
667 // The call is marked as pure (no important side effects), but result isn't used.
668 // It's safe to remove the call.
669 GraphKit kit(call->jvms());
670 kit.replace_call(call, C->top(), true, do_asserts);
671 } else {
672 // Make a clone of the JVMState that appropriate to use for driving a parse
673 JVMState* old_jvms = call->jvms();
674 JVMState* jvms = old_jvms->clone_shallow(C);
675 uint size = call->req();
676 SafePointNode* map = new SafePointNode(size, jvms);
677 for (uint i1 = 0; i1 < size; i1++) {
678 map->init_req(i1, call->in(i1));
679 }
680 // Call node has in(ReturnAdr) set to top() node.
681 // We have to set map->in(ReturnAdr) to correct value
682 // because it is used by uncommon traps.
683 Node* ret_adr = C->start()->proj_out_or_null(TypeFunc::ReturnAdr);
684 precond(ret_adr != nullptr);
685 map->set_req(TypeFunc::ReturnAdr, ret_adr);
686
687 PhaseGVN& gvn = *C->initial_gvn();
688 // Make sure the state is a MergeMem for parsing.
689 if (!map->in(TypeFunc::Memory)->is_MergeMem()) {
690 Node* mem = MergeMemNode::make(map->in(TypeFunc::Memory));
691 gvn.set_type_bottom(mem);
692 map->set_req(TypeFunc::Memory, mem);
693 }
694
695 // blow away old call arguments
696 for (uint i1 = TypeFunc::Parms; i1 < r->cnt(); i1++) {
697 map->set_req(i1, C->top());
698 }
699 jvms->set_map(map);
700 precond(ret_adr == jvms->map()->returnadr());
701
702 // Make enough space in the expression stack to transfer
703 // the incoming arguments and return value.
704 map->ensure_stack(jvms, jvms->method()->max_stack());
705 const TypeTuple* domain_sig = call->_tf->domain_sig();
706 uint nargs = method()->arg_size();
707 assert(domain_sig->cnt() - TypeFunc::Parms == nargs, "inconsistent signature");
708
709 uint j = TypeFunc::Parms;
710 int arg_num = 0;
711 for (uint i1 = 0; i1 < nargs; i1++) {
712 const Type* t = domain_sig->field_at(TypeFunc::Parms + i1);
713 if (t->is_inlinetypeptr() && !method()->mismatch() && method()->is_scalarized_arg(arg_num)) {
714 // Inline type arguments are not passed by reference: we get an argument per
715 // field of the inline type. Build InlineTypeNodes from the inline type arguments.
716 GraphKit arg_kit(jvms, &gvn);
717 Node* vt = InlineTypeNode::make_from_multi(&arg_kit, call, t->inline_klass(), j, /* in= */ true, /* null_free= */ !t->maybe_null());
718 // GraphKit::access_load_at() may be called from InlineTypeNode::make_from_multi() and it may change the map
719 // that arg_kit uses.
720 map = arg_kit.map();
721 map->set_control(arg_kit.control());
722 map->set_argument(jvms, i1, vt);
723 } else {
724 map->set_argument(jvms, i1, call->in(j++));
725 }
726 if (t != Type::HALF) {
727 arg_num++;
728 }
729 }
730
731 C->log_late_inline(this);
732
733 // JVMState is ready, so time to perform some checks and prepare for inlining attempt.
734 if (!do_late_inline_check(C, jvms)) {
735 map->disconnect_inputs(C);
736 return;
737 }
738
739 // Check if we are late inlining a method handle call that returns an inline type as fields.
740 Node* buffer_oop = nullptr;
741 ciMethod* inline_method = inline_cg()->method();
742 ciType* return_type = inline_method->return_type();
743 if (!call->tf()->returns_inline_type_as_fields() &&
744 return_type->is_inlinetype() && return_type->as_inline_klass()->can_be_returned_as_fields()) {
745 assert(is_mh_late_inline(), "Unexpected return type");
746
747 // Allocate a buffer for the inline type returned as fields because the caller expects an oop return.
748 // Do this before the method handle call in case the buffer allocation triggers deoptimization and
749 // we need to "re-execute" the call in the interpreter (to make sure the call is only executed once).
750 GraphKit arg_kit(jvms, &gvn);
751 {
752 PreserveReexecuteState preexecs(&arg_kit);
753 arg_kit.jvms()->set_should_reexecute(true);
754 arg_kit.inc_sp(nargs);
755 Node* klass_node = arg_kit.makecon(TypeKlassPtr::make(return_type->as_inline_klass()));
756 buffer_oop = arg_kit.new_instance(klass_node, nullptr, nullptr, /* deoptimize_on_exception */ true);
757 }
758 jvms = arg_kit.transfer_exceptions_into_jvms();
759 }
760
761 // Setup default node notes to be picked up by the inlining
762 Node_Notes* old_nn = C->node_notes_at(call->_idx);
763 if (old_nn != nullptr) {
764 Node_Notes* entry_nn = old_nn->clone(C);
765 entry_nn->set_jvms(jvms);
766 C->set_default_node_notes(entry_nn);
767 }
768
769 // Now perform the inlining using the synthesized JVMState
770 JVMState* new_jvms = inline_cg()->generate(jvms);
771 if (new_jvms == nullptr) return; // no change
772 if (C->failing()) return;
773
774 if (is_mh_late_inline()) {
775 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (method handle)");
776 } else if (is_string_late_inline()) {
777 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (string method)");
778 } else if (is_boxing_late_inline()) {
779 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (boxing method)");
780 } else if (is_vector_reboxing_late_inline()) {
781 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded (vector reboxing method)");
782 } else {
783 C->inline_printer()->record(method(), jvms, InliningResult::SUCCESS, "late inline succeeded");
784 }
785
786 // Capture any exceptional control flow
787 GraphKit kit(new_jvms);
788
789 // Find the result object
790 Node* result = C->top();
791 int result_size = method()->return_type()->size();
792 if (result_size != 0 && !kit.stopped()) {
793 result = (result_size == 1) ? kit.pop() : kit.pop_pair();
794 }
795
796 if (call->is_CallStaticJava() && call->as_CallStaticJava()->is_boxing_method()
797 && !call->tf()->returns_inline_type_as_fields()) {
798 result = kit.must_be_not_null(result, false);
799 }
800
801 if (inline_cg()->is_inline()) {
802 C->set_has_loops(C->has_loops() || inline_method->has_loops());
803 C->env()->notice_inlined_method(inline_method);
804 }
805 C->set_inlining_progress(true);
806 C->set_do_cleanup(kit.stopped()); // path is dead; needs cleanup
807
808 // Handle inline type returns
809 InlineTypeNode* vt = result->isa_InlineType();
810 if (vt != nullptr) {
811 if (call->tf()->returns_inline_type_as_fields()) {
812 vt->replace_call_results(&kit, call, C);
813 } else {
814 // Result might still be allocated (for example, if it has been stored to a non-flat field)
815 if (!vt->is_allocated(&kit.gvn())) {
816 assert(buffer_oop != nullptr, "should have allocated a buffer");
817 RegionNode* region = new RegionNode(3);
818
819 // Check if result is null
820 Node* null_ctl = kit.top();
821 kit.null_check_common(vt->get_null_marker(), T_INT, false, &null_ctl);
822 region->init_req(1, null_ctl);
823 PhiNode* oop = PhiNode::make(region, kit.gvn().zerocon(T_OBJECT), TypeInstPtr::make(TypePtr::BotPTR, vt->type()->inline_klass()));
824 Node* init_mem = kit.reset_memory();
825 PhiNode* mem = PhiNode::make(region, init_mem, Type::MEMORY, TypePtr::BOTTOM);
826
827 // Not null, initialize the buffer
828 kit.set_all_memory(init_mem);
829
830 Node* payload_ptr = kit.basic_plus_adr(buffer_oop, kit.gvn().type(vt)->inline_klass()->payload_offset());
831 vt->store_flat(&kit, buffer_oop, payload_ptr, false, true, true, IN_HEAP | MO_UNORDERED);
832 // Do not let stores that initialize this buffer be reordered with a subsequent
833 // store that would make this buffer accessible by other threads.
834 AllocateNode* alloc = AllocateNode::Ideal_allocation(buffer_oop);
835 assert(alloc != nullptr, "must have an allocation node");
836 kit.insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
837 region->init_req(2, kit.control());
838 oop->init_req(2, buffer_oop);
839 mem->init_req(2, kit.merged_memory());
840
841 // Update oop input to buffer
842 kit.gvn().hash_delete(vt);
843 vt->set_oop(kit.gvn(), kit.gvn().transform(oop));
844 vt->set_is_buffered(kit.gvn());
845 vt = kit.gvn().transform(vt)->as_InlineType();
846
847 kit.set_control(kit.gvn().transform(region));
848 kit.set_all_memory(kit.gvn().transform(mem));
849 kit.record_for_igvn(region);
850 kit.record_for_igvn(oop);
851 kit.record_for_igvn(mem);
852 }
853 result = vt;
854 }
855 DEBUG_ONLY(buffer_oop = nullptr);
856 } else {
857 assert(result->is_top() || !call->tf()->returns_inline_type_as_fields() || !call->as_CallJava()->method()->return_type()->is_loaded(), "Unexpected return value");
858 }
859 assert(kit.stopped() || buffer_oop == nullptr, "unused buffer allocation");
860
861 kit.replace_call(call, result, true, do_asserts);
862 }
863 }
864
865 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
866
867 public:
868 LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
869 LateInlineCallGenerator(method, inline_cg) {}
870
871 virtual JVMState* generate(JVMState* jvms) {
872 Compile *C = Compile::current();
873
874 C->log_inline_id(this);
875
876 C->add_string_late_inline(this);
877
878 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
879 return new_jvms;
880 }
1103 // Merge memory
1104 kit.merge_memory(slow_map->merged_memory(), region, 2);
1105 // Transform new memory Phis.
1106 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1107 Node* phi = mms.memory();
1108 if (phi->is_Phi() && phi->in(0) == region) {
1109 mms.set_memory(gvn.transform(phi));
1110 }
1111 }
1112 uint tos = kit.jvms()->stkoff() + kit.sp();
1113 uint limit = slow_map->req();
1114 for (uint i = TypeFunc::Parms; i < limit; i++) {
1115 // Skip unused stack slots; fast forward to monoff();
1116 if (i == tos) {
1117 i = kit.jvms()->monoff();
1118 if( i >= limit ) break;
1119 }
1120 Node* m = kit.map()->in(i);
1121 Node* n = slow_map->in(i);
1122 if (m != n) {
1123 #ifdef ASSERT
1124 if (m->is_InlineType() != n->is_InlineType()) {
1125 InlineTypeNode* unique_vt = m->is_InlineType() ? m->as_InlineType() : n->as_InlineType();
1126 assert(unique_vt->is_allocated(&gvn), "InlineType can be merged with an oop only if it is allocated");
1127 }
1128 #endif
1129 const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
1130 Node* phi = PhiNode::make(region, m, t);
1131 phi->set_req(2, n);
1132 kit.map()->set_req(i, gvn.transform(phi));
1133 }
1134 }
1135 return kit.transfer_exceptions_into_jvms();
1136 }
1137
1138
1139 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline) {
1140 assert(callee->is_method_handle_intrinsic(), "for_method_handle_call mismatch");
1141 bool input_not_const;
1142 CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, allow_inline, input_not_const);
1143 Compile* C = Compile::current();
1144 bool should_delay = C->should_delay_inlining();
1145 if (cg != nullptr) {
1146 if (should_delay && IncrementalInlineMH) {
1147 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1148 } else {
1149 return cg;
1150 }
1151 }
1152 int bci = jvms->bci();
1153 ciCallProfile profile = caller->call_profile_at_bci(bci);
1154 int call_site_count = caller->scale_count(profile.count());
1155
1156 if (IncrementalInlineMH && (AlwaysIncrementalInline ||
1157 (call_site_count > 0 && (should_delay || input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())))) {
1158 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1159 } else {
1160 // Out-of-line call.
1161 return CallGenerator::for_direct_call(callee);
1162 }
1163 }
1164
1165
1166 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1167 GraphKit kit(jvms);
1168 PhaseGVN& gvn = kit.gvn();
1169 Compile* C = kit.C;
1170 vmIntrinsics::ID iid = callee->intrinsic_id();
1171 input_not_const = true;
1172 if (StressMethodHandleLinkerInlining) {
1173 allow_inline = false;
1174 }
1175 switch (iid) {
1176 case vmIntrinsics::_invokeBasic:
1177 {
1178 // Get MethodHandle receiver:
1179 Node* receiver = kit.argument(0);
1180 if (receiver->Opcode() == Op_ConP) {
1181 input_not_const = false;
1182 const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
1183 if (recv_toop != nullptr) {
1184 ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
1185 const int vtable_index = Method::invalid_vtable_index;
1193 false /* call_does_dispatch */,
1194 jvms,
1195 allow_inline,
1196 PROB_ALWAYS);
1197 return cg;
1198 } else {
1199 assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
1200 Type::str(receiver->bottom_type()));
1201 print_inlining_failure(C, callee, jvms, "receiver is always null");
1202 }
1203 } else {
1204 print_inlining_failure(C, callee, jvms, "receiver not constant");
1205 }
1206 } break;
1207
1208 case vmIntrinsics::_linkToVirtual:
1209 case vmIntrinsics::_linkToStatic:
1210 case vmIntrinsics::_linkToSpecial:
1211 case vmIntrinsics::_linkToInterface:
1212 {
1213 int nargs = callee->arg_size();
1214 // Get MemberName argument:
1215 Node* member_name = kit.argument(nargs - 1);
1216 if (member_name->Opcode() == Op_ConP) {
1217 input_not_const = false;
1218 const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1219 ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1220
1221 if (!ciMethod::is_consistent_info(callee, target)) {
1222 print_inlining_failure(C, callee, jvms, "signatures mismatch");
1223 return nullptr;
1224 }
1225
1226 // In lambda forms we erase signature types to avoid resolving issues
1227 // involving class loaders. When we optimize a method handle invoke
1228 // to a direct call we must cast the receiver and arguments to its
1229 // actual types.
1230 ciSignature* signature = target->signature();
1231 const int receiver_skip = target->is_static() ? 0 : 1;
1232 // Cast receiver to its type.
1233 if (!target->is_static()) {
1234 Node* recv = kit.argument(0);
1235 Node* casted_recv = kit.maybe_narrow_object_type(recv, signature->accessing_klass(), target->receiver_maybe_larval());
1236 if (casted_recv->is_top()) {
1237 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1238 return nullptr; // FIXME: effectively dead; issue a halt node instead
1239 } else if (casted_recv != recv) {
1240 kit.set_argument(0, casted_recv);
1241 }
1242 }
1243 // Cast reference arguments to its type.
1244 for (int i = 0, j = 0; i < signature->count(); i++) {
1245 ciType* t = signature->type_at(i);
1246 if (t->is_klass()) {
1247 Node* arg = kit.argument(receiver_skip + j);
1248 Node* casted_arg = kit.maybe_narrow_object_type(arg, t->as_klass(), false);
1249 if (casted_arg->is_top()) {
1250 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1251 return nullptr; // FIXME: effectively dead; issue a halt node instead
1252 } else if (casted_arg != arg) {
1253 kit.set_argument(receiver_skip + j, casted_arg);
1254 }
1255 }
1256 j += t->size(); // long and double take two slots
1257 }
1258
1259 // Try to get the most accurate receiver type
1260 const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual);
1261 const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1262 int vtable_index = Method::invalid_vtable_index;
1263 bool call_does_dispatch = false;
1264
1265 ciKlass* speculative_receiver_type = nullptr;
1266 if (is_virtual_or_interface) {
1267 ciInstanceKlass* klass = target->holder();
1268 Node* receiver_node = kit.argument(0);
1269 const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1270 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
1271 // optimize_virtual_call() takes 2 different holder
1272 // arguments for a corner case that doesn't apply here (see
1273 // Parse::do_call())
1274 target = C->optimize_virtual_call(caller, klass, klass,
1275 target, receiver_type, is_virtual,
1276 call_does_dispatch, vtable_index, // out-parameters
1277 false /* check_access */);
1278 // We lack profiling at this call but type speculation may
1279 // provide us with a type
1280 speculative_receiver_type = (receiver_type != nullptr) ? receiver_type->speculative_type() : nullptr;
1281 }
1282 CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1283 allow_inline,
1284 PROB_ALWAYS,
1285 speculative_receiver_type,
1286 true);
1287 return cg;
1288 } else {
1289 print_inlining_failure(C, callee, jvms, "member_name not constant");
1290 }
1291 } break;
1292
1293 case vmIntrinsics::_linkToNative:
1294 print_inlining_failure(C, callee, jvms, "native call");
1295 break;
1296
1297 default:
1298 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1299 break;
1300 }
1301 return nullptr;
1302 }
1303
1304 //------------------------PredicatedIntrinsicGenerator------------------------------
1305 // Internal class which handles all predicated Intrinsic calls.
1306 class PredicatedIntrinsicGenerator : public CallGenerator {
1338 // do_intrinsic(0)
1339 // else
1340 // if (predicate(1))
1341 // do_intrinsic(1)
1342 // ...
1343 // else
1344 // do_java_comp
1345
1346 GraphKit kit(jvms);
1347 PhaseGVN& gvn = kit.gvn();
1348
1349 CompileLog* log = kit.C->log();
1350 if (log != nullptr) {
1351 log->elem("predicated_intrinsic bci='%d' method='%d'",
1352 jvms->bci(), log->identify(method()));
1353 }
1354
1355 if (!method()->is_static()) {
1356 // We need an explicit receiver null_check before checking its type in predicate.
1357 // We share a map with the caller, so his JVMS gets adjusted.
1358 kit.null_check_receiver_before_call(method());
1359 if (kit.stopped()) {
1360 return kit.transfer_exceptions_into_jvms();
1361 }
1362 }
1363
1364 int n_predicates = _intrinsic->predicates_count();
1365 assert(n_predicates > 0, "sanity");
1366
1367 JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1368
1369 // Region for normal compilation code if intrinsic failed.
1370 Node* slow_region = new RegionNode(1);
1371
1372 int results = 0;
1373 for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1374 #ifdef ASSERT
1375 JVMState* old_jvms = kit.jvms();
1376 SafePointNode* old_map = kit.map();
1377 Node* old_io = old_map->i_o();
1378 Node* old_mem = old_map->memory();
|