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