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