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