Mon, 28 Mar 2011 03:58:07 -0700
7022998: JSR 292 recursive method handle calls inline themselves infinitely
Reviewed-by: never, kvn
1 /*
2 * Copyright (c) 1998, 2011, 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 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "compiler/compileBroker.hpp"
29 #include "compiler/compileLog.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "oops/objArrayKlass.hpp"
32 #include "opto/callGenerator.hpp"
33 #include "opto/parse.hpp"
34 #include "runtime/handles.inline.hpp"
36 //=============================================================================
37 //------------------------------InlineTree-------------------------------------
38 InlineTree::InlineTree( Compile* c,
39 const InlineTree *caller_tree, ciMethod* callee,
40 JVMState* caller_jvms, int caller_bci,
41 float site_invoke_ratio, int site_depth_adjust)
42 : C(c), _caller_jvms(caller_jvms),
43 _caller_tree((InlineTree*)caller_tree),
44 _method(callee), _site_invoke_ratio(site_invoke_ratio),
45 _site_depth_adjust(site_depth_adjust),
46 _count_inline_bcs(method()->code_size())
47 {
48 NOT_PRODUCT(_count_inlines = 0;)
49 if (_caller_jvms != NULL) {
50 // Keep a private copy of the caller_jvms:
51 _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
52 _caller_jvms->set_bci(caller_jvms->bci());
53 assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");
54 }
55 assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
56 assert((caller_tree == NULL ? 0 : caller_tree->stack_depth() + 1) == stack_depth(), "correct (redundant) depth parameter");
57 assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
58 if (UseOldInlining) {
59 // Update hierarchical counts, count_inline_bcs() and count_inlines()
60 InlineTree *caller = (InlineTree *)caller_tree;
61 for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
62 caller->_count_inline_bcs += count_inline_bcs();
63 NOT_PRODUCT(caller->_count_inlines++;)
64 }
65 }
66 }
68 InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms,
69 float site_invoke_ratio, int site_depth_adjust)
70 : C(c), _caller_jvms(caller_jvms), _caller_tree(NULL),
71 _method(callee_method), _site_invoke_ratio(site_invoke_ratio),
72 _site_depth_adjust(site_depth_adjust),
73 _count_inline_bcs(method()->code_size())
74 {
75 NOT_PRODUCT(_count_inlines = 0;)
76 assert(!UseOldInlining, "do not use for old stuff");
77 }
79 static bool is_init_with_ea(ciMethod* callee_method,
80 ciMethod* caller_method, Compile* C) {
81 // True when EA is ON and a java constructor is called or
82 // a super constructor is called from an inlined java constructor.
83 return C->do_escape_analysis() && EliminateAllocations &&
84 ( callee_method->is_initializer() ||
85 (caller_method->is_initializer() &&
86 caller_method != C->method() &&
87 caller_method->holder()->is_subclass_of(callee_method->holder()))
88 );
89 }
91 // positive filter: should send be inlined? returns NULL, if yes, or rejection msg
92 const char* InlineTree::shouldInline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const {
93 // Allows targeted inlining
94 if(callee_method->should_inline()) {
95 *wci_result = *(WarmCallInfo::always_hot());
96 if (PrintInlining && Verbose) {
97 CompileTask::print_inline_indent(inline_depth());
98 tty->print_cr("Inlined method is hot: ");
99 }
100 return NULL;
101 }
103 // positive filter: should send be inlined? returns NULL (--> yes)
104 // or rejection msg
105 int max_size = C->max_inline_size();
106 int size = callee_method->code_size();
108 // Check for too many throws (and not too huge)
109 if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
110 size < InlineThrowMaxSize ) {
111 wci_result->set_profit(wci_result->profit() * 100);
112 if (PrintInlining && Verbose) {
113 CompileTask::print_inline_indent(inline_depth());
114 tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
115 }
116 return NULL;
117 }
119 if (!UseOldInlining) {
120 return NULL; // size and frequency are represented in a new way
121 }
123 int call_site_count = method()->scale_count(profile.count());
124 int invoke_count = method()->interpreter_invocation_count();
125 assert( invoke_count != 0, "Require invokation count greater than zero");
126 int freq = call_site_count/invoke_count;
128 // bump the max size if the call is frequent
129 if ((freq >= InlineFrequencyRatio) ||
130 (call_site_count >= InlineFrequencyCount) ||
131 is_init_with_ea(callee_method, caller_method, C)) {
133 max_size = C->freq_inline_size();
134 if (size <= max_size && TraceFrequencyInlining) {
135 CompileTask::print_inline_indent(inline_depth());
136 tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
137 CompileTask::print_inline_indent(inline_depth());
138 callee_method->print();
139 tty->cr();
140 }
141 } else {
142 // Not hot. Check for medium-sized pre-existing nmethod at cold sites.
143 if (callee_method->has_compiled_code() &&
144 callee_method->instructions_size(CompLevel_full_optimization) > InlineSmallCode/4)
145 return "already compiled into a medium method";
146 }
147 if (size > max_size) {
148 if (max_size > C->max_inline_size())
149 return "hot method too big";
150 return "too big";
151 }
152 return NULL;
153 }
156 // negative filter: should send NOT be inlined? returns NULL, ok to inline, or rejection msg
157 const char* InlineTree::shouldNotInline(ciMethod *callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const {
158 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
159 if (!UseOldInlining) {
160 const char* fail = NULL;
161 if (callee_method->is_abstract()) fail = "abstract method";
162 // note: we allow ik->is_abstract()
163 if (!callee_method->holder()->is_initialized()) fail = "method holder not initialized";
164 if (callee_method->is_native()) fail = "native method";
166 if (fail) {
167 *wci_result = *(WarmCallInfo::always_cold());
168 return fail;
169 }
171 if (callee_method->has_unloaded_classes_in_signature()) {
172 wci_result->set_profit(wci_result->profit() * 0.1);
173 }
175 // don't inline exception code unless the top method belongs to an
176 // exception class
177 if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
178 ciMethod* top_method = caller_jvms() ? caller_jvms()->of_depth(1)->method() : method();
179 if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
180 wci_result->set_profit(wci_result->profit() * 0.1);
181 }
182 }
184 if (callee_method->has_compiled_code() && callee_method->instructions_size(CompLevel_full_optimization) > InlineSmallCode) {
185 wci_result->set_profit(wci_result->profit() * 0.1);
186 // %%% adjust wci_result->size()?
187 }
189 return NULL;
190 }
192 // Always inline MethodHandle methods and generated MethodHandle adapters.
193 if (callee_method->is_method_handle_invoke() || callee_method->is_method_handle_adapter())
194 return NULL;
196 // First check all inlining restrictions which are required for correctness
197 if (callee_method->is_abstract()) return "abstract method";
198 // note: we allow ik->is_abstract()
199 if (!callee_method->holder()->is_initialized()) return "method holder not initialized";
200 if (callee_method->is_native()) return "native method";
201 if (callee_method->has_unloaded_classes_in_signature()) return "unloaded signature classes";
203 if (callee_method->should_inline()) {
204 // ignore heuristic controls on inlining
205 return NULL;
206 }
208 // Now perform checks which are heuristic
210 if( callee_method->has_compiled_code() && callee_method->instructions_size(CompLevel_full_optimization) > InlineSmallCode )
211 return "already compiled into a big method";
213 // don't inline exception code unless the top method belongs to an
214 // exception class
215 if (caller_tree() != NULL &&
216 callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
217 const InlineTree *top = this;
218 while (top->caller_tree() != NULL) top = top->caller_tree();
219 ciInstanceKlass* k = top->method()->holder();
220 if (!k->is_subclass_of(C->env()->Throwable_klass()))
221 return "exception method";
222 }
224 // use frequency-based objections only for non-trivial methods
225 if (callee_method->code_size() <= MaxTrivialSize) return NULL;
227 // don't use counts with -Xcomp or CTW
228 if (UseInterpreter && !CompileTheWorld) {
230 if (!callee_method->has_compiled_code() &&
231 !callee_method->was_executed_more_than(0)) {
232 return "never executed";
233 }
235 if (is_init_with_ea(callee_method, caller_method, C)) {
237 // Escape Analysis: inline all executed constructors
239 } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
240 CompileThreshold >> 1))) {
241 return "executed < MinInliningThreshold times";
242 }
243 }
245 if (callee_method->should_not_inline()) {
246 return "disallowed by CompilerOracle";
247 }
249 if (UseStringCache) {
250 // Do not inline StringCache::profile() method used only at the beginning.
251 if (callee_method->name() == ciSymbol::profile_name() &&
252 callee_method->holder()->name() == ciSymbol::java_lang_StringCache()) {
253 return "profiling method";
254 }
255 }
257 return NULL;
258 }
260 //-----------------------------try_to_inline-----------------------------------
261 // return NULL if ok, reason for not inlining otherwise
262 // Relocated from "InliningClosure::try_to_inline"
263 const char* InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) {
265 // Old algorithm had funny accumulating BC-size counters
266 if (UseOldInlining && ClipInlining
267 && (int)count_inline_bcs() >= DesiredMethodLimit) {
268 return "size > DesiredMethodLimit";
269 }
271 const char *msg = NULL;
272 if ((msg = shouldInline(callee_method, caller_method, caller_bci,
273 profile, wci_result)) != NULL) {
274 return msg;
275 }
276 if ((msg = shouldNotInline(callee_method, caller_method,
277 wci_result)) != NULL) {
278 return msg;
279 }
281 if (InlineAccessors && callee_method->is_accessor()) {
282 // accessor methods are not subject to any of the following limits.
283 return NULL;
284 }
286 // suppress a few checks for accessors and trivial methods
287 if (callee_method->code_size() > MaxTrivialSize) {
289 // don't inline into giant methods
290 if (C->unique() > (uint)NodeCountInliningCutoff) {
291 return "NodeCountInliningCutoff";
292 }
294 if ((!UseInterpreter || CompileTheWorld) &&
295 is_init_with_ea(callee_method, caller_method, C)) {
297 // Escape Analysis stress testing when running Xcomp or CTW:
298 // inline constructors even if they are not reached.
300 } else if (profile.count() == 0) {
301 // don't inline unreached call sites
302 return "call site not reached";
303 }
304 }
306 if (!C->do_inlining() && InlineAccessors) {
307 return "not an accessor";
308 }
309 if( inline_depth() > MaxInlineLevel ) {
310 return "inlining too deep";
311 }
313 // We need to detect recursive inlining of method handle targets: if
314 // the current method is a method handle adapter and one of the
315 // callers is the same method as the callee, we bail out if
316 // MaxRecursiveInlineLevel is hit.
317 if (method()->is_method_handle_adapter()) {
318 JVMState* jvms = caller_jvms();
319 int inline_level = 0;
320 while (jvms != NULL && jvms->has_method()) {
321 if (jvms->method() == callee_method) {
322 inline_level++;
323 if (inline_level > MaxRecursiveInlineLevel)
324 return "recursively inlining too deep";
325 }
326 jvms = jvms->caller();
327 }
328 }
330 if (method() == callee_method && inline_depth() > MaxRecursiveInlineLevel) {
331 return "recursively inlining too deep";
332 }
334 int size = callee_method->code_size();
336 if (UseOldInlining && ClipInlining
337 && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
338 return "size > DesiredMethodLimit";
339 }
341 // ok, inline this method
342 return NULL;
343 }
345 //------------------------------pass_initial_checks----------------------------
346 bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
347 ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
348 // Check if a callee_method was suggested
349 if( callee_method == NULL ) return false;
350 // Check if klass of callee_method is loaded
351 if( !callee_holder->is_loaded() ) return false;
352 if( !callee_holder->is_initialized() ) return false;
353 if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
354 // Checks that constant pool's call site has been visited
355 // stricter than callee_holder->is_initialized()
356 ciBytecodeStream iter(caller_method);
357 iter.force_bci(caller_bci);
358 Bytecodes::Code call_bc = iter.cur_bc();
359 // An invokedynamic instruction does not have a klass.
360 if (call_bc != Bytecodes::_invokedynamic) {
361 int index = iter.get_index_u2_cpcache();
362 if (!caller_method->is_klass_loaded(index, true)) {
363 return false;
364 }
365 // Try to do constant pool resolution if running Xcomp
366 if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
367 return false;
368 }
369 }
370 }
371 // We will attempt to see if a class/field/etc got properly loaded. If it
372 // did not, it may attempt to throw an exception during our probing. Catch
373 // and ignore such exceptions and do not attempt to compile the method.
374 if( callee_method->should_exclude() ) return false;
376 return true;
377 }
379 #ifndef PRODUCT
380 //------------------------------print_inlining---------------------------------
381 // Really, the failure_msg can be a success message also.
382 void InlineTree::print_inlining(ciMethod* callee_method, int caller_bci, const char* failure_msg) const {
383 CompileTask::print_inlining(callee_method, inline_depth(), caller_bci, failure_msg ? failure_msg : "inline");
384 if (callee_method == NULL) tty->print(" callee not monotonic or profiled");
385 if (Verbose && callee_method) {
386 const InlineTree *top = this;
387 while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
388 tty->print(" bcs: %d+%d invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
389 }
390 }
391 #endif
393 //------------------------------ok_to_inline-----------------------------------
394 WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci) {
395 assert(callee_method != NULL, "caller checks for optimized virtual!");
396 #ifdef ASSERT
397 // Make sure the incoming jvms has the same information content as me.
398 // This means that we can eventually make this whole class AllStatic.
399 if (jvms->caller() == NULL) {
400 assert(_caller_jvms == NULL, "redundant instance state");
401 } else {
402 assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
403 }
404 assert(_method == jvms->method(), "redundant instance state");
405 #endif
406 const char *failure_msg = NULL;
407 int caller_bci = jvms->bci();
408 ciMethod *caller_method = jvms->method();
410 if( !pass_initial_checks(caller_method, caller_bci, callee_method)) {
411 if( PrintInlining ) {
412 failure_msg = "failed_initial_checks";
413 print_inlining( callee_method, caller_bci, failure_msg);
414 }
415 return NULL;
416 }
418 // Check if inlining policy says no.
419 WarmCallInfo wci = *(initial_wci);
420 failure_msg = try_to_inline(callee_method, caller_method, caller_bci, profile, &wci);
421 if (failure_msg != NULL && C->log() != NULL) {
422 C->log()->begin_elem("inline_fail reason='");
423 C->log()->text("%s", failure_msg);
424 C->log()->end_elem("'");
425 }
427 #ifndef PRODUCT
428 if (UseOldInlining && InlineWarmCalls
429 && (PrintOpto || PrintOptoInlining || PrintInlining)) {
430 bool cold = wci.is_cold();
431 bool hot = !cold && wci.is_hot();
432 bool old_cold = (failure_msg != NULL);
433 if (old_cold != cold || (Verbose || WizardMode)) {
434 tty->print(" OldInlining= %4s : %s\n WCI=",
435 old_cold ? "cold" : "hot", failure_msg ? failure_msg : "OK");
436 wci.print();
437 }
438 }
439 #endif
440 if (UseOldInlining) {
441 if (failure_msg == NULL)
442 wci = *(WarmCallInfo::always_hot());
443 else
444 wci = *(WarmCallInfo::always_cold());
445 }
446 if (!InlineWarmCalls) {
447 if (!wci.is_cold() && !wci.is_hot()) {
448 // Do not inline the warm calls.
449 wci = *(WarmCallInfo::always_cold());
450 }
451 }
453 if (!wci.is_cold()) {
454 // In -UseOldInlining, the failure_msg may also be a success message.
455 if (failure_msg == NULL) failure_msg = "inline (hot)";
457 // Inline!
458 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
459 if (UseOldInlining)
460 build_inline_tree_for_callee(callee_method, jvms, caller_bci);
461 if (InlineWarmCalls && !wci.is_hot())
462 return new (C) WarmCallInfo(wci); // copy to heap
463 return WarmCallInfo::always_hot();
464 }
466 // Do not inline
467 if (failure_msg == NULL) failure_msg = "too cold to inline";
468 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
469 return NULL;
470 }
472 //------------------------------compute_callee_frequency-----------------------
473 float InlineTree::compute_callee_frequency( int caller_bci ) const {
474 int count = method()->interpreter_call_site_count(caller_bci);
475 int invcnt = method()->interpreter_invocation_count();
476 float freq = (float)count/(float)invcnt;
477 // Call-site count / interpreter invocation count, scaled recursively.
478 // Always between 0.0 and 1.0. Represents the percentage of the method's
479 // total execution time used at this call site.
481 return freq;
482 }
484 //------------------------------build_inline_tree_for_callee-------------------
485 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
486 float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
487 // Attempt inlining.
488 InlineTree* old_ilt = callee_at(caller_bci, callee_method);
489 if (old_ilt != NULL) {
490 return old_ilt;
491 }
492 int new_depth_adjust = 0;
493 if (caller_jvms->method() != NULL) {
494 if (caller_jvms->method()->is_method_handle_adapter())
495 new_depth_adjust -= 1; // don't count actions in MH or indy adapter frames
496 else if (callee_method->is_method_handle_invoke()) {
497 new_depth_adjust -= 1; // don't count method handle calls from java.lang.invoke implem
498 }
499 if (new_depth_adjust != 0 && PrintInlining) {
500 stringStream nm1; caller_jvms->method()->print_name(&nm1);
501 stringStream nm2; callee_method->print_name(&nm2);
502 tty->print_cr("discounting inlining depth from %s to %s", nm1.base(), nm2.base());
503 }
504 if (new_depth_adjust != 0 && C->log()) {
505 int id1 = C->log()->identify(caller_jvms->method());
506 int id2 = C->log()->identify(callee_method);
507 C->log()->elem("inline_depth_discount caller='%d' callee='%d'", id1, id2);
508 }
509 }
510 InlineTree *ilt = new InlineTree(C, this, callee_method, caller_jvms, caller_bci, recur_frequency, _site_depth_adjust + new_depth_adjust);
511 _subtrees.append( ilt );
513 NOT_PRODUCT( _count_inlines += 1; )
515 return ilt;
516 }
519 //---------------------------------------callee_at-----------------------------
520 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
521 for (int i = 0; i < _subtrees.length(); i++) {
522 InlineTree* sub = _subtrees.at(i);
523 if (sub->caller_bci() == bci && callee == sub->method()) {
524 return sub;
525 }
526 }
527 return NULL;
528 }
531 //------------------------------build_inline_tree_root-------------------------
532 InlineTree *InlineTree::build_inline_tree_root() {
533 Compile* C = Compile::current();
535 // Root of inline tree
536 InlineTree *ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F, 0);
538 return ilt;
539 }
542 //-------------------------find_subtree_from_root-----------------------------
543 // Given a jvms, which determines a call chain from the root method,
544 // find the corresponding inline tree.
545 // Note: This method will be removed or replaced as InlineTree goes away.
546 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found) {
547 InlineTree* iltp = root;
548 uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
549 for (uint d = 1; d <= depth; d++) {
550 JVMState* jvmsp = jvms->of_depth(d);
551 // Select the corresponding subtree for this bci.
552 assert(jvmsp->method() == iltp->method(), "tree still in sync");
553 ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
554 InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
555 if (!sub) {
556 if (create_if_not_found && d == depth) {
557 return iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
558 }
559 assert(sub != NULL, "should be a sub-ilt here");
560 return NULL;
561 }
562 iltp = sub;
563 }
564 return iltp;
565 }