Path: blob/master/src/hotspot/share/opto/bytecodeInfo.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/ciReplay.hpp"26#include "classfile/vmSymbols.hpp"27#include "compiler/compileBroker.hpp"28#include "compiler/compilerEvent.hpp"29#include "compiler/compileLog.hpp"30#include "interpreter/linkResolver.hpp"31#include "jfr/jfrEvents.hpp"32#include "oops/objArrayKlass.hpp"33#include "opto/callGenerator.hpp"34#include "opto/parse.hpp"35#include "runtime/handles.inline.hpp"36#include "utilities/events.hpp"3738//=============================================================================39//------------------------------InlineTree-------------------------------------40InlineTree::InlineTree(Compile* c,41const InlineTree *caller_tree, ciMethod* callee,42JVMState* caller_jvms, int caller_bci,43int max_inline_level) :44C(c),45_caller_jvms(caller_jvms),46_method(callee),47_caller_tree((InlineTree*) caller_tree),48_count_inline_bcs(method()->code_size_for_inlining()),49_max_inline_level(max_inline_level),50_subtrees(c->comp_arena(), 2, 0, NULL),51_msg(NULL)52{53#ifndef PRODUCT54_count_inlines = 0;55_forced_inline = false;56#endif57if (_caller_jvms != NULL) {58// Keep a private copy of the caller_jvms:59_caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());60_caller_jvms->set_bci(caller_jvms->bci());61assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");62}63assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");64assert((caller_tree == NULL ? 0 : caller_tree->stack_depth() + 1) == stack_depth(), "correct (redundant) depth parameter");65assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");66// Update hierarchical counts, count_inline_bcs() and count_inlines()67InlineTree *caller = (InlineTree *)caller_tree;68for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {69caller->_count_inline_bcs += count_inline_bcs();70NOT_PRODUCT(caller->_count_inlines++;)71}72}7374/**75* Return true when EA is ON and a java constructor is called or76* a super constructor is called from an inlined java constructor.77* Also return true for boxing methods.78* Also return true for methods returning Iterator (including Iterable::iterator())79* that is essential for forall-loops performance.80*/81static bool is_init_with_ea(ciMethod* callee_method,82ciMethod* caller_method, Compile* C) {83if (!C->do_escape_analysis() || !EliminateAllocations) {84return false; // EA is off85}86if (callee_method->is_initializer()) {87return true; // constuctor88}89if (caller_method->is_initializer() &&90caller_method != C->method() &&91caller_method->holder()->is_subclass_of(callee_method->holder())) {92return true; // super constructor is called from inlined constructor93}94if (C->eliminate_boxing() && callee_method->is_boxing_method()) {95return true;96}97ciType *retType = callee_method->signature()->return_type();98ciKlass *iter = C->env()->Iterator_klass();99if(retType->is_loaded() && iter->is_loaded() && retType->is_subtype_of(iter)) {100return true;101}102return false;103}104105/**106* Force inlining unboxing accessor.107*/108static bool is_unboxing_method(ciMethod* callee_method, Compile* C) {109return C->eliminate_boxing() && callee_method->is_unboxing_method();110}111112// positive filter: should callee be inlined?113bool InlineTree::should_inline(ciMethod* callee_method, ciMethod* caller_method,114int caller_bci, ciCallProfile& profile) {115// Allows targeted inlining116if (C->directive()->should_inline(callee_method)) {117set_msg("force inline by CompileCommand");118_forced_inline = true;119return true;120}121122if (callee_method->force_inline()) {123set_msg("force inline by annotation");124_forced_inline = true;125return true;126}127128#ifndef PRODUCT129int inline_depth = inline_level()+1;130if (ciReplay::should_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {131set_msg("force inline by ciReplay");132_forced_inline = true;133return true;134}135#endif136137int size = callee_method->code_size_for_inlining();138139// Check for too many throws (and not too huge)140if(callee_method->interpreter_throwout_count() > InlineThrowCount &&141size < InlineThrowMaxSize ) {142if (C->print_inlining() && Verbose) {143CompileTask::print_inline_indent(inline_level());144tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());145}146set_msg("many throws");147return true;148}149150int default_max_inline_size = C->max_inline_size();151int inline_small_code_size = InlineSmallCode / 4;152int max_inline_size = default_max_inline_size;153154int call_site_count = method()->scale_count(profile.count());155int invoke_count = method()->interpreter_invocation_count();156157assert(invoke_count != 0, "require invocation count greater than zero");158int freq = call_site_count / invoke_count;159160// bump the max size if the call is frequent161if ((freq >= InlineFrequencyRatio) ||162(call_site_count >= InlineFrequencyCount) ||163is_unboxing_method(callee_method, C) ||164is_init_with_ea(callee_method, caller_method, C)) {165166max_inline_size = C->freq_inline_size();167if (size <= max_inline_size && TraceFrequencyInlining) {168CompileTask::print_inline_indent(inline_level());169tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);170CompileTask::print_inline_indent(inline_level());171callee_method->print();172tty->cr();173}174} else {175// Not hot. Check for medium-sized pre-existing nmethod at cold sites.176if (callee_method->has_compiled_code() &&177callee_method->instructions_size() > inline_small_code_size) {178set_msg("already compiled into a medium method");179return false;180}181}182if (size > max_inline_size) {183if (max_inline_size > default_max_inline_size) {184set_msg("hot method too big");185} else {186set_msg("too big");187}188return false;189}190return true;191}192193194// negative filter: should callee NOT be inlined?195bool InlineTree::should_not_inline(ciMethod *callee_method,196ciMethod* caller_method,197JVMState* jvms) {198199const char* fail_msg = NULL;200201// First check all inlining restrictions which are required for correctness202if (callee_method->is_abstract()) {203fail_msg = "abstract method"; // // note: we allow ik->is_abstract()204} else if (!callee_method->holder()->is_initialized() &&205// access allowed in the context of static initializer206C->needs_clinit_barrier(callee_method->holder(), caller_method)) {207fail_msg = "method holder not initialized";208} else if (callee_method->is_native()) {209fail_msg = "native method";210} else if (callee_method->dont_inline()) {211fail_msg = "don't inline by annotation";212}213214// one more inlining restriction215if (fail_msg == NULL && callee_method->has_unloaded_classes_in_signature()) {216fail_msg = "unloaded signature classes";217}218219if (fail_msg != NULL) {220set_msg(fail_msg);221return true;222}223224// ignore heuristic controls on inlining225if (C->directive()->should_inline(callee_method)) {226set_msg("force inline by CompileCommand");227return false;228}229230if (C->directive()->should_not_inline(callee_method)) {231set_msg("disallowed by CompileCommand");232return true;233}234235#ifndef PRODUCT236int caller_bci = jvms->bci();237int inline_depth = inline_level()+1;238if (ciReplay::should_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {239set_msg("force inline by ciReplay");240return false;241}242243if (ciReplay::should_not_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {244set_msg("disallowed by ciReplay");245return true;246}247248if (ciReplay::should_not_inline(callee_method)) {249set_msg("disallowed by ciReplay");250return true;251}252#endif253254if (callee_method->force_inline()) {255set_msg("force inline by annotation");256return false;257}258259// Now perform checks which are heuristic260261if (is_unboxing_method(callee_method, C)) {262// Inline unboxing methods.263return false;264}265266if (callee_method->has_compiled_code() &&267callee_method->instructions_size() > InlineSmallCode) {268set_msg("already compiled into a big method");269return true;270}271272// don't inline exception code unless the top method belongs to an273// exception class274if (caller_tree() != NULL &&275callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {276const InlineTree *top = this;277while (top->caller_tree() != NULL) top = top->caller_tree();278ciInstanceKlass* k = top->method()->holder();279if (!k->is_subclass_of(C->env()->Throwable_klass())) {280set_msg("exception method");281return true;282}283}284285// use frequency-based objections only for non-trivial methods286if (callee_method->code_size() <= MaxTrivialSize) {287return false;288}289290// don't use counts with -Xcomp291if (UseInterpreter) {292293if (!callee_method->has_compiled_code() &&294!callee_method->was_executed_more_than(0)) {295set_msg("never executed");296return true;297}298299if (is_init_with_ea(callee_method, caller_method, C)) {300// Escape Analysis: inline all executed constructors301return false;302} else {303intx counter_high_value;304// Tiered compilation uses a different "high value" than non-tiered compilation.305// Determine the right value to use.306if (TieredCompilation) {307counter_high_value = InvocationCounter::count_limit / 2;308} else {309counter_high_value = CompileThreshold / 2;310}311if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold, counter_high_value))) {312set_msg("executed < MinInliningThreshold times");313return true;314}315}316}317318return false;319}320321bool InlineTree::is_not_reached(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile) {322if (!UseInterpreter) {323return false; // -Xcomp324}325if (profile.count() > 0) {326return false; // reachable according to profile327}328if (!callee_method->was_executed_more_than(0)) {329return true; // callee was never executed330}331if (caller_method->is_not_reached(caller_bci)) {332return true; // call site not resolved333}334if (profile.count() == -1) {335return false; // immature profile; optimistically treat as reached336}337assert(profile.count() == 0, "sanity");338339// Profile info is scarce.340// Try to guess: check if the call site belongs to a start block.341// Call sites in a start block should be reachable if no exception is thrown earlier.342ciMethodBlocks* caller_blocks = caller_method->get_method_blocks();343bool is_start_block = caller_blocks->block_containing(caller_bci)->start_bci() == 0;344if (is_start_block) {345return false; // treat the call reached as part of start block346}347return true; // give up and treat the call site as not reached348}349350//-----------------------------try_to_inline-----------------------------------351// return true if ok352// Relocated from "InliningClosure::try_to_inline"353bool InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method,354int caller_bci, JVMState* jvms, ciCallProfile& profile,355bool& should_delay) {356357if (ClipInlining && (int)count_inline_bcs() >= DesiredMethodLimit) {358if (!callee_method->force_inline() || !IncrementalInline) {359set_msg("size > DesiredMethodLimit");360return false;361} else if (!C->inlining_incrementally()) {362should_delay = true;363}364}365366_forced_inline = false; // Reset367if (!should_inline(callee_method, caller_method, caller_bci, profile)) {368return false;369}370if (should_not_inline(callee_method, caller_method, jvms)) {371return false;372}373374if (InlineAccessors && callee_method->is_accessor()) {375// accessor methods are not subject to any of the following limits.376set_msg("accessor");377return true;378}379380// suppress a few checks for accessors and trivial methods381if (callee_method->code_size() > MaxTrivialSize) {382383// don't inline into giant methods384if (C->over_inlining_cutoff()) {385if ((!callee_method->force_inline() && !caller_method->is_compiled_lambda_form())386|| !IncrementalInline) {387set_msg("NodeCountInliningCutoff");388return false;389} else {390should_delay = true;391}392}393394if (!UseInterpreter &&395is_init_with_ea(callee_method, caller_method, C)) {396// Escape Analysis stress testing when running Xcomp:397// inline constructors even if they are not reached.398} else if (forced_inline()) {399// Inlining was forced by CompilerOracle, ciReplay or annotation400} else if (is_not_reached(callee_method, caller_method, caller_bci, profile)) {401// don't inline unreached call sites402set_msg("call site not reached");403return false;404}405}406407if (!C->do_inlining() && InlineAccessors) {408set_msg("not an accessor");409return false;410}411412// Limit inlining depth in case inlining is forced or413// _max_inline_level was increased to compensate for lambda forms.414if (inline_level() > MaxForceInlineLevel) {415set_msg("MaxForceInlineLevel");416return false;417}418if (inline_level() > _max_inline_level) {419if (!callee_method->force_inline() || !IncrementalInline) {420set_msg("inlining too deep");421return false;422} else if (!C->inlining_incrementally()) {423should_delay = true;424}425}426427// detect direct and indirect recursive inlining428{429// count the current method and the callee430const bool is_compiled_lambda_form = callee_method->is_compiled_lambda_form();431int inline_level = 0;432if (!is_compiled_lambda_form) {433if (method() == callee_method) {434inline_level++;435}436}437// count callers of current method and callee438Node* callee_argument0 = is_compiled_lambda_form ? jvms->map()->argument(jvms, 0)->uncast() : NULL;439for (JVMState* j = jvms->caller(); j != NULL && j->has_method(); j = j->caller()) {440if (j->method() == callee_method) {441if (is_compiled_lambda_form) {442// Since compiled lambda forms are heavily reused we allow recursive inlining. If it is truly443// a recursion (using the same "receiver") we limit inlining otherwise we can easily blow the444// compiler stack.445Node* caller_argument0 = j->map()->argument(j, 0)->uncast();446if (caller_argument0 == callee_argument0) {447inline_level++;448}449} else {450inline_level++;451}452}453}454if (inline_level > MaxRecursiveInlineLevel) {455set_msg("recursive inlining is too deep");456return false;457}458}459460int size = callee_method->code_size_for_inlining();461462if (ClipInlining && (int)count_inline_bcs() + size >= DesiredMethodLimit) {463if (!callee_method->force_inline() || !IncrementalInline) {464set_msg("size > DesiredMethodLimit");465return false;466} else if (!C->inlining_incrementally()) {467should_delay = true;468}469}470471// ok, inline this method472return true;473}474475//------------------------------pass_initial_checks----------------------------476bool InlineTree::pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {477// Check if a callee_method was suggested478if (callee_method == NULL) {479return false;480}481ciInstanceKlass *callee_holder = callee_method->holder();482// Check if klass of callee_method is loaded483if (!callee_holder->is_loaded()) {484return false;485}486if (!callee_holder->is_initialized() &&487// access allowed in the context of static initializer488C->needs_clinit_barrier(callee_holder, caller_method)) {489return false;490}491if( !UseInterpreter ) /* running Xcomp */ {492// Checks that constant pool's call site has been visited493// stricter than callee_holder->is_initialized()494ciBytecodeStream iter(caller_method);495iter.force_bci(caller_bci);496Bytecodes::Code call_bc = iter.cur_bc();497// An invokedynamic instruction does not have a klass.498if (call_bc != Bytecodes::_invokedynamic) {499int index = iter.get_index_u2_cpcache();500if (!caller_method->is_klass_loaded(index, true)) {501return false;502}503// Try to do constant pool resolution if running Xcomp504if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {505return false;506}507}508}509return true;510}511512//------------------------------check_can_parse--------------------------------513const char* InlineTree::check_can_parse(ciMethod* callee) {514// Certain methods cannot be parsed at all:515if ( callee->is_native()) return "native method";516if ( callee->is_abstract()) return "abstract method";517if (!callee->has_balanced_monitors()) return "not compilable (unbalanced monitors)";518if ( callee->get_flow_analysis()->failing()) return "not compilable (flow analysis failed)";519if (!callee->can_be_parsed()) return "cannot be parsed";520return NULL;521}522523//------------------------------print_inlining---------------------------------524void InlineTree::print_inlining(ciMethod* callee_method, int caller_bci,525ciMethod* caller_method, bool success) const {526const char* inline_msg = msg();527assert(inline_msg != NULL, "just checking");528if (C->log() != NULL) {529if (success) {530C->log()->inline_success(inline_msg);531} else {532C->log()->inline_fail(inline_msg);533}534}535CompileTask::print_inlining_ul(callee_method, inline_level(),536caller_bci, inline_msg);537if (C->print_inlining()) {538C->print_inlining(callee_method, inline_level(), caller_bci, inline_msg);539guarantee(callee_method != NULL, "would crash in CompilerEvent::InlineEvent::post");540if (Verbose) {541const InlineTree *top = this;542while (top->caller_tree() != NULL) { top = top->caller_tree(); }543//tty->print(" bcs: %d+%d invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());544}545}546EventCompilerInlining event;547if (event.should_commit()) {548CompilerEvent::InlineEvent::post(event, C->compile_id(), caller_method->get_Method(), callee_method, success, inline_msg, caller_bci);549}550}551552//------------------------------ok_to_inline-----------------------------------553bool InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile,554bool& should_delay) {555assert(callee_method != NULL, "caller checks for optimized virtual!");556assert(!should_delay, "should be initialized to false");557#ifdef ASSERT558// Make sure the incoming jvms has the same information content as me.559// This means that we can eventually make this whole class AllStatic.560if (jvms->caller() == NULL) {561assert(_caller_jvms == NULL, "redundant instance state");562} else {563assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");564}565assert(_method == jvms->method(), "redundant instance state");566#endif567int caller_bci = jvms->bci();568ciMethod* caller_method = jvms->method();569570// Do some initial checks.571if (!pass_initial_checks(caller_method, caller_bci, callee_method)) {572set_msg("failed initial checks");573print_inlining(callee_method, caller_bci, caller_method, false /* !success */);574return false;575}576577// Do some parse checks.578set_msg(check_can_parse(callee_method));579if (msg() != NULL) {580print_inlining(callee_method, caller_bci, caller_method, false /* !success */);581return false;582}583584// Check if inlining policy says no.585bool success = try_to_inline(callee_method, caller_method, caller_bci, jvms, profile,586should_delay); // out587if (success) {588// Inline!589if (msg() == NULL) {590set_msg("inline (hot)");591}592print_inlining(callee_method, caller_bci, caller_method, true /* success */);593build_inline_tree_for_callee(callee_method, jvms, caller_bci);594return true;595} else {596// Do not inline597if (msg() == NULL) {598set_msg("too cold to inline");599}600print_inlining(callee_method, caller_bci, caller_method, false /* !success */ );601return false;602}603}604605//------------------------------build_inline_tree_for_callee-------------------606InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {607// Attempt inlining.608InlineTree* old_ilt = callee_at(caller_bci, callee_method);609if (old_ilt != NULL) {610return old_ilt;611}612int max_inline_level_adjust = 0;613if (caller_jvms->method() != NULL) {614if (caller_jvms->method()->is_compiled_lambda_form()) {615max_inline_level_adjust += 1; // don't count actions in MH or indy adapter frames616} else if (callee_method->is_method_handle_intrinsic() ||617callee_method->is_compiled_lambda_form()) {618max_inline_level_adjust += 1; // don't count method handle calls from java.lang.invoke implementation619}620if (max_inline_level_adjust != 0 && C->print_inlining() && (Verbose || WizardMode)) {621CompileTask::print_inline_indent(inline_level());622tty->print_cr(" \\-> discounting inline depth");623}624if (max_inline_level_adjust != 0 && C->log()) {625int id1 = C->log()->identify(caller_jvms->method());626int id2 = C->log()->identify(callee_method);627C->log()->elem("inline_level_discount caller='%d' callee='%d'", id1, id2);628}629}630// Allocate in the comp_arena to make sure the InlineTree is live when dumping a replay compilation file631InlineTree* ilt = new (C->comp_arena()) InlineTree(C, this, callee_method, caller_jvms, caller_bci, _max_inline_level + max_inline_level_adjust);632_subtrees.append(ilt);633634NOT_PRODUCT( _count_inlines += 1; )635636return ilt;637}638639640//---------------------------------------callee_at-----------------------------641InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {642for (int i = 0; i < _subtrees.length(); i++) {643InlineTree* sub = _subtrees.at(i);644if (sub->caller_bci() == bci && callee == sub->method()) {645return sub;646}647}648return NULL;649}650651652//------------------------------build_inline_tree_root-------------------------653InlineTree *InlineTree::build_inline_tree_root() {654Compile* C = Compile::current();655656// Root of inline tree657InlineTree* ilt = new InlineTree(C, NULL, C->method(), NULL, -1, MaxInlineLevel);658659return ilt;660}661662663//-------------------------find_subtree_from_root-----------------------------664// Given a jvms, which determines a call chain from the root method,665// find the corresponding inline tree.666// Note: This method will be removed or replaced as InlineTree goes away.667InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee) {668InlineTree* iltp = root;669uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;670for (uint d = 1; d <= depth; d++) {671JVMState* jvmsp = jvms->of_depth(d);672// Select the corresponding subtree for this bci.673assert(jvmsp->method() == iltp->method(), "tree still in sync");674ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();675InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);676if (sub == NULL) {677if (d == depth) {678sub = iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());679}680guarantee(sub != NULL, "should be a sub-ilt here");681return sub;682}683iltp = sub;684}685return iltp;686}687688// Count number of nodes in this subtree689int InlineTree::count() const {690int result = 1;691for (int i = 0 ; i < _subtrees.length(); i++) {692result += _subtrees.at(i)->count();693}694return result;695}696697void InlineTree::dump_replay_data(outputStream* out) {698out->print(" %d %d ", inline_level(), caller_bci());699method()->dump_name_as_ascii(out);700for (int i = 0 ; i < _subtrees.length(); i++) {701_subtrees.at(i)->dump_replay_data(out);702}703}704705706#ifndef PRODUCT707void InlineTree::print_impl(outputStream* st, int indent) const {708for (int i = 0; i < indent; i++) st->print(" ");709st->print(" @ %d", caller_bci());710method()->print_short_name(st);711st->cr();712713for (int i = 0 ; i < _subtrees.length(); i++) {714_subtrees.at(i)->print_impl(st, indent + 2);715}716}717718void InlineTree::print_value_on(outputStream* st) const {719print_impl(st, 2);720}721#endif722723724