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