1 /*
  2  * Copyright (c) 2016, 2024, 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 };
 64 
 65 class CompilationModeFlag : AllStatic {
 66   enum class Mode {
 67     NORMAL,
 68     QUICK_ONLY,
 69     HIGH_ONLY,
 70     HIGH_ONLY_QUICK_INTERNAL
 71   };
 72   static Mode _mode;
 73   static void print_error();
 74 public:
 75   static bool initialize();
 76   static bool normal()                   { return _mode == Mode::NORMAL;                   }
 77   static bool quick_only()               { return _mode == Mode::QUICK_ONLY;               }
 78   static bool high_only()                { return _mode == Mode::HIGH_ONLY;                }
 79   static bool high_only_quick_internal() { return _mode == Mode::HIGH_ONLY_QUICK_INTERNAL; }
 80 
 81   static bool disable_intermediate()     { return high_only() || high_only_quick_internal(); }
 82   static bool quick_internal()           { return !high_only(); }
 83 
 84   static void set_high_only_quick_internal() { _mode = Mode::HIGH_ONLY_QUICK_INTERNAL; }
 85   static void set_quick_only()               { _mode = Mode::QUICK_ONLY;               }
 86   static void set_high_only()                { _mode = Mode::HIGH_ONLY;                }
 87 };
 88 
 89 inline bool is_c1_compile(int comp_level) {
 90   return comp_level > CompLevel_none && comp_level < CompLevel_full_optimization;
 91 }
 92 
 93 inline bool is_c2_compile(int comp_level) {
 94   return comp_level == CompLevel_full_optimization;
 95 }
 96 
 97 inline bool is_compile(int comp_level) {
 98   return is_c1_compile(comp_level) || is_c2_compile(comp_level);
 99 }
100 
101 
102 // States of Restricted Transactional Memory usage.
103 enum RTMState: u1 {
104   NoRTM      = 0x2, // Don't use RTM
105   UseRTM     = 0x1, // Use RTM
106   ProfileRTM = 0x0  // Use RTM with abort ratio calculation
107 };
108 
109 #ifndef INCLUDE_RTM_OPT
110 #define INCLUDE_RTM_OPT 0
111 #endif
112 #if INCLUDE_RTM_OPT
113 #define RTM_OPT_ONLY(code) code
114 #else
115 #define RTM_OPT_ONLY(code)
116 #endif
117 
118 class CompilerConfig : public AllStatic {
119 public:
120   // Scale compile thresholds
121   // Returns threshold scaled with CompileThresholdScaling
122   static intx scaled_compile_threshold(intx threshold, double scale);
123   static intx scaled_compile_threshold(intx threshold);
124   static intx jvmflag_scaled_compile_threshold(intx threshold);
125 
126   // Returns freq_log scaled with CompileThresholdScaling
127   static intx scaled_freq_log(intx freq_log, double scale);
128   static intx scaled_freq_log(intx freq_log);
129   static intx jvmflag_scaled_freq_log(intx freq_log);
130 
131   static bool check_args_consistency(bool status);
132 
133   static void ergo_initialize();
134 
135   // Which compilers are baked in?
136   constexpr static bool has_c1()     { return COMPILER1_PRESENT(true) NOT_COMPILER1(false); }
137   constexpr static bool has_c2()     { return COMPILER2_PRESENT(true) NOT_COMPILER2(false); }
138   constexpr static bool has_jvmci()  { return JVMCI_ONLY(true) NOT_JVMCI(false);            }
139   constexpr static bool has_tiered() { return has_c1() && (has_c2() || has_jvmci());        }
140 
141   inline static bool is_jvmci_compiler();
142   inline static bool is_jvmci();
143   inline static bool is_interpreter_only();
144 
145   // is_*_only() functions describe situations in which the JVM is in one way or another
146   // forced to use a particular compiler or their combination. The constraint functions
147   // deliberately ignore the fact that there may also be methods installed
148   // through JVMCI (where the JVMCI compiler was invoked not through the broker). Be sure
149   // to check for those (using is_jvmci()) in situations where it matters.
150 
151   inline static bool is_tiered();
152 
153   inline static bool is_c1_enabled();
154   inline static bool is_c1_only();
155   inline static bool is_c1_simple_only();
156   inline static bool is_c1_or_interpreter_only_no_jvmci();
157   inline static bool is_c1_only_no_jvmci();
158   inline static bool is_c1_profiling();
159 
160   inline static bool is_jvmci_compiler_enabled();
161   inline static bool is_jvmci_compiler_only();
162 
163   inline static bool is_c2_only();
164   inline static bool is_c2_enabled();
165   inline static bool is_c2_or_jvmci_compiler_only();
166   inline static bool is_c2_or_jvmci_compiler_enabled();
167 
168 private:
169   static bool is_compilation_mode_selected();
170   static void set_compilation_policy_flags();
171   static void set_jvmci_specific_flags();
172   static void set_legacy_emulation_flags();
173   static void set_client_emulation_mode_flags();
174 };
175 
176 #endif // SHARE_COMPILER_COMPILERDEFINITIONS_HPP