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   // one more inlining restriction
215   if (fail_msg == NULL && callee_method->has_unloaded_classes_in_signature()) {
216     fail_msg = "unloaded signature classes";
217   }
218 
219   if (fail_msg != NULL) {
220     set_msg(fail_msg);
221     return true;
222   }
223 
224   // ignore heuristic controls on inlining
225   if (C->directive()->should_inline(callee_method)) {
226     set_msg("force inline by CompileCommand");
227     return false;
228   }
229 
230   if (C->directive()->should_not_inline(callee_method)) {
231     set_msg("disallowed by CompileCommand");
232     return true;
233   }
234 
235 #ifndef PRODUCT
236   int caller_bci = jvms->bci();
237   int inline_depth = inline_level()+1;
238   if (ciReplay::should_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {
239     set_msg("force inline by ciReplay");
240     return false;
241   }
242 
243   if (ciReplay::should_not_inline(C->replay_inline_data(), callee_method, caller_bci, inline_depth)) {
244     set_msg("disallowed by ciReplay");
245     return true;
246   }
247 
248   if (ciReplay::should_not_inline(callee_method)) {
249     set_msg("disallowed by ciReplay");
250     return true;
251   }
252 #endif
253 
254   if (callee_method->force_inline()) {
255     set_msg("force inline by annotation");
256     return false;
257   }
258 
259   // Now perform checks which are heuristic
260 
261   if (is_unboxing_method(callee_method, C)) {
262     // Inline unboxing methods.
263     return false;
264   }
265 
266   if (callee_method->has_compiled_code() &&
267       callee_method->instructions_size() > InlineSmallCode) {
268     set_msg("already compiled into a big method");
269     return true;
270   }
271 
272   // don't inline exception code unless the top method belongs to an
273   // exception class
274   if (caller_tree() != NULL &&
275       callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
276     const InlineTree *top = this;
277     while (top->caller_tree() != NULL) top = top->caller_tree();
278     ciInstanceKlass* k = top->method()->holder();
279     if (!k->is_subclass_of(C->env()->Throwable_klass())) {
280       set_msg("exception method");
281       return true;
282     }
283   }
284 
285   // use frequency-based objections only for non-trivial methods
286   if (callee_method->code_size() <= MaxTrivialSize) {
287     return false;
288   }
289 
290   // don't use counts with -Xcomp
291   if (UseInterpreter) {
292 
293     if (!callee_method->has_compiled_code() &&
294         !callee_method->was_executed_more_than(0)) {
295       set_msg("never executed");
296       return true;
297     }
298 
299     if (is_init_with_ea(callee_method, caller_method, C)) {
300       // Escape Analysis: inline all executed constructors
301       return false;
302     } else {
303       intx counter_high_value;
304       // Tiered compilation uses a different "high value" than non-tiered compilation.
305       // Determine the right value to use.
306       if (TieredCompilation) {
307         counter_high_value = InvocationCounter::count_limit / 2;
308       } else {
309         counter_high_value = CompileThreshold / 2;
310       }
311       if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold, counter_high_value))) {
312         set_msg("executed < MinInliningThreshold times");
313         return true;
314       }
315     }
316   }
317 
318   return false;
319 }
320 
321 bool InlineTree::is_not_reached(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile) {
322   if (!UseInterpreter) {
323     return false; // -Xcomp
324   }
325   if (profile.count() > 0) {
326     return false; // reachable according to profile
327   }
328   if (!callee_method->was_executed_more_than(0)) {
329     return true; // callee was never executed
330   }
331   if (caller_method->is_not_reached(caller_bci)) {
332     return true; // call site not resolved
333   }
334   if (profile.count() == -1) {
335     return false; // immature profile; optimistically treat as reached
336   }
337   assert(profile.count() == 0, "sanity");
338 
339   // Profile info is scarce.
340   // Try to guess: check if the call site belongs to a start block.
341   // Call sites in a start block should be reachable if no exception is thrown earlier.
342   ciMethodBlocks* caller_blocks = caller_method->get_method_blocks();
343   bool is_start_block = caller_blocks->block_containing(caller_bci)->start_bci() == 0;
344   if (is_start_block) {
345     return false; // treat the call reached as part of start block
346   }
347   return true; // give up and treat the call site as not reached
348 }
349 
350 //-----------------------------try_to_inline-----------------------------------
351 // return true if ok
352 // Relocated from "InliningClosure::try_to_inline"
353 bool InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method,
354                                int caller_bci, JVMState* jvms, ciCallProfile& profile,
355                                bool& should_delay) {
356 
357   if (ClipInlining && (int)count_inline_bcs() >= DesiredMethodLimit) {
358     if (!callee_method->force_inline() || !IncrementalInline) {
359       set_msg("size > DesiredMethodLimit");
360       return false;
361     } else if (!C->inlining_incrementally()) {
362       should_delay = true;
363     }
364   }
365 
366   _forced_inline = false; // Reset
367   if (!should_inline(callee_method, caller_method, caller_bci, profile)) {
368     return false;
369   }
370   if (should_not_inline(callee_method, caller_method, jvms)) {
371     return false;
372   }
373 
374   if (InlineAccessors && callee_method->is_accessor()) {
375     // accessor methods are not subject to any of the following limits.
376     set_msg("accessor");
377     return true;
378   }
379 
380   // suppress a few checks for accessors and trivial methods
381   if (callee_method->code_size() > MaxTrivialSize) {
382 
383     // don't inline into giant methods
384     if (C->over_inlining_cutoff()) {
385       if ((!callee_method->force_inline() && !caller_method->is_compiled_lambda_form())
386           || !IncrementalInline) {
387         set_msg("NodeCountInliningCutoff");
388         return false;
389       } else {
390         should_delay = true;
391       }
392     }
393 
394     if (!UseInterpreter &&
395         is_init_with_ea(callee_method, caller_method, C)) {
396       // Escape Analysis stress testing when running Xcomp:
397       // inline constructors even if they are not reached.
398     } else if (forced_inline()) {
399       // Inlining was forced by CompilerOracle, ciReplay or annotation
400     } else if (is_not_reached(callee_method, caller_method, caller_bci, profile)) {
401       // don't inline unreached call sites
402        set_msg("call site not reached");
403        return false;
404     }
405   }
406 
407   if (!C->do_inlining() && InlineAccessors) {
408     set_msg("not an accessor");
409     return false;
410   }
411 
412   // Limit inlining depth in case inlining is forced or
413   // _max_inline_level was increased to compensate for lambda forms.
414   if (inline_level() > MaxForceInlineLevel) {
415     set_msg("MaxForceInlineLevel");
416     return false;
417   }
418   if (inline_level() > _max_inline_level) {
419     if (!callee_method->force_inline() || !IncrementalInline) {
420       set_msg("inlining too deep");
421       return false;
422     } else if (!C->inlining_incrementally()) {
423       should_delay = true;
424     }
425   }
426 
427   // detect direct and indirect recursive inlining
428   {
429     // count the current method and the callee
430     const bool is_compiled_lambda_form = callee_method->is_compiled_lambda_form();
431     int inline_level = 0;
432     if (!is_compiled_lambda_form) {
433       if (method() == callee_method) {
434         inline_level++;
435       }
436     }
437     // count callers of current method and callee
438     Node* callee_argument0 = is_compiled_lambda_form ? jvms->map()->argument(jvms, 0)->uncast() : NULL;
439     for (JVMState* j = jvms->caller(); j != NULL && j->has_method(); j = j->caller()) {
440       if (j->method() == callee_method) {
441         if (is_compiled_lambda_form) {
442           // Since compiled lambda forms are heavily reused we allow recursive inlining.  If it is truly
443           // a recursion (using the same "receiver") we limit inlining otherwise we can easily blow the
444           // compiler stack.
445           Node* caller_argument0 = j->map()->argument(j, 0)->uncast();
446           if (caller_argument0 == callee_argument0) {
447             inline_level++;
448           }
449         } else {
450           inline_level++;
451         }
452       }
453     }
454     if (inline_level > MaxRecursiveInlineLevel) {
455       set_msg("recursive inlining is too deep");
456       return false;
457     }
458   }
459 
460   int size = callee_method->code_size_for_inlining();
461 
462   if (ClipInlining && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
463     if (!callee_method->force_inline() || !IncrementalInline) {
464       set_msg("size > DesiredMethodLimit");
465       return false;
466     } else if (!C->inlining_incrementally()) {
467       should_delay = true;
468     }
469   }
470 
471   // ok, inline this method
472   return true;
473 }
474 
475 //------------------------------pass_initial_checks----------------------------
476 bool InlineTree::pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
477   // Check if a callee_method was suggested
478   if (callee_method == NULL) {
479     return false;
480   }
481   ciInstanceKlass *callee_holder = callee_method->holder();
482   // Check if klass of callee_method is loaded
483   if (!callee_holder->is_loaded()) {
484     return false;
485   }
486   if (!callee_holder->is_initialized() &&
487       // access allowed in the context of static initializer
488       C->needs_clinit_barrier(callee_holder, caller_method)) {
489     return false;
490   }
491   if( !UseInterpreter ) /* running Xcomp */ {
492     // Checks that constant pool's call site has been visited
493     // stricter than callee_holder->is_initialized()
494     ciBytecodeStream iter(caller_method);
495     iter.force_bci(caller_bci);
496     Bytecodes::Code call_bc = iter.cur_bc();
497     // An invokedynamic instruction does not have a klass.
498     if (call_bc != Bytecodes::_invokedynamic) {
499       int index = iter.get_index_u2_cpcache();
500       if (!caller_method->is_klass_loaded(index, true)) {
501         return false;
502       }
503       // Try to do constant pool resolution if running Xcomp
504       if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
505         return false;
506       }
507     }
508   }
509   return true;
510 }
511 
512 //------------------------------check_can_parse--------------------------------
513 const char* InlineTree::check_can_parse(ciMethod* callee) {
514   // Certain methods cannot be parsed at all:
515   if ( callee->is_native())                     return "native method";
516   if ( callee->is_abstract())                   return "abstract method";
517   if (!callee->has_balanced_monitors())         return "not compilable (unbalanced monitors)";
518   if ( callee->get_flow_analysis()->failing())  return "not compilable (flow analysis failed)";
519   if (!callee->can_be_parsed())                 return "cannot be parsed";
520   return NULL;
521 }
522 
523 //------------------------------print_inlining---------------------------------
524 void InlineTree::print_inlining(ciMethod* callee_method, int caller_bci,
525                                 ciMethod* caller_method, bool success) const {
526   const char* inline_msg = msg();
527   assert(inline_msg != NULL, "just checking");
528   if (C->log() != NULL) {
529     if (success) {
530       C->log()->inline_success(inline_msg);
531     } else {
532       C->log()->inline_fail(inline_msg);
533     }
534   }
535   CompileTask::print_inlining_ul(callee_method, inline_level(),
536                                                caller_bci, inline_msg);
537   if (C->print_inlining()) {
538     C->print_inlining(callee_method, inline_level(), caller_bci, inline_msg);
539     guarantee(callee_method != NULL, "would crash in CompilerEvent::InlineEvent::post");
540     if (Verbose) {
541       const InlineTree *top = this;
542       while (top->caller_tree() != NULL) { top = top->caller_tree(); }
543       //tty->print("  bcs: %d+%d  invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
544     }
545   }
546   EventCompilerInlining event;
547   if (event.should_commit()) {
548     CompilerEvent::InlineEvent::post(event, C->compile_id(), caller_method->get_Method(), callee_method, success, inline_msg, caller_bci);
549   }
550 }
551 
552 //------------------------------ok_to_inline-----------------------------------
553 bool InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile,
554                               bool& should_delay) {
555   assert(callee_method != NULL, "caller checks for optimized virtual!");
556   assert(!should_delay, "should be initialized to false");
557 #ifdef ASSERT
558   // Make sure the incoming jvms has the same information content as me.
559   // This means that we can eventually make this whole class AllStatic.
560   if (jvms->caller() == NULL) {
561     assert(_caller_jvms == NULL, "redundant instance state");
562   } else {
563     assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
564   }
565   assert(_method == jvms->method(), "redundant instance state");
566 #endif
567   int         caller_bci    = jvms->bci();
568   ciMethod*   caller_method = jvms->method();
569 
570   // Do some initial checks.
571   if (!pass_initial_checks(caller_method, caller_bci, callee_method)) {
572     set_msg("failed initial checks");
573     print_inlining(callee_method, caller_bci, caller_method, false /* !success */);
574     return false;
575   }
576 
577   // Do some parse checks.
578   set_msg(check_can_parse(callee_method));
579   if (msg() != NULL) {
580     print_inlining(callee_method, caller_bci, caller_method, false /* !success */);
581     return false;
582   }
583 
584   // Check if inlining policy says no.
585   bool success = try_to_inline(callee_method, caller_method, caller_bci, jvms, profile,
586                                should_delay); // out
587   if (success) {
588     // Inline!
589     if (msg() == NULL) {
590       set_msg("inline (hot)");
591     }
592     print_inlining(callee_method, caller_bci, caller_method, true /* success */);
593     build_inline_tree_for_callee(callee_method, jvms, caller_bci);
594     return true;
595   } else {
596     // Do not inline
597     if (msg() == NULL) {
598       set_msg("too cold to inline");
599     }
600     print_inlining(callee_method, caller_bci, caller_method, false /* !success */ );
601     return false;
602   }
603 }
604 
605 //------------------------------build_inline_tree_for_callee-------------------
606 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
607   // Attempt inlining.
608   InlineTree* old_ilt = callee_at(caller_bci, callee_method);
609   if (old_ilt != NULL) {
610     return old_ilt;
611   }
612   int max_inline_level_adjust = 0;
613   if (caller_jvms->method() != NULL) {
614     if (caller_jvms->method()->is_compiled_lambda_form()) {
615       max_inline_level_adjust += 1;  // don't count actions in MH or indy adapter frames
616     } else if (callee_method->is_method_handle_intrinsic() ||
617                callee_method->is_compiled_lambda_form()) {
618       max_inline_level_adjust += 1;  // don't count method handle calls from java.lang.invoke implementation
619     }
620     if (max_inline_level_adjust != 0 && C->print_inlining() && (Verbose || WizardMode)) {
621       CompileTask::print_inline_indent(inline_level());
622       tty->print_cr(" \\-> discounting inline depth");
623     }
624     if (max_inline_level_adjust != 0 && C->log()) {
625       int id1 = C->log()->identify(caller_jvms->method());
626       int id2 = C->log()->identify(callee_method);
627       C->log()->elem("inline_level_discount caller='%d' callee='%d'", id1, id2);
628     }
629   }
630   // Allocate in the comp_arena to make sure the InlineTree is live when dumping a replay compilation file
631   InlineTree* ilt = new (C->comp_arena()) InlineTree(C, this, callee_method, caller_jvms, caller_bci, _max_inline_level + max_inline_level_adjust);
632   _subtrees.append(ilt);
633 
634   NOT_PRODUCT( _count_inlines += 1; )
635 
636   return ilt;
637 }
638 
639 
640 //---------------------------------------callee_at-----------------------------
641 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
642   for (int i = 0; i < _subtrees.length(); i++) {
643     InlineTree* sub = _subtrees.at(i);
644     if (sub->caller_bci() == bci && callee == sub->method()) {
645       return sub;
646     }
647   }
648   return NULL;
649 }
650 
651 
652 //------------------------------build_inline_tree_root-------------------------
653 InlineTree *InlineTree::build_inline_tree_root() {
654   Compile* C = Compile::current();
655 
656   // Root of inline tree
657   InlineTree* ilt = new InlineTree(C, NULL, C->method(), NULL, -1, MaxInlineLevel);
658 
659   return ilt;
660 }
661 
662 
663 //-------------------------find_subtree_from_root-----------------------------
664 // Given a jvms, which determines a call chain from the root method,
665 // find the corresponding inline tree.
666 // Note: This method will be removed or replaced as InlineTree goes away.
667 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee) {
668   InlineTree* iltp = root;
669   uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
670   for (uint d = 1; d <= depth; d++) {
671     JVMState* jvmsp  = jvms->of_depth(d);
672     // Select the corresponding subtree for this bci.
673     assert(jvmsp->method() == iltp->method(), "tree still in sync");
674     ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
675     InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
676     if (sub == NULL) {
677       if (d == depth) {
678         sub = iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
679       }
680       guarantee(sub != NULL, "should be a sub-ilt here");
681       return sub;
682     }
683     iltp = sub;
684   }
685   return iltp;
686 }
687 
688 // Count number of nodes in this subtree
689 int InlineTree::count() const {
690   int result = 1;
691   for (int i = 0 ; i < _subtrees.length(); i++) {
692     result += _subtrees.at(i)->count();
693   }
694   return result;
695 }
696 
697 void InlineTree::dump_replay_data(outputStream* out) {
698   out->print(" %d %d ", inline_level(), caller_bci());
699   method()->dump_name_as_ascii(out);
700   for (int i = 0 ; i < _subtrees.length(); i++) {
701     _subtrees.at(i)->dump_replay_data(out);
702   }
703 }
704 
705 
706 #ifndef PRODUCT
707 void InlineTree::print_impl(outputStream* st, int indent) const {
708   for (int i = 0; i < indent; i++) st->print(" ");
709   st->print(" @ %d", caller_bci());
710   method()->print_short_name(st);
711   st->cr();
712 
713   for (int i = 0 ; i < _subtrees.length(); i++) {
714     _subtrees.at(i)->print_impl(st, indent + 2);
715   }
716 }
717 
718 void InlineTree::print_value_on(outputStream* st) const {
719   print_impl(st, 2);
720 }
721 #endif