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