Tue, 05 Jan 2010 15:21:25 +0100
6893268: additional dynamic language related optimizations in C2
Summary: C2 needs some additional optimizations to be able to handle MethodHandle invokes and invokedynamic instructions at the best performance.
Reviewed-by: kvn, never
1 /*
2 * Copyright 1998-2009 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 assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");
41 }
42 assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
43 assert((caller_tree == NULL ? 0 : caller_tree->inline_depth() + 1) == inline_depth(), "correct (redundant) depth parameter");
44 assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
45 if (UseOldInlining) {
46 // Update hierarchical counts, count_inline_bcs() and count_inlines()
47 InlineTree *caller = (InlineTree *)caller_tree;
48 for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
49 caller->_count_inline_bcs += count_inline_bcs();
50 NOT_PRODUCT(caller->_count_inlines++;)
51 }
52 }
53 }
55 InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms, float site_invoke_ratio)
56 : C(c), _caller_jvms(caller_jvms), _caller_tree(NULL),
57 _method(callee_method), _site_invoke_ratio(site_invoke_ratio),
58 _count_inline_bcs(method()->code_size()) {
59 NOT_PRODUCT(_count_inlines = 0;)
60 assert(!UseOldInlining, "do not use for old stuff");
61 }
65 static void print_indent(int depth) {
66 tty->print(" ");
67 for (int i = depth; i != 0; --i) tty->print(" ");
68 }
70 static bool is_init_with_ea(ciMethod* callee_method,
71 ciMethod* caller_method, Compile* C) {
72 // True when EA is ON and a java constructor is called or
73 // a super constructor is called from an inlined java constructor.
74 return C->do_escape_analysis() && EliminateAllocations &&
75 ( callee_method->is_initializer() ||
76 (caller_method->is_initializer() &&
77 caller_method != C->method() &&
78 caller_method->holder()->is_subclass_of(callee_method->holder()))
79 );
80 }
82 // positive filter: should send be inlined? returns NULL, if yes, or rejection msg
83 const char* InlineTree::shouldInline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const {
84 // Allows targeted inlining
85 if(callee_method->should_inline()) {
86 *wci_result = *(WarmCallInfo::always_hot());
87 if (PrintInlining && Verbose) {
88 print_indent(inline_depth());
89 tty->print_cr("Inlined method is hot: ");
90 }
91 return NULL;
92 }
94 // positive filter: should send be inlined? returns NULL (--> yes)
95 // or rejection msg
96 int max_size = C->max_inline_size();
97 int size = callee_method->code_size();
99 // Check for too many throws (and not too huge)
100 if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
101 size < InlineThrowMaxSize ) {
102 wci_result->set_profit(wci_result->profit() * 100);
103 if (PrintInlining && Verbose) {
104 print_indent(inline_depth());
105 tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
106 }
107 return NULL;
108 }
110 if (!UseOldInlining) {
111 return NULL; // size and frequency are represented in a new way
112 }
114 int call_site_count = method()->scale_count(profile.count());
115 int invoke_count = method()->interpreter_invocation_count();
116 assert( invoke_count != 0, "Require invokation count greater than zero");
117 int freq = call_site_count/invoke_count;
119 // bump the max size if the call is frequent
120 if ((freq >= InlineFrequencyRatio) ||
121 (call_site_count >= InlineFrequencyCount) ||
122 is_init_with_ea(callee_method, caller_method, C)) {
124 max_size = C->freq_inline_size();
125 if (size <= max_size && TraceFrequencyInlining) {
126 print_indent(inline_depth());
127 tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
128 print_indent(inline_depth());
129 callee_method->print();
130 tty->cr();
131 }
132 } else {
133 // Not hot. Check for medium-sized pre-existing nmethod at cold sites.
134 if (callee_method->has_compiled_code() &&
135 callee_method->instructions_size() > InlineSmallCode/4)
136 return "already compiled into a medium method";
137 }
138 if (size > max_size) {
139 if (max_size > C->max_inline_size())
140 return "hot method too big";
141 return "too big";
142 }
143 return NULL;
144 }
147 // negative filter: should send NOT be inlined? returns NULL, ok to inline, or rejection msg
148 const char* InlineTree::shouldNotInline(ciMethod *callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const {
149 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
150 if (!UseOldInlining) {
151 const char* fail = NULL;
152 if (callee_method->is_abstract()) fail = "abstract method";
153 // note: we allow ik->is_abstract()
154 if (!callee_method->holder()->is_initialized()) fail = "method holder not initialized";
155 if (callee_method->is_native()) fail = "native method";
157 if (fail) {
158 *wci_result = *(WarmCallInfo::always_cold());
159 return fail;
160 }
162 if (callee_method->has_unloaded_classes_in_signature()) {
163 wci_result->set_profit(wci_result->profit() * 0.1);
164 }
166 // don't inline exception code unless the top method belongs to an
167 // exception class
168 if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
169 ciMethod* top_method = caller_jvms() ? caller_jvms()->of_depth(1)->method() : method();
170 if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
171 wci_result->set_profit(wci_result->profit() * 0.1);
172 }
173 }
175 if (callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode) {
176 wci_result->set_profit(wci_result->profit() * 0.1);
177 // %%% adjust wci_result->size()?
178 }
180 return NULL;
181 }
183 // Always inline MethodHandle methods.
184 if (callee_method->is_method_handle_invoke())
185 return NULL;
187 // First check all inlining restrictions which are required for correctness
188 if (callee_method->is_abstract()) return "abstract method";
189 // note: we allow ik->is_abstract()
190 if (!callee_method->holder()->is_initialized()) return "method holder not initialized";
191 if (callee_method->is_native()) return "native method";
192 if (callee_method->has_unloaded_classes_in_signature()) return "unloaded signature classes";
194 if (callee_method->should_inline()) {
195 // ignore heuristic controls on inlining
196 return NULL;
197 }
199 // Now perform checks which are heuristic
201 if( callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode )
202 return "already compiled into a big method";
204 // don't inline exception code unless the top method belongs to an
205 // exception class
206 if (caller_tree() != NULL &&
207 callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
208 const InlineTree *top = this;
209 while (top->caller_tree() != NULL) top = top->caller_tree();
210 ciInstanceKlass* k = top->method()->holder();
211 if (!k->is_subclass_of(C->env()->Throwable_klass()))
212 return "exception method";
213 }
215 // use frequency-based objections only for non-trivial methods
216 if (callee_method->code_size() <= MaxTrivialSize) return NULL;
218 // don't use counts with -Xcomp or CTW
219 if (UseInterpreter && !CompileTheWorld) {
221 if (!callee_method->has_compiled_code() &&
222 !callee_method->was_executed_more_than(0)) {
223 return "never executed";
224 }
226 if (is_init_with_ea(callee_method, caller_method, C)) {
228 // Escape Analysis: inline all executed constructors
230 } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
231 CompileThreshold >> 1))) {
232 return "executed < MinInliningThreshold times";
233 }
234 }
236 if (callee_method->should_not_inline()) {
237 return "disallowed by CompilerOracle";
238 }
240 if (UseStringCache) {
241 // Do not inline StringCache::profile() method used only at the beginning.
242 if (callee_method->name() == ciSymbol::profile_name() &&
243 callee_method->holder()->name() == ciSymbol::java_lang_StringCache()) {
244 return "profiling method";
245 }
246 }
248 return NULL;
249 }
251 //-----------------------------try_to_inline-----------------------------------
252 // return NULL if ok, reason for not inlining otherwise
253 // Relocated from "InliningClosure::try_to_inline"
254 const char* InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) {
256 // Old algorithm had funny accumulating BC-size counters
257 if (UseOldInlining && ClipInlining
258 && (int)count_inline_bcs() >= DesiredMethodLimit) {
259 return "size > DesiredMethodLimit";
260 }
262 const char *msg = NULL;
263 if ((msg = shouldInline(callee_method, caller_method, caller_bci,
264 profile, wci_result)) != NULL) {
265 return msg;
266 }
267 if ((msg = shouldNotInline(callee_method, caller_method,
268 wci_result)) != NULL) {
269 return msg;
270 }
272 bool is_accessor = InlineAccessors && callee_method->is_accessor();
274 // suppress a few checks for accessors and trivial methods
275 if (!is_accessor && callee_method->code_size() > MaxTrivialSize) {
277 // don't inline into giant methods
278 if (C->unique() > (uint)NodeCountInliningCutoff) {
279 return "NodeCountInliningCutoff";
280 }
282 if ((!UseInterpreter || CompileTheWorld) &&
283 is_init_with_ea(callee_method, caller_method, C)) {
285 // Escape Analysis stress testing when running Xcomp or CTW:
286 // inline constructors even if they are not reached.
288 } else if (profile.count() == 0) {
289 // don't inline unreached call sites
290 return "call site not reached";
291 }
292 }
294 if (!C->do_inlining() && InlineAccessors && !is_accessor) {
295 return "not an accessor";
296 }
297 if( inline_depth() > MaxInlineLevel ) {
298 return "inlining too deep";
299 }
300 if( method() == callee_method &&
301 inline_depth() > MaxRecursiveInlineLevel ) {
302 return "recursively inlining too deep";
303 }
305 int size = callee_method->code_size();
307 if (UseOldInlining && ClipInlining
308 && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
309 return "size > DesiredMethodLimit";
310 }
312 // ok, inline this method
313 return NULL;
314 }
316 //------------------------------pass_initial_checks----------------------------
317 bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
318 ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
319 // Check if a callee_method was suggested
320 if( callee_method == NULL ) return false;
321 // Check if klass of callee_method is loaded
322 if( !callee_holder->is_loaded() ) return false;
323 if( !callee_holder->is_initialized() ) return false;
324 if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
325 // Checks that constant pool's call site has been visited
326 // stricter than callee_holder->is_initialized()
327 ciBytecodeStream iter(caller_method);
328 iter.force_bci(caller_bci);
329 Bytecodes::Code call_bc = iter.cur_bc();
330 // An invokedynamic instruction does not have a klass.
331 if (call_bc != Bytecodes::_invokedynamic) {
332 int index = iter.get_index_int();
333 if (!caller_method->is_klass_loaded(index, true)) {
334 return false;
335 }
336 // Try to do constant pool resolution if running Xcomp
337 if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
338 return false;
339 }
340 }
341 }
342 // We will attempt to see if a class/field/etc got properly loaded. If it
343 // did not, it may attempt to throw an exception during our probing. Catch
344 // and ignore such exceptions and do not attempt to compile the method.
345 if( callee_method->should_exclude() ) return false;
347 return true;
348 }
350 #ifndef PRODUCT
351 //------------------------------print_inlining---------------------------------
352 // Really, the failure_msg can be a success message also.
353 void InlineTree::print_inlining(ciMethod *callee_method, int caller_bci, const char *failure_msg) const {
354 print_indent(inline_depth());
355 tty->print("@ %d ", caller_bci);
356 if( callee_method ) callee_method->print_short_name();
357 else tty->print(" callee not monotonic or profiled");
358 tty->print(" %s", (failure_msg ? failure_msg : "inline"));
359 if( Verbose && callee_method ) {
360 const InlineTree *top = this;
361 while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
362 tty->print(" bcs: %d+%d invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
363 }
364 tty->cr();
365 }
366 #endif
368 //------------------------------ok_to_inline-----------------------------------
369 WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci) {
370 assert(callee_method != NULL, "caller checks for optimized virtual!");
371 #ifdef ASSERT
372 // Make sure the incoming jvms has the same information content as me.
373 // This means that we can eventually make this whole class AllStatic.
374 if (jvms->caller() == NULL) {
375 assert(_caller_jvms == NULL, "redundant instance state");
376 } else {
377 assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
378 }
379 assert(_method == jvms->method(), "redundant instance state");
380 #endif
381 const char *failure_msg = NULL;
382 int caller_bci = jvms->bci();
383 ciMethod *caller_method = jvms->method();
385 if( !pass_initial_checks(caller_method, caller_bci, callee_method)) {
386 if( PrintInlining ) {
387 failure_msg = "failed_initial_checks";
388 print_inlining( callee_method, caller_bci, failure_msg);
389 }
390 return NULL;
391 }
393 // Check if inlining policy says no.
394 WarmCallInfo wci = *(initial_wci);
395 failure_msg = try_to_inline(callee_method, caller_method, caller_bci, profile, &wci);
396 if (failure_msg != NULL && C->log() != NULL) {
397 C->log()->begin_elem("inline_fail reason='");
398 C->log()->text("%s", failure_msg);
399 C->log()->end_elem("'");
400 }
402 #ifndef PRODUCT
403 if (UseOldInlining && InlineWarmCalls
404 && (PrintOpto || PrintOptoInlining || PrintInlining)) {
405 bool cold = wci.is_cold();
406 bool hot = !cold && wci.is_hot();
407 bool old_cold = (failure_msg != NULL);
408 if (old_cold != cold || (Verbose || WizardMode)) {
409 tty->print(" OldInlining= %4s : %s\n WCI=",
410 old_cold ? "cold" : "hot", failure_msg ? failure_msg : "OK");
411 wci.print();
412 }
413 }
414 #endif
415 if (UseOldInlining) {
416 if (failure_msg == NULL)
417 wci = *(WarmCallInfo::always_hot());
418 else
419 wci = *(WarmCallInfo::always_cold());
420 }
421 if (!InlineWarmCalls) {
422 if (!wci.is_cold() && !wci.is_hot()) {
423 // Do not inline the warm calls.
424 wci = *(WarmCallInfo::always_cold());
425 }
426 }
428 if (!wci.is_cold()) {
429 // In -UseOldInlining, the failure_msg may also be a success message.
430 if (failure_msg == NULL) failure_msg = "inline (hot)";
432 // Inline!
433 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
434 if (UseOldInlining)
435 build_inline_tree_for_callee(callee_method, jvms, caller_bci);
436 if (InlineWarmCalls && !wci.is_hot())
437 return new (C) WarmCallInfo(wci); // copy to heap
438 return WarmCallInfo::always_hot();
439 }
441 // Do not inline
442 if (failure_msg == NULL) failure_msg = "too cold to inline";
443 if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
444 return NULL;
445 }
447 //------------------------------compute_callee_frequency-----------------------
448 float InlineTree::compute_callee_frequency( int caller_bci ) const {
449 int count = method()->interpreter_call_site_count(caller_bci);
450 int invcnt = method()->interpreter_invocation_count();
451 float freq = (float)count/(float)invcnt;
452 // Call-site count / interpreter invocation count, scaled recursively.
453 // Always between 0.0 and 1.0. Represents the percentage of the method's
454 // total execution time used at this call site.
456 return freq;
457 }
459 //------------------------------build_inline_tree_for_callee-------------------
460 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
461 float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
462 // Attempt inlining.
463 InlineTree* old_ilt = callee_at(caller_bci, callee_method);
464 if (old_ilt != NULL) {
465 return old_ilt;
466 }
467 InlineTree *ilt = new InlineTree( C, this, callee_method, caller_jvms, caller_bci, recur_frequency );
468 _subtrees.append( ilt );
470 NOT_PRODUCT( _count_inlines += 1; )
472 return ilt;
473 }
476 //---------------------------------------callee_at-----------------------------
477 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
478 for (int i = 0; i < _subtrees.length(); i++) {
479 InlineTree* sub = _subtrees.at(i);
480 if (sub->caller_bci() == bci && callee == sub->method()) {
481 return sub;
482 }
483 }
484 return NULL;
485 }
488 //------------------------------build_inline_tree_root-------------------------
489 InlineTree *InlineTree::build_inline_tree_root() {
490 Compile* C = Compile::current();
492 // Root of inline tree
493 InlineTree *ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F);
495 return ilt;
496 }
499 //-------------------------find_subtree_from_root-----------------------------
500 // Given a jvms, which determines a call chain from the root method,
501 // find the corresponding inline tree.
502 // Note: This method will be removed or replaced as InlineTree goes away.
503 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found) {
504 InlineTree* iltp = root;
505 uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
506 for (uint d = 1; d <= depth; d++) {
507 JVMState* jvmsp = jvms->of_depth(d);
508 // Select the corresponding subtree for this bci.
509 assert(jvmsp->method() == iltp->method(), "tree still in sync");
510 ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
511 InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
512 if (!sub) {
513 if (create_if_not_found && d == depth) {
514 return iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
515 }
516 assert(sub != NULL, "should be a sub-ilt here");
517 return NULL;
518 }
519 iltp = sub;
520 }
521 return iltp;
522 }