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