1 /*
  2  * Copyright (c) 2000, 2023, 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 #ifndef SHARE_OPTO_CALLGENERATOR_HPP
 26 #define SHARE_OPTO_CALLGENERATOR_HPP
 27 
 28 #include "compiler/compileBroker.hpp"
 29 #include "opto/callnode.hpp"
 30 #include "opto/compile.hpp"
 31 #include "opto/type.hpp"
 32 #include "runtime/deoptimization.hpp"
 33 
 34 //---------------------------CallGenerator-------------------------------------
 35 // The subclasses of this class handle generation of ideal nodes for
 36 // call sites and method entry points.
 37 
 38 class CallGenerator : public ArenaObj {
 39  private:
 40   ciMethod*             _method;                // The method being called.
 41 
 42  protected:
 43   CallGenerator(ciMethod* method) : _method(method) {}
 44 
 45   void do_late_inline_helper();
 46 
 47   virtual bool           do_late_inline_check(Compile* C, JVMState* jvms) { ShouldNotReachHere(); return false; }
 48   virtual bool           is_pure_call() const                             { ShouldNotReachHere(); return false; }
 49 
 50  public:
 51   // Accessors
 52   ciMethod*          method() const             { return _method; }
 53 
 54   // is_inline: At least some code implementing the method is copied here.
 55   virtual bool      is_inline() const           { return false; }
 56   // is_intrinsic: There's a method-specific way of generating the inline code.
 57   virtual bool      is_intrinsic() const        { return false; }
 58   // is_parse: Bytecodes implementing the specific method are copied here.
 59   virtual bool      is_parse() const            { return false; }
 60   // is_virtual: The call uses the receiver type to select or check the method.
 61   virtual bool      is_virtual() const          { return false; }
 62   // is_deferred: The decision whether to inline or not is deferred.
 63   virtual bool      is_deferred() const         { return false; }
 64   // is_predicated: Uses an explicit check (predicate).
 65   virtual bool      is_predicated() const       { return false; }
 66   virtual int       predicates_count() const    { return 0; }
 67   // is_trap: Does not return to the caller.  (E.g., uncommon trap.)
 68   virtual bool      is_trap() const             { return false; }
 69   // does_virtual_dispatch: Should try inlining as normal method first.
 70   virtual bool      does_virtual_dispatch() const     { return false; }
 71 
 72   // is_late_inline: supports conversion of call into an inline
 73   virtual bool      is_late_inline() const         { return false; }
 74   // same but for method handle calls
 75   virtual bool      is_mh_late_inline() const      { return false; }
 76   virtual bool      is_string_late_inline() const  { return false; }
 77   virtual bool      is_virtual_late_inline() const { return false; }
 78 
 79   // Replace the call with an inline version of the code
 80   virtual void do_late_inline() { ShouldNotReachHere(); }
 81 
 82   virtual CallNode* call_node() const { return nullptr; }
 83   virtual CallGenerator* with_call_node(CallNode* call)  { return this; }
 84 
 85   virtual void set_unique_id(jlong id)          { fatal("unique id only for late inlines"); };
 86   virtual jlong unique_id() const               { fatal("unique id only for late inlines"); return 0; };
 87 
 88   virtual CallGenerator* inline_cg()    const                             { ShouldNotReachHere(); return nullptr;  }
 89 
 90   virtual void set_callee_method(ciMethod* callee) { ShouldNotReachHere(); }
 91 
 92   // Note:  It is possible for a CG to be both inline and virtual.
 93   // (The hashCode intrinsic does a vtable check and an inlined fast path.)
 94 
 95   // Allocate CallGenerators only in Compile arena since some of them are referenced from CallNodes.
 96   void* operator new(size_t size) throw() {
 97     Compile* C = Compile::current();
 98     return ArenaObj::operator new(size, C->comp_arena());
 99   }
100 
101   // Utilities:
102   const TypeFunc*   tf() const;
103 
104   // The given jvms has state and arguments for a call to my method.
105   // Edges after jvms->argoff() carry all (pre-popped) argument values.
106   //
107   // Update the map with state and return values (if any) and return it.
108   // The return values (0, 1, or 2) must be pushed on the map's stack,
109   // and the sp of the jvms incremented accordingly.
110   //
111   // The jvms is returned on success.  Alternatively, a copy of the
112   // given jvms, suitably updated, may be returned, in which case the
113   // caller should discard the original jvms.
114   //
115   // The non-Parm edges of the returned map will contain updated global state,
116   // and one or two edges before jvms->sp() will carry any return values.
117   // Other map edges may contain locals or monitors, and should not
118   // be changed in meaning.
119   //
120   // If the call traps, the returned map must have a control edge of top.
121   // If the call can throw, the returned map must report has_exceptions().
122   //
123   // If the result is null, it means that this CallGenerator was unable
124   // to handle the given call, and another CallGenerator should be consulted.
125   virtual JVMState* generate(JVMState* jvms) = 0;
126 
127   // How to generate a call site that is inlined:
128   static CallGenerator* for_inline(ciMethod* m, float expected_uses = -1);
129   // How to generate code for an on-stack replacement handler.
130   static CallGenerator* for_osr(ciMethod* m, int osr_bci);
131 
132   // How to generate vanilla out-of-line call sites:
133   static CallGenerator* for_direct_call(ciMethod* m, bool separate_io_projs = false);   // static, special
134   static CallGenerator* for_virtual_call(ciMethod* m, int vtable_index);  // virtual, interface
135 
136   static CallGenerator* for_method_handle_call(  JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline);
137   static CallGenerator* for_method_handle_inline(JVMState* jvms, ciMethod* caller, ciMethod* callee, bool allow_inline, bool& input_not_const);
138 
139   // How to generate a replace a direct call with an inline version
140   static CallGenerator* for_late_inline(ciMethod* m, CallGenerator* inline_cg);
141   static CallGenerator* for_mh_late_inline(ciMethod* caller, ciMethod* callee, bool input_not_const);
142   static CallGenerator* for_string_late_inline(ciMethod* m, CallGenerator* inline_cg);
143   static CallGenerator* for_boxing_late_inline(ciMethod* m, CallGenerator* inline_cg);
144   static CallGenerator* for_vector_reboxing_late_inline(ciMethod* m, CallGenerator* inline_cg);
145   static CallGenerator* for_late_inline_virtual(ciMethod* m, int vtable_index, float expected_uses);
146 
147   // How to make a call that optimistically assumes a receiver type:
148   static CallGenerator* for_predicted_call(ciKlass* predicted_receiver,
149                                            CallGenerator* if_missed,
150                                            CallGenerator* if_hit,
151                                            float hit_prob);
152 
153   static CallGenerator* for_guarded_call(ciKlass* predicted_receiver,
154                                          CallGenerator* if_missed,
155                                          CallGenerator* if_hit);
156 
157   // How to make a call that optimistically assumes a MethodHandle target:
158   static CallGenerator* for_predicted_dynamic_call(ciMethodHandle* predicted_method_handle,
159                                                    CallGenerator* if_missed,
160                                                    CallGenerator* if_hit,
161                                                    float hit_prob);
162 
163   // How to make a call that gives up and goes back to the interpreter:
164   static CallGenerator* for_uncommon_trap(ciMethod* m,
165                                           Deoptimization::DeoptReason reason,
166                                           Deoptimization::DeoptAction action);
167 
168   // Registry for intrinsics:
169   static CallGenerator* for_intrinsic(ciMethod* m);
170   static void register_intrinsic(ciMethod* m, CallGenerator* cg);
171   static CallGenerator* for_predicated_intrinsic(CallGenerator* intrinsic,
172                                                  CallGenerator* cg);
173   virtual Node* generate_predicate(JVMState* jvms, int predicate) { return nullptr; };
174 
175   virtual void print_inlining_late(InliningResult result, const char* msg) { ShouldNotReachHere(); }
176 
177   static void print_inlining(Compile* C, ciMethod* callee, int inline_level, int bci, const char* msg) {
178     print_inlining_impl(C, callee, inline_level, bci, InliningResult::SUCCESS, msg);
179   }
180 
181   static void print_inlining_failure(Compile* C, ciMethod* callee, int inline_level, int bci, const char* msg) {
182     print_inlining_impl(C, callee, inline_level, bci, InliningResult::FAILURE, msg);
183     C->log_inline_failure(msg);
184   }
185 
186   static bool is_inlined_method_handle_intrinsic(JVMState* jvms, ciMethod* m);
187   static bool is_inlined_method_handle_intrinsic(ciMethod* caller, int bci, ciMethod* m);
188   static bool is_inlined_method_handle_intrinsic(ciMethod* symbolic_info, ciMethod* m);
189 
190 private:
191   static void print_inlining_impl(Compile* C, ciMethod* callee, int inline_level, int bci,
192                                   InliningResult result, const char* msg) {
193     if (C->print_inlining()) {
194       C->print_inlining(callee, inline_level, bci, result, msg);
195     }
196   }
197 };
198 
199 
200 //------------------------InlineCallGenerator----------------------------------
201 class InlineCallGenerator : public CallGenerator {
202  protected:
203   InlineCallGenerator(ciMethod* method) : CallGenerator(method) {}
204 
205  public:
206   virtual bool      is_inline() const           { return true; }
207 };
208 
209 #endif // SHARE_OPTO_CALLGENERATOR_HPP