8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciCallSite.hpp"
26 #include "ci/ciMethodHandle.hpp"
27 #include "ci/ciSymbols.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "compiler/compileLog.hpp"
31 #include "interpreter/linkResolver.hpp"
32 #include "logging/log.hpp"
33 #include "logging/logLevel.hpp"
34 #include "logging/logMessage.hpp"
35 #include "logging/logStream.hpp"
36 #include "opto/addnode.hpp"
37 #include "opto/callGenerator.hpp"
38 #include "opto/castnode.hpp"
39 #include "opto/cfgnode.hpp"
40 #include "opto/graphKit.hpp"
41 #include "opto/mulnode.hpp"
42 #include "opto/parse.hpp"
43 #include "opto/rootnode.hpp"
44 #include "opto/runtime.hpp"
45 #include "opto/subnode.hpp"
46 #include "prims/methodHandles.hpp"
47 #include "runtime/sharedRuntime.hpp"
48 #include "utilities/macros.hpp"
49 #if INCLUDE_JFR
50 #include "jfr/jfr.hpp"
51 #endif
52
53 static void print_trace_type_profile(outputStream* out, int depth, ciKlass* prof_klass, int site_count, int receiver_count,
54 bool with_deco) {
55 if (with_deco) {
56 CompileTask::print_inline_indent(depth, out);
57 }
58 out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
59 prof_klass->name()->print_symbol_on(out);
60 if (with_deco) {
61 out->cr();
62 }
63 }
64
65 static void trace_type_profile(Compile* C, ciMethod* method, JVMState* jvms,
66 ciMethod* prof_method, ciKlass* prof_klass, int site_count, int receiver_count) {
67 int depth = jvms->depth() - 1;
68 int bci = jvms->bci();
615 // Speculative type of the receiver if any
616 ciKlass* speculative_receiver_type = nullptr;
617 if (is_virtual_or_interface) {
618 Node* receiver_node = stack(sp() - nargs);
619 const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
620 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
621 // For arrays, klass below is Object. When vtable calls are used,
622 // resolving the call with Object would allow an illegal call to
623 // finalize() on an array. We use holder instead: illegal calls to
624 // finalize() won't be compiled as vtable calls (IC call
625 // resolution will catch the illegal call) and the few legal calls
626 // on array types won't be either.
627 callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
628 receiver_type, is_virtual,
629 call_does_dispatch, vtable_index); // out-parameters
630 speculative_receiver_type = receiver_type != nullptr ? receiver_type->speculative_type() : nullptr;
631 }
632
633 // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
634 ciKlass* receiver_constraint = nullptr;
635 if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_initializer()) {
636 ciInstanceKlass* calling_klass = method()->holder();
637 ciInstanceKlass* sender_klass = calling_klass;
638 if (sender_klass->is_interface()) {
639 receiver_constraint = sender_klass;
640 }
641 } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
642 assert(holder->is_interface(), "How did we get a non-interface method here!");
643 receiver_constraint = holder;
644 }
645
646 if (receiver_constraint != nullptr) {
647 Node* receiver_node = stack(sp() - nargs);
648 Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint, Type::trust_interfaces));
649 Node* bad_type_ctrl = nullptr;
650 Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl);
651 if (bad_type_ctrl != nullptr) {
652 PreserveJVMState pjvms(this);
653 set_control(bad_type_ctrl);
654 uncommon_trap(Deoptimization::Reason_class_check,
655 Deoptimization::Action_none);
656 }
657 if (stopped()) {
658 return; // MUST uncommon-trap?
659 }
660 set_stack(sp() - nargs, casted_receiver);
661 }
662
663 // Note: It's OK to try to inline a virtual call.
664 // The call generator will not attempt to inline a polymorphic call
665 // unless it knows how to optimize the receiver dispatch.
666 bool try_inline = (C->do_inlining() || InlineAccessors);
667
668 // ---------------------
669 dec_sp(nargs); // Temporarily pop args for JVM state of call
670 JVMState* jvms = sync_jvms();
671
672 // ---------------------
673 // Decide call tactic.
674 // This call checks with CHA, the interpreter profile, intrinsics table, etc.
675 // It decides whether inlining is desirable or not.
676 CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
677
678 // NOTE: Don't use orig_callee and callee after this point! Use cg->method() instead.
679 orig_callee = callee = nullptr;
680
681 // ---------------------
682
683 // Feed profiling data for arguments to the type system so it can
684 // propagate it as speculative types
685 record_profiled_arguments_for_speculation(cg->method(), bc());
686
687 #ifndef PRODUCT
688 // bump global counters for calls
689 count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
690
691 // Record first part of parsing work for this call
692 parse_histogram()->record_change();
693 #endif // not PRODUCT
694
695 assert(jvms == this->jvms(), "still operating on the right JVMS");
696 assert(jvms_in_sync(), "jvms must carry full info into CG");
743
744 assert(check_call_consistency(jvms, cg), "inconsistent info");
745
746 if (!stopped()) {
747 // This was some sort of virtual call, which did a null check for us.
748 // Now we can assert receiver-not-null, on the normal return path.
749 if (receiver != nullptr && cg->is_virtual()) {
750 Node* cast = cast_not_null(receiver);
751 // %%% assert(receiver == cast, "should already have cast the receiver");
752 }
753
754 ciType* rtype = cg->method()->return_type();
755 ciType* ctype = declared_signature->return_type();
756
757 if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
758 // Be careful here with return types.
759 if (ctype != rtype) {
760 BasicType rt = rtype->basic_type();
761 BasicType ct = ctype->basic_type();
762 if (ct == T_VOID) {
763 // It's OK for a method to return a value that is discarded.
764 // The discarding does not require any special action from the caller.
765 // The Java code knows this, at VerifyType.isNullConversion.
766 pop_node(rt); // whatever it was, pop it
767 } else if (rt == T_INT || is_subword_type(rt)) {
768 // Nothing. These cases are handled in lambda form bytecode.
769 assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
770 } else if (is_reference_type(rt)) {
771 assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
772 if (ctype->is_loaded()) {
773 const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
774 const Type* sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
775 if (arg_type != nullptr && !arg_type->higher_equal(sig_type)) {
776 Node* retnode = pop();
777 Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
778 push(cast_obj);
779 }
780 }
781 } else {
782 assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
783 // push a zero; it's better than getting an oop/int mismatch
801 // If the return type of the method is not loaded, assert that the
802 // value we got is a null. Otherwise, we need to recompile.
803 if (!rtype->is_loaded()) {
804 if (PrintOpto && (Verbose || WizardMode)) {
805 method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
806 cg->method()->print_name(); tty->cr();
807 }
808 if (C->log() != nullptr) {
809 C->log()->elem("assert_null reason='return' klass='%d'",
810 C->log()->identify(rtype));
811 }
812 // If there is going to be a trap, put it at the next bytecode:
813 set_bci(iter().next_bci());
814 null_assert(peek());
815 set_bci(iter().cur_bci()); // put it back
816 }
817 BasicType ct = ctype->basic_type();
818 if (is_reference_type(ct)) {
819 record_profiled_return_for_speculation();
820 }
821 }
822
823 // Restart record of parsing work after possible inlining of call
824 #ifndef PRODUCT
825 parse_histogram()->set_initial_state(bc());
826 #endif
827 }
828
829 //---------------------------catch_call_exceptions-----------------------------
830 // Put a Catch and CatchProj nodes behind a just-created call.
831 // Send their caught exceptions to the proper handler.
832 // This may be used after a call to the rethrow VM stub,
833 // when it is needed to process unloaded exception classes.
834 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
835 // Exceptions are delivered through this channel:
836 Node* i_o = this->i_o();
837
838 // Add a CatchNode.
839 Arena tmp_mem{mtCompiler};
840 GrowableArray<int> bcis(&tmp_mem, 8, 0, -1);
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciCallSite.hpp"
26 #include "ci/ciMethodHandle.hpp"
27 #include "ci/ciSymbols.hpp"
28 #include "classfile/vmIntrinsics.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "compiler/compileLog.hpp"
32 #include "interpreter/linkResolver.hpp"
33 #include "jvm_io.h"
34 #include "logging/log.hpp"
35 #include "logging/logLevel.hpp"
36 #include "logging/logMessage.hpp"
37 #include "logging/logStream.hpp"
38 #include "opto/addnode.hpp"
39 #include "opto/callGenerator.hpp"
40 #include "opto/castnode.hpp"
41 #include "opto/cfgnode.hpp"
42 #include "opto/graphKit.hpp"
43 #include "opto/inlinetypenode.hpp"
44 #include "opto/mulnode.hpp"
45 #include "opto/parse.hpp"
46 #include "opto/rootnode.hpp"
47 #include "opto/runtime.hpp"
48 #include "opto/subnode.hpp"
49 #include "prims/methodHandles.hpp"
50 #include "runtime/sharedRuntime.hpp"
51 #include "utilities/macros.hpp"
52 #include "utilities/ostream.hpp"
53 #if INCLUDE_JFR
54 #include "jfr/jfr.hpp"
55 #endif
56
57 static void print_trace_type_profile(outputStream* out, int depth, ciKlass* prof_klass, int site_count, int receiver_count,
58 bool with_deco) {
59 if (with_deco) {
60 CompileTask::print_inline_indent(depth, out);
61 }
62 out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
63 prof_klass->name()->print_symbol_on(out);
64 if (with_deco) {
65 out->cr();
66 }
67 }
68
69 static void trace_type_profile(Compile* C, ciMethod* method, JVMState* jvms,
70 ciMethod* prof_method, ciKlass* prof_klass, int site_count, int receiver_count) {
71 int depth = jvms->depth() - 1;
72 int bci = jvms->bci();
619 // Speculative type of the receiver if any
620 ciKlass* speculative_receiver_type = nullptr;
621 if (is_virtual_or_interface) {
622 Node* receiver_node = stack(sp() - nargs);
623 const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
624 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
625 // For arrays, klass below is Object. When vtable calls are used,
626 // resolving the call with Object would allow an illegal call to
627 // finalize() on an array. We use holder instead: illegal calls to
628 // finalize() won't be compiled as vtable calls (IC call
629 // resolution will catch the illegal call) and the few legal calls
630 // on array types won't be either.
631 callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
632 receiver_type, is_virtual,
633 call_does_dispatch, vtable_index); // out-parameters
634 speculative_receiver_type = receiver_type != nullptr ? receiver_type->speculative_type() : nullptr;
635 }
636
637 // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
638 ciKlass* receiver_constraint = nullptr;
639 if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_constructor()) {
640 ciInstanceKlass* calling_klass = method()->holder();
641 ciInstanceKlass* sender_klass = calling_klass;
642 if (sender_klass->is_interface()) {
643 receiver_constraint = sender_klass;
644 }
645 } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
646 assert(holder->is_interface(), "How did we get a non-interface method here!");
647 receiver_constraint = holder;
648 }
649
650 if (receiver_constraint != nullptr) {
651 Node* receiver_node = stack(sp() - nargs);
652 Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint, Type::trust_interfaces));
653 Node* bad_type_ctrl = nullptr;
654 SafePointNode* new_cast_failure_map = nullptr;
655 Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl, &new_cast_failure_map);
656 if (bad_type_ctrl != nullptr) {
657 PreserveJVMState pjvms(this);
658 if (new_cast_failure_map != nullptr) {
659 // The current map on the success path could have been modified. Use the dedicated failure path map.
660 set_map(new_cast_failure_map);
661 }
662 set_control(bad_type_ctrl);
663 uncommon_trap(Deoptimization::Reason_class_check,
664 Deoptimization::Action_none);
665 }
666 if (stopped()) {
667 return; // MUST uncommon-trap?
668 }
669 set_stack(sp() - nargs, casted_receiver);
670 }
671
672 // Note: It's OK to try to inline a virtual call.
673 // The call generator will not attempt to inline a polymorphic call
674 // unless it knows how to optimize the receiver dispatch.
675 bool try_inline = (C->do_inlining() || InlineAccessors);
676
677 // ---------------------
678 dec_sp(nargs); // Temporarily pop args for JVM state of call
679 JVMState* jvms = sync_jvms();
680
681 // ---------------------
682 // Decide call tactic.
683 // This call checks with CHA, the interpreter profile, intrinsics table, etc.
684 // It decides whether inlining is desirable or not.
685 CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
686 if (failing()) {
687 return;
688 }
689 assert(cg != nullptr, "must find a CallGenerator for callee %s", callee->name()->as_utf8());
690
691 // NOTE: Don't use orig_callee and callee after this point! Use cg->method() instead.
692 orig_callee = callee = nullptr;
693
694 // ---------------------
695
696 // Feed profiling data for arguments to the type system so it can
697 // propagate it as speculative types
698 record_profiled_arguments_for_speculation(cg->method(), bc());
699
700 #ifndef PRODUCT
701 // bump global counters for calls
702 count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
703
704 // Record first part of parsing work for this call
705 parse_histogram()->record_change();
706 #endif // not PRODUCT
707
708 assert(jvms == this->jvms(), "still operating on the right JVMS");
709 assert(jvms_in_sync(), "jvms must carry full info into CG");
756
757 assert(check_call_consistency(jvms, cg), "inconsistent info");
758
759 if (!stopped()) {
760 // This was some sort of virtual call, which did a null check for us.
761 // Now we can assert receiver-not-null, on the normal return path.
762 if (receiver != nullptr && cg->is_virtual()) {
763 Node* cast = cast_not_null(receiver);
764 // %%% assert(receiver == cast, "should already have cast the receiver");
765 }
766
767 ciType* rtype = cg->method()->return_type();
768 ciType* ctype = declared_signature->return_type();
769
770 if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
771 // Be careful here with return types.
772 if (ctype != rtype) {
773 BasicType rt = rtype->basic_type();
774 BasicType ct = ctype->basic_type();
775 if (ct == T_VOID) {
776 // It's OK for a method to return a value that is discarded.
777 // The discarding does not require any special action from the caller.
778 // The Java code knows this, at VerifyType.isNullConversion.
779 pop_node(rt); // whatever it was, pop it
780 } else if (rt == T_INT || is_subword_type(rt)) {
781 // Nothing. These cases are handled in lambda form bytecode.
782 assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
783 } else if (is_reference_type(rt)) {
784 assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
785 if (ctype->is_loaded()) {
786 const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
787 const Type* sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
788 if (arg_type != nullptr && !arg_type->higher_equal(sig_type)) {
789 Node* retnode = pop();
790 Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
791 push(cast_obj);
792 }
793 }
794 } else {
795 assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
796 // push a zero; it's better than getting an oop/int mismatch
814 // If the return type of the method is not loaded, assert that the
815 // value we got is a null. Otherwise, we need to recompile.
816 if (!rtype->is_loaded()) {
817 if (PrintOpto && (Verbose || WizardMode)) {
818 method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
819 cg->method()->print_name(); tty->cr();
820 }
821 if (C->log() != nullptr) {
822 C->log()->elem("assert_null reason='return' klass='%d'",
823 C->log()->identify(rtype));
824 }
825 // If there is going to be a trap, put it at the next bytecode:
826 set_bci(iter().next_bci());
827 null_assert(peek());
828 set_bci(iter().cur_bci()); // put it back
829 }
830 BasicType ct = ctype->basic_type();
831 if (is_reference_type(ct)) {
832 record_profiled_return_for_speculation();
833 }
834
835 if (!rtype->is_void()) {
836 Node* retnode = peek();
837 const Type* rettype = gvn().type(retnode);
838 if (!cg->method()->return_value_is_larval() && !retnode->is_InlineType() && rettype->is_inlinetypeptr()) {
839 retnode = InlineTypeNode::make_from_oop(this, retnode, rettype->inline_klass());
840 dec_sp(1);
841 push(retnode);
842 }
843 }
844
845 if (cg->method()->receiver_maybe_larval() && receiver != nullptr &&
846 !receiver->is_InlineType() && gvn().type(receiver)->is_inlinetypeptr()) {
847 InlineTypeNode* non_larval = InlineTypeNode::make_from_oop(this, receiver, gvn().type(receiver)->inline_klass());
848 // Relinquish the oop input, we will delay the allocation to the point it is needed, see the
849 // comments in InlineTypeNode::Ideal for more details
850 non_larval = non_larval->clone_if_required(&gvn(), nullptr);
851 non_larval->set_oop(gvn(), null());
852 non_larval->set_is_buffered(gvn(), false);
853 non_larval = gvn().transform(non_larval)->as_InlineType();
854 map()->replace_edge(receiver, non_larval);
855 }
856 }
857
858 // Restart record of parsing work after possible inlining of call
859 #ifndef PRODUCT
860 parse_histogram()->set_initial_state(bc());
861 #endif
862 }
863
864 //---------------------------catch_call_exceptions-----------------------------
865 // Put a Catch and CatchProj nodes behind a just-created call.
866 // Send their caught exceptions to the proper handler.
867 // This may be used after a call to the rethrow VM stub,
868 // when it is needed to process unloaded exception classes.
869 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
870 // Exceptions are delivered through this channel:
871 Node* i_o = this->i_o();
872
873 // Add a CatchNode.
874 Arena tmp_mem{mtCompiler};
875 GrowableArray<int> bcis(&tmp_mem, 8, 0, -1);
|