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