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 void register_command(TypedMethodOptionMatcher* matcher,
 321                              CompileCommandEnum option,
 322                              T value) {
 323   assert(matcher != option_list, "No circular lists please");
 324   if (option == CompileCommandEnum::Log && !LogCompilation) {
 325     tty->print_cr("Warning:  +LogCompilation must be enabled in order for individual methods to be logged with ");
 326     tty->print_cr("          CompileCommand=log,<method pattern>");
 327   }
 328   assert(CompilerOracle::option_matches_type(option, value), "Value must match option type");
 329 
 330   if (option == CompileCommandEnum::Blackhole && !UnlockExperimentalVMOptions) {
 331     warning("Blackhole compile option is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions");
 332     // Delete matcher as we don't keep it
 333     delete matcher;
 334     return;
 335   }
 336 
 337   matcher->init(option, option_list);
 338   matcher->set_value<T>(value);
 339   option_list = matcher;
 340   command_set_in_filter(option);
 341 
 342   if (!CompilerOracle::be_quiet()) {
 343     // Print out the successful registration of a compile command
 344     ttyLocker ttyl;
 345     tty->print("CompileCommand: %s ", option2name(option));
 346     matcher->print();
 347   }
 348 
 349   return;
 350 }
 351 
 352 template<typename T>
 353 bool CompilerOracle::has_option_value(const methodHandle& method, CompileCommandEnum option, T& value) {
 354   assert(option_matches_type(option, value), "Value must match option type");
 355   if (!has_command(option)) {
 356     return false;
 357   }
 358   if (option_list != nullptr) {
 359     TypedMethodOptionMatcher* m = option_list->match(method, option);
 360     if (m != nullptr) {
 361       value = m->value<T>();
 362       return true;
 363     }
 364   }
 365   return false;
 366 }
 367 
 368 static bool resolve_inlining_predicate(CompileCommandEnum option, const methodHandle& method) {
 369   assert(option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline, "Sanity");
 370   bool v1 = false;
 371   bool v2 = false;
 372   bool has_inline = CompilerOracle::has_option_value(method, CompileCommandEnum::Inline, v1);
 373   bool has_dnotinline = CompilerOracle::has_option_value(method, CompileCommandEnum::DontInline, v2);
 374   if (has_inline && has_dnotinline) {
 375     if (v1 && v2) {
 376       // Conflict options detected
 377       // Find the last one for that method and return the predicate accordingly
 378       // option_list lists options in reverse order. So the first option we find is the last which was specified.
 379       CompileCommandEnum last_one = CompileCommandEnum::Unknown;
 380       TypedMethodOptionMatcher* current = option_list;
 381       while (current != nullptr) {
 382         last_one = current->option();
 383         if (last_one == CompileCommandEnum::Inline || last_one == CompileCommandEnum::DontInline) {
 384           if (current->matches(method)) {
 385             return last_one == option;
 386           }
 387         }
 388         current = current->next();
 389       }
 390       ShouldNotReachHere();
 391       return false;
 392     } else {
 393       // No conflicts
 394       return option == CompileCommandEnum::Inline ? v1 : v2;
 395     }
 396   } else {
 397     if (option == CompileCommandEnum::Inline) {
 398       return has_inline ? v1 : false;
 399     } else {
 400       return has_dnotinline ? v2 : false;
 401     }
 402   }
 403 }
 404 
 405 static bool check_predicate(CompileCommandEnum option, const methodHandle& method) {
 406   // Special handling for Inline and DontInline since conflict options may be specified
 407   if (option == CompileCommandEnum::Inline || option == CompileCommandEnum::DontInline) {
 408     return resolve_inlining_predicate(option, method);
 409   }
 410 
 411   bool value = false;
 412   if (CompilerOracle::has_option_value(method, option, value)) {
 413     return value;
 414   }
 415   return false;
 416 }
 417 
 418 bool CompilerOracle::has_any_command_set() {
 419   return any_set;
 420 }
 421 
 422 // Explicit instantiation for all OptionTypes supported.
 423 template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, CompileCommandEnum option, intx& value);
 424 template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, CompileCommandEnum option, uintx& value);
 425 template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, CompileCommandEnum option, bool& value);
 426 template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, CompileCommandEnum option, ccstr& value);
 427 template bool CompilerOracle::has_option_value<double>(const methodHandle& method, CompileCommandEnum option, double& value);
 428 
 429 template<typename T>
 430 bool CompilerOracle::option_matches_type(CompileCommandEnum option, T& value) {
 431   enum OptionType option_type = option2type(option);
 432   if (option_type == OptionType::Unknown) {
 433     return false; // Can't query options with type Unknown.
 434   }
 435   if (option_type == OptionType::Ccstrlist) {
 436     option_type = OptionType::Ccstr; // CCstrList type options are stored as Ccstr
 437   }
 438   return (get_type_for<T>() == option_type);
 439 }
 440 
 441 template bool CompilerOracle::option_matches_type<intx>(CompileCommandEnum option, intx& value);
 442 template bool CompilerOracle::option_matches_type<uintx>(CompileCommandEnum option, uintx& value);
 443 template bool CompilerOracle::option_matches_type<bool>(CompileCommandEnum option, bool& value);
 444 template bool CompilerOracle::option_matches_type<ccstr>(CompileCommandEnum option, ccstr& value);
 445 template bool CompilerOracle::option_matches_type<double>(CompileCommandEnum option, double& value);
 446 
 447 bool CompilerOracle::has_option(const methodHandle& method, CompileCommandEnum option) {
 448   bool value = false;
 449   has_option_value(method, option, value);
 450   return value;
 451 }
 452 
 453 bool CompilerOracle::should_exclude(const methodHandle& method) {
 454   if (check_predicate(CompileCommandEnum::Exclude, method)) {
 455     return true;
 456   }
 457   if (has_command(CompileCommandEnum::CompileOnly)) {
 458     return !check_predicate(CompileCommandEnum::CompileOnly, method);
 459   }
 460   return false;
 461 }
 462 
 463 bool CompilerOracle::should_inline(const methodHandle& method) {
 464   return (check_predicate(CompileCommandEnum::Inline, method));
 465 }
 466 
 467 bool CompilerOracle::should_not_inline(const methodHandle& method) {
 468   return check_predicate(CompileCommandEnum::DontInline, method) || check_predicate(CompileCommandEnum::Exclude, method);
 469 }
 470 
 471 bool CompilerOracle::should_print(const methodHandle& method) {
 472   return check_predicate(CompileCommandEnum::Print, method);
 473 }
 474 
 475 bool CompilerOracle::should_print_methods() {
 476   return has_command(CompileCommandEnum::Print);
 477 }
 478 
 479 // Tells whether there are any methods to collect memory statistics for
 480 bool CompilerOracle::should_collect_memstat() {
 481   return has_command(CompileCommandEnum::MemStat) || has_command(CompileCommandEnum::MemLimit);
 482 }
 483 
 484 bool CompilerOracle::should_log(const methodHandle& method) {
 485   if (!LogCompilation) return false;
 486   if (!has_command(CompileCommandEnum::Log)) {
 487     return true;  // by default, log all
 488   }
 489   return (check_predicate(CompileCommandEnum::Log, method));
 490 }
 491 
 492 bool CompilerOracle::should_break_at(const methodHandle& method) {
 493   return check_predicate(CompileCommandEnum::Break, method);
 494 }
 495 
 496 void CompilerOracle::tag_blackhole_if_possible(const methodHandle& method) {
 497   if (!check_predicate(CompileCommandEnum::Blackhole, method)) {
 498     return;
 499   }
 500   guarantee(UnlockExperimentalVMOptions, "Checked during initial parsing");
 501   if (method->result_type() != T_VOID) {
 502     warning("Blackhole compile option only works for methods with void type: %s",
 503             method->name_and_sig_as_C_string());
 504     return;
 505   }
 506   if (!method->is_empty_method()) {
 507     warning("Blackhole compile option only works for empty methods: %s",
 508             method->name_and_sig_as_C_string());
 509     return;
 510   }
 511   if (!method->is_static()) {
 512     warning("Blackhole compile option only works for static methods: %s",
 513             method->name_and_sig_as_C_string());
 514     return;
 515   }
 516   if (method->intrinsic_id() == vmIntrinsics::_blackhole) {
 517     return;
 518   }
 519   if (method->intrinsic_id() != vmIntrinsics::_none) {
 520     warning("Blackhole compile option only works for methods that do not have intrinsic set: %s, %s",
 521             method->name_and_sig_as_C_string(), vmIntrinsics::name_at(method->intrinsic_id()));
 522     return;
 523   }
 524   method->set_intrinsic_id(vmIntrinsics::_blackhole);
 525 }
 526 
 527 static CompileCommandEnum match_option_name(const char* line, int* bytes_read, char* errorbuf, int bufsize) {
 528   assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommandEnum::Count), "option_names size mismatch");
 529 
 530   *bytes_read = 0;
 531   char option_buf[256];
 532   int matches = sscanf(line, "%255[a-zA-Z0-9]%n", option_buf, bytes_read);
 533   if (matches > 0 && strcasecmp(option_buf, "unknown") != 0) {
 534     for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
 535       if (strcasecmp(option_buf, option_names[i]) == 0) {
 536         return static_cast<CompileCommandEnum>(i);
 537       }
 538     }
 539   }
 540   jio_snprintf(errorbuf, bufsize, "Unrecognized option '%s'", option_buf);
 541   return CompileCommandEnum::Unknown;
 542 }
 543 
 544 // match exactly and don't mess with errorbuf
 545 CompileCommandEnum CompilerOracle::parse_option_name(const char* line) {
 546   for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {
 547     if (strcasecmp(line, option_names[i]) == 0) {
 548       return static_cast<CompileCommandEnum>(i);
 549     }
 550   }
 551   return CompileCommandEnum::Unknown;
 552 }
 553 
 554 enum OptionType CompilerOracle::parse_option_type(const char* type_str) {
 555   for (uint i = 0; i < ARRAY_SIZE(optiontype_names); i++) {
 556     if (strcasecmp(type_str, optiontype_names[i]) == 0) {
 557       return static_cast<enum OptionType>(i);
 558     }
 559   }
 560   return OptionType::Unknown;
 561 }
 562 
 563 static void print_tip() { // CMH Update info
 564   tty->cr();
 565   tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>' - to set boolean option to true");
 566   tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>,<value>'");
 567   tty->print_cr("Use:   '-XX:CompileCommand=help' for more information and to list all option.");
 568   tty->cr();
 569 }
 570 
 571 static void print_option(CompileCommandEnum option, const char* name, enum OptionType type) {
 572   if (type != OptionType::Unknown) {
 573     tty->print_cr("    %s (%s)", name, optiontype2name(type));
 574   }
 575 }
 576 
 577 static void print_commands() {
 578   tty->cr();
 579   tty->print_cr("All available options:");
 580 #define enum_of_options(option, name, ctype) print_option(CompileCommandEnum::option, name, OptionType::ctype);
 581   COMPILECOMMAND_OPTIONS(enum_of_options)
 582 #undef enum_of_options
 583   tty->cr();
 584 }
 585 
 586 static void usage() {
 587   tty->cr();
 588   tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
 589   tty->print_cr("behavior of the dynamic compilers.");
 590   tty->cr();
 591   tty->print_cr("Compile commands has this general form:");
 592   tty->print_cr("-XX:CompileCommand=<option><method pattern><value>");
 593   tty->print_cr("    Sets <option> to the specified value for methods matching <method pattern>");
 594   tty->print_cr("    All options are typed");
 595   tty->cr();
 596   tty->print_cr("-XX:CompileCommand=<option><method pattern>");
 597   tty->print_cr("    Sets <option> to true for methods matching <method pattern>");
 598   tty->print_cr("    Only applies to boolean options.");
 599   tty->cr();
 600   tty->print_cr("-XX:CompileCommand=quiet");
 601   tty->print_cr("    Silence the compile command output");
 602   tty->cr();
 603   tty->print_cr("-XX:CompileCommand=help");
 604   tty->print_cr("    Prints this help text");
 605   tty->cr();
 606   print_commands();
 607   tty->cr();
 608     tty->print_cr("Method patterns has the format:");
 609   tty->print_cr("  package/Class.method()");
 610   tty->cr();
 611   tty->print_cr("For backward compatibility this form is also allowed:");
 612   tty->print_cr("  package.Class::method()");
 613   tty->cr();
 614   tty->print_cr("The signature can be separated by an optional whitespace or comma:");
 615   tty->print_cr("  package/Class.method ()");
 616   tty->cr();
 617   tty->print_cr("The class and method identifier can be used together with leading or");
 618   tty->print_cr("trailing *'s for wildcard matching:");
 619   tty->print_cr("  *ackage/Clas*.*etho*()");
 620   tty->cr();
 621   tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
 622   tty->print_cr("  -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
 623   tty->cr();
 624   tty->print_cr("The CompileCommands can be loaded from a file with the flag");
 625   tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
 626   tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
 627   tty->print_cr("Add one command on each line.");
 628   tty->print_cr("  exclude java/*.*");
 629   tty->print_cr("  option java/*.* ReplayInline");
 630   tty->cr();
 631   tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
 632   tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
 633   tty->print_cr("commands to the same method results in undefined behavior.");
 634   tty->cr();
 635   tty->print_cr("The 'exclude' command excludes methods from top-level compilations as well as");
 636   tty->print_cr("from inlining, whereas the 'compileonly' command only excludes methods from");
 637   tty->print_cr("top-level compilations (i.e. they can still be inlined into other compilation units).");
 638   tty->cr();
 639 };
 640 
 641 static int skip_whitespace(char* &line) {
 642   // Skip any leading spaces
 643   int whitespace_read = 0;
 644   sscanf(line, "%*[ \t]%n", &whitespace_read);
 645   line += whitespace_read;
 646   return whitespace_read;
 647 }
 648 
 649 static void skip_comma(char* &line) {
 650   // Skip any leading spaces
 651   if (*line == ',') {
 652     line++;
 653   }
 654 }
 655 
 656 static bool parseMemLimit(const char* line, intx& value, int& bytes_read, char* errorbuf, const int buf_size) {
 657   // Format:
 658   // "<memory size>['~' <suboption>]"
 659   // <memory size> can have units, e.g. M
 660   // <suboption> one of "crash" "stop", if omitted, "stop" is implied.
 661   //
 662   // Examples:
 663   // -XX:CompileCommand='memlimit,*.*,20m'
 664   // -XX:CompileCommand='memlimit,*.*,20m~stop'
 665   // -XX:CompileCommand='memlimit,Option::toString,1m~crash'
 666   //
 667   // The resulting intx carries the size and whether we are to stop or crash:
 668   // - neg. value means crash
 669   // - pos. value (default) means stop
 670   size_t s = 0;
 671   char* end;
 672   if (!parse_integer<size_t>(line, &end, &s)) {
 673     jio_snprintf(errorbuf, buf_size, "MemLimit: invalid value");
 674     return false;
 675   }
 676   bytes_read = (int)(end - line);
 677 
 678   intx v = (intx)s;
 679   if ((*end) != '\0') {
 680     if (strncasecmp(end, "~crash", 6) == 0) {
 681       v = -v;
 682       bytes_read += 6;
 683     } else if (strncasecmp(end, "~stop", 5) == 0) {
 684       // ok, this is the default
 685       bytes_read += 5;
 686     } else {
 687       jio_snprintf(errorbuf, buf_size, "MemLimit: invalid option");
 688       return false;
 689     }
 690   }
 691   value = v;
 692   return true;
 693 }
 694 
 695 static bool parseMemStat(const char* line, uintx& value, int& bytes_read, char* errorbuf, const int buf_size) {
 696 
 697 #define IF_ENUM_STRING(S, CMD)                \
 698   if (strncasecmp(line, S, strlen(S)) == 0) { \
 699     bytes_read += (int)strlen(S);             \
 700     CMD                                       \
 701     return true;                              \
 702   }
 703 
 704   IF_ENUM_STRING("collect", {
 705     value = (uintx)MemStatAction::collect;
 706   });
 707   IF_ENUM_STRING("print", {
 708     value = (uintx)MemStatAction::print;
 709   });
 710 #undef IF_ENUM_STRING
 711 
 712   jio_snprintf(errorbuf, buf_size, "MemStat: invalid option");
 713 
 714   return false;
 715 }
 716 
 717 static void scan_value(enum OptionType type, char* line, int& total_bytes_read,
 718         TypedMethodOptionMatcher* matcher, CompileCommandEnum option, char* errorbuf, const int buf_size) {
 719   int bytes_read = 0;
 720   const char* ccname = option2name(option);
 721   const char* type_str = optiontype2name(type);
 722   int skipped = skip_whitespace(line);
 723   total_bytes_read += skipped;
 724   if (type == OptionType::Intx) {
 725     intx value;
 726     bool success = false;
 727     if (option == CompileCommandEnum::MemLimit) {
 728       // Special parsing for MemLimit
 729       success = parseMemLimit(line, value, bytes_read, errorbuf, buf_size);
 730     } else {
 731       // Is it a raw number?
 732       success = sscanf(line, "%zd%n", &value, &bytes_read) == 1;
 733     }
 734     if (success) {
 735       total_bytes_read += bytes_read;
 736       line += bytes_read;
 737       register_command(matcher, option, value);
 738       return;
 739     } else {
 740       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 741     }
 742   } else if (type == OptionType::Uintx) {
 743     uintx value;
 744     bool success = false;
 745     if (option == CompileCommandEnum::MemStat) {
 746       // Special parsing for MemStat
 747       success = parseMemStat(line, value, bytes_read, errorbuf, buf_size);
 748     } else {
 749       // parse as raw number
 750       success = sscanf(line, "%zu%n", &value, &bytes_read) == 1;
 751     }
 752     if (success) {
 753       total_bytes_read += bytes_read;
 754       line += bytes_read;
 755       register_command(matcher, option, value);
 756     } else {
 757       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 758     }
 759   } else if (type == OptionType::Ccstr) {
 760     ResourceMark rm;
 761     char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 762     if (sscanf(line, "%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 763       total_bytes_read += bytes_read;
 764       line += bytes_read;
 765       register_command(matcher, option, (ccstr) value);
 766       return;
 767     } else {
 768       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 769     }
 770   } else if (type == OptionType::Ccstrlist) {
 771     // Accumulates several strings into one. The internal type is ccstr.
 772     ResourceMark rm;
 773     char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 774     char* next_value = value;
 775     if (sscanf(line, "%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
 776       total_bytes_read += bytes_read;
 777       line += bytes_read;
 778       next_value += bytes_read + 1;
 779       char* end_value = next_value - 1;
 780       while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {
 781         total_bytes_read += bytes_read;
 782         line += bytes_read;
 783         *end_value = ' '; // override '\0'
 784         next_value += bytes_read;
 785         end_value = next_value-1;
 786       }
 787 
 788       if (option == CompileCommandEnum::ControlIntrinsic || option == CompileCommandEnum::DisableIntrinsic) {
 789         ControlIntrinsicValidator validator(value, (option == CompileCommandEnum::DisableIntrinsic));
 790 
 791         if (!validator.is_valid()) {
 792           jio_snprintf(errorbuf, buf_size, "Unrecognized intrinsic detected in %s: %s", option2name(option), validator.what());
 793         }
 794       }
 795 #if !defined(PRODUCT) && defined(COMPILER2)
 796       else if (option == CompileCommandEnum::TraceAutoVectorization) {
 797         TraceAutoVectorizationTagValidator validator(value, true);
 798 
 799         if (!validator.is_valid()) {
 800           jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
 801         }
 802       } else if (option == CompileCommandEnum::TraceMergeStores) {
 803         TraceMergeStores::TagValidator validator(value, true);
 804 
 805         if (!validator.is_valid()) {
 806           jio_snprintf(errorbuf, buf_size, "Unrecognized tag name in %s: %s", option2name(option), validator.what());
 807         }
 808       } else if (option == CompileCommandEnum::PrintIdealPhase) {
 809         PhaseNameValidator validator(value);
 810 
 811         if (!validator.is_valid()) {
 812           jio_snprintf(errorbuf, buf_size, "Unrecognized phase name in %s: %s", option2name(option), validator.what());
 813         }
 814       } else if (option == CompileCommandEnum::TestOptionList) {
 815         // all values are ok
 816       }
 817 #endif
 818       else {
 819         assert(false, "Ccstrlist type option missing validator");
 820       }
 821 
 822       register_command(matcher, option, (ccstr) value);
 823       return;
 824     } else {
 825       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 826     }
 827   } else if (type == OptionType::Bool) {
 828     char value[256];
 829     if (*line == '\0') {
 830       // Short version of a CompileCommand sets a boolean Option to true
 831       // -XXCompileCommand=<Option>,<method pattern>
 832       register_command(matcher, option, true);
 833       return;
 834     }
 835     if (sscanf(line, "%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 836       if (strcasecmp(value, "true") == 0) {
 837         total_bytes_read += bytes_read;
 838         line += bytes_read;
 839         register_command(matcher, option, true);
 840         return;
 841       } else if (strcasecmp(value, "false") == 0) {
 842         total_bytes_read += bytes_read;
 843         line += bytes_read;
 844         register_command(matcher, option, false);
 845         return;
 846       } else {
 847         jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 848       }
 849     } else {
 850       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 851     }
 852   } else if (type == OptionType::Double) {
 853     char buffer[2][256];
 854     // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 855     // so read integer and fraction part of double value separately.
 856     if (sscanf(line, "%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 857       char value[512] = "";
 858       jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
 859       total_bytes_read += bytes_read;
 860       line += bytes_read;
 861       register_command(matcher, option, atof(value));
 862       return;
 863     } else {
 864       jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);
 865     }
 866   } else {
 867     jio_snprintf(errorbuf, buf_size, "Type '%s' not supported ", type_str);
 868   }
 869 }
 870 
 871 // Scan next option and value in line, return MethodMatcher object on success, nullptr on failure.
 872 // On failure, error_msg contains description for the first error.
 873 // For future extensions: set error_msg on first error.
 874 static void scan_option_and_value(enum OptionType type, char* line, int& total_bytes_read,
 875                                 TypedMethodOptionMatcher* matcher,
 876                                 char* errorbuf, const int buf_size) {
 877   total_bytes_read = 0;
 878   int bytes_read = 0;
 879   char option_buf[256];
 880 
 881   // Read option name.
 882   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option_buf, &bytes_read) == 1) {
 883     line += bytes_read;
 884     total_bytes_read += bytes_read;
 885     int bytes_read2 = 0;
 886     total_bytes_read += skip_whitespace(line);
 887     CompileCommandEnum option = match_option_name(option_buf, &bytes_read2, errorbuf, buf_size);
 888     if (option == CompileCommandEnum::Unknown) {
 889       assert(*errorbuf != '\0', "error must have been set");
 890       return;
 891     }
 892     enum OptionType optiontype = option2type(option);
 893     if (option2type(option) != type) {
 894       const char* optiontype_name = optiontype2name(optiontype);
 895       const char* type_name = optiontype2name(type);
 896       jio_snprintf(errorbuf, buf_size, "Option '%s' with type '%s' doesn't match supplied type '%s'", option_buf, optiontype_name, type_name);
 897       return;
 898     }
 899     scan_value(type, line, total_bytes_read, matcher, option, errorbuf, buf_size);
 900   } else {
 901     const char* type_str = optiontype2name(type);
 902     jio_snprintf(errorbuf, buf_size, "Option name for type '%s' should be alphanumeric ", type_str);
 903   }
 904   return;
 905 }
 906 
 907 void CompilerOracle::print_parse_error(char* error_msg, char* original_line) {
 908   assert(*error_msg != '\0', "Must have error_message");
 909   ttyLocker ttyl;
 910   tty->print_cr("CompileCommand: An error occurred during parsing");
 911   tty->print_cr("Error: %s", error_msg);
 912   tty->print_cr("Line: '%s'", original_line);
 913   print_tip();
 914 }
 915 
 916 class LineCopy : StackObj {
 917   const char* _copy;
 918 public:
 919     LineCopy(char* line) {
 920       _copy = os::strdup(line, mtInternal);
 921     }
 922     ~LineCopy() {
 923       os::free((void*)_copy);
 924     }
 925     char* get() {
 926       return (char*)_copy;
 927     }
 928 };
 929 
 930 bool CompilerOracle::parse_from_line_quietly(char* line) {
 931   const bool quiet0 = _quiet;
 932   _quiet = true;
 933   const bool result = parse_from_line(line);
 934   _quiet = quiet0;
 935   return result;
 936 }
 937 
 938 bool CompilerOracle::parse_from_line(char* line) {
 939   if ((line[0] == '\0') || (line[0] == '#')) {
 940     return true;
 941   }
 942 
 943   LineCopy original(line);
 944   int bytes_read;
 945   char error_buf[1024] = {0};
 946 
 947   CompileCommandEnum option = match_option_name(line, &bytes_read, error_buf, sizeof(error_buf));
 948   line += bytes_read;
 949   ResourceMark rm;
 950 
 951   if (option == CompileCommandEnum::Unknown) {
 952     print_parse_error(error_buf, original.get());
 953     return false;
 954   }
 955 
 956   if (option == CompileCommandEnum::Quiet) {
 957     _quiet = true;
 958     return true;
 959   }
 960 
 961   if (option == CompileCommandEnum::Help) {
 962     usage();
 963     return true;
 964   }
 965 
 966   if (option == CompileCommandEnum::Option) {
 967     // Look for trailing options.
 968     //
 969     // Two types of trailing options are
 970     // supported:
 971     //
 972     // (1) CompileCommand=option,Klass::method,option
 973     // (2) CompileCommand=option,Klass::method,type,option,value
 974     //
 975     // Type (1) is used to enable a boolean option for a method.
 976     //
 977     // Type (2) is used to support options with a value. Values can have the
 978     // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 979 
 980     char option_type[256]; // stores option for Type (1) and type of Type (2)
 981     skip_comma(line);
 982     TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
 983     if (archetype == nullptr) {
 984       print_parse_error(error_buf, original.get());
 985       return false;
 986     }
 987 
 988     skip_whitespace(line);
 989 
 990     // This is unnecessarily complex. Should retire multi-option lines and skip while loop
 991     while (sscanf(line, "%255[a-zA-Z0-9]%n", option_type, &bytes_read) == 1) {
 992       line += bytes_read;
 993 
 994       // typed_matcher is used as a blueprint for each option, deleted at the end
 995       TypedMethodOptionMatcher* typed_matcher = archetype->clone();
 996       enum OptionType type = parse_option_type(option_type);
 997       if (type != OptionType::Unknown) {
 998         // Type (2) option: parse option name and value.
 999         scan_option_and_value(type, line, bytes_read, typed_matcher, error_buf, sizeof(error_buf));
1000         if (*error_buf != '\0') {
1001           print_parse_error(error_buf, original.get());
1002           return false;
1003         }
1004         line += bytes_read;
1005       } else {
1006         // Type (1) option - option_type contains the option name -> bool value = true is implied
1007         int bytes_read;
1008         CompileCommandEnum option = match_option_name(option_type, &bytes_read, error_buf, sizeof(error_buf));
1009         if (option == CompileCommandEnum::Unknown) {
1010           print_parse_error(error_buf, original.get());
1011           return false;
1012         }
1013         if (option2type(option) == OptionType::Bool) {
1014           register_command(typed_matcher, option, true);
1015         } else {
1016           jio_snprintf(error_buf, sizeof(error_buf), "  Missing type '%s' before option '%s'",
1017                        optiontype2name(option2type(option)), option2name(option));
1018           print_parse_error(error_buf, original.get());
1019           return false;
1020         }
1021       }
1022       assert(typed_matcher != nullptr, "sanity");
1023       assert(*error_buf == '\0', "No error here");
1024       skip_whitespace(line);
1025     } // while(
1026     delete archetype;
1027   } else {  // not an OptionCommand
1028     // Command has the following form:
1029     // CompileCommand=<option>,<method pattern><value>
1030     // CompileCommand=<option>,<method pattern>     (implies option is bool and value is true)
1031     assert(*error_buf == '\0', "Don't call here with error_buf already set");
1032     enum OptionType type = option2type(option);
1033     int bytes_read = 0;
1034     skip_comma(line);
1035     TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));
1036     if (matcher == nullptr) {
1037       print_parse_error(error_buf, original.get());
1038       return false;
1039     }
1040     skip_whitespace(line);
1041     if (*line == '\0') {
1042       if (option2type(option) == OptionType::Bool) {
1043         // if this is a bool option this implies true
1044         register_command(matcher, option, true);
1045         return true;
1046       } else if (option == CompileCommandEnum::MemStat) {
1047         // MemStat default action is to collect data but to not print
1048         register_command(matcher, option, (uintx)MemStatAction::collect);
1049         return true;
1050       } else {
1051         jio_snprintf(error_buf, sizeof(error_buf), "  Option '%s' is not followed by a value", option2name(option));
1052         print_parse_error(error_buf, original.get());
1053         return false;
1054       }
1055     }
1056     scan_value(type, line, bytes_read, matcher, option, error_buf, sizeof(error_buf));
1057     if (*error_buf != '\0') {
1058       print_parse_error(error_buf, original.get());
1059       return false;
1060     }
1061     assert(matcher != nullptr, "consistency");
1062   }
1063   return true;
1064 }
1065 
1066 static const char* default_cc_file = ".hotspot_compiler";
1067 
1068 static const char* cc_file() {
1069 #ifdef ASSERT
1070   if (CompileCommandFile == nullptr)
1071     return default_cc_file;
1072 #endif
1073   return CompileCommandFile;
1074 }
1075 
1076 bool CompilerOracle::has_command_file() {
1077   return cc_file() != nullptr;
1078 }
1079 
1080 bool CompilerOracle::_quiet = false;
1081 
1082 bool CompilerOracle::parse_from_file() {
1083   assert(has_command_file(), "command file must be specified");
1084   FILE* stream = os::fopen(cc_file(), "rt");
1085   if (stream == nullptr) {
1086     return true;
1087   }
1088 
1089   FileInput input(stream, /*need_close=*/ true);
1090   return parse_from_input(&input, parse_from_line);
1091 }
1092 
1093 bool CompilerOracle::parse_from_input(inputStream::Input* input,
1094                                       CompilerOracle::
1095                                       parse_from_line_fn_t* parse_from_line) {
1096   bool success = true;
1097   for (inputStream in(input); !in.done(); in.next()) {
1098     if (!parse_from_line(in.current_line())) {
1099       success = false;
1100     }
1101   }
1102   return success;
1103 }
1104 
1105 bool CompilerOracle::parse_from_string(const char* str,
1106                                        CompilerOracle::
1107                                        parse_from_line_fn_t* parse_from_line) {
1108   MemoryInput input(str, strlen(str));
1109   return parse_from_input(&input, parse_from_line);
1110 }
1111 
1112 bool compilerOracle_init() {
1113   bool success = true;
1114   // Register default compile commands first - any commands specified via CompileCommand will
1115   // supersede these default commands.
1116   for (int i = 0; default_compile_commands[i] != nullptr; i ++) {
1117     char* s = os::strdup(default_compile_commands[i]);
1118     success = CompilerOracle::parse_from_line_quietly(s);
1119     os::free(s);
1120     assert(success, "default compile command \"%s\" failed to parse", default_compile_commands[i]);
1121   }
1122   if (!CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line)) {
1123     success = false;
1124   }
1125   if (!CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only)) {
1126     success = false;
1127   }
1128   if (CompilerOracle::has_command_file()) {
1129     if (!CompilerOracle::parse_from_file()) {
1130       success = false;
1131     }
1132   } else {
1133     struct stat buf;
1134     if (os::stat(default_cc_file, &buf) == 0) {
1135       warning("%s file is present but has been ignored.  "
1136               "Run with -XX:CompileCommandFile=%s to load the file.",
1137               default_cc_file, default_cc_file);
1138     }
1139   }
1140   if (has_command(CompileCommandEnum::Print)) {
1141     if (PrintAssembly) {
1142       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
1143     }
1144   }
1145   return success;
1146 }
1147 
1148 bool CompilerOracle::parse_compile_only(char* line) {
1149   if (line[0] == '\0') {
1150     return true;
1151   }
1152   ResourceMark rm;
1153   char error_buf[1024] = {0};
1154   LineCopy original(line);
1155   char* method_pattern;
1156   do {
1157     if (line[0] == '\0') {
1158       break;
1159     }
1160     method_pattern = strtok_r(line, ",", &line);
1161     if (method_pattern != nullptr) {
1162       TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(method_pattern, error_buf, sizeof(error_buf));
1163       if (matcher != nullptr) {
1164         register_command(matcher, CompileCommandEnum::CompileOnly, true);
1165         continue;
1166       }
1167     }
1168     ttyLocker ttyl;
1169     tty->print_cr("CompileOnly: An error occurred during parsing");
1170     if (*error_buf != '\0') {
1171       tty->print_cr("Error: %s", error_buf);
1172     }
1173     tty->print_cr("Line: '%s'", original.get());
1174     return false;
1175   } while (method_pattern != nullptr && line != nullptr);
1176   return true;
1177 }
1178 
1179 CompileCommandEnum CompilerOracle::string_to_option(const char* name) {
1180   int bytes_read = 0;
1181   char errorbuf[1024] = {0};
1182   return match_option_name(name, &bytes_read, errorbuf, sizeof(errorbuf));
1183 }