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