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