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