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