bytecodeInfo.cpp 25.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "ci/ciReplay.hpp"
27 28
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
29
#include "compiler/compileBroker.hpp"
30 31 32 33 34 35
#include "compiler/compileLog.hpp"
#include "interpreter/linkResolver.hpp"
#include "oops/objArrayKlass.hpp"
#include "opto/callGenerator.hpp"
#include "opto/parse.hpp"
#include "runtime/handles.inline.hpp"
D
duke 已提交
36 37 38

//=============================================================================
//------------------------------InlineTree-------------------------------------
39 40 41 42 43 44 45 46 47 48
InlineTree::InlineTree(Compile* c,
                       const InlineTree *caller_tree, ciMethod* callee,
                       JVMState* caller_jvms, int caller_bci,
                       float site_invoke_ratio, int max_inline_level) :
  C(c),
  _caller_jvms(caller_jvms),
  _caller_tree((InlineTree*) caller_tree),
  _method(callee),
  _site_invoke_ratio(site_invoke_ratio),
  _max_inline_level(max_inline_level),
R
roland 已提交
49
  _count_inline_bcs(method()->code_size_for_inlining()),
50 51
  _subtrees(c->comp_arena(), 2, 0, NULL),
  _msg(NULL)
52
{
D
duke 已提交
53 54 55 56 57
  NOT_PRODUCT(_count_inlines = 0;)
  if (_caller_jvms != NULL) {
    // Keep a private copy of the caller_jvms:
    _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
    _caller_jvms->set_bci(caller_jvms->bci());
58
    assert(!caller_jvms->should_reexecute(), "there should be no reexecute bytecode with inlining");
D
duke 已提交
59 60
  }
  assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
61
  assert((caller_tree == NULL ? 0 : caller_tree->stack_depth() + 1) == stack_depth(), "correct (redundant) depth parameter");
D
duke 已提交
62 63 64 65 66 67 68 69 70 71 72
  assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
  if (UseOldInlining) {
    // Update hierarchical counts, count_inline_bcs() and count_inlines()
    InlineTree *caller = (InlineTree *)caller_tree;
    for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
      caller->_count_inline_bcs += count_inline_bcs();
      NOT_PRODUCT(caller->_count_inlines++;)
    }
  }
}

73
InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms,
74 75 76 77 78 79 80
                       float site_invoke_ratio, int max_inline_level) :
  C(c),
  _caller_jvms(caller_jvms),
  _caller_tree(NULL),
  _method(callee_method),
  _site_invoke_ratio(site_invoke_ratio),
  _max_inline_level(max_inline_level),
81 82
  _count_inline_bcs(method()->code_size()),
  _msg(NULL)
83
{
D
duke 已提交
84 85 86 87
  NOT_PRODUCT(_count_inlines = 0;)
  assert(!UseOldInlining, "do not use for old stuff");
}

88 89 90 91 92
/**
 *  Return true when EA is ON and a java constructor is called or
 *  a super constructor is called from an inlined java constructor.
 *  Also return true for boxing methods.
 */
93 94
static bool is_init_with_ea(ciMethod* callee_method,
                            ciMethod* caller_method, Compile* C) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  if (!C->do_escape_analysis() || !EliminateAllocations) {
    return false; // EA is off
  }
  if (callee_method->is_initializer()) {
    return true; // constuctor
  }
  if (caller_method->is_initializer() &&
      caller_method != C->method() &&
      caller_method->holder()->is_subclass_of(callee_method->holder())) {
    return true; // super constructor is called from inlined constructor
  }
  if (C->eliminate_boxing() && callee_method->is_boxing_method()) {
    return true;
  }
  return false;
110 111
}

112 113 114
/**
 *  Force inlining unboxing accessor.
 */
115 116 117 118
static bool is_unboxing_method(ciMethod* callee_method, Compile* C) {
  return C->eliminate_boxing() && callee_method->is_unboxing_method();
}

