1 /*
   2  * Copyright 1998-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_bytecodeInfo.cpp.incl"
  27 
  28 //=============================================================================
  29 //------------------------------InlineTree-------------------------------------
  30 InlineTree::InlineTree( Compile* c, const InlineTree *caller_tree, ciMethod* callee, JVMState* caller_jvms, int caller_bci, float site_invoke_ratio )
  31 : C(c), _caller_jvms(caller_jvms),
  32   _caller_tree((InlineTree*)caller_tree),
  33   _method(callee), _site_invoke_ratio(site_invoke_ratio),
  34   _count_inline_bcs(method()->code_size()) {
  35   NOT_PRODUCT(_count_inlines = 0;)
  36   if (_caller_jvms != NULL) {
  37     // Keep a private copy of the caller_jvms:
  38     _caller_jvms = new (C) JVMState(caller_jvms->method(), caller_tree->caller_jvms());
  39     _caller_jvms->set_bci(caller_jvms->bci());

  40   }
  41   assert(_caller_jvms->same_calls_as(caller_jvms), "consistent JVMS");
  42   assert((caller_tree == NULL ? 0 : caller_tree->inline_depth() + 1) == inline_depth(), "correct (redundant) depth parameter");
  43   assert(caller_bci == this->caller_bci(), "correct (redundant) bci parameter");
  44   if (UseOldInlining) {
  45     // Update hierarchical counts, count_inline_bcs() and count_inlines()
  46     InlineTree *caller = (InlineTree *)caller_tree;
  47     for( ; caller != NULL; caller = ((InlineTree *)(caller->caller_tree())) ) {
  48       caller->_count_inline_bcs += count_inline_bcs();
  49       NOT_PRODUCT(caller->_count_inlines++;)
  50     }
  51   }
  52 }
  53 
  54 InlineTree::InlineTree(Compile* c, ciMethod* callee_method, JVMState* caller_jvms, float site_invoke_ratio)
  55 : C(c), _caller_jvms(caller_jvms), _caller_tree(NULL),
  56   _method(callee_method), _site_invoke_ratio(site_invoke_ratio),
  57   _count_inline_bcs(method()->code_size()) {
  58   NOT_PRODUCT(_count_inlines = 0;)
  59   assert(!UseOldInlining, "do not use for old stuff");
  60 }
  61 
  62 
  63 
  64 static void print_indent(int depth) {
  65   tty->print("      ");
  66   for (int i = depth; i != 0; --i) tty->print("  ");
  67 }
  68 
  69 static bool is_init_with_ea(ciMethod* callee_method,
  70                             ciMethod* caller_method, Compile* C) {
  71   // True when EA is ON and a java constructor is called or
  72   // a super constructor is called from an inlined java constructor.
  73   return C->do_escape_analysis() && EliminateAllocations &&
  74          ( callee_method->is_initializer() ||
  75            (caller_method->is_initializer() &&
  76             caller_method != C->method() &&
  77             caller_method->holder()->is_subclass_of(callee_method->holder()))
  78          );
  79 }
  80 
  81 // positive filter: should send be inlined?  returns NULL, if yes, or rejection msg
  82 const char* InlineTree::shouldInline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) const {
  83   // Allows targeted inlining
  84   if(callee_method->should_inline()) {
  85     *wci_result = *(WarmCallInfo::always_hot());
  86     if (PrintInlining && Verbose) {
  87       print_indent(inline_depth());
  88       tty->print_cr("Inlined method is hot: ");
  89     }
  90     return NULL;
  91   }
  92 
  93   // positive filter: should send be inlined?  returns NULL (--> yes)
  94   // or rejection msg
  95   int max_size = C->max_inline_size();
  96   int size     = callee_method->code_size();
  97 
  98   // Check for too many throws (and not too huge)
  99   if(callee_method->interpreter_throwout_count() > InlineThrowCount &&
 100      size < InlineThrowMaxSize ) {
 101     wci_result->set_profit(wci_result->profit() * 100);
 102     if (PrintInlining && Verbose) {
 103       print_indent(inline_depth());
 104       tty->print_cr("Inlined method with many throws (throws=%d):", callee_method->interpreter_throwout_count());
 105     }
 106     return NULL;
 107   }
 108 
 109   if (!UseOldInlining) {
 110     return NULL;  // size and frequency are represented in a new way
 111   }
 112 
 113   int call_site_count  = method()->scale_count(profile.count());
 114   int invoke_count     = method()->interpreter_invocation_count();
 115   assert( invoke_count != 0, "Require invokation count greater than zero");
 116   int freq = call_site_count/invoke_count;
 117 
 118   // bump the max size if the call is frequent
 119   if ((freq >= InlineFrequencyRatio) ||
 120       (call_site_count >= InlineFrequencyCount) ||
 121       is_init_with_ea(callee_method, caller_method, C)) {
 122 
 123     max_size = C->freq_inline_size();
 124     if (size <= max_size && TraceFrequencyInlining) {
 125       print_indent(inline_depth());
 126       tty->print_cr("Inlined frequent method (freq=%d count=%d):", freq, call_site_count);
 127       print_indent(inline_depth());
 128       callee_method->print();
 129       tty->cr();
 130     }
 131   } else {
 132     // Not hot.  Check for medium-sized pre-existing nmethod at cold sites.
 133     if (callee_method->has_compiled_code() &&
 134         callee_method->instructions_size() > InlineSmallCode/4)
 135       return "already compiled into a medium method";
 136   }
 137   if (size > max_size) {
 138     if (max_size > C->max_inline_size())
 139       return "hot method too big";
 140     return "too big";
 141   }
 142   return NULL;
 143 }
 144 
 145 
 146 // negative filter: should send NOT be inlined?  returns NULL, ok to inline, or rejection msg
 147 const char* InlineTree::shouldNotInline(ciMethod *callee_method, ciMethod* caller_method, WarmCallInfo* wci_result) const {
 148   // negative filter: should send NOT be inlined?  returns NULL (--> inline) or rejection msg
 149   if (!UseOldInlining) {
 150     const char* fail = NULL;
 151     if (callee_method->is_abstract())               fail = "abstract method";
 152     // note: we allow ik->is_abstract()
 153     if (!callee_method->holder()->is_initialized()) fail = "method holder not initialized";
 154     if (callee_method->is_native())                 fail = "native method";
 155 
 156     if (fail) {
 157       *wci_result = *(WarmCallInfo::always_cold());
 158       return fail;
 159     }
 160 
 161     if (callee_method->has_unloaded_classes_in_signature()) {
 162       wci_result->set_profit(wci_result->profit() * 0.1);
 163     }
 164 
 165     // don't inline exception code unless the top method belongs to an
 166     // exception class
 167     if (callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
 168       ciMethod* top_method = caller_jvms() ? caller_jvms()->of_depth(1)->method() : method();
 169       if (!top_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
 170         wci_result->set_profit(wci_result->profit() * 0.1);
 171       }
 172     }
 173 
 174     if (callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode) {
 175       wci_result->set_profit(wci_result->profit() * 0.1);
 176       // %%% adjust wci_result->size()?
 177     }
 178 
 179     return NULL;
 180   }
 181 
 182   // First check all inlining restrictions which are required for correctness
 183   if (callee_method->is_abstract())               return "abstract method";
 184   // note: we allow ik->is_abstract()
 185   if (!callee_method->holder()->is_initialized()) return "method holder not initialized";
 186   if (callee_method->is_native())                 return "native method";
 187   if (callee_method->has_unloaded_classes_in_signature()) return "unloaded signature classes";
 188 
 189   if (callee_method->should_inline()) {
 190     // ignore heuristic controls on inlining
 191     return NULL;
 192   }
 193 
 194   // Now perform checks which are heuristic
 195 
 196   if( callee_method->has_compiled_code() && callee_method->instructions_size() > InlineSmallCode )
 197     return "already compiled into a big method";
 198 
 199   // don't inline exception code unless the top method belongs to an
 200   // exception class
 201   if (caller_tree() != NULL &&
 202       callee_method->holder()->is_subclass_of(C->env()->Throwable_klass())) {
 203     const InlineTree *top = this;
 204     while (top->caller_tree() != NULL) top = top->caller_tree();
 205     ciInstanceKlass* k = top->method()->holder();
 206     if (!k->is_subclass_of(C->env()->Throwable_klass()))
 207       return "exception method";
 208   }
 209 
 210   // use frequency-based objections only for non-trivial methods
 211   if (callee_method->code_size() <= MaxTrivialSize) return NULL;
 212 
 213   // don't use counts with -Xcomp or CTW
 214   if (UseInterpreter && !CompileTheWorld) {
 215 
 216     if (!callee_method->has_compiled_code() &&
 217         !callee_method->was_executed_more_than(0)) {
 218       return "never executed";
 219     }
 220 
 221     if (is_init_with_ea(callee_method, caller_method, C)) {
 222 
 223       // Escape Analysis: inline all executed constructors
 224 
 225     } else if (!callee_method->was_executed_more_than(MIN2(MinInliningThreshold,
 226                                                            CompileThreshold >> 1))) {
 227       return "executed < MinInliningThreshold times";
 228     }
 229   }
 230 
 231   if (callee_method->should_not_inline()) {
 232     return "disallowed by CompilerOracle";
 233   }
 234 
 235   if (UseStringCache) {
 236     // Do not inline StringCache::profile() method used only at the beginning.
 237     if (callee_method->name() == ciSymbol::profile_name() &&
 238         callee_method->holder()->name() == ciSymbol::java_lang_StringCache()) {
 239       return "profiling method";
 240     }
 241   }
 242 
 243   return NULL;
 244 }
 245 
 246 //-----------------------------try_to_inline-----------------------------------
 247 // return NULL if ok, reason for not inlining otherwise
 248 // Relocated from "InliningClosure::try_to_inline"
 249 const char* InlineTree::try_to_inline(ciMethod* callee_method, ciMethod* caller_method, int caller_bci, ciCallProfile& profile, WarmCallInfo* wci_result) {
 250 
 251   // Old algorithm had funny accumulating BC-size counters
 252   if (UseOldInlining && ClipInlining
 253       && (int)count_inline_bcs() >= DesiredMethodLimit) {
 254     return "size > DesiredMethodLimit";
 255   }
 256 
 257   const char *msg = NULL;
 258   if ((msg = shouldInline(callee_method, caller_method, caller_bci,
 259                           profile, wci_result)) != NULL) {
 260     return msg;
 261   }
 262   if ((msg = shouldNotInline(callee_method, caller_method,
 263                              wci_result)) != NULL) {
 264     return msg;
 265   }
 266 
 267   bool is_accessor = InlineAccessors && callee_method->is_accessor();
 268 
 269   // suppress a few checks for accessors and trivial methods
 270   if (!is_accessor && callee_method->code_size() > MaxTrivialSize) {
 271 
 272     // don't inline into giant methods
 273     if (C->unique() > (uint)NodeCountInliningCutoff) {
 274       return "NodeCountInliningCutoff";
 275     }
 276 
 277     if ((!UseInterpreter || CompileTheWorld) &&
 278         is_init_with_ea(callee_method, caller_method, C)) {
 279 
 280       // Escape Analysis stress testing when running Xcomp or CTW:
 281       // inline constructors even if they are not reached.
 282 
 283     } else if (profile.count() == 0) {
 284       // don't inline unreached call sites
 285       return "call site not reached";
 286     }
 287   }
 288 
 289   if (!C->do_inlining() && InlineAccessors && !is_accessor) {
 290     return "not an accessor";
 291   }
 292   if( inline_depth() > MaxInlineLevel ) {
 293     return "inlining too deep";
 294   }
 295   if( method() == callee_method &&
 296       inline_depth() > MaxRecursiveInlineLevel ) {
 297     return "recursively inlining too deep";
 298   }
 299 
 300   int size = callee_method->code_size();
 301 
 302   if (UseOldInlining && ClipInlining
 303       && (int)count_inline_bcs() + size >= DesiredMethodLimit) {
 304     return "size > DesiredMethodLimit";
 305   }
 306 
 307   // ok, inline this method
 308   return NULL;
 309 }
 310 
 311 //------------------------------pass_initial_checks----------------------------
 312 bool pass_initial_checks(ciMethod* caller_method, int caller_bci, ciMethod* callee_method) {
 313   ciInstanceKlass *callee_holder = callee_method ? callee_method->holder() : NULL;
 314   // Check if a callee_method was suggested
 315   if( callee_method == NULL )            return false;
 316   // Check if klass of callee_method is loaded
 317   if( !callee_holder->is_loaded() )      return false;
 318   if( !callee_holder->is_initialized() ) return false;
 319   if( !UseInterpreter || CompileTheWorld /* running Xcomp or CTW */ ) {
 320     // Checks that constant pool's call site has been visited
 321     // stricter than callee_holder->is_initialized()
 322     ciBytecodeStream iter(caller_method);
 323     iter.force_bci(caller_bci);
 324     int index = iter.get_index_int();
 325     if( !caller_method->is_klass_loaded(index, true) ) {
 326       return false;
 327     }
 328     // Try to do constant pool resolution if running Xcomp
 329     Bytecodes::Code call_bc = iter.cur_bc();
 330     if( !caller_method->check_call(index, call_bc == Bytecodes::_invokestatic) ) {
 331       return false;
 332     }
 333   }
 334   // We will attempt to see if a class/field/etc got properly loaded.  If it
 335   // did not, it may attempt to throw an exception during our probing.  Catch
 336   // and ignore such exceptions and do not attempt to compile the method.
 337   if( callee_method->should_exclude() )  return false;
 338 
 339   return true;
 340 }
 341 
 342 #ifndef PRODUCT
 343 //------------------------------print_inlining---------------------------------
 344 // Really, the failure_msg can be a success message also.
 345 void InlineTree::print_inlining(ciMethod *callee_method, int caller_bci, const char *failure_msg) const {
 346   print_indent(inline_depth());
 347   tty->print("@ %d  ", caller_bci);
 348   if( callee_method ) callee_method->print_short_name();
 349   else                tty->print(" callee not monotonic or profiled");
 350   tty->print("  %s", (failure_msg ? failure_msg : "inline"));
 351   if( Verbose && callee_method ) {
 352     const InlineTree *top = this;
 353     while( top->caller_tree() != NULL ) { top = top->caller_tree(); }
 354     tty->print("  bcs: %d+%d  invoked: %d", top->count_inline_bcs(), callee_method->code_size(), callee_method->interpreter_invocation_count());
 355   }
 356   tty->cr();
 357 }
 358 #endif
 359 
 360 //------------------------------ok_to_inline-----------------------------------
 361 WarmCallInfo* InlineTree::ok_to_inline(ciMethod* callee_method, JVMState* jvms, ciCallProfile& profile, WarmCallInfo* initial_wci) {
 362   assert(callee_method != NULL, "caller checks for optimized virtual!");
 363 #ifdef ASSERT
 364   // Make sure the incoming jvms has the same information content as me.
 365   // This means that we can eventually make this whole class AllStatic.
 366   if (jvms->caller() == NULL) {
 367     assert(_caller_jvms == NULL, "redundant instance state");
 368   } else {
 369     assert(_caller_jvms->same_calls_as(jvms->caller()), "redundant instance state");
 370   }
 371   assert(_method == jvms->method(), "redundant instance state");
 372 #endif
 373   const char *failure_msg   = NULL;
 374   int         caller_bci    = jvms->bci();
 375   ciMethod   *caller_method = jvms->method();
 376 
 377   if( !pass_initial_checks(caller_method, caller_bci, callee_method)) {
 378     if( PrintInlining ) {
 379       failure_msg = "failed_initial_checks";
 380       print_inlining( callee_method, caller_bci, failure_msg);
 381     }
 382     return NULL;
 383   }
 384 
 385   // Check if inlining policy says no.
 386   WarmCallInfo wci = *(initial_wci);
 387   failure_msg = try_to_inline(callee_method, caller_method, caller_bci, profile, &wci);
 388   if (failure_msg != NULL && C->log() != NULL) {
 389     C->log()->begin_elem("inline_fail reason='");
 390     C->log()->text("%s", failure_msg);
 391     C->log()->end_elem("'");
 392   }
 393 
 394 #ifndef PRODUCT
 395   if (UseOldInlining && InlineWarmCalls
 396       && (PrintOpto || PrintOptoInlining || PrintInlining)) {
 397     bool cold = wci.is_cold();
 398     bool hot  = !cold && wci.is_hot();
 399     bool old_cold = (failure_msg != NULL);
 400     if (old_cold != cold || (Verbose || WizardMode)) {
 401       tty->print("   OldInlining= %4s : %s\n           WCI=",
 402                  old_cold ? "cold" : "hot", failure_msg ? failure_msg : "OK");
 403       wci.print();
 404     }
 405   }
 406 #endif
 407   if (UseOldInlining) {
 408     if (failure_msg == NULL)
 409       wci = *(WarmCallInfo::always_hot());
 410     else
 411       wci = *(WarmCallInfo::always_cold());
 412   }
 413   if (!InlineWarmCalls) {
 414     if (!wci.is_cold() && !wci.is_hot()) {
 415       // Do not inline the warm calls.
 416       wci = *(WarmCallInfo::always_cold());
 417     }
 418   }
 419 
 420   if (!wci.is_cold()) {
 421     // In -UseOldInlining, the failure_msg may also be a success message.
 422     if (failure_msg == NULL)  failure_msg = "inline (hot)";
 423 
 424     // Inline!
 425     if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
 426     if (UseOldInlining)
 427       build_inline_tree_for_callee(callee_method, jvms, caller_bci);
 428     if (InlineWarmCalls && !wci.is_hot())
 429       return new (C) WarmCallInfo(wci);  // copy to heap
 430     return WarmCallInfo::always_hot();
 431   }
 432 
 433   // Do not inline
 434   if (failure_msg == NULL)  failure_msg = "too cold to inline";
 435   if( PrintInlining ) print_inlining( callee_method, caller_bci, failure_msg);
 436   return NULL;
 437 }
 438 
 439 //------------------------------compute_callee_frequency-----------------------
 440 float InlineTree::compute_callee_frequency( int caller_bci ) const {
 441   int count  = method()->interpreter_call_site_count(caller_bci);
 442   int invcnt = method()->interpreter_invocation_count();
 443   float freq = (float)count/(float)invcnt;
 444   // Call-site count / interpreter invocation count, scaled recursively.
 445   // Always between 0.0 and 1.0.  Represents the percentage of the method's
 446   // total execution time used at this call site.
 447 
 448   return freq;
 449 }
 450 
 451 //------------------------------build_inline_tree_for_callee-------------------
 452 InlineTree *InlineTree::build_inline_tree_for_callee( ciMethod* callee_method, JVMState* caller_jvms, int caller_bci) {
 453   float recur_frequency = _site_invoke_ratio * compute_callee_frequency(caller_bci);
 454   // Attempt inlining.
 455   InlineTree* old_ilt = callee_at(caller_bci, callee_method);
 456   if (old_ilt != NULL) {
 457     return old_ilt;
 458   }
 459   InlineTree *ilt = new InlineTree( C, this, callee_method, caller_jvms, caller_bci, recur_frequency );
 460   _subtrees.append( ilt );
 461 
 462   NOT_PRODUCT( _count_inlines += 1; )
 463 
 464   return ilt;
 465 }
 466 
 467 
 468 //---------------------------------------callee_at-----------------------------
 469 InlineTree *InlineTree::callee_at(int bci, ciMethod* callee) const {
 470   for (int i = 0; i < _subtrees.length(); i++) {
 471     InlineTree* sub = _subtrees.at(i);
 472     if (sub->caller_bci() == bci && callee == sub->method()) {
 473       return sub;
 474     }
 475   }
 476   return NULL;
 477 }
 478 
 479 
 480 //------------------------------build_inline_tree_root-------------------------
 481 InlineTree *InlineTree::build_inline_tree_root() {
 482   Compile* C = Compile::current();
 483 
 484   // Root of inline tree
 485   InlineTree *ilt = new InlineTree(C, NULL, C->method(), NULL, -1, 1.0F);
 486 
 487   return ilt;
 488 }
 489 
 490 
 491 //-------------------------find_subtree_from_root-----------------------------
 492 // Given a jvms, which determines a call chain from the root method,
 493 // find the corresponding inline tree.
 494 // Note: This method will be removed or replaced as InlineTree goes away.
 495 InlineTree* InlineTree::find_subtree_from_root(InlineTree* root, JVMState* jvms, ciMethod* callee, bool create_if_not_found) {
 496   InlineTree* iltp = root;
 497   uint depth = jvms && jvms->has_method() ? jvms->depth() : 0;
 498   for (uint d = 1; d <= depth; d++) {
 499     JVMState* jvmsp  = jvms->of_depth(d);
 500     // Select the corresponding subtree for this bci.
 501     assert(jvmsp->method() == iltp->method(), "tree still in sync");
 502     ciMethod* d_callee = (d == depth) ? callee : jvms->of_depth(d+1)->method();
 503     InlineTree* sub = iltp->callee_at(jvmsp->bci(), d_callee);
 504     if (!sub) {
 505       if (create_if_not_found && d == depth) {
 506         return iltp->build_inline_tree_for_callee(d_callee, jvmsp, jvmsp->bci());
 507       }
 508       assert(sub != NULL, "should be a sub-ilt here");
 509       return NULL;
 510     }
 511     iltp = sub;
 512   }
 513   return iltp;
 514 }
--- EOF ---