Path: blob/master/src/hotspot/share/compiler/compilerDirectives.cpp
40930 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "ci/ciMethod.hpp"26#include "ci/ciUtilities.inline.hpp"27#include "compiler/abstractCompiler.hpp"28#include "compiler/compilerDirectives.hpp"29#include "compiler/compilerOracle.hpp"30#include "memory/allocation.inline.hpp"31#include "memory/resourceArea.hpp"32#include "runtime/globals_extension.hpp"3334CompilerDirectives::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};4041CompilerDirectives::~CompilerDirectives() {42if (_c1_store != NULL) {43delete _c1_store;44}45if (_c2_store != NULL) {46delete _c2_store;47}4849// remove all linked method matchers50BasicMatcher* tmp = _match;51while (tmp != NULL) {52BasicMatcher* next = tmp->next();53delete tmp;54tmp = next;55}56}5758void CompilerDirectives::print(outputStream* st) {59assert(DirectivesStack_lock->owned_by_self(), "");60if (_match != NULL) {61st->cr();62st->print("Directive:");63if (is_default_directive()) {64st->print_cr(" (default)");65} else {66st->cr();67}68st->print(" matching: ");69_match->print(st);70BasicMatcher* tmp = _match->next();71while (tmp != NULL) {72st->print(", ");73tmp->print(st);74tmp = tmp->next();75}76st->cr();77} else {78assert(0, "There should always be a match");79}8081if (_c1_store != NULL) {82st->print_cr(" c1 directives:");83_c1_store->print(st);84}85if (_c2_store != NULL) {86st->cr();87st->print_cr(" c2 directives:");88_c2_store->print(st);89}90//---91}9293void CompilerDirectives::finalize(outputStream* st) {94if (_c1_store != NULL) {95_c1_store->finalize(st);96}97if (_c2_store != NULL) {98_c2_store->finalize(st);99}100}101102void DirectiveSet::finalize(outputStream* st) {103// Check LogOption and warn104if (LogOption && !LogCompilation) {105st->print_cr("Warning: +LogCompilation must be set to enable compilation logging from directives");106}107if (PrintAssemblyOption && FLAG_IS_DEFAULT(DebugNonSafepoints)) {108warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");109DebugNonSafepoints = true;110}111112// if any flag has been modified - set directive as enabled113// unless it already has been explicitly set.114if (!_modified[EnableIndex]) {115if (_inlinematchers != NULL) {116EnableOption = true;117return;118}119int i;120for (i = 0; i < number_of_flags; i++) {121if (_modified[i]) {122EnableOption = true;123return;124}125}126}127}128129CompilerDirectives* CompilerDirectives::next() {130return _next;131}132133bool CompilerDirectives::match(const methodHandle& method) {134if (is_default_directive()) {135return true;136}137if (method == NULL) {138return false;139}140if (_match->match(method)) {141return true;142}143return false;144}145146bool CompilerDirectives::add_match(char* str, const char*& error_msg) {147BasicMatcher* bm = BasicMatcher::parse_method_pattern(str, error_msg, false);148if (bm == NULL) {149assert(error_msg != NULL, "Must have error message");150return false;151} else {152bm->set_next(_match);153_match = bm;154return true;155}156}157158void CompilerDirectives::inc_refcount() {159assert(DirectivesStack_lock->owned_by_self(), "");160_ref_count++;161}162163void CompilerDirectives::dec_refcount() {164assert(DirectivesStack_lock->owned_by_self(), "");165_ref_count--;166}167168int CompilerDirectives::refcount() {169assert(DirectivesStack_lock->owned_by_self(), "");170return _ref_count;171}172173DirectiveSet* CompilerDirectives::get_for(AbstractCompiler *comp) {174assert(DirectivesStack_lock->owned_by_self(), "");175if (comp == NULL) { // Xint176return _c1_store;177} else if (comp->is_c2()) {178return _c2_store;179} else {180// use c1_store as default181assert(comp->is_c1() || comp->is_jvmci(), "");182return _c1_store;183}184}185186// In the list of Control/disabled intrinsics, the ID of the control intrinsics can separated:187// - by ',' (if -XX:Control/DisableIntrinsic is used once when invoking the VM) or188// - by '\n' (if -XX:Control/DisableIntrinsic is used multiple times when invoking the VM) or189// - by ' ' (if Control/DisableIntrinsic is used on a per-method level, e.g., with CompileCommand).190//191// To simplify the processing of the list, the canonicalize_control_intrinsic() method192// returns a new copy of the list in which '\n' and ' ' is replaced with ','.193ccstrlist DirectiveSet::canonicalize_control_intrinsic(ccstrlist option_value) {194char* canonicalized_list = NEW_C_HEAP_ARRAY(char, strlen(option_value) + 1, mtCompiler);195int i = 0;196char current;197while ((current = option_value[i]) != '\0') {198if (current == '\n' || current == ' ') {199canonicalized_list[i] = ',';200} else {201canonicalized_list[i] = current;202}203i++;204}205canonicalized_list[i] = '\0';206return canonicalized_list;207}208209ControlIntrinsicIter::ControlIntrinsicIter(ccstrlist option_value, bool disable_all)210: _disableIntrinsic(disable_all) {211_list = (char*)DirectiveSet::canonicalize_control_intrinsic(option_value);212_saved_ptr = _list;213_enabled = false;214215_token = strtok_r(_saved_ptr, ",", &_saved_ptr);216next_token();217}218219ControlIntrinsicIter::~ControlIntrinsicIter() {220FREE_C_HEAP_ARRAY(char, _list);221}222223// pre-increment224ControlIntrinsicIter& ControlIntrinsicIter::operator++() {225_token = strtok_r(NULL, ",", &_saved_ptr);226next_token();227return *this;228}229230void ControlIntrinsicIter::next_token() {231if (_token && !_disableIntrinsic) {232char ch = _token[0];233234if (ch != '+' && ch != '-') {235warning("failed to parse %s. must start with +/-!", _token);236} else {237_enabled = ch == '+';238_token++;239}240}241}242243void DirectiveSet::init_control_intrinsic() {244for (ControlIntrinsicIter iter(ControlIntrinsic); *iter != NULL; ++iter) {245vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);246247if (id != vmIntrinsics::_none) {248_intrinsic_control_words[vmIntrinsics::as_int(id)] = iter.is_enabled();249}250}251252// Order matters, DisableIntrinsic can overwrite ControlIntrinsic253for (ControlIntrinsicIter iter(DisableIntrinsic, true/*disable_all*/); *iter != NULL; ++iter) {254vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);255256if (id != vmIntrinsics::_none) {257_intrinsic_control_words[vmIntrinsics::as_int(id)] = false;258}259}260}261262DirectiveSet::DirectiveSet(CompilerDirectives* d) :_inlinematchers(NULL), _directive(d) {263#define init_defaults_definition(name, type, dvalue, compiler) this->name##Option = dvalue;264compilerdirectives_common_flags(init_defaults_definition)265compilerdirectives_c2_flags(init_defaults_definition)266compilerdirectives_c1_flags(init_defaults_definition)267memset(_modified, 0, sizeof(_modified));268_intrinsic_control_words.fill_in(/*default value*/TriBool());269}270271DirectiveSet::~DirectiveSet() {272// remove all linked methodmatchers273InlineMatcher* tmp = _inlinematchers;274while (tmp != NULL) {275InlineMatcher* next = tmp->next();276delete tmp;277tmp = next;278}279}280281// A smart pointer of DirectiveSet. It uses Copy-on-Write strategy to avoid cloning.282// It provides 2 accesses of the underlying raw pointer.283// 1) operator->() returns a pointer to a constant DirectiveSet. It's read-only.284// 2) cloned() returns a pointer that points to the cloned DirectiveSet.285// Users should only use cloned() when they need to update DirectiveSet.286//287// In the end, users need to invoke commit() to finalize the pending changes.288// If cloning happens, the smart pointer will return the new pointer after releasing the original289// one on DirectivesStack. If cloning doesn't happen, it returns the original intact pointer.290class DirectiveSetPtr {291private:292DirectiveSet* _origin;293DirectiveSet* _clone;294NONCOPYABLE(DirectiveSetPtr);295296public:297DirectiveSetPtr(DirectiveSet* origin): _origin(origin), _clone(nullptr) {298assert(origin != nullptr, "DirectiveSetPtr cannot be initialized with a NULL pointer.");299}300301DirectiveSet const* operator->() {302return (_clone == nullptr) ? _origin : _clone;303}304305DirectiveSet* cloned() {306if (_clone == nullptr) {307_clone = DirectiveSet::clone(_origin);308}309return _clone;310}311312DirectiveSet* commit() {313if (_clone != nullptr) {314// We are returning a (parentless) copy. The originals parent don't need to account for this.315DirectivesStack::release(_origin);316_origin = _clone;317_clone = nullptr;318}319320return _origin;321}322};323324// Backward compatibility for CompileCommands325// Breaks the abstraction and causes lots of extra complexity326// - if some option is changed we need to copy directiveset since it no longer can be shared327// - Need to free copy after use328// - Requires a modified bit so we don't overwrite options that is set by directives329DirectiveSet* DirectiveSet::compilecommand_compatibility_init(const methodHandle& method) {330// Early bail out - checking all options is expensive - we rely on them not being used331// Only set a flag if it has not been modified and value changes.332// Only copy set if a flag needs to be set333if (!CompilerDirectivesIgnoreCompileCommandsOption && CompilerOracle::has_any_command_set()) {334DirectiveSetPtr set(this);335336// All CompileCommands are not equal so this gets a bit verbose337// When CompileCommands have been refactored less clutter will remain.338if (CompilerOracle::should_break_at(method)) {339if (!_modified[BreakAtCompileIndex]) {340set.cloned()->BreakAtCompileOption = true;341}342if (!_modified[BreakAtExecuteIndex]) {343set.cloned()->BreakAtExecuteOption = true;344}345}346if (!_modified[LogIndex]) {347bool log = CompilerOracle::should_log(method);348if (log != set->LogOption) {349set.cloned()->LogOption = log;350}351}352353if (CompilerOracle::should_print(method)) {354if (!_modified[PrintAssemblyIndex]) {355set.cloned()->PrintAssemblyOption = true;356}357}358// Exclude as in should not compile == Enabled359if (CompilerOracle::should_exclude(method)) {360if (!_modified[ExcludeIndex]) {361set.cloned()->ExcludeOption = true;362}363}364365// inline and dontinline (including exclude) are implemented in the directiveset accessors366#define init_default_cc(name, type, dvalue, cc_flag) { type v; if (!_modified[name##Index] && CompileCommand::cc_flag != CompileCommand::Unknown && CompilerOracle::has_option_value(method, CompileCommand::cc_flag, v) && v != this->name##Option) { set.cloned()->name##Option = v; } }367compilerdirectives_common_flags(init_default_cc)368compilerdirectives_c2_flags(init_default_cc)369compilerdirectives_c1_flags(init_default_cc)370371// Canonicalize DisableIntrinsic to contain only ',' as a separator.372ccstrlist option_value;373bool need_reset = true; // if Control/DisableIntrinsic redefined, only need to reset control_words once374375if (!_modified[ControlIntrinsicIndex] &&376CompilerOracle::has_option_value(method, CompileCommand::ControlIntrinsic, option_value)) {377ControlIntrinsicIter iter(option_value);378379if (need_reset) {380set.cloned()->_intrinsic_control_words.fill_in(TriBool());381need_reset = false;382}383384while (*iter != NULL) {385vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);386if (id != vmIntrinsics::_none) {387set.cloned()->_intrinsic_control_words[vmIntrinsics::as_int(id)] = iter.is_enabled();388}389390++iter;391}392}393394395if (!_modified[DisableIntrinsicIndex] &&396CompilerOracle::has_option_value(method, CompileCommand::DisableIntrinsic, option_value)) {397ControlIntrinsicIter iter(option_value, true/*disable_all*/);398399if (need_reset) {400set.cloned()->_intrinsic_control_words.fill_in(TriBool());401need_reset = false;402}403404while (*iter != NULL) {405vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);406if (id != vmIntrinsics::_none) {407set.cloned()->_intrinsic_control_words[vmIntrinsics::as_int(id)] = false;408}409410++iter;411}412}413414return set.commit();415}416// Nothing changed417return this;418}419420CompilerDirectives* DirectiveSet::directive() {421assert(_directive != NULL, "Must have been initialized");422return _directive;423}424425bool DirectiveSet::matches_inline(const methodHandle& method, int inline_action) {426if (_inlinematchers != NULL) {427if (_inlinematchers->match(method, inline_action)) {428return true;429}430}431return false;432}433434bool DirectiveSet::should_inline(ciMethod* inlinee) {435inlinee->check_is_loaded();436VM_ENTRY_MARK;437methodHandle mh(THREAD, inlinee->get_Method());438439if (_inlinematchers != NULL) {440return matches_inline(mh, InlineMatcher::force_inline);441}442if (!CompilerDirectivesIgnoreCompileCommandsOption) {443return CompilerOracle::should_inline(mh);444}445return false;446}447448bool DirectiveSet::should_not_inline(ciMethod* inlinee) {449inlinee->check_is_loaded();450VM_ENTRY_MARK;451methodHandle mh(THREAD, inlinee->get_Method());452453if (_inlinematchers != NULL) {454return matches_inline(mh, InlineMatcher::dont_inline);455}456if (!CompilerDirectivesIgnoreCompileCommandsOption) {457return CompilerOracle::should_not_inline(mh);458}459return false;460}461462bool DirectiveSet::parse_and_add_inline(char* str, const char*& error_msg) {463InlineMatcher* m = InlineMatcher::parse_inline_pattern(str, error_msg);464if (m != NULL) {465// add matcher last in chain - the order is significant466append_inline(m);467return true;468} else {469assert(error_msg != NULL, "Error message must be set");470return false;471}472}473474void DirectiveSet::append_inline(InlineMatcher* m) {475if (_inlinematchers == NULL) {476_inlinematchers = m;477return;478}479InlineMatcher* tmp = _inlinematchers;480while (tmp->next() != NULL) {481tmp = tmp->next();482}483tmp->set_next(m);484}485486void DirectiveSet::print_inline(outputStream* st) {487if (_inlinematchers == NULL) {488st->print_cr(" inline: -");489} else {490st->print(" inline: ");491_inlinematchers->print(st);492InlineMatcher* tmp = _inlinematchers->next();493while (tmp != NULL) {494st->print(", ");495tmp->print(st);496tmp = tmp->next();497}498st->cr();499}500}501502bool DirectiveSet::is_intrinsic_disabled(const methodHandle& method) {503vmIntrinsics::ID id = method->intrinsic_id();504assert(id > vmIntrinsics::_none && id < vmIntrinsics::ID_LIMIT, "invalid intrinsic_id!");505506TriBool b = _intrinsic_control_words[vmIntrinsics::as_int(id)];507if (b.is_default()) {508return false; // if unset, every intrinsic is enabled.509} else {510return !b;511}512}513514DirectiveSet* DirectiveSet::clone(DirectiveSet const* src) {515DirectiveSet* set = new DirectiveSet(NULL);516// Ordinary allocations of DirectiveSet would call init_control_intrinsic()517// immediately to create a new copy for set->Control/DisableIntrinsicOption.518// However, here it does not need to because the code below creates519// a copy of src->Control/DisableIntrinsicOption that initializes520// set->Control/DisableIntrinsicOption.521522memcpy(set->_modified, src->_modified, sizeof(src->_modified));523524InlineMatcher* tmp = src->_inlinematchers;525while (tmp != NULL) {526set->append_inline(tmp->clone());527tmp = tmp->next();528}529530#define copy_members_definition(name, type, dvalue, cc_flag) set->name##Option = src->name##Option;531compilerdirectives_common_flags(copy_members_definition)532compilerdirectives_c2_flags(copy_members_definition)533compilerdirectives_c1_flags(copy_members_definition)534535set->_intrinsic_control_words = src->_intrinsic_control_words;536return set;537}538539// Create a new dirstack and push a default directive540void DirectivesStack::init() {541CompilerDirectives* _default_directives = new CompilerDirectives();542char str[] = "*.*";543const char* error_msg = NULL;544_default_directives->add_match(str, error_msg);545#if defined(COMPILER1) || INCLUDE_JVMCI546_default_directives->_c1_store->EnableOption = true;547#endif548#ifdef COMPILER2549if (CompilerConfig::is_c2_enabled()) {550_default_directives->_c2_store->EnableOption = true;551}552#endif553assert(error_msg == NULL, "Must succeed.");554push(_default_directives);555}556557DirectiveSet* DirectivesStack::getDefaultDirective(AbstractCompiler* comp) {558MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);559560assert(_bottom != NULL, "Must never be empty");561_bottom->inc_refcount();562return _bottom->get_for(comp);563}564565void DirectivesStack::push(CompilerDirectives* directive) {566MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);567568directive->inc_refcount();569if (_top == NULL) {570assert(_bottom == NULL, "There can only be one default directive");571_bottom = directive; // default directive, can never be removed.572}573574directive->set_next(_top);575_top = directive;576_depth++;577}578579void DirectivesStack::pop(int count) {580MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);581assert(count > -1, "No negative values");582for (int i = 0; i < count; i++) {583pop_inner();584}585}586587void DirectivesStack::pop_inner() {588assert(DirectivesStack_lock->owned_by_self(), "");589590if (_top->next() == NULL) {591return; // Do nothing - don't allow an empty stack592}593CompilerDirectives* tmp = _top;594_top = _top->next();595_depth--;596597DirectivesStack::release(tmp);598}599600bool DirectivesStack::check_capacity(int request_size, outputStream* st) {601if ((request_size + _depth) > CompilerDirectivesLimit) {602st->print_cr("Could not add %i more directives. Currently %i/%i directives.", request_size, _depth, CompilerDirectivesLimit);603return false;604}605return true;606}607608void DirectivesStack::clear() {609// holding the lock during the whole operation ensuring consistent result610MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);611while (_top->next() != NULL) {612pop_inner();613}614}615616void DirectivesStack::print(outputStream* st) {617MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);618CompilerDirectives* tmp = _top;619while (tmp != NULL) {620tmp->print(st);621tmp = tmp->next();622st->cr();623}624}625626void DirectivesStack::release(DirectiveSet* set) {627assert(set != NULL, "Never NULL");628MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);629if (set->is_exclusive_copy()) {630// Old CompilecCmmands forced us to create an exclusive copy631delete set;632} else {633assert(set->directive() != NULL, "Never NULL");634release(set->directive());635}636}637638639void DirectivesStack::release(CompilerDirectives* dir) {640assert(DirectivesStack_lock->owned_by_self(), "");641dir->dec_refcount();642if (dir->refcount() == 0) {643delete dir;644}645}646647DirectiveSet* DirectivesStack::getMatchingDirective(const methodHandle& method, AbstractCompiler *comp) {648assert(_depth > 0, "Must never be empty");649650DirectiveSet* match = NULL;651{652MutexLocker locker(DirectivesStack_lock, Mutex::_no_safepoint_check_flag);653654CompilerDirectives* dir = _top;655assert(dir != NULL, "Must be initialized");656657while (dir != NULL) {658if (dir->is_default_directive() || dir->match(method)) {659match = dir->get_for(comp);660assert(match != NULL, "Consistency");661if (match->EnableOption) {662// The directiveSet for this compile is also enabled -> success663dir->inc_refcount();664break;665}666}667dir = dir->next();668}669}670guarantee(match != NULL, "There should always be a default directive that matches");671672// Check for legacy compile commands update, without DirectivesStack_lock673return match->compilecommand_compatibility_init(method);674}675676677