119 120 121 122
// positive filter: should callee be inlined?
bool InlineTree::should_inline(ciMethod* callee_method, ciMethod* caller_method,
                               int caller_bci, ciCallProfile& profile,
                               WarmCallInfo* wci_result) {
D
duke 已提交
123 124 125
  // Allows targeted inlining
  if(callee_method->should_inline()) {
    *wci_result = *(WarmCallInfo::always_hot());
126
    if (C->print_inlining() && Verbose) {
127
      CompileTask::print_inline_indent(inline_level());
D
duke 已提交
128 129
      tty->print_cr("Inlined method is hot: ");
    }
130 131
    set_msg("force inline by CompilerOracle");
    return true;
D
duke 已提交
132 133
  }

134
  int size = callee_method->code_size_for_inlining();
D
duke 已提交
135 136

  // Check for too many throws (and not too huge)
137 138
  if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
     size < InlineThrowMaxSize ) {
D
duke 已提交
139
    wci_result->set_profit(wci_result->profit() * 100);
140
    if (C->print_inlining() && Verbose) {
141
      CompileTask::print_inline_indent(inline_level());
D
duke 已提交
142 143
      tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
    }
144 145
    set_msg("many throws");
    return true;
D
duke 已提交
146 147 148
  }

  if (!UseOldInlining) {
149 150
    set_msg("!UseOldInlining");
    return true;  // size and frequency are represented in a new way
D
duke 已提交
151 152
  }

153 154 155 156
  int default_max_inline_size = C->max_inline_size();
  int inline_small_code_size  = InlineSmallCode / 4;
  int max_inline_size         = default_max_inline_size;

D
duke 已提交
157 158
  int call_site_count  = method()->scale_count(profile.count());
  int invoke_count     = method()->interpreter_invocation_count();
159 160 161

  assert(invoke_count != 0, "require invocation count greater than zero");
  int freq = call_site_count / invoke_count;
162

D
duke 已提交
163
  // bump the max size if the call is frequent
164 165
  if ((freq >= InlineFrequencyRatio) ||
      (call_site_count >= InlineFrequencyCount) ||
166
      is_unboxing_method(callee_method, C) ||
167 168
      is_init_with_ea(callee_method, caller_method, C)) {

169 170
    max_inline_size = C->freq_inline_size();
    if (size <= max_inline_size && TraceFrequencyInlining) {
171
      CompileTask::print_inline_indent(inline_level());
D
duke 已提交
172
      tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
173
      CompileTask::print_inline_indent(inline_level());
D
duke 已提交
174 175 176 177 178
      callee_method->print();
      tty->cr();
    }
  } else {
    // Not hot.  Check for medium-sized pre-existing nmethod at cold sites.
179
    if (callee_method->has_compiled_code() &&
180
        callee_method->instructions_size() > inline_small_code_size) {
181 182
      set_msg("already compiled into a medium method");
      return false;
183
    }
D
duke 已提交
184
  }
185
  if (size > max_inline_size) {
186 187 188 189 190 191
    if (max_inline_size > default_max_inline_size) {
      set_msg("hot method too big");
    } else {
      set_msg("too big");
    }
    return false;
D
duke 已提交
192
  }
193
  return true;
D
duke 已提交
194 195 196
}


