< prev index next >

src/hotspot/share/opto/doCall.cpp

Print this page

   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) {
  60     out->cr();
  61   }
  62 }
  63 
  64 static void trace_type_profile(Compile* C, ciMethod* method, JVMState* jvms,
  65                                ciMethod* prof_method, ciKlass* prof_klass, int site_count, int receiver_count) {
  66   int depth = jvms->depth() - 1;
  67   int bci = jvms->bci();

 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);
 638     }
 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 #include "utilities/ostream.hpp"
  52 #if INCLUDE_JFR
  53 #include "jfr/jfr.hpp"
  54 #endif
  55 
  56 static void print_trace_type_profile(outputStream* out, int depth, ciKlass* prof_klass, int site_count, int receiver_count,
  57                                      bool with_deco) {
  58   if (with_deco) {
  59     CompileTask::print_inline_indent(depth, out);
  60   }
  61   out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count);
  62   prof_klass->name()->print_symbol_on(out);
  63   if (with_deco) {
  64     out->cr();
  65   }
  66 }
  67 
  68 static void trace_type_profile(Compile* C, ciMethod* method, JVMState* jvms,
  69                                ciMethod* prof_method, ciKlass* prof_klass, int site_count, int receiver_count) {
  70   int depth = jvms->depth() - 1;
  71   int bci = jvms->bci();

 601   // Speculative type of the receiver if any
 602   ciKlass* speculative_receiver_type = nullptr;
 603   if (is_virtual_or_interface) {
 604     Node* receiver_node             = stack(sp() - nargs);
 605     const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
 606     // call_does_dispatch and vtable_index are out-parameters.  They might be changed.
 607     // For arrays, klass below is Object. When vtable calls are used,
 608     // resolving the call with Object would allow an illegal call to
 609     // finalize() on an array. We use holder instead: illegal calls to
 610     // finalize() won't be compiled as vtable calls (IC call
 611     // resolution will catch the illegal call) and the few legal calls
 612     // on array types won't be either.
 613     callee = C->optimize_virtual_call(method(), klass, holder, orig_callee,
 614                                       receiver_type, is_virtual,
 615                                       call_does_dispatch, vtable_index);  // out-parameters
 616     speculative_receiver_type = receiver_type != nullptr ? receiver_type->speculative_type() : nullptr;
 617   }
 618 
 619   // Additional receiver subtype checks for interface calls via invokespecial or invokeinterface.
 620   ciKlass* receiver_constraint = nullptr;
 621   if (iter().cur_bc_raw() == Bytecodes::_invokespecial && !orig_callee->is_object_constructor()) {
 622     ciInstanceKlass* calling_klass = method()->holder();
 623     ciInstanceKlass* sender_klass = calling_klass;
 624     if (sender_klass->is_interface()) {
 625       receiver_constraint = sender_klass;
 626     }
 627   } else if (iter().cur_bc_raw() == Bytecodes::_invokeinterface && orig_callee->is_private()) {
 628     assert(holder->is_interface(), "How did we get a non-interface method here!");
 629     receiver_constraint = holder;
 630   }
 631 
 632   if (receiver_constraint != nullptr) {
 633     Node* receiver_node = stack(sp() - nargs);
 634     Node* cls_node = makecon(TypeKlassPtr::make(receiver_constraint, Type::trust_interfaces));
 635     Node* bad_type_ctrl = nullptr;
 636     SafePointNode* new_cast_failure_map = nullptr;
 637     Node* casted_receiver = gen_checkcast(receiver_node, cls_node, &bad_type_ctrl, &new_cast_failure_map);
 638     if (bad_type_ctrl != nullptr) {
 639       PreserveJVMState pjvms(this);
 640       if (new_cast_failure_map != nullptr) {
 641         // The current map on the success path could have been modified. Use the dedicated failure path map.
 642         set_map(new_cast_failure_map);
 643       }
 644       set_control(bad_type_ctrl);
 645       uncommon_trap(Deoptimization::Reason_class_check,
 646                     Deoptimization::Action_none);
 647     }
 648     if (stopped()) {
 649       return; // MUST uncommon-trap?
 650     }
 651     set_stack(sp() - nargs, casted_receiver);
 652   }
 653 
 654   // Note:  It's OK to try to inline a virtual call.
 655   // The call generator will not attempt to inline a polymorphic call
 656   // unless it knows how to optimize the receiver dispatch.
 657   bool try_inline = (C->do_inlining() || InlineAccessors);
 658 
 659   // ---------------------
 660   dec_sp(nargs);              // Temporarily pop args for JVM state of call
 661   JVMState* jvms = sync_jvms();
 662 
 663   // ---------------------
 664   // Decide call tactic.
 665   // This call checks with CHA, the interpreter profile, intrinsics table, etc.
 666   // It decides whether inlining is desirable or not.
 667   CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type);
 668   if (failing()) {
 669     return;
 670   }
 671   assert(cg != nullptr, "must find a CallGenerator for callee %s", callee->name()->as_utf8());
 672 
 673   // NOTE:  Don't use orig_callee and callee after this point!  Use cg->method() instead.
 674   orig_callee = callee = nullptr;
 675 
 676   // ---------------------
 677 
 678   // Feed profiling data for arguments to the type system so it can
 679   // propagate it as speculative types
 680   record_profiled_arguments_for_speculation(cg->method(), bc());
 681 
 682 #ifndef PRODUCT
 683   // bump global counters for calls
 684   count_compiled_calls(/*at_method_entry*/ false, cg->is_inline());
 685 
 686   // Record first part of parsing work for this call
 687   parse_histogram()->record_change();
 688 #endif // not PRODUCT
 689 
 690   assert(jvms == this->jvms(), "still operating on the right JVMS");
 691   assert(jvms_in_sync(),       "jvms must carry full info into CG");

 738 
 739   assert(check_call_consistency(jvms, cg), "inconsistent info");
 740 
 741   if (!stopped()) {
 742     // This was some sort of virtual call, which did a null check for us.
 743     // Now we can assert receiver-not-null, on the normal return path.
 744     if (receiver != nullptr && cg->is_virtual()) {
 745       Node* cast = cast_not_null(receiver);
 746       // %%% assert(receiver == cast, "should already have cast the receiver");
 747     }
 748 
 749     ciType* rtype = cg->method()->return_type();
 750     ciType* ctype = declared_signature->return_type();
 751 
 752     if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) {
 753       // Be careful here with return types.
 754       if (ctype != rtype) {
 755         BasicType rt = rtype->basic_type();
 756         BasicType ct = ctype->basic_type();
 757         if (ct == T_VOID) {
 758           // It's OK for a method to return a value that is discarded.
 759           // The discarding does not require any special action from the caller.
 760           // The Java code knows this, at VerifyType.isNullConversion.
 761           pop_node(rt);  // whatever it was, pop it
 762         } else if (rt == T_INT || is_subword_type(rt)) {
 763           // Nothing.  These cases are handled in lambda form bytecode.
 764           assert(ct == T_INT || is_subword_type(ct), "must match: rt=%s, ct=%s", type2name(rt), type2name(ct));
 765         } else if (is_reference_type(rt)) {
 766           assert(is_reference_type(ct), "rt=%s, ct=%s", type2name(rt), type2name(ct));
 767           if (ctype->is_loaded()) {
 768             const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass());
 769             const Type*       sig_type = TypeOopPtr::make_from_klass(ctype->as_klass());
 770             if (arg_type != nullptr && !arg_type->higher_equal(sig_type)) {
 771               Node* retnode = pop();
 772               Node* cast_obj = _gvn.transform(new CheckCastPPNode(control(), retnode, sig_type));
 773               push(cast_obj);
 774             }
 775           }
 776         } else {
 777           assert(rt == ct, "unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct));
 778           // push a zero; it's better than getting an oop/int mismatch

 796     // If the return type of the method is not loaded, assert that the
 797     // value we got is a null.  Otherwise, we need to recompile.
 798     if (!rtype->is_loaded()) {
 799       if (PrintOpto && (Verbose || WizardMode)) {
 800         method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
 801         cg->method()->print_name(); tty->cr();
 802       }
 803       if (C->log() != nullptr) {
 804         C->log()->elem("assert_null reason='return' klass='%d'",
 805                        C->log()->identify(rtype));
 806       }
 807       // If there is going to be a trap, put it at the next bytecode:
 808       set_bci(iter().next_bci());
 809       null_assert(peek());
 810       set_bci(iter().cur_bci()); // put it back
 811     }
 812     BasicType ct = ctype->basic_type();
 813     if (is_reference_type(ct)) {
 814       record_profiled_return_for_speculation();
 815     }
 816 
 817     if (!rtype->is_void()) {
 818       Node* retnode = peek();
 819       const Type* rettype = gvn().type(retnode);
 820       if (!cg->method()->return_value_is_larval() && !retnode->is_InlineType() && rettype->is_inlinetypeptr()) {
 821         retnode = InlineTypeNode::make_from_oop(this, retnode, rettype->inline_klass());
 822         dec_sp(1);
 823         push(retnode);
 824       }
 825     }
 826 
 827     if (cg->method()->receiver_maybe_larval() && receiver != nullptr &&
 828         !receiver->is_InlineType() && gvn().type(receiver)->is_inlinetypeptr()) {
 829       InlineTypeNode* non_larval = InlineTypeNode::make_from_oop(this, receiver, gvn().type(receiver)->inline_klass());
 830       // Relinquish the oop input, we will delay the allocation to the point it is needed, see the
 831       // comments in InlineTypeNode::Ideal for more details
 832       non_larval = non_larval->clone_if_required(&gvn(), nullptr);
 833       non_larval->set_oop(gvn(), null());
 834       non_larval->set_is_buffered(gvn(), false);
 835       non_larval = gvn().transform(non_larval)->as_InlineType();
 836       map()->replace_edge(receiver, non_larval);
 837     }
 838   }
 839 
 840   // Restart record of parsing work after possible inlining of call
 841 #ifndef PRODUCT
 842   parse_histogram()->set_initial_state(bc());
 843 #endif
 844 }
 845 
 846 //---------------------------catch_call_exceptions-----------------------------
 847 // Put a Catch and CatchProj nodes behind a just-created call.
 848 // Send their caught exceptions to the proper handler.
 849 // This may be used after a call to the rethrow VM stub,
 850 // when it is needed to process unloaded exception classes.
 851 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
 852   // Exceptions are delivered through this channel:
 853   Node* i_o = this->i_o();
 854 
 855   // Add a CatchNode.
 856   Arena tmp_mem{mtCompiler};
 857   GrowableArray<int> bcis(&tmp_mem, 8, 0, -1);
< prev index next >