1 /*
  2  * Copyright (c) 1999, 2024, 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_CI_CIMETHOD_HPP
 26 #define SHARE_CI_CIMETHOD_HPP
 27 
 28 #include "ci/ciFlags.hpp"
 29 #include "ci/ciInstanceKlass.hpp"
 30 #include "ci/ciObject.hpp"
 31 #include "ci/ciSignature.hpp"
 32 #include "classfile/vmIntrinsics.hpp"
 33 #include "compiler/methodLiveness.hpp"
 34 #include "oops/method.hpp"
 35 #include "runtime/handles.hpp"
 36 #include "utilities/bitMap.hpp"
 37 #include "utilities/vmEnums.hpp"
 38 
 39 class ciMethodBlocks;
 40 class MethodLiveness;
 41 class Arena;
 42 class BCEscapeAnalyzer;
 43 class InlineTree;
 44 class xmlStream;
 45 
 46 // Whether profiling found an oop to be always, never or sometimes
 47 // null
 48 enum ProfilePtrKind {
 49   ProfileAlwaysNull,
 50   ProfileNeverNull,
 51   ProfileMaybeNull
 52 };
 53 
 54 // ciMethod
 55 //
 56 // This class represents a Method* in the HotSpot virtual
 57 // machine.
 58 class ciMethod : public ciMetadata {
 59   friend class CompileBroker;
 60   CI_PACKAGE_ACCESS
 61   friend class ciEnv;
 62   friend class ciExceptionHandlerStream;
 63   friend class ciBytecodeStream;
 64   friend class ciMethodHandle;
 65   friend class ciReplay;
 66   friend class InlineTree;
 67 
 68  private:
 69   // General method information.
 70   ciFlags          _flags;
 71   ciSymbol*        _name;
 72   ciInstanceKlass* _holder;
 73   ciSignature*     _signature;
 74   ciMethodData*    _method_data;
 75   ciMethodBlocks*   _method_blocks;
 76 
 77   // Code attributes.
 78   int _code_size;
 79   int _max_stack;
 80   int _max_locals;
 81   vmIntrinsicID _intrinsic_id;
 82   int _handler_count;
 83   int _interpreter_invocation_count;
 84   int _interpreter_throwout_count;
 85   int _inline_instructions_size;
 86   int _size_of_parameters;
 87 
 88   bool _uses_monitors;
 89   bool _balanced_monitors;
 90   bool _is_c1_compilable;
 91   bool _is_c2_compilable;
 92   bool _can_be_parsed;
 93   bool _can_be_statically_bound;
 94   bool _can_omit_stack_trace;
 95   bool _has_reserved_stack_access;
 96   bool _is_overpass;
 97 
 98   // Lazy fields, filled in on demand
 99   address              _code;
100   ciExceptionHandler** _exception_handlers;
101 
102   // Optional liveness analyzer.
103   MethodLiveness* _liveness;
104 #if defined(COMPILER2)
105   ciTypeFlow*         _flow;
106   BCEscapeAnalyzer*   _bcea;
107 #endif
108 
109   ciMethod(const methodHandle& h_m, ciInstanceKlass* holder);
110   ciMethod(ciInstanceKlass* holder, ciSymbol* name, ciSymbol* signature, ciInstanceKlass* accessor);
111 
112   oop loader() const                             { return _holder->loader(); }
113 
114   const char* type_string()                      { return "ciMethod"; }
115 
116   void print_impl(outputStream* st);
117 
118   void load_code();
119 
120   bool ensure_method_data(const methodHandle& h_m);
121 
122   void code_at_put(int bci, Bytecodes::Code code) {
123     Bytecodes::check(code);
124     assert(0 <= bci && bci < code_size(), "valid bci");
125     address bcp = _code + bci;
126     *bcp = code;
127   }
128 
129   // Check bytecode and profile data collected are compatible
130   void assert_virtual_call_type_ok(int bci);
131   void assert_call_type_ok(int bci);
132 
133   // Check and update the profile counter in case of overflow
134   static int check_overflow(int c, Bytecodes::Code code);
135 
136  public:
137   void check_is_loaded() const                   { assert(is_loaded(), "not loaded"); }
138 
139   // Basic method information.
140   ciFlags flags() const                          { check_is_loaded(); return _flags; }
141   ciSymbol* name() const                         { return _name; }
142   ciInstanceKlass* holder() const                { return _holder; }
143   ciMethodData* method_data();
144   ciMethodData* method_data_or_null();
145 
146   // Signature information.
147   ciSignature* signature() const                 { return _signature; }
148   ciType*      return_type() const               { return _signature->return_type(); }
149   int          arg_size_no_receiver() const      { return _signature->size(); }
150   // Can only be used on loaded ciMethods
151   int          arg_size() const                  {
152     check_is_loaded();
153     return _signature->size() + (_flags.is_static() ? 0 : 1);
154   }
155   // Report the number of elements on stack when invoking the current method.
156   // If the method is loaded, arg_size() gives precise information about the
157   // number of stack elements (using the method's signature and its flags).
158   // However, if the method is not loaded, the number of stack elements must
159   // be determined differently, as the method's flags are not yet available.
160   // The invoke_arg_size() method assumes in that case that all bytecodes except
161   // invokestatic and invokedynamic have a receiver that is also pushed onto the
162   // stack by the caller of the current method.
163   int invoke_arg_size(Bytecodes::Code code) const {
164     if (is_loaded()) {
165       return arg_size();
166     } else {
167       int arg_size = _signature->size();
168       if (code != Bytecodes::_invokestatic &&
169           code != Bytecodes::_invokedynamic) {
170         arg_size++;
171       }
172       return arg_size;
173     }
174   }
175 
176   Method* get_Method() const {
177     Method* m = (Method*)_metadata;
178     assert(m != nullptr, "illegal use of unloaded method");
179     return m;
180   }
181 
182   // Method code and related information.
183   address code()                                 { if (_code == nullptr) load_code(); return _code; }
184   int code_size() const                          { check_is_loaded(); return _code_size; }
185   int max_stack() const                          { check_is_loaded(); return _max_stack; }
186   int max_locals() const                         { check_is_loaded(); return _max_locals; }
187   vmIntrinsicID intrinsic_id() const             { check_is_loaded(); return _intrinsic_id; }
188   bool has_exception_handlers() const            { check_is_loaded(); return _handler_count > 0; }
189   int exception_table_length() const             { check_is_loaded(); return _handler_count; }
190   int interpreter_invocation_count() const       { check_is_loaded(); return _interpreter_invocation_count; }
191   int interpreter_throwout_count() const         { check_is_loaded(); return _interpreter_throwout_count; }
192   int size_of_parameters() const                 { check_is_loaded(); return _size_of_parameters; }
193 
194   // Code size for inlining decisions.
195   int code_size_for_inlining();
196 
197   bool caller_sensitive()       const { return get_Method()->caller_sensitive();       }
198   bool force_inline()           const { return get_Method()->force_inline();           }
199   bool dont_inline()            const { return get_Method()->dont_inline();            }
200   bool intrinsic_candidate()    const { return get_Method()->intrinsic_candidate();    }
201   bool is_static_initializer()  const { return get_Method()->is_static_initializer();  }
202   bool changes_current_thread() const { return get_Method()->changes_current_thread(); }
203   bool deprecated()             const { return is_loaded() && get_Method()->deprecated(); }
204 
205   bool check_intrinsic_candidate() const {
206     if (intrinsic_id() == vmIntrinsics::_blackhole) {
207       // This is the intrinsic without an associated method, so no intrinsic_candidate
208       // flag is set. The intrinsic is still correct.
209       return true;
210     }
211     return (CheckIntrinsics ? intrinsic_candidate() : true);
212   }
213 
214   int highest_osr_comp_level();
215 
216   Bytecodes::Code java_code_at_bci(int bci) {
217     address bcp = code() + bci;
218     return Bytecodes::java_code_at(nullptr, bcp);
219   }
220   Bytecodes::Code raw_code_at_bci(int bci) {
221     address bcp = code() + bci;
222     return Bytecodes::code_at(nullptr, bcp);
223   }
224   BCEscapeAnalyzer  *get_bcea();
225   ciMethodBlocks    *get_method_blocks();
226 
227   bool    has_linenumber_table() const;          // length unknown until decompression
228 
229   int line_number_from_bci(int bci) const;
230 
231   // Runtime information.
232   int           vtable_index();
233 
234   // Analysis and profiling.
235   //
236   // Usage note: liveness_at_bci and init_vars should be wrapped in ResourceMarks.
237   bool          has_monitor_bytecodes() const    { return _uses_monitors; }
238   bool          has_balanced_monitors();
239 
240   // Returns a bitmap indicating which locals are required to be
241   // maintained as live for deopt.  raw_liveness_at_bci is always the
242   // direct output of the liveness computation while liveness_at_bci
243   // may mark all locals as live to improve support for debugging Java
244   // code by maintaining the state of as many locals as possible.
245   MethodLivenessResult raw_liveness_at_bci(int bci);
246   MethodLivenessResult liveness_at_bci(int bci);
247 
248   // Get the interpreters viewpoint on oop liveness.  MethodLiveness is
249   // conservative in the sense that it may consider locals to be live which
250   // cannot be live, like in the case where a local could contain an oop or
251   // a primitive along different paths.  In that case the local must be
252   // dead when those paths merge. Since the interpreter's viewpoint is
253   // used when gc'ing an interpreter frame we need to use its viewpoint
254   // during OSR when loading the locals.
255 
256   ResourceBitMap live_local_oops_at_bci(int bci);
257 
258   bool needs_clinit_barrier() const;
259 
260 #ifdef COMPILER1
261   const BitMap& bci_block_start();
262 #endif
263 
264   ciTypeFlow*   get_flow_analysis();
265   ciTypeFlow*   get_osr_flow_analysis(int osr_bci);  // alternate entry point
266   ciCallProfile call_profile_at_bci(int bci);
267 
268   // Does type profiling provide any useful information at this point?
269   bool          argument_profiled_type(int bci, int i, ciKlass*& type, ProfilePtrKind& ptr_kind);
270   bool          parameter_profiled_type(int i, ciKlass*& type, ProfilePtrKind& ptr_kind);
271   bool          return_profiled_type(int bci, ciKlass*& type, ProfilePtrKind& ptr_kind);
272 
273   ciField*      get_field_at_bci( int bci, bool &will_link);
274   ciMethod*     get_method_at_bci(int bci, bool &will_link, ciSignature* *declared_signature);
275   ciMethod*     get_method_at_bci(int bci) {
276     bool ignored_will_link;
277     ciSignature* ignored_declared_signature;
278     return get_method_at_bci(bci, ignored_will_link, &ignored_declared_signature);
279   }
280 
281   ciKlass*      get_declared_method_holder_at_bci(int bci);
282 
283   ciSignature*  get_declared_signature_at_bci(int bci) {
284     bool ignored_will_link;
285     ciSignature* declared_signature;
286     get_method_at_bci(bci, ignored_will_link, &declared_signature);
287     assert(declared_signature != nullptr, "cannot be null");
288     return declared_signature;
289   }
290 
291   // Given a certain calling environment, find the monomorphic target
292   // for the call.  Return null if the call is not monomorphic in
293   // its calling environment.
294   ciMethod* find_monomorphic_target(ciInstanceKlass* caller,
295                                     ciInstanceKlass* callee_holder,
296                                     ciInstanceKlass* actual_receiver,
297                                     bool check_access = true);
298 
299   // Given a known receiver klass, find the target for the call.
300   // Return null if the call has no target or is abstract.
301   ciMethod* resolve_invoke(ciKlass* caller, ciKlass* exact_receiver, bool check_access = true, bool allow_abstract = false);
302 
303   // Find the proper vtable index to invoke this method.
304   int resolve_vtable_index(ciKlass* caller, ciKlass* receiver);
305 
306   bool has_option(CompileCommandEnum option);
307   bool has_option_value(CompileCommandEnum option, double& value);
308   bool can_be_compiled();
309   bool can_be_parsed() const { return _can_be_parsed; }
310   bool has_compiled_code();
311   void log_nmethod_identity(xmlStream* log);
312   bool is_not_reached(int bci);
313   bool was_executed_more_than(int times);
314   bool has_unloaded_classes_in_signature();
315   bool is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const;
316   bool check_call(int refinfo_index, bool is_static) const;
317   bool ensure_method_data();  // make sure it exists in the VM also
318   MethodCounters* ensure_method_counters();
319 
320   int inline_instructions_size();
321   int scale_count(int count, float prof_factor = 1.);  // make MDO count commensurate with IIC
322 
323   // Stack walking support
324   bool is_ignored_by_security_stack_walk() const;
325 
326   // JSR 292 support
327   bool is_method_handle_intrinsic()  const;
328   bool is_compiled_lambda_form() const;
329   bool has_member_arg() const;
330 
331   // What kind of ciObject is this?
332   bool is_method() const                         { return true; }
333 
334   // Java access flags
335   bool is_public      () const                   { return flags().is_public(); }
336   bool is_private     () const                   { return flags().is_private(); }
337   bool is_protected   () const                   { return flags().is_protected(); }
338   bool is_static      () const                   { return flags().is_static(); }
339   bool is_final       () const                   { return flags().is_final(); }
340   bool is_synchronized() const                   { return flags().is_synchronized(); }
341   bool is_native      () const                   { return flags().is_native(); }
342   bool is_interface   () const                   { return flags().is_interface(); }
343   bool is_abstract    () const                   { return flags().is_abstract(); }
344 
345   // Other flags
346   bool is_final_method() const                   { return is_final() || holder()->is_final(); }
347   bool is_default_method() const                 { return !is_abstract() && !is_private() &&
348                                                           holder()->is_interface(); }
349   bool is_overpass    () const                   { check_is_loaded(); return _is_overpass; }
350   bool has_loops      () const;
351   bool has_jsrs       () const;
352   bool is_getter      () const;
353   bool is_setter      () const;
354   bool is_accessor    () const;
355   bool is_initializer () const;
356   bool is_empty       () const;
357   bool can_be_statically_bound() const           { return _can_be_statically_bound; }
358   bool has_reserved_stack_access() const         { return _has_reserved_stack_access; }
359   bool is_boxing_method() const;
360   bool is_unboxing_method() const;
361   bool is_vector_method() const;
362   bool is_object_initializer() const;
363 
364   bool can_be_statically_bound(ciInstanceKlass* context) const;
365 
366   bool can_omit_stack_trace() const;
367 
368   // Replay data methods
369   static void dump_name_as_ascii(outputStream* st, Method* method);
370   void dump_name_as_ascii(outputStream* st);
371   void dump_replay_data(outputStream* st);
372 
373   // Print the bytecodes of this method.
374   void print_codes_on(outputStream* st);
375   void print_codes() {
376     print_codes_on(tty);
377   }
378   void print_codes_on(int from, int to, outputStream* st);
379 
380   // Print the name of this method in various incarnations.
381   void print_name(outputStream* st = tty);
382   void print_short_name(outputStream* st = tty);
383 
384   static bool is_consistent_info(ciMethod* declared_method, ciMethod* resolved_method);
385 };
386 
387 #endif // SHARE_CI_CIMETHOD_HPP