1 /*
  2  * Copyright (c) 2016, 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_COMPILERDEFINITIONS_HPP
 26 #define SHARE_COMPILER_COMPILERDEFINITIONS_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "utilities/globalDefinitions.hpp"
 30 
 31 // The (closed set) of concrete compiler classes.
 32 enum CompilerType : u1 {
 33   compiler_none,
 34   compiler_c1,
 35   compiler_c2,
 36   compiler_jvmci,
 37   compiler_number_of_types
 38 };
 39 
 40 extern const char* compilertype2name_tab[compiler_number_of_types];     // Map CompilerType to its name
 41 inline const char* compilertype2name(CompilerType t) { return (uint)t < compiler_number_of_types ? compilertype2name_tab[t] : "invalid"; }
 42 
 43 // Handy constants for deciding which compiler mode to use.
 44 enum MethodCompilation {
 45   InvocationEntryBci   = -1,     // i.e., not a on-stack replacement compilation
 46   BeforeBci            = InvocationEntryBci,
 47   AfterBci             = -2,
 48   UnwindBci            = -3,
 49   AfterExceptionBci    = -4,
 50   UnknownBci           = -5,
 51   InvalidFrameStateBci = -6
 52 };
 53 
 54 // Enumeration to distinguish tiers of compilation
 55 enum CompLevel : s1 {
 56   CompLevel_any               = -1,        // Used for querying the state
 57   CompLevel_all               = -1,        // Used for changing the state
 58   CompLevel_none              = 0,         // Interpreter
 59   CompLevel_simple            = 1,         // C1
 60   CompLevel_limited_profile   = 2,         // C1, invocation & backedge counters
 61   CompLevel_full_profile      = 3,         // C1, invocation & backedge counters + mdo
 62   CompLevel_full_optimization = 4,         // C2 or JVMCI
 63   CompLevel_count             = 4
 64 };
 65 
 66 class CompilationModeFlag : AllStatic {
 67   enum class Mode {
 68     NORMAL,
 69     QUICK_ONLY,
 70     HIGH_ONLY,
 71     HIGH_ONLY_QUICK_INTERNAL
 72   };
 73   static Mode _mode;
 74   static void print_error();
 75 public:
 76   static bool initialize();
 77   static bool normal()                   { return _mode == Mode::NORMAL;                   }
 78   static bool quick_only()               { return _mode == Mode::QUICK_ONLY;               }
 79   static bool high_only()                { return _mode == Mode::HIGH_ONLY;                }
 80   static bool high_only_quick_internal() { return _mode == Mode::HIGH_ONLY_QUICK_INTERNAL; }
 81 
 82   static bool disable_intermediate()     { return high_only() || high_only_quick_internal(); }
 83   static bool quick_internal()           { return !high_only(); }
 84 
 85   static void set_high_only_quick_internal() { _mode = Mode::HIGH_ONLY_QUICK_INTERNAL; }
 86   static void set_quick_only()               { _mode = Mode::QUICK_ONLY;               }
 87   static void set_high_only()                { _mode = Mode::HIGH_ONLY;                }
 88 };
 89 
 90 inline bool is_c1_compile(int comp_level) {
 91   return comp_level > CompLevel_none && comp_level < CompLevel_full_optimization;
 92 }
 93 
 94 inline bool is_c2_compile(int comp_level) {
 95   return comp_level == CompLevel_full_optimization;
 96 }
 97 
 98 inline bool is_compile(int comp_level) {
 99   return is_c1_compile(comp_level) || is_c2_compile(comp_level);
100 }
101 
102 
103 // States of Restricted Transactional Memory usage.
104 enum RTMState {
105   NoRTM      = 0x2, // Don't use RTM
106   UseRTM     = 0x1, // Use RTM
107   ProfileRTM = 0x0  // Use RTM with abort ratio calculation
108 };
109 
110 #ifndef INCLUDE_RTM_OPT
111 #define INCLUDE_RTM_OPT 0
112 #endif
113 #if INCLUDE_RTM_OPT
114 #define RTM_OPT_ONLY(code) code
115 #else
116 #define RTM_OPT_ONLY(code)
117 #endif
118 
119 class CompilerConfig : public AllStatic {
120 public:
121   // Scale compile thresholds
122   // Returns threshold scaled with CompileThresholdScaling
123   static intx scaled_compile_threshold(intx threshold, double scale);
124   static intx scaled_compile_threshold(intx threshold);
125   static intx jvmflag_scaled_compile_threshold(intx threshold);
126 
127   // Returns freq_log scaled with CompileThresholdScaling
128   static intx scaled_freq_log(intx freq_log, double scale);
129   static intx scaled_freq_log(intx freq_log);
130   static intx jvmflag_scaled_freq_log(intx freq_log);
131 
132   static bool check_args_consistency(bool status);
133 
134   static void ergo_initialize();
135 
136   // Which compilers are baked in?
137   constexpr static bool has_c1()     { return COMPILER1_PRESENT(true) NOT_COMPILER1(false); }
138   constexpr static bool has_c2()     { return COMPILER2_PRESENT(true) NOT_COMPILER2(false); }
139   constexpr static bool has_jvmci()  { return JVMCI_ONLY(true) NOT_JVMCI(false);            }
140   constexpr static bool has_tiered() { return has_c1() && (has_c2() || has_jvmci());        }
141 
142   inline static bool is_jvmci_compiler();
143   inline static bool is_jvmci();
144   inline static bool is_interpreter_only();
145 
146   // is_*_only() functions describe situations in which the JVM is in one way or another
147   // forced to use a particular compiler or their combination. The constraint functions
148   // deliberately ignore the fact that there may also be methods installed
149   // through JVMCI (where the JVMCI compiler was invoked not through the broker). Be sure
150   // to check for those (using is_jvmci()) in situations where it matters.
151 
152   inline static bool is_tiered();
153 
154   inline static bool is_c1_enabled();
155   inline static bool is_c1_only();
156   inline static bool is_c1_simple_only();
157   inline static bool is_c1_or_interpreter_only_no_jvmci();
158   inline static bool is_c1_only_no_jvmci();
159   inline static bool is_c1_profiling();
160 
161   inline static bool is_jvmci_compiler_enabled();
162   inline static bool is_jvmci_compiler_only();
163 
164   inline static bool is_c2_only();
165   inline static bool is_c2_enabled();
166   inline static bool is_c2_or_jvmci_compiler_only();
167   inline static bool is_c2_or_jvmci_compiler_enabled();
168 
169 private:
170   static bool is_compilation_mode_selected();
171   static void set_compilation_policy_flags();
172   static void set_jvmci_specific_flags();
173   static void set_legacy_emulation_flags();
174   static void set_client_emulation_mode_flags();
175 };
176 
177 #endif // SHARE_COMPILER_COMPILERDEFINITIONS_HPP