1 /*
   2  * Copyright (c) 2000, 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_VM_RUNTIME_COMPILATIONPOLICY_HPP
  26 #define SHARE_VM_RUNTIME_COMPILATIONPOLICY_HPP
  27 
  28 #include "code/nmethod.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "runtime/vmOperations.hpp"
  32 #include "utilities/growableArray.hpp"
  33 
  34 // The CompilationPolicy selects which method (if any) should be compiled.
  35 // It also decides which methods must always be compiled (i.e., are never
  36 // interpreted).
  37 class CompileTask;
  38 class CompileQueue;
  39 class RFrame;
  40 
  41 class CompilationPolicy : public CHeapObj<mtCompiler> {
  42   static CompilationPolicy* _policy;
  43   // Accumulated time
  44   static elapsedTimer       _accumulated_time;
  45 
  46   static bool               _in_vm_startup;
  47 
  48   // m must be compiled before executing it
  49   static bool must_be_compiled(const methodHandle& m, int comp_level = CompLevel_all);
  50 
  51 public:
  52   // If m must_be_compiled then request a compilation from the CompileBroker.
  53   // This supports the -Xcomp option.
  54   static void compile_if_required(const methodHandle& m, TRAPS);
  55 
  56   // m is allowed to be compiled
  57   static bool can_be_compiled(const methodHandle& m, int comp_level = CompLevel_all);
  58   // m is allowed to be osr compiled
  59   static bool can_be_osr_compiled(const methodHandle& m, int comp_level = CompLevel_all);
  60   static bool is_compilation_enabled();
  61   static void set_policy(CompilationPolicy* policy) { _policy = policy; }
  62   static CompilationPolicy* policy()                { return _policy; }
  63 
  64   static CompileTask* select_task_helper(CompileQueue* compile_queue);
  65 
  66   // Profiling
  67   elapsedTimer* accumulated_time() { return &_accumulated_time; }
  68   void print_time() PRODUCT_RETURN;
  69   // Return initial compile level that is used with Xcomp
  70   virtual CompLevel initial_compile_level() = 0;
  71   virtual int compiler_count(CompLevel comp_level) = 0;
  72   // main notification entry, return a pointer to an nmethod if the OSR is required,
  73   // returns NULL otherwise.
  74   virtual nmethod* event(const methodHandle& method, const methodHandle& inlinee, int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) = 0;
  75   // safepoint() is called at the end of the safepoint
  76   virtual void do_safepoint_work() = 0;
  77   // reprofile request
  78   virtual void reprofile(ScopeDesc* trap_scope, bool is_osr) = 0;
  79   // delay_compilation(method) can be called by any component of the runtime to notify the policy
  80   // that it's recommended to delay the compilation of this method.
  81   virtual void delay_compilation(Method* method) = 0;
  82   // disable_compilation() is called whenever the runtime decides to disable compilation of the
  83   // specified method.
  84   virtual void disable_compilation(Method* method) = 0;
  85   // Select task is called by CompileBroker. The queue is guaranteed to have at least one
  86   // element and is locked. The function should select one and return it.
  87   virtual CompileTask* select_task(CompileQueue* compile_queue) = 0;
  88   // Tell the runtime if we think a given method is adequately profiled.
  89   virtual bool is_mature(Method* method) = 0;
  90   // Do policy initialization
  91   virtual void initialize() = 0;
  92   virtual bool should_not_inline(ciEnv* env, ciMethod* method) { return false; }
  93 };
  94 
  95 // A base class for baseline policies.
  96 class NonTieredCompPolicy : public CompilationPolicy {
  97   int _compiler_count;
  98 protected:
  99   static void trace_frequency_counter_overflow(const methodHandle& m, int branch_bci, int bci);
 100   static void trace_osr_request(const methodHandle& method, nmethod* osr, int bci);
 101   static void trace_osr_completion(nmethod* osr_nm);
 102   void reset_counter_for_invocation_event(const methodHandle& method);
 103   void reset_counter_for_back_branch_event(const methodHandle& method);
 104 public:
 105   NonTieredCompPolicy() : _compiler_count(0) { }
 106   virtual CompLevel initial_compile_level() { return CompLevel_highest_tier; }
 107   virtual int compiler_count(CompLevel comp_level);
 108   virtual void do_safepoint_work();
 109   virtual void reprofile(ScopeDesc* trap_scope, bool is_osr);
 110   virtual void delay_compilation(Method* method);
 111   virtual void disable_compilation(Method* method);
 112   virtual bool is_mature(Method* method);
 113   virtual void initialize();
 114   virtual CompileTask* select_task(CompileQueue* compile_queue);
 115   virtual nmethod* event(const methodHandle& method, const methodHandle& inlinee, int branch_bci, int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread);
 116   virtual void method_invocation_event(const methodHandle& m, JavaThread* thread) = 0;
 117   virtual void method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) = 0;
 118 };
 119 
 120 class SimpleCompPolicy : public NonTieredCompPolicy {
 121  public:
 122   virtual void method_invocation_event(const methodHandle& m, JavaThread* thread);
 123   virtual void method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread);
 124 };
 125 
 126 // StackWalkCompPolicy - existing C2 policy
 127 
 128 #ifdef COMPILER2
 129 class StackWalkCompPolicy : public NonTieredCompPolicy {
 130  public:
 131   virtual void method_invocation_event(const methodHandle& m, JavaThread* thread);
 132   virtual void method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread);
 133 
 134  private:
 135   RFrame* findTopInlinableFrame(GrowableArray<RFrame*>* stack);
 136   RFrame* senderOf(RFrame* rf, GrowableArray<RFrame*>* stack);
 137 
 138   // the following variables hold values computed by the last inlining decision
 139   // they are used for performance debugging only (print better messages)
 140   static const char* _msg;            // reason for not inlining
 141 
 142   static const char* shouldInline   (const methodHandle& callee, float frequency, int cnt);
 143   // positive filter: should send be inlined?  returns NULL (--> yes) or rejection msg
 144   static const char* shouldNotInline(const methodHandle& callee);
 145   // negative filter: should send NOT be inlined?  returns NULL (--> inline) or rejection msg
 146 
 147 };
 148 #endif
 149 
 150 #endif // SHARE_VM_RUNTIME_COMPILATIONPOLICY_HPP