197 198 199
// negative filter: should callee NOT be inlined?
bool InlineTree::should_not_inline(ciMethod *callee_method,
                                   ciMethod* caller_method,
200
                                   JVMState* jvms,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                                   WarmCallInfo* wci_result) {

  const char* fail_msg = NULL;

  // First check all inlining restrictions which are required for correctness
  if ( callee_method->is_abstract()) {
    fail_msg = "abstract method"; // // note: we allow ik->is_abstract()
  } else if (!callee_method->holder()->is_initialized()) {
    fail_msg = "method holder not initialized";
  } else if ( callee_method->is_native()) {
    fail_msg = "native method";
  } else if ( callee_method->dont_inline()) {
    fail_msg = "don't inline by annotation";
  }

D
duke 已提交
216
  if (!UseOldInlining) {
217
    if (fail_msg != NULL) {
D
duke 已提交
218
      *wci_result = *(WarmCallInfo::always_cold());
219 220
      set_msg(fail_msg);
      return true;
D
duke 已提交
221 222 223 224 225 226 227 228 229
    }

    if (callee_method->has_unloaded_classes_in_signature()) {
      wci_result->set_profit(wci_result->profit() * 0.1);
    }

    // don't inline exception code unless the top method belongs to an
    // exception class
    if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
230
      ciMethod* top_method = jvms->caller() != NULL ? jvms->caller()->of_depth(1)->method() : method();
D
duke 已提交
231 232 233 234 235
      if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
        wci_result->set_profit(wci_result->profit() * 0.1);
      }
    }

236
    if (callee_method->has_compiled_code() &&
237
        callee_method->instructions_size() > InlineSmallCode) {
D
duke 已提交
238 239 240 241
      wci_result->set_profit(wci_result->profit() * 0.1);
      // %%% adjust wci_result->size()?
    }

242
    return false;
D
duke 已提交
243 244
  }

245 246 247 248
  // one more inlining restriction
  if (fail_msg == NULL && callee_method->has_unloaded_classes_in_signature()) {
    fail_msg = "unloaded signature classes";
  }
D
duke 已提交
249

250 251 252 253 254 255
  if (fail_msg != NULL) {
    set_msg(fail_msg);
    return true;
  }

  // ignore heuristic controls on inlining
R
roland 已提交
256
  if (callee_method->should_inline()) {
257 258
    set_msg("force inline by CompilerOracle");
    return false;
D
duke 已提交
259 260
  }

261 262 263 264 265 266 267 268 269 270 271 272
  if (callee_method->should_not_inline()) {
    set_msg("disallowed by CompilerOracle");
    return true;
  }

#ifndef PRODUCT
  if (ciReplay::should_not_inline(callee_method)) {
    set_msg("disallowed by ciReplay");
    return true;
  }
#endif

D
duke 已提交
273 274
  // Now perform checks which are heuristic

275 276 277 278 279
  if (is_unboxing_method(callee_method, C)) {
    // Inline unboxing methods.
    return false;
  }

R
roland 已提交
280 281 282
  if (!callee_method->force_inline()) {
    if (callee_method->has_compiled_code() &&
        callee_method->instructions_size() > InlineSmallCode) {
283 284
      set_msg("already compiled into a big method");
      return true;
R
roland 已提交
285
    }
286
  }
D
duke 已提交
287 288 289 290 291 292 293 294

  // don't inline exception code unless the top method belongs to an
  // exception class
  if (caller_tree() != NULL &&
      callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
    const InlineTree *top = this;
    while (top->caller_tree() != NULL) top = top->caller_tree();
    ciInstanceKlass* k = top->method()->holder();
295 296 297 298
    if (!k->is_subclass_of(C->env()->Throwable_klass())) {
      set_msg("exception method");
      return true;
    }
D
duke 已提交
299 300 301
  }

  // use frequency-based objections only for non-trivial methods
302 303 304
  if (callee_method->code_size() <= MaxTrivialSize) {
    return false;
  }
305 306 307 308 309 310

  // don't use counts with -Xcomp or CTW
  if (UseInterpreter && !CompileTheWorld) {

    if (!callee_method->has_compiled_code() &&
        !callee_method->was_executed_more_than(0)) {
311 312
      set_msg("never executed");
      return true;
313 314 315 316
    }

    if (is_init_with_ea(callee_method, caller_method, C)) {
      // Escape Analysis: inline all executed constructors
317
      return false;
318 319
    } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
                                                           CompileThreshold >> 1))) {
320 321
      set_msg("executed < MinInliningThreshold times");
      return true;
322
    }
D
duke 已提交
323 324
  }

