1 /*
  2  * Copyright (c) 1999, 2023, 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_COMPILER_ABSTRACTCOMPILER_HPP
 26 #define SHARE_COMPILER_ABSTRACTCOMPILER_HPP
 27 
 28 #include "ci/compilerInterface.hpp"
 29 #include "compiler/compilerDefinitions.hpp"
 30 #include "compiler/compilerDirectives.hpp"
 31 
 32 typedef void (*initializer)(void);
 33 
 34 // Per-compiler statistics
 35 class CompilerStatistics {
 36  public:
 37   class Data {
 38     friend class VMStructs;
 39   public:
 40     elapsedTimer _time;  // time spent compiling
 41     uint _bytes;         // number of bytecodes compiled, including inlined bytecodes
 42     uint _count;         // number of compilations
 43     Data() : _bytes(0), _count(0) {}
 44     void update(elapsedTimer time, int bytes) {
 45       _time.add(&time);
 46       _bytes += bytes;
 47       _count++;
 48     }
 49     void reset() {
 50       _time.reset();
 51     }
 52   };
 53 
 54  public:
 55   Data _standard;  // stats for non-OSR compilations
 56   Data _osr;       // stats for OSR compilations
 57   Data _bailout;
 58   Data _invalidated;
 59   Data _made_not_entrant;
 60   uint _nmethods_size; //
 61   uint _nmethods_code_size;
 62 
 63   double total_time() { return _standard._time.seconds() + _osr._time.seconds(); }
 64 
 65   double bytes_per_second() {
 66     uint bytes = _standard._bytes + _osr._bytes;
 67     if (bytes == 0) {
 68       return 0.0;
 69     }
 70     double seconds = total_time();
 71     return seconds == 0.0 ? 0.0 : (bytes / seconds);
 72   }
 73 
 74   CompilerStatistics() : _nmethods_size(0), _nmethods_code_size(0) {}
 75 };
 76 
 77 class AbstractCompiler : public CHeapObj<mtCompiler> {
 78  private:
 79   volatile int _num_compiler_threads;
 80 
 81  protected:
 82   volatile int _compiler_state;
 83   // Used for tracking global state of compiler runtime initialization
 84   enum { uninitialized, initializing, initialized, failed, shut_down };
 85 
 86   // This method returns true for the first compiler thread that reaches that methods.
 87   // This thread will initialize the compiler runtime.
 88   bool should_perform_init();
 89 
 90  private:
 91   const CompilerType _type;
 92 
 93   CompilerStatistics _stats;
 94 
 95  public:
 96   AbstractCompiler(CompilerType type) : _num_compiler_threads(0), _compiler_state(uninitialized), _type(type) {}
 97 
 98   // This function determines the compiler thread that will perform the
 99   // shutdown of the corresponding compiler runtime.
100   bool should_perform_shutdown();
101 
102   // Name of this compiler
103   virtual const char* name() = 0;
104 
105   // Determine if the current compiler provides an intrinsic
106   // for method 'method'. An intrinsic is available if:
107   //  - the intrinsic is enabled (by using the appropriate command-line flag,
108   //    the command-line compile ommand, or a compiler directive)
109   //  - the platform on which the VM is running supports the intrinsic
110   //    (i.e., the platform provides the instructions necessary for the compiler
111   //    to generate the intrinsic code).
112   //
113   // The directive provides the compilation context and includes pre-evaluated values
114   // dependent on VM flags, compile commands, and compiler directives.
115   //
116   // Usually, the compilation context is the caller of the method 'method'.
117   // The only case when for a non-recursive method 'method' the compilation context
118   // is not the caller of the 'method' (but it is the method itself) is
119   // java.lang.ref.Reference::get.
120   // For java.lang.ref.Reference::get, the intrinsic version is used
121   // instead of the compiled version so that the value in the referent
122   // field can be registered by the G1 pre-barrier code. The intrinsified
123   // version of Reference::get also adds a memory barrier to prevent
124   // commoning reads from the referent field across safepoint since GC
125   // can change the referent field's value. See Compile::Compile()
126   // in src/share/vm/opto/compile.cpp or
127   // GraphBuilder::GraphBuilder() in src/share/vm/c1/c1_GraphBuilder.cpp
128   // for more details.
129 
130   bool is_intrinsic_available(const methodHandle& method, DirectiveSet* directive) {
131     vmIntrinsics::ID id = method->intrinsic_id();
132     assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
133     return is_intrinsic_supported(method) &&
134            vmIntrinsics::is_intrinsic_available(id) &&
135            !directive->is_intrinsic_disabled(id);
136   }
137 
138   // Determines if an intrinsic is supported by the compiler, that is,
139   // the compiler provides the instructions necessary to generate
140   // the intrinsic code for method 'method'.
141   //
142   // The 'is_intrinsic_supported' method is an allow-list, that is,
143   // by default no intrinsics are supported by a compiler except
144   // the ones listed in the method. Overriding methods should conform
145   // to this behavior.
146   virtual bool is_intrinsic_supported(const methodHandle& method) {
147     return false;
148   }
149 
150   // Compiler type queries.
151   bool is_c1() const                     { return _type == compiler_c1; }
152   bool is_c2() const                     { return _type == compiler_c2; }
153   bool is_jvmci() const                  { return _type == compiler_jvmci; }
154   CompilerType type() const              { return _type; }
155 
156   // Compiler threads are hidden by default.
157   virtual bool is_hidden_from_external_view() const { return true; }
158 
159   // Customization
160   virtual void initialize () = 0;
161 
162   void set_num_compiler_threads(int num) { _num_compiler_threads = num;  }
163   int num_compiler_threads()             { return _num_compiler_threads; }
164 
165   // Get/set state of compiler objects
166   bool is_initialized()           { return _compiler_state == initialized; }
167   bool is_failed     ()           { return _compiler_state == failed;}
168   void set_state     (int state);
169   void set_shut_down ()           { set_state(shut_down); }
170   // Compilation entry point for methods
171   virtual void compile_method(ciEnv* env, ciMethod* target, int entry_bci, bool install_code, DirectiveSet* directive) {
172     ShouldNotReachHere();
173   }
174 
175   // Notifies this compiler that the current thread (`current`) is about to stop.
176   // The current thread currently holds the CompileThread_lock.
177   virtual void stopping_compiler_thread(CompilerThread* current) {
178     // Do nothing
179   }
180 
181   // Notifies this compiler that queue is empty just prior to waiting on
182   // MethodCompileQueue_lock which is held by the current thread (`thread`).
183   virtual void on_empty_queue(CompileQueue* queue, CompilerThread* thread) {
184     // Do nothing
185   }
186 
187   // Print compilation timers and statistics
188   virtual void print_timers() {
189     ShouldNotReachHere();
190   }
191 
192   CompilerStatistics* stats() { return &_stats; }
193 };
194 
195 #endif // SHARE_COMPILER_ABSTRACTCOMPILER_HPP