1 /*
  2  * Copyright (c) 1998, 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_COMPILERORACLE_HPP
 26 #define SHARE_COMPILER_COMPILERORACLE_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "utilities/istream.hpp"
 31 
 32 class methodHandle;
 33 
 34 
 35 // CompilerOracle is an interface for turning on and off compilation
 36 // for some methods
 37 
 38 //       OPTION_TYPES: type, name
 39 #define OPTION_TYPES(type) \
 40   type(Intx, "intx") \
 41   type(Uintx, "uintx") \
 42   type(Bool, "bool") \
 43   type(Ccstr, "ccstr") \
 44   type(Ccstrlist, "ccstrlist") \
 45   type(Double, "double")
 46 
 47 //       COMPILECOMMAND_OPTIONS: option, name, variant, type
 48 #define COMPILECOMMAND_OPTIONS(option) \
 49   option(Help,  "help",  Unknown) \
 50   option(Quiet, "quiet", Unknown) \
 51   option(Log, "log", Bool) \
 52   option(Print, "print", Bool) \
 53   option(Inline,  "inline", Bool) \
 54   option(DontInline,  "dontinline", Bool) \
 55   option(Blackhole,  "blackhole", Bool) \
 56   option(CompileOnly, "compileonly", Bool)\
 57   option(Exclude, "exclude", Bool) \
 58   option(Break, "break", Bool) \
 59   option(BreakAtExecute, "BreakAtExecute", Bool) \
 60   option(BreakAtCompile, "BreakAtCompile", Bool) \
 61   option(MemLimit, "MemLimit", Intx) \
 62   option(MemStat, "MemStat", Uintx) \
 63   option(PrintAssembly, "PrintAssembly", Bool) \
 64   option(PrintCompilation, "PrintCompilation", Bool) \
 65   option(PrintInlining, "PrintInlining", Bool) \
 66   option(PrintIntrinsics, "PrintIntrinsics", Bool) \
 67   option(PrintNMethods, "PrintNMethods", Bool)   \
 68   option(PrintOptoAssembly, "PrintOptoAssembly", Bool) \
 69   option(PrintDebugInfo,    "PrintDebugInfo",    Bool) \
 70   option(PrintRelocations,  "PrintRelocations",  Bool) \
 71   option(PrintDependencies, "PrintDependencies", Bool) \
 72   option(BackgroundCompilation, "BackgroundCompilation", Bool) \
 73   option(RepeatCompilation, "RepeatCompilation", Intx) \
 74   option(ReplayInline,   "ReplayInline", Bool) \
 75   option(DumpReplay,     "DumpReplay", Bool) \
 76   option(DumpInline,     "DumpInline", Bool) \
 77   option(CompileThresholdScaling, "CompileThresholdScaling", Double) \
 78   option(ControlIntrinsic,  "ControlIntrinsic",  Ccstrlist) \
 79   option(DisableIntrinsic,  "DisableIntrinsic",  Ccstrlist) \
 80   option(NoRTMLockEliding,  "NoRTMLockEliding",  Bool) \
 81   option(UseRTMLockEliding, "UseRTMLockEliding", Bool) \
 82   option(BlockLayoutByFrequency, "BlockLayoutByFrequency", Bool) \
 83   option(TraceOptoPipelining, "TraceOptoPipelining", Bool) \
 84   option(TraceOptoOutput, "TraceOptoOutput", Bool) \
 85   option(TraceSpilling, "TraceSpilling", Bool) \
 86 NOT_PRODUCT(option(TraceEscapeAnalysis, "TraceEscapeAnalysis", Bool)) \
 87 NOT_PRODUCT(option(PrintIdeal, "PrintIdeal", Bool))  \
 88 NOT_PRODUCT(option(PrintIdealPhase, "PrintIdealPhase", Ccstrlist)) \
 89 NOT_PRODUCT(option(IGVPrintLevel, "IGVPrintLevel", Intx)) \
 90 NOT_PRODUCT(option(TraceAutoVectorization, "TraceAutoVectorization", Ccstrlist)) \
 91   option(Vectorize, "Vectorize", Bool) \
 92   option(CloneMapDebug, "CloneMapDebug", Bool) \
 93   option(IncrementalInlineForceCleanup, "IncrementalInlineForceCleanup", Bool) \
 94   option(MaxNodeLimit, "MaxNodeLimit", Intx)  \
 95   option(PrecompileRecorded, "PrecompileRecorded", Uintx) \
 96   option(DontPrecompile, "DontPrecompile", Bool) \
 97   option(DontPreload, "DontPreload", Bool) \
 98   option(IgnorePrecompiled, "IgnorePrecompiled", Bool) \
 99   option(IgnoreRecordedProfile, "IgnoreRecordedProfile", Bool) \
100   option(TooManyTrapsAtBCI, "TooManyTrapsAtBCI", Ccstrlist) \
101 NOT_PRODUCT(option(TestOptionInt,    "TestOptionInt",    Intx)) \
102 NOT_PRODUCT(option(TestOptionUint,   "TestOptionUint",   Uintx)) \
103 NOT_PRODUCT(option(TestOptionBool,   "TestOptionBool",   Bool)) \
104 NOT_PRODUCT(option(TestOptionBool2,  "TestOptionBool2",  Bool)) \
105 NOT_PRODUCT(option(TestOptionStr,    "TestOptionStr",    Ccstr)) \
106 NOT_PRODUCT(option(TestOptionList,   "TestOptionList",   Ccstrlist)) \
107 NOT_PRODUCT(option(TestOptionDouble, "TestOptionDouble", Double)) \
108   option(Option, "option", Unknown) \
109   option(Unknown, "unknown", Unknown)
110 
111 enum class CompileCommandEnum : int {
112   #define enum_of_options(option, name, ctype) option,
113     COMPILECOMMAND_OPTIONS(enum_of_options)
114   #undef enum_of_options
115   Count
116 };
117 
118 enum class OptionType {
119 #define enum_of_types(type, name) type,
120     OPTION_TYPES(enum_of_types)
121 #undef enum_of_types
122     Unknown
123 };
124 
125 enum class MemStatAction {
126   collect = 1, print = 2
127 };
128 
129 class CompilerOracle : AllStatic {
130  public:
131   typedef bool parse_from_line_fn_t(char*);
132 
133  private:
134   static bool _quiet;
135   static void print_parse_error(char* error_msg, char* original_line);
136   static void print_command(CompileCommandEnum option, const char* name, enum OptionType type);
137 
138   // The core parser.
139   static bool parse_from_input(inputStream::Input* input,
140                                parse_from_line_fn_t* parse_from_line);
141 
142  public:
143   // True if the command file has been specified or is implicit
144   static bool has_command_file();
145 
146   // Reads from file and adds to lists
147   static bool parse_from_file();
148 
149   // Tells whether we to exclude compilation of method
150   static bool should_exclude(const methodHandle& method);
151   static bool be_quiet() { return _quiet; }
152 
153   // Tells whether we want to inline this method
154   static bool should_inline(const methodHandle& method);
155 
156   // Tells whether we want to disallow inlining of this method
157   static bool should_not_inline(const methodHandle& method);
158 
159   // Tells whether this method changes Thread.currentThread()
160   static bool changes_current_thread(const methodHandle& method);
161 
162   // Tells whether we should print the assembly for this method
163   static bool should_print(const methodHandle& method);
164 
165   // Tells whether we should log the compilation data for this method
166   static bool should_log(const methodHandle& method);
167 
168   // Tells whether to break when compiling method
169   static bool should_break_at(const methodHandle& method);
170 
171   // Tells whether there are any methods to print for print_method_statistics()
172   static bool should_print_methods();
173 
174   // Tells whether there are any methods to (collect|collect+print) memory statistics for
175   static bool should_collect_memstat();
176   static bool should_print_final_memstat_report();
177 
178   // Tags the method as blackhole candidate, if possible.
179   static void tag_blackhole_if_possible(const methodHandle& method);
180 
181   // A wrapper for checking bool options
182   static bool has_option(const methodHandle& method, CompileCommandEnum option);
183 
184   // Check if method has option and value set. If yes, overwrite value and return true,
185   // otherwise leave value unchanged and return false.
186   template<typename T>
187   static bool has_option_value(const methodHandle& method, CompileCommandEnum option, T& value);
188 
189   // This check is currently only needed by whitebox API
190   template<typename T>
191   static bool option_matches_type(CompileCommandEnum option, T& value);
192 
193   // Reads from string instead of file
194   static bool parse_from_string(const char* option_string,
195                                 parse_from_line_fn_t* parser);
196   static bool parse_from_line(char* line);
197   static bool parse_from_line_quietly(char* line);
198   static bool parse_compile_only(char* line);
199 
200   // Fast check if there is any option set that compile control needs to know about
201   static bool has_any_command_set();
202 
203   // convert a string to a proper compilecommand option - used from whitebox.
204   // returns CompileCommandEnum::Unknown on names not matching an option.
205   static CompileCommandEnum string_to_option(const char* name);
206 
207   // convert a string to a proper compilecommand option
208   // returns CompileCommandEnum::Unknown if name is not an option.
209   static CompileCommandEnum parse_option_name(const char* name);
210 
211   // convert a string to a proper option type
212   // returns OptionType::Unknown on strings not matching an option type.
213   static enum OptionType parse_option_type(const char* type_str);
214 };
215 
216 #endif // SHARE_COMPILER_COMPILERORACLE_HPP