325
  return false;
D
duke 已提交
326 327 328
}

//-----------------------------try_to_inline-----------------------------------
329
// return true if ok
D
duke 已提交
330
// Relocated from "InliningClosure::try_to_inline"
331
bool InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method,
332
                               int caller_bci, JVMState* jvms, ciCallProfile& profile,
333 334 335
                               WarmCallInfo* wci_result, bool& should_delay) {

   // Old algorithm had funny accumulating BC-size counters
D
duke 已提交
336 337
  if (UseOldInlining && ClipInlining
      && (int)count_inline_bcs() >= DesiredMethodLimit) {
R
roland 已提交
338
    if (!callee_method->force_inline() || !IncrementalInline) {
339 340
      set_msg("size > DesiredMethodLimit");
      return false;
R
roland 已提交
341 342 343
    } else if (!C->inlining_incrementally()) {
      should_delay = true;
    }
D
duke 已提交
344 345
  }

346 347 348 349
  if (!should_inline(callee_method, caller_method, caller_bci, profile,
                     wci_result)) {
    return false;
  }
350
  if (should_not_inline(callee_method, caller_method, jvms, wci_result)) {
351 352
    return false;
  }
D
duke 已提交
353

354 355
  if (InlineAccessors && callee_method->is_accessor()) {
    // accessor methods are not subject to any of the following limits.
356 357
    set_msg("accessor");
    return true;
358
  }
D
duke 已提交
359 360

  // suppress a few checks for accessors and trivial methods
361
  if (callee_method->code_size() > MaxTrivialSize) {
362

D
duke 已提交
363
    // don't inline into giant methods
R
roland 已提交
364 365 366
    if (C->over_inlining_cutoff()) {
      if ((!callee_method->force_inline() && !caller_method->is_compiled_lambda_form())
          || !IncrementalInline) {
367 368
        set_msg("NodeCountInliningCutoff");
        return false;
R
roland 已提交
369 370 371
      } else {
        should_delay = true;
      }
372
    }
D
duke 已提交
373

374 375
    if ((!UseInterpreter || CompileTheWorld) &&
        is_init_with_ea(callee_method, caller_method, C)) {
D
duke 已提交
376

377 378
      // Escape Analysis stress testing when running Xcomp or CTW:
      // inline constructors even if they are not reached.
D
duke 已提交
379

380 381
    } else if (profile.count() == 0) {
      // don't inline unreached call sites
382 383
       set_msg("call site not reached");
       return false;
384 385 386
    }
  }

387
  if (!C->do_inlining() && InlineAccessors) {
388 389
    set_msg("not an accessor");
    return false;
390
  }
391
  if (inline_level() > _max_inline_level) {
392 393 394 395
    if (callee_method->force_inline() && inline_level() > MaxForceInlineLevel) {
      set_msg("MaxForceInlineLevel");
      return false;
    }
R
roland 已提交
396
    if (!callee_method->force_inline() || !IncrementalInline) {
397 398
      set_msg("inlining too deep");
      return false;
R
roland 已提交
399 400 401
    } else if (!C->inlining_incrementally()) {
      should_delay = true;
    }
402
  }
403

404
  // detect direct and indirect recursive inlining
405
  {
406
    // count the current method and the callee
407 408 409 410 411 412
    const bool is_compiled_lambda_form = callee_method->is_compiled_lambda_form();
    int inline_level = 0;
    if (!is_compiled_lambda_form) {
      if (method() == callee_method) {
        inline_level++;
      }
413
    }
414
    // count callers of current method and callee
415 416 417 418 419 420 421 422 423 424 425 426 427
    Node* callee_argument0 = is_compiled_lambda_form ? jvms->map()->argument(jvms, 0)->uncast() : NULL;
    for (JVMState* j = jvms->caller(); j != NULL && j->has_method(); j = j->caller()) {
      if (j->method() == callee_method) {
        if (is_compiled_lambda_form) {
          // Since compiled lambda forms are heavily reused we allow recursive inlining.  If it is truly
          // a recursion (using the same "receiver") we limit inlining otherwise we can easily blow the
          // compiler stack.
          Node* caller_argument0 = j->map()->argument(j, 0)->uncast();
          if (caller_argument0 == callee_argument0) {
            inline_level++;
          }
        } else {
          inline_level++;
428
        }
429
      }
430 431 432 433
    }
    if (inline_level > MaxRecursiveInlineLevel) {
      set_msg("recursive inlining is too deep");
      return false;
434 435 436
    }
  }

437
  int size = callee_method->code_size_for_inlining();
D
duke 已提交
438 439 440

  if (UseOldInlining && ClipInlining
      && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
R
roland 已提交
441
    if (!callee_method->force_inline() || !IncrementalInline) {
442 443
      set_msg("size > DesiredMethodLimit");
      return false;
R
roland 已提交
444 445 446
    } else if (!C->inlining_incrementally()) {
      should_delay = true;
    }
D
duke 已提交
447 448 449
  }

  // ok, inline this method
450
  return true;
D
duke 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
}

