49 default:
50 fatal("CompilationPolicyChoice must be in the range: [0-1]");
51 }
52 }
53
54 void CompilationPolicy::completed_vm_startup() {
55 if (TraceCompilationPolicy) {
56 tty->print("CompilationPolicy: completed vm startup.\n");
57 }
58 _in_vm_startup = false;
59 }
60
61 // Returns true if m must be compiled before executing it
62 // This is intended to force compiles for methods (usually for
63 // debugging) that would otherwise be interpreted for some reason.
64 bool CompilationPolicy::mustBeCompiled(methodHandle m) {
65 if (m->has_compiled_code()) return false; // already compiled
66 if (!canBeCompiled(m)) return false;
67
68 return !UseInterpreter || // must compile all methods
69 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops()); // eagerly compile loop methods
70 }
71
72 // Returns true if m is allowed to be compiled
73 bool CompilationPolicy::canBeCompiled(methodHandle m) {
74 if (m->is_abstract()) return false;
75 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
76
77 return !m->is_not_compilable();
78 }
79
80 #ifndef PRODUCT
81 void CompilationPolicy::print_time() {
82 tty->print_cr ("Accumulated compilationPolicy times:");
83 tty->print_cr ("---------------------------");
84 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
85 }
86
87 static void trace_osr_completion(nmethod* osr_nm) {
88 if (TraceOnStackReplacement) {
89 if (osr_nm == NULL) tty->print_cr("compilation failed");
110 InvocationCounter* i = m->invocation_counter();
111 InvocationCounter* b = m->backedge_counter();
112
113 // Don't set invocation_counter's value too low otherwise the method will
114 // look like immature (ic < ~5300) which prevents the inlining based on
115 // the type profiling.
116 i->set(i->state(), CompileThreshold);
117 // Don't reset counter too low - it is used to check if OSR method is ready.
118 b->set(b->state(), CompileThreshold / 2);
119 }
120
121 // SimpleCompPolicy - compile current method
122
123 void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) {
124 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
125
126 int hot_count = m->invocation_count();
127 reset_counter_for_invocation_event(m);
128 const char* comment = "count";
129
130 if (!delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
131 nmethod* nm = m->code();
132 if (nm == NULL ) {
133 const char* comment = "count";
134 CompileBroker::compile_method(m, InvocationEntryBci,
135 m, hot_count, comment, CHECK);
136 } else {
137 #ifdef TIERED
138
139 if (nm->is_compiled_by_c1()) {
140 const char* comment = "tier1 overflow";
141 CompileBroker::compile_method(m, InvocationEntryBci,
142 m, hot_count, comment, CHECK);
143 }
144 #endif // TIERED
145 }
146 }
147 }
148
149 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
150 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
151
152 int hot_count = m->backedge_count();
153 const char* comment = "backedge_count";
154
155 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
156 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
157
158 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
159 }
160 }
161
162 int SimpleCompPolicy::compilation_level(methodHandle m, int branch_bci)
163 {
164 #ifdef TIERED
165 if (!TieredCompilation) {
166 return CompLevel_highest_tier;
167 }
168 if (/* m()->tier1_compile_done() && */
169 // QQQ HACK FIX ME set tier1_compile_done!!
170 !m()->is_native()) {
171 // Grab the nmethod so it doesn't go away while it's being queried
172 nmethod* code = m()->code();
173 if (code != NULL && code->is_compiled_by_c1()) {
174 return CompLevel_highest_tier;
175 }
177 return CompLevel_fast_compile;
178 #else
179 return CompLevel_highest_tier;
180 #endif // TIERED
181 }
182
183 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
184
185 #ifdef COMPILER2
186 const char* StackWalkCompPolicy::_msg = NULL;
187
188
189 // Consider m for compilation
190 void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) {
191 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
192
193 int hot_count = m->invocation_count();
194 reset_counter_for_invocation_event(m);
195 const char* comment = "count";
196
197 if (m->code() == NULL && !delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
198 ResourceMark rm(THREAD);
199 JavaThread *thread = (JavaThread*)THREAD;
200 frame fr = thread->last_frame();
201 assert(fr.is_interpreted_frame(), "must be interpreted");
202 assert(fr.interpreter_frame_method() == m(), "bad method");
203
204 if (TraceCompilationPolicy) {
205 tty->print("method invocation trigger: ");
206 m->print_short_name(tty);
207 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size());
208 }
209 RegisterMap reg_map(thread, false);
210 javaVFrame* triggerVF = thread->last_java_vframe(®_map);
211 // triggerVF is the frame that triggered its counter
212 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
213
214 if (first->top_method()->code() != NULL) {
215 // called obsolete method/nmethod -- no need to recompile
216 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code());
217 } else if (compilation_level(m, InvocationEntryBci) == CompLevel_fast_compile) {
221 } else {
222 if (TimeCompilationPolicy) accumulated_time()->start();
223 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
224 stack->push(first);
225 RFrame* top = findTopInlinableFrame(stack);
226 if (TimeCompilationPolicy) accumulated_time()->stop();
227 assert(top != NULL, "findTopInlinableFrame returned null");
228 if (TraceCompilationPolicy) top->print();
229 CompileBroker::compile_method(top->top_method(), InvocationEntryBci,
230 m, hot_count, comment, CHECK);
231 }
232 }
233 }
234
235 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
236 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
237
238 int hot_count = m->backedge_count();
239 const char* comment = "backedge_count";
240
241 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
242 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
243
244 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
245 }
246 }
247
248 int StackWalkCompPolicy::compilation_level(methodHandle m, int osr_bci)
249 {
250 int comp_level = CompLevel_full_optimization;
251 if (TieredCompilation && osr_bci == InvocationEntryBci) {
252 if (CompileTheWorld) {
253 // Under CTW, the first compile is tier1, the second tier2
254 if (m->highest_tier_compile() == CompLevel_none) {
255 comp_level = CompLevel_fast_compile;
256 }
257 } else if (!m->has_osr_nmethod()) {
258 // Before tier1 is done, use invocation_count + backedge_count to
259 // compare against the threshold. After that, the counters may/will
260 // be reset, so rely on the straight interpreter_invocation_count.
261 if (m->highest_tier_compile() == CompLevel_initial_compile) {
|
49 default:
50 fatal("CompilationPolicyChoice must be in the range: [0-1]");
51 }
52 }
53
54 void CompilationPolicy::completed_vm_startup() {
55 if (TraceCompilationPolicy) {
56 tty->print("CompilationPolicy: completed vm startup.\n");
57 }
58 _in_vm_startup = false;
59 }
60
61 // Returns true if m must be compiled before executing it
62 // This is intended to force compiles for methods (usually for
63 // debugging) that would otherwise be interpreted for some reason.
64 bool CompilationPolicy::mustBeCompiled(methodHandle m) {
65 if (m->has_compiled_code()) return false; // already compiled
66 if (!canBeCompiled(m)) return false;
67
68 return !UseInterpreter || // must compile all methods
69 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
70 }
71
72 // Returns true if m is allowed to be compiled
73 bool CompilationPolicy::canBeCompiled(methodHandle m) {
74 if (m->is_abstract()) return false;
75 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
76
77 return !m->is_not_compilable();
78 }
79
80 #ifndef PRODUCT
81 void CompilationPolicy::print_time() {
82 tty->print_cr ("Accumulated compilationPolicy times:");
83 tty->print_cr ("---------------------------");
84 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
85 }
86
87 static void trace_osr_completion(nmethod* osr_nm) {
88 if (TraceOnStackReplacement) {
89 if (osr_nm == NULL) tty->print_cr("compilation failed");
110 InvocationCounter* i = m->invocation_counter();
111 InvocationCounter* b = m->backedge_counter();
112
113 // Don't set invocation_counter's value too low otherwise the method will
114 // look like immature (ic < ~5300) which prevents the inlining based on
115 // the type profiling.
116 i->set(i->state(), CompileThreshold);
117 // Don't reset counter too low - it is used to check if OSR method is ready.
118 b->set(b->state(), CompileThreshold / 2);
119 }
120
121 // SimpleCompPolicy - compile current method
122
123 void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) {
124 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
125
126 int hot_count = m->invocation_count();
127 reset_counter_for_invocation_event(m);
128 const char* comment = "count";
129
130 if (!delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler && CompileBroker::should_compile_new_jobs()) {
131 nmethod* nm = m->code();
132 if (nm == NULL ) {
133 const char* comment = "count";
134 CompileBroker::compile_method(m, InvocationEntryBci,
135 m, hot_count, comment, CHECK);
136 } else {
137 #ifdef TIERED
138
139 if (nm->is_compiled_by_c1()) {
140 const char* comment = "tier1 overflow";
141 CompileBroker::compile_method(m, InvocationEntryBci,
142 m, hot_count, comment, CHECK);
143 }
144 #endif // TIERED
145 }
146 }
147 }
148
149 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
150 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
151
152 int hot_count = m->backedge_count();
153 const char* comment = "backedge_count";
154
155 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m) && CompileBroker::should_compile_new_jobs()) {
156 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
157
158 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
159 }
160 }
161
162 int SimpleCompPolicy::compilation_level(methodHandle m, int branch_bci)
163 {
164 #ifdef TIERED
165 if (!TieredCompilation) {
166 return CompLevel_highest_tier;
167 }
168 if (/* m()->tier1_compile_done() && */
169 // QQQ HACK FIX ME set tier1_compile_done!!
170 !m()->is_native()) {
171 // Grab the nmethod so it doesn't go away while it's being queried
172 nmethod* code = m()->code();
173 if (code != NULL && code->is_compiled_by_c1()) {
174 return CompLevel_highest_tier;
175 }
177 return CompLevel_fast_compile;
178 #else
179 return CompLevel_highest_tier;
180 #endif // TIERED
181 }
182
183 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
184
185 #ifdef COMPILER2
186 const char* StackWalkCompPolicy::_msg = NULL;
187
188
189 // Consider m for compilation
190 void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) {
191 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
192
193 int hot_count = m->invocation_count();
194 reset_counter_for_invocation_event(m);
195 const char* comment = "count";
196
197 if (m->code() == NULL && !delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler && CompileBroker::should_compile_new_jobs()) {
198 ResourceMark rm(THREAD);
199 JavaThread *thread = (JavaThread*)THREAD;
200 frame fr = thread->last_frame();
201 assert(fr.is_interpreted_frame(), "must be interpreted");
202 assert(fr.interpreter_frame_method() == m(), "bad method");
203
204 if (TraceCompilationPolicy) {
205 tty->print("method invocation trigger: ");
206 m->print_short_name(tty);
207 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size());
208 }
209 RegisterMap reg_map(thread, false);
210 javaVFrame* triggerVF = thread->last_java_vframe(®_map);
211 // triggerVF is the frame that triggered its counter
212 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
213
214 if (first->top_method()->code() != NULL) {
215 // called obsolete method/nmethod -- no need to recompile
216 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code());
217 } else if (compilation_level(m, InvocationEntryBci) == CompLevel_fast_compile) {
221 } else {
222 if (TimeCompilationPolicy) accumulated_time()->start();
223 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
224 stack->push(first);
225 RFrame* top = findTopInlinableFrame(stack);
226 if (TimeCompilationPolicy) accumulated_time()->stop();
227 assert(top != NULL, "findTopInlinableFrame returned null");
228 if (TraceCompilationPolicy) top->print();
229 CompileBroker::compile_method(top->top_method(), InvocationEntryBci,
230 m, hot_count, comment, CHECK);
231 }
232 }
233 }
234
235 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
236 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
237
238 int hot_count = m->backedge_count();
239 const char* comment = "backedge_count";
240
241 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m) && CompileBroker::should_compile_new_jobs()) {
242 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
243
244 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
245 }
246 }
247
248 int StackWalkCompPolicy::compilation_level(methodHandle m, int osr_bci)
249 {
250 int comp_level = CompLevel_full_optimization;
251 if (TieredCompilation && osr_bci == InvocationEntryBci) {
252 if (CompileTheWorld) {
253 // Under CTW, the first compile is tier1, the second tier2
254 if (m->highest_tier_compile() == CompLevel_none) {
255 comp_level = CompLevel_fast_compile;
256 }
257 } else if (!m->has_osr_nmethod()) {
258 // Before tier1 is done, use invocation_count + backedge_count to
259 // compare against the threshold. After that, the counters may/will
260 // be reset, so rely on the straight interpreter_invocation_count.
261 if (m->highest_tier_compile() == CompLevel_initial_compile) {
|