1 /*
  2  * Copyright (c) 1998, 2021, 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  */
 24 
 25 #include "precompiled.hpp"
 26 #include "ci/ciReplay.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "compiler/compileBroker.hpp"
 29 #include "compiler/compilerEvent.hpp"
 30 #include "compiler/compileLog.hpp"
 31 #include "interpreter/linkResolver.hpp"
 32 #include "jfr/jfrEvents.hpp"
 33 #include "oops/objArrayKlass.hpp"
 34 #include "opto/callGenerator.hpp"
 35 #include "opto/parse.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 #include "utilities/events.hpp"
 38 
 39 //=============================================================================
 40 //------------------------------InlineTree-------------------------------------
 41 InlineTree::InlineTree(Compile* c,
 42                        const InlineTree *caller_tree, ciMethod* callee,
 43                        JVMState* caller_jvms, int caller_bci,
 44                        int max_inline_level) :
 45   C(c),
 46   _caller_jvms(caller_jvms),
 47   _method(callee),
 48   _caller_tree((InlineTree*) caller_tree),
 49   _count_inline_bcs(method()->code_size_for_inlining()),
 50   _max_inline_level(max_inline_level),
 51   _subtrees(c->comp_arena(), 2, 0, NULL),
 52   _msg(NULL)
 53 {
 54 #ifndef PRODUCT
 55   _count_inlines = 0;
 56   _forced_inline = false;
 57 #endif
 58   if (_caller_jvms != NULL) {
 59     // Keep a private copy of the caller_jvms:
 60     _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
 61     _caller_jvms->set_bci(caller_jvms->bci());
 62     assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");
 63   }
 64   assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
 65   assert((caller_tree == NULL ? 0 : caller_tree->stack_depth() + 1) == stack_depth(), "correct (redundant) depth parameter");
 66   assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
 67   // Update hierarchical counts, count_inline_bcs() and count_inlines()
 68   InlineTree *caller = (InlineTree *)caller_tree;
 69   for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
 70     caller->_count_inline_bcs += count_inline_bcs();
 71     NOT_PRODUCT(caller->_count_inlines++;)
 72   }
 73 }
 74 
 75 /**
 76  *  Return true when EA is ON and a java constructor is called or
 77  *  a super constructor is called from an inlined java constructor.
 78  *  Also return true for boxing methods.
 79  *  Also return true for methods returning Iterator (including Iterable::iterator())
 80  *  that is essential for forall-loops performance.
 81  */
 82 static bool is_init_with_ea(ciMethod* callee_method,
 83                             ciMethod* caller_method, Compile* C) {
 84   if (!C->do_escape_analysis() || !EliminateAllocations) {
 85     return false; // EA is off
 86   }
 87   if (callee_method->is_initializer()) {
 88     return true; // constuctor
 89   }
 90   if (caller_method->is_initializer() &&
 91       caller_method != C->method() &&
 92       caller_method->holder()->is_subclass_of(callee_method->holder())) {
 93     return true; // super constructor is called from inlined constructor
 94   }
 95   if (C->eliminate_boxing() && callee_method->is_boxing_method()) {
 96     return true;
 97   }
 98   ciType *retType = callee_method->signature()->return_type();
 99   ciKlass *iter = C->env()->Iterator_klass();
100   if(retType->is_loaded() && iter->is_loaded() && retType->is_subtype_of(iter)) {
101     return true;
102   }
103   return false;
104 }
105 
106 /**
107  *  Force inlining unboxing accessor.
108  */
109 static bool is_unboxing_method(ciMethod* callee_method, Compile* C) {
110   return C->eliminate_boxing() && callee_method->is_unboxing_method();
111 }
112 
113 // positive filter: should callee be inlined?
114 bool InlineTree::should_inline(ciMethod* callee_method, ciMethod* caller_method,
115                                int caller_bci, ciCallProfile& profile) {
116   // Allows targeted inlining
117   if (C->directive()->should_inline(callee_method)) {
118     set_msg("force inline by CompileCommand");
119     _forced_inline = true;
120     return true;
121   }
122 
123   if (callee_method->force_inline()) {
124     set_msg("force inline by annotation");
125     _forced_inline = true;
126     return true;
127   }
128 
129 #ifndef PRODUCT
130   int inline_depth = inline_level()+1;
131   if (ciReplay::should_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {
132     set_msg("force inline by ciReplay");
133     _forced_inline = true;
134     return true;
135   }
136 #endif
137 
138   int size = callee_method->code_size_for_inlining();
139 
140   // Check for too many throws (and not too huge)
141   if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
142      size < InlineThrowMaxSize ) {
143     if (C->print_inlining() && Verbose) {
144       CompileTask::print_inline_indent(inline_level());
145       tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
146     }
147     set_msg("many throws");
148     return true;
149   }
150 
151   int default_max_inline_size = C->max_inline_size();
152   int inline_small_code_size  = InlineSmallCode / 4;
153   int max_inline_size         = default_max_inline_size;
154 
155   int call_site_count  = method()->scale_count(profile.count());
156   int invoke_count     = method()->interpreter_invocation_count();
157 
158   assert(invoke_count != 0, "require invocation count greater than zero");
159   double freq = (double)call_site_count / (double)invoke_count;
160 
161   // bump the max size if the call is frequent
162   if ((freq >= InlineFrequencyRatio) ||
163       is_unboxing_method(callee_method, C) ||
164       is_init_with_ea(callee_method, caller_method, C)) {
165 
166     max_inline_size = C->freq_inline_size();
167     if (size <= max_inline_size && TraceFrequencyInlining) {
168       CompileTask::print_inline_indent(inline_level());
169       tty->print_cr("Inlined frequent method (freq=%lf):", freq);
170       CompileTask::print_inline_indent(inline_level());
171       callee_method->print();
172       tty->cr();
173     }
174   } else {
175     // Not hot.  Check for medium-sized pre-existing nmethod at cold sites.
176     if (callee_method->has_compiled_code() &&
177         callee_method->instructions_size() > inline_small_code_size) {
178       set_msg("already compiled into a medium method");
179       return false;
180     }
181   }
182   if (size > max_inline_size) {
183     if (max_inline_size > default_max_inline_size) {
184       set_msg("hot method too big");
185     } else {
186       set_msg("too big");
187     }
188     return false;
189   }
190   return true;
191 }
192 
193 
194 // negative filter: should callee NOT be inlined?
195 bool InlineTree::should_not_inline(ciMethod *callee_method,
196                                    ciMethod* caller_method,
197                                    JVMState* jvms) {
198 
199   const char* fail_msg = NULL;
200 
201   // First check all inlining restrictions which are required for correctness
202   if (callee_method->is_abstract()) {
203     fail_msg = "abstract method"; // // note: we allow ik->is_abstract()
204   } else if (!callee_method->holder()->is_initialized() &&
205              // access allowed in the context of static initializer
206              C->needs_clinit_barrier(callee_method->holder(), caller_method)) {
207     fail_msg = "method holder not initialized";
208   } else if (callee_method->is_native()) {
209     fail_msg = "native method";
210   } else if (callee_method->dont_inline()) {
211     fail_msg = "don't inline by annotation";
212   }
213 
214   // Don't inline a method that changes Thread.currentThread() except
215   // into another method that is annotated @ChangesCurrentThread.
216   if (callee_method->changes_current_thread()
217       && ! C->method()->changes_current_thread()) {
218     fail_msg = "method changes current thread";
219   }
220 
221   // one more inlining restriction
222   if (fail_msg == NULL && callee_method->has_unloaded_classes_in_signature()) {
223     fail_msg = "unloaded signature classes";
224   }
225 
226   if (fail_msg != NULL) {
227     set_msg(fail_msg);
228     return true;
229   }
230 
231   // ignore heuristic controls on inlining
232   if (C->directive()->should_inline(callee_method)) {
233     set_msg("force inline by CompileCommand");
234     return false;
235   }
236 
237   if (C->directive()->should_not_inline(callee_method)) {
238     set_msg("disallowed by CompileCommand");
239     return true;
240   }
241 
242 #ifndef PRODUCT
243   int caller_bci = jvms->bci();
244   int inline_depth = inline_level()+1;
245   if (ciReplay::should_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {
246     set_msg("force inline by ciReplay");
247     return false;
248   }
249 
250   if (ciReplay::should_not_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {
251     set_msg("disallowed by ciReplay");
252     return true;
253   }
254 
255   if (ciReplay::should_not_inline(callee_method)) {
256     set_msg("disallowed by ciReplay");
257     return true;
258   }
259 #endif
260 
261   if (callee_method->force_inline()) {
262     set_msg("force inline by annotation");
263     return false;
264   }
265 
266   // Now perform checks which are heuristic
267 
268   if (is_unboxing_method(callee_method, C)) {
269     // Inline unboxing methods.
270     return false;
271   }
272 
273   if (callee_method->has_compiled_code() &&
274       callee_method->instructions_size() > InlineSmallCode) {
275     set_msg("already compiled into a big method");
276     return true;
277   }
278 
279   // don't inline exception code unless the top method belongs to an
280   // exception class
281   if (caller_tree() != NULL &&
282       callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
283     const InlineTree *top = this;
284     while (top->caller_tree() != NULL) top = top->caller_tree();
285     ciInstanceKlass* k = top->method()->holder();
286     if (!k->is_subclass_of(C->env()->Throwable_klass())) {
287       set_msg("exception method");
288       return true;
289     }
290   }
291 
292   // use frequency-based objections only for non-trivial methods
293   if (callee_method->code_size() <= MaxTrivialSize) {
294     return false;
295   }
296 
297   // don't use counts with -Xcomp
298   if (UseInterpreter) {
299 
300     if (!callee_method->has_compiled_code() &&
301         !callee_method->was_executed_more_than(0)) {
302       set_msg("never executed");
303       return true;
304     }
305 
306     if (is_init_with_ea(callee_method, caller_method, C)) {
307       // Escape Analysis: inline all executed constructors
308       return false;
309     } else {
310       intx counter_high_value;
311       // Tiered compilation uses a different "high value" than non-tiered compilation.
312       // Determine the right value to use.
313       if (TieredCompilation) {
314         counter_high_value = InvocationCounter::count_limit / 2;
315       } else {
316         counter_high_value = CompileThreshold / 2;
317       }
318       if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold, counter_high_value))) {
319         set_msg("executed < MinInliningThreshold times");
320         return true;
321       }
322     }
323   }
324 
325   return false;
326 }
327 
328 bool InlineTree::is_not_reached(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile) {
329   if (!UseInterpreter) {
330     return false; // -Xcomp
331   }
332   if (profile.count() > 0) {
333     return false; // reachable according to profile
334   }
335   if (!callee_method->was_executed_more_than(0)) {
336     return true; // callee was never executed
337   }
338   if (caller_method->is_not_reached(caller_bci)) {
339     return true; // call site not resolved
340   }
341   if (profile.count() == -1) {
342     return false; // immature profile; optimistically treat as reached
343   }
344   assert(profile.count() == 0, "sanity");
345 
346   // Profile info is scarce.
347   // Try to guess: check if the call site belongs to a start block.
348   // Call sites in a start block should be reachable if no exception is thrown earlier.
349   ciMethodBlocks* caller_blocks = caller_method->get_method_blocks();
350   bool is_start_block = caller_blocks->block_containing(caller_bci)->start_bci() == 0;
351   if (is_start_block) {
352     return false; // treat the call reached as part of start block
353   }
354   return true; // give up and treat the call site as not reached
355 }
356 
357 //-----------------------------try_to_inline-----------------------------------
358 // return true if ok
359 // Relocated from "InliningClosure::try_to_inline"
360 bool InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method,
361                                int caller_bci, JVMState* jvms, ciCallProfile& profile,
362                                bool& should_delay) {
363 
364   if (ClipInlining && (int)count_inline_bcs() >= DesiredMethodLimit) {
365     if (!callee_method->force_inline() || !IncrementalInline) {
366       set_msg("size > DesiredMethodLimit");
367       return false;
368     } else if (!C->inlining_incrementally()) {
369       should_delay = true;
370     }
371   }
372 
373   _forced_inline = false; // Reset
374   if (!should_inline(callee_method, caller_method, caller_bci, profile)) {
375     return false;
376   }
377   if (should_not_inline(callee_method, caller_method, jvms)) {
378     return false;
379   }
380 
381   if (InlineAccessors && callee_method->is_accessor()) {
382     // accessor methods are not subject to any of the following limits.
383     set_msg("accessor");
384     return true;
385   }
386 
387   // suppress a few checks for accessors and trivial methods
388   if (callee_method->code_size() > MaxTrivialSize) {
389 
390     // don't inline into giant methods
391     if (C->over_inlining_cutoff()) {
392       if ((!callee_method->force_inline() && !caller_method->is_compiled_lambda_form())
393           || !IncrementalInline) {
394         set_msg("NodeCountInliningCutoff");
395         return false;
396       } else {
397         should_delay = true;
398       }
399     }
400 
401     if (!UseInterpreter &&
402         is_init_with_ea(callee_method, caller_method, C)) {
403       // Escape Analysis stress testing when running Xcomp:
404       // inline constructors even if they are not reached.
405     } else if (forced_inline()) {
406       // Inlining was forced by CompilerOracle, ciReplay or annotation
407     } else if (is_not_reached(callee_method, caller_method, caller_bci, profile)) {
408       // don't inline unreached call sites
409        set_msg("call site not reached");
410        return false;
411     }
412   }
413 
414   if (!C->do_inlining() && InlineAccessors) {
415     set_msg("not an accessor");
416     return false;
417   }
418 
419   // Limit inlining depth in case inlining is forced or
420   // _max_inline_level was increased to compensate for lambda forms.
421   if (inline_level() > MaxForceInlineLevel) {
422     set_msg("MaxForceInlineLevel");
423     return false;
424   }
425   if (inline_level() > _max_inline_level) {
426     if (!callee_method->force_inline() || !IncrementalInline) {
427       set_msg("inlining too deep");
428       return false;
429     } else if (!C->inlining_incrementally()) {
430       should_delay = true;
431     }
432   }
433 
434   // detect direct and indirect recursive inlining
435   {
436     // count the current method and the callee
437     const bool is_compiled_lambda_form = callee_method->is_compiled_lambda_form();
438     int inline_level = 0;
439     if (!is_compiled_lambda_form) {
440       if (method() == callee_method) {
441         inline_level++;
442       }
443     }
444     // count callers of current method and callee
445     Node* callee_argument0 = is_compiled_lambda_form ? jvms->map()->argument(jvms, 0)->uncast() : NULL;
446     for (JVMState* j = jvms->caller(); j != NULL && j->has_method(); j = j->caller()) {
447       if (j->method() == callee_method) {
448         if (is_compiled_lambda_form) {
449           // Since compiled lambda forms are heavily reused we allow recursive inlining.  If it is truly
450           // a recursion (using the same "receiver") we limit inlining otherwise we can easily blow the
451           // compiler stack.
452           Node* caller_argument0 = j->map()->argument(j, 0)->uncast();
453           if (caller_argument0 == callee_argument0) {
454             inline_level++;
455           }
456         } else {
457           inline_level++;
458         }
459       }
460     }
461     if (inline_level > MaxRecursiveInlineLevel) {
462       set_msg("recursive inlining is too deep");
463       return false;
464     }
465   }
466 
467   int size = callee_method->code_size_for_inlining();
468 
469   if (ClipInlining && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
470     if (!callee_method->force_inline() || !IncrementalInline) {
471       set_msg("size > DesiredMethodLimit");
472       return false;
473     } else if (!C->inlining_incrementally()) {
474       should_delay = true;
475     }
476   }
477 
478   // ok, inline this method
479   return true;
480 }
481 
482 //------------------------------pass_initial_checks----------------------------
483 bool InlineTree::pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
484   // Check if a callee_method was suggested
485   if (callee_method == NULL) {
486     return false;
487   }
488   ciInstanceKlass *callee_holder = callee_method->holder();
489   // Check if klass of callee_method is loaded
490   if (!callee_holder->is_loaded()) {
491     return false;
492   }
493   if (!callee_holder->is_initialized() &&
494       // access allowed in the context of static initializer
495       C->needs_clinit_barrier(callee_holder, caller_method)) {
496     return false;
497   }
498   if( !UseInterpreter ) /* running Xcomp */ {
499     // Checks that constant pool's call site has been visited
500     // stricter than callee_holder->is_initialized()
501     ciBytecodeStream iter(caller_method);
502     iter.force_bci(caller_bci);
503     Bytecodes::Code call_bc = iter.cur_bc();
504     // An invokedynamic instruction does not have a klass.
505     if (call_bc != Bytecodes::_invokedynamic) {
506       int index = iter.get_index_u2_cpcache();
507       if (!caller_method->is_klass_loaded(index, true)) {
508         return false;
509       }
510       // Try to do constant pool resolution if running Xcomp
511       if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
512         return false;
513       }
514     }
515   }
516   return true;
517 }
518 
519 //------------------------------check_can_parse--------------------------------
520 const char* InlineTree::check_can_parse(ciMethod* callee) {
521   // Certain methods cannot be parsed at all:
522   if ( callee->is_native())                     return "native method";
523   if ( callee->is_abstract())                   return "abstract method";
524   if (!callee->has_balanced_monitors())         return "not compilable (unbalanced monitors)";
525   if ( callee->get_flow_analysis()->failing())  return "not compilable (flow analysis failed)";
526   if (!callee->can_be_parsed())                 return "cannot be parsed";
527   return NULL;
528 }
529 
530 //------------------------------print_inlining---------------------------------
531 void InlineTree::print_inlining(ciMethod* callee_method, int caller_bci,
532                                 ciMethod* caller_method, bool success) const {
533   const char* inline_msg = msg();
534   assert(inline_msg != NULL, "just checking");
535   if (C->log() != NULL) {
536     if (success) {
537       C->log()->inline_success(inline_msg);
538     } else {
539       C->log()->inline_fail(inline_msg);
540     }
541   }
542   CompileTask::print_inlining_ul(callee_method, inline_level(),
543                                                caller_bci, inline_msg);
544   if (C->print_inlining()) {
545     C->print_inlining(callee_method, inline_level(), caller_bci, inline_msg);
546     guarantee(callee_method != NULL, "would crash in CompilerEvent::InlineEvent::post");
547     if (Verbose) {
548       const InlineTree *top = this;
549       while (top->caller_tree() != NULL) { top = top->caller_tree(); }
550       //tty->print("  bcs: %d+%d  invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
551     }
552   }
553   EventCompilerInlining event;
554   if (event.should_commit()) {
555     CompilerEvent::InlineEvent::post(event, C->compile_id(), caller_method->get_Method(), callee_method, success, inline_msg, caller_bci);
556   }
557 }
558 
559 //------------------------------ok_to_inline-----------------------------------
560 bool InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile,
561                               bool& should_delay) {
562   assert(callee_method != NULL, "caller checks for optimized virtual!");
563   assert(!should_delay, "should be initialized to false");
564 #ifdef ASSERT
565   // Make sure the incoming jvms has the same information content as me.
566   // This means that we can eventually make this whole class AllStatic.
567   if (jvms->caller() == NULL) {
568     assert(_caller_jvms == NULL, "redundant instance state");
569   } else {
570     assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
571   }
572   assert(_method == jvms->method(), "redundant instance state");
573 #endif
574   int         caller_bci    = jvms->bci();
575   ciMethod*   caller_method = jvms->method();
576 
577   // Do some initial checks.
578   if (!pass_initial_checks(caller_method, caller_bci, callee_method)) {
579     set_msg("failed initial checks");
580     print_inlining(callee_method, caller_bci, caller_method, false /* !success */);
581     return false;
582   }
583 
584   // Do some parse checks.
585   set_msg(check_can_parse(callee_method));
586   if (msg() != NULL) {
587     print_inlining(callee_method, caller_bci, caller_method, false /* !success */);
588     return false;
589   }
590 
591   // Check if inlining policy says no.
592   bool success = try_to_inline(callee_method, caller_method, caller_bci, jvms, profile,
593                                should_delay); // out
594   if (success) {
595     // Inline!
596     if (msg() == NULL) {
597       set_msg("inline (hot)");
598     }
599     print_inlining(callee_method, caller_bci, caller_method, true /* success */);
600     build_inline_tree_for_callee(callee_method, jvms, caller_bci);
601     return true;
602   } else {
603     // Do not inline
604     if (msg() == NULL) {
605       set_msg("too cold to inline");
606     }
607     print_inlining(callee_method, caller_bci, caller_method, false /* !success */ );
608     return false;
609   }
610 }
611 
612 //------------------------------build_inline_tree_for_callee-------------------
613 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
614   // Attempt inlining.
615   InlineTree* old_ilt = callee_at(caller_bci, callee_method);
616   if (old_ilt != NULL) {
617     return old_ilt;
618   }
619   int max_inline_level_adjust = 0;
620   if (caller_jvms->method() != NULL) {
621     if (caller_jvms->method()->is_compiled_lambda_form()) {
622       max_inline_level_adjust += 1;  // don't count actions in MH or indy adapter frames
623     } else if (callee_method->is_method_handle_intrinsic() ||
624                callee_method->is_compiled_lambda_form()) {
625       max_inline_level_adjust += 1;  // don't count method handle calls from java.lang.invoke implementation
626     }
627     if (max_inline_level_adjust != 0 && C->print_inlining() && (Verbose || WizardMode)) {
628       CompileTask::print_inline_indent(inline_level());
629       tty->print_cr(" \\-> discounting inline depth");
630     }
631     if (max_inline_level_adjust != 0 && C->log()) {
632       int id1 = C->log()->identify(caller_jvms->method());
633       int id2 = C->log()->identify(callee_method);
634       C->log()->elem("inline_level_discount caller='%d' callee='%d'", id1, id2);
635     }
636   }
637   // Allocate in the comp_arena to make sure the InlineTree is live when dumping a replay compilation file
638   InlineTree* ilt = new (C->comp_arena()) InlineTree(C, this, callee_method, caller_jvms, caller_bci, _max_inline_level + max_inline_level_adjust);
639   _subtrees.append(ilt);
640 
641   NOT_PRODUCT( _count_inlines += 1; )
642 
643   return ilt;
644 }
645 
646 
647 //---------------------------------------callee_at-----------------------------
648 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
649   for (int i = 0; i < _subtrees.length(); i++) {
650     InlineTree* sub = _subtrees.at(i);
651     if (sub->caller_bci() == bci && callee == sub->method()) {
652       return sub;
653     }
654   }
655   return NULL;
656 }
657 
658 
659 //------------------------------build_inline_tree_root-------------------------
660 InlineTree *InlineTree::build_inline_tree_root() {
661   Compile* C = Compile::current();
662 
663   // Root of inline tree
664   InlineTree* ilt = new InlineTree(C, NULL, C->method(), NULL, -1, MaxInlineLevel);
665 
666   return ilt;
667 }
668 
669 
670 //-------------------------find_subtree_from_root-----------------------------
671 // Given a jvms, which determines a call chain from the root method,
672 // find the corresponding inline tree.
673 // Note: This method will be removed or replaced as InlineTree goes away.
674 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee) {
675   InlineTree* iltp = root;
676   uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
677   for (uint d = 1; d <= depth; d++) {
678     JVMState* jvmsp  = jvms->of_depth(d);
679     // Select the corresponding subtree for this bci.
680     assert(jvmsp->method() == iltp->method(), "tree still in sync");
681     ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
682     InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
683     if (sub == NULL) {
684       if (d == depth) {
685         sub = iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
686       }
687       guarantee(sub != NULL, "should be a sub-ilt here");
688       return sub;
689     }
690     iltp = sub;
691   }
692   return iltp;
693 }
694 
695 // Count number of nodes in this subtree
696 int InlineTree::count() const {
697   int result = 1;
698   for (int i = 0 ; i < _subtrees.length(); i++) {
699     result += _subtrees.at(i)->count();
700   }
701   return result;
702 }
703 
704 void InlineTree::dump_replay_data(outputStream* out) {
705   out->print(" %d %d ", inline_level(), caller_bci());
706   method()->dump_name_as_ascii(out);
707   for (int i = 0 ; i < _subtrees.length(); i++) {
708     _subtrees.at(i)->dump_replay_data(out);
709   }
710 }
711 
712 
713 #ifndef PRODUCT
714 void InlineTree::print_impl(outputStream* st, int indent) const {
715   for (int i = 0; i < indent; i++) st->print(" ");
716   st->print(" @ %d", caller_bci());
717   method()->print_short_name(st);
718   st->cr();
719 
720   for (int i = 0 ; i < _subtrees.length(); i++) {
721     _subtrees.at(i)->print_impl(st, indent + 2);
722   }
723 }
724 
725 void InlineTree::print_value_on(outputStream* st) const {
726   print_impl(st, 2);
727 }
728 #endif