1 /*
   2  * Copyright (c) 1998, 2020, 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 "precompiled.hpp"
  26 #include "ci/ciMethod.hpp"
  27 #include "ci/ciUtilities.inline.hpp"
  28 #include "compiler/abstractCompiler.hpp"
  29 #include "compiler/compilerDirectives.hpp"
  30 #include "compiler/compilerOracle.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 
  34 CompilerDirectives::CompilerDirectives() : _next(NULL), _match(NULL), _ref_count(0) {
  35   _c1_store = new DirectiveSet(this);
  36   _c1_store->init_control_intrinsic();
  37   _c2_store = new DirectiveSet(this);
  38   _c2_store->init_control_intrinsic();
  39 };
  40 
  41 CompilerDirectives::~CompilerDirectives() {
  42   if (_c1_store != NULL) {
  43     delete _c1_store;
  44   }
  45   if (_c2_store != NULL) {
  46     delete _c2_store;
  47   }
  48 
  49   // remove all linked method matchers
  50   BasicMatcher* tmp = _match;
  51   while (tmp != NULL) {
  52     BasicMatcher* next = tmp->next();
  53     delete tmp;
  54     tmp = next;
  55   }
  56 }
  57 
  58 void CompilerDirectives::print(outputStream* st) {
  59   assert(DirectivesStack_lock->owned_by_self(), "");
  60   if (_match != NULL) {
  61     st->cr();
  62     st->print("Directive:");
  63     if (is_default_directive()) {
  64       st->print_cr(" (default)");
  65     } else {
  66       st->cr();
  67     }
  68     st->print(" matching: ");
  69     _match->print(st);
  70     BasicMatcher* tmp = _match->next();
  71     while (tmp != NULL) {
  72       st->print(", ");
  73       tmp->print(st);
  74       tmp = tmp->next();
  75     }
  76     st->cr();
  77   } else {
  78     assert(0, "There should always be a match");
  79   }
  80 
  81   if (_c1_store != NULL) {
  82     st->print_cr(" c1 directives:");
  83     _c1_store->print(st);
  84   }
  85   if (_c2_store != NULL) {
  86     st->cr();
  87     st->print_cr(" c2 directives:");
  88     _c2_store->print(st);
  89   }
  90   //---
  91 }
  92 
  93 void CompilerDirectives::finalize(outputStream* st) {
  94   if (_c1_store != NULL) {
  95     _c1_store->finalize(st);
  96   }
  97   if (_c2_store != NULL) {
  98     _c2_store->finalize(st);
  99   }
 100 }
 101 
 102 void DirectiveSet::finalize(outputStream* st) {
 103   // Check LogOption and warn
 104   if (LogOption && !LogCompilation) {
 105     st->print_cr("Warning:  +LogCompilation must be set to enable compilation logging from directives");
 106   }
 107 
 108   // if any flag has been modified - set directive as enabled
 109   // unless it already has been explicitly set.
 110   if (!_modified[EnableIndex]) {
 111     if (_inlinematchers != NULL) {
 112       EnableOption = true;
 113       return;
 114     }
 115     int i;
 116     for (i = 0; i < number_of_flags; i++) {
 117       if (_modified[i]) {
 118         EnableOption = true;
 119         return;
 120       }
 121     }
 122   }
 123 }
 124 
 125 CompilerDirectives* CompilerDirectives::next() {
 126   return _next;
 127 }
 128 
 129 bool CompilerDirectives::match(const methodHandle& method) {
 130   if (is_default_directive()) {
 131     return true;
 132   }
 133   if (method == NULL) {
 134     return false;
 135   }
 136   if (_match->match(method)) {
 137     return true;
 138   }
 139   return false;
 140 }
 141 
 142 bool CompilerDirectives::add_match(char* str, const char*& error_msg) {
 143   BasicMatcher* bm = BasicMatcher::parse_method_pattern(str, error_msg);
 144   if (bm == NULL) {
 145     assert(error_msg != NULL, "Must have error message");
 146     return false;
 147   } else {
 148     bm->set_next(_match);
 149     _match = bm;
 150     return true;
 151   }
 152 }
 153 
 154 void CompilerDirectives::inc_refcount() {
 155   assert(DirectivesStack_lock->owned_by_self(), "");
 156   _ref_count++;
 157 }
 158 
 159 void CompilerDirectives::dec_refcount() {
 160   assert(DirectivesStack_lock->owned_by_self(), "");
 161   _ref_count--;
 162 }
 163 
 164 int CompilerDirectives::refcount() {
 165   assert(DirectivesStack_lock->owned_by_self(), "");
 166   return _ref_count;
 167 }
 168 
 169 DirectiveSet* CompilerDirectives::get_for(AbstractCompiler *comp) {
 170   assert(DirectivesStack_lock->owned_by_self(), "");
 171   if (comp == NULL) { // Xint
 172     return _c1_store;
 173   } else  if (comp->is_c2()) {
 174     return _c2_store;
 175   } else {
 176     // use c1_store as default
 177     assert(comp->is_c1() || comp->is_jvmci(), "");
 178     return _c1_store;
 179   }
 180 }
 181 
 182 // In the list of Control/disabled intrinsics, the ID of the control intrinsics can separated:
 183 // - by ',' (if -XX:Control/DisableIntrinsic is used once when invoking the VM) or
 184 // - by '\n' (if -XX:Control/DisableIntrinsic is used multiple times when invoking the VM) or
 185 // - by ' ' (if Control/DisableIntrinsic is used on a per-method level, e.g., with CompileCommand).
 186 //
 187 // To simplify the processing of the list, the canonicalize_control_intrinsic() method
 188 // returns a new copy of the list in which '\n' and ' ' is replaced with ','.
 189 ccstrlist DirectiveSet::canonicalize_control_intrinsic(ccstrlist option_value) {
 190   char* canonicalized_list = NEW_C_HEAP_ARRAY(char, strlen(option_value) + 1, mtCompiler);
 191   int i = 0;
 192   char current;
 193   while ((current = option_value[i]) != '\0') {
 194     if (current == '\n' || current == ' ') {
 195       canonicalized_list[i] = ',';
 196     } else {
 197       canonicalized_list[i] = current;
 198     }
 199     i++;
 200   }
 201   canonicalized_list[i] = '\0';
 202   return canonicalized_list;
 203 }
 204 
 205 ControlIntrinsicIter::ControlIntrinsicIter(ccstrlist option_value, bool disable_all)
 206   : _disableIntrinsic(disable_all) {
 207   _list = (char*)DirectiveSet::canonicalize_control_intrinsic(option_value);
 208   _saved_ptr = _list;
 209   _enabled = false;
 210 
 211   _token = strtok_r(_saved_ptr, ",", &_saved_ptr);
 212   next_token();
 213 }
 214 
 215 ControlIntrinsicIter::~ControlIntrinsicIter() {
 216   FREE_C_HEAP_ARRAY(char, _list);
 217 }
 218 
 219 // pre-increment
 220 ControlIntrinsicIter& ControlIntrinsicIter::operator++() {
 221   _token = strtok_r(NULL, ",", &_saved_ptr);
 222   next_token();
 223   return *this;
 224 }
 225 
 226 void ControlIntrinsicIter::next_token() {
 227   if (_token && !_disableIntrinsic) {
 228     char ch = _token[0];
 229 
 230     if (ch != '+' && ch != '-') {
 231       warning("failed to parse %s. must start with +/-!", _token);
 232     } else {
 233       _enabled = ch == '+';
 234       _token++;
 235     }
 236   }
 237 }
 238 
 239 void DirectiveSet::init_control_intrinsic() {
 240   for (ControlIntrinsicIter iter(ControlIntrinsic); *iter != NULL; ++iter) {
 241     vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
 242 
 243     if (id != vmIntrinsics::_none) {
 244       _intrinsic_control_words[id] = iter.is_enabled();
 245     }
 246   }
 247 
 248   // Order matters, DisableIntrinsic can overwrite ControlIntrinsic
 249   for (ControlIntrinsicIter iter(DisableIntrinsic, true/*disable_all*/); *iter != NULL; ++iter) {
 250     vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
 251 
 252     if (id != vmIntrinsics::_none) {
 253       _intrinsic_control_words[id] = false;
 254     }
 255   }
 256 }
 257 
 258 DirectiveSet::DirectiveSet(CompilerDirectives* d) :_inlinematchers(NULL), _directive(d) {
 259 #define init_defaults_definition(name, type, dvalue, compiler) this->name##Option = dvalue;
 260   compilerdirectives_common_flags(init_defaults_definition)
 261   compilerdirectives_c2_flags(init_defaults_definition)
 262   compilerdirectives_c1_flags(init_defaults_definition)
 263   memset(_modified, 0, sizeof(_modified));
 264   _intrinsic_control_words.fill_in(/*default value*/TriBool());
 265 }
 266 
 267 DirectiveSet::~DirectiveSet() {
 268   // remove all linked methodmatchers
 269   InlineMatcher* tmp = _inlinematchers;
 270   while (tmp != NULL) {
 271     InlineMatcher* next = tmp->next();
 272     delete tmp;
 273     tmp = next;
 274   }
 275 }
 276 
 277 // Backward compatibility for CompileCommands
 278 // Breaks the abstraction and causes lots of extra complexity
 279 // - if some option is changed we need to copy directiveset since it no longer can be shared
 280 // - Need to free copy after use
 281 // - Requires a modified bit so we don't overwrite options that is set by directives
 282 
 283 DirectiveSet* DirectiveSet::compilecommand_compatibility_init(const methodHandle& method) {
 284   // Early bail out - checking all options is expensive - we rely on them not being used
 285   // Only set a flag if it has not been modified and value changes.
 286   // Only copy set if a flag needs to be set
 287   if (!CompilerDirectivesIgnoreCompileCommandsOption && CompilerOracle::has_any_option()) {
 288     DirectiveSet* set = DirectiveSet::clone(this);
 289 
 290     bool changed = false; // Track if we actually change anything
 291 
 292     // All CompileCommands are not equal so this gets a bit verbose
 293     // When CompileCommands have been refactored less clutter will remain.
 294     if (CompilerOracle::should_break_at(method)) {
 295       if (!_modified[BreakAtCompileIndex]) {
 296         set->BreakAtCompileOption = true;
 297         changed = true;
 298       }
 299       if (!_modified[BreakAtExecuteIndex]) {
 300         set->BreakAtExecuteOption = true;
 301         changed = true;
 302       }
 303     }
 304     if (!_modified[LogIndex]) {
 305       bool log = CompilerOracle::should_log(method);
 306       if (log != set->LogOption) {
 307         set->LogOption = log;
 308         changed = true;
 309       }
 310     }
 311 
 312     if (CompilerOracle::should_print(method)) {
 313       if (!_modified[PrintAssemblyIndex]) {
 314         set->PrintAssemblyOption = true;
 315         changed = true;
 316       }
 317     }
 318     // Exclude as in should not compile == Enabled
 319     if (CompilerOracle::should_exclude(method)) {
 320       if (!_modified[ExcludeIndex]) {
 321         set->ExcludeOption = true;
 322         changed = true;
 323       }
 324     }
 325 
 326     // inline and dontinline (including exclude) are implemented in the directiveset accessors
 327 #define init_default_cc(name, type, dvalue, cc_flag) { type v; if (!_modified[name##Index] && CompilerOracle::has_option_value(method, #cc_flag, v) && v != this->name##Option) { set->name##Option = v; changed = true;} }
 328     compilerdirectives_common_flags(init_default_cc)
 329     compilerdirectives_c2_flags(init_default_cc)
 330     compilerdirectives_c1_flags(init_default_cc)
 331 
 332     // Canonicalize DisableIntrinsic to contain only ',' as a separator.
 333     ccstrlist option_value;
 334     bool need_reset = true; // if Control/DisableIntrinsic redefined, only need to reset control_words once
 335 
 336     if (!_modified[ControlIntrinsicIndex] &&
 337         CompilerOracle::has_option_value(method, "ControlIntrinsic", option_value)) {
 338       ControlIntrinsicIter iter(option_value);
 339 
 340       if (need_reset) {
 341         set->_intrinsic_control_words.fill_in(TriBool());
 342         need_reset = false;
 343       }
 344 
 345       while (*iter != NULL) {
 346         vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
 347         if (id != vmIntrinsics::_none) {
 348           set->_intrinsic_control_words[id] = iter.is_enabled();
 349         }
 350 
 351         ++iter;
 352       }
 353     }
 354 
 355 
 356     if (!_modified[DisableIntrinsicIndex] &&
 357         CompilerOracle::has_option_value(method, "DisableIntrinsic", option_value)) {
 358       ControlIntrinsicIter iter(option_value, true/*disable_all*/);
 359 
 360       if (need_reset) {
 361         set->_intrinsic_control_words.fill_in(TriBool());
 362         need_reset = false;
 363       }
 364 
 365       while (*iter != NULL) {
 366         vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
 367         if (id != vmIntrinsics::_none) {
 368           set->_intrinsic_control_words[id] = false;
 369         }
 370 
 371         ++iter;
 372       }
 373     }
 374 
 375 
 376     if (!changed) {
 377       // We didn't actually update anything, discard.
 378       delete set;
 379     } else {
 380       // We are returning a (parentless) copy. The originals parent don't need to account for this.
 381       DirectivesStack::release(this);
 382       return set;
 383     }
 384   }
 385   // Nothing changed
 386   return this;
 387 }
 388 
 389 CompilerDirectives* DirectiveSet::directive() {
 390   assert(_directive != NULL, "Must have been initialized");
 391   return _directive;
 392 }
 393 
 394 bool DirectiveSet::matches_inline(const methodHandle& method, int inline_action) {
 395   if (_inlinematchers != NULL) {
 396     if (_inlinematchers->match(method, inline_action)) {
 397       return true;
 398     }
 399   }
 400   return false;
 401 }
 402 
 403 bool DirectiveSet::should_inline(ciMethod* inlinee) {
 404   inlinee->check_is_loaded();
 405   VM_ENTRY_MARK;
 406   methodHandle mh(THREAD, inlinee->get_Method());
 407 
 408   if (_inlinematchers != NULL) {
 409     return matches_inline(mh, InlineMatcher::force_inline);
 410   }
 411   if (!CompilerDirectivesIgnoreCompileCommandsOption) {
 412     return CompilerOracle::should_inline(mh);
 413   }
 414   return false;
 415 }
 416 
 417 bool DirectiveSet::should_not_inline(ciMethod* inlinee) {
 418   inlinee->check_is_loaded();
 419   VM_ENTRY_MARK;
 420   methodHandle mh(THREAD, inlinee->get_Method());
 421 
 422   if (_inlinematchers != NULL) {
 423     return matches_inline(mh, InlineMatcher::dont_inline);
 424   }
 425   if (!CompilerDirectivesIgnoreCompileCommandsOption) {
 426     return CompilerOracle::should_not_inline(mh);
 427   }
 428   return false;
 429 }
 430 
 431 bool DirectiveSet::parse_and_add_inline(char* str, const char*& error_msg) {
 432   InlineMatcher* m = InlineMatcher::parse_inline_pattern(str, error_msg);
 433   if (m != NULL) {
 434     // add matcher last in chain - the order is significant
 435     append_inline(m);
 436     return true;
 437   } else {
 438     assert(error_msg != NULL, "Error message must be set");
 439     return false;
 440   }
 441 }
 442 
 443 void DirectiveSet::append_inline(InlineMatcher* m) {
 444   if (_inlinematchers == NULL) {
 445     _inlinematchers = m;
 446     return;
 447   }
 448   InlineMatcher* tmp = _inlinematchers;
 449   while (tmp->next() != NULL) {
 450     tmp = tmp->next();
 451   }
 452   tmp->set_next(m);
 453 }
 454 
 455 void DirectiveSet::print_inline(outputStream* st) {
 456   if (_inlinematchers == NULL) {
 457     st->print_cr("  inline: -");
 458   } else {
 459     st->print("  inline: ");
 460     _inlinematchers->print(st);
 461     InlineMatcher* tmp = _inlinematchers->next();
 462     while (tmp != NULL) {
 463       st->print(", ");
 464       tmp->print(st);
 465       tmp = tmp->next();
 466     }
 467     st->cr();
 468   }
 469 }
 470 
 471 bool DirectiveSet::is_intrinsic_disabled(const methodHandle& method) {
 472   vmIntrinsics::ID id = method->intrinsic_id();
 473   assert(id > vmIntrinsics::_none && id < vmIntrinsics::ID_LIMIT, "invalid intrinsic_id!");
 474 
 475   TriBool b = _intrinsic_control_words[id];
 476   if (b.is_default()) {
 477     return false; // if unset, every intrinsic is enabled.
 478   } else {
 479     return !b;
 480   }
 481 }
 482 
 483 DirectiveSet* DirectiveSet::clone(DirectiveSet const* src) {
 484   DirectiveSet* set = new DirectiveSet(NULL);
 485   // Ordinary allocations of DirectiveSet would call init_control_intrinsic()
 486   // immediately to create a new copy for set->Control/DisableIntrinsicOption.
 487   // However, here it does not need to because the code below creates
 488   // a copy of src->Control/DisableIntrinsicOption that initializes
 489   // set->Control/DisableIntrinsicOption.
 490 
 491   memcpy(set->_modified, src->_modified, sizeof(src->_modified));
 492 
 493   InlineMatcher* tmp = src->_inlinematchers;
 494   while (tmp != NULL) {
 495     set->append_inline(tmp->clone());
 496     tmp = tmp->next();
 497   }
 498 
 499   #define copy_members_definition(name, type, dvalue, cc_flag) set->name##Option = src->name##Option;
 500     compilerdirectives_common_flags(copy_members_definition)
 501     compilerdirectives_c2_flags(copy_members_definition)
 502     compilerdirectives_c1_flags(copy_members_definition)
 503 
 504   set->_intrinsic_control_words = src->_intrinsic_control_words;
 505   return set;
 506 }
 507 
 508 // Create a new dirstack and push a default directive
 509 void DirectivesStack::init() {
 510   CompilerDirectives* _default_directives = new CompilerDirectives();
 511   char str[] = "*.*";
 512   const char* error_msg = NULL;
 513   _default_directives->add_match(str, error_msg);
 514 #if defined(COMPILER1) || INCLUDE_JVMCI
 515   _default_directives->_c1_store->EnableOption = true;
 516 #endif
 517 #ifdef COMPILER2
 518   if (is_server_compilation_mode_vm()) {
 519     _default_directives->_c2_store->EnableOption = true;
 520   }
 521 #endif
 522   assert(error_msg == NULL, "Must succeed.");
 523   push(_default_directives);
 524 }
 525 
 526 DirectiveSet* DirectivesStack::getDefaultDirective(AbstractCompiler* comp) {
 527   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 528 
 529   assert(_bottom != NULL, "Must never be empty");
 530   _bottom->inc_refcount();
 531   return _bottom->get_for(comp);
 532 }
 533 
 534 void DirectivesStack::push(CompilerDirectives* directive) {
 535   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 536 
 537   directive->inc_refcount();
 538   if (_top == NULL) {
 539     assert(_bottom == NULL, "There can only be one default directive");
 540     _bottom = directive; // default directive, can never be removed.
 541   }
 542 
 543   directive->set_next(_top);
 544   _top = directive;
 545   _depth++;
 546 }
 547 
 548 void DirectivesStack::pop(int count) {
 549   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 550   assert(count > -1, "No negative values");
 551   for (int i = 0; i < count; i++) {
 552     pop_inner();
 553   }
 554 }
 555 
 556 void DirectivesStack::pop_inner() {
 557   assert(DirectivesStack_lock->owned_by_self(), "");
 558 
 559   if (_top->next() == NULL) {
 560     return; // Do nothing - don't allow an empty stack
 561   }
 562   CompilerDirectives* tmp = _top;
 563   _top = _top->next();
 564   _depth--;
 565 
 566   DirectivesStack::release(tmp);
 567 }
 568 
 569 bool DirectivesStack::check_capacity(int request_size, outputStream* st) {
 570   if ((request_size + _depth) > CompilerDirectivesLimit) {
 571     st->print_cr("Could not add %i more directives. Currently %i/%i directives.", request_size, _depth, CompilerDirectivesLimit);
 572     return false;
 573   }
 574   return true;
 575 }
 576 
 577 void DirectivesStack::clear() {
 578   // holding the lock during the whole operation ensuring consistent result
 579   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 580   while (_top->next() != NULL) {
 581     pop_inner();
 582   }
 583 }
 584 
 585 void DirectivesStack::print(outputStream* st) {
 586   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 587   CompilerDirectives* tmp = _top;
 588   while (tmp != NULL) {
 589     tmp->print(st);
 590     tmp = tmp->next();
 591     st->cr();
 592   }
 593 }
 594 
 595 void DirectivesStack::release(DirectiveSet* set) {
 596   assert(set != NULL, "Never NULL");
 597   MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 598   if (set->is_exclusive_copy()) {
 599     // Old CompilecCmmands forced us to create an exclusive copy
 600     delete set;
 601   } else {
 602     assert(set->directive() != NULL, "Never NULL");
 603     release(set->directive());
 604   }
 605 }
 606 
 607 
 608 void DirectivesStack::release(CompilerDirectives* dir) {
 609   assert(DirectivesStack_lock->owned_by_self(), "");
 610   dir->dec_refcount();
 611   if (dir->refcount() == 0) {
 612     delete dir;
 613   }
 614 }
 615 
 616 DirectiveSet* DirectivesStack::getMatchingDirective(const methodHandle& method, AbstractCompiler *comp) {
 617   assert(_depth > 0, "Must never be empty");
 618 
 619   DirectiveSet* match = NULL;
 620   {
 621     MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 622 
 623     CompilerDirectives* dir = _top;
 624     assert(dir != NULL, "Must be initialized");
 625 
 626     while (dir != NULL) {
 627       if (dir->is_default_directive() || dir->match(method)) {
 628         match = dir->get_for(comp);
 629         assert(match != NULL, "Consistency");
 630         if (match->EnableOption) {
 631           // The directiveSet for this compile is also enabled -> success
 632           dir->inc_refcount();
 633           break;
 634         }
 635       }
 636       dir = dir->next();
 637     }
 638   }
 639   guarantee(match != NULL, "There should always be a default directive that matches");
 640 
 641   // Check for legacy compile commands update, without DirectivesStack_lock
 642   return match->compilecommand_compatibility_init(method);
 643 }