//------------------------------pass_initial_checks----------------------------
bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
  ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
  // Check if a callee_method was suggested
  if( callee_method == NULL )            return false;
  // Check if klass of callee_method is loaded
  if( !callee_holder->is_loaded() )      return false;
  if( !callee_holder->is_initialized() ) return false;
  if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
    // Checks that constant pool's call site has been visited
    // stricter than callee_holder->is_initialized()
    ciBytecodeStream iter(caller_method);
    iter.force_bci(caller_bci);
    Bytecodes::Code call_bc = iter.cur_bc();
467 468
    // An invokedynamic instruction does not have a klass.
    if (call_bc != Bytecodes::_invokedynamic) {
469
      int index = iter.get_index_u2_cpcache();
470 471 472 473 474 475 476
      if (!caller_method->is_klass_loaded(index, true)) {
        return false;
      }
      // Try to do constant pool resolution if running Xcomp
      if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
        return false;
      }
D
duke 已提交
477 478 479 480 481 482 483 484 485 486
    }
  }
  // We will attempt to see if a class/field/etc got properly loaded.  If it
  // did not, it may attempt to throw an exception during our probing.  Catch
  // and ignore such exceptions and do not attempt to compile the method.
  if( callee_method->should_exclude() )  return false;

  return true;
}

487 488 489 490
//------------------------------check_can_parse--------------------------------
const char* InlineTree::check_can_parse(ciMethod* callee) {
  // Certain methods cannot be parsed at all:
  if ( callee->is_native())                     return "native method";
491
  if ( callee->is_abstract())                   return "abstract method";
492 493 494 495 496 497
  if (!callee->can_be_compiled())               return "not compilable (disabled)";
  if (!callee->has_balanced_monitors())         return "not compilable (unbalanced monitors)";
  if ( callee->get_flow_analysis()->failing())  return "not compilable (flow analysis failed)";
  return NULL;
}

D
duke 已提交
498
//------------------------------print_inlining---------------------------------
499
void InlineTree::print_inlining(ciMethod* callee_method, int caller_bci,
500 501 502
                                bool success) const {
  const char* inline_msg = msg();
  assert(inline_msg != NULL, "just checking");
503 504
  if (C->log() != NULL) {
    if (success) {
505
      C->log()->inline_success(inline_msg);
506
    } else {
507
      C->log()->inline_fail(inline_msg);
508 509
    }
  }
510
  if (C->print_inlining()) {
511
    C->print_inlining(callee_method, inline_level(), caller_bci, inline_msg);
512 513 514 515 516 517
    if (callee_method == NULL) tty->print(" callee not monotonic or profiled");
    if (Verbose && callee_method) {
      const InlineTree *top = this;
      while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
      //tty->print("  bcs: %d+%d  invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
    }
D
duke 已提交
518 519 520 521
  }
}

