src/share/vm/opto/doCall.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7792
99edc344d77c
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

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

mercurial