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