duke@435: /* duke@435: * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_compilationPolicy.cpp.incl" duke@435: duke@435: CompilationPolicy* CompilationPolicy::_policy; duke@435: elapsedTimer CompilationPolicy::_accumulated_time; duke@435: bool CompilationPolicy::_in_vm_startup; duke@435: duke@435: // Determine compilation policy based on command line argument duke@435: void compilationPolicy_init() { duke@435: CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup); duke@435: duke@435: switch(CompilationPolicyChoice) { duke@435: case 0: duke@435: CompilationPolicy::set_policy(new SimpleCompPolicy()); duke@435: break; duke@435: duke@435: case 1: duke@435: #ifdef COMPILER2 duke@435: CompilationPolicy::set_policy(new StackWalkCompPolicy()); duke@435: #else duke@435: Unimplemented(); duke@435: #endif duke@435: break; duke@435: duke@435: default: duke@435: fatal("CompilationPolicyChoice must be in the range: [0-1]"); duke@435: } duke@435: } duke@435: duke@435: void CompilationPolicy::completed_vm_startup() { duke@435: if (TraceCompilationPolicy) { duke@435: tty->print("CompilationPolicy: completed vm startup.\n"); duke@435: } duke@435: _in_vm_startup = false; duke@435: } duke@435: duke@435: // Returns true if m must be compiled before executing it duke@435: // This is intended to force compiles for methods (usually for duke@435: // debugging) that would otherwise be interpreted for some reason. duke@435: bool CompilationPolicy::mustBeCompiled(methodHandle m) { duke@435: if (m->has_compiled_code()) return false; // already compiled duke@435: if (!canBeCompiled(m)) return false; duke@435: duke@435: return !UseInterpreter || // must compile all methods duke@435: (UseCompiler && AlwaysCompileLoopMethods && m->has_loops()); // eagerly compile loop methods duke@435: } duke@435: duke@435: // Returns true if m is allowed to be compiled duke@435: bool CompilationPolicy::canBeCompiled(methodHandle m) { duke@435: if (m->is_abstract()) return false; duke@435: if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false; duke@435: duke@435: return !m->is_not_compilable(); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void CompilationPolicy::print_time() { duke@435: tty->print_cr ("Accumulated compilationPolicy times:"); duke@435: tty->print_cr ("---------------------------"); duke@435: tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds()); duke@435: } duke@435: duke@435: static void trace_osr_completion(nmethod* osr_nm) { duke@435: if (TraceOnStackReplacement) { duke@435: if (osr_nm == NULL) tty->print_cr("compilation failed"); duke@435: else tty->print_cr("nmethod " INTPTR_FORMAT, osr_nm); duke@435: } duke@435: } duke@435: #endif // !PRODUCT duke@435: duke@435: void CompilationPolicy::reset_counter_for_invocation_event(methodHandle m) { duke@435: // Make sure invocation and backedge counter doesn't overflow again right away duke@435: // as would be the case for native methods. duke@435: duke@435: // BUT also make sure the method doesn't look like it was never executed. duke@435: // Set carry bit and reduce counter's value to min(count, CompileThreshold/2). duke@435: m->invocation_counter()->set_carry(); duke@435: m->backedge_counter()->set_carry(); duke@435: duke@435: assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed"); duke@435: } duke@435: duke@435: void CompilationPolicy::reset_counter_for_back_branch_event(methodHandle m) { duke@435: // Delay next back-branch event but pump up invocation counter to triger duke@435: // whole method compilation. duke@435: InvocationCounter* i = m->invocation_counter(); duke@435: InvocationCounter* b = m->backedge_counter(); duke@435: duke@435: // Don't set invocation_counter's value too low otherwise the method will duke@435: // look like immature (ic < ~5300) which prevents the inlining based on duke@435: // the type profiling. duke@435: i->set(i->state(), CompileThreshold); duke@435: // Don't reset counter too low - it is used to check if OSR method is ready. duke@435: b->set(b->state(), CompileThreshold / 2); duke@435: } duke@435: duke@435: // SimpleCompPolicy - compile current method duke@435: duke@435: void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) { duke@435: assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now."); duke@435: duke@435: int hot_count = m->invocation_count(); duke@435: reset_counter_for_invocation_event(m); duke@435: const char* comment = "count"; duke@435: duke@435: if (!delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) { duke@435: nmethod* nm = m->code(); duke@435: if (nm == NULL ) { duke@435: const char* comment = "count"; duke@435: CompileBroker::compile_method(m, InvocationEntryBci, duke@435: m, hot_count, comment, CHECK); duke@435: } else { duke@435: #ifdef TIERED duke@435: duke@435: if (nm->is_compiled_by_c1()) { duke@435: const char* comment = "tier1 overflow"; duke@435: CompileBroker::compile_method(m, InvocationEntryBci, duke@435: m, hot_count, comment, CHECK); duke@435: } duke@435: #endif // TIERED duke@435: } duke@435: } duke@435: } duke@435: duke@435: void SimpleCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) { duke@435: assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now."); duke@435: duke@435: int hot_count = m->backedge_count(); duke@435: const char* comment = "backedge_count"; duke@435: duke@435: if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) { duke@435: CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK); duke@435: duke@435: NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));) duke@435: } duke@435: } duke@435: duke@435: int SimpleCompPolicy::compilation_level(methodHandle m, int branch_bci) duke@435: { duke@435: #ifdef TIERED duke@435: if (!TieredCompilation) { duke@435: return CompLevel_highest_tier; duke@435: } duke@435: if (/* m()->tier1_compile_done() && */ duke@435: // QQQ HACK FIX ME set tier1_compile_done!! duke@435: !m()->is_native()) { duke@435: // Grab the nmethod so it doesn't go away while it's being queried duke@435: nmethod* code = m()->code(); duke@435: if (code != NULL && code->is_compiled_by_c1()) { duke@435: return CompLevel_highest_tier; duke@435: } duke@435: } duke@435: return CompLevel_fast_compile; duke@435: #else duke@435: return CompLevel_highest_tier; duke@435: #endif // TIERED duke@435: } duke@435: duke@435: // StackWalkCompPolicy - walk up stack to find a suitable method to compile duke@435: duke@435: #ifdef COMPILER2 duke@435: const char* StackWalkCompPolicy::_msg = NULL; duke@435: duke@435: duke@435: // Consider m for compilation duke@435: void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) { duke@435: assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now."); duke@435: duke@435: int hot_count = m->invocation_count(); duke@435: reset_counter_for_invocation_event(m); duke@435: const char* comment = "count"; duke@435: duke@435: if (m->code() == NULL && !delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) { duke@435: ResourceMark rm(THREAD); duke@435: JavaThread *thread = (JavaThread*)THREAD; duke@435: frame fr = thread->last_frame(); duke@435: assert(fr.is_interpreted_frame(), "must be interpreted"); duke@435: assert(fr.interpreter_frame_method() == m(), "bad method"); duke@435: duke@435: if (TraceCompilationPolicy) { duke@435: tty->print("method invocation trigger: "); duke@435: m->print_short_name(tty); duke@435: tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size()); duke@435: } duke@435: RegisterMap reg_map(thread, false); duke@435: javaVFrame* triggerVF = thread->last_java_vframe(®_map); duke@435: // triggerVF is the frame that triggered its counter duke@435: RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m); duke@435: duke@435: if (first->top_method()->code() != NULL) { duke@435: // called obsolete method/nmethod -- no need to recompile duke@435: if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code()); duke@435: } else if (compilation_level(m, InvocationEntryBci) == CompLevel_fast_compile) { duke@435: // Tier1 compilation policy avaoids stack walking. duke@435: CompileBroker::compile_method(m, InvocationEntryBci, duke@435: m, hot_count, comment, CHECK); duke@435: } else { duke@435: if (TimeCompilationPolicy) accumulated_time()->start(); duke@435: GrowableArray* stack = new GrowableArray(50); duke@435: stack->push(first); duke@435: RFrame* top = findTopInlinableFrame(stack); duke@435: if (TimeCompilationPolicy) accumulated_time()->stop(); duke@435: assert(top != NULL, "findTopInlinableFrame returned null"); duke@435: if (TraceCompilationPolicy) top->print(); duke@435: CompileBroker::compile_method(top->top_method(), InvocationEntryBci, duke@435: m, hot_count, comment, CHECK); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) { duke@435: assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now."); duke@435: duke@435: int hot_count = m->backedge_count(); duke@435: const char* comment = "backedge_count"; duke@435: duke@435: if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) { duke@435: CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK); duke@435: duke@435: NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));) duke@435: } duke@435: } duke@435: duke@435: int StackWalkCompPolicy::compilation_level(methodHandle m, int osr_bci) duke@435: { duke@435: int comp_level = CompLevel_full_optimization; duke@435: if (TieredCompilation && osr_bci == InvocationEntryBci) { duke@435: if (CompileTheWorld) { duke@435: // Under CTW, the first compile is tier1, the second tier2 duke@435: if (m->highest_tier_compile() == CompLevel_none) { duke@435: comp_level = CompLevel_fast_compile; duke@435: } duke@435: } else if (!m->has_osr_nmethod()) { duke@435: // Before tier1 is done, use invocation_count + backedge_count to duke@435: // compare against the threshold. After that, the counters may/will duke@435: // be reset, so rely on the straight interpreter_invocation_count. duke@435: if (m->highest_tier_compile() == CompLevel_initial_compile) { duke@435: if (m->interpreter_invocation_count() < Tier2CompileThreshold) { duke@435: comp_level = CompLevel_fast_compile; duke@435: } duke@435: } else if (m->invocation_count() + m->backedge_count() < duke@435: Tier2CompileThreshold) { duke@435: comp_level = CompLevel_fast_compile; duke@435: } duke@435: } duke@435: duke@435: } duke@435: return comp_level; duke@435: } duke@435: duke@435: duke@435: RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray* stack) { duke@435: // go up the stack until finding a frame that (probably) won't be inlined duke@435: // into its caller duke@435: RFrame* current = stack->at(0); // current choice for stopping duke@435: assert( current && !current->is_compiled(), "" ); duke@435: const char* msg = NULL; duke@435: duke@435: while (1) { duke@435: duke@435: // before going up the stack further, check if doing so would get us into duke@435: // compiled code duke@435: RFrame* next = senderOf(current, stack); duke@435: if( !next ) // No next frame up the stack? duke@435: break; // Then compile with current frame duke@435: duke@435: methodHandle m = current->top_method(); duke@435: methodHandle next_m = next->top_method(); duke@435: duke@435: if (TraceCompilationPolicy && Verbose) { duke@435: tty->print("[caller: "); duke@435: next_m->print_short_name(tty); duke@435: tty->print("] "); duke@435: } duke@435: duke@435: if( !Inline ) { // Inlining turned off duke@435: msg = "Inlining turned off"; duke@435: break; duke@435: } duke@435: if (next_m->is_not_compilable()) { // Did fail to compile this before/ duke@435: msg = "caller not compilable"; duke@435: break; duke@435: } duke@435: if (next->num() > MaxRecompilationSearchLength) { duke@435: // don't go up too high when searching for recompilees duke@435: msg = "don't go up any further: > MaxRecompilationSearchLength"; duke@435: break; duke@435: } duke@435: if (next->distance() > MaxInterpretedSearchLength) { duke@435: // don't go up too high when searching for recompilees duke@435: msg = "don't go up any further: next > MaxInterpretedSearchLength"; duke@435: break; duke@435: } duke@435: // Compiled frame above already decided not to inline; duke@435: // do not recompile him. duke@435: if (next->is_compiled()) { duke@435: msg = "not going up into optimized code"; duke@435: break; duke@435: } duke@435: duke@435: // Interpreted frame above us was already compiled. Do not force duke@435: // a recompile, although if the frame above us runs long enough an duke@435: // OSR might still happen. duke@435: if( current->is_interpreted() && next_m->has_compiled_code() ) { duke@435: msg = "not going up -- already compiled caller"; duke@435: break; duke@435: } duke@435: duke@435: // Compute how frequent this call site is. We have current method 'm'. duke@435: // We know next method 'next_m' is interpreted. Find the call site and duke@435: // check the various invocation counts. duke@435: int invcnt = 0; // Caller counts duke@435: if (ProfileInterpreter) { duke@435: invcnt = next_m->interpreter_invocation_count(); duke@435: } duke@435: int cnt = 0; // Call site counts duke@435: if (ProfileInterpreter && next_m->method_data() != NULL) { duke@435: ResourceMark rm; duke@435: int bci = next->top_vframe()->bci(); duke@435: ProfileData* data = next_m->method_data()->bci_to_data(bci); duke@435: if (data != NULL && data->is_CounterData()) duke@435: cnt = data->as_CounterData()->count(); duke@435: } duke@435: duke@435: // Caller counts / call-site counts; i.e. is this call site duke@435: // a hot call site for method next_m? duke@435: int freq = (invcnt) ? cnt/invcnt : cnt; duke@435: duke@435: // Check size and frequency limits duke@435: if ((msg = shouldInline(m, freq, cnt)) != NULL) { duke@435: break; duke@435: } duke@435: // Check inlining negative tests duke@435: if ((msg = shouldNotInline(m)) != NULL) { duke@435: break; duke@435: } duke@435: duke@435: duke@435: // If the caller method is too big or something then we do not want to duke@435: // compile it just to inline a method duke@435: if (!canBeCompiled(next_m)) { duke@435: msg = "caller cannot be compiled"; duke@435: break; duke@435: } duke@435: duke@435: if( next_m->name() == vmSymbols::class_initializer_name() ) { duke@435: msg = "do not compile class initializer (OSR ok)"; duke@435: break; duke@435: } duke@435: duke@435: if (TraceCompilationPolicy && Verbose) { duke@435: tty->print("\n\t check caller: "); duke@435: next_m->print_short_name(tty); duke@435: tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)next_m(), next_m->code_size()); duke@435: } duke@435: duke@435: current = next; duke@435: } duke@435: duke@435: assert( !current || !current->is_compiled(), "" ); duke@435: duke@435: if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg); duke@435: duke@435: return current; duke@435: } duke@435: duke@435: RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray* stack) { duke@435: RFrame* sender = rf->caller(); duke@435: if (sender && sender->num() == stack->length()) stack->push(sender); duke@435: return sender; duke@435: } duke@435: duke@435: duke@435: const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) { duke@435: // Allows targeted inlining duke@435: // positive filter: should send be inlined? returns NULL (--> yes) duke@435: // or rejection msg duke@435: int max_size = MaxInlineSize; duke@435: int cost = m->code_size(); duke@435: duke@435: // Check for too many throws (and not too huge) duke@435: if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) { duke@435: return NULL; duke@435: } duke@435: duke@435: // bump the max size if the call is frequent duke@435: if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) { duke@435: if (TraceFrequencyInlining) { duke@435: tty->print("(Inlined frequent method)\n"); duke@435: m->print(); duke@435: } duke@435: max_size = FreqInlineSize; duke@435: } duke@435: if (cost > max_size) { duke@435: return (_msg = "too big"); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) { duke@435: // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg duke@435: if (m->is_abstract()) return (_msg = "abstract method"); duke@435: // note: we allow ik->is_abstract() duke@435: if (!instanceKlass::cast(m->method_holder())->is_initialized()) return (_msg = "method holder not initialized"); duke@435: if (m->is_native()) return (_msg = "native method"); duke@435: nmethod* m_code = m->code(); duke@435: if( m_code != NULL && m_code->instructions_size() > InlineSmallCode ) duke@435: return (_msg = "already compiled into a big method"); duke@435: duke@435: // use frequency-based objections only for non-trivial methods duke@435: if (m->code_size() <= MaxTrivialSize) return NULL; duke@435: if (UseInterpreter) { // don't use counts with -Xcomp duke@435: if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed"); duke@435: if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times"); duke@435: } duke@435: if (methodOopDesc::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes"); duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: duke@435: duke@435: #endif // COMPILER2