1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)doCall.cpp   1.207 07/07/19 19:08:29 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/_doCall.cpp.incl"
  30 
  31 #ifndef PRODUCT
  32 void trace_type_profile(ciMethod *method, int depth, int bci, ciMethod *prof_method, ciKlass *prof_klass, int site_count, int receiver_count) {
  33   if (TraceTypeProfile || PrintInlining || PrintOptoInlining) {
  34     tty->print("   ");
  35     for( int i = 0; i < depth; i++ ) tty->print("  ");
  36     if (!PrintOpto) {
  37       method->print_short_name();
  38       tty->print(" ->");
  39     }
  40     tty->print(" @ %d  ", bci);
  41     prof_method->print_short_name();
  42     tty->print("  >>TypeProfile (%d/%d counts) = ", receiver_count, site_count);
  43     prof_klass->name()->print_symbol();
  44     tty->print_cr(" (%d bytes)", prof_method->code_size());
  45   }
  46 }
  47 #endif
  48 
  49 CallGenerator* Compile::call_generator(ciMethod* call_method, int vtable_index, bool call_is_virtual, JVMState* jvms, bool allow_inline, float prof_factor) {
  50   CallGenerator* cg;
  51 
  52   // Dtrace currently doesn't work unless all calls are vanilla
  53   if (DTraceMethodProbes) {
  54     allow_inline = false;
  55   }
  56 
  57   // Note: When we get profiling during stage-1 compiles, we want to pull
  58   // from more specific profile data which pertains to this inlining.
  59   // Right now, ignore the information in jvms->caller(), and do method[bci].
  60   ciCallProfile profile = jvms->method()->call_profile_at_bci(jvms->bci());
  61 
  62   // See how many times this site has been invoked.
  63   int site_count = profile.count();
  64   int receiver_count = -1;
  65   if (call_is_virtual && UseTypeProfile && profile.has_receiver(0)) {
  66     // Receivers in the profile structure are ordered by call counts
  67     // so that the most called (major) receiver is profile.receiver(0).
  68     receiver_count = profile.receiver_count(0);
  69   }
  70 
  71   CompileLog* log = this->log();
  72   if (log != NULL) {
  73     int rid = (receiver_count >= 0)? log->identify(profile.receiver(0)): -1;
  74     int r2id = (profile.morphism() == 2)? log->identify(profile.receiver(1)):-1;
  75     log->begin_elem("call method='%d' count='%d' prof_factor='%g'",
  76                     log->identify(call_method), site_count, prof_factor);
  77     if (call_is_virtual)  log->print(" virtual='1'");
  78     if (allow_inline)     log->print(" inline='1'");
  79     if (receiver_count >= 0) {
  80       log->print(" receiver='%d' receiver_count='%d'", rid, receiver_count);
  81       if (profile.has_receiver(1)) {
  82         log->print(" receiver2='%d' receiver2_count='%d'", r2id, profile.receiver_count(1));
  83       }
  84     }
  85     log->end_elem();
  86   }
  87 
  88   // Special case the handling of certain common, profitable library
  89   // methods.  If these methods are replaced with specialized code,
  90   // then we return it as the inlined version of the call.
  91   // We do this before the strict f.p. check below because the
  92   // intrinsics handle strict f.p. correctly.
  93   if (allow_inline) {
  94     cg = find_intrinsic(call_method, call_is_virtual);
  95     if (cg != NULL)  return cg;
  96   }
  97 
  98   // Do not inline strict fp into non-strict code, or the reverse
  99   bool caller_method_is_strict = jvms->method()->is_strict();
 100   if( caller_method_is_strict ^ call_method->is_strict() ) {
 101     allow_inline = false;
 102   }
 103 
 104   // Attempt to inline...
 105   if (allow_inline) {
 106     // The profile data is only partly attributable to this caller,
 107     // scale back the call site information.
 108     float past_uses = jvms->method()->scale_count(site_count, prof_factor);
 109     // This is the number of times we expect the call code to be used.
 110     float expected_uses = past_uses;
 111 
 112     // Try inlining a bytecoded method:
 113     if (!call_is_virtual) {
 114       InlineTree* ilt;
 115       if (UseOldInlining) {
 116         ilt = InlineTree::find_subtree_from_root(this->ilt(), jvms->caller(), jvms->method());
 117       } else {
 118         // Make a disembodied, stateless ILT.
 119         // TO DO:  When UseOldInlining is removed, copy the ILT code elsewhere.
 120         float site_invoke_ratio = prof_factor;
 121         // Note:  ilt is for the root of this parse, not the present call site.
 122         ilt = new InlineTree(this, jvms->method(), jvms->caller(), site_invoke_ratio);
 123       }
 124       WarmCallInfo scratch_ci;
 125       if (!UseOldInlining)
 126         scratch_ci.init(jvms, call_method, profile, prof_factor);
 127       WarmCallInfo* ci = ilt->ok_to_inline(call_method, jvms, profile, &scratch_ci);
 128       assert(ci != &scratch_ci, "do not let this pointer escape");
 129       bool allow_inline   = (ci != NULL && !ci->is_cold());
 130       bool require_inline = (allow_inline && ci->is_hot());
 131 
 132       if (allow_inline) {
 133         CallGenerator* cg = CallGenerator::for_inline(call_method, expected_uses);
 134         if (cg == NULL) {
 135           // Fall through.
 136         } else if (require_inline || !InlineWarmCalls) {
 137           return cg;
 138         } else {
 139           CallGenerator* cold_cg = call_generator(call_method, vtable_index, call_is_virtual, jvms, false, prof_factor);
 140           return CallGenerator::for_warm_call(ci, cold_cg, cg);
 141         }
 142       }
 143     }
 144 
 145     // Try using the type profile.
 146     if (call_is_virtual && site_count > 0 && receiver_count > 0) {
 147       // The major receiver's count >= TypeProfileMajorReceiverPercent of site_count.
 148       bool have_major_receiver = (100.*profile.receiver_prob(0) >= (float)TypeProfileMajorReceiverPercent);
 149       ciMethod* receiver_method = NULL;
 150       if (have_major_receiver || profile.morphism() == 1 || 
 151           (profile.morphism() == 2 && UseBimorphicInlining)) {
 152         // receiver_method = profile.method();
 153         // Profiles do not suggest methods now.  Look it up in the major receiver.
 154         receiver_method = call_method->resolve_invoke(jvms->method()->holder(),
 155                                                       profile.receiver(0));
 156       }
 157       if (receiver_method != NULL) {
 158         // The single majority receiver sufficiently outweighs the minority.
 159         CallGenerator* hit_cg = this->call_generator(receiver_method, 
 160               vtable_index, !call_is_virtual, jvms, allow_inline, prof_factor);
 161         if (hit_cg != NULL) {
 162           // Look up second receiver.
 163           CallGenerator* next_hit_cg = NULL;
 164           ciMethod* next_receiver_method = NULL;
 165           if (profile.morphism() == 2 && UseBimorphicInlining) { 
 166             next_receiver_method = call_method->resolve_invoke(jvms->method()->holder(),
 167                                                                profile.receiver(1));
 168             if (next_receiver_method != NULL) {
 169               next_hit_cg = this->call_generator(next_receiver_method, 
 170                                   vtable_index, !call_is_virtual, jvms, 
 171                                   allow_inline, prof_factor);
 172               if (next_hit_cg != NULL && !next_hit_cg->is_inline() && 
 173                   have_major_receiver && UseOnlyInlinedBimorphic) {
 174                   // Skip if we can't inline second receiver's method
 175                   next_hit_cg = NULL;
 176               }
 177             }
 178           }
 179           CallGenerator* miss_cg;
 180           if (( profile.morphism() == 1 || 
 181                (profile.morphism() == 2 && next_hit_cg != NULL) ) && 
 182 
 183               !too_many_traps(Deoptimization::Reason_class_check)
 184 
 185               // Check only total number of traps per method to allow
 186               // the transition from monomorphic to bimorphic case between 
 187               // compilations without falling into virtual call.
 188               // A monomorphic case may have the class_check trap flag is set 
 189               // due to the time gap between the uncommon trap processing 
 190               // when flags are set in MDO and the call site bytecode execution
 191               // in Interpreter when MDO counters are updated.
 192               // There was also class_check trap in monomorphic case due to 
 193               // the bug 6225440.
 194 
 195              ) {
 196             // Generate uncommon trap for class check failure path
 197             // in case of monomorphic or bimorphic virtual call site.
 198             miss_cg = CallGenerator::for_uncommon_trap(call_method,
 199                         Deoptimization::Reason_class_check,
 200                         Deoptimization::Action_maybe_recompile);
 201           } else {
 202             // Generate virtual call for class check failure path
 203             // in case of polymorphic virtual call site.
 204             miss_cg = CallGenerator::for_virtual_call(call_method, vtable_index);
 205           }
 206           if (miss_cg != NULL) {
 207             if (next_hit_cg != NULL) {
 208               NOT_PRODUCT(trace_type_profile(jvms->method(), jvms->depth(), jvms->bci(), next_receiver_method, profile.receiver(1), site_count, profile.receiver_count(1)));
 209               // We don't need to record dependency on a receiver here and below.
 210               // Whenever we inline, the dependency is added by Parse::Parse().
 211               miss_cg = CallGenerator::for_predicted_call(profile.receiver(1), miss_cg, next_hit_cg, PROB_MAX);
 212             }
 213             if (miss_cg != NULL) {
 214               NOT_PRODUCT(trace_type_profile(jvms->method(), jvms->depth(), jvms->bci(), receiver_method, profile.receiver(0), site_count, receiver_count));
 215               cg = CallGenerator::for_predicted_call(profile.receiver(0), miss_cg, hit_cg, profile.receiver_prob(0));
 216               if (cg != NULL)  return cg;
 217             }
 218           }
 219         }
 220       }
 221     }
 222   }
 223 
 224   // There was no special inlining tactic, or it bailed out.
 225   // Use a more generic tactic, like a simple call.
 226   if (call_is_virtual) {
 227     return CallGenerator::for_virtual_call(call_method, vtable_index);
 228   } else {
 229     // Class Hierarchy Analysis or Type Profile reveals a unique target,
 230     // or it is a static or special call.
 231     return CallGenerator::for_direct_call(call_method);
 232   }
 233 }
 234 
 235 
 236 // uncommon-trap call-sites where callee is unloaded, uninitialized or will not link
 237 bool Parse::can_not_compile_call_site(ciMethod *dest_method, ciInstanceKlass* klass) {
 238   // Additional inputs to consider...
 239   // bc      = bc()
 240   // caller  = method()
 241   // iter().get_method_holder_index()
 242   assert( dest_method->is_loaded(), "ciTypeFlow should not let us get here" );
 243   // Interface classes can be loaded & linked and never get around to
 244   // being initialized.  Uncommon-trap for not-initialized static or
 245   // v-calls.  Let interface calls happen.
 246   ciInstanceKlass* holder_klass  = dest_method->holder();
 247   if (!holder_klass->is_initialized() &&
 248       !holder_klass->is_interface()) {
 249     uncommon_trap(Deoptimization::Reason_uninitialized,
 250                   Deoptimization::Action_reinterpret,
 251                   holder_klass);
 252     return true;
 253   }
 254 
 255   assert(dest_method->will_link(method()->holder(), klass, bc()), "dest_method: typeflow responsibility");
 256   return false;
 257 }
 258 
 259 
 260 //------------------------------do_call----------------------------------------
 261 // Handle your basic call.  Inline if we can & want to, else just setup call.
 262 void Parse::do_call() {
 263   // It's likely we are going to add debug info soon.
 264   // Also, if we inline a guy who eventually needs debug info for this JVMS,
 265   // our contribution to it is cleaned up right here.
 266   kill_dead_locals();
 267 
 268   // Set frequently used booleans
 269   bool is_virtual = bc() == Bytecodes::_invokevirtual;
 270   bool is_virtual_or_interface = is_virtual || bc() == Bytecodes::_invokeinterface;
 271   bool has_receiver = is_virtual_or_interface || bc() == Bytecodes::_invokespecial;
 272 
 273   // Find target being called
 274   bool             will_link;
 275   ciMethod*        dest_method   = iter().get_method(will_link);
 276   ciInstanceKlass* holder_klass  = dest_method->holder();
 277   ciKlass* holder = iter().get_declared_method_holder();
 278   ciInstanceKlass* klass = ciEnv::get_instance_klass_for_declared_method_holder(holder);
 279 
 280   int   nargs    = dest_method->arg_size();
 281 
 282   // uncommon-trap when callee is unloaded, uninitialized or will not link
 283   // bailout when too many arguments for register representation
 284   if (!will_link || can_not_compile_call_site(dest_method, klass)) {
 285 #ifndef PRODUCT
 286     if (PrintOpto && (Verbose || WizardMode)) {
 287       method()->print_name(); tty->print_cr(" can not compile call at bci %d to:", bci());
 288       dest_method->print_name(); tty->cr();
 289     }
 290 #endif
 291     return;
 292   }
 293   assert(holder_klass->is_loaded(), "");
 294   assert(dest_method->is_static() == !has_receiver, "must match bc");
 295   // Note: this takes into account invokeinterface of methods declared in java/lang/Object,
 296   // which should be invokevirtuals but according to the VM spec may be invokeinterfaces
 297   assert(holder_klass->is_interface() || holder_klass->super() == NULL || (bc() != Bytecodes::_invokeinterface), "must match bc");
 298   // Note:  In the absence of miranda methods, an abstract class K can perform
 299   // an invokevirtual directly on an interface method I.m if K implements I.
 300 
 301   // ---------------------
 302   // Does Class Hierarchy Analysis reveal only a single target of a v-call?
 303   // Then we may inline or make a static call, but become dependent on there being only 1 target.
 304   // Does the call-site type profile reveal only one receiver?
 305   // Then we may introduce a run-time check and inline on the path where it succeeds.
 306   // The other path may uncommon_trap, check for another receiver, or do a v-call.
 307 
 308   // Choose call strategy.
 309   bool call_is_virtual = is_virtual_or_interface;
 310   int vtable_index = methodOopDesc::invalid_vtable_index;
 311   ciMethod* call_method = dest_method;
 312 
 313   // Try to get the most accurate receiver type
 314   if (is_virtual_or_interface) {
 315     Node*             receiver_node = stack(sp() - nargs);
 316     const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr();
 317     ciMethod* optimized_virtual_method = optimize_inlining(method(), bci(), klass, dest_method, receiver_type);
 318 
 319     // Have the call been sufficiently improved such that it is no longer a virtual?
 320     if (optimized_virtual_method != NULL) {
 321       call_method     = optimized_virtual_method;
 322       call_is_virtual = false;
 323     } else if (!UseInlineCaches && is_virtual && call_method->is_loaded()) {
 324       // We can make a vtable call at this site
 325       vtable_index = call_method->resolve_vtable_index(method()->holder(), klass);
 326     }
 327   }
 328 
 329   // Note:  It's OK to try to inline a virtual call.
 330   // The call generator will not attempt to inline a polymorphic call
 331   // unless it knows how to optimize the receiver dispatch.
 332   bool try_inline = (C->do_inlining() || InlineAccessors);
 333 
 334   // ---------------------
 335   inc_sp(- nargs);              // Temporarily pop args for JVM state of call
 336   JVMState* jvms = sync_jvms();
 337 
 338   // ---------------------
 339   // Decide call tactic.
 340   // This call checks with CHA, the interpreter profile, intrinsics table, etc.
 341   // It decides whether inlining is desirable or not.
 342   CallGenerator* cg = C->call_generator(call_method, vtable_index, call_is_virtual, jvms, try_inline, prof_factor());
 343 
 344   // ---------------------
 345   // Round double arguments before call
 346   round_double_arguments(dest_method);
 347 
 348 #ifndef PRODUCT
 349   // bump global counters for calls
 350   count_compiled_calls(false/*at_method_entry*/, cg->is_inline());
 351 
 352   // Record first part of parsing work for this call
 353   parse_histogram()->record_change();
 354 #endif // not PRODUCT
 355 
 356   assert(jvms == this->jvms(), "still operating on the right JVMS");
 357   assert(jvms_in_sync(),       "jvms must carry full info into CG");
 358 
 359   // save across call, for a subsequent cast_not_null.
 360   Node* receiver = has_receiver ? argument(0) : NULL;
 361 
 362   // Bump method data counters (We profile *before* the call is made 
 363   // because exceptions don't return to the call site.)
 364   profile_call(receiver);
 365 
 366   JVMState* new_jvms;
 367   if ((new_jvms = cg->generate(jvms)) == NULL) {
 368     // When inlining attempt fails (e.g., too many arguments),
 369     // it may contaminate the current compile state, making it
 370     // impossible to pull back and try again.  Once we call
 371     // cg->generate(), we are committed.  If it fails, the whole
 372     // compilation task is compromised.
 373     if (failing())  return;
 374 #ifndef PRODUCT
 375     if (PrintOpto || PrintOptoInlining || PrintInlining) {
 376       // Only one fall-back, so if an intrinsic fails, ignore any bytecodes.
 377       if (cg->is_intrinsic() && call_method->code_size() > 0) {
 378         tty->print("Bailed out of intrinsic, will not inline: ");
 379         call_method->print_name(); tty->cr();
 380       }
 381     }
 382 #endif
 383     // This can happen if a library intrinsic is available, but refuses
 384     // the call site, perhaps because it did not match a pattern the
 385     // intrinsic was expecting to optimize.  The fallback position is
 386     // to call out-of-line.
 387     try_inline = false;  // Inline tactic bailed out.
 388     cg = C->call_generator(call_method, vtable_index, call_is_virtual, jvms, try_inline, prof_factor());
 389     if ((new_jvms = cg->generate(jvms)) == NULL) {
 390       guarantee(failing(), "call failed to generate:  calls should work");
 391       return;
 392     }
 393   }
 394 
 395   if (cg->is_inline()) {
 396     // Accumulate has_loops estimate
 397     C->set_has_loops(C->has_loops() || call_method->has_loops());
 398     C->env()->notice_inlined_method(call_method);
 399   }
 400 
 401   // Reset parser state from [new_]jvms, which now carries results of the call.
 402   // Return value (if any) is already pushed on the stack by the cg.
 403   add_exception_states_from(new_jvms);
 404   if (new_jvms->map()->control() == top()) {
 405     stop_and_kill_map();
 406   } else {
 407     assert(new_jvms->same_calls_as(jvms), "method/bci left unchanged");
 408     set_jvms(new_jvms);
 409   }
 410 
 411   if (!stopped()) {
 412     // This was some sort of virtual call, which did a null check for us.
 413     // Now we can assert receiver-not-null, on the normal return path.
 414     if (receiver != NULL && cg->is_virtual()) {
 415       Node* cast = cast_not_null(receiver);
 416       // %%% assert(receiver == cast, "should already have cast the receiver");
 417     }
 418 
 419     // Round double result after a call from strict to non-strict code
 420     round_double_result(dest_method);
 421 
 422     // If the return type of the method is not loaded, assert that the
 423     // value we got is a null.  Otherwise, we need to recompile.
 424     if (!dest_method->return_type()->is_loaded()) {
 425 #ifndef PRODUCT
 426       if (PrintOpto && (Verbose || WizardMode)) {
 427         method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci());
 428         dest_method->print_name(); tty->cr();
 429       }
 430 #endif
 431       if (C->log() != NULL) {
 432         C->log()->elem("assert_null reason='return' klass='%d'",
 433                        C->log()->identify(dest_method->return_type()));
 434       }
 435       // If there is going to be a trap, put it at the next bytecode:
 436       set_bci(iter().next_bci());
 437       do_null_assert(peek(), T_OBJECT);
 438       set_bci(iter().cur_bci()); // put it back
 439     }
 440   }
 441 
 442   // Restart record of parsing work after possible inlining of call
 443 #ifndef PRODUCT
 444   parse_histogram()->set_initial_state(bc());
 445 #endif
 446 }
 447 
 448 //---------------------------catch_call_exceptions-----------------------------
 449 // Put a Catch and CatchProj nodes behind a just-created call.
 450 // Send their caught exceptions to the proper handler.
 451 // This may be used after a call to the rethrow VM stub,
 452 // when it is needed to process unloaded exception classes.
 453 void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) {
 454   // Exceptions are delivered through this channel:
 455   Node* i_o = this->i_o();
 456 
 457   // Add a CatchNode.
 458   GrowableArray<int>* bcis = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, -1);
 459   GrowableArray<const Type*>* extypes = new (C->node_arena()) GrowableArray<const Type*>(C->node_arena(), 8, 0, NULL);
 460   GrowableArray<int>* saw_unloaded = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, 0);
 461 
 462   for (; !handlers.is_done(); handlers.next()) {
 463     ciExceptionHandler* h        = handlers.handler();
 464     int                 h_bci    = h->handler_bci();
 465     ciInstanceKlass*    h_klass  = h->is_catch_all() ? env()->Throwable_klass() : h->catch_klass();
 466     // Do not introduce unloaded exception types into the graph:
 467     if (!h_klass->is_loaded()) {
 468       if (saw_unloaded->contains(h_bci)) {
 469         /* We've already seen an unloaded exception with h_bci, 
 470            so don't duplicate. Duplication will cause the CatchNode to be
 471            unnecessarily large. See 4713716. */
 472         continue;
 473       } else {
 474         saw_unloaded->append(h_bci);
 475       }
 476     }
 477     const Type*         h_extype = TypeOopPtr::make_from_klass(h_klass);
 478     // (We use make_from_klass because it respects UseUniqueSubclasses.)
 479     h_extype = h_extype->join(TypeInstPtr::NOTNULL);
 480     assert(!h_extype->empty(), "sanity");
 481     // Note:  It's OK if the BCIs repeat themselves.
 482     bcis->append(h_bci);
 483     extypes->append(h_extype);
 484   }
 485 
 486   int len = bcis->length();
 487   CatchNode *cn = new (C, 2) CatchNode(control(), i_o, len+1);
 488   Node *catch_ = _gvn.transform(cn);
 489 
 490   // now branch with the exception state to each of the (potential)
 491   // handlers
 492   for(int i=0; i < len; i++) {
 493     // Setup JVM state to enter the handler.
 494     PreserveJVMState pjvms(this);
 495     // Locals are just copied from before the call.
 496     // Get control from the CatchNode.
 497     int handler_bci = bcis->at(i);
 498     Node* ctrl = _gvn.transform( new (C, 1) CatchProjNode(catch_, i+1,handler_bci));
 499     // This handler cannot happen?
 500     if (ctrl == top())  continue;
 501     set_control(ctrl);
 502 
 503     // Create exception oop
 504     const TypeInstPtr* extype = extypes->at(i)->is_instptr();
 505     Node *ex_oop = _gvn.transform(new (C, 2) CreateExNode(extypes->at(i), ctrl, i_o));
 506 
 507     // Handle unloaded exception classes.
 508     if (saw_unloaded->contains(handler_bci)) {
 509       // An unloaded exception type is coming here.  Do an uncommon trap.
 510 #ifndef PRODUCT
 511       // We do not expect the same handler bci to take both cold unloaded
 512       // and hot loaded exceptions.  But, watch for it.
 513       if (extype->is_loaded()) {
 514         tty->print_cr("Warning: Handler @%d takes mixed loaded/unloaded exceptions in ");
 515         method()->print_name(); tty->cr();
 516       } else if (PrintOpto && (Verbose || WizardMode)) {
 517         tty->print("Bailing out on unloaded exception type ");
 518         extype->klass()->print_name();
 519         tty->print(" at bci:%d in ", bci());
 520         method()->print_name(); tty->cr();
 521       }
 522 #endif
 523       // Emit an uncommon trap instead of processing the block.
 524       set_bci(handler_bci);
 525       push_ex_oop(ex_oop);
 526       uncommon_trap(Deoptimization::Reason_unloaded,
 527                     Deoptimization::Action_reinterpret,
 528                     extype->klass(), "!loaded exception");
 529       set_bci(iter().cur_bci()); // put it back
 530       continue;
 531     }
 532 
 533     // go to the exception handler
 534     if (handler_bci < 0) {     // merge with corresponding rethrow node
 535       throw_to_exit(make_exception_state(ex_oop));
 536     } else {                      // Else jump to corresponding handle
 537       push_ex_oop(ex_oop);        // Clear stack and push just the oop.
 538       merge_exception(handler_bci);
 539     }
 540   }
 541 
 542   // The first CatchProj is for the normal return.
 543   // (Note:  If this is a call to rethrow_Java, this node goes dead.)
 544   set_control(_gvn.transform( new (C, 1) CatchProjNode(catch_, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci)));
 545 }
 546 
 547 
 548 //----------------------------catch_inline_exceptions--------------------------
 549 // Handle all exceptions thrown by an inlined method or individual bytecode.
 550 // Common case 1: we have no handler, so all exceptions merge right into
 551 // the rethrow case.
 552 // Case 2: we have some handlers, with loaded exception klasses that have
 553 // no subklasses.  We do a Deutsch-Shiffman style type-check on the incoming
 554 // exception oop and branch to the handler directly.
 555 // Case 3: We have some handlers with subklasses or are not loaded at
 556 // compile-time.  We have to call the runtime to resolve the exception.
 557 // So we insert a RethrowCall and all the logic that goes with it.
 558 void Parse::catch_inline_exceptions(SafePointNode* ex_map) {
 559   // Caller is responsible for saving away the map for normal control flow!
 560   assert(stopped(), "call set_map(NULL) first");
 561   assert(method()->has_exception_handlers(), "don't come here w/o work to do");
 562 
 563   Node* ex_node = saved_ex_oop(ex_map);
 564   if (ex_node == top()) {
 565     // No action needed.
 566     return;
 567   }
 568   const TypeInstPtr* ex_type = _gvn.type(ex_node)->isa_instptr();
 569   NOT_PRODUCT(if (ex_type==NULL) tty->print_cr("*** Exception not InstPtr"));
 570   if (ex_type == NULL)
 571     ex_type = TypeOopPtr::make_from_klass(env()->Throwable_klass())->is_instptr();
 572 
 573   // determine potential exception handlers
 574   ciExceptionHandlerStream handlers(method(), bci(),
 575                                     ex_type->klass()->as_instance_klass(),
 576                                     ex_type->klass_is_exact());
 577 
 578   // Start executing from the given throw state.  (Keep its stack, for now.)
 579   // Get the exception oop as known at compile time.
 580   ex_node = use_exception_state(ex_map);
 581 
 582   // Get the exception oop klass from its header
 583   Node* ex_klass_node = NULL;
 584   if (has_ex_handler() && !ex_type->klass_is_exact()) {
 585     Node* p = basic_plus_adr( ex_node, ex_node, oopDesc::klass_offset_in_bytes());
 586     ex_klass_node = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS, TypeKlassPtr::OBJECT) );
 587 
 588     // Compute the exception klass a little more cleverly.
 589     // Obvious solution is to simple do a LoadKlass from the 'ex_node'.
 590     // However, if the ex_node is a PhiNode, I'm going to do a LoadKlass for
 591     // each arm of the Phi.  If I know something clever about the exceptions
 592     // I'm loading the class from, I can replace the LoadKlass with the
 593     // klass constant for the exception oop.
 594     if( ex_node->is_Phi() ) {
 595       ex_klass_node = new (C, ex_node->req()) PhiNode( ex_node->in(0), TypeKlassPtr::OBJECT );
 596       for( uint i = 1; i < ex_node->req(); i++ ) {
 597         Node* p = basic_plus_adr( ex_node->in(i), ex_node->in(i), oopDesc::klass_offset_in_bytes() );
 598         Node* k = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS, TypeKlassPtr::OBJECT) );
 599         ex_klass_node->init_req( i, k );
 600       }
 601       _gvn.set_type(ex_klass_node, TypeKlassPtr::OBJECT);
 602       
 603     }
 604   }
 605 
 606   // Scan the exception table for applicable handlers.
 607   // If none, we can call rethrow() and be done!
 608   // If precise (loaded with no subklasses), insert a D.S. style
 609   // pointer compare to the correct handler and loop back.
 610   // If imprecise, switch to the Rethrow VM-call style handling.
 611 
 612   int remaining = handlers.count_remaining();
 613 
 614   // iterate through all entries sequentially
 615   for (;!handlers.is_done(); handlers.next()) {
 616     // Do nothing if turned off
 617     if( !DeutschShiffmanExceptions ) break;
 618     ciExceptionHandler* handler = handlers.handler();
 619 
 620     if (handler->is_rethrow()) {
 621       // If we fell off the end of the table without finding an imprecise
 622       // exception klass (and without finding a generic handler) then we
 623       // know this exception is not handled in this method.  We just rethrow
 624       // the exception into the caller.
 625       throw_to_exit(make_exception_state(ex_node));
 626       return;
 627     }
 628 
 629     // exception handler bci range covers throw_bci => investigate further
 630     int handler_bci = handler->handler_bci();
 631 
 632     if (remaining == 1) {
 633       push_ex_oop(ex_node);        // Push exception oop for handler
 634 #ifndef PRODUCT
 635       if (PrintOpto && WizardMode) {
 636         tty->print_cr("  Catching every inline exception bci:%d -> handler_bci:%d", bci(), handler_bci);
 637       }
 638 #endif
 639       merge_exception(handler_bci); // jump to handler
 640       return;                   // No more handling to be done here!
 641     }
 642 
 643     // %%% The following logic replicates make_from_klass_unique.
 644     // TO DO:  Replace by a subroutine call.  Then generalize
 645     // the type check, as noted in the next "%%%" comment.
 646 
 647     ciInstanceKlass* klass = handler->catch_klass();
 648     if (UseUniqueSubclasses) {
 649       // (We use make_from_klass because it respects UseUniqueSubclasses.)
 650       const TypeOopPtr* tp = TypeOopPtr::make_from_klass(klass);
 651       klass = tp->klass()->as_instance_klass();
 652     }
 653 
 654     // Get the handler's klass
 655     if (!klass->is_loaded())    // klass is not loaded?
 656       break;                    // Must call Rethrow!
 657     if (klass->is_interface())  // should not happen, but...
 658       break;                    // bail out
 659     // See if the loaded exception klass has no subtypes
 660     if (klass->has_subklass())
 661       break;                    // Cannot easily do precise test ==> Rethrow
 662 
 663     // %%% Now that subclass checking is very fast, we need to rewrite
 664     // this section and remove the option "DeutschShiffmanExceptions".
 665     // The exception processing chain should be a normal typecase pattern,
 666     // with a bailout to the interpreter only in the case of unloaded
 667     // classes.  (The bailout should mark the method non-entrant.)
 668     // This rewrite should be placed in GraphKit::, not Parse::.
 669 
 670     // Add a dependence; if any subclass added we need to recompile
 671     // %%% should use stronger assert_unique_concrete_subtype instead
 672     if (!klass->is_final()) {
 673       C->dependencies()->assert_leaf_type(klass);
 674     }
 675 
 676     // Implement precise test 
 677     const TypeKlassPtr *tk = TypeKlassPtr::make(klass);
 678     Node* con = _gvn.makecon(tk);
 679     Node* cmp = _gvn.transform( new (C, 3) CmpPNode(ex_klass_node, con) );
 680     Node* bol = _gvn.transform( new (C, 2) BoolNode(cmp, BoolTest::ne) );
 681     { BuildCutout unless(this, bol, PROB_LIKELY(0.7f));
 682       const TypeInstPtr* tinst = TypeInstPtr::make_exact(TypePtr::NotNull, klass);
 683       Node* ex_oop = _gvn.transform(new (C, 2) CheckCastPPNode(control(), ex_node, tinst));
 684       push_ex_oop(ex_oop);      // Push exception oop for handler
 685 #ifndef PRODUCT
 686       if (PrintOpto && WizardMode) {
 687         tty->print("  Catching inline exception bci:%d -> handler_bci:%d -- ", bci(), handler_bci);
 688         klass->print_name();
 689         tty->cr();
 690       }
 691 #endif
 692       merge_exception(handler_bci);
 693     }
 694 
 695     // Come here if exception does not match handler.
 696     // Carry on with more handler checks.
 697     --remaining;
 698   }
 699 
 700   assert(!stopped(), "you should return if you finish the chain");
 701 
 702   if (remaining == 1) {
 703     // Further checks do not matter.
 704   }
 705 
 706   if (can_rerun_bytecode()) {
 707     // Do not push_ex_oop here!
 708     // Re-executing the bytecode will reproduce the throwing condition.
 709     bool must_throw = true;
 710     uncommon_trap(Deoptimization::Reason_unhandled,
 711                   Deoptimization::Action_none,
 712                   (ciKlass*)NULL, (const char*)NULL, // default args
 713                   must_throw);
 714     return;
 715   }
 716 
 717   // Oops, need to call into the VM to resolve the klasses at runtime.
 718   // Note:  This call must not deoptimize, since it is not a real at this bci!
 719   kill_dead_locals();
 720 
 721   make_runtime_call(RC_NO_LEAF | RC_MUST_THROW,
 722                     OptoRuntime::rethrow_Type(),
 723                     OptoRuntime::rethrow_stub(),
 724                     NULL, NULL,
 725                     ex_node);
 726 
 727   // Rethrow is a pure call, no side effects, only a result.
 728   // The result cannot be allocated, so we use I_O
 729 
 730   // Catch exceptions from the rethrow
 731   catch_call_exceptions(handlers);
 732 }
 733 
 734 
 735 // (Note:  Moved add_debug_info into GraphKit::add_safepoint_edges.)
 736 
 737 
 738 #ifndef PRODUCT
 739 void Parse::count_compiled_calls(bool at_method_entry, bool is_inline) {
 740   if( CountCompiledCalls ) {
 741     if( at_method_entry ) {
 742       // bump invocation counter if top method (for statistics)
 743       if (CountCompiledCalls && depth() == 1) {
 744         const TypeInstPtr* addr_type = TypeInstPtr::make(method());
 745         Node* adr1 = makecon(addr_type);
 746         Node* adr2 = basic_plus_adr(adr1, adr1, in_bytes(methodOopDesc::compiled_invocation_counter_offset()));
 747         increment_counter(adr2);
 748       }
 749     } else if (is_inline) {
 750       switch (bc()) {
 751       case Bytecodes::_invokevirtual:   increment_counter(SharedRuntime::nof_inlined_calls_addr()); break;
 752       case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_inlined_interface_calls_addr()); break;
 753       case Bytecodes::_invokestatic:  
 754       case Bytecodes::_invokespecial:   increment_counter(SharedRuntime::nof_inlined_static_calls_addr()); break;
 755       default: fatal("unexpected call bytecode");
 756       }
 757     } else {
 758       switch (bc()) {
 759       case Bytecodes::_invokevirtual:   increment_counter(SharedRuntime::nof_normal_calls_addr()); break;
 760       case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_interface_calls_addr()); break;
 761       case Bytecodes::_invokestatic:  
 762       case Bytecodes::_invokespecial:   increment_counter(SharedRuntime::nof_static_calls_addr()); break;
 763       default: fatal("unexpected call bytecode");
 764       }
 765     }
 766   }
 767 }
 768 #endif //PRODUCT
 769 
 770 
 771 // Identify possible target method and inlining style
 772 ciMethod* Parse::optimize_inlining(ciMethod* caller, int bci, ciInstanceKlass* klass, 
 773                                    ciMethod *dest_method, const TypeOopPtr* receiver_type) {
 774   // only use for virtual or interface calls
 775 
 776   // If it is obviously final, do not bother to call find_monomorphic_target,
 777   // because the class hierarchy checks are not needed, and may fail due to
 778   // incompletely loaded classes.  Since we do our own class loading checks
 779   // in this module, we may confidently bind to any method.
 780   if (dest_method->can_be_statically_bound()) {
 781     return dest_method;
 782   }
 783 
 784   // Attempt to improve the receiver
 785   bool actual_receiver_is_exact = false;
 786   ciInstanceKlass* actual_receiver = klass;
 787   if (receiver_type != NULL) {
 788     // Array methods are all inherited from Object, and are monomorphic.
 789     if (receiver_type->isa_aryptr() &&
 790         dest_method->holder() == env()->Object_klass()) {
 791       return dest_method;
 792     }
 793 
 794     // All other interesting cases are instance klasses.
 795     if (!receiver_type->isa_instptr()) {
 796       return NULL;
 797     }
 798 
 799     ciInstanceKlass *ikl = receiver_type->klass()->as_instance_klass();
 800     if (ikl->is_loaded() && ikl->is_initialized() && !ikl->is_interface() &&
 801         (ikl == actual_receiver || ikl->is_subtype_of(actual_receiver))) {
 802       // ikl is a same or better type than the original actual_receiver,
 803       // e.g. static receiver from bytecodes.
 804       actual_receiver = ikl;
 805       // Is the actual_receiver exact?
 806       actual_receiver_is_exact = receiver_type->klass_is_exact();
 807     }
 808   }
 809 
 810   ciInstanceKlass*   calling_klass = caller->holder();
 811   ciMethod* cha_monomorphic_target = dest_method->find_monomorphic_target(calling_klass, klass, actual_receiver);
 812   if (cha_monomorphic_target != NULL) {
 813     assert(!cha_monomorphic_target->is_abstract(), "");
 814     // Look at the method-receiver type.  Does it add "too much information"?
 815     ciKlass*    mr_klass = cha_monomorphic_target->holder();
 816     const Type* mr_type  = TypeInstPtr::make(TypePtr::BotPTR, mr_klass);
 817     if (receiver_type == NULL || !receiver_type->higher_equal(mr_type)) {
 818       // Calling this method would include an implicit cast to its holder.
 819       // %%% Not yet implemented.  Would throw minor asserts at present.
 820       // %%% The most common wins are already gained by +UseUniqueSubclasses.
 821       // To fix, put the higher_equal check at the call of this routine,
 822       // and add a CheckCastPP to the receiver.
 823       if (TraceDependencies) {
 824         tty->print_cr("found unique CHA method, but could not cast up");
 825         tty->print("  method  = ");
 826         cha_monomorphic_target->print();
 827         tty->cr();
 828       }
 829       if (C->log() != NULL) {
 830         C->log()->elem("missed_CHA_opportunity klass='%d' method='%d'",
 831                        C->log()->identify(klass),
 832                        C->log()->identify(cha_monomorphic_target));
 833       }
 834       cha_monomorphic_target = NULL;
 835     }
 836   }
 837   if (cha_monomorphic_target != NULL) {
 838     // Hardwiring a virtual.
 839     // If we inlined because CHA revealed only a single target method,
 840     // then we are dependent on that target method not getting overridden
 841     // by dynamic class loading.  Be sure to test the "static" receiver
 842     // dest_method here, as opposed to the actual receiver, which may
 843     // falsely lead us to believe that the receiver is final or private.
 844     C->dependencies()->assert_unique_concrete_method(actual_receiver, cha_monomorphic_target);
 845     return cha_monomorphic_target;
 846   }
 847 
 848   // If the type is exact, we can still bind the method w/o a vcall.
 849   // (This case comes after CHA so we can see how much extra work it does.)
 850   if (actual_receiver_is_exact) {
 851     // In case of evolution, there is a dependence on every inlined method, since each
 852     // such method can be changed when its class is redefined.
 853     ciMethod* exact_method = dest_method->resolve_invoke(calling_klass, actual_receiver);
 854     if (exact_method != NULL) {
 855 #ifndef PRODUCT
 856       if (PrintOpto) {
 857         tty->print("  Calling method via exact type @%d --- ", bci);
 858         exact_method->print_name();
 859         tty->cr();
 860       }
 861 #endif
 862       return exact_method;
 863     }
 864   }
 865 
 866   return NULL;
 867 }