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