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 #include "classfile/symbolTable.hpp"
26 #include "compiler/compilerDirectives.hpp"
27 #include "compiler/compilerOracle.hpp"
28 #include "compiler/methodMatcher.hpp"
29 #include "jvm.h"
30 #include "memory/allocation.inline.hpp"
31 #include "memory/oopFactory.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/klass.hpp"
34 #include "oops/method.inline.hpp"
35 #include "oops/symbol.hpp"
36 #include "opto/phasetype.hpp"
37 #include "opto/traceAutoVectorizationTag.hpp"
38 #include "opto/traceMergeStoresTag.hpp"
39 #include "runtime/globals_extension.hpp"
40 #include "runtime/handles.inline.hpp"
41 #include "runtime/jniHandles.hpp"
42 #include "runtime/os.hpp"
43 #include "utilities/istream.hpp"
44 #include "utilities/parseInteger.hpp"
45
46 // Default compile commands, if defined, are parsed before any of the
47 // explicitly defined compile commands. Thus, explicitly defined compile
48 // commands take precedence over default compile commands. The effect is
49 // as if the default compile commands had been specified at the start of
50 // the command line.
51 static const char* const default_compile_commands[] = {
52 #ifdef ASSERT
53 // In debug builds, impose a (generous) per-compilation memory limit
54 // to catch pathological compilations during testing. The suboption
55 // "crash" will cause the JVM to assert.
56 //
57 // Note: to disable the default limit at the command line,
58 // set a limit of 0 (e.g. -XX:CompileCommand=MemLimit,*.*,0).
59 "MemLimit,*.*,1G~crash",
60 #endif
61 nullptr };
62
63 static const char* optiontype_names[] = {
64 #define enum_of_types(type, name) name,
65 OPTION_TYPES(enum_of_types)
66 #undef enum_of_types
67 };
68
69 static const char* optiontype2name(enum OptionType type) {
70 return optiontype_names[static_cast<int>(type)];
71 }
72
73 static enum OptionType option_types[] = {
74 #define enum_of_options(option, name, ctype) OptionType::ctype,
75 COMPILECOMMAND_OPTIONS(enum_of_options)
76 #undef enum_of_options
77 };
78
79 static enum OptionType option2type(CompileCommandEnum option) {
80 return option_types[static_cast<int>(option)];
81 }
82
83 static const char* option_names[] = {
84 #define enum_of_options(option, name, ctype) name,
85 COMPILECOMMAND_OPTIONS(enum_of_options)
86 #undef enum_of_options
87 };
88
89 static const char* option2name(CompileCommandEnum option) {
90 return option_names[static_cast<int>(option)];
91 }
92
93 /* Methods to map real type names to OptionType */
94 template<typename T>
95 static OptionType get_type_for() {
96 return OptionType::Unknown;
97 };
98
99 template<> OptionType get_type_for<intx>() {
100 return OptionType::Intx;
101 }
102
103 template<> OptionType get_type_for<uintx>() {
104 return OptionType::Uintx;
105 }
106
107 template<> OptionType get_type_for<bool>() {
108 return OptionType::Bool;
109 }
110
111 template<> OptionType get_type_for<ccstr>() {
112 return OptionType::Ccstr;
113 }
114
115 template<> OptionType get_type_for<double>() {
116 return OptionType::Double;
117 }
118
119 class MethodMatcher;
120 class TypedMethodOptionMatcher;
121
122 static TypedMethodOptionMatcher* option_list = nullptr;
123 static bool any_set = false;
124
125 // A filter for quick lookup if an option is set
126 static bool option_filter[static_cast<int>(CompileCommandEnum::Unknown) + 1] = { 0 };
127
128 static void command_set_in_filter(CompileCommandEnum option) {
129 assert(option != CompileCommandEnum::Unknown, "sanity");
130 assert(option2type(option) != OptionType::Unknown, "sanity");
131
132 if ((option != CompileCommandEnum::DontInline) &&
133 (option != CompileCommandEnum::Inline) &&
134 (option != CompileCommandEnum::Log)) {
135 any_set = true;
136 }
137 option_filter[static_cast<int>(option)] = true;
138 }
139
140 static bool has_command(CompileCommandEnum option) {
141 return option_filter[static_cast<int>(option)];
142 }
143
144 class TypedMethodOptionMatcher : public MethodMatcher {
145 private:
146 TypedMethodOptionMatcher* _next;
147 CompileCommandEnum _option;
148 public:
149
150 union {
151 bool bool_value;
152 intx intx_value;
153 uintx uintx_value;
154 double double_value;
155 ccstr ccstr_value;
156 } _u;
157
158 TypedMethodOptionMatcher() : MethodMatcher(),
159 _next(nullptr),
160 _option(CompileCommandEnum::Unknown) {
161 memset(&_u, 0, sizeof(_u));
162 }
163
164 ~TypedMethodOptionMatcher();
165 static TypedMethodOptionMatcher* parse_method_pattern(char*& line, char* errorbuf, const int buf_size);
166 TypedMethodOptionMatcher* match(const methodHandle &method, CompileCommandEnum option);
167
168 void init(CompileCommandEnum option, TypedMethodOptionMatcher* next) {
169 _next = next;
170 _option = option;
171 }
172
173 void init_matcher(Symbol* class_name, Mode class_mode,
174 Symbol* method_name, Mode method_mode,
175 Symbol* signature) {
176 MethodMatcher::init(class_name, class_mode, method_name, method_mode, signature);
177 }
178
179 void set_next(TypedMethodOptionMatcher* next) {_next = next; }
180 TypedMethodOptionMatcher* next() { return _next; }
181 CompileCommandEnum option() { return _option; }
182 template<typename T> T value();
183 template<typename T> void set_value(T value);
184 void print();
185 void print_all();
186 TypedMethodOptionMatcher* clone();
187 };
188
189 // A few templated accessors instead of a full template class.
190 template<> intx TypedMethodOptionMatcher::value<intx>() {
191 return _u.intx_value;
192 }
193
194 template<> uintx TypedMethodOptionMatcher::value<uintx>() {
195 return _u.uintx_value;
196 }
197
198 template<> bool TypedMethodOptionMatcher::value<bool>() {
199 return _u.bool_value;
200 }
201
202 template<> double TypedMethodOptionMatcher::value<double>() {
203 return _u.double_value;
204 }
205
206 template<> ccstr TypedMethodOptionMatcher::value<ccstr>() {
207 return _u.ccstr_value;
208 }
209
210 template<> void TypedMethodOptionMatcher::set_value(intx value) {
211 _u.intx_value = value;
212 }
213
214 template<> void TypedMethodOptionMatcher::set_value(uintx value) {
215 _u.uintx_value = value;
216 }
217
218 template<> void TypedMethodOptionMatcher::set_value(double value) {
219 _u.double_value = value;
220 }
221
222 template<> void TypedMethodOptionMatcher::set_value(bool value) {
223 _u.bool_value = value;
224 }
225
226 template<> void TypedMethodOptionMatcher::set_value(ccstr value) {
227 _u.ccstr_value = (ccstr)os::strdup_check_oom(value);
228 }
229
230 void TypedMethodOptionMatcher::print() {
231 ttyLocker ttyl;
232 print_base(tty);
233 const char* name = option2name(_option);
234 enum OptionType type = option2type(_option);
235 switch (type) {
236 case OptionType::Intx:
237 tty->print_cr(" intx %s = %zd", name, value<intx>());
238 break;
239 case OptionType::Uintx:
240 tty->print_cr(" uintx %s = %zu", name, value<uintx>());
241 break;
242 case OptionType::Bool:
243 tty->print_cr(" bool %s = %s", name, value<bool>() ? "true" : "false");
244 break;
245 case OptionType::Double:
246 tty->print_cr(" double %s = %f", name, value<double>());
247 break;
248 case OptionType::Ccstr:
249 case OptionType::Ccstrlist:
250 tty->print_cr(" const char* %s = '%s'", name, value<ccstr>());
251 break;
252 default:
253 ShouldNotReachHere();
254 }
255 }
256
257 void TypedMethodOptionMatcher::print_all() {
258 print();
259 if (_next != nullptr) {
260 tty->print(" ");
261 _next->print_all();
262 }
263 }
264
265 TypedMethodOptionMatcher* TypedMethodOptionMatcher::clone() {
266 TypedMethodOptionMatcher* m = new TypedMethodOptionMatcher();
267 m->_class_mode = _class_mode;
268 m->_class_name = _class_name;
269 m->_method_mode = _method_mode;
270 m->_method_name = _method_name;
271 m->_signature = _signature;
272 // Need to ref count the symbols
273 if (_class_name != nullptr) {
274 _class_name->increment_refcount();
275 }
276 if (_method_name != nullptr) {
277 _method_name->increment_refcount();
278 }
279 if (_signature != nullptr) {
280 _signature->increment_refcount();
281 }
282 return m;
283 }
284
285 TypedMethodOptionMatcher::~TypedMethodOptionMatcher() {
286 enum OptionType type = option2type(_option);
287 if (type == OptionType::Ccstr || type == OptionType::Ccstrlist) {
288 ccstr v = value<ccstr>();
289 os::free((void*)v);
290 }
291 }
292
293 TypedMethodOptionMatcher* TypedMethodOptionMatcher::parse_method_pattern(char*& line, char* errorbuf, const int buf_size) {
294 assert(*errorbuf == '\0', "Dont call here with error_msg already set");
295 const char* error_msg = nullptr;
296 TypedMethodOptionMatcher* tom = new TypedMethodOptionMatcher();
297 MethodMatcher::parse_method_pattern(line, error_msg, tom);
298 if (error_msg != nullptr) {
299 jio_snprintf(errorbuf, buf_size, error_msg);
300 delete tom;
301 return nullptr;
302 }
303 return tom;
304 }
305
306 TypedMethodOptionMatcher* TypedMethodOptionMatcher::match(const methodHandle& method, CompileCommandEnum option) {
307 TypedMethodOptionMatcher* current = this;
308 while (current != nullptr) {
309 if (current->_option == option) {
310 if (current->matches(method)) {
311 return current;
312 }
313 }
314 current = current->next();
315 }
316 return nullptr;
317 }
318
319 template<typename T>
320 static bool register_command(TypedMethodOptionMatcher* matcher,
321 CompileCommandEnum option,
322 char* errorbuf,
323 const int buf_size,
324 T value) {
325 assert(matcher != option_list, "No circular lists please");
326 if (option == CompileCommandEnum::Log && !LogCompilation) {
327 tty->print_cr("Warning: +LogCompilation must be enabled in order for individual methods to be logged with ");
328 tty->print_cr(" CompileCommand=log,<method pattern>");
329 }
330 assert(CompilerOracle::option_matches_type(option, value), "Value must match option type");
331
332 if (option == CompileCommandEnum::Blackhole && !UnlockExperimentalVMOptions) {
333 warning("Blackhole compile option is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions");
334 // Delete matcher as we don't keep it
335 delete matcher;
336 return true;
337 }
338
339 if (!UnlockDiagnosticVMOptions) {
340 const char* name = option2name(option);
341 JVMFlag* flag = JVMFlag::find_declared_flag(name);
342 if (flag != nullptr && flag->is_diagnostic()) {
343 jio_snprintf(errorbuf, buf_size, "VM option '%s' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.", name);
344 delete matcher;
345 return false;
346 }
347 }
348
349 matcher->init(option, option_list);
350 matcher->set_value<T>(value);
351 option_list = matcher;
352 command_set_in_filter(option);
353
354 if (!CompilerOracle::be_quiet()) {
355 // Print out the successful registration of a compile command
356 ttyLocker ttyl;
357 tty->print("CompileCommand: %s ", option2name(option));
358 matcher->print();
359 }
360
361 return true;
362 }
363
364 template<typename T>
365 bool CompilerOracle::has_option_value(const methodHandle& method, CompileCommandEnum option, T& value) {
366 assert(option_matches_type(option, value), "Value must match option type");
367 if (!has_command(option)) {
368 return false;
369 }
370 if (option_list != nullptr) {
371 TypedMethodOptionMatcher* m = option_list->match(method, option);
372 if (m != nullptr) {
373 value = m->value<T>();
374 return true;
375 }
376 }
377 return false;
378 }
379
380 static bool resolve_inlining_predicate(CompileCommandEnum option, const methodHandle& method) {
381 assert(option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline, "Sanity");
382 bool v1 = false;
383 bool v2 = false;
384 bool has_inline = CompilerOracle::has_option_value(method, CompileCommandEnum::Inline, v1);
385 bool has_dnotinline = CompilerOracle::has_option_value(method, CompileCommandEnum::DontInline, v2);
386 if (has_inline && has_dnotinline) {
387 if (v1 && v2) {
388 // Conflict options detected
389 // Find the last one for that method and return the predicate accordingly
390 // option_list lists options in reverse order. So the first option we find is the last which was specified.
391 CompileCommandEnum last_one = CompileCommandEnum::Unknown;
392 TypedMethodOptionMatcher* current = option_list;
393 while (current != nullptr) {
394 last_one = current->option();
395 if (last_one == CompileCommandEnum::Inline || last_one == CompileCommandEnum::DontInline) {
396 if (current->matches(method)) {
397 return last_one == option;
398 }
399 }
400 current = current->next();
401 }
402 ShouldNotReachHere();
403 return false;
404 } else {
405 // No conflicts
406 return option == CompileCommandEnum::Inline ? v1 : v2;
407 }
408 } else {
409 if (option == CompileCommandEnum::Inline) {
410 return has_inline ? v1 : false;
411 } else {
412 return has_dnotinline ? v2 : false;
413 }
414 }
415 }
416
417 static bool check_predicate(CompileCommandEnum option, const methodHandle& method) {
418 // Special handling for Inline and DontInline since conflict options may be specified
419 if (option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline) {
420 return resolve_inlining_predicate(option, method);
421 }
422
423 bool value = false;
424 if (CompilerOracle::has_option_value(method, option, value)) {
425 return value;
426 }
427 return false;
428 }
429
430 bool CompilerOracle::has_any_command_set() {
431 return any_set;
432 }
433
434 // Explicit instantiation for all OptionTypes supported.
435 template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, CompileCommandEnum option, intx& value);
436 template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, CompileCommandEnum option, uintx& value);
437 template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, CompileCommandEnum option, bool& value);
438 template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, CompileCommandEnum option, ccstr& value);
439 template bool CompilerOracle::has_option_value<double>(const methodHandle& method, CompileCommandEnum option, double& value);
440
441 template<typename T>
442 bool CompilerOracle::option_matches_type(CompileCommandEnum option, T& value) {
443 enum OptionType option_type = option2type(option);
444 if (option_type == OptionType::Unknown) {
445 return false; // Can't query options with type Unknown.
446 }
447 if (option_type == OptionType::Ccstrlist) {
448 option_type = OptionType::Ccstr; // CCstrList type options are stored as Ccstr
449 }
450 return (get_type_for<T>() == option_type);
451 }
452
453 template bool CompilerOracle::option_matches_type<intx>(CompileCommandEnum option, intx& value);
454 template bool CompilerOracle::option_matches_type<uintx>(CompileCommandEnum option, uintx& value);
455 template bool CompilerOracle::option_matches_type<bool>(CompileCommandEnum option, bool& value);
456 template bool CompilerOracle::option_matches_type<ccstr>(CompileCommandEnum option, ccstr& value);
457 template bool CompilerOracle::option_matches_type<double>(CompileCommandEnum option, double& value);
458
459 bool CompilerOracle::has_option(const methodHandle& method, CompileCommandEnum option) {
460 bool value = false;
461 has_option_value(method, option, value);
462 return value;
463 }
464
465 bool CompilerOracle::should_exclude(const methodHandle& method) {
466 if (check_predicate(CompileCommandEnum::Exclude, method)) {
467 return true;
468 }
469 if (has_command(CompileCommandEnum::CompileOnly)) {
470 return !check_predicate(CompileCommandEnum::CompileOnly, method);
471 }
472 return false;
473 }
474
475 bool CompilerOracle::should_inline(const methodHandle& method) {
476 return (check_predicate(CompileCommandEnum::Inline, method));
477 }
478
479 bool CompilerOracle::should_not_inline(const methodHandle& method) {
480 return check_predicate(CompileCommandEnum::DontInline, method) || check_predicate(CompileCommandEnum::Exclude, method);
481 }
482
483 bool CompilerOracle::should_delay_inline(const methodHandle& method) {
484 return (check_predicate(CompileCommandEnum::DelayInline, method));
485 }
486
487 bool CompilerOracle::should_print(const methodHandle& method) {
488 return check_predicate(CompileCommandEnum::Print, method);
489 }
490
491 bool CompilerOracle::should_print_methods() {
492 return has_command(CompileCommandEnum::Print);
493 }
494
495 // Tells whether there are any methods to collect memory statistics for
496 bool CompilerOracle::should_collect_memstat() {
497 return has_command(CompileCommandEnum::MemStat) || has_command(CompileCommandEnum::MemLimit);
498 }
499
500 bool CompilerOracle::should_log(const methodHandle& method) {
501 if (!LogCompilation) return false;
502 if (!has_command(CompileCommandEnum::Log)) {
503 return true; // by default, log all
504 }
505 return (check_predicate(CompileCommandEnum::Log, method));
506 }
507
508 bool CompilerOracle::should_break_at(const methodHandle& method) {
509 return check_predicate(CompileCommandEnum::Break, method);
510 }
511
512 void CompilerOracle::tag_blackhole_if_possible(const methodHandle& method) {
513 if (!check_predicate(CompileCommandEnum::Blackhole, method)) {
514 return;
515 }
516 guarantee(UnlockExperimentalVMOptions, "Checked during initial parsing");
517 if (method->result_type() != T_VOID) {
518 warning("Blackhole compile option only works for methods with void type: %s",
519 method->name_and_sig_as_C_string());
520 return;
521 }
522 if (!method->is_empty_method()) {
523 warning("Blackhole compile option only works for empty methods: %s",
524 method->name_and_sig_as_C_string());
525 return;
526 }
527 if (!method->is_static()) {
528 warning("Blackhole compile option only works for static methods: %s",
529 method->name_and_sig_as_C_string());
530 return;
531 }
532 if (method->intrinsic_id() == vmIntrinsics::_blackhole) {
533 return;
534 }
535 if (method->intrinsic_id() != vmIntrinsics::_none) {
536 warning("Blackhole compile option only works for methods that do not have intrinsic set: %s, %s",
537 method->name_and_sig_as_C_string(), vmIntrinsics::name_at(method->intrinsic_id()));
538 return;
539 }
540 method->set_intrinsic_id(vmIntrinsics::_blackhole);
541 }
542
543 static CompileCommandEnum match_option_name(const char* line, int* bytes_read, char* errorbuf, int bufsize) {
544 assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommandEnum::Count), "option_names size mismatch");
545
546 *bytes_read = 0;
547 char option_buf[256];
548 int matches = sscanf(line, "%255[a-zA-Z0-9]%n", option_buf, bytes_read);
549 if (matches > 0 && strcasecmp(option_buf, "unknown") != 0) {
550 for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
551 if (strcasecmp(option_buf, option_names[i]) == 0) {
552 return static_cast<CompileCommandEnum>(i);
553 }
554 }
555 }
556 jio_snprintf(errorbuf, bufsize, "Unrecognized option '%s'", option_buf);
557 return CompileCommandEnum::Unknown;
558 }
559
560 // match exactly and don't mess with errorbuf
561 CompileCommandEnum CompilerOracle::parse_option_name(const char* line) {
562 for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
563 if (strcasecmp(line, option_names[i]) == 0) {
564 return static_cast<CompileCommandEnum>(i);
565 }
566 }
567 return CompileCommandEnum::Unknown;
568 }
569
570 enum OptionType CompilerOracle::parse_option_type(const char* type_str) {
571 for (uint i = 0; i < ARRAY_SIZE(optiontype_names); i++) {
572 if (strcasecmp(type_str, optiontype_names[i]) == 0) {
573 return static_cast<enum OptionType>(i);
574 }
575 }
576 return OptionType::Unknown;
577 }
578
579 static void print_tip() { // CMH Update info
580 tty->cr();
581 tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>' - to set boolean option to true");
582 tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>,<value>'");
583 tty->print_cr("Use: '-XX:CompileCommand=help' for more information and to list all option.");
584 tty->cr();
585 }
586
587 static void print_option(CompileCommandEnum option, const char* name, enum OptionType type) {
588 if (type != OptionType::Unknown) {
589 tty->print_cr(" %s (%s)", name, optiontype2name(type));
590 }
591 }
592
593 static void print_commands() {
594 tty->cr();
595 tty->print_cr("All available options:");
596 #define enum_of_options(option, name, ctype) print_option(CompileCommandEnum::option, name, OptionType::ctype);
597 COMPILECOMMAND_OPTIONS(enum_of_options)
598 #undef enum_of_options
599 tty->cr();
600 }
601
602 static void usage() {
603 tty->cr();
604 tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
605 tty->print_cr("behavior of the dynamic compilers.");
606 tty->cr();
607 tty->print_cr("Compile commands has this general form:");
608 tty->print_cr("-XX:CompileCommand=<option><method pattern><value>");
609 tty->print_cr(" Sets <option> to the specified value for methods matching <method pattern>");
610 tty->print_cr(" All options are typed");
611 tty->cr();
612 tty->print_cr("-XX:CompileCommand=<option><method pattern>");
613 tty->print_cr(" Sets <option> to true for methods matching <method pattern>");
614 tty->print_cr(" Only applies to boolean options.");
615 tty->cr();
616 tty->print_cr("-XX:CompileCommand=quiet");
617 tty->print_cr(" Silence the compile command output");
618 tty->cr();
619 tty->print_cr("-XX:CompileCommand=help");
620 tty->print_cr(" Prints this help text");
621 tty->cr();
622 print_commands();
623 tty->cr();
624 tty->print_cr("The <method pattern> has the format '<class>.<method><descriptor>'.");
625 tty->cr();
626 tty->print_cr("For example, the <method pattern>");
627 tty->cr();
628 tty->print_cr(" package/Class.method(Lpackage/Parameter;)Lpackage/Return;");
629 tty->cr();
630 tty->print_cr("matches the <method> 'method' in <class> 'package/Class' with <descriptor>");
631 tty->print_cr("'(Lpackage/Parameter;)Lpackage/Return;'");
632 tty->cr();
633 tty->print_cr("For backward compatibility this form is also allowed:");
634 tty->cr();
635 tty->print_cr(" package.Class::method(Lpackage.Parameter;)Lpackage.Return;");
636 tty->cr();
637 tty->print_cr("A whitespace or comma can optionally separate the <descriptor> from the");
638 tty->print_cr("<method>:");
639 tty->cr();
640 tty->print_cr(" package/Class.method (Lpackage/Parameter;)Lpackage/Return;");
641 tty->print_cr(" package/Class.method,(Lpackage/Parameter;)Lpackage/Return;");
642 tty->cr();
643 tty->print_cr("The <class> and <method> accept leading and trailing '*' wildcards");
644 tty->print_cr("matching:");
645 tty->cr();
646 tty->print_cr(" *ackage/Clas*.*etho*(Lpackage/Parameter;)Lpackage/Return;");
647 tty->cr();
648 tty->print_cr("The <descriptor> does not support explicit wildcards and");
649 tty->print_cr("always has an implicit trailing wildcard. Therefore,");
650 tty->cr();
651 tty->print_cr(" package/Class.method(Lpackage/Parameter;)Lpackage/Return;");
652 tty->cr();
653 tty->print_cr("matches a subset of");
654 tty->cr();
655 tty->print_cr(" package/Class.method(Lpackage/Parameter;)");
656 tty->cr();
657 tty->print_cr("which matches a subset of");
658 tty->cr();
659 tty->print_cr(" package/Class.method");
660 tty->cr();
661 tty->print_cr("which matches all possible descriptors.");
662 tty->cr();
663 tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
664 tty->print_cr(" -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
665 tty->cr();
666 tty->print_cr("The CompileCommands can be loaded from a file with the flag");
667 tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
668 tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
669 tty->print_cr("Add one command on each line.");
670 tty->print_cr(" exclude java/*.*");
671 tty->print_cr(" option java/*.* ReplayInline");
672 tty->cr();
673 tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
674 tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
675 tty->print_cr("commands to the same method results in undefined behavior.");
676 tty->cr();
677 tty->print_cr("The 'exclude' command excludes methods from top-level compilations as well as");
678 tty->print_cr("from inlining, whereas the 'compileonly' command only excludes methods from");
679 tty->print_cr("top-level compilations (i.e. they can still be inlined into other compilation units).");
680 tty->cr();
681 };
682
683 static int skip_whitespace(char* &line) {
684 // Skip any leading spaces
685 int whitespace_read = 0;
686 sscanf(line, "%*[ \t]%n", &whitespace_read);
687 line += whitespace_read;
688 return whitespace_read;
689 }
690
691 static void skip_comma(char* &line) {
692 // Skip any leading spaces
693 if (*line == ',') {
694 line++;
695 }
696 }
697
698 static bool parseMemLimit(const char* line, intx& value, int& bytes_read, char* errorbuf, const int buf_size) {
699 // Format:
700 // "<memory size>['~' <suboption>]"
701 // <memory size> can have units, e.g. M
702 // <suboption> one of "crash" "stop", if omitted, "stop" is implied.
703 //
704 // Examples:
705 // -XX:CompileCommand='memlimit,*.*,20m'
706 // -XX:CompileCommand='memlimit,*.*,20m~stop'
707 // -XX:CompileCommand='memlimit,Option::toString,1m~crash'
708 //
709 // The resulting intx carries the size and whether we are to stop or crash:
710 // - neg. value means crash
711 // - pos. value (default) means stop
712 size_t s = 0;
713 char* end;
714 if (!parse_integer<size_t>(line, &end, &s)) {
715 jio_snprintf(errorbuf, buf_size, "MemLimit: invalid value");
716 return false;
717 }
718 bytes_read = (int)(end - line);
719
720 intx v = (intx)s;
721 if ((*end) != '\0') {
722 if (strncasecmp(end, "~crash", 6) == 0) {
723 v = -v;
724 bytes_read += 6;
725 } else if (strncasecmp(end, "~stop", 5) == 0) {
726 // ok, this is the default
727 bytes_read += 5;
728 } else {
729 jio_snprintf(errorbuf, buf_size, "MemLimit: invalid option");
730 return false;
731 }
732 }
733 value = v;
734 return true;
735 }
736
737 static bool parseMemStat(const char* line, uintx& value, int& bytes_read, char* errorbuf, const int buf_size) {
738
739 #define IF_ENUM_STRING(S, CMD) \
740 if (strncasecmp(line, S, strlen(S)) == 0) { \
741 bytes_read += (int)strlen(S); \
742 CMD \
743 return true; \
744 }
745
746 IF_ENUM_STRING("collect", {
747 value = (uintx)MemStatAction::collect;
748 });
749 IF_ENUM_STRING("print", {
750 value = (uintx)MemStatAction::print;
751 });
752 #undef IF_ENUM_STRING
753
754 jio_snprintf(errorbuf, buf_size, "MemStat: invalid option");
755
756 return false;
757 }
758
759 static bool scan_value(enum OptionType type, char* line, int& total_bytes_read,
760 TypedMethodOptionMatcher* matcher, CompileCommandEnum option, char* errorbuf, const int buf_size) {
761 int bytes_read = 0;
762 const char* ccname = option2name(option);
763 const char* type_str = optiontype2name(type);
764 int skipped = skip_whitespace(line);
765 total_bytes_read += skipped;
766 if (type == OptionType::Intx) {
767 intx value;
768 bool success = false;
769 if (option == CompileCommandEnum::MemLimit) {
770 // Special parsing for MemLimit
771 success = parseMemLimit(line, value, bytes_read, errorbuf, buf_size);
772 } else {
773 // Is it a raw number?
774 success = sscanf(line, "%zd%n", &value, &bytes_read) == 1;
775 }
776 if (success) {
777 total_bytes_read += bytes_read;
778 return register_command(matcher, option, errorbuf, buf_size, value);
779 } else {
780 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
781 return false;
782 }
783 } else if (type == OptionType::Uintx) {
784 uintx value;
785 bool success = false;
786 if (option == CompileCommandEnum::MemStat) {
787 // Special parsing for MemStat
788 success = parseMemStat(line, value, bytes_read, errorbuf, buf_size);
789 } else {
790 // parse as raw number
791 success = sscanf(line, "%zu%n", &value, &bytes_read) == 1;
792 }
793 if (success) {
794 total_bytes_read += bytes_read;
795 return register_command(matcher, option, errorbuf, buf_size, value);
796 } else {
797 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
798 return false;
799 }
800 } else if (type == OptionType::Ccstr) {
801 ResourceMark rm;
802 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
803 if (sscanf(line, "%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
804 total_bytes_read += bytes_read;
805 return register_command(matcher, option, errorbuf, buf_size, (ccstr) value);
806 } else {
807 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
808 return false;
809 }
810 } else if (type == OptionType::Ccstrlist) {
811 // Accumulates several strings into one. The internal type is ccstr.
812 ResourceMark rm;
813 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
814 char* next_value = value;
815 if (sscanf(line, "%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
816 total_bytes_read += bytes_read;
817 line += bytes_read;
818 next_value += bytes_read + 1;
819 char* end_value = next_value - 1;
820 while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
821 total_bytes_read += bytes_read;
822 line += bytes_read;
823 *end_value = ' '; // override '\0'
824 next_value += bytes_read;
825 end_value = next_value-1;
826 }
827
828 if (option == CompileCommandEnum::ControlIntrinsic || option == CompileCommandEnum::DisableIntrinsic) {
829 ControlIntrinsicValidator validator(value, (option == CompileCommandEnum::DisableIntrinsic));
830
831 if (!validator.is_valid()) {
832 jio_snprintf(errorbuf, buf_size, "Unrecognized intrinsic detected in %s: %s", option2name(option), validator.what());
833 return false;
834 }
835 }
836 #if !defined(PRODUCT) && defined(COMPILER2)
837 else if (option == CompileCommandEnum::TraceAutoVectorization) {
838 TraceAutoVectorizationTagValidator validator(value, true);
839
840 if (!validator.is_valid()) {
841 jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
842 return false;
843 }
844 } else if (option == CompileCommandEnum::TraceMergeStores) {
845 TraceMergeStores::TagValidator validator(value, true);
846
847 if (!validator.is_valid()) {
848 jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
849 return false;
850 }
851 } else if (option == CompileCommandEnum::PrintIdealPhase) {
852 PhaseNameValidator validator(value);
853
854 if (!validator.is_valid()) {
855 jio_snprintf(errorbuf, buf_size, "Unrecognized phase name in %s: %s", option2name(option), validator.what());
856 return false;
857 }
858 } else if (option == CompileCommandEnum::TestOptionList) {
859 // all values are ok
860 } else if (option == CompileCommandEnum::TooManyTrapsAtBCI) {
861 // FIXME
862 }
863 #endif
864 else {
865 assert(false, "Ccstrlist type option missing validator");
866 }
867
868 return register_command(matcher, option, errorbuf, buf_size, (ccstr) value);
869 } else {
870 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
871 return false;
872 }
873 } else if (type == OptionType::Bool) {
874 char value[256];
875 if (*line == '\0') {
876 // Short version of a CompileCommand sets a boolean Option to true
877 // -XXCompileCommand=<Option>,<method pattern>
878 return register_command(matcher, option, errorbuf, buf_size,true);
879 }
880 if (sscanf(line, "%255[a-zA-Z]%n", value, &bytes_read) == 1) {
881 if (strcasecmp(value, "true") == 0) {
882 total_bytes_read += bytes_read;
883 return register_command(matcher, option, errorbuf, buf_size,true);
884 } else if (strcasecmp(value, "false") == 0) {
885 total_bytes_read += bytes_read;
886 return register_command(matcher, option, errorbuf, buf_size,false);
887 } else {
888 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
889 return false;
890 }
891 } else {
892 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
893 return false;
894 }
895 } else if (type == OptionType::Double) {
896 char buffer[2][256];
897 // Decimal separator '.' has been replaced with ' ' or '/' earlier,
898 // so read integer and fraction part of double value separately.
899 if (sscanf(line, "%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
900 char value[512] = "";
901 jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
902 total_bytes_read += bytes_read;
903 return register_command(matcher, option, errorbuf, buf_size, atof(value));
904 } else {
905 jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
906 return false;
907 }
908 } else {
909 jio_snprintf(errorbuf, buf_size, "Type '%s' not supported ", type_str);
910 return false;
911 }
912 }
913
914 // Scan next option and value in line, return MethodMatcher object on success, nullptr on failure.
915 // On failure, error_msg contains description for the first error.
916 // For future extensions: set error_msg on first error.
917 static bool scan_option_and_value(enum OptionType type, char* line, int& total_bytes_read,
918 TypedMethodOptionMatcher* matcher,
919 char* errorbuf, const int buf_size) {
920 total_bytes_read = 0;
921 int bytes_read = 0;
922 char option_buf[256];
923
924 // Read option name.
925 if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option_buf, &bytes_read) == 1) {
926 line += bytes_read;
927 total_bytes_read += bytes_read;
928 int bytes_read2 = 0;
929 total_bytes_read += skip_whitespace(line);
930 CompileCommandEnum option = match_option_name(option_buf, &bytes_read2, errorbuf, buf_size);
931 if (option == CompileCommandEnum::Unknown) {
932 assert(*errorbuf != '\0', "error must have been set");
933 return false;
934 }
935 enum OptionType optiontype = option2type(option);
936 if (option2type(option) != type) {
937 const char* optiontype_name = optiontype2name(optiontype);
938 const char* type_name = optiontype2name(type);
939 jio_snprintf(errorbuf, buf_size, "Option '%s' with type '%s' doesn't match supplied type '%s'", option_buf, optiontype_name, type_name);
940 return false;
941 }
942 return scan_value(type, line, total_bytes_read, matcher, option, errorbuf, buf_size);
943 } else {
944 const char* type_str = optiontype2name(type);
945 jio_snprintf(errorbuf, buf_size, "Option name for type '%s' should be alphanumeric ", type_str);
946 return false;
947 }
948 }
949
950 void CompilerOracle::print_parse_error(char* error_msg, char* original_line) {
951 assert(*error_msg != '\0', "Must have error_message");
952 ttyLocker ttyl;
953 tty->print_cr("CompileCommand: An error occurred during parsing");
954 tty->print_cr("Error: %s", error_msg);
955 tty->print_cr("Line: '%s'", original_line);
956 print_tip();
957 }
958
959 class LineCopy : StackObj {
960 const char* _copy;
961 public:
962 LineCopy(char* line) {
963 _copy = os::strdup(line, mtInternal);
964 }
965 ~LineCopy() {
966 os::free((void*)_copy);
967 }
968 char* get() {
969 return (char*)_copy;
970 }
971 };
972
973 bool CompilerOracle::parse_from_line_quietly(char* line) {
974 const bool quiet0 = _quiet;
975 _quiet = true;
976 const bool result = parse_from_line(line);
977 _quiet = quiet0;
978 return result;
979 }
980
981 bool CompilerOracle::parse_from_line(char* line) {
982 if ((line[0] == '\0') || (line[0] == '#')) {
983 return true;
984 }
985
986 LineCopy original(line);
987 int bytes_read;
988 char error_buf[1024] = {0};
989
990 CompileCommandEnum option = match_option_name(line, &bytes_read, error_buf, sizeof(error_buf));
991 line += bytes_read;
992 ResourceMark rm;
993
994 if (option == CompileCommandEnum::Unknown) {
995 print_parse_error(error_buf, original.get());
996 return false;
997 }
998
999 if (option == CompileCommandEnum::Quiet) {
1000 _quiet = true;
1001 return true;
1002 }
1003
1004 if (option == CompileCommandEnum::Help) {
1005 usage();
1006 return true;
1007 }
1008
1009 if (option == CompileCommandEnum::Option) {
1010 // Look for trailing options.
1011 //
1012 // Two types of trailing options are
1013 // supported:
1014 //
1015 // (1) CompileCommand=option,Klass::method,option
1016 // (2) CompileCommand=option,Klass::method,type,option,value
1017 //
1018 // Type (1) is used to enable a boolean option for a method.
1019 //
1020 // Type (2) is used to support options with a value. Values can have the
1021 // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
1022
1023 char option_type[256]; // stores option for Type (1) and type of Type (2)
1024 skip_comma(line);
1025 TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
1026 if (archetype == nullptr) {
1027 print_parse_error(error_buf, original.get());
1028 return false;
1029 }
1030
1031 skip_whitespace(line);
1032
1033 // This is unnecessarily complex. Should retire multi-option lines and skip while loop
1034 while (sscanf(line, "%255[a-zA-Z0-9]%n", option_type, &bytes_read) == 1) {
1035 line += bytes_read;
1036
1037 // typed_matcher is used as a blueprint for each option, deleted at the end
1038 TypedMethodOptionMatcher* typed_matcher = archetype->clone();
1039 enum OptionType type = parse_option_type(option_type);
1040 if (type != OptionType::Unknown) {
1041 // Type (2) option: parse option name and value.
1042 if (!scan_option_and_value(type, line, bytes_read, typed_matcher, error_buf, sizeof(error_buf))) {
1043 print_parse_error(error_buf, original.get());
1044 return false;
1045 }
1046 line += bytes_read;
1047 } else {
1048 // Type (1) option - option_type contains the option name -> bool value = true is implied
1049 int bytes_read;
1050 CompileCommandEnum option = match_option_name(option_type, &bytes_read, error_buf, sizeof(error_buf));
1051 if (option == CompileCommandEnum::Unknown) {
1052 print_parse_error(error_buf, original.get());
1053 return false;
1054 }
1055 if (option2type(option) == OptionType::Bool) {
1056 if (!register_command(typed_matcher, option, error_buf, sizeof(error_buf), true)) {
1057 print_parse_error(error_buf, original.get());
1058 return false;
1059 }
1060 } else {
1061 jio_snprintf(error_buf, sizeof(error_buf), " Missing type '%s' before option '%s'",
1062 optiontype2name(option2type(option)), option2name(option));
1063 print_parse_error(error_buf, original.get());
1064 return false;
1065 }
1066 }
1067 assert(typed_matcher != nullptr, "sanity");
1068 assert(*error_buf == '\0', "No error here");
1069 skip_whitespace(line);
1070 } // while(
1071 delete archetype;
1072 } else { // not an OptionCommand
1073 // Command has the following form:
1074 // CompileCommand=<option>,<method pattern><value>
1075 // CompileCommand=<option>,<method pattern> (implies option is bool and value is true)
1076 assert(*error_buf == '\0', "Don't call here with error_buf already set");
1077 enum OptionType type = option2type(option);
1078 int bytes_read = 0;
1079 skip_comma(line);
1080 TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
1081 if (matcher == nullptr) {
1082 print_parse_error(error_buf, original.get());
1083 return false;
1084 }
1085 skip_whitespace(line);
1086 if (*line == '\0') {
1087 if (option2type(option) == OptionType::Bool) {
1088 // if this is a bool option this implies true
1089 if (!register_command(matcher, option, error_buf, sizeof(error_buf), true)) {
1090 print_parse_error(error_buf, original.get());
1091 return false;
1092 }
1093 return true;
1094 } else if (option == CompileCommandEnum::MemStat) {
1095 // MemStat default action is to collect data but to not print
1096 if (!register_command(matcher, option, error_buf, sizeof(error_buf), (uintx)MemStatAction::collect)) {
1097 print_parse_error(error_buf, original.get());
1098 return false;
1099 }
1100 return true;
1101 } else {
1102 jio_snprintf(error_buf, sizeof(error_buf), " Option '%s' is not followed by a value", option2name(option));
1103 print_parse_error(error_buf, original.get());
1104 return false;
1105 }
1106 }
1107 if (!scan_value(type, line, bytes_read, matcher, option, error_buf, sizeof(error_buf))) {
1108 print_parse_error(error_buf, original.get());
1109 return false;
1110 }
1111 assert(matcher != nullptr, "consistency");
1112 }
1113 return true;
1114 }
1115
1116 static const char* default_cc_file = ".hotspot_compiler";
1117
1118 static const char* cc_file() {
1119 #ifdef ASSERT
1120 if (CompileCommandFile == nullptr)
1121 return default_cc_file;
1122 #endif
1123 return CompileCommandFile;
1124 }
1125
1126 bool CompilerOracle::has_command_file() {
1127 return cc_file() != nullptr;
1128 }
1129
1130 bool CompilerOracle::_quiet = false;
1131
1132 bool CompilerOracle::parse_from_file() {
1133 assert(has_command_file(), "command file must be specified");
1134 FILE* stream = os::fopen(cc_file(), "rt");
1135 if (stream == nullptr) {
1136 return true;
1137 }
1138
1139 FileInput input(stream, /*need_close=*/ true);
1140 return parse_from_input(&input, parse_from_line);
1141 }
1142
1143 bool CompilerOracle::parse_from_input(inputStream::Input* input,
1144 CompilerOracle::
1145 parse_from_line_fn_t* parse_from_line) {
1146 bool success = true;
1147 for (inputStream in(input); !in.done(); in.next()) {
1148 if (!parse_from_line(in.current_line())) {
1149 success = false;
1150 }
1151 }
1152 return success;
1153 }
1154
1155 bool CompilerOracle::parse_from_string(const char* str,
1156 CompilerOracle::
1157 parse_from_line_fn_t* parse_from_line) {
1158 MemoryInput input(str, strlen(str));
1159 return parse_from_input(&input, parse_from_line);
1160 }
1161
1162 bool compilerOracle_init() {
1163 bool success = true;
1164 // Register default compile commands first - any commands specified via CompileCommand will
1165 // supersede these default commands.
1166 for (int i = 0; default_compile_commands[i] != nullptr; i ++) {
1167 char* s = os::strdup(default_compile_commands[i]);
1168 success = CompilerOracle::parse_from_line_quietly(s);
1169 os::free(s);
1170 assert(success, "default compile command \"%s\" failed to parse", default_compile_commands[i]);
1171 }
1172 if (!CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line)) {
1173 success = false;
1174 }
1175 if (!CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only)) {
1176 success = false;
1177 }
1178 if (CompilerOracle::has_command_file()) {
1179 if (!CompilerOracle::parse_from_file()) {
1180 success = false;
1181 }
1182 } else {
1183 struct stat buf;
1184 if (os::stat(default_cc_file, &buf) == 0) {
1185 warning("%s file is present but has been ignored. "
1186 "Run with -XX:CompileCommandFile=%s to load the file.",
1187 default_cc_file, default_cc_file);
1188 }
1189 }
1190 if (has_command(CompileCommandEnum::Print)) {
1191 if (PrintAssembly) {
1192 warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
1193 }
1194 }
1195 return success;
1196 }
1197
1198 bool CompilerOracle::parse_compile_only(char* line) {
1199 if (line[0] == '\0') {
1200 return true;
1201 }
1202 ResourceMark rm;
1203 char error_buf[1024] = {0};
1204 LineCopy original(line);
1205 char* method_pattern;
1206 do {
1207 if (line[0] == '\0') {
1208 break;
1209 }
1210 method_pattern = strtok_r(line, ",", &line);
1211 if (method_pattern != nullptr) {
1212 TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(method_pattern, error_buf, sizeof(error_buf));
1213 if (matcher != nullptr) {
1214 if (register_command(matcher, CompileCommandEnum::CompileOnly, error_buf, sizeof(error_buf), true)) {
1215 continue;
1216 }
1217 }
1218 }
1219 ttyLocker ttyl;
1220 tty->print_cr("CompileOnly: An error occurred during parsing");
1221 if (*error_buf != '\0') {
1222 tty->print_cr("Error: %s", error_buf);
1223 }
1224 tty->print_cr("Line: '%s'", original.get());
1225 return false;
1226 } while (method_pattern != nullptr && line != nullptr);
1227 return true;
1228 }
1229
1230 CompileCommandEnum CompilerOracle::string_to_option(const char* name) {
1231 int bytes_read = 0;
1232 char errorbuf[1024] = {0};
1233 return match_option_name(name, &bytes_read, errorbuf, sizeof(errorbuf));
1234 }