Fri, 29 Apr 2016 00:06:10 +0800
Added MIPS 64-bit port.
1 /*
2 * Copyright (c) 2000, 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 */
25 /*
26 * This file has been modified by Loongson Technology in 2015. These
27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
28 * available on the same license terms set forth above.
29 */
31 #include "precompiled.hpp"
32 #include "code/compiledIC.hpp"
33 #include "code/nmethod.hpp"
34 #include "code/scopeDesc.hpp"
35 #include "compiler/compilerOracle.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "oops/methodData.hpp"
38 #include "oops/method.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "prims/nativeLookup.hpp"
41 #include "runtime/advancedThresholdPolicy.hpp"
42 #include "runtime/compilationPolicy.hpp"
43 #include "runtime/frame.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/rframe.hpp"
46 #include "runtime/simpleThresholdPolicy.hpp"
47 #include "runtime/stubRoutines.hpp"
48 #include "runtime/thread.hpp"
49 #include "runtime/timer.hpp"
50 #include "runtime/vframe.hpp"
51 #include "runtime/vm_operations.hpp"
52 #include "utilities/events.hpp"
53 #include "utilities/globalDefinitions.hpp"
55 CompilationPolicy* CompilationPolicy::_policy;
56 elapsedTimer CompilationPolicy::_accumulated_time;
57 bool CompilationPolicy::_in_vm_startup;
59 // Determine compilation policy based on command line argument
60 void compilationPolicy_init() {
61 CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
63 switch(CompilationPolicyChoice) {
64 case 0:
65 CompilationPolicy::set_policy(new SimpleCompPolicy());
66 break;
68 case 1:
69 #ifdef COMPILER2
70 CompilationPolicy::set_policy(new StackWalkCompPolicy());
71 #else
72 Unimplemented();
73 #endif
74 break;
75 case 2:
76 #ifdef TIERED
77 CompilationPolicy::set_policy(new SimpleThresholdPolicy());
78 #else
79 Unimplemented();
80 #endif
81 break;
82 case 3:
83 #ifdef TIERED
84 CompilationPolicy::set_policy(new AdvancedThresholdPolicy());
85 #else
86 Unimplemented();
87 #endif
88 break;
89 default:
90 fatal("CompilationPolicyChoice must be in the range: [0-3]");
91 }
92 CompilationPolicy::policy()->initialize();
93 }
95 void CompilationPolicy::completed_vm_startup() {
96 if (TraceCompilationPolicy) {
97 tty->print("CompilationPolicy: completed vm startup.\n");
98 }
99 _in_vm_startup = false;
100 }
102 // Returns true if m must be compiled before executing it
103 // This is intended to force compiles for methods (usually for
104 // debugging) that would otherwise be interpreted for some reason.
105 bool CompilationPolicy::must_be_compiled(methodHandle m, int comp_level) {
106 // Don't allow Xcomp to cause compiles in replay mode
107 if (ReplayCompiles) return false;
109 if (m->has_compiled_code()) return false; // already compiled
110 if (!can_be_compiled(m, comp_level)) return false;
112 return !UseInterpreter || // must compile all methods
113 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
114 }
116 // Returns true if m is allowed to be compiled
117 bool CompilationPolicy::can_be_compiled(methodHandle m, int comp_level) {
118 // allow any levels for WhiteBox
119 assert(WhiteBoxAPI || comp_level == CompLevel_all || is_compile(comp_level), "illegal compilation level");
121 if (m->is_abstract()) return false;
122 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
124 // Math intrinsics should never be compiled as this can lead to
125 // monotonicity problems because the interpreter will prefer the
126 // compiled code to the intrinsic version. This can't happen in
127 // production because the invocation counter can't be incremented
128 // but we shouldn't expose the system to this problem in testing
129 // modes.
130 if (!AbstractInterpreter::can_be_compiled(m)) {
131 return false;
132 }
133 if (comp_level == CompLevel_all) {
134 if (TieredCompilation) {
135 // enough to be compilable at any level for tiered
136 return !m->is_not_compilable(CompLevel_simple) || !m->is_not_compilable(CompLevel_full_optimization);
137 } else {
138 // must be compilable at available level for non-tiered
139 return !m->is_not_compilable(CompLevel_highest_tier);
140 }
141 } else if (is_compile(comp_level)) {
142 return !m->is_not_compilable(comp_level);
143 }
144 return false;
145 }
147 // Returns true if m is allowed to be osr compiled
148 bool CompilationPolicy::can_be_osr_compiled(methodHandle m, int comp_level) {
149 bool result = false;
150 if (comp_level == CompLevel_all) {
151 if (TieredCompilation) {
152 // enough to be osr compilable at any level for tiered
153 result = !m->is_not_osr_compilable(CompLevel_simple) || !m->is_not_osr_compilable(CompLevel_full_optimization);
154 } else {
155 // must be osr compilable at available level for non-tiered
156 result = !m->is_not_osr_compilable(CompLevel_highest_tier);
157 }
158 } else if (is_compile(comp_level)) {
159 result = !m->is_not_osr_compilable(comp_level);
160 }
161 return (result && can_be_compiled(m, comp_level));
162 }
164 bool CompilationPolicy::is_compilation_enabled() {
165 // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler
166 return !delay_compilation_during_startup() && CompileBroker::should_compile_new_jobs();
167 }
169 #ifndef PRODUCT
170 void CompilationPolicy::print_time() {
171 tty->print_cr ("Accumulated compilationPolicy times:");
172 tty->print_cr ("---------------------------");
173 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
174 }
176 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
177 if (TraceOnStackReplacement) {
178 if (osr_nm == NULL) tty->print_cr("compilation failed");
179 else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
180 }
181 }
182 #endif // !PRODUCT
184 void NonTieredCompPolicy::initialize() {
185 // Setup the compiler thread numbers
186 if (CICompilerCountPerCPU) {
187 // Example: if CICompilerCountPerCPU is true, then we get
188 // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
189 // May help big-app startup time.
190 _compiler_count = MAX2(log2_intptr(os::active_processor_count())-1,1);
191 FLAG_SET_ERGO(intx, CICompilerCount, _compiler_count);
192 } else {
193 _compiler_count = CICompilerCount;
194 }
195 }
197 // Note: this policy is used ONLY if TieredCompilation is off.
198 // compiler_count() behaves the following way:
199 // - with TIERED build (with both COMPILER1 and COMPILER2 defined) it should return
200 // zero for the c1 compilation levels, hence the particular ordering of the
201 // statements.
202 // - the same should happen when COMPILER2 is defined and COMPILER1 is not
203 // (server build without TIERED defined).
204 // - if only COMPILER1 is defined (client build), zero should be returned for
205 // the c2 level.
206 // - if neither is defined - always return zero.
207 int NonTieredCompPolicy::compiler_count(CompLevel comp_level) {
208 assert(!TieredCompilation, "This policy should not be used with TieredCompilation");
209 #ifdef COMPILER2
210 if (is_c2_compile(comp_level)) {
211 return _compiler_count;
212 } else {
213 return 0;
214 }
215 #endif
217 #ifdef COMPILER1
218 if (is_c1_compile(comp_level)) {
219 return _compiler_count;
220 } else {
221 return 0;
222 }
223 #endif
225 return 0;
226 }
228 void NonTieredCompPolicy::reset_counter_for_invocation_event(methodHandle m) {
229 // Make sure invocation and backedge counter doesn't overflow again right away
230 // as would be the case for native methods.
232 // BUT also make sure the method doesn't look like it was never executed.
233 // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
234 MethodCounters* mcs = m->method_counters();
235 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
236 mcs->invocation_counter()->set_carry();
237 mcs->backedge_counter()->set_carry();
239 assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
240 }
242 void NonTieredCompPolicy::reset_counter_for_back_branch_event(methodHandle m) {
243 // Delay next back-branch event but pump up invocation counter to triger
244 // whole method compilation.
245 MethodCounters* mcs = m->method_counters();
246 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
247 InvocationCounter* i = mcs->invocation_counter();
248 InvocationCounter* b = mcs->backedge_counter();
250 // Don't set invocation_counter's value too low otherwise the method will
251 // look like immature (ic < ~5300) which prevents the inlining based on
252 // the type profiling.
253 i->set(i->state(), CompileThreshold);
254 // Don't reset counter too low - it is used to check if OSR method is ready.
255 b->set(b->state(), CompileThreshold / 2);
256 }
258 //
259 // CounterDecay
260 //
261 // Interates through invocation counters and decrements them. This
262 // is done at each safepoint.
263 //
264 class CounterDecay : public AllStatic {
265 static jlong _last_timestamp;
266 static void do_method(Method* m) {
267 MethodCounters* mcs = m->method_counters();
268 if (mcs != NULL) {
269 mcs->invocation_counter()->decay();
270 }
271 }
272 public:
273 static void decay();
274 static bool is_decay_needed() {
275 return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength;
276 }
277 };
279 jlong CounterDecay::_last_timestamp = 0;
281 void CounterDecay::decay() {
282 _last_timestamp = os::javaTimeMillis();
284 // This operation is going to be performed only at the end of a safepoint
285 // and hence GC's will not be going on, all Java mutators are suspended
286 // at this point and hence SystemDictionary_lock is also not needed.
287 assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
288 int nclasses = SystemDictionary::number_of_classes();
289 double classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
290 CounterHalfLifeTime);
291 for (int i = 0; i < classes_per_tick; i++) {
292 Klass* k = SystemDictionary::try_get_next_class();
293 if (k != NULL && k->oop_is_instance()) {
294 InstanceKlass::cast(k)->methods_do(do_method);
295 }
296 }
297 }
299 // Called at the end of the safepoint
300 void NonTieredCompPolicy::do_safepoint_work() {
301 if(UseCounterDecay && CounterDecay::is_decay_needed()) {
302 CounterDecay::decay();
303 }
304 }
306 void NonTieredCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
307 ScopeDesc* sd = trap_scope;
308 MethodCounters* mcs;
309 InvocationCounter* c;
310 for (; !sd->is_top(); sd = sd->sender()) {
311 mcs = sd->method()->method_counters();
312 if (mcs != NULL) {
313 // Reset ICs of inlined methods, since they can trigger compilations also.
314 mcs->invocation_counter()->reset();
315 }
316 }
317 mcs = sd->method()->method_counters();
318 if (mcs != NULL) {
319 c = mcs->invocation_counter();
320 if (is_osr) {
321 // It was an OSR method, so bump the count higher.
322 c->set(c->state(), CompileThreshold);
323 } else {
324 c->reset();
325 }
326 mcs->backedge_counter()->reset();
327 }
328 }
330 // This method can be called by any component of the runtime to notify the policy
331 // that it's recommended to delay the complation of this method.
332 void NonTieredCompPolicy::delay_compilation(Method* method) {
333 MethodCounters* mcs = method->method_counters();
334 if (mcs != NULL) {
335 mcs->invocation_counter()->decay();
336 mcs->backedge_counter()->decay();
337 }
338 }
340 void NonTieredCompPolicy::disable_compilation(Method* method) {
341 MethodCounters* mcs = method->method_counters();
342 if (mcs != NULL) {
343 mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
344 mcs->backedge_counter()->set_state(InvocationCounter::wait_for_nothing);
345 }
346 }
348 #ifdef MIPS64
349 bool NonTieredCompPolicy::compare(CompileTask* task_x, CompileTask* task_y) {
351 if (task_x->weight() > task_y->weight()) {
352 return true;
353 }
355 return false;
356 }
358 void NonTieredCompPolicy::update_speed(jlong t, CompileTask* task) {
359 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();
360 jlong delta_t = t - task->prev_time();
361 Method* m = task->method();
363 int ic = m->interpreter_invocation_count();
364 int bc = m->backedge_count() + m->get_decay_counter();
365 int pre_ic = task->prev_ic_count();
366 int pre_bc = task->prev_bc_count();
368 int delta_e = (ic + bc) - (pre_ic + pre_bc);
370 if (delta_s >= MinUpdateTime) {
371 if (delta_t >= MinUpdateTime && delta_e > 0) {
372 task->set_prev_time(t);
373 task->set_prev_ic_count(ic);
374 task->set_prev_bc_count(bc);
375 int delta_n = FactorOfSizeScheduling * (ic - pre_ic) / 100 + 10 * (bc - pre_bc) / 100;
376 task->set_speed(delta_n * 1.0 / delta_t);
377 task->set_weight();
378 } else
379 if (delta_t > MaxUpdateTime && delta_e == 0) {
380 task->set_speed(0);
381 task->set_weight();
382 }
383 }
384 }
386 bool NonTieredCompPolicy::task_should_be_removed(jlong t, jlong timeout, CompileTask* task) {
387 jlong delta_s = t - SafepointSynchronize::end_of_last_safepoint();
388 jlong delta_t = t - task->prev_time();
389 Method* m = task->method();
391 if (delta_t > timeout && delta_s > timeout) {
392 int ic = m->interpreter_invocation_count();
393 int bc = m->backedge_count() + m->get_decay_counter();
395 if(ic > InvocationOldThreshold || bc > LoopOldThreshold) {
396 // This task is old enough, do not remove it.
397 return false;
398 }
400 return task->speed() < 0.001;
401 }
402 return false;
403 }
405 #endif
407 CompileTask* NonTieredCompPolicy::select_task(CompileQueue* compile_queue) {
408 #ifndef MIPS64
409 return compile_queue->first();
410 #else
411 CompileTask *max_task = NULL;
412 jlong t = os::javaTimeMillis();
414 int counter = 1;
415 for (CompileTask* task = compile_queue->first(); task != NULL;) {
416 CompileTask* next_task = task->next();
417 counter++;
418 if (counter > MaxCompileQueueSize) return max_task;
419 update_speed(t, task);
420 if (max_task == NULL) {
421 max_task = task;
422 } else {
423 if (task_should_be_removed(t, MinWatchTime, task)) {
424 CompileTaskWrapper ctw(task); // Frees the task
425 compile_queue->remove(task);
426 task->method()->clear_queued_for_compilation();
427 task = next_task;
428 continue;
429 }
431 if (compare(task, max_task)) {
432 max_task = task;
433 }
434 }
435 task = next_task;
436 }
438 return max_task;
439 #endif
440 }
442 bool NonTieredCompPolicy::is_mature(Method* method) {
443 MethodData* mdo = method->method_data();
444 assert(mdo != NULL, "Should be");
445 uint current = mdo->mileage_of(method);
446 uint initial = mdo->creation_mileage();
447 if (current < initial)
448 return true; // some sort of overflow
449 uint target;
450 if (ProfileMaturityPercentage <= 0)
451 target = (uint) -ProfileMaturityPercentage; // absolute value
452 else
453 target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
454 return (current >= initial + target);
455 }
457 nmethod* NonTieredCompPolicy::event(methodHandle method, methodHandle inlinee, int branch_bci,
458 int bci, CompLevel comp_level, nmethod* nm, JavaThread* thread) {
459 assert(comp_level == CompLevel_none, "This should be only called from the interpreter");
460 NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci));
461 #ifdef MIPS64
462 method->incr_num_of_requests(1);
463 #endif
464 if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
465 // If certain JVMTI events (e.g. frame pop event) are requested then the
466 // thread is forced to remain in interpreted code. This is
467 // implemented partly by a check in the run_compiled_code
468 // section of the interpreter whether we should skip running
469 // compiled code, and partly by skipping OSR compiles for
470 // interpreted-only threads.
471 if (bci != InvocationEntryBci) {
472 reset_counter_for_back_branch_event(method);
473 return NULL;
474 }
475 }
476 if (CompileTheWorld || ReplayCompiles) {
477 // Don't trigger other compiles in testing mode
478 if (bci == InvocationEntryBci) {
479 reset_counter_for_invocation_event(method);
480 } else {
481 reset_counter_for_back_branch_event(method);
482 }
483 return NULL;
484 }
486 if (bci == InvocationEntryBci) {
487 // when code cache is full, compilation gets switched off, UseCompiler
488 // is set to false
489 if (!method->has_compiled_code() && UseCompiler) {
490 method_invocation_event(method, thread);
491 } else {
492 // Force counter overflow on method entry, even if no compilation
493 // happened. (The method_invocation_event call does this also.)
494 reset_counter_for_invocation_event(method);
495 }
496 // compilation at an invocation overflow no longer goes and retries test for
497 // compiled method. We always run the loser of the race as interpreted.
498 // so return NULL
499 return NULL;
500 } else {
501 // counter overflow in a loop => try to do on-stack-replacement
502 nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
503 NOT_PRODUCT(trace_osr_request(method, osr_nm, bci));
504 // when code cache is full, we should not compile any more...
505 if (osr_nm == NULL && UseCompiler) {
506 method_back_branch_event(method, bci, thread);
507 osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
508 }
509 if (osr_nm == NULL) {
510 reset_counter_for_back_branch_event(method);
511 return NULL;
512 }
513 return osr_nm;
514 }
515 return NULL;
516 }
518 #ifndef PRODUCT
519 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
520 void NonTieredCompPolicy::trace_frequency_counter_overflow(methodHandle m, int branch_bci, int bci) {
521 if (TraceInvocationCounterOverflow) {
522 MethodCounters* mcs = m->method_counters();
523 assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
524 InvocationCounter* ic = mcs->invocation_counter();
525 InvocationCounter* bc = mcs->backedge_counter();
526 ResourceMark rm;
527 const char* msg =
528 bci == InvocationEntryBci
529 ? "comp-policy cntr ovfl @ %d in entry of "
530 : "comp-policy cntr ovfl @ %d in loop of ";
531 PRAGMA_DIAG_PUSH
532 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
533 tty->print(msg, bci);
534 PRAGMA_DIAG_POP
535 m->print_value();
536 tty->cr();
537 ic->print();
538 bc->print();
539 if (ProfileInterpreter) {
540 if (bci != InvocationEntryBci) {
541 MethodData* mdo = m->method_data();
542 if (mdo != NULL) {
543 int count = mdo->bci_to_data(branch_bci)->as_JumpData()->taken();
544 tty->print_cr("back branch count = %d", count);
545 }
546 }
547 }
548 }
549 }
551 void NonTieredCompPolicy::trace_osr_request(methodHandle method, nmethod* osr, int bci) {
552 if (TraceOnStackReplacement) {
553 ResourceMark rm;
554 tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
555 method->print_short_name(tty);
556 tty->print_cr(" at bci %d", bci);
557 }
558 }
559 #endif // !PRODUCT
561 // SimpleCompPolicy - compile current method
563 void SimpleCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) {
564 const int comp_level = CompLevel_highest_tier;
565 const int hot_count = m->invocation_count();
566 #ifdef MIPS64
567 const int bc = m->backedge_count();
568 #endif
570 reset_counter_for_invocation_event(m);
571 #ifdef MIPS64
572 const int new_bc = m->backedge_count();
573 const int delta = bc - new_bc;
574 #endif
575 const char* comment = "count";
577 #ifdef MIPS64
578 if(delta > 0) m->incr_decay_counter(delta);
579 #endif
581 if (is_compilation_enabled() && can_be_compiled(m, comp_level)) {
582 nmethod* nm = m->code();
583 if (nm == NULL ) {
584 CompileBroker::compile_method(m, InvocationEntryBci, comp_level, m, hot_count, comment, thread);
585 }
586 }
587 }
589 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) {
590 const int comp_level = CompLevel_highest_tier;
591 const int hot_count = m->backedge_count();
592 const char* comment = "backedge_count";
594 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
595 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread);
596 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
597 }
598 }
599 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
601 #ifdef COMPILER2
602 const char* StackWalkCompPolicy::_msg = NULL;
605 // Consider m for compilation
606 void StackWalkCompPolicy::method_invocation_event(methodHandle m, JavaThread* thread) {
607 const int comp_level = CompLevel_highest_tier;
608 const int hot_count = m->invocation_count();
609 reset_counter_for_invocation_event(m);
610 const char* comment = "count";
612 if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) {
613 ResourceMark rm(thread);
614 frame fr = thread->last_frame();
615 assert(fr.is_interpreted_frame(), "must be interpreted");
616 assert(fr.interpreter_frame_method() == m(), "bad method");
618 if (TraceCompilationPolicy) {
619 tty->print("method invocation trigger: ");
620 m->print_short_name(tty);
621 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)m()), m->code_size());
622 }
623 RegisterMap reg_map(thread, false);
624 javaVFrame* triggerVF = thread->last_java_vframe(®_map);
625 // triggerVF is the frame that triggered its counter
626 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
628 if (first->top_method()->code() != NULL) {
629 // called obsolete method/nmethod -- no need to recompile
630 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, p2i(first->top_method()->code()));
631 } else {
632 if (TimeCompilationPolicy) accumulated_time()->start();
633 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
634 stack->push(first);
635 RFrame* top = findTopInlinableFrame(stack);
636 if (TimeCompilationPolicy) accumulated_time()->stop();
637 assert(top != NULL, "findTopInlinableFrame returned null");
638 if (TraceCompilationPolicy) top->print();
639 CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level,
640 m, hot_count, comment, thread);
641 }
642 }
643 }
645 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int bci, JavaThread* thread) {
646 const int comp_level = CompLevel_highest_tier;
647 const int hot_count = m->backedge_count();
648 const char* comment = "backedge_count";
650 if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
651 CompileBroker::compile_method(m, bci, comp_level, m, hot_count, comment, thread);
652 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
653 }
654 }
656 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
657 // go up the stack until finding a frame that (probably) won't be inlined
658 // into its caller
659 RFrame* current = stack->at(0); // current choice for stopping
660 assert( current && !current->is_compiled(), "" );
661 const char* msg = NULL;
663 while (1) {
665 // before going up the stack further, check if doing so would get us into
666 // compiled code
667 RFrame* next = senderOf(current, stack);
668 if( !next ) // No next frame up the stack?
669 break; // Then compile with current frame
671 methodHandle m = current->top_method();
672 methodHandle next_m = next->top_method();
674 if (TraceCompilationPolicy && Verbose) {
675 tty->print("[caller: ");
676 next_m->print_short_name(tty);
677 tty->print("] ");
678 }
680 if( !Inline ) { // Inlining turned off
681 msg = "Inlining turned off";
682 break;
683 }
684 if (next_m->is_not_compilable()) { // Did fail to compile this before/
685 msg = "caller not compilable";
686 break;
687 }
688 if (next->num() > MaxRecompilationSearchLength) {
689 // don't go up too high when searching for recompilees
690 msg = "don't go up any further: > MaxRecompilationSearchLength";
691 break;
692 }
693 if (next->distance() > MaxInterpretedSearchLength) {
694 // don't go up too high when searching for recompilees
695 msg = "don't go up any further: next > MaxInterpretedSearchLength";
696 break;
697 }
698 // Compiled frame above already decided not to inline;
699 // do not recompile him.
700 if (next->is_compiled()) {
701 msg = "not going up into optimized code";
702 break;
703 }
705 // Interpreted frame above us was already compiled. Do not force
706 // a recompile, although if the frame above us runs long enough an
707 // OSR might still happen.
708 if( current->is_interpreted() && next_m->has_compiled_code() ) {
709 msg = "not going up -- already compiled caller";
710 break;
711 }
713 // Compute how frequent this call site is. We have current method 'm'.
714 // We know next method 'next_m' is interpreted. Find the call site and
715 // check the various invocation counts.
716 int invcnt = 0; // Caller counts
717 if (ProfileInterpreter) {
718 invcnt = next_m->interpreter_invocation_count();
719 }
720 int cnt = 0; // Call site counts
721 if (ProfileInterpreter && next_m->method_data() != NULL) {
722 ResourceMark rm;
723 int bci = next->top_vframe()->bci();
724 ProfileData* data = next_m->method_data()->bci_to_data(bci);
725 if (data != NULL && data->is_CounterData())
726 cnt = data->as_CounterData()->count();
727 }
729 // Caller counts / call-site counts; i.e. is this call site
730 // a hot call site for method next_m?
731 int freq = (invcnt) ? cnt/invcnt : cnt;
733 // Check size and frequency limits
734 if ((msg = shouldInline(m, freq, cnt)) != NULL) {
735 break;
736 }
737 // Check inlining negative tests
738 if ((msg = shouldNotInline(m)) != NULL) {
739 break;
740 }
743 // If the caller method is too big or something then we do not want to
744 // compile it just to inline a method
745 if (!can_be_compiled(next_m, CompLevel_any)) {
746 msg = "caller cannot be compiled";
747 break;
748 }
750 if( next_m->name() == vmSymbols::class_initializer_name() ) {
751 msg = "do not compile class initializer (OSR ok)";
752 break;
753 }
755 if (TraceCompilationPolicy && Verbose) {
756 tty->print("\n\t check caller: ");
757 next_m->print_short_name(tty);
758 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)next_m()), next_m->code_size());
759 }
761 current = next;
762 }
764 assert( !current || !current->is_compiled(), "" );
766 if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
768 return current;
769 }
771 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
772 RFrame* sender = rf->caller();
773 if (sender && sender->num() == stack->length()) stack->push(sender);
774 return sender;
775 }
778 const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) {
779 // Allows targeted inlining
780 // positive filter: should send be inlined? returns NULL (--> yes)
781 // or rejection msg
782 int max_size = MaxInlineSize;
783 int cost = m->code_size();
785 // Check for too many throws (and not too huge)
786 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
787 return NULL;
788 }
790 // bump the max size if the call is frequent
791 if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
792 if (TraceFrequencyInlining) {
793 tty->print("(Inlined frequent method)\n");
794 m->print();
795 }
796 max_size = FreqInlineSize;
797 }
798 if (cost > max_size) {
799 return (_msg = "too big");
800 }
801 return NULL;
802 }
805 const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) {
806 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
807 if (m->is_abstract()) return (_msg = "abstract method");
808 // note: we allow ik->is_abstract()
809 if (!m->method_holder()->is_initialized()) return (_msg = "method holder not initialized");
810 if (m->is_native()) return (_msg = "native method");
811 nmethod* m_code = m->code();
812 if (m_code != NULL && m_code->code_size() > InlineSmallCode)
813 return (_msg = "already compiled into a big method");
815 // use frequency-based objections only for non-trivial methods
816 if (m->code_size() <= MaxTrivialSize) return NULL;
817 if (UseInterpreter) { // don't use counts with -Xcomp
818 if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
819 if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
820 }
821 if (Method::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
823 return NULL;
824 }
828 #endif // COMPILER2