//------------------------------ok_to_inline-----------------------------------
R
roland 已提交
522
WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci, bool& should_delay) {
D
duke 已提交
523
  assert(callee_method != NULL, "caller checks for optimized virtual!");
R
roland 已提交
524
  assert(!should_delay, "should be initialized to false");
D
duke 已提交
525 526 527 528 529 530 531 532 533 534 535
#ifdef ASSERT
  // Make sure the incoming jvms has the same information content as me.
  // This means that we can eventually make this whole class AllStatic.
  if (jvms->caller() == NULL) {
    assert(_caller_jvms == NULL, "redundant instance state");
  } else {
    assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
  }
  assert(_method == jvms->method(), "redundant instance state");
#endif
  int         caller_bci    = jvms->bci();
536
  ciMethod*   caller_method = jvms->method();
D
duke 已提交
537

538 539
  // Do some initial checks.
  if (!pass_initial_checks(caller_method, caller_bci, callee_method)) {
540 541
    set_msg("failed initial checks");
    print_inlining(callee_method, caller_bci, false /* !success */);
D
duke 已提交
542 543 544
    return NULL;
  }

545
  // Do some parse checks.
546 547 548
  set_msg(check_can_parse(callee_method));
  if (msg() != NULL) {
    print_inlining(callee_method, caller_bci, false /* !success */);
549 550 551
    return NULL;
  }

D
duke 已提交
552 553
  // Check if inlining policy says no.
  WarmCallInfo wci = *(initial_wci);
554
  bool success = try_to_inline(callee_method, caller_method, caller_bci,
555
                               jvms, profile, &wci, should_delay);
D
duke 已提交
556 557 558

#ifndef PRODUCT
  if (UseOldInlining && InlineWarmCalls
559
      && (PrintOpto || C->print_inlining())) {
D
duke 已提交
560 561
    bool cold = wci.is_cold();
    bool hot  = !cold && wci.is_hot();
562
    bool old_cold = !success;
D
duke 已提交
563
    if (old_cold != cold || (Verbose || WizardMode)) {
564 565 566
      if (msg() == NULL) {
        set_msg("OK");
      }
D
duke 已提交
567
      tty->print("   OldInlining= %4s : %s\n           WCI=",
568
                 old_cold ? "cold" : "hot", msg());
D
duke 已提交
569 570 571 572 573
      wci.print();
    }
  }
#endif
  if (UseOldInlining) {
574
    if (success) {
D
duke 已提交
575
      wci = *(WarmCallInfo::always_hot());
576
    } else {
D
duke 已提交
577
      wci = *(WarmCallInfo::always_cold());
578
    }
579
  }
D
duke 已提交
580 581 582 583 584 585 586 587 588
  if (!InlineWarmCalls) {
    if (!wci.is_cold() && !wci.is_hot()) {
      // Do not inline the warm calls.
      wci = *(WarmCallInfo::always_cold());
    }
  }

  if (!wci.is_cold()) {
    // Inline!
589 590 591 592
    if (msg() == NULL) {
      set_msg("inline (hot)");
    }
    print_inlining(callee_method, caller_bci, true /* success */);
D
duke 已提交
593 594 595 596 597 598 599 600
    if (UseOldInlining)
      build_inline_tree_for_callee(callee_method, jvms, caller_bci);
    if (InlineWarmCalls && !wci.is_hot())
      return new (C) WarmCallInfo(wci);  // copy to heap
    return WarmCallInfo::always_hot();
  }

  // Do not inline
601 602 603 604
  if (msg() == NULL) {
    set_msg("too cold to inline");
  }
  print_inlining(callee_method, caller_bci, false /* !success */ );
D
duke 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  return NULL;
}

