1 /*
2 * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/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/mulnode.hpp"
41 #include "opto/parse.hpp"
42 #include "opto/rootnode.hpp"
43 #include "opto/runtime.hpp"
44 #include "opto/subnode.hpp"
45 #include "prims/methodHandles.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "utilities/macros.hpp"
48 #if INCLUDE_JFR
49 #include "jfr/jfr.hpp"
50 #endif
51
52 static void print_trace_type_profile(outputStream* out, int depth, ciKlass* prof_klass, int site_count, int receiver_count,
53 bool with_deco) {
54 if (with_deco) {
55 CompileTask::print_inline_indent(depth, out);
56 }
57 out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
58 prof_klass->name()->print_symbol_on(out);
59 if (with_deco) {
597 // Speculative type of the receiver if any
598 ciKlass* speculative_receiver_type = nullptr;
599 if (is_virtual_or_interface) {
600 Node* receiver_node = stack(sp() - nargs);
601 const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
602 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
603 // For arrays, klass below is Object. When vtable calls are used,
604 // resolving the call with Object would allow an illegal call to
605 // finalize() on an array. We use holder instead: illegal calls to
606 // finalize() won't be compiled as vtable calls (IC call
607 // resolution will catch the illegal call) and the few legal calls
608 // on array types won't be either.
609 callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
610 receiver_type, is_virtual,
611 call_does_dispatch, vtable_index); // out-parameters
612 speculative_receiver_type = receiver_type != nullptr ? receiver_type->speculative_type() : nullptr;
613 }
614
615 // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
616 ciKlass* receiver_constraint = nullptr;
617 if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_initializer()) {
618 ciInstanceKlass* calling_klass = method()->holder();
619 ciInstanceKlass* sender_klass = calling_klass;
620 if (sender_klass->is_interface()) {
621 receiver_constraint = sender_klass;
622 }
623 } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
624 assert(holder->is_interface(), "How did we get a non-interface method here!");
625 receiver_constraint = holder;
626 }
627
628 if (receiver_constraint != nullptr) {
629 Node* receiver_node = stack(sp() - nargs);
630 Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint, Type::trust_interfaces));
631 Node* bad_type_ctrl = nullptr;
632 Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl);
633 if (bad_type_ctrl != nullptr) {
634 PreserveJVMState pjvms(this);
635 set_control(bad_type_ctrl);
636 uncommon_trap(Deoptimization::Reason_class_check,
637 Deoptimization::Action_none);
639 if (stopped()) {
640 return; // MUST uncommon-trap?
641 }
642 set_stack(sp() - nargs, casted_receiver);
643 }
644
645 // Note: It's OK to try to inline a virtual call.
646 // The call generator will not attempt to inline a polymorphic call
647 // unless it knows how to optimize the receiver dispatch.
648 bool try_inline = (C->do_inlining() || InlineAccessors);
649
650 // ---------------------
651 dec_sp(nargs); // Temporarily pop args for JVM state of call
652 JVMState* jvms = sync_jvms();
653
654 // ---------------------
655 // Decide call tactic.
656 // This call checks with CHA, the interpreter profile, intrinsics table, etc.
657 // It decides whether inlining is desirable or not.
658 CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
659
660 // NOTE: Don't use orig_callee and callee after this point! Use cg->method() instead.
661 orig_callee = callee = nullptr;
662
663 // ---------------------
664
665 // Feed profiling data for arguments to the type system so it can
666 // propagate it as speculative types
667 record_profiled_arguments_for_speculation(cg->method(), bc());
668
669 #ifndef PRODUCT
670 // bump global counters for calls
671 count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
672
673 // Record first part of parsing work for this call
674 parse_histogram()->record_change();
675 #endif // not PRODUCT
676
677 assert(jvms == this->jvms(), "still operating on the right JVMS");
678 assert(jvms_in_sync(), "jvms must carry full info into CG");
725
726 assert(check_call_consistency(jvms, cg), "inconsistent info");
727
728 if (!stopped()) {
729 // This was some sort of virtual call, which did a null check for us.
730 // Now we can assert receiver-not-null, on the normal return path.
731 if (receiver != nullptr && cg->is_virtual()) {
732 Node* cast = cast_not_null(receiver);
733 // %%% assert(receiver == cast, "should already have cast the receiver");
734 }
735
736 ciType* rtype = cg->method()->return_type();
737 ciType* ctype = declared_signature->return_type();
738
739 if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
740 // Be careful here with return types.
741 if (ctype != rtype) {
742 BasicType rt = rtype->basic_type();
743 BasicType ct = ctype->basic_type();
744 if (ct == T_VOID) {
745 // It's OK for a method to return a value that is discarded.
746 // The discarding does not require any special action from the caller.
747 // The Java code knows this, at VerifyType.isNullConversion.
748 pop_node(rt); // whatever it was, pop it
749 } else if (rt == T_INT || is_subword_type(rt)) {
750 // Nothing. These cases are handled in lambda form bytecode.
751 assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
752 } else if (is_reference_type(rt)) {
753 assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
754 if (ctype->is_loaded()) {
755 const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
756 const Type* sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
757 if (arg_type != nullptr && !arg_type->higher_equal(sig_type)) {
758 Node* retnode = pop();
759 Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
760 push(cast_obj);
761 }
762 }
763 } else {
764 assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
765 // push a zero; it's better than getting an oop/int mismatch
783 // If the return type of the method is not loaded, assert that the
784 // value we got is a null. Otherwise, we need to recompile.
785 if (!rtype->is_loaded()) {
786 if (PrintOpto && (Verbose || WizardMode)) {
787 method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
788 cg->method()->print_name(); tty->cr();
789 }
790 if (C->log() != nullptr) {
791 C->log()->elem("assert_null reason='return' klass='%d'",
792 C->log()->identify(rtype));
793 }
794 // If there is going to be a trap, put it at the next bytecode:
795 set_bci(iter().next_bci());
796 null_assert(peek());
797 set_bci(iter().cur_bci()); // put it back
798 }
799 BasicType ct = ctype->basic_type();
800 if (is_reference_type(ct)) {
801 record_profiled_return_for_speculation();
802 }
803 }
804
805 // Restart record of parsing work after possible inlining of call
806 #ifndef PRODUCT
807 parse_histogram()->set_initial_state(bc());
808 #endif
809 }
810
811 //---------------------------catch_call_exceptions-----------------------------
812 // Put a Catch and CatchProj nodes behind a just-created call.
813 // Send their caught exceptions to the proper handler.
814 // This may be used after a call to the rethrow VM stub,
815 // when it is needed to process unloaded exception classes.
816 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
817 // Exceptions are delivered through this channel:
818 Node* i_o = this->i_o();
819
820 // Add a CatchNode.
821 Arena tmp_mem{mtCompiler};
822 GrowableArray<int> bcis(&tmp_mem, 8, 0, -1);
|
1 /*
2 * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/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/inlinetypenode.hpp"
43 #include "opto/mulnode.hpp"
44 #include "opto/parse.hpp"
45 #include "opto/rootnode.hpp"
46 #include "opto/runtime.hpp"
47 #include "opto/subnode.hpp"
48 #include "prims/methodHandles.hpp"
49 #include "runtime/sharedRuntime.hpp"
50 #include "utilities/macros.hpp"
51 #if INCLUDE_JFR
52 #include "jfr/jfr.hpp"
53 #endif
54
55 static void print_trace_type_profile(outputStream* out, int depth, ciKlass* prof_klass, int site_count, int receiver_count,
56 bool with_deco) {
57 if (with_deco) {
58 CompileTask::print_inline_indent(depth, out);
59 }
60 out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
61 prof_klass->name()->print_symbol_on(out);
62 if (with_deco) {
600 // Speculative type of the receiver if any
601 ciKlass* speculative_receiver_type = nullptr;
602 if (is_virtual_or_interface) {
603 Node* receiver_node = stack(sp() - nargs);
604 const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
605 // call_does_dispatch and vtable_index are out-parameters. They might be changed.
606 // For arrays, klass below is Object. When vtable calls are used,
607 // resolving the call with Object would allow an illegal call to
608 // finalize() on an array. We use holder instead: illegal calls to
609 // finalize() won't be compiled as vtable calls (IC call
610 // resolution will catch the illegal call) and the few legal calls
611 // on array types won't be either.
612 callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
613 receiver_type, is_virtual,
614 call_does_dispatch, vtable_index); // out-parameters
615 speculative_receiver_type = receiver_type != nullptr ? receiver_type->speculative_type() : nullptr;
616 }
617
618 // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
619 ciKlass* receiver_constraint = nullptr;
620 if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_constructor()) {
621 ciInstanceKlass* calling_klass = method()->holder();
622 ciInstanceKlass* sender_klass = calling_klass;
623 if (sender_klass->is_interface()) {
624 receiver_constraint = sender_klass;
625 }
626 } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
627 assert(holder->is_interface(), "How did we get a non-interface method here!");
628 receiver_constraint = holder;
629 }
630
631 if (receiver_constraint != nullptr) {
632 Node* receiver_node = stack(sp() - nargs);
633 Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint, Type::trust_interfaces));
634 Node* bad_type_ctrl = nullptr;
635 Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl);
636 if (bad_type_ctrl != nullptr) {
637 PreserveJVMState pjvms(this);
638 set_control(bad_type_ctrl);
639 uncommon_trap(Deoptimization::Reason_class_check,
640 Deoptimization::Action_none);
642 if (stopped()) {
643 return; // MUST uncommon-trap?
644 }
645 set_stack(sp() - nargs, casted_receiver);
646 }
647
648 // Note: It's OK to try to inline a virtual call.
649 // The call generator will not attempt to inline a polymorphic call
650 // unless it knows how to optimize the receiver dispatch.
651 bool try_inline = (C->do_inlining() || InlineAccessors);
652
653 // ---------------------
654 dec_sp(nargs); // Temporarily pop args for JVM state of call
655 JVMState* jvms = sync_jvms();
656
657 // ---------------------
658 // Decide call tactic.
659 // This call checks with CHA, the interpreter profile, intrinsics table, etc.
660 // It decides whether inlining is desirable or not.
661 CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
662 if (failing()) {
663 return;
664 }
665 assert(cg != nullptr, "must find a CallGenerator for callee %s", callee->name()->as_utf8());
666
667 // NOTE: Don't use orig_callee and callee after this point! Use cg->method() instead.
668 orig_callee = callee = nullptr;
669
670 // ---------------------
671
672 // Feed profiling data for arguments to the type system so it can
673 // propagate it as speculative types
674 record_profiled_arguments_for_speculation(cg->method(), bc());
675
676 #ifndef PRODUCT
677 // bump global counters for calls
678 count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
679
680 // Record first part of parsing work for this call
681 parse_histogram()->record_change();
682 #endif // not PRODUCT
683
684 assert(jvms == this->jvms(), "still operating on the right JVMS");
685 assert(jvms_in_sync(), "jvms must carry full info into CG");
732
733 assert(check_call_consistency(jvms, cg), "inconsistent info");
734
735 if (!stopped()) {
736 // This was some sort of virtual call, which did a null check for us.
737 // Now we can assert receiver-not-null, on the normal return path.
738 if (receiver != nullptr && cg->is_virtual()) {
739 Node* cast = cast_not_null(receiver);
740 // %%% assert(receiver == cast, "should already have cast the receiver");
741 }
742
743 ciType* rtype = cg->method()->return_type();
744 ciType* ctype = declared_signature->return_type();
745
746 if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
747 // Be careful here with return types.
748 if (ctype != rtype) {
749 BasicType rt = rtype->basic_type();
750 BasicType ct = ctype->basic_type();
751 if (ct == T_VOID) {
752 // It's OK for a method to return a value that is discarded.
753 // The discarding does not require any special action from the caller.
754 // The Java code knows this, at VerifyType.isNullConversion.
755 pop_node(rt); // whatever it was, pop it
756 } else if (rt == T_INT || is_subword_type(rt)) {
757 // Nothing. These cases are handled in lambda form bytecode.
758 assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
759 } else if (is_reference_type(rt)) {
760 assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
761 if (ctype->is_loaded()) {
762 const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
763 const Type* sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
764 if (arg_type != nullptr && !arg_type->higher_equal(sig_type)) {
765 Node* retnode = pop();
766 Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
767 push(cast_obj);
768 }
769 }
770 } else {
771 assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
772 // push a zero; it's better than getting an oop/int mismatch
790 // If the return type of the method is not loaded, assert that the
791 // value we got is a null. Otherwise, we need to recompile.
792 if (!rtype->is_loaded()) {
793 if (PrintOpto && (Verbose || WizardMode)) {
794 method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
795 cg->method()->print_name(); tty->cr();
796 }
797 if (C->log() != nullptr) {
798 C->log()->elem("assert_null reason='return' klass='%d'",
799 C->log()->identify(rtype));
800 }
801 // If there is going to be a trap, put it at the next bytecode:
802 set_bci(iter().next_bci());
803 null_assert(peek());
804 set_bci(iter().cur_bci()); // put it back
805 }
806 BasicType ct = ctype->basic_type();
807 if (is_reference_type(ct)) {
808 record_profiled_return_for_speculation();
809 }
810
811 if (!rtype->is_void()) {
812 Node* retnode = peek();
813 const Type* rettype = gvn().type(retnode);
814 if (rettype->is_inlinetypeptr() && !retnode->is_InlineType()) {
815 retnode = InlineTypeNode::make_from_oop(this, retnode, rettype->inline_klass());
816 dec_sp(1);
817 push(retnode);
818 }
819 }
820
821 if (cg->method()->is_object_constructor() && receiver != nullptr && gvn().type(receiver)->is_inlinetypeptr()) {
822 InlineTypeNode* non_larval = InlineTypeNode::make_from_oop(this, receiver, gvn().type(receiver)->inline_klass());
823 // Relinquish the oop input, we will delay the allocation to the point it is needed, see the
824 // comments in InlineTypeNode::Ideal for more details
825 non_larval = non_larval->clone_if_required(&gvn(), nullptr);
826 non_larval->set_oop(gvn(), null());
827 non_larval->set_is_buffered(gvn(), false);
828 non_larval = gvn().transform(non_larval)->as_InlineType();
829 map()->replace_edge(receiver, non_larval);
830 }
831 }
832
833 // Restart record of parsing work after possible inlining of call
834 #ifndef PRODUCT
835 parse_histogram()->set_initial_state(bc());
836 #endif
837 }
838
839 //---------------------------catch_call_exceptions-----------------------------
840 // Put a Catch and CatchProj nodes behind a just-created call.
841 // Send their caught exceptions to the proper handler.
842 // This may be used after a call to the rethrow VM stub,
843 // when it is needed to process unloaded exception classes.
844 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
845 // Exceptions are delivered through this channel:
846 Node* i_o = this->i_o();
847
848 // Add a CatchNode.
849 Arena tmp_mem{mtCompiler};
850 GrowableArray<int> bcis(&tmp_mem, 8, 0, -1);
|