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