Thu, 19 Mar 2009 09:13:24 -0700
Merge
1 /*
2 * Copyright 1998-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_bytecodeInfo.cpp.incl"
28 //=============================================================================
29 //------------------------------InlineTree-------------------------------------
30 InlineTree::InlineTree( Compile* c, const InlineTree *caller_tree, ciMethod* callee, JVMState* caller_jvms, int caller_bci, float site_invoke_ratio )
31 : C(c), _caller_jvms(caller_jvms),
32 _caller_tree((InlineTree*)caller_tree),
33 _method(callee), _site_invoke_ratio(site_invoke_ratio),
34 _count_inline_bcs(method()->code_size()) {
35 NOT_PRODUCT(_count_inlines = 0;)
36 if (_caller_jvms != NULL) {
37 // Keep a private copy of the caller_jvms:
38 _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
39 _caller_jvms->set_bci(caller_jvms->bci());
40 }
41 assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
42 assert((caller_tree == NULL ? 0 : caller_tree->inline_depth() + 1) == inline_depth(), "correct (redundant) depth parameter");
43 assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
44 if (UseOldInlining) {
45 // Update hierarchical counts, count_inline_bcs() and count_inlines()
46 InlineTree *caller = (InlineTree *)caller_tree;
47 for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
48 caller->_count_inline_bcs += count_inline_bcs();
49 NOT_PRODUCT(caller->_count_inlines++;)
50 }
51 }
52 }
54 InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms, float site_invoke_ratio)
55 : C(c), _caller_jvms(caller_jvms), _caller_tree(NULL),
56 _method(callee_method), _site_invoke_ratio(site_invoke_ratio),
57 _count_inline_bcs(method()->code_size()) {
58 NOT_PRODUCT(_count_inlines = 0;)
59 assert(!UseOldInlining, "do not use for old stuff");
60 }
64 static void print_indent(int depth) {
65 tty->print(" ");
66 for (int i = depth; i != 0; --i) tty->print(" ");
67 }
69 static bool is_init_with_ea(ciMethod* callee_method,
70 ciMethod* caller_method, Compile* C) {
71 // True when EA is ON and a java constructor is called or
72 // a super constructor is called from an inlined java constructor.
73 return C->do_escape_analysis() && EliminateAllocations &&
74 ( callee_method->is_initializer() ||
75 (caller_method->is_initializer() &&
76 caller_method != C->method() &&
77 caller_method->holder()->is_subclass_of(callee_method->holder()))
78 );
79 }
81 // positive filter: should send be inlined? returns NULL, if yes, or rejection msg
82 const char* InlineTree::shouldInline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const {
83 // Allows targeted inlining
84 if(callee_method->should_inline()) {
85 *wci_result = *(WarmCallInfo::always_hot());
86 if (PrintInlining && Verbose) {
87 print_indent(inline_depth());
88 tty->print_cr("Inlined method is hot: ");
89 }
90 return NULL;
91 }
93 // positive filter: should send be inlined? returns NULL (--> yes)
94 // or rejection msg
95 int max_size = C->max_inline_size();
96 int size = callee_method->code_size();
98 // Check for too many throws (and not too huge)
99 if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
100 size < InlineThrowMaxSize ) {
101 wci_result->set_profit(wci_result->profit() * 100);
102 if (PrintInlining && Verbose) {
103 print_indent(inline_depth());
104 tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
105 }
106 return NULL;
107 }
109 if (!UseOldInlining) {
110 return NULL; // size and frequency are represented in a new way
111 }
113 int call_site_count = method()->scale_count(profile.count());
114 int invoke_count = method()->interpreter_invocation_count();
115 assert( invoke_count != 0, "Require invokation count greater than zero");
116 int freq = call_site_count/invoke_count;
118 // bump the max size if the call is frequent
119 if ((freq >= InlineFrequencyRatio) ||
120 (call_site_count >= InlineFrequencyCount) ||
121 is_init_with_ea(callee_method, caller_method, C)) {
123 max_size = C->freq_inline_size();
124 if (size <= max_size && TraceFrequencyInlining) {
125 print_indent(inline_depth());
126 tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
127 print_indent(inline_depth());
128 callee_method->print();
129 tty->cr();
130 }
131 } else {
132 // Not hot. Check for medium-sized pre-existing nmethod at cold sites.
133 if (callee_method->has_compiled_code() &&
134 callee_method->instructions_size() > InlineSmallCode/4)
135 return "already compiled into a medium method";
136 }
137 if (size > max_size) {
138 if (max_size > C->max_inline_size())
139 return "hot method too big";
140 return "too big";
141 }
142 return NULL;
143 }
146 // negative filter: should send NOT be inlined? returns NULL, ok to inline, or rejection msg
147 const char* InlineTree::shouldNotInline(ciMethod *callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const {
148 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
149 if (!UseOldInlining) {
150 const char* fail = NULL;
151 if (callee_method->is_abstract()) fail = "abstract method";
152 // note: we allow ik->is_abstract()
153 if (!callee_method->holder()->is_initialized()) fail = "method holder not initialized";
154 if (callee_method->is_native()) fail = "native method";
156 if (fail) {
157 *wci_result = *(WarmCallInfo::always_cold());
158 return fail;
159 }
161 if (callee_method->has_unloaded_classes_in_signature()) {
162 wci_result->set_profit(wci_result->profit() * 0.1);
163 }
165 // don't inline exception code unless the top method belongs to an
166 // exception class
167 if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
168 ciMethod* top_method = caller_jvms() ? caller_jvms()->of_depth(1)->method() : method();
169 if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
170 wci_result->set_profit(wci_result->profit() * 0.1);
171 }
172 }
174 if (callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode) {
175 wci_result->set_profit(wci_result->profit() * 0.1);
176 // %%% adjust wci_result->size()?
177 }
179 return NULL;
180 }
182 // First check all inlining restrictions which are required for correctness
183 if (callee_method->is_abstract()) return "abstract method";
184 // note: we allow ik->is_abstract()
185 if (!callee_method->holder()->is_initialized()) return "method holder not initialized";
186 if (callee_method->is_native()) return "native method";
187 if (callee_method->has_unloaded_classes_in_signature()) return "unloaded signature classes";
189 if (callee_method->should_inline()) {
190 // ignore heuristic controls on inlining
191 return NULL;
192 }
194 // Now perform checks which are heuristic
196 if( callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode )
197 return "already compiled into a big method";
199 // don't inline exception code unless the top method belongs to an
200 // exception class
201 if (caller_tree() != NULL &&
202 callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
203 const InlineTree *top = this;
204 while (top->caller_tree() != NULL) top = top->caller_tree();
205 ciInstanceKlass* k = top->method()->holder();
206 if (!k->is_subclass_of(C->env()->Throwable_klass()))
207 return "exception method";
208 }
210 // use frequency-based objections only for non-trivial methods
211 if (callee_method->code_size() <= MaxTrivialSize) return NULL;
213 // don't use counts with -Xcomp or CTW
214 if (UseInterpreter && !CompileTheWorld) {
216 if (!callee_method->has_compiled_code() &&
217 !callee_method->was_executed_more_than(0)) {
218 return "never executed";
219 }
221 if (is_init_with_ea(callee_method, caller_method, C)) {
223 // Escape Analysis: inline all executed constructors
225 } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
226 CompileThreshold >> 1))) {
227 return "executed < MinInliningThreshold times";
228 }
229 }
231 if (callee_method->should_not_inline()) {
232 return "disallowed by CompilerOracle";
233 }
235 return NULL;
236 }
238 //-----------------------------try_to_inline-----------------------------------
239 // return NULL if ok, reason for not inlining otherwise
240 // Relocated from "InliningClosure::try_to_inline"
241 const char* InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) {
243 // Old algorithm had funny accumulating BC-size counters
244 if (UseOldInlining && ClipInlining
245 && (int)count_inline_bcs() >= DesiredMethodLimit) {
246 return "size > DesiredMethodLimit";
247 }
249 const char *msg = NULL;
250 if ((msg = shouldInline(callee_method, caller_method, caller_bci,
251 profile, wci_result)) != NULL) {
252 return msg;
253 }
254 if ((msg = shouldNotInline(callee_method, caller_method,
255 wci_result)) != NULL) {
256 return msg;
257 }
259 bool is_accessor = InlineAccessors && callee_method->is_accessor();
261 // suppress a few checks for accessors and trivial methods
262 if (!is_accessor && callee_method->code_size() > MaxTrivialSize) {
264 // don't inline into giant methods
265 if (C->unique() > (uint)NodeCountInliningCutoff) {
266 return "NodeCountInliningCutoff";
267 }
269 if ((!UseInterpreter || CompileTheWorld) &&
270 is_init_with_ea(callee_method, caller_method, C)) {
272 // Escape Analysis stress testing when running Xcomp or CTW:
273 // inline constructors even if they are not reached.
275 } else if (profile.count() == 0) {
276 // don't inline unreached call sites
277 return "call site not reached";
278 }
279 }
281 if (!C->do_inlining() && InlineAccessors && !is_accessor) {
282 return "not an accessor";
283 }
284 if( inline_depth() > MaxInlineLevel ) {
285 return "inlining too deep";
286 }
287 if( method() == callee_method &&
288 inline_depth() > MaxRecursiveInlineLevel ) {
289 return "recursively inlining too deep";
290 }
292 int size = callee_method->code_size();
294 if (UseOldInlining && ClipInlining
295 && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
296 return "size > DesiredMethodLimit";
297 }
299 // ok, inline this method
300 return NULL;
301 }
303 //------------------------------pass_initial_checks----------------------------
304 bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
305 ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
306 // Check if a callee_method was suggested
307 if( callee_method == NULL ) return false;
308 // Check if klass of callee_method is loaded
309 if( !callee_holder->is_loaded() ) return false;
310 if( !callee_holder->is_initialized() ) return false;
311 if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
312 // Checks that constant pool's call site has been visited
313 // stricter than callee_holder->is_initialized()
314 ciBytecodeStream iter(caller_method);
315 iter.force_bci(caller_bci);
316 int index = iter.get_index_big();
317 if( !caller_method->is_klass_loaded(index, true) ) {
318 return false;
319 }
320 // Try to do constant pool resolution if running Xcomp
321 Bytecodes::Code call_bc = iter.cur_bc();
322 if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
323 return false;
324 }
325 }
326 // We will attempt to see if a class/field/etc got properly loaded. If it
327 // did not, it may attempt to throw an exception during our probing. Catch
328 // and ignore such exceptions and do not attempt to compile the method.
329 if( callee_method->should_exclude() ) return false;
331 return true;
332 }
334 #ifndef PRODUCT
335 //------------------------------print_inlining---------------------------------
336 // Really, the failure_msg can be a success message also.
337 void InlineTree::print_inlining(ciMethod *callee_method, int caller_bci, const char *failure_msg) const {
338 print_indent(inline_depth());
339 tty->print("@ %d ", caller_bci);
340 if( callee_method ) callee_method->print_short_name();
341 else tty->print(" callee not monotonic or profiled");
342 tty->print(" %s", (failure_msg ? failure_msg : "inline"));
343 if( Verbose && callee_method ) {
344 const InlineTree *top = this;
345 while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
346 tty->print(" bcs: %d+%d invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
347 }
348 tty->cr();
349 }
350 #endif
352 //------------------------------ok_to_inline-----------------------------------
353 WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci) {
354 assert(callee_method != NULL, "caller checks for optimized virtual!");
355 #ifdef ASSERT
356 // Make sure the incoming jvms has the same information content as me.
357 // This means that we can eventually make this whole class AllStatic.
358 if (jvms->caller() == NULL) {
359 assert(_caller_jvms == NULL, "redundant instance state");
360 } else {
361 assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
362 }
363 assert(_method == jvms->method(), "redundant instance state");
364 #endif
365 const char *failure_msg = NULL;
366 int caller_bci = jvms->bci();
367 ciMethod *caller_method = jvms->method();
369 if( !pass_initial_checks(caller_method, caller_bci, callee_method)) {
370 if( PrintInlining ) {
371 failure_msg = "failed_initial_checks";
372 print_inlining( callee_method, caller_bci, failure_msg);
373 }
374 return NULL;
375 }
377 // Check if inlining policy says no.
378 WarmCallInfo wci = *(initial_wci);
379 failure_msg = try_to_inline(callee_method, caller_method, caller_bci, profile, &wci);
380 if (failure_msg != NULL && C->log() != NULL) {
381 C->log()->begin_elem("inline_fail reason='");
382 C->log()->text("%s", failure_msg);
383 C->log()->end_elem("'");
384 }
386 #ifndef PRODUCT
387 if (UseOldInlining && InlineWarmCalls
388 && (PrintOpto || PrintOptoInlining || PrintInlining)) {
389 bool cold = wci.is_cold();
390 bool hot = !cold && wci.is_hot();
391 bool old_cold = (failure_msg != NULL);
392 if (old_cold != cold || (Verbose || WizardMode)) {
393 tty->print(" OldInlining= %4s : %s\n WCI=",
394 old_cold ? "cold" : "hot", failure_msg ? failure_msg : "OK");
395 wci.print();
396 }
397 }
398 #endif
399 if (UseOldInlining) {
400 if (failure_msg == NULL)
401 wci = *(WarmCallInfo::always_hot());
402 else
403 wci = *(WarmCallInfo::always_cold());
404 }
405 if (!InlineWarmCalls) {
406 if (!wci.is_cold() && !wci.is_hot()) {
407 // Do not inline the warm calls.
408 wci = *(WarmCallInfo::always_cold());
409 }
410 }
412 if (!wci.is_cold()) {
413 // In -UseOldInlining, the failure_msg may also be a success message.
414 if (failure_msg == NULL) failure_msg = "inline (hot)";
416 // Inline!
417 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
418 if (UseOldInlining)
419 build_inline_tree_for_callee(callee_method, jvms, caller_bci);
420 if (InlineWarmCalls && !wci.is_hot())
421 return new (C) WarmCallInfo(wci); // copy to heap
422 return WarmCallInfo::always_hot();
423 }
425 // Do not inline
426 if (failure_msg == NULL) failure_msg = "too cold to inline";
427 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
428 return NULL;
429 }
431 //------------------------------compute_callee_frequency-----------------------
432 float InlineTree::compute_callee_frequency( int caller_bci ) const {
433 int count = method()->interpreter_call_site_count(caller_bci);
434 int invcnt = method()->interpreter_invocation_count();
435 float freq = (float)count/(float)invcnt;
436 // Call-site count / interpreter invocation count, scaled recursively.
437 // Always between 0.0 and 1.0. Represents the percentage of the method's
438 // total execution time used at this call site.
440 return freq;
441 }
443 //------------------------------build_inline_tree_for_callee-------------------
444 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
445 float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
446 // Attempt inlining.
447 InlineTree* old_ilt = callee_at(caller_bci, callee_method);
448 if (old_ilt != NULL) {
449 return old_ilt;
450 }
451 InlineTree *ilt = new InlineTree( C, this, callee_method, caller_jvms, caller_bci, recur_frequency );
452 _subtrees.append( ilt );
454 NOT_PRODUCT( _count_inlines += 1; )
456 return ilt;
457 }
460 //---------------------------------------callee_at-----------------------------
461 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
462 for (int i = 0; i < _subtrees.length(); i++) {
463 InlineTree* sub = _subtrees.at(i);
464 if (sub->caller_bci() == bci && callee == sub->method()) {
465 return sub;
466 }
467 }
468 return NULL;
469 }
472 //------------------------------build_inline_tree_root-------------------------
473 InlineTree *InlineTree::build_inline_tree_root() {
474 Compile* C = Compile::current();
476 // Root of inline tree
477 InlineTree *ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F);
479 return ilt;
480 }
483 //-------------------------find_subtree_from_root-----------------------------
484 // Given a jvms, which determines a call chain from the root method,
485 // find the corresponding inline tree.
486 // Note: This method will be removed or replaced as InlineTree goes away.
487 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found) {
488 InlineTree* iltp = root;
489 uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
490 for (uint d = 1; d <= depth; d++) {
491 JVMState* jvmsp = jvms->of_depth(d);
492 // Select the corresponding subtree for this bci.
493 assert(jvmsp->method() == iltp->method(), "tree still in sync");
494 ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
495 InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
496 if (!sub) {
497 if (create_if_not_found && d == depth) {
498 return iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
499 }
500 assert(sub != NULL, "should be a sub-ilt here");
501 return NULL;
502 }
503 iltp = sub;
504 }
505 return iltp;
506 }