1 /* 2 * Copyright (c) 1998, 2025, 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_COMPILERDIRECTIVES_HPP 26 #define SHARE_COMPILER_COMPILERDIRECTIVES_HPP 27 28 #include "classfile/vmIntrinsics.hpp" 29 #include "ci/ciMethod.hpp" 30 #include "compiler/methodMatcher.hpp" 31 #include "opto/phasetype.hpp" 32 #include "utilities/bitMap.hpp" 33 #include "utilities/exceptions.hpp" 34 #include "utilities/tribool.hpp" 35 36 // Directives flag name, type, default value, compile command name 37 #define compilerdirectives_common_other_flags(cflags) \ 38 cflags(Enable, bool, false, Unknown) \ 39 cflags(Exclude, bool, false, Unknown) \ 40 cflags(BreakAtExecute, bool, false, BreakAtExecute) \ 41 cflags(BreakAtCompile, bool, false, BreakAtCompile) \ 42 cflags(Log, bool, LogCompilation, Unknown) \ 43 cflags(MemLimit, intx, 0, MemLimit) \ 44 cflags(MemStat, uintx, 0, MemStat) \ 45 cflags(PrintAssembly, bool, PrintAssembly, PrintAssembly) \ 46 cflags(PrintCompilation, bool, PrintCompilation, PrintCompilation) \ 47 cflags(PrintInlining, bool, PrintInlining, PrintInlining) \ 48 cflags(PrintNMethods, bool, PrintNMethods, PrintNMethods) \ 49 cflags(BackgroundCompilation, bool, BackgroundCompilation, BackgroundCompilation) \ 50 cflags(ReplayInline, bool, false, ReplayInline) \ 51 cflags(DumpReplay, bool, false, DumpReplay) \ 52 cflags(DumpInline, bool, false, DumpInline) \ 53 cflags(CompilerDirectivesIgnoreCompileCommands, bool, CompilerDirectivesIgnoreCompileCommands, Unknown) \ 54 cflags(RepeatCompilation, intx, RepeatCompilation, RepeatCompilation) \ 55 cflags(PrecompileRecorded, uintx, 0, PrecompileRecorded) \ 56 cflags(DontPrecompile, bool, false, DontPrecompile) \ 57 cflags(DontPreload, bool, false, DontPreload) \ 58 cflags(IgnoreRecordedProfile, bool, false, IgnoreRecordedProfile) \ 59 cflags(IgnorePrecompiled, bool, false, IgnorePrecompiled) \ 60 cflags(TooManyTrapsAtBCI, ccstrlist, "", TooManyTrapsAtBCI) 61 #define compilerdirectives_common_string_flags(cflags) \ 62 cflags(DisableIntrinsic, ccstrlist, DisableIntrinsic, DisableIntrinsic) \ 63 cflags(ControlIntrinsic, ccstrlist, ControlIntrinsic, ControlIntrinsic) 64 #define compilerdirectives_common_flags(cflags) \ 65 compilerdirectives_common_other_flags(cflags) \ 66 compilerdirectives_common_string_flags(cflags) 67 68 #ifdef COMPILER1 69 #define compilerdirectives_c1_other_flags(cflags) 70 #define compilerdirectives_c1_string_flags(cflags) 71 #else 72 #define compilerdirectives_c1_other_flags(cflags) 73 #define compilerdirectives_c1_string_flags(cflags) 74 #endif 75 76 #define compilerdirectives_c1_flags(cflags) \ 77 compilerdirectives_c1_other_flags(cflags) \ 78 compilerdirectives_c1_string_flags(cflags) 79 80 #ifdef COMPILER2 81 #define compilerdirectives_c2_other_flags(cflags) \ 82 cflags(BlockLayoutByFrequency, bool, BlockLayoutByFrequency, BlockLayoutByFrequency) \ 83 cflags(PrintOptoAssembly, bool, PrintOptoAssembly, PrintOptoAssembly) \ 84 cflags(PrintIntrinsics, bool, PrintIntrinsics, PrintIntrinsics) \ 85 NOT_PRODUCT(cflags(TraceOptoPipelining, bool, TraceOptoPipelining, TraceOptoPipelining)) \ 86 NOT_PRODUCT(cflags(TraceOptoOutput, bool, TraceOptoOutput, TraceOptoOutput)) \ 87 NOT_PRODUCT(cflags(TraceEscapeAnalysis, bool, false, TraceEscapeAnalysis)) \ 88 NOT_PRODUCT(cflags(PrintIdeal, bool, PrintIdeal, PrintIdeal)) \ 89 cflags(TraceSpilling, bool, TraceSpilling, TraceSpilling) \ 90 cflags(Vectorize, bool, false, Vectorize) \ 91 cflags(CloneMapDebug, bool, false, CloneMapDebug) \ 92 NOT_PRODUCT(cflags(IGVPrintLevel, intx, PrintIdealGraphLevel, IGVPrintLevel)) \ 93 cflags(IncrementalInlineForceCleanup, bool, IncrementalInlineForceCleanup, IncrementalInlineForceCleanup) \ 94 cflags(MaxNodeLimit, intx, MaxNodeLimit, MaxNodeLimit) 95 #define compilerdirectives_c2_string_flags(cflags) \ 96 NOT_PRODUCT(cflags(TraceAutoVectorization, ccstrlist, "", TraceAutoVectorization)) \ 97 NOT_PRODUCT(cflags(TraceMergeStores, ccstrlist, "", TraceMergeStores)) \ 98 NOT_PRODUCT(cflags(PrintIdealPhase, ccstrlist, "", PrintIdealPhase)) 99 #else 100 #define compilerdirectives_c2_other_flags(cflags) 101 #define compilerdirectives_c2_string_flags(cflags) 102 #endif 103 104 #define compilerdirectives_c2_flags(cflags) \ 105 compilerdirectives_c2_other_flags(cflags) \ 106 compilerdirectives_c2_string_flags(cflags) 107 108 class AbstractCompiler; 109 class CompilerDirectives; 110 class DirectiveSet; 111 112 class DirectivesStack : AllStatic { 113 private: 114 static CompilerDirectives* _top; 115 static CompilerDirectives* _bottom; 116 static int _depth; 117 118 static void pop_inner(); // no lock version of pop 119 public: 120 static void init(); 121 static DirectiveSet* getMatchingDirective(const methodHandle& mh, AbstractCompiler* comp); 122 static DirectiveSet* getDefaultDirective(AbstractCompiler* comp); 123 static void push(CompilerDirectives* directive); 124 static void pop(int count); 125 static bool check_capacity(int request_size, outputStream* st); 126 static void clear(); 127 static void print(outputStream* st); 128 static void release(DirectiveSet* set); 129 static void release(CompilerDirectives* dir); 130 }; 131 132 class DirectiveSet : public CHeapObj<mtCompiler> { 133 private: 134 InlineMatcher* _inlinematchers; 135 CompilerDirectives* _directive; 136 TriBoolArray<(size_t)vmIntrinsics::number_of_intrinsics(), int> _intrinsic_control_words; 137 CHeapBitMap _ideal_phase_name_set; 138 CHeapBitMap _trace_auto_vectorization_tags; 139 CHeapBitMap _trace_merge_stores_tags; 140 141 public: 142 DirectiveSet(CompilerDirectives* directive); 143 ~DirectiveSet(); 144 void init_control_intrinsic(); 145 CompilerDirectives* directive(); 146 bool parse_and_add_inline(char* str, const char*& error_msg); 147 void append_inline(InlineMatcher* m); 148 bool should_inline(ciMethod* inlinee); 149 bool should_not_inline(ciMethod* inlinee); 150 void print_inline(outputStream* st); 151 DirectiveSet* compilecommand_compatibility_init(const methodHandle& method); 152 bool is_exclusive_copy() { return _directive == nullptr; } 153 bool matches_inline(const methodHandle& method, int inline_action); 154 static DirectiveSet* clone(DirectiveSet const* src); 155 bool is_intrinsic_disabled(vmIntrinsicID id); 156 static ccstrlist canonicalize_control_intrinsic(ccstrlist option_value); 157 void finalize(outputStream* st); 158 bool is_c1(CompilerDirectives* directive) const; 159 bool is_c2(CompilerDirectives* directive) const; 160 bool should_collect_memstat() const; 161 bool should_print_memstat() const; 162 size_t mem_limit() const; 163 bool should_crash_at_mem_limit() const; // true: crash false: stop compilation 164 165 typedef enum { 166 #define enum_of_flags(name, type, dvalue, cc_flag) name##Index, 167 compilerdirectives_common_flags(enum_of_flags) 168 compilerdirectives_c2_flags(enum_of_flags) 169 compilerdirectives_c1_flags(enum_of_flags) 170 #undef enum_of_flags 171 number_of_flags 172 } flags; 173 174 private: 175 bool _modified[number_of_flags]; // Records what options where set by a directive 176 public: 177 #define flag_store_definition(name, type, dvalue, cc_flag) type name##Option; 178 compilerdirectives_common_flags(flag_store_definition) 179 compilerdirectives_c2_flags(flag_store_definition) 180 compilerdirectives_c1_flags(flag_store_definition) 181 #undef flag_store_definition 182 183 // Casting to get the same function signature for all setters. Used from parser. 184 #define set_function_definition(name, type, dvalue, cc_flag) void set_##name(void* value) { type val = *(type*)value; name##Option = val; _modified[name##Index] = true; } 185 compilerdirectives_common_other_flags(set_function_definition) 186 compilerdirectives_c2_other_flags(set_function_definition) 187 compilerdirectives_c1_other_flags(set_function_definition) 188 #undef set_function_definition 189 190 // Casting to get the same function signature for all setters. Used from parser. 191 // 192 // IMPORTANT: Takes ownership, will use os::free. Ensure the memory was dynamically allocated on the 193 // C heap. 194 #define set_string_function_definition(name, type, dvalue, cc_flag) \ 195 void set_##name(void* value) { \ 196 if (_modified[name##Index]) { \ 197 os::free(const_cast<char*>(name##Option)); \ 198 } \ 199 type val = *(type*)value; \ 200 name##Option = val; \ 201 _modified[name##Index] = true; \ 202 } 203 compilerdirectives_common_string_flags(set_string_function_definition) 204 compilerdirectives_c2_string_flags(set_string_function_definition) 205 compilerdirectives_c1_string_flags(set_string_function_definition) 206 #undef set_string_function_definition 207 208 void set_ideal_phase_name_set(const BitMap& set) { 209 _ideal_phase_name_set.set_from(set); 210 }; 211 bool should_print_phase(const CompilerPhaseType cpt) const { 212 return _ideal_phase_name_set.at(cpt); 213 }; 214 void set_trace_auto_vectorization_tags(const CHeapBitMap& tags) { 215 _trace_auto_vectorization_tags.set_from(tags); 216 }; 217 const CHeapBitMap& trace_auto_vectorization_tags() { 218 return _trace_auto_vectorization_tags; 219 }; 220 void set_trace_merge_stores_tags(const CHeapBitMap& tags) { 221 _trace_merge_stores_tags.set_from(tags); 222 }; 223 const CHeapBitMap& trace_merge_stores_tags() { 224 return _trace_merge_stores_tags; 225 }; 226 227 void print_intx(outputStream* st, ccstr n, intx v, bool mod) { if (mod) { st->print("%s:%zd ", n, v); } } 228 void print_uintx(outputStream* st, ccstr n, intx v, bool mod) { if (mod) { st->print("%s:%zu ", n, v); } } 229 void print_bool(outputStream* st, ccstr n, bool v, bool mod) { if (mod) { st->print("%s:%s ", n, v ? "true" : "false"); } } 230 void print_double(outputStream* st, ccstr n, double v, bool mod) { if (mod) { st->print("%s:%f ", n, v); } } 231 void print_ccstr(outputStream* st, ccstr n, ccstr v, bool mod) { if (mod) { st->print("%s:%s ", n, v); } } 232 void print_ccstrlist(outputStream* st, ccstr n, ccstr v, bool mod) { print_ccstr(st, n, v, mod); } 233 234 void print(outputStream* st) { 235 print_inline(st); 236 st->print(" "); 237 #define print_function_definition(name, type, dvalue, cc_flag) print_##type(st, #name, this->name##Option, true); 238 compilerdirectives_common_flags(print_function_definition) 239 compilerdirectives_c2_flags(print_function_definition) 240 compilerdirectives_c1_flags(print_function_definition) 241 #undef print_function_definition 242 st->cr(); 243 } 244 }; 245 246 // Iterator of ControlIntrinsic=+_id1,-_id2,+_id3,... 247 // 248 // If disable_all is set, it accepts DisableIntrinsic and all intrinsic Ids 249 // appear in the list are disabled. Arguments don't have +/- prefix. eg. 250 // DisableIntrinsic=_id1,_id2,_id3,... 251 class ControlIntrinsicIter { 252 private: 253 bool _enabled; 254 char* _token; 255 char* _saved_ptr; 256 char* _list; 257 const bool _disableIntrinsic; 258 void next_token(); 259 260 public: 261 ControlIntrinsicIter(ccstrlist option, bool disable_all = false); 262 ~ControlIntrinsicIter(); 263 264 bool is_enabled() const { return _enabled; } 265 const char* operator*() const { return _token; } 266 267 ControlIntrinsicIter& operator++(); 268 }; 269 270 class ControlIntrinsicValidator { 271 private: 272 bool _valid; 273 char* _bad; 274 275 public: 276 ControlIntrinsicValidator(ccstrlist option, bool disable_all) : _valid(true), _bad(nullptr) { 277 for (ControlIntrinsicIter iter(option, disable_all); *iter != nullptr && _valid; ++iter) { 278 if (vmIntrinsics::_none == vmIntrinsics::find_id(*iter)) { 279 const size_t len = MIN2<size_t>(strlen(*iter), 63) + 1; // cap len to a value we know is enough for all intrinsic names 280 _bad = NEW_C_HEAP_ARRAY(char, len, mtCompiler); 281 // strncpy always writes len characters. If the source string is shorter, the function fills the remaining bytes with nullptrs. 282 strncpy(_bad, *iter, len); 283 _valid = false; 284 } 285 } 286 } 287 288 ~ControlIntrinsicValidator() { 289 if (_bad != nullptr) { 290 FREE_C_HEAP_ARRAY(char, _bad); 291 } 292 } 293 294 bool is_valid() const { 295 return _valid; 296 } 297 298 const char* what() const { 299 return _bad; 300 } 301 }; 302 303 class CompilerDirectives : public CHeapObj<mtCompiler> { 304 private: 305 CompilerDirectives* _next; 306 BasicMatcher* _match; 307 int _ref_count; 308 309 public: 310 311 CompilerDirectives(); 312 ~CompilerDirectives(); 313 314 CompilerDirectives* next(); 315 void set_next(CompilerDirectives* next) {_next = next; } 316 317 bool match(const methodHandle& method); 318 BasicMatcher* match() { return _match; } 319 bool add_match(char* str, const char*& error_msg); 320 DirectiveSet* get_for(AbstractCompiler *comp); 321 void print(outputStream* st); 322 bool is_default_directive() { return _next == nullptr; } 323 void finalize(outputStream* st); 324 325 void inc_refcount(); 326 void dec_refcount(); 327 int refcount(); 328 329 DirectiveSet* _c1_store; 330 DirectiveSet* _c2_store; 331 }; 332 333 #endif // SHARE_COMPILER_COMPILERDIRECTIVES_HPP