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 
  30 typedef void (*initializer)(void);
  31 
  32 #if INCLUDE_JVMCI
  33 // Per-compiler statistics
  34 class CompilerStatistics VALUE_OBJ_CLASS_SPEC {
  35   friend class VMStructs;
  36 
  37   class Data VALUE_OBJ_CLASS_SPEC {
  38     friend class VMStructs;
  39   public:
  40     elapsedTimer _time;  // time spent compiling
  41     int _bytes;          // number of bytecodes compiled, including inlined bytecodes
  42     int _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   int _nmethods_size; //
  58   int _nmethods_code_size;
  59   int bytes_per_second() {
  60     int bytes = _standard._bytes + _osr._bytes;
  61     if (bytes == 0) {
  62       return 0;
  63     }
  64     double seconds = _standard._time.seconds() + _osr._time.seconds();
  65     return seconds == 0.0 ? 0 : (int) (bytes / seconds);
  66   }
  67   CompilerStatistics() : _nmethods_size(0), _nmethods_code_size(0) {}
  68 };
  69 #endif // INCLUDE_JVMCI
  70 
  71 class AbstractCompiler : public CHeapObj<mtCompiler> {
  72  private:
  73   volatile int _num_compiler_threads;
  74 
  75  protected:
  76   volatile int _compiler_state;
  77   // Used for tracking global state of compiler runtime initialization
  78   enum { uninitialized, initializing, initialized, failed, shut_down };
  79 
  80   // This method returns true for the first compiler thread that reaches that methods.
  81   // This thread will initialize the compiler runtime.
  82   bool should_perform_init();
  83 
  84   // The (closed set) of concrete compiler classes.
  85   enum Type {
  86     none,
  87     c1,
  88     c2,
  89     jvmci,
  90     shark
  91   };
  92 
  93  private:
  94   Type _type;
  95 
  96 #if INCLUDE_JVMCI
  97   CompilerStatistics _stats;
  98 #endif
  99 
 100  public:
 101   AbstractCompiler(Type type) : _type(type), _compiler_state(uninitialized), _num_compiler_threads(0) {}
 102 
 103   // This function determines the compiler thread that will perform the
 104   // shutdown of the corresponding compiler runtime.
 105   bool should_perform_shutdown();
 106 
 107   // Name of this compiler
 108   virtual const char* name() = 0;
 109 
 110   // Missing feature tests
 111   virtual bool supports_native()                 { return true; }
 112   virtual bool supports_osr   ()                 { return true; }
 113   virtual bool can_compile_method(methodHandle method)  { return true; }
 114 
 115   // Determine if the current compiler provides an intrinsic
 116   // for method 'method'. An intrinsic is available if:
 117   //  - the intrinsic is enabled (by using the appropriate command-line flag) and
 118   //  - the platform on which the VM is running supports the intrinsic
 119   //    (i.e., the platform provides the instructions necessary for the compiler
 120   //    to generate the intrinsic code).
 121   //
 122   // The second parameter, 'compilation_context', is needed to implement functionality
 123   // related to the DisableIntrinsic command-line flag. The DisableIntrinsic flag can
 124   // be used to prohibit the compilers to use an intrinsic. There are three ways to
 125   // disable an intrinsic using the DisableIntrinsic flag:
 126   //
 127   // (1) -XX:DisableIntrinsic=_hashCode,_getClass
 128   //     Disables intrinsification of _hashCode and _getClass globally
 129   //     (i.e., the intrinsified version the methods will not be used at all).
 130   // (2) -XX:CompileCommand=option,aClass::aMethod,ccstr,DisableIntrinsic,_hashCode
 131   //     Disables intrinsification of _hashCode if it is called from
 132   //     aClass::aMethod (but not for any other call site of _hashCode)
 133   // (3) -XX:CompileCommand=option,java.lang.ref.Reference::get,ccstr,DisableIntrinsic,_Reference_get
 134   //     Some methods are not compiled by C2. Instead, the C2 compiler
 135   //     returns directly the intrinsified version of these methods.
 136   //     The command above forces C2 to compile _Reference_get, but
 137   //     allows using the intrinsified version of _Reference_get at all
 138   //     other call sites.
 139   //
 140   // From the modes above, (1) disable intrinsics globally, (2) and (3)
 141   // disable intrinsics on a per-method basis. In cases (2) and (3) the
 142   // compilation context is aClass::aMethod and java.lang.ref.Reference::get,
 143   // respectively.
 144   virtual bool is_intrinsic_available(methodHandle method, methodHandle compilation_context) {
 145     return is_intrinsic_supported(method) &&
 146            !vmIntrinsics::is_disabled_by_flags(method, compilation_context);
 147   }
 148 
 149   // Determines if an intrinsic is supported by the compiler, that is,
 150   // the compiler provides the instructions necessary to generate
 151   // the intrinsic code for method 'method'.
 152   //
 153   // The 'is_intrinsic_supported' method is a white list, that is,
 154   // by default no intrinsics are supported by a compiler except
 155   // the ones listed in the method. Overriding methods should conform
 156   // to this behavior.
 157   virtual bool is_intrinsic_supported(methodHandle method) {
 158     return false;
 159   }
 160 
 161   // Compiler type queries.
 162   bool is_c1()                                   { return _type == c1; }
 163   bool is_c2()                                   { return _type == c2; }
 164   bool is_jvmci()                                { return _type == jvmci; }
 165   bool is_shark()                                { return _type == shark; }
 166 
 167   // Customization
 168   virtual void initialize () = 0;
 169 
 170   void set_num_compiler_threads(int num) { _num_compiler_threads = num;  }
 171   int num_compiler_threads()             { return _num_compiler_threads; }
 172 
 173   // Get/set state of compiler objects
 174   bool is_initialized()           { return _compiler_state == initialized; }
 175   bool is_failed     ()           { return _compiler_state == failed;}
 176   void set_state     (int state);
 177   void set_shut_down ()           { set_state(shut_down); }
 178   // Compilation entry point for methods
 179   virtual void compile_method(ciEnv* env, ciMethod* target, int entry_bci) {
 180     ShouldNotReachHere();
 181   }
 182 
 183 
 184   // Print compilation timers and statistics
 185   virtual void print_timers() {
 186     ShouldNotReachHere();
 187   }
 188 
 189 #if INCLUDE_JVMCI
 190   CompilerStatistics* stats() { return &_stats; }
 191 #endif
 192 };
 193 
 194 #endif // SHARE_VM_COMPILER_ABSTRACTCOMPILER_HPP