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_COMPILERDIRECTIVES_HPP
 26 #define SHARE_COMPILER_COMPILERDIRECTIVES_HPP
 27 
 28 #include "classfile/vmIntrinsics.hpp"
 29 #include "ci/ciMetadata.hpp"
 30 #include "ci/ciMethod.hpp"
 31 #include "compiler/compiler_globals.hpp"
 32 #include "compiler/methodMatcher.hpp"
 33 #include "opto/phasetype.hpp"
 34 #include "utilities/bitMap.hpp"
 35 #include "utilities/exceptions.hpp"
 36 #include "utilities/tribool.hpp"
 37 
 38   //      Directives flag name,    type, default value, compile command name
 39   #define compilerdirectives_common_other_flags(cflags) \
 40     cflags(Enable,                  bool, false, Unknown) \
 41     cflags(Exclude,                 bool, false, Unknown) \
 42     cflags(BreakAtExecute,          bool, false, BreakAtExecute) \
 43     cflags(BreakAtCompile,          bool, false, BreakAtCompile) \
 44     cflags(Log,                     bool, LogCompilation, Unknown) \
 45     cflags(MemLimit,                intx, 0, MemLimit) \
 46     cflags(MemStat,                 uintx, 0, MemStat) \
 47     cflags(PrintAssembly,           bool, PrintAssembly, PrintAssembly) \
 48     cflags(PrintCompilation,        bool, PrintCompilation, PrintCompilation) \
 49     cflags(PrintInlining,           bool, PrintInlining, PrintInlining) \
 50     cflags(PrintNMethods,           bool, PrintNMethods, PrintNMethods) \
 51     cflags(BackgroundCompilation,   bool, BackgroundCompilation, BackgroundCompilation) \
 52     cflags(ReplayInline,            bool, false, ReplayInline) \
 53     cflags(DumpReplay,              bool, false, DumpReplay) \
 54     cflags(DumpInline,              bool, false, DumpInline) \
 55     cflags(CompilerDirectivesIgnoreCompileCommands, bool, CompilerDirectivesIgnoreCompileCommands, Unknown) \
 56     cflags(RepeatCompilation,       intx, RepeatCompilation, RepeatCompilation) \
 57     cflags(PrecompileRecorded,      uintx, 0, PrecompileRecorded) \
 58     cflags(DontPrecompile,          bool, false, DontPrecompile) \
 59     cflags(DontPreload,             bool, false, DontPreload) \
 60     cflags(IgnoreRecordedProfile,   bool, false, IgnoreRecordedProfile) \
 61     cflags(IgnorePrecompiled,       bool, false, IgnorePrecompiled) \
 62     cflags(TooManyTrapsAtBCI,       ccstrlist, "", TooManyTrapsAtBCI)
 63 #define compilerdirectives_common_string_flags(cflags)                           \
 64   cflags(DisableIntrinsic,        ccstrlist, DisableIntrinsic, DisableIntrinsic) \
 65   cflags(ControlIntrinsic,        ccstrlist, ControlIntrinsic, ControlIntrinsic)
 66 #define compilerdirectives_common_flags(cflags) \
 67   compilerdirectives_common_other_flags(cflags) \
 68   compilerdirectives_common_string_flags(cflags)
 69 
 70 #ifdef COMPILER1
 71   #define compilerdirectives_c1_other_flags(cflags)
 72   #define compilerdirectives_c1_string_flags(cflags)
 73 #else
 74   #define compilerdirectives_c1_other_flags(cflags)
 75   #define compilerdirectives_c1_string_flags(cflags)
 76 #endif
 77 
 78 #define compilerdirectives_c1_flags(cflags) \
 79   compilerdirectives_c1_other_flags(cflags) \
 80   compilerdirectives_c1_string_flags(cflags)
 81 
 82 #ifdef COMPILER2
 83   #define compilerdirectives_c2_other_flags(cflags) \
 84     cflags(BlockLayoutByFrequency,  bool, BlockLayoutByFrequency,  BlockLayoutByFrequency) \
 85     cflags(PrintOptoAssembly,       bool, PrintOptoAssembly, PrintOptoAssembly) \
 86     cflags(PrintIntrinsics,         bool, PrintIntrinsics, PrintIntrinsics) \
 87 NOT_PRODUCT(cflags(TraceOptoPipelining, bool, TraceOptoPipelining, TraceOptoPipelining)) \
 88 NOT_PRODUCT(cflags(TraceOptoOutput,     bool, TraceOptoOutput, TraceOptoOutput)) \
 89 NOT_PRODUCT(cflags(TraceEscapeAnalysis, bool, false, TraceEscapeAnalysis)) \
 90 NOT_PRODUCT(cflags(PrintIdeal,          bool, PrintIdeal, PrintIdeal)) \
 91     cflags(TraceSpilling,           bool, TraceSpilling, TraceSpilling) \
 92     cflags(Vectorize,               bool, false, Vectorize) \
 93     cflags(CloneMapDebug,           bool, false, CloneMapDebug) \
 94 NOT_PRODUCT(cflags(IGVPrintLevel,       intx, PrintIdealGraphLevel, IGVPrintLevel)) \
 95     cflags(IncrementalInlineForceCleanup, bool, IncrementalInlineForceCleanup, IncrementalInlineForceCleanup) \
 96     cflags(MaxNodeLimit,            intx, MaxNodeLimit, MaxNodeLimit)
 97 #define compilerdirectives_c2_string_flags(cflags) \
 98 NOT_PRODUCT(cflags(TraceAutoVectorization, ccstrlist, "", TraceAutoVectorization)) \
 99 NOT_PRODUCT(cflags(PrintIdealPhase,     ccstrlist, "", PrintIdealPhase))
