1 /*
   2  * Copyright (c) 2000, 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 "classfile/classLoaderData.inline.hpp"
  27 #include "code/compiledIC.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "code/scopeDesc.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/methodData.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/nativeLookup.hpp"
  36 #include "runtime/advancedThresholdPolicy.hpp"
  37 #include "runtime/compilationPolicy.hpp"
  38 #include "runtime/frame.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/rframe.hpp"
  41 #include "runtime/simpleThresholdPolicy.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "runtime/thread.hpp"
  44 #include "runtime/timer.hpp"
  45 #include "runtime/vframe.hpp"
  46 #include "runtime/vm_operations.hpp"
  47 #include "utilities/events.hpp"
  48 #include "utilities/globalDefinitions.hpp"
  49 
  50 CompilationPolicy* CompilationPolicy::_policy;
  51 elapsedTimer       CompilationPolicy::_accumulated_time;
  52 bool               CompilationPolicy::_in_vm_startup;
  53 
  54 // Determine compilation policy based on command line argument
  55 void compilationPolicy_init() {
  56   CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
  57 
  58   switch(CompilationPolicyChoice) {
  59   case 0:
  60     CompilationPolicy::set_policy(new SimpleCompPolicy());
  61     break;
  62 
  63   case 1:
  64 #ifdef COMPILER2
  65     CompilationPolicy::set_policy(new StackWalkCompPolicy());
  66 #else
  67     Unimplemented();
  68 #endif
  69     break;
  70   case 2:
  71 #ifdef TIERED
  72     CompilationPolicy::set_policy(new SimpleThresholdPolicy());
  73 #else
  74     Unimplemented();
  75 #endif
  76     break;
  77   case 3:
  78 #ifdef TIERED
  79     CompilationPolicy::set_policy(new AdvancedThresholdPolicy());
  80 #else
  81     Unimplemented();
  82 #endif
  83     break;
  84   default:
  85     fatal("CompilationPolicyChoice must be in the range: [0-3]");
  86   }
  87   CompilationPolicy::policy()->initialize();
  88 }
  89 
  90 void CompilationPolicy::completed_vm_startup() {
  91   if (TraceCompilationPolicy) {
  92     tty->print("CompilationPolicy: completed vm startup.\n");
  93   }
  94   _in_vm_startup = false;
  95 }
  96 
  97 // Returns true if m must be compiled before executing it
  98 // This is intended to force compiles for methods (usually for
  99 // debugging) that would otherwise be interpreted for some reason.
 100 bool CompilationPolicy::must_be_compiled(const methodHandle& m, int comp_level) {
 101   // Don't allow Xcomp to cause compiles in replay mode
 102   if (ReplayCompiles) return false;
 103 
 104   if (m->has_compiled_code()) return false;       // already compiled
 105   if (!can_be_compiled(m, comp_level)) return false;
 106 
 107   return !UseInterpreter ||                                              // must compile all methods
 108          (UseCompiler && AlwaysCompileLoopMethods && m->has_loops() && CompileBroker::should_compile_new_jobs()); // eagerly compile loop methods
 109 }
 110 
 111 void CompilationPolicy::compile_if_required(const methodHandle& selected_method, TRAPS) {
 112   if (must_be_compiled(selected_method)) {
 113     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
 114 
 115     // Note: with several active threads, the must_be_compiled may be true
 116     //       while can_be_compiled is false; remove assert
 117     // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile");
 118     if (!THREAD->can_call_java() || THREAD->is_Compiler_thread()) {
 119       // don't force compilation, resolve was on behalf of compiler
 120       return;
 121     }
 122     if (selected_method->method_holder()->is_not_initialized()) {
 123       // 'is_not_initialized' means not only '!is_initialized', but also that
 124       // initialization has not been started yet ('!being_initialized')
 125       // Do not force compilation of methods in uninitialized classes.
 126       // Note that doing this would throw an assert later,
 127       // in CompileBroker::compile_method.
 128       // We sometimes use the link resolver to do reflective lookups
 129       // even before classes are initialized.
 130       return;
 131     }
 132     CompileBroker::compile_method(selected_method, InvocationEntryBci,
 133         CompilationPolicy::policy()->initial_compile_level(),
 134         methodHandle(), 0, CompileTask::Reason_MustBeCompiled, CHECK);
 135   }
 136 }
 137 
 138 // Returns true if m is allowed to be compiled
 139 bool CompilationPolicy::can_be_compiled(const methodHandle& m, int comp_level) {
 140   // allow any levels for WhiteBox
 141   assert(WhiteBoxAPI || comp_level == CompLevel_all || is_compile(comp_level), "illegal compilation level");
 142 
 143   if (m->is_abstract()) return false;
 144   if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
 145 
 146   // Math intrinsics should never be compiled as this can lead to
 147   // monotonicity problems because the interpreter will prefer the
 148   // compiled code to the intrinsic version.  This can't happen in
 149   // production because the invocation counter can't be incremented
 150   // but we shouldn't expose the system to this problem in testing
 151   // modes.
 152   if (!AbstractInterpreter::can_be_compiled(m)) {
 153     return false;
 154   }
 155   if (comp_level == CompLevel_all) {
 156     if (TieredCompilation) {
 157       // enough to be compilable at any level for tiered
 158       return !m->is_not_compilable(CompLevel_simple) || !m->is_not_compilable(CompLevel_full_optimization);
 159     } else {
 160       // must be compilable at available level for non-tiered
 161       return !m->is_not_compilable(CompLevel_highest_tier);
 162     }
 163   } else if (is_compile(comp_level)) {
 164     return !m->is_not_compilable(comp_level);
 165   }
 166   return false;
 167 }
 168 
 169 // Returns true if m is allowed to be osr compiled
 170 bool CompilationPolicy::can_be_osr_compiled(const methodHandle& m, int comp_level) {
 171   bool result = false;
 172   if (comp_level == CompLevel_all) {
 173     if (TieredCompilation) {
 174       // enough to be osr compilable at any level for tiered
 175       result = !m->is_not_osr_compilable(CompLevel_simple) || !m->is_not_osr_compilable(CompLevel_full_optimization);
 176     } else {
 177       // must be osr compilable at available level for non-tiered
 178       result = !m->is_not_osr_compilable(CompLevel_highest_tier);
 179     }
 180   } else if (is_compile(comp_level)) {
 181     result = !m->is_not_osr_compilable(comp_level);
 182   }
 183   return (result && can_be_compiled(m, comp_level));
 184 }
 185 
 186 bool CompilationPolicy::is_compilation_enabled() {
 187   // NOTE: CompileBroker::should_compile_new_jobs() checks for UseCompiler
 188   return !delay_compilation_during_startup() && CompileBroker::should_compile_new_jobs();
 189 }
 190 
 191 CompileTask* CompilationPolicy::select_task_helper(CompileQueue* compile_queue) {
 192 #if INCLUDE_JVMCI
 193   if (UseJVMCICompiler && !BackgroundCompilation) {
 194     /*
 195      * In blocking compilation mode, the CompileBroker will make
 196      * compilations submitted by a JVMCI compiler thread non-blocking. These
 197      * compilations should be scheduled after all blocking compilations
 198      * to service non-compiler related compilations sooner and reduce the
 199      * chance of such compilations timing out.
 200      */
 201     for (CompileTask* task = compile_queue->first(); task != NULL; task = task->next()) {
 202       if (task->is_blocking()) {
 203         return task;
 204       }
 205     }
 206   }
 207 #endif
 208   return compile_queue->first();
 209 }
 210 
 211 #ifndef PRODUCT
 212 void CompilationPolicy::print_time() {
 213   tty->print_cr ("Accumulated compilationPolicy times:");
 214   tty->print_cr ("---------------------------");
 215   tty->print_cr ("  Total: %3.3f sec.", _accumulated_time.seconds());
 216 }
 217 
 218 void NonTieredCompPolicy::trace_osr_completion(nmethod* osr_nm) {
 219   if (TraceOnStackReplacement) {
 220     if (osr_nm == NULL) tty->print_cr("compilation failed");
 221     else tty->print_cr("nmethod " INTPTR_FORMAT, p2i(osr_nm));
 222   }
 223 }
 224 #endif // !PRODUCT
 225 
 226 void NonTieredCompPolicy::initialize() {
 227   // Setup the compiler thread numbers
 228   if (CICompilerCountPerCPU) {
 229     // Example: if CICompilerCountPerCPU is true, then we get
 230     // max(log2(8)-1,1) = 2 compiler threads on an 8-way machine.
 231     // May help big-app startup time.
 232     _compiler_count = MAX2(log2_intptr(os::active_processor_count())-1,1);
 233     FLAG_SET_ERGO(intx, CICompilerCount, _compiler_count);
 234   } else {
 235     _compiler_count = CICompilerCount;
 236   }
 237 }
 238 
 239 // Note: this policy is used ONLY if TieredCompilation is off.
 240 // compiler_count() behaves the following way:
 241 // - with TIERED build (with both COMPILER1 and COMPILER2 defined) it should return
 242 //   zero for the c1 compilation levels in server compilation mode runs
 243 //   and c2 compilation levels in client compilation mode runs.
 244 // - with COMPILER2 not defined it should return zero for c2 compilation levels.
 245 // - with COMPILER1 not defined it should return zero for c1 compilation levels.
 246 // - if neither is defined - always return zero.
 247 int NonTieredCompPolicy::compiler_count(CompLevel comp_level) {
 248   assert(!TieredCompilation, "This policy should not be used with TieredCompilation");
 249   if (COMPILER2_PRESENT(is_server_compilation_mode_vm() && is_c2_compile(comp_level) ||)
 250       is_client_compilation_mode_vm() && is_c1_compile(comp_level)) {
 251     return _compiler_count;
 252   }
 253   return 0;
 254 }
 255 
 256 void NonTieredCompPolicy::reset_counter_for_invocation_event(const methodHandle& m) {
 257   // Make sure invocation and backedge counter doesn't overflow again right away
 258   // as would be the case for native methods.
 259 
 260   // BUT also make sure the method doesn't look like it was never executed.
 261   // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
 262   MethodCounters* mcs = m->method_counters();
 263   assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 264   mcs->invocation_counter()->set_carry();
 265   mcs->backedge_counter()->set_carry();
 266 
 267   assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
 268 }
 269 
 270 void NonTieredCompPolicy::reset_counter_for_back_branch_event(const methodHandle& m) {
 271   // Delay next back-branch event but pump up invocation counter to trigger
 272   // whole method compilation.
 273   MethodCounters* mcs = m->method_counters();
 274   assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 275   InvocationCounter* i = mcs->invocation_counter();
 276   InvocationCounter* b = mcs->backedge_counter();
 277 
 278   // Don't set invocation_counter's value too low otherwise the method will
 279   // look like immature (ic < ~5300) which prevents the inlining based on
 280   // the type profiling.
 281   i->set(i->state(), CompileThreshold);
 282   // Don't reset counter too low - it is used to check if OSR method is ready.
 283   b->set(b->state(), CompileThreshold / 2);
 284 }
 285 
 286 //
 287 // CounterDecay
 288 //
 289 // Iterates through invocation counters and decrements them. This
 290 // is done at each safepoint.
 291 //
 292 class CounterDecay : public AllStatic {
 293   static jlong _last_timestamp;
 294   static void do_method(Method* m) {
 295     MethodCounters* mcs = m->method_counters();
 296     if (mcs != NULL) {
 297       mcs->invocation_counter()->decay();
 298     }
 299   }
 300 public:
 301   static void decay();
 302   static bool is_decay_needed() {
 303     return (os::javaTimeMillis() - _last_timestamp) > CounterDecayMinIntervalLength;
 304   }
 305 };
 306 
 307 jlong CounterDecay::_last_timestamp = 0;
 308 
 309 void CounterDecay::decay() {
 310   _last_timestamp = os::javaTimeMillis();
 311 
 312   // This operation is going to be performed only at the end of a safepoint
 313   // and hence GC's will not be going on, all Java mutators are suspended
 314   // at this point and hence SystemDictionary_lock is also not needed.
 315   assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
 316   size_t nclasses = ClassLoaderDataGraph::num_instance_classes();
 317   size_t classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
 318                                         CounterHalfLifeTime);
 319   for (size_t i = 0; i < classes_per_tick; i++) {
 320     InstanceKlass* k = ClassLoaderDataGraph::try_get_next_class();
 321     if (k != NULL) {
 322       k->methods_do(do_method);
 323     }
 324   }
 325 }
 326 
 327 // Called at the end of the safepoint
 328 void NonTieredCompPolicy::do_safepoint_work() {
 329   if(UseCounterDecay && CounterDecay::is_decay_needed()) {
 330     CounterDecay::decay();
 331   }
 332 }
 333 
 334 void NonTieredCompPolicy::reprofile(ScopeDesc* trap_scope, bool is_osr) {
 335   ScopeDesc* sd = trap_scope;
 336   MethodCounters* mcs;
 337   InvocationCounter* c;
 338   for (; !sd->is_top(); sd = sd->sender()) {
 339     mcs = sd->method()->method_counters();
 340     if (mcs != NULL) {
 341       // Reset ICs of inlined methods, since they can trigger compilations also.
 342       mcs->invocation_counter()->reset();
 343     }
 344   }
 345   mcs = sd->method()->method_counters();
 346   if (mcs != NULL) {
 347     c = mcs->invocation_counter();
 348     if (is_osr) {
 349       // It was an OSR method, so bump the count higher.
 350       c->set(c->state(), CompileThreshold);
 351     } else {
 352       c->reset();
 353     }
 354     mcs->backedge_counter()->reset();
 355   }
 356 }
 357 
 358 // This method can be called by any component of the runtime to notify the policy
 359 // that it's recommended to delay the compilation of this method.
 360 void NonTieredCompPolicy::delay_compilation(Method* method) {
 361   MethodCounters* mcs = method->method_counters();
 362   if (mcs != NULL) {
 363     mcs->invocation_counter()->decay();
 364     mcs->backedge_counter()->decay();
 365   }
 366 }
 367 
 368 void NonTieredCompPolicy::disable_compilation(Method* method) {
 369   MethodCounters* mcs = method->method_counters();
 370   if (mcs != NULL) {
 371     mcs->invocation_counter()->set_state(InvocationCounter::wait_for_nothing);
 372     mcs->backedge_counter()->set_state(InvocationCounter::wait_for_nothing);
 373   }
 374 }
 375 
 376 CompileTask* NonTieredCompPolicy::select_task(CompileQueue* compile_queue) {
 377   return select_task_helper(compile_queue);
 378 }
 379 
 380 bool NonTieredCompPolicy::is_mature(Method* method) {
 381   MethodData* mdo = method->method_data();
 382   assert(mdo != NULL, "Should be");
 383   uint current = mdo->mileage_of(method);
 384   uint initial = mdo->creation_mileage();
 385   if (current < initial)
 386     return true;  // some sort of overflow
 387   uint target;
 388   if (ProfileMaturityPercentage <= 0)
 389     target = (uint) -ProfileMaturityPercentage;  // absolute value
 390   else
 391     target = (uint)( (ProfileMaturityPercentage * CompileThreshold) / 100 );
 392   return (current >= initial + target);
 393 }
 394 
 395 nmethod* NonTieredCompPolicy::event(const methodHandle& method, const methodHandle& inlinee, int branch_bci,
 396                                     int bci, CompLevel comp_level, CompiledMethod* nm, JavaThread* thread) {
 397   assert(comp_level == CompLevel_none, "This should be only called from the interpreter");
 398   NOT_PRODUCT(trace_frequency_counter_overflow(method, branch_bci, bci));
 399   if (JvmtiExport::can_post_interpreter_events() && thread->is_interp_only_mode()) {
 400     // If certain JVMTI events (e.g. frame pop event) are requested then the
 401     // thread is forced to remain in interpreted code. This is
 402     // implemented partly by a check in the run_compiled_code
 403     // section of the interpreter whether we should skip running
 404     // compiled code, and partly by skipping OSR compiles for
 405     // interpreted-only threads.
 406     if (bci != InvocationEntryBci) {
 407       reset_counter_for_back_branch_event(method);
 408       return NULL;
 409     }
 410   }
 411   if (CompileTheWorld || ReplayCompiles) {
 412     // Don't trigger other compiles in testing mode
 413     if (bci == InvocationEntryBci) {
 414       reset_counter_for_invocation_event(method);
 415     } else {
 416       reset_counter_for_back_branch_event(method);
 417     }
 418     return NULL;
 419   }
 420 
 421   if (bci == InvocationEntryBci) {
 422     // when code cache is full, compilation gets switched off, UseCompiler
 423     // is set to false
 424     if (!method->has_compiled_code() && UseCompiler) {
 425       method_invocation_event(method, thread);
 426     } else {
 427       // Force counter overflow on method entry, even if no compilation
 428       // happened.  (The method_invocation_event call does this also.)
 429       reset_counter_for_invocation_event(method);
 430     }
 431     // compilation at an invocation overflow no longer goes and retries test for
 432     // compiled method. We always run the loser of the race as interpreted.
 433     // so return NULL
 434     return NULL;
 435   } else {
 436     // counter overflow in a loop => try to do on-stack-replacement
 437     nmethod* osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
 438     NOT_PRODUCT(trace_osr_request(method, osr_nm, bci));
 439     // when code cache is full, we should not compile any more...
 440     if (osr_nm == NULL && UseCompiler) {
 441       method_back_branch_event(method, bci, thread);
 442       osr_nm = method->lookup_osr_nmethod_for(bci, CompLevel_highest_tier, true);
 443     }
 444     if (osr_nm == NULL) {
 445       reset_counter_for_back_branch_event(method);
 446       return NULL;
 447     }
 448     return osr_nm;
 449   }
 450   return NULL;
 451 }
 452 
 453 #ifndef PRODUCT
 454 void NonTieredCompPolicy::trace_frequency_counter_overflow(const methodHandle& m, int branch_bci, int bci) {
 455   if (TraceInvocationCounterOverflow) {
 456     MethodCounters* mcs = m->method_counters();
 457     assert(mcs != NULL, "MethodCounters cannot be NULL for profiling");
 458     InvocationCounter* ic = mcs->invocation_counter();
 459     InvocationCounter* bc = mcs->backedge_counter();
 460     ResourceMark rm;
 461     if (bci == InvocationEntryBci) {
 462       tty->print("comp-policy cntr ovfl @ %d in entry of ", bci);
 463     } else {
 464       tty->print("comp-policy cntr ovfl @ %d in loop of ", bci);
 465     }
 466     m->print_value();
 467     tty->cr();
 468     ic->print();
 469     bc->print();
 470     if (ProfileInterpreter) {
 471       if (bci != InvocationEntryBci) {
 472         MethodData* mdo = m->method_data();
 473         if (mdo != NULL) {
 474           ProfileData *pd = mdo->bci_to_data(branch_bci);
 475           if (pd == NULL) {
 476             tty->print_cr("back branch count = N/A (missing ProfileData)");
 477           } else {
 478             tty->print_cr("back branch count = %d", pd->as_JumpData()->taken());
 479           }
 480         }
 481       }
 482     }
 483   }
 484 }
 485 
 486 void NonTieredCompPolicy::trace_osr_request(const methodHandle& method, nmethod* osr, int bci) {
 487   if (TraceOnStackReplacement) {
 488     ResourceMark rm;
 489     tty->print(osr != NULL ? "Reused OSR entry for " : "Requesting OSR entry for ");
 490     method->print_short_name(tty);
 491     tty->print_cr(" at bci %d", bci);
 492   }
 493 }
 494 #endif // !PRODUCT
 495 
 496 // SimpleCompPolicy - compile current method
 497 
 498 void SimpleCompPolicy::method_invocation_event(const methodHandle& m, JavaThread* thread) {
 499   const int comp_level = CompLevel_highest_tier;
 500   const int hot_count = m->invocation_count();
 501   reset_counter_for_invocation_event(m);
 502 
 503   if (is_compilation_enabled() && can_be_compiled(m, comp_level)) {
 504     CompiledMethod* nm = m->code();
 505     if (nm == NULL ) {
 506       CompileBroker::compile_method(m, InvocationEntryBci, comp_level, m, hot_count, CompileTask::Reason_InvocationCount, thread);
 507     }
 508   }
 509 }
 510 
 511 void SimpleCompPolicy::method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) {
 512   const int comp_level = CompLevel_highest_tier;
 513   const int hot_count = m->backedge_count();
 514 
 515   if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
 516     CompileBroker::compile_method(m, bci, comp_level, m, hot_count, CompileTask::Reason_BackedgeCount, thread);
 517     NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
 518   }
 519 }
 520 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
 521 
 522 #ifdef COMPILER2
 523 const char* StackWalkCompPolicy::_msg = NULL;
 524 
 525 
 526 // Consider m for compilation
 527 void StackWalkCompPolicy::method_invocation_event(const methodHandle& m, JavaThread* thread) {
 528   const int comp_level = CompLevel_highest_tier;
 529   const int hot_count = m->invocation_count();
 530   reset_counter_for_invocation_event(m);
 531 
 532   if (is_compilation_enabled() && m->code() == NULL && can_be_compiled(m, comp_level)) {
 533     ResourceMark rm(thread);
 534     frame       fr     = thread->last_frame();
 535     assert(fr.is_interpreted_frame(), "must be interpreted");
 536     assert(fr.interpreter_frame_method() == m(), "bad method");
 537 
 538     if (TraceCompilationPolicy) {
 539       tty->print("method invocation trigger: ");
 540       m->print_short_name(tty);
 541       tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)m()), m->code_size());
 542     }
 543     RegisterMap reg_map(thread, false);
 544     javaVFrame* triggerVF = thread->last_java_vframe(&reg_map);
 545     // triggerVF is the frame that triggered its counter
 546     RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m());
 547 
 548     if (first->top_method()->code() != NULL) {
 549       // called obsolete method/nmethod -- no need to recompile
 550       if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, p2i(first->top_method()->code()));
 551     } else {
 552       if (TimeCompilationPolicy) accumulated_time()->start();
 553       GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
 554       stack->push(first);
 555       RFrame* top = findTopInlinableFrame(stack);
 556       if (TimeCompilationPolicy) accumulated_time()->stop();
 557       assert(top != NULL, "findTopInlinableFrame returned null");
 558       if (TraceCompilationPolicy) top->print();
 559       CompileBroker::compile_method(top->top_method(), InvocationEntryBci, comp_level,
 560                                     m, hot_count, CompileTask::Reason_InvocationCount, thread);
 561     }
 562   }
 563 }
 564 
 565 void StackWalkCompPolicy::method_back_branch_event(const methodHandle& m, int bci, JavaThread* thread) {
 566   const int comp_level = CompLevel_highest_tier;
 567   const int hot_count = m->backedge_count();
 568 
 569   if (is_compilation_enabled() && can_be_osr_compiled(m, comp_level)) {
 570     CompileBroker::compile_method(m, bci, comp_level, m, hot_count, CompileTask::Reason_BackedgeCount, thread);
 571     NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(bci, comp_level, true));)
 572   }
 573 }
 574 
 575 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
 576   // go up the stack until finding a frame that (probably) won't be inlined
 577   // into its caller
 578   RFrame* current = stack->at(0); // current choice for stopping
 579   assert( current && !current->is_compiled(), "" );
 580   const char* msg = NULL;
 581 
 582   while (1) {
 583 
 584     // before going up the stack further, check if doing so would get us into
 585     // compiled code
 586     RFrame* next = senderOf(current, stack);
 587     if( !next )               // No next frame up the stack?
 588       break;                  // Then compile with current frame
 589 
 590     Method* m = current->top_method();
 591     Method* next_m = next->top_method();
 592 
 593     if (TraceCompilationPolicy && Verbose) {
 594       tty->print("[caller: ");
 595       next_m->print_short_name(tty);
 596       tty->print("] ");
 597     }
 598 
 599     if( !Inline ) {           // Inlining turned off
 600       msg = "Inlining turned off";
 601       break;
 602     }
 603     if (next_m->is_not_compilable()) { // Did fail to compile this before/
 604       msg = "caller not compilable";
 605       break;
 606     }
 607     if (next->num() > MaxRecompilationSearchLength) {
 608       // don't go up too high when searching for recompilees
 609       msg = "don't go up any further: > MaxRecompilationSearchLength";
 610       break;
 611     }
 612     if (next->distance() > MaxInterpretedSearchLength) {
 613       // don't go up too high when searching for recompilees
 614       msg = "don't go up any further: next > MaxInterpretedSearchLength";
 615       break;
 616     }
 617     // Compiled frame above already decided not to inline;
 618     // do not recompile him.
 619     if (next->is_compiled()) {
 620       msg = "not going up into optimized code";
 621       break;
 622     }
 623 
 624     // Interpreted frame above us was already compiled.  Do not force
 625     // a recompile, although if the frame above us runs long enough an
 626     // OSR might still happen.
 627     if( current->is_interpreted() && next_m->has_compiled_code() ) {
 628       msg = "not going up -- already compiled caller";
 629       break;
 630     }
 631 
 632     // Compute how frequent this call site is.  We have current method 'm'.
 633     // We know next method 'next_m' is interpreted.  Find the call site and
 634     // check the various invocation counts.
 635     int invcnt = 0;             // Caller counts
 636     if (ProfileInterpreter) {
 637       invcnt = next_m->interpreter_invocation_count();
 638     }
 639     int cnt = 0;                // Call site counts
 640     if (ProfileInterpreter && next_m->method_data() != NULL) {
 641       ResourceMark rm;
 642       int bci = next->top_vframe()->bci();
 643       ProfileData* data = next_m->method_data()->bci_to_data(bci);
 644       if (data != NULL && data->is_CounterData())
 645         cnt = data->as_CounterData()->count();
 646     }
 647 
 648     // Caller counts / call-site counts; i.e. is this call site
 649     // a hot call site for method next_m?
 650     int freq = (invcnt) ? cnt/invcnt : cnt;
 651 
 652     // Check size and frequency limits
 653     if ((msg = shouldInline(m, freq, cnt)) != NULL) {
 654       break;
 655     }
 656     // Check inlining negative tests
 657     if ((msg = shouldNotInline(m)) != NULL) {
 658       break;
 659     }
 660 
 661 
 662     // If the caller method is too big or something then we do not want to
 663     // compile it just to inline a method
 664     if (!can_be_compiled(next_m, CompLevel_any)) {
 665       msg = "caller cannot be compiled";
 666       break;
 667     }
 668 
 669     if( next_m->name() == vmSymbols::class_initializer_name() ) {
 670       msg = "do not compile class initializer (OSR ok)";
 671       break;
 672     }
 673 
 674     if (TraceCompilationPolicy && Verbose) {
 675       tty->print("\n\t     check caller: ");
 676       next_m->print_short_name(tty);
 677       tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", p2i((address)next_m), next_m->code_size());
 678     }
 679 
 680     current = next;
 681   }
 682 
 683   assert( !current || !current->is_compiled(), "" );
 684 
 685   if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
 686 
 687   return current;
 688 }
 689 
 690 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
 691   RFrame* sender = rf->caller();
 692   if (sender && sender->num() == stack->length()) stack->push(sender);
 693   return sender;
 694 }
 695 
 696 
 697 const char* StackWalkCompPolicy::shouldInline(const methodHandle& m, float freq, int cnt) {
 698   // Allows targeted inlining
 699   // positive filter: should send be inlined?  returns NULL (--> yes)
 700   // or rejection msg
 701   int max_size = MaxInlineSize;
 702   int cost = m->code_size();
 703 
 704   // Check for too many throws (and not too huge)
 705   if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
 706     return NULL;
 707   }
 708 
 709   // bump the max size if the call is frequent
 710   if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
 711     if (TraceFrequencyInlining) {
 712       tty->print("(Inlined frequent method)\n");
 713       m->print();
 714     }
 715     max_size = FreqInlineSize;
 716   }
 717   if (cost > max_size) {
 718     return (_msg = "too big");
 719   }
 720   return NULL;
 721 }
 722 
 723 
 724 const char* StackWalkCompPolicy::shouldNotInline(const methodHandle& m) {
 725   // negative filter: should send NOT be inlined?  returns NULL (--> inline) or rejection msg
 726   if (m->is_abstract()) return (_msg = "abstract method");
 727   // note: we allow ik->is_abstract()
 728   if (!m->method_holder()->is_initialized()) return (_msg = "method holder not initialized");
 729   if (m->is_native()) return (_msg = "native method");
 730   CompiledMethod* m_code = m->code();
 731   if (m_code != NULL && m_code->code_size() > InlineSmallCode)
 732     return (_msg = "already compiled into a big method");
 733 
 734   // use frequency-based objections only for non-trivial methods
 735   if (m->code_size() <= MaxTrivialSize) return NULL;
 736   if (UseInterpreter) {     // don't use counts with -Xcomp
 737     if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
 738     if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
 739   }
 740   if (Method::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
 741 
 742   return NULL;
 743 }
 744 
 745 
 746 
 747 #endif // COMPILER2