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