100 #else
101   #define compilerdirectives_c2_other_flags(cflags)
102   #define compilerdirectives_c2_string_flags(cflags)
103 #endif
104 
105 #define compilerdirectives_c2_flags(cflags) \
106   compilerdirectives_c2_other_flags(cflags) \
107   compilerdirectives_c2_string_flags(cflags)
108 
109 class AbstractCompiler;
110 class CompilerDirectives;
111 class DirectiveSet;
112 
113 class DirectivesStack : AllStatic {
114 private:
115   static CompilerDirectives* _top;
116   static CompilerDirectives* _bottom;
117   static int _depth;
118 
119   static void pop_inner(); // no lock version of pop
120 public:
121   static void init();
122   static DirectiveSet* getMatchingDirective(const methodHandle& mh, AbstractCompiler* comp);
123   static DirectiveSet* getDefaultDirective(AbstractCompiler* comp);
124   static void push(CompilerDirectives* directive);
125   static void pop(int count);
126   static bool check_capacity(int request_size, outputStream* st);
127   static void clear();
128   static void print(outputStream* st);
129   static void release(DirectiveSet* set);
130   static void release(CompilerDirectives* dir);
131 };
132 
133 class DirectiveSet : public CHeapObj<mtCompiler> {
134 private:
135   InlineMatcher* _inlinematchers;
136   CompilerDirectives* _directive;
137   TriBoolArray<(size_t)vmIntrinsics::number_of_intrinsics(), int> _intrinsic_control_words;
138   CHeapBitMap _ideal_phase_name_set;
139   CHeapBitMap _trace_auto_vectorization_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 
221   void print_intx(outputStream* st, ccstr n, intx v, bool mod) { if (mod) { st->print("%s:" INTX_FORMAT " ", n, v); } }
222   void print_uintx(outputStream* st, ccstr n, intx v, bool mod) { if (mod) { st->print("%s:" UINTX_FORMAT " ", n, v); } }
223   void print_bool(outputStream* st, ccstr n, bool v, bool mod) { if (mod) { st->print("%s:%s ", n, v ? "true" : "false"); } }
224   void print_double(outputStream* st, ccstr n, double v, bool mod) { if (mod) { st->print("%s:%f ", n, v); } }
225   void print_ccstr(outputStream* st, ccstr n, ccstr v, bool mod) { if (mod) { st->print("%s:%s ", n, v); } }
226   void print_ccstrlist(outputStream* st, ccstr n, ccstr v, bool mod) { print_ccstr(st, n, v, mod); }
227 
228 void print(outputStream* st) {
229     print_inline(st);
230     st->print("  ");
231 #define print_function_definition(name, type, dvalue, cc_flag) print_##type(st, #name, this->name##Option, true);
232     compilerdirectives_common_flags(print_function_definition)
233     compilerdirectives_c2_flags(print_function_definition)
234     compilerdirectives_c1_flags(print_function_definition)
235 #undef print_function_definition
236     st->cr();
237   }
238 };
239 
240 // Iterator of ControlIntrinsic=+_id1,-_id2,+_id3,...
241 //
242 // If disable_all is set, it accepts DisableIntrinsic and all intrinsic Ids
243 // appear in the list are disabled. Arguments don't have +/- prefix. eg.
244 // DisableIntrinsic=_id1,_id2,_id3,...
245 class ControlIntrinsicIter {
246  private:
247   bool _enabled;
248   char* _token;
249   char* _saved_ptr;
250   char* _list;
251   const bool _disableIntrinsic;
252   void next_token();
253 
254  public:
255   ControlIntrinsicIter(ccstrlist option, bool disable_all = false);
256   ~ControlIntrinsicIter();
257 
258   bool is_enabled() const { return _enabled; }
259   const char* operator*() const { return _token; }
260 
261   ControlIntrinsicIter& operator++();
262 };
263 
264 class ControlIntrinsicValidator {
265  private:
266   bool _valid;
267   char* _bad;
268 
269  public:
270   ControlIntrinsicValidator(ccstrlist option, bool disable_all) : _valid(true), _bad(nullptr) {
271     for (ControlIntrinsicIter iter(option, disable_all); *iter != nullptr && _valid; ++iter) {
272       if (vmIntrinsics::_none == vmIntrinsics::find_id(*iter)) {
273         const size_t len = MIN2<size_t>(strlen(*iter), 63) + 1;  // cap len to a value we know is enough for all intrinsic names
274         _bad = NEW_C_HEAP_ARRAY(char, len, mtCompiler);
275         // strncpy always writes len characters. If the source string is shorter, the function fills the remaining bytes with nullptrs.
276         strncpy(_bad, *iter, len);
277         _valid = false;
278       }
279     }
280   }
281 
282   ~ControlIntrinsicValidator() {
283     if (_bad != nullptr) {
284       FREE_C_HEAP_ARRAY(char, _bad);
285     }
286   }
287 
288   bool is_valid() const {
289     return _valid;
290   }
291 
292   const char* what() const {
293     return _bad;
294   }
295 };
296 
297 class CompilerDirectives : public CHeapObj<mtCompiler> {
298 private:
299   CompilerDirectives* _next;
300   BasicMatcher* _match;
301   int _ref_count;
302 
303 public:
304 
305   CompilerDirectives();
306   ~CompilerDirectives();
307 
308   CompilerDirectives* next();
309   void set_next(CompilerDirectives* next) {_next = next; }
310 
311   bool match(const methodHandle& method);
312   BasicMatcher* match() { return _match; }
313   bool add_match(char* str, const char*& error_msg);
314   DirectiveSet* get_for(AbstractCompiler *comp);
315   void print(outputStream* st);
316   bool is_default_directive() { return _next == nullptr; }
317   void finalize(outputStream* st);
318 
319   void inc_refcount();
320   void dec_refcount();
321   int refcount();
322 
323   DirectiveSet* _c1_store;
324   DirectiveSet* _c2_store;
325 };
326 
327 #endif // SHARE_COMPILER_COMPILERDIRECTIVES_HPP