src/share/vm/opto/doCall.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial