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