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 result = kit.must_be_not_null(result, false);
798 }
799
800 if (inline_cg()->is_inline()) {
801 C->set_has_loops(C->has_loops() || inline_method->has_loops());
802 C->env()->notice_inlined_method(inline_method);
803 }
804 C->set_inlining_progress(true);
805 C->set_do_cleanup(kit.stopped()); // path is dead; needs cleanup
806
807 // Handle inline type returns
808 InlineTypeNode* vt = result->isa_InlineType();
809 if (vt != nullptr) {
810 if (call->tf()->returns_inline_type_as_fields()) {
811 vt->replace_call_results(&kit, call, C);
812 } else {
813 // Result might still be allocated (for example, if it has been stored to a non-flat field)
814 if (!vt->is_allocated(&kit.gvn())) {
815 assert(buffer_oop != nullptr, "should have allocated a buffer");
816 RegionNode* region = new RegionNode(3);
817
818 // Check if result is null
819 Node* null_ctl = kit.top();
820 kit.null_check_common(vt->get_null_marker(), T_INT, false, &null_ctl);
821 region->init_req(1, null_ctl);
822 PhiNode* oop = PhiNode::make(region, kit.gvn().zerocon(T_OBJECT), TypeInstPtr::make(TypePtr::BotPTR, vt->type()->inline_klass()));
823 Node* init_mem = kit.reset_memory();
824 PhiNode* mem = PhiNode::make(region, init_mem, Type::MEMORY, TypePtr::BOTTOM);
825
826 // Not null, initialize the buffer
827 kit.set_all_memory(init_mem);
828
829 Node* payload_ptr = kit.basic_plus_adr(buffer_oop, kit.gvn().type(vt)->inline_klass()->payload_offset());
830 vt->store_flat(&kit, buffer_oop, payload_ptr, false, true, true, IN_HEAP | MO_UNORDERED);
831 // Do not let stores that initialize this buffer be reordered with a subsequent
832 // store that would make this buffer accessible by other threads.
833 AllocateNode* alloc = AllocateNode::Ideal_allocation(buffer_oop);
834 assert(alloc != nullptr, "must have an allocation node");
835 kit.insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
836 region->init_req(2, kit.control());
837 oop->init_req(2, buffer_oop);
838 mem->init_req(2, kit.merged_memory());
839
840 // Update oop input to buffer
841 kit.gvn().hash_delete(vt);
842 vt->set_oop(kit.gvn(), kit.gvn().transform(oop));
843 vt->set_is_buffered(kit.gvn());
844 vt = kit.gvn().transform(vt)->as_InlineType();
845
846 kit.set_control(kit.gvn().transform(region));
847 kit.set_all_memory(kit.gvn().transform(mem));
848 kit.record_for_igvn(region);
849 kit.record_for_igvn(oop);
850 kit.record_for_igvn(mem);
851 }
852 result = vt;
853 }
854 DEBUG_ONLY(buffer_oop = nullptr);
855 } else {
856 assert(result->is_top() || !call->tf()->returns_inline_type_as_fields() || !call->as_CallJava()->method()->return_type()->is_loaded(), "Unexpected return value");
857 }
858 assert(kit.stopped() || buffer_oop == nullptr, "unused buffer allocation");
859
860 kit.replace_call(call, result, true, do_asserts);
861 }
862 }
863
864 class LateInlineStringCallGenerator : public LateInlineCallGenerator {
865
866 public:
867 LateInlineStringCallGenerator(ciMethod* method, CallGenerator* inline_cg) :
868 LateInlineCallGenerator(method, inline_cg) {}
869
870 virtual JVMState* generate(JVMState* jvms) {
871 Compile *C = Compile::current();
872
873 C->log_inline_id(this);
874
875 C->add_string_late_inline(this);
876
877 JVMState* new_jvms = DirectCallGenerator::generate(jvms);
878 return new_jvms;
879 }
1102 // Merge memory
1103 kit.merge_memory(slow_map->merged_memory(), region, 2);
1104 // Transform new memory Phis.
1105 for (MergeMemStream mms(kit.merged_memory()); mms.next_non_empty();) {
1106 Node* phi = mms.memory();
1107 if (phi->is_Phi() && phi->in(0) == region) {
1108 mms.set_memory(gvn.transform(phi));
1109 }
1110 }
1111 uint tos = kit.jvms()->stkoff() + kit.sp();
1112 uint limit = slow_map->req();
1113 for (uint i = TypeFunc::Parms; i < limit; i++) {
1114 // Skip unused stack slots; fast forward to monoff();
1115 if (i == tos) {
1116 i = kit.jvms()->monoff();
1117 if( i >= limit ) break;
1118 }
1119 Node* m = kit.map()->in(i);
1120 Node* n = slow_map->in(i);
1121 if (m != n) {
1122 #ifdef ASSERT
1123 if (m->is_InlineType() != n->is_InlineType()) {
1124 InlineTypeNode* unique_vt = m->is_InlineType() ? m->as_InlineType() : n->as_InlineType();
1125 assert(unique_vt->is_allocated(&gvn), "InlineType can be merged with an oop only if it is allocated");
1126 }
1127 #endif
1128 const Type* t = gvn.type(m)->meet_speculative(gvn.type(n));
1129 Node* phi = PhiNode::make(region, m, t);
1130 phi->set_req(2, n);
1131 kit.map()->set_req(i, gvn.transform(phi));
1132 }
1133 }
1134 return kit.transfer_exceptions_into_jvms();
1135 }
1136
1137
1138 CallGenerator* CallGenerator::for_method_handle_call(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline) {
1139 assert(callee->is_method_handle_intrinsic(), "for_method_handle_call mismatch");
1140 bool input_not_const;
1141 CallGenerator* cg = CallGenerator::for_method_handle_inline(jvms, caller, callee, allow_inline, input_not_const);
1142 Compile* C = Compile::current();
1143 bool should_delay = C->should_delay_inlining();
1144 if (cg != nullptr) {
1145 if (should_delay && IncrementalInlineMH) {
1146 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1147 } else {
1148 return cg;
1149 }
1150 }
1151 int bci = jvms->bci();
1152 ciCallProfile profile = caller->call_profile_at_bci(bci);
1153 int call_site_count = caller->scale_count(profile.count());
1154
1155 if (IncrementalInlineMH && (AlwaysIncrementalInline ||
1156 (call_site_count > 0 && (should_delay || input_not_const || !C->inlining_incrementally() || C->over_inlining_cutoff())))) {
1157 return CallGenerator::for_mh_late_inline(caller, callee, input_not_const);
1158 } else {
1159 // Out-of-line call.
1160 return CallGenerator::for_direct_call(callee);
1161 }
1162 }
1163
1164
1165 CallGenerator* CallGenerator::for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const) {
1166 GraphKit kit(jvms);
1167 PhaseGVN& gvn = kit.gvn();
1168 Compile* C = kit.C;
1169 vmIntrinsics::ID iid = callee->intrinsic_id();
1170 input_not_const = true;
1171 if (StressMethodHandleLinkerInlining) {
1172 allow_inline = false;
1173 }
1174 switch (iid) {
1175 case vmIntrinsics::_invokeBasic:
1176 {
1177 // Get MethodHandle receiver:
1178 Node* receiver = kit.argument(0);
1179 if (receiver->Opcode() == Op_ConP) {
1180 input_not_const = false;
1181 const TypeOopPtr* recv_toop = receiver->bottom_type()->isa_oopptr();
1182 if (recv_toop != nullptr) {
1183 ciMethod* target = recv_toop->const_oop()->as_method_handle()->get_vmtarget();
1184 const int vtable_index = Method::invalid_vtable_index;
1192 false /* call_does_dispatch */,
1193 jvms,
1194 allow_inline,
1195 PROB_ALWAYS);
1196 return cg;
1197 } else {
1198 assert(receiver->bottom_type() == TypePtr::NULL_PTR, "not a null: %s",
1199 Type::str(receiver->bottom_type()));
1200 print_inlining_failure(C, callee, jvms, "receiver is always null");
1201 }
1202 } else {
1203 print_inlining_failure(C, callee, jvms, "receiver not constant");
1204 }
1205 } break;
1206
1207 case vmIntrinsics::_linkToVirtual:
1208 case vmIntrinsics::_linkToStatic:
1209 case vmIntrinsics::_linkToSpecial:
1210 case vmIntrinsics::_linkToInterface:
1211 {
1212 int nargs = callee->arg_size();
1213 // Get MemberName argument:
1214 Node* member_name = kit.argument(nargs - 1);
1215 if (member_name->Opcode() == Op_ConP) {
1216 input_not_const = false;
1217 const TypeOopPtr* oop_ptr = member_name->bottom_type()->is_oopptr();
1218 ciMethod* target = oop_ptr->const_oop()->as_member_name()->get_vmtarget();
1219
1220 if (!ciMethod::is_consistent_info(callee, target)) {
1221 print_inlining_failure(C, callee, jvms, "signatures mismatch");
1222 return nullptr;
1223 }
1224
1225 // In lambda forms we erase signature types to avoid resolving issues
1226 // involving class loaders. When we optimize a method handle invoke
1227 // to a direct call we must cast the receiver and arguments to its
1228 // actual types.
1229 ciSignature* signature = target->signature();
1230 const int receiver_skip = target->is_static() ? 0 : 1;
1231 // Cast receiver to its type.
1232 if (!target->is_static()) {
1233 Node* recv = kit.argument(0);
1234 Node* casted_recv = kit.maybe_narrow_object_type(recv, signature->accessing_klass(), target->receiver_maybe_larval());
1235 if (casted_recv->is_top()) {
1236 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1237 return nullptr; // FIXME: effectively dead; issue a halt node instead
1238 } else if (casted_recv != recv) {
1239 kit.set_argument(0, casted_recv);
1240 }
1241 }
1242 // Cast reference arguments to its type.
1243 for (int i = 0, j = 0; i < signature->count(); i++) {
1244 ciType* t = signature->type_at(i);
1245 if (t->is_klass()) {
1246 Node* arg = kit.argument(receiver_skip + j);
1247 Node* casted_arg = kit.maybe_narrow_object_type(arg, t->as_klass(), false);
1248 if (casted_arg->is_top()) {
1249 print_inlining_failure(C, callee, jvms, "argument types mismatch");
1250 return nullptr; // FIXME: effectively dead; issue a halt node instead
1251 } else if (casted_arg != arg) {
1252 kit.set_argument(receiver_skip + j, casted_arg);
1253 }
1254 }
1255 j += t->size(); // long and double take two slots
1256 }
1257
1258 // Try to get the most accurate receiver type
1259 const bool is_virtual = (iid == vmIntrinsics::_linkToVirtual);
1260 const bool is_virtual_or_interface = (is_virtual || iid == vmIntrinsics::_linkToInterface);
1261 int vtable_index = Method::invalid_vtable_index;
1262 bool call_does_dispatch = false;
1263
1264 ciKlass* speculative_receiver_type = nullptr;
1265 if (is_virtual_or_interface) {
1266 ciInstanceKlass* klass = target->holder();
1267 Node* receiver_node = kit.argument(0);
1268 const TypeOopPtr* receiver_type = gvn.type(receiver_node)->isa_oopptr();
1269 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
1270 // optimize_virtual_call() takes 2 different holder
1271 // arguments for a corner case that doesn't apply here (see
1272 // Parse::do_call())
1273 target = C->optimize_virtual_call(caller, klass, klass,
1274 target, receiver_type, is_virtual,
1275 call_does_dispatch, vtable_index, // out-parameters
1276 false /* check_access */);
1277 // We lack profiling at this call but type speculation may
1278 // provide us with a type
1279 speculative_receiver_type = (receiver_type != nullptr) ? receiver_type->speculative_type() : nullptr;
1280 }
1281 CallGenerator* cg = C->call_generator(target, vtable_index, call_does_dispatch, jvms,
1282 allow_inline,
1283 PROB_ALWAYS,
1284 speculative_receiver_type,
1285 true);
1286 return cg;
1287 } else {
1288 print_inlining_failure(C, callee, jvms, "member_name not constant");
1289 }
1290 } break;
1291
1292 case vmIntrinsics::_linkToNative:
1293 print_inlining_failure(C, callee, jvms, "native call");
1294 break;
1295
1296 default:
1297 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
1298 break;
1299 }
1300 return nullptr;
1301 }
1302
1303 //------------------------PredicatedIntrinsicGenerator------------------------------
1304 // Internal class which handles all predicated Intrinsic calls.
1305 class PredicatedIntrinsicGenerator : public CallGenerator {
1337 // do_intrinsic(0)
1338 // else
1339 // if (predicate(1))
1340 // do_intrinsic(1)
1341 // ...
1342 // else
1343 // do_java_comp
1344
1345 GraphKit kit(jvms);
1346 PhaseGVN& gvn = kit.gvn();
1347
1348 CompileLog* log = kit.C->log();
1349 if (log != nullptr) {
1350 log->elem("predicated_intrinsic bci='%d' method='%d'",
1351 jvms->bci(), log->identify(method()));
1352 }
1353
1354 if (!method()->is_static()) {
1355 // We need an explicit receiver null_check before checking its type in predicate.
1356 // We share a map with the caller, so his JVMS gets adjusted.
1357 kit.null_check_receiver_before_call(method());
1358 if (kit.stopped()) {
1359 return kit.transfer_exceptions_into_jvms();
1360 }
1361 }
1362
1363 int n_predicates = _intrinsic->predicates_count();
1364 assert(n_predicates > 0, "sanity");
1365
1366 JVMState** result_jvms = NEW_RESOURCE_ARRAY(JVMState*, (n_predicates+1));
1367
1368 // Region for normal compilation code if intrinsic failed.
1369 Node* slow_region = new RegionNode(1);
1370
1371 int results = 0;
1372 for (int predicate = 0; (predicate < n_predicates) && !kit.stopped(); predicate++) {
1373 #ifdef ASSERT
1374 JVMState* old_jvms = kit.jvms();
1375 SafePointNode* old_map = kit.map();
1376 Node* old_io = old_map->i_o();
1377 Node* old_mem = old_map->memory();
|