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