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