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