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