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