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     if (_inlinematchers != NULL) {
 108       EnableOption = true;
 109       return;
 110     }
 111     int i;
 112     for (i = 0; i < number_of_flags; i++) {
 113       if (_modified[i]) {
 114         EnableOption = true;
 115         return;
 116       }
 117     }
 118   }
 119 }
 120 
 121 CompilerDirectives* CompilerDirectives::next() {
 122   return _next;
 123 }
 124 
 125 bool CompilerDirectives::match(methodHandle method) {
 126   if (is_default_directive()) {
 127     return true;
 128   }
 129   if (method == NULL) {
 130     return false;
 131   }
 132   if (_match->match(method)) {
 133     return true;
 134   }
 135   return false;
 136 }
 137 
 138 bool CompilerDirectives::add_match(char* str, const char*& error_msg) {
 139   BasicMatcher* bm = BasicMatcher::parse_method_pattern(str, error_msg);
 140   if (bm == NULL) {
 141     assert(error_msg != NULL, "Must have error message");
 142     return false;
 143   } else {
 144     bm->set_next(_match);
 145     _match = bm;
 146     return true;
 147   }
 148 }
 149 
 150 void CompilerDirectives::inc_refcount() {
 151   assert(DirectivesStack_lock->owned_by_self(), "");
 152   _ref_count++;
 153 }
 154 
 155 void CompilerDirectives::dec_refcount() {
 156   assert(DirectivesStack_lock->owned_by_self(), "");
 157   _ref_count--;
 158 }
 159 
 160 int CompilerDirectives::refcount() {
 161   assert(DirectivesStack_lock->owned_by_self(), "");
 162   return _ref_count;
 163 }
 164 
 165 DirectiveSet* CompilerDirectives::get_for(AbstractCompiler *comp) {
 166   assert(DirectivesStack_lock->owned_by_self(), "");
 167   if (comp == NULL) { // Xint
 168     return _c1_store;
 169   } else  if (comp->is_c2()) {
 170     return _c2_store;
 171   } else {
 172     // use c1_store as default
 173     assert(comp->is_c1() || comp->is_jvmci() || comp->is_shark(), "");
 174     return _c1_store;
 175   }
 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 (!_modified[LogIndex]) {
 256       bool log = CompilerOracle::should_log(method);
 257       if (log != set->LogOption) {
 258         set->LogOption = log;
 259         changed = true;
 260       }
 261     }
 262 
 263     if (CompilerOracle::should_print(method)) {
 264       if (!_modified[PrintAssemblyIndex]) {
 265         set->PrintAssemblyOption = true;
 266         changed = true;
 267       }
 268     }
 269     // Exclude as in should not compile == Enabled
 270     if (CompilerOracle::should_exclude(method)) {
 271       if (!_modified[ExcludeIndex]) {
 272         set->ExcludeOption = true;
 273         changed = true;
 274       }
 275     }
 276 
 277     // inline and dontinline (including exclude) are implemented in the directiveset accessors
 278 #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;} }
 279     compilerdirectives_common_flags(init_default_cc)
 280     compilerdirectives_c2_flags(init_default_cc)
 281     compilerdirectives_c1_flags(init_default_cc)
 282 
 283     // Canonicalize DisableIntrinsic to contain only ',' as a separator.
 284     ccstrlist option_value;
 285     if (!_modified[DisableIntrinsicIndex] &&
 286         CompilerOracle::has_option_value(method, "DisableIntrinsic", option_value)) {
 287       set->DisableIntrinsicOption = canonicalize_disableintrinsic(option_value);
 288     }
 289 
 290 
 291     if (!changed) {
 292       // We didn't actually update anything, discard.
 293       delete set;
 294     } else {
 295       // We are returning a (parentless) copy. The originals parent don't need to account for this.
 296       DirectivesStack::release(this);
 297       return set;
 298     }
 299   }
 300   // Nothing changed
 301   return this;
 302 }
 303 
 304 CompilerDirectives* DirectiveSet::directive() {
 305   assert(_directive != NULL, "Must have been initialized");
 306   return _directive;
 307 }
 308 
 309 bool DirectiveSet::matches_inline(methodHandle method, int inline_action) {
 310   if (_inlinematchers != NULL) {
 311     if (_inlinematchers->match(method, inline_action)) {
 312       return true;
 313     }
 314   }
 315   return false;
 316 }
 317 
 318 bool DirectiveSet::should_inline(ciMethod* inlinee) {
 319   inlinee->check_is_loaded();
 320   VM_ENTRY_MARK;
 321   methodHandle mh(THREAD, inlinee->get_Method());
 322 
 323   if (_inlinematchers != NULL) {
 324     return matches_inline(mh, InlineMatcher::force_inline);
 325   }
 326   if (!CompilerDirectivesIgnoreCompileCommandsOption) {
 327     return CompilerOracle::should_inline(mh);
 328   }
 329   return false;
 330 }
 331 
 332 bool DirectiveSet::should_not_inline(ciMethod* inlinee) {
 333   inlinee->check_is_loaded();
 334   VM_ENTRY_MARK;
 335   methodHandle mh(THREAD, inlinee->get_Method());
 336 
 337   if (_inlinematchers != NULL) {
 338     return matches_inline(mh, InlineMatcher::dont_inline);
 339   }
 340   if (!CompilerDirectivesIgnoreCompileCommandsOption) {
 341     return CompilerOracle::should_not_inline(mh);
 342   }
 343   return false;
 344 }
 345 
 346 bool DirectiveSet::parse_and_add_inline(char* str, const char*& error_msg) {
 347   InlineMatcher* m = InlineMatcher::parse_inline_pattern(str, error_msg);
 348   if (m != NULL) {
 349     // add matcher last in chain - the order is significant
 350     append_inline(m);
 351     return true;
 352   } else {
 353     assert(error_msg != NULL, "Error message must be set");
 354     return false;
 355   }
 356 }
 357 
 358 void DirectiveSet::append_inline(InlineMatcher* m) {
 359   if (_inlinematchers == NULL) {
 360     _inlinematchers = m;
 361     return;
 362   }
 363   InlineMatcher* tmp = _inlinematchers;
 364   while (tmp->next() != NULL) {
 365     tmp = tmp->next();
 366   }
 367   tmp->set_next(m);
 368 }
 369 
 370 void DirectiveSet::print_inline(outputStream* st) {
 371   if (_inlinematchers == NULL) {
 372     st->print_cr("  inline: -");
 373   } else {
 374     st->print("  inline: ");
 375     _inlinematchers->print(st);
 376     InlineMatcher* tmp = _inlinematchers->next();
 377     while (tmp != NULL) {
 378       st->print(", ");
 379       tmp->print(st);
 380       tmp = tmp->next();
 381     }
 382     st->cr();
 383   }
 384 }
 385 
 386 bool DirectiveSet::is_intrinsic_disabled(methodHandle method) {
 387   vmIntrinsics::ID id = method->intrinsic_id();
 388   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 389 
 390   ResourceMark rm;
 391 
 392   // Create a copy of the string that contains the list of disabled
 393   // intrinsics. The copy is created because the string
 394   // will be modified by strtok(). Then, the list is tokenized with
 395   // ',' as a separator.
 396   size_t length = strlen(DisableIntrinsicOption);
 397   char* local_list = NEW_RESOURCE_ARRAY(char, length + 1);
 398   strncpy(local_list, DisableIntrinsicOption, length + 1);
 399 
 400   char* token = strtok(local_list, ",");
 401   while (token != NULL) {
 402     if (strcmp(token, vmIntrinsics::name_at(id)) == 0) {
 403       return true;
 404     } else {
 405       token = strtok(NULL, ",");
 406     }
 407   }
 408 
 409   return false;
 410 }
 411 
 412 DirectiveSet* DirectiveSet::clone(DirectiveSet const* src) {
 413   DirectiveSet* set = new DirectiveSet(NULL);
 414   memcpy(set->_modified, src->_modified, sizeof(src->_modified));
 415 
 416   InlineMatcher* tmp = src->_inlinematchers;
 417   while (tmp != NULL) {
 418     set->append_inline(tmp->clone());
 419     tmp = tmp->next();
 420   }
 421 
 422   #define copy_members_definition(name, type, dvalue, cc_flag) set->name##Option = src->name##Option;
 423     compilerdirectives_common_flags(copy_members_definition)
 424     compilerdirectives_c2_flags(copy_members_definition)
 425     compilerdirectives_c1_flags(copy_members_definition)
 426 
 427   // Create a local copy of the DisableIntrinsicOption.
 428   assert(src->DisableIntrinsicOption != NULL, "");
 429   size_t len = strlen(src->DisableIntrinsicOption) + 1;
 430   char* s = NEW_C_HEAP_ARRAY(char, len, mtCompiler);
 431   strncpy(s, src->DisableIntrinsicOption, len);
 432   assert(s[len-1] == '\0', "");
 433   set->DisableIntrinsicOption = s;
 434   return set;
 435 }
 436 
 437 // Create a new dirstack and push a default directive
 438 void DirectivesStack::init() {
 439   CompilerDirectives* _default_directives = new CompilerDirectives();
 440   char str[] = "*.*";
 441   const char* error_msg = NULL;
 442   _default_directives->add_match(str, error_msg);
 443 #ifdef COMPILER1
 444   _default_directives->_c1_store->EnableOption = true;
 445 #endif
 446 #ifdef COMPILER2
 447   _default_directives->_c2_store->EnableOption = true;
 448 #endif
 449   assert(error_msg == NULL, "Must succeed.");
 450   push(_default_directives);
 451 }
 452 
 453 DirectiveSet* DirectivesStack::getDefaultDirective(AbstractCompiler* comp) {
 454   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 455 
 456   assert(_bottom != NULL, "Must never be empty");
 457   _bottom->inc_refcount();
 458   return _bottom->get_for(comp);
 459 }
 460 
 461 void DirectivesStack::push(CompilerDirectives* directive) {
 462   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 463 
 464   directive->inc_refcount();
 465   if (_top == NULL) {
 466     assert(_bottom == NULL, "There can only be one default directive");
 467     _bottom = directive; // default directive, can never be removed.
 468   }
 469 
 470   directive->set_next(_top);
 471   _top = directive;
 472   _depth++;
 473 }
 474 
 475 void DirectivesStack::pop() {
 476   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 477   pop_inner();
 478 }
 479 
 480 void DirectivesStack::pop_inner() {
 481   assert(DirectivesStack_lock->owned_by_self(), "");
 482 
 483   if (_top->next() == NULL) {
 484     return; // Do nothing - don't allow an empty stack
 485   }
 486   CompilerDirectives* tmp = _top;
 487   _top = _top->next();
 488   _depth--;
 489 
 490   DirectivesStack::release(tmp);
 491 }
 492 
 493 bool DirectivesStack::check_capacity(int request_size, outputStream* st) {
 494   if ((request_size + _depth) > CompilerDirectivesLimit) {
 495     st->print_cr("Could not add %i more directives. Currently %i/%i directives.", request_size, _depth, CompilerDirectivesLimit);
 496     return false;
 497   }
 498   return true;
 499 }
 500 
 501 void DirectivesStack::clear() {
 502   // holding the lock during the whole operation ensuring consistent result
 503   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 504   while (_top->next() != NULL) {
 505     pop_inner();
 506   }
 507 }
 508 
 509 void DirectivesStack::print(outputStream* st) {
 510   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 511   CompilerDirectives* tmp = _top;
 512   while (tmp != NULL) {
 513     tmp->print(st);
 514     tmp = tmp->next();
 515     st->cr();
 516   }
 517 }
 518 
 519 void DirectivesStack::release(DirectiveSet* set) {
 520   assert(set != NULL, "Never NULL");
 521   MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 522   if (set->is_exclusive_copy()) {
 523     // Old CompilecCmmands forced us to create an exclusive copy
 524     delete set;
 525   } else {
 526     assert(set->directive() != NULL, "Never NULL");
 527     release(set->directive());
 528   }
 529 }
 530 
 531 
 532 void DirectivesStack::release(CompilerDirectives* dir) {
 533   assert(DirectivesStack_lock->owned_by_self(), "");
 534   dir->dec_refcount();
 535   if (dir->refcount() == 0) {
 536     delete dir;
 537   }
 538 }
 539 
 540 DirectiveSet* DirectivesStack::getMatchingDirective(methodHandle method, AbstractCompiler *comp) {
 541   assert(_depth > 0, "Must never be empty");
 542 
 543   DirectiveSet* match = NULL;
 544   {
 545     MutexLockerEx locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);
 546 
 547     CompilerDirectives* dir = _top;
 548     assert(dir != NULL, "Must be initialized");
 549 
 550     while (dir != NULL) {
 551       if (dir->is_default_directive() || dir->match(method)) {
 552         match = dir->get_for(comp);
 553         assert(match != NULL, "Consistency");
 554         if (match->EnableOption) {
 555           // The directiveSet for this compile is also enabled -> success
 556           dir->inc_refcount();
 557           break;
 558         }
 559       }
 560       dir = dir->next();
 561     }
 562   }
 563   guarantee(match != NULL, "There should always be a default directive that matches");
 564 
 565   // Check for legacy compile commands update, without DirectivesStack_lock
 566   return match->compilecommand_compatibility_init(method);
 567 }
--- EOF ---