//------------------------------compute_callee_frequency-----------------------
float InlineTree::compute_callee_frequency( int caller_bci ) const {
  int count  = method()->interpreter_call_site_count(caller_bci);
  int invcnt = method()->interpreter_invocation_count();
  float freq = (float)count/(float)invcnt;
  // Call-site count / interpreter invocation count, scaled recursively.
  // Always between 0.0 and 1.0.  Represents the percentage of the method's
  // total execution time used at this call site.

  return freq;
}

//------------------------------build_inline_tree_for_callee-------------------
InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
  float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
  // Attempt inlining.
  InlineTree* old_ilt = callee_at(caller_bci, callee_method);
  if (old_ilt != NULL) {
    return old_ilt;
  }
628
  int max_inline_level_adjust = 0;
629
  if (caller_jvms->method() != NULL) {
630
    if (caller_jvms->method()->is_compiled_lambda_form())
631
      max_inline_level_adjust += 1;  // don't count actions in MH or indy adapter frames
632 633
    else if (callee_method->is_method_handle_intrinsic() ||
             callee_method->is_compiled_lambda_form()) {
634
      max_inline_level_adjust += 1;  // don't count method handle calls from java.lang.invoke implem
635
    }
636
    if (max_inline_level_adjust != 0 && C->print_inlining() && (Verbose || WizardMode)) {
637
      CompileTask::print_inline_indent(inline_level());
638
      tty->print_cr(" \\-> discounting inline depth");
639
    }
640
    if (max_inline_level_adjust != 0 && C->log()) {
641 642
      int id1 = C->log()->identify(caller_jvms->method());
      int id2 = C->log()->identify(callee_method);
643
      C->log()->elem("inline_level_discount caller='%d' callee='%d'", id1, id2);
644 645
    }
  }
646 647
  InlineTree* ilt = new InlineTree(C, this, callee_method, caller_jvms, caller_bci, recur_frequency, _max_inline_level + max_inline_level_adjust);
  _subtrees.append(ilt);
D
duke 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

  NOT_PRODUCT( _count_inlines += 1; )

  return ilt;
}


//---------------------------------------callee_at-----------------------------
InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
  for (int i = 0; i < _subtrees.length(); i++) {
    InlineTree* sub = _subtrees.at(i);
    if (sub->caller_bci() == bci && callee == sub->method()) {
      return sub;
    }
  }
  return NULL;
}


//------------------------------build_inline_tree_root-------------------------
InlineTree *InlineTree::build_inline_tree_root() {
  Compile* C = Compile::current();

  // Root of inline tree
672
  InlineTree* ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F, MaxInlineLevel);
D
duke 已提交
673 674 675 676 677 678 679 680 681

  return ilt;
}


//-------------------------find_subtree_from_root-----------------------------
// Given a jvms, which determines a call chain from the root method,
// find the corresponding inline tree.
// Note: This method will be removed or replaced as InlineTree goes away.
682
InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee) {
D
duke 已提交
683 684 685 686 687 688 689 690
  InlineTree* iltp = root;
  uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
  for (uint d = 1; d <= depth; d++) {
    JVMState* jvmsp  = jvms->of_depth(d);
    // Select the corresponding subtree for this bci.
    assert(jvmsp->method() == iltp->method(), "tree still in sync");
    ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
    InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
691 692 693
    if (sub == NULL) {
      if (d == depth) {
        sub = iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
D
duke 已提交
694
      }
695 696
      guarantee(sub != NULL, "should be a sub-ilt here");
      return sub;
D
duke 已提交
697 698 699 700 701
    }
    iltp = sub;
  }
  return iltp;
}
N
never 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720



#ifndef PRODUCT
void InlineTree::print_impl(outputStream* st, int indent) const {
  for (int i = 0; i < indent; i++) st->print(" ");
  st->print(" @ %d ", caller_bci());
  method()->print_short_name(st);
  st->cr();

  for (int i = 0 ; i < _subtrees.length(); i++) {
    _subtrees.at(i)->print_impl(st, indent + 2);
  }
}

void InlineTree::print_value_on(outputStream* st) const {
  print_impl(st, 2);
}
#endif