1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/doCall.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,1065 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "ci/ciCallSite.hpp" 1.30 +#include "ci/ciMethodHandle.hpp" 1.31 +#include "classfile/vmSymbols.hpp" 1.32 +#include "compiler/compileBroker.hpp" 1.33 +#include "compiler/compileLog.hpp" 1.34 +#include "interpreter/linkResolver.hpp" 1.35 +#include "opto/addnode.hpp" 1.36 +#include "opto/callGenerator.hpp" 1.37 +#include "opto/cfgnode.hpp" 1.38 +#include "opto/mulnode.hpp" 1.39 +#include "opto/parse.hpp" 1.40 +#include "opto/rootnode.hpp" 1.41 +#include "opto/runtime.hpp" 1.42 +#include "opto/subnode.hpp" 1.43 +#include "prims/nativeLookup.hpp" 1.44 +#include "runtime/sharedRuntime.hpp" 1.45 + 1.46 +void trace_type_profile(Compile* C, ciMethod *method, int depth, int bci, ciMethod *prof_method, ciKlass *prof_klass, int site_count, int receiver_count) { 1.47 + if (TraceTypeProfile || C->print_inlining()) { 1.48 + outputStream* out = tty; 1.49 + if (!C->print_inlining()) { 1.50 + if (NOT_PRODUCT(!PrintOpto &&) !PrintCompilation) { 1.51 + method->print_short_name(); 1.52 + tty->cr(); 1.53 + } 1.54 + CompileTask::print_inlining(prof_method, depth, bci); 1.55 + } else { 1.56 + out = C->print_inlining_stream(); 1.57 + } 1.58 + CompileTask::print_inline_indent(depth, out); 1.59 + out->print(" \\-> TypeProfile (%d/%d counts) = ", receiver_count, site_count); 1.60 + stringStream ss; 1.61 + prof_klass->name()->print_symbol_on(&ss); 1.62 + out->print("%s", ss.as_string()); 1.63 + out->cr(); 1.64 + } 1.65 +} 1.66 + 1.67 +CallGenerator* Compile::call_generator(ciMethod* callee, int vtable_index, bool call_does_dispatch, 1.68 + JVMState* jvms, bool allow_inline, 1.69 + float prof_factor, ciKlass* speculative_receiver_type, 1.70 + bool allow_intrinsics, bool delayed_forbidden) { 1.71 + ciMethod* caller = jvms->method(); 1.72 + int bci = jvms->bci(); 1.73 + Bytecodes::Code bytecode = caller->java_code_at_bci(bci); 1.74 + guarantee(callee != NULL, "failed method resolution"); 1.75 + 1.76 + // Dtrace currently doesn't work unless all calls are vanilla 1.77 + if (env()->dtrace_method_probes()) { 1.78 + allow_inline = false; 1.79 + } 1.80 + 1.81 + // Note: When we get profiling during stage-1 compiles, we want to pull 1.82 + // from more specific profile data which pertains to this inlining. 1.83 + // Right now, ignore the information in jvms->caller(), and do method[bci]. 1.84 + ciCallProfile profile = caller->call_profile_at_bci(bci); 1.85 + 1.86 + // See how many times this site has been invoked. 1.87 + int site_count = profile.count(); 1.88 + int receiver_count = -1; 1.89 + if (call_does_dispatch && UseTypeProfile && profile.has_receiver(0)) { 1.90 + // Receivers in the profile structure are ordered by call counts 1.91 + // so that the most called (major) receiver is profile.receiver(0). 1.92 + receiver_count = profile.receiver_count(0); 1.93 + } 1.94 + 1.95 + CompileLog* log = this->log(); 1.96 + if (log != NULL) { 1.97 + int rid = (receiver_count >= 0)? log->identify(profile.receiver(0)): -1; 1.98 + int r2id = (rid != -1 && profile.has_receiver(1))? log->identify(profile.receiver(1)):-1; 1.99 + log->begin_elem("call method='%d' count='%d' prof_factor='%g'", 1.100 + log->identify(callee), site_count, prof_factor); 1.101 + if (call_does_dispatch) log->print(" virtual='1'"); 1.102 + if (allow_inline) log->print(" inline='1'"); 1.103 + if (receiver_count >= 0) { 1.104 + log->print(" receiver='%d' receiver_count='%d'", rid, receiver_count); 1.105 + if (profile.has_receiver(1)) { 1.106 + log->print(" receiver2='%d' receiver2_count='%d'", r2id, profile.receiver_count(1)); 1.107 + } 1.108 + } 1.109 + log->end_elem(); 1.110 + } 1.111 + 1.112 + // Special case the handling of certain common, profitable library 1.113 + // methods. If these methods are replaced with specialized code, 1.114 + // then we return it as the inlined version of the call. 1.115 + // We do this before the strict f.p. check below because the 1.116 + // intrinsics handle strict f.p. correctly. 1.117 + CallGenerator* cg_intrinsic = NULL; 1.118 + if (allow_inline && allow_intrinsics) { 1.119 + CallGenerator* cg = find_intrinsic(callee, call_does_dispatch); 1.120 + if (cg != NULL) { 1.121 + if (cg->is_predicted()) { 1.122 + // Code without intrinsic but, hopefully, inlined. 1.123 + CallGenerator* inline_cg = this->call_generator(callee, 1.124 + vtable_index, call_does_dispatch, jvms, allow_inline, prof_factor, speculative_receiver_type, false); 1.125 + if (inline_cg != NULL) { 1.126 + cg = CallGenerator::for_predicted_intrinsic(cg, inline_cg); 1.127 + } 1.128 + } 1.129 + 1.130 + // If intrinsic does the virtual dispatch, we try to use the type profile 1.131 + // first, and hopefully inline it as the regular virtual call below. 1.132 + // We will retry the intrinsic if nothing had claimed it afterwards. 1.133 + if (cg->does_virtual_dispatch()) { 1.134 + cg_intrinsic = cg; 1.135 + cg = NULL; 1.136 + } else { 1.137 + return cg; 1.138 + } 1.139 + } 1.140 + } 1.141 + 1.142 + // Do method handle calls. 1.143 + // NOTE: This must happen before normal inlining logic below since 1.144 + // MethodHandle.invoke* are native methods which obviously don't 1.145 + // have bytecodes and so normal inlining fails. 1.146 + if (callee->is_method_handle_intrinsic()) { 1.147 + CallGenerator* cg = CallGenerator::for_method_handle_call(jvms, caller, callee, delayed_forbidden); 1.148 + assert(cg == NULL || !delayed_forbidden || !cg->is_late_inline() || cg->is_mh_late_inline(), "unexpected CallGenerator"); 1.149 + return cg; 1.150 + } 1.151 + 1.152 + // Do not inline strict fp into non-strict code, or the reverse 1.153 + if (caller->is_strict() ^ callee->is_strict()) { 1.154 + allow_inline = false; 1.155 + } 1.156 + 1.157 + // Attempt to inline... 1.158 + if (allow_inline) { 1.159 + // The profile data is only partly attributable to this caller, 1.160 + // scale back the call site information. 1.161 + float past_uses = jvms->method()->scale_count(site_count, prof_factor); 1.162 + // This is the number of times we expect the call code to be used. 1.163 + float expected_uses = past_uses; 1.164 + 1.165 + // Try inlining a bytecoded method: 1.166 + if (!call_does_dispatch) { 1.167 + InlineTree* ilt = InlineTree::find_subtree_from_root(this->ilt(), jvms->caller(), jvms->method()); 1.168 + WarmCallInfo scratch_ci; 1.169 + bool should_delay = false; 1.170 + WarmCallInfo* ci = ilt->ok_to_inline(callee, jvms, profile, &scratch_ci, should_delay); 1.171 + assert(ci != &scratch_ci, "do not let this pointer escape"); 1.172 + bool allow_inline = (ci != NULL && !ci->is_cold()); 1.173 + bool require_inline = (allow_inline && ci->is_hot()); 1.174 + 1.175 + if (allow_inline) { 1.176 + CallGenerator* cg = CallGenerator::for_inline(callee, expected_uses); 1.177 + 1.178 + if (require_inline && cg != NULL) { 1.179 + // Delay the inlining of this method to give us the 1.180 + // opportunity to perform some high level optimizations 1.181 + // first. 1.182 + if (should_delay_string_inlining(callee, jvms)) { 1.183 + assert(!delayed_forbidden, "strange"); 1.184 + return CallGenerator::for_string_late_inline(callee, cg); 1.185 + } else if (should_delay_boxing_inlining(callee, jvms)) { 1.186 + assert(!delayed_forbidden, "strange"); 1.187 + return CallGenerator::for_boxing_late_inline(callee, cg); 1.188 + } else if ((should_delay || AlwaysIncrementalInline) && !delayed_forbidden) { 1.189 + return CallGenerator::for_late_inline(callee, cg); 1.190 + } 1.191 + } 1.192 + if (cg == NULL || should_delay) { 1.193 + // Fall through. 1.194 + } else if (require_inline || !InlineWarmCalls) { 1.195 + return cg; 1.196 + } else { 1.197 + CallGenerator* cold_cg = call_generator(callee, vtable_index, call_does_dispatch, jvms, false, prof_factor); 1.198 + return CallGenerator::for_warm_call(ci, cold_cg, cg); 1.199 + } 1.200 + } 1.201 + } 1.202 + 1.203 + // Try using the type profile. 1.204 + if (call_does_dispatch && site_count > 0 && receiver_count > 0) { 1.205 + // The major receiver's count >= TypeProfileMajorReceiverPercent of site_count. 1.206 + bool have_major_receiver = (100.*profile.receiver_prob(0) >= (float)TypeProfileMajorReceiverPercent); 1.207 + ciMethod* receiver_method = NULL; 1.208 + 1.209 + int morphism = profile.morphism(); 1.210 + if (speculative_receiver_type != NULL) { 1.211 + // We have a speculative type, we should be able to resolve 1.212 + // the call. We do that before looking at the profiling at 1.213 + // this invoke because it may lead to bimorphic inlining which 1.214 + // a speculative type should help us avoid. 1.215 + receiver_method = callee->resolve_invoke(jvms->method()->holder(), 1.216 + speculative_receiver_type); 1.217 + if (receiver_method == NULL) { 1.218 + speculative_receiver_type = NULL; 1.219 + } else { 1.220 + morphism = 1; 1.221 + } 1.222 + } 1.223 + if (receiver_method == NULL && 1.224 + (have_major_receiver || morphism == 1 || 1.225 + (morphism == 2 && UseBimorphicInlining))) { 1.226 + // receiver_method = profile.method(); 1.227 + // Profiles do not suggest methods now. Look it up in the major receiver. 1.228 + receiver_method = callee->resolve_invoke(jvms->method()->holder(), 1.229 + profile.receiver(0)); 1.230 + } 1.231 + if (receiver_method != NULL) { 1.232 + // The single majority receiver sufficiently outweighs the minority. 1.233 + CallGenerator* hit_cg = this->call_generator(receiver_method, 1.234 + vtable_index, !call_does_dispatch, jvms, allow_inline, prof_factor); 1.235 + if (hit_cg != NULL) { 1.236 + // Look up second receiver. 1.237 + CallGenerator* next_hit_cg = NULL; 1.238 + ciMethod* next_receiver_method = NULL; 1.239 + if (morphism == 2 && UseBimorphicInlining) { 1.240 + next_receiver_method = callee->resolve_invoke(jvms->method()->holder(), 1.241 + profile.receiver(1)); 1.242 + if (next_receiver_method != NULL) { 1.243 + next_hit_cg = this->call_generator(next_receiver_method, 1.244 + vtable_index, !call_does_dispatch, jvms, 1.245 + allow_inline, prof_factor); 1.246 + if (next_hit_cg != NULL && !next_hit_cg->is_inline() && 1.247 + have_major_receiver && UseOnlyInlinedBimorphic) { 1.248 + // Skip if we can't inline second receiver's method 1.249 + next_hit_cg = NULL; 1.250 + } 1.251 + } 1.252 + } 1.253 + CallGenerator* miss_cg; 1.254 + Deoptimization::DeoptReason reason = morphism == 2 ? 1.255 + Deoptimization::Reason_bimorphic : 1.256 + (speculative_receiver_type == NULL ? Deoptimization::Reason_class_check : Deoptimization::Reason_speculate_class_check); 1.257 + if ((morphism == 1 || (morphism == 2 && next_hit_cg != NULL)) && 1.258 + !too_many_traps(jvms->method(), jvms->bci(), reason) 1.259 + ) { 1.260 + // Generate uncommon trap for class check failure path 1.261 + // in case of monomorphic or bimorphic virtual call site. 1.262 + miss_cg = CallGenerator::for_uncommon_trap(callee, reason, 1.263 + Deoptimization::Action_maybe_recompile); 1.264 + } else { 1.265 + // Generate virtual call for class check failure path 1.266 + // in case of polymorphic virtual call site. 1.267 + miss_cg = CallGenerator::for_virtual_call(callee, vtable_index); 1.268 + } 1.269 + if (miss_cg != NULL) { 1.270 + if (next_hit_cg != NULL) { 1.271 + assert(speculative_receiver_type == NULL, "shouldn't end up here if we used speculation"); 1.272 + trace_type_profile(C, jvms->method(), jvms->depth() - 1, jvms->bci(), next_receiver_method, profile.receiver(1), site_count, profile.receiver_count(1)); 1.273 + // We don't need to record dependency on a receiver here and below. 1.274 + // Whenever we inline, the dependency is added by Parse::Parse(). 1.275 + miss_cg = CallGenerator::for_predicted_call(profile.receiver(1), miss_cg, next_hit_cg, PROB_MAX); 1.276 + } 1.277 + if (miss_cg != NULL) { 1.278 + trace_type_profile(C, jvms->method(), jvms->depth() - 1, jvms->bci(), receiver_method, profile.receiver(0), site_count, receiver_count); 1.279 + ciKlass* k = speculative_receiver_type != NULL ? speculative_receiver_type : profile.receiver(0); 1.280 + float hit_prob = speculative_receiver_type != NULL ? 1.0 : profile.receiver_prob(0); 1.281 + CallGenerator* cg = CallGenerator::for_predicted_call(k, miss_cg, hit_cg, hit_prob); 1.282 + if (cg != NULL) return cg; 1.283 + } 1.284 + } 1.285 + } 1.286 + } 1.287 + } 1.288 + } 1.289 + 1.290 + // Nothing claimed the intrinsic, we go with straight-forward inlining 1.291 + // for already discovered intrinsic. 1.292 + if (allow_inline && allow_intrinsics && cg_intrinsic != NULL) { 1.293 + assert(cg_intrinsic->does_virtual_dispatch(), "sanity"); 1.294 + return cg_intrinsic; 1.295 + } 1.296 + 1.297 + // There was no special inlining tactic, or it bailed out. 1.298 + // Use a more generic tactic, like a simple call. 1.299 + if (call_does_dispatch) { 1.300 + return CallGenerator::for_virtual_call(callee, vtable_index); 1.301 + } else { 1.302 + // Class Hierarchy Analysis or Type Profile reveals a unique target, 1.303 + // or it is a static or special call. 1.304 + return CallGenerator::for_direct_call(callee, should_delay_inlining(callee, jvms)); 1.305 + } 1.306 +} 1.307 + 1.308 +// Return true for methods that shouldn't be inlined early so that 1.309 +// they are easier to analyze and optimize as intrinsics. 1.310 +bool Compile::should_delay_string_inlining(ciMethod* call_method, JVMState* jvms) { 1.311 + if (has_stringbuilder()) { 1.312 + 1.313 + if ((call_method->holder() == C->env()->StringBuilder_klass() || 1.314 + call_method->holder() == C->env()->StringBuffer_klass()) && 1.315 + (jvms->method()->holder() == C->env()->StringBuilder_klass() || 1.316 + jvms->method()->holder() == C->env()->StringBuffer_klass())) { 1.317 + // Delay SB calls only when called from non-SB code 1.318 + return false; 1.319 + } 1.320 + 1.321 + switch (call_method->intrinsic_id()) { 1.322 + case vmIntrinsics::_StringBuilder_void: 1.323 + case vmIntrinsics::_StringBuilder_int: 1.324 + case vmIntrinsics::_StringBuilder_String: 1.325 + case vmIntrinsics::_StringBuilder_append_char: 1.326 + case vmIntrinsics::_StringBuilder_append_int: 1.327 + case vmIntrinsics::_StringBuilder_append_String: 1.328 + case vmIntrinsics::_StringBuilder_toString: 1.329 + case vmIntrinsics::_StringBuffer_void: 1.330 + case vmIntrinsics::_StringBuffer_int: 1.331 + case vmIntrinsics::_StringBuffer_String: 1.332 + case vmIntrinsics::_StringBuffer_append_char: 1.333 + case vmIntrinsics::_StringBuffer_append_int: 1.334 + case vmIntrinsics::_StringBuffer_append_String: 1.335 + case vmIntrinsics::_StringBuffer_toString: 1.336 + case vmIntrinsics::_Integer_toString: 1.337 + return true; 1.338 + 1.339 + case vmIntrinsics::_String_String: 1.340 + { 1.341 + Node* receiver = jvms->map()->in(jvms->argoff() + 1); 1.342 + if (receiver->is_Proj() && receiver->in(0)->is_CallStaticJava()) { 1.343 + CallStaticJavaNode* csj = receiver->in(0)->as_CallStaticJava(); 1.344 + ciMethod* m = csj->method(); 1.345 + if (m != NULL && 1.346 + (m->intrinsic_id() == vmIntrinsics::_StringBuffer_toString || 1.347 + m->intrinsic_id() == vmIntrinsics::_StringBuilder_toString)) 1.348 + // Delay String.<init>(new SB()) 1.349 + return true; 1.350 + } 1.351 + return false; 1.352 + } 1.353 + 1.354 + default: 1.355 + return false; 1.356 + } 1.357 + } 1.358 + return false; 1.359 +} 1.360 + 1.361 +bool Compile::should_delay_boxing_inlining(ciMethod* call_method, JVMState* jvms) { 1.362 + if (eliminate_boxing() && call_method->is_boxing_method()) { 1.363 + set_has_boxed_value(true); 1.364 + return aggressive_unboxing(); 1.365 + } 1.366 + return false; 1.367 +} 1.368 + 1.369 +// uncommon-trap call-sites where callee is unloaded, uninitialized or will not link 1.370 +bool Parse::can_not_compile_call_site(ciMethod *dest_method, ciInstanceKlass* klass) { 1.371 + // Additional inputs to consider... 1.372 + // bc = bc() 1.373 + // caller = method() 1.374 + // iter().get_method_holder_index() 1.375 + assert( dest_method->is_loaded(), "ciTypeFlow should not let us get here" ); 1.376 + // Interface classes can be loaded & linked and never get around to 1.377 + // being initialized. Uncommon-trap for not-initialized static or 1.378 + // v-calls. Let interface calls happen. 1.379 + ciInstanceKlass* holder_klass = dest_method->holder(); 1.380 + if (!holder_klass->is_being_initialized() && 1.381 + !holder_klass->is_initialized() && 1.382 + !holder_klass->is_interface()) { 1.383 + uncommon_trap(Deoptimization::Reason_uninitialized, 1.384 + Deoptimization::Action_reinterpret, 1.385 + holder_klass); 1.386 + return true; 1.387 + } 1.388 + 1.389 + assert(dest_method->is_loaded(), "dest_method: typeflow responsibility"); 1.390 + return false; 1.391 +} 1.392 + 1.393 + 1.394 +//------------------------------do_call---------------------------------------- 1.395 +// Handle your basic call. Inline if we can & want to, else just setup call. 1.396 +void Parse::do_call() { 1.397 + // It's likely we are going to add debug info soon. 1.398 + // Also, if we inline a guy who eventually needs debug info for this JVMS, 1.399 + // our contribution to it is cleaned up right here. 1.400 + kill_dead_locals(); 1.401 + 1.402 + // Set frequently used booleans 1.403 + const bool is_virtual = bc() == Bytecodes::_invokevirtual; 1.404 + const bool is_virtual_or_interface = is_virtual || bc() == Bytecodes::_invokeinterface; 1.405 + const bool has_receiver = Bytecodes::has_receiver(bc()); 1.406 + 1.407 + // Find target being called 1.408 + bool will_link; 1.409 + ciSignature* declared_signature = NULL; 1.410 + ciMethod* orig_callee = iter().get_method(will_link, &declared_signature); // callee in the bytecode 1.411 + ciInstanceKlass* holder_klass = orig_callee->holder(); 1.412 + ciKlass* holder = iter().get_declared_method_holder(); 1.413 + ciInstanceKlass* klass = ciEnv::get_instance_klass_for_declared_method_holder(holder); 1.414 + assert(declared_signature != NULL, "cannot be null"); 1.415 + 1.416 + // uncommon-trap when callee is unloaded, uninitialized or will not link 1.417 + // bailout when too many arguments for register representation 1.418 + if (!will_link || can_not_compile_call_site(orig_callee, klass)) { 1.419 +#ifndef PRODUCT 1.420 + if (PrintOpto && (Verbose || WizardMode)) { 1.421 + method()->print_name(); tty->print_cr(" can not compile call at bci %d to:", bci()); 1.422 + orig_callee->print_name(); tty->cr(); 1.423 + } 1.424 +#endif 1.425 + return; 1.426 + } 1.427 + assert(holder_klass->is_loaded(), ""); 1.428 + //assert((bc_callee->is_static() || is_invokedynamic) == !has_receiver , "must match bc"); // XXX invokehandle (cur_bc_raw) 1.429 + // Note: this takes into account invokeinterface of methods declared in java/lang/Object, 1.430 + // which should be invokevirtuals but according to the VM spec may be invokeinterfaces 1.431 + assert(holder_klass->is_interface() || holder_klass->super() == NULL || (bc() != Bytecodes::_invokeinterface), "must match bc"); 1.432 + // Note: In the absence of miranda methods, an abstract class K can perform 1.433 + // an invokevirtual directly on an interface method I.m if K implements I. 1.434 + 1.435 + // orig_callee is the resolved callee which's signature includes the 1.436 + // appendix argument. 1.437 + const int nargs = orig_callee->arg_size(); 1.438 + const bool is_signature_polymorphic = MethodHandles::is_signature_polymorphic(orig_callee->intrinsic_id()); 1.439 + 1.440 + // Push appendix argument (MethodType, CallSite, etc.), if one. 1.441 + if (iter().has_appendix()) { 1.442 + ciObject* appendix_arg = iter().get_appendix(); 1.443 + const TypeOopPtr* appendix_arg_type = TypeOopPtr::make_from_constant(appendix_arg); 1.444 + Node* appendix_arg_node = _gvn.makecon(appendix_arg_type); 1.445 + push(appendix_arg_node); 1.446 + } 1.447 + 1.448 + // --------------------- 1.449 + // Does Class Hierarchy Analysis reveal only a single target of a v-call? 1.450 + // Then we may inline or make a static call, but become dependent on there being only 1 target. 1.451 + // Does the call-site type profile reveal only one receiver? 1.452 + // Then we may introduce a run-time check and inline on the path where it succeeds. 1.453 + // The other path may uncommon_trap, check for another receiver, or do a v-call. 1.454 + 1.455 + // Try to get the most accurate receiver type 1.456 + ciMethod* callee = orig_callee; 1.457 + int vtable_index = Method::invalid_vtable_index; 1.458 + bool call_does_dispatch = false; 1.459 + 1.460 + // Speculative type of the receiver if any 1.461 + ciKlass* speculative_receiver_type = NULL; 1.462 + if (is_virtual_or_interface) { 1.463 + Node* receiver_node = stack(sp() - nargs); 1.464 + const TypeOopPtr* receiver_type = _gvn.type(receiver_node)->isa_oopptr(); 1.465 + // call_does_dispatch and vtable_index are out-parameters. They might be changed. 1.466 + // For arrays, klass below is Object. When vtable calls are used, 1.467 + // resolving the call with Object would allow an illegal call to 1.468 + // finalize() on an array. We use holder instead: illegal calls to 1.469 + // finalize() won't be compiled as vtable calls (IC call 1.470 + // resolution will catch the illegal call) and the few legal calls 1.471 + // on array types won't be either. 1.472 + callee = C->optimize_virtual_call(method(), bci(), klass, holder, orig_callee, 1.473 + receiver_type, is_virtual, 1.474 + call_does_dispatch, vtable_index); // out-parameters 1.475 + speculative_receiver_type = receiver_type != NULL ? receiver_type->speculative_type() : NULL; 1.476 + } 1.477 + 1.478 + // Note: It's OK to try to inline a virtual call. 1.479 + // The call generator will not attempt to inline a polymorphic call 1.480 + // unless it knows how to optimize the receiver dispatch. 1.481 + bool try_inline = (C->do_inlining() || InlineAccessors); 1.482 + 1.483 + // --------------------- 1.484 + dec_sp(nargs); // Temporarily pop args for JVM state of call 1.485 + JVMState* jvms = sync_jvms(); 1.486 + 1.487 + // --------------------- 1.488 + // Decide call tactic. 1.489 + // This call checks with CHA, the interpreter profile, intrinsics table, etc. 1.490 + // It decides whether inlining is desirable or not. 1.491 + CallGenerator* cg = C->call_generator(callee, vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type); 1.492 + 1.493 + // NOTE: Don't use orig_callee and callee after this point! Use cg->method() instead. 1.494 + orig_callee = callee = NULL; 1.495 + 1.496 + // --------------------- 1.497 + // Round double arguments before call 1.498 + round_double_arguments(cg->method()); 1.499 + 1.500 + // Feed profiling data for arguments to the type system so it can 1.501 + // propagate it as speculative types 1.502 + record_profiled_arguments_for_speculation(cg->method(), bc()); 1.503 + 1.504 +#ifndef PRODUCT 1.505 + // bump global counters for calls 1.506 + count_compiled_calls(/*at_method_entry*/ false, cg->is_inline()); 1.507 + 1.508 + // Record first part of parsing work for this call 1.509 + parse_histogram()->record_change(); 1.510 +#endif // not PRODUCT 1.511 + 1.512 + assert(jvms == this->jvms(), "still operating on the right JVMS"); 1.513 + assert(jvms_in_sync(), "jvms must carry full info into CG"); 1.514 + 1.515 + // save across call, for a subsequent cast_not_null. 1.516 + Node* receiver = has_receiver ? argument(0) : NULL; 1.517 + 1.518 + // The extra CheckCastPP for speculative types mess with PhaseStringOpts 1.519 + if (receiver != NULL && !call_does_dispatch && !cg->is_string_late_inline()) { 1.520 + // Feed profiling data for a single receiver to the type system so 1.521 + // it can propagate it as a speculative type 1.522 + receiver = record_profiled_receiver_for_speculation(receiver); 1.523 + } 1.524 + 1.525 + // Bump method data counters (We profile *before* the call is made 1.526 + // because exceptions don't return to the call site.) 1.527 + profile_call(receiver); 1.528 + 1.529 + JVMState* new_jvms = cg->generate(jvms, this); 1.530 + if (new_jvms == NULL) { 1.531 + // When inlining attempt fails (e.g., too many arguments), 1.532 + // it may contaminate the current compile state, making it 1.533 + // impossible to pull back and try again. Once we call 1.534 + // cg->generate(), we are committed. If it fails, the whole 1.535 + // compilation task is compromised. 1.536 + if (failing()) return; 1.537 + 1.538 + // This can happen if a library intrinsic is available, but refuses 1.539 + // the call site, perhaps because it did not match a pattern the 1.540 + // intrinsic was expecting to optimize. Should always be possible to 1.541 + // get a normal java call that may inline in that case 1.542 + cg = C->call_generator(cg->method(), vtable_index, call_does_dispatch, jvms, try_inline, prof_factor(), speculative_receiver_type, /* allow_intrinsics= */ false); 1.543 + if ((new_jvms = cg->generate(jvms, this)) == NULL) { 1.544 + guarantee(failing(), "call failed to generate: calls should work"); 1.545 + return; 1.546 + } 1.547 + } 1.548 + 1.549 + if (cg->is_inline()) { 1.550 + // Accumulate has_loops estimate 1.551 + C->set_has_loops(C->has_loops() || cg->method()->has_loops()); 1.552 + C->env()->notice_inlined_method(cg->method()); 1.553 + } 1.554 + 1.555 + // Reset parser state from [new_]jvms, which now carries results of the call. 1.556 + // Return value (if any) is already pushed on the stack by the cg. 1.557 + add_exception_states_from(new_jvms); 1.558 + if (new_jvms->map()->control() == top()) { 1.559 + stop_and_kill_map(); 1.560 + } else { 1.561 + assert(new_jvms->same_calls_as(jvms), "method/bci left unchanged"); 1.562 + set_jvms(new_jvms); 1.563 + } 1.564 + 1.565 + if (!stopped()) { 1.566 + // This was some sort of virtual call, which did a null check for us. 1.567 + // Now we can assert receiver-not-null, on the normal return path. 1.568 + if (receiver != NULL && cg->is_virtual()) { 1.569 + Node* cast = cast_not_null(receiver); 1.570 + // %%% assert(receiver == cast, "should already have cast the receiver"); 1.571 + } 1.572 + 1.573 + // Round double result after a call from strict to non-strict code 1.574 + round_double_result(cg->method()); 1.575 + 1.576 + ciType* rtype = cg->method()->return_type(); 1.577 + ciType* ctype = declared_signature->return_type(); 1.578 + 1.579 + if (Bytecodes::has_optional_appendix(iter().cur_bc_raw()) || is_signature_polymorphic) { 1.580 + // Be careful here with return types. 1.581 + if (ctype != rtype) { 1.582 + BasicType rt = rtype->basic_type(); 1.583 + BasicType ct = ctype->basic_type(); 1.584 + if (ct == T_VOID) { 1.585 + // It's OK for a method to return a value that is discarded. 1.586 + // The discarding does not require any special action from the caller. 1.587 + // The Java code knows this, at VerifyType.isNullConversion. 1.588 + pop_node(rt); // whatever it was, pop it 1.589 + } else if (rt == T_INT || is_subword_type(rt)) { 1.590 + // Nothing. These cases are handled in lambda form bytecode. 1.591 + assert(ct == T_INT || is_subword_type(ct), err_msg_res("must match: rt=%s, ct=%s", type2name(rt), type2name(ct))); 1.592 + } else if (rt == T_OBJECT || rt == T_ARRAY) { 1.593 + assert(ct == T_OBJECT || ct == T_ARRAY, err_msg_res("rt=%s, ct=%s", type2name(rt), type2name(ct))); 1.594 + if (ctype->is_loaded()) { 1.595 + const TypeOopPtr* arg_type = TypeOopPtr::make_from_klass(rtype->as_klass()); 1.596 + const Type* sig_type = TypeOopPtr::make_from_klass(ctype->as_klass()); 1.597 + if (arg_type != NULL && !arg_type->higher_equal(sig_type)) { 1.598 + Node* retnode = pop(); 1.599 + Node* cast_obj = _gvn.transform(new (C) CheckCastPPNode(control(), retnode, sig_type)); 1.600 + push(cast_obj); 1.601 + } 1.602 + } 1.603 + } else { 1.604 + assert(rt == ct, err_msg_res("unexpected mismatch: rt=%s, ct=%s", type2name(rt), type2name(ct))); 1.605 + // push a zero; it's better than getting an oop/int mismatch 1.606 + pop_node(rt); 1.607 + Node* retnode = zerocon(ct); 1.608 + push_node(ct, retnode); 1.609 + } 1.610 + // Now that the value is well-behaved, continue with the call-site type. 1.611 + rtype = ctype; 1.612 + } 1.613 + } else { 1.614 + // Symbolic resolution enforces the types to be the same. 1.615 + // NOTE: We must relax the assert for unloaded types because two 1.616 + // different ciType instances of the same unloaded class type 1.617 + // can appear to be "loaded" by different loaders (depending on 1.618 + // the accessing class). 1.619 + assert(!rtype->is_loaded() || !ctype->is_loaded() || rtype == ctype, 1.620 + err_msg_res("mismatched return types: rtype=%s, ctype=%s", rtype->name(), ctype->name())); 1.621 + } 1.622 + 1.623 + // If the return type of the method is not loaded, assert that the 1.624 + // value we got is a null. Otherwise, we need to recompile. 1.625 + if (!rtype->is_loaded()) { 1.626 +#ifndef PRODUCT 1.627 + if (PrintOpto && (Verbose || WizardMode)) { 1.628 + method()->print_name(); tty->print_cr(" asserting nullness of result at bci: %d", bci()); 1.629 + cg->method()->print_name(); tty->cr(); 1.630 + } 1.631 +#endif 1.632 + if (C->log() != NULL) { 1.633 + C->log()->elem("assert_null reason='return' klass='%d'", 1.634 + C->log()->identify(rtype)); 1.635 + } 1.636 + // If there is going to be a trap, put it at the next bytecode: 1.637 + set_bci(iter().next_bci()); 1.638 + null_assert(peek()); 1.639 + set_bci(iter().cur_bci()); // put it back 1.640 + } 1.641 + BasicType ct = ctype->basic_type(); 1.642 + if (ct == T_OBJECT || ct == T_ARRAY) { 1.643 + ciKlass* better_type = method()->return_profiled_type(bci()); 1.644 + if (UseTypeSpeculation && better_type != NULL) { 1.645 + // If profiling reports a single type for the return value, 1.646 + // feed it to the type system so it can propagate it as a 1.647 + // speculative type 1.648 + record_profile_for_speculation(stack(sp()-1), better_type); 1.649 + } 1.650 + } 1.651 + } 1.652 + 1.653 + // Restart record of parsing work after possible inlining of call 1.654 +#ifndef PRODUCT 1.655 + parse_histogram()->set_initial_state(bc()); 1.656 +#endif 1.657 +} 1.658 + 1.659 +//---------------------------catch_call_exceptions----------------------------- 1.660 +// Put a Catch and CatchProj nodes behind a just-created call. 1.661 +// Send their caught exceptions to the proper handler. 1.662 +// This may be used after a call to the rethrow VM stub, 1.663 +// when it is needed to process unloaded exception classes. 1.664 +void Parse::catch_call_exceptions(ciExceptionHandlerStream& handlers) { 1.665 + // Exceptions are delivered through this channel: 1.666 + Node* i_o = this->i_o(); 1.667 + 1.668 + // Add a CatchNode. 1.669 + GrowableArray<int>* bcis = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, -1); 1.670 + GrowableArray<const Type*>* extypes = new (C->node_arena()) GrowableArray<const Type*>(C->node_arena(), 8, 0, NULL); 1.671 + GrowableArray<int>* saw_unloaded = new (C->node_arena()) GrowableArray<int>(C->node_arena(), 8, 0, 0); 1.672 + 1.673 + for (; !handlers.is_done(); handlers.next()) { 1.674 + ciExceptionHandler* h = handlers.handler(); 1.675 + int h_bci = h->handler_bci(); 1.676 + ciInstanceKlass* h_klass = h->is_catch_all() ? env()->Throwable_klass() : h->catch_klass(); 1.677 + // Do not introduce unloaded exception types into the graph: 1.678 + if (!h_klass->is_loaded()) { 1.679 + if (saw_unloaded->contains(h_bci)) { 1.680 + /* We've already seen an unloaded exception with h_bci, 1.681 + so don't duplicate. Duplication will cause the CatchNode to be 1.682 + unnecessarily large. See 4713716. */ 1.683 + continue; 1.684 + } else { 1.685 + saw_unloaded->append(h_bci); 1.686 + } 1.687 + } 1.688 + const Type* h_extype = TypeOopPtr::make_from_klass(h_klass); 1.689 + // (We use make_from_klass because it respects UseUniqueSubclasses.) 1.690 + h_extype = h_extype->join(TypeInstPtr::NOTNULL); 1.691 + assert(!h_extype->empty(), "sanity"); 1.692 + // Note: It's OK if the BCIs repeat themselves. 1.693 + bcis->append(h_bci); 1.694 + extypes->append(h_extype); 1.695 + } 1.696 + 1.697 + int len = bcis->length(); 1.698 + CatchNode *cn = new (C) CatchNode(control(), i_o, len+1); 1.699 + Node *catch_ = _gvn.transform(cn); 1.700 + 1.701 + // now branch with the exception state to each of the (potential) 1.702 + // handlers 1.703 + for(int i=0; i < len; i++) { 1.704 + // Setup JVM state to enter the handler. 1.705 + PreserveJVMState pjvms(this); 1.706 + // Locals are just copied from before the call. 1.707 + // Get control from the CatchNode. 1.708 + int handler_bci = bcis->at(i); 1.709 + Node* ctrl = _gvn.transform( new (C) CatchProjNode(catch_, i+1,handler_bci)); 1.710 + // This handler cannot happen? 1.711 + if (ctrl == top()) continue; 1.712 + set_control(ctrl); 1.713 + 1.714 + // Create exception oop 1.715 + const TypeInstPtr* extype = extypes->at(i)->is_instptr(); 1.716 + Node *ex_oop = _gvn.transform(new (C) CreateExNode(extypes->at(i), ctrl, i_o)); 1.717 + 1.718 + // Handle unloaded exception classes. 1.719 + if (saw_unloaded->contains(handler_bci)) { 1.720 + // An unloaded exception type is coming here. Do an uncommon trap. 1.721 +#ifndef PRODUCT 1.722 + // We do not expect the same handler bci to take both cold unloaded 1.723 + // and hot loaded exceptions. But, watch for it. 1.724 + if ((Verbose || WizardMode) && extype->is_loaded()) { 1.725 + tty->print("Warning: Handler @%d takes mixed loaded/unloaded exceptions in ", bci()); 1.726 + method()->print_name(); tty->cr(); 1.727 + } else if (PrintOpto && (Verbose || WizardMode)) { 1.728 + tty->print("Bailing out on unloaded exception type "); 1.729 + extype->klass()->print_name(); 1.730 + tty->print(" at bci:%d in ", bci()); 1.731 + method()->print_name(); tty->cr(); 1.732 + } 1.733 +#endif 1.734 + // Emit an uncommon trap instead of processing the block. 1.735 + set_bci(handler_bci); 1.736 + push_ex_oop(ex_oop); 1.737 + uncommon_trap(Deoptimization::Reason_unloaded, 1.738 + Deoptimization::Action_reinterpret, 1.739 + extype->klass(), "!loaded exception"); 1.740 + set_bci(iter().cur_bci()); // put it back 1.741 + continue; 1.742 + } 1.743 + 1.744 + // go to the exception handler 1.745 + if (handler_bci < 0) { // merge with corresponding rethrow node 1.746 + throw_to_exit(make_exception_state(ex_oop)); 1.747 + } else { // Else jump to corresponding handle 1.748 + push_ex_oop(ex_oop); // Clear stack and push just the oop. 1.749 + merge_exception(handler_bci); 1.750 + } 1.751 + } 1.752 + 1.753 + // The first CatchProj is for the normal return. 1.754 + // (Note: If this is a call to rethrow_Java, this node goes dead.) 1.755 + set_control(_gvn.transform( new (C) CatchProjNode(catch_, CatchProjNode::fall_through_index, CatchProjNode::no_handler_bci))); 1.756 +} 1.757 + 1.758 + 1.759 +//----------------------------catch_inline_exceptions-------------------------- 1.760 +// Handle all exceptions thrown by an inlined method or individual bytecode. 1.761 +// Common case 1: we have no handler, so all exceptions merge right into 1.762 +// the rethrow case. 1.763 +// Case 2: we have some handlers, with loaded exception klasses that have 1.764 +// no subklasses. We do a Deutsch-Shiffman style type-check on the incoming 1.765 +// exception oop and branch to the handler directly. 1.766 +// Case 3: We have some handlers with subklasses or are not loaded at 1.767 +// compile-time. We have to call the runtime to resolve the exception. 1.768 +// So we insert a RethrowCall and all the logic that goes with it. 1.769 +void Parse::catch_inline_exceptions(SafePointNode* ex_map) { 1.770 + // Caller is responsible for saving away the map for normal control flow! 1.771 + assert(stopped(), "call set_map(NULL) first"); 1.772 + assert(method()->has_exception_handlers(), "don't come here w/o work to do"); 1.773 + 1.774 + Node* ex_node = saved_ex_oop(ex_map); 1.775 + if (ex_node == top()) { 1.776 + // No action needed. 1.777 + return; 1.778 + } 1.779 + const TypeInstPtr* ex_type = _gvn.type(ex_node)->isa_instptr(); 1.780 + NOT_PRODUCT(if (ex_type==NULL) tty->print_cr("*** Exception not InstPtr")); 1.781 + if (ex_type == NULL) 1.782 + ex_type = TypeOopPtr::make_from_klass(env()->Throwable_klass())->is_instptr(); 1.783 + 1.784 + // determine potential exception handlers 1.785 + ciExceptionHandlerStream handlers(method(), bci(), 1.786 + ex_type->klass()->as_instance_klass(), 1.787 + ex_type->klass_is_exact()); 1.788 + 1.789 + // Start executing from the given throw state. (Keep its stack, for now.) 1.790 + // Get the exception oop as known at compile time. 1.791 + ex_node = use_exception_state(ex_map); 1.792 + 1.793 + // Get the exception oop klass from its header 1.794 + Node* ex_klass_node = NULL; 1.795 + if (has_ex_handler() && !ex_type->klass_is_exact()) { 1.796 + Node* p = basic_plus_adr( ex_node, ex_node, oopDesc::klass_offset_in_bytes()); 1.797 + ex_klass_node = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS, TypeKlassPtr::OBJECT) ); 1.798 + 1.799 + // Compute the exception klass a little more cleverly. 1.800 + // Obvious solution is to simple do a LoadKlass from the 'ex_node'. 1.801 + // However, if the ex_node is a PhiNode, I'm going to do a LoadKlass for 1.802 + // each arm of the Phi. If I know something clever about the exceptions 1.803 + // I'm loading the class from, I can replace the LoadKlass with the 1.804 + // klass constant for the exception oop. 1.805 + if( ex_node->is_Phi() ) { 1.806 + ex_klass_node = new (C) PhiNode( ex_node->in(0), TypeKlassPtr::OBJECT ); 1.807 + for( uint i = 1; i < ex_node->req(); i++ ) { 1.808 + Node* p = basic_plus_adr( ex_node->in(i), ex_node->in(i), oopDesc::klass_offset_in_bytes() ); 1.809 + Node* k = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), p, TypeInstPtr::KLASS, TypeKlassPtr::OBJECT) ); 1.810 + ex_klass_node->init_req( i, k ); 1.811 + } 1.812 + _gvn.set_type(ex_klass_node, TypeKlassPtr::OBJECT); 1.813 + 1.814 + } 1.815 + } 1.816 + 1.817 + // Scan the exception table for applicable handlers. 1.818 + // If none, we can call rethrow() and be done! 1.819 + // If precise (loaded with no subklasses), insert a D.S. style 1.820 + // pointer compare to the correct handler and loop back. 1.821 + // If imprecise, switch to the Rethrow VM-call style handling. 1.822 + 1.823 + int remaining = handlers.count_remaining(); 1.824 + 1.825 + // iterate through all entries sequentially 1.826 + for (;!handlers.is_done(); handlers.next()) { 1.827 + ciExceptionHandler* handler = handlers.handler(); 1.828 + 1.829 + if (handler->is_rethrow()) { 1.830 + // If we fell off the end of the table without finding an imprecise 1.831 + // exception klass (and without finding a generic handler) then we 1.832 + // know this exception is not handled in this method. We just rethrow 1.833 + // the exception into the caller. 1.834 + throw_to_exit(make_exception_state(ex_node)); 1.835 + return; 1.836 + } 1.837 + 1.838 + // exception handler bci range covers throw_bci => investigate further 1.839 + int handler_bci = handler->handler_bci(); 1.840 + 1.841 + if (remaining == 1) { 1.842 + push_ex_oop(ex_node); // Push exception oop for handler 1.843 +#ifndef PRODUCT 1.844 + if (PrintOpto && WizardMode) { 1.845 + tty->print_cr(" Catching every inline exception bci:%d -> handler_bci:%d", bci(), handler_bci); 1.846 + } 1.847 +#endif 1.848 + merge_exception(handler_bci); // jump to handler 1.849 + return; // No more handling to be done here! 1.850 + } 1.851 + 1.852 + // Get the handler's klass 1.853 + ciInstanceKlass* klass = handler->catch_klass(); 1.854 + 1.855 + if (!klass->is_loaded()) { // klass is not loaded? 1.856 + // fall through into catch_call_exceptions which will emit a 1.857 + // handler with an uncommon trap. 1.858 + break; 1.859 + } 1.860 + 1.861 + if (klass->is_interface()) // should not happen, but... 1.862 + break; // bail out 1.863 + 1.864 + // Check the type of the exception against the catch type 1.865 + const TypeKlassPtr *tk = TypeKlassPtr::make(klass); 1.866 + Node* con = _gvn.makecon(tk); 1.867 + Node* not_subtype_ctrl = gen_subtype_check(ex_klass_node, con); 1.868 + if (!stopped()) { 1.869 + PreserveJVMState pjvms(this); 1.870 + const TypeInstPtr* tinst = TypeOopPtr::make_from_klass_unique(klass)->cast_to_ptr_type(TypePtr::NotNull)->is_instptr(); 1.871 + assert(klass->has_subklass() || tinst->klass_is_exact(), "lost exactness"); 1.872 + Node* ex_oop = _gvn.transform(new (C) CheckCastPPNode(control(), ex_node, tinst)); 1.873 + push_ex_oop(ex_oop); // Push exception oop for handler 1.874 +#ifndef PRODUCT 1.875 + if (PrintOpto && WizardMode) { 1.876 + tty->print(" Catching inline exception bci:%d -> handler_bci:%d -- ", bci(), handler_bci); 1.877 + klass->print_name(); 1.878 + tty->cr(); 1.879 + } 1.880 +#endif 1.881 + merge_exception(handler_bci); 1.882 + } 1.883 + set_control(not_subtype_ctrl); 1.884 + 1.885 + // Come here if exception does not match handler. 1.886 + // Carry on with more handler checks. 1.887 + --remaining; 1.888 + } 1.889 + 1.890 + assert(!stopped(), "you should return if you finish the chain"); 1.891 + 1.892 + // Oops, need to call into the VM to resolve the klasses at runtime. 1.893 + // Note: This call must not deoptimize, since it is not a real at this bci! 1.894 + kill_dead_locals(); 1.895 + 1.896 + make_runtime_call(RC_NO_LEAF | RC_MUST_THROW, 1.897 + OptoRuntime::rethrow_Type(), 1.898 + OptoRuntime::rethrow_stub(), 1.899 + NULL, NULL, 1.900 + ex_node); 1.901 + 1.902 + // Rethrow is a pure call, no side effects, only a result. 1.903 + // The result cannot be allocated, so we use I_O 1.904 + 1.905 + // Catch exceptions from the rethrow 1.906 + catch_call_exceptions(handlers); 1.907 +} 1.908 + 1.909 + 1.910 +// (Note: Moved add_debug_info into GraphKit::add_safepoint_edges.) 1.911 + 1.912 + 1.913 +#ifndef PRODUCT 1.914 +void Parse::count_compiled_calls(bool at_method_entry, bool is_inline) { 1.915 + if( CountCompiledCalls ) { 1.916 + if( at_method_entry ) { 1.917 + // bump invocation counter if top method (for statistics) 1.918 + if (CountCompiledCalls && depth() == 1) { 1.919 + const TypePtr* addr_type = TypeMetadataPtr::make(method()); 1.920 + Node* adr1 = makecon(addr_type); 1.921 + Node* adr2 = basic_plus_adr(adr1, adr1, in_bytes(Method::compiled_invocation_counter_offset())); 1.922 + increment_counter(adr2); 1.923 + } 1.924 + } else if (is_inline) { 1.925 + switch (bc()) { 1.926 + case Bytecodes::_invokevirtual: increment_counter(SharedRuntime::nof_inlined_calls_addr()); break; 1.927 + case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_inlined_interface_calls_addr()); break; 1.928 + case Bytecodes::_invokestatic: 1.929 + case Bytecodes::_invokedynamic: 1.930 + case Bytecodes::_invokespecial: increment_counter(SharedRuntime::nof_inlined_static_calls_addr()); break; 1.931 + default: fatal("unexpected call bytecode"); 1.932 + } 1.933 + } else { 1.934 + switch (bc()) { 1.935 + case Bytecodes::_invokevirtual: increment_counter(SharedRuntime::nof_normal_calls_addr()); break; 1.936 + case Bytecodes::_invokeinterface: increment_counter(SharedRuntime::nof_interface_calls_addr()); break; 1.937 + case Bytecodes::_invokestatic: 1.938 + case Bytecodes::_invokedynamic: 1.939 + case Bytecodes::_invokespecial: increment_counter(SharedRuntime::nof_static_calls_addr()); break; 1.940 + default: fatal("unexpected call bytecode"); 1.941 + } 1.942 + } 1.943 + } 1.944 +} 1.945 +#endif //PRODUCT 1.946 + 1.947 + 1.948 +ciMethod* Compile::optimize_virtual_call(ciMethod* caller, int bci, ciInstanceKlass* klass, 1.949 + ciKlass* holder, ciMethod* callee, 1.950 + const TypeOopPtr* receiver_type, bool is_virtual, 1.951 + bool& call_does_dispatch, int& vtable_index) { 1.952 + // Set default values for out-parameters. 1.953 + call_does_dispatch = true; 1.954 + vtable_index = Method::invalid_vtable_index; 1.955 + 1.956 + // Choose call strategy. 1.957 + ciMethod* optimized_virtual_method = optimize_inlining(caller, bci, klass, callee, receiver_type); 1.958 + 1.959 + // Have the call been sufficiently improved such that it is no longer a virtual? 1.960 + if (optimized_virtual_method != NULL) { 1.961 + callee = optimized_virtual_method; 1.962 + call_does_dispatch = false; 1.963 + } else if (!UseInlineCaches && is_virtual && callee->is_loaded()) { 1.964 + // We can make a vtable call at this site 1.965 + vtable_index = callee->resolve_vtable_index(caller->holder(), holder); 1.966 + } 1.967 + return callee; 1.968 +} 1.969 + 1.970 +// Identify possible target method and inlining style 1.971 +ciMethod* Compile::optimize_inlining(ciMethod* caller, int bci, ciInstanceKlass* klass, 1.972 + ciMethod* callee, const TypeOopPtr* receiver_type) { 1.973 + // only use for virtual or interface calls 1.974 + 1.975 + // If it is obviously final, do not bother to call find_monomorphic_target, 1.976 + // because the class hierarchy checks are not needed, and may fail due to 1.977 + // incompletely loaded classes. Since we do our own class loading checks 1.978 + // in this module, we may confidently bind to any method. 1.979 + if (callee->can_be_statically_bound()) { 1.980 + return callee; 1.981 + } 1.982 + 1.983 + // Attempt to improve the receiver 1.984 + bool actual_receiver_is_exact = false; 1.985 + ciInstanceKlass* actual_receiver = klass; 1.986 + if (receiver_type != NULL) { 1.987 + // Array methods are all inherited from Object, and are monomorphic. 1.988 + // finalize() call on array is not allowed. 1.989 + if (receiver_type->isa_aryptr() && 1.990 + callee->holder() == env()->Object_klass() && 1.991 + callee->name() != ciSymbol::finalize_method_name()) { 1.992 + return callee; 1.993 + } 1.994 + 1.995 + // All other interesting cases are instance klasses. 1.996 + if (!receiver_type->isa_instptr()) { 1.997 + return NULL; 1.998 + } 1.999 + 1.1000 + ciInstanceKlass *ikl = receiver_type->klass()->as_instance_klass(); 1.1001 + if (ikl->is_loaded() && ikl->is_initialized() && !ikl->is_interface() && 1.1002 + (ikl == actual_receiver || ikl->is_subtype_of(actual_receiver))) { 1.1003 + // ikl is a same or better type than the original actual_receiver, 1.1004 + // e.g. static receiver from bytecodes. 1.1005 + actual_receiver = ikl; 1.1006 + // Is the actual_receiver exact? 1.1007 + actual_receiver_is_exact = receiver_type->klass_is_exact(); 1.1008 + } 1.1009 + } 1.1010 + 1.1011 + ciInstanceKlass* calling_klass = caller->holder(); 1.1012 + ciMethod* cha_monomorphic_target = callee->find_monomorphic_target(calling_klass, klass, actual_receiver); 1.1013 + if (cha_monomorphic_target != NULL) { 1.1014 + assert(!cha_monomorphic_target->is_abstract(), ""); 1.1015 + // Look at the method-receiver type. Does it add "too much information"? 1.1016 + ciKlass* mr_klass = cha_monomorphic_target->holder(); 1.1017 + const Type* mr_type = TypeInstPtr::make(TypePtr::BotPTR, mr_klass); 1.1018 + if (receiver_type == NULL || !receiver_type->higher_equal(mr_type)) { 1.1019 + // Calling this method would include an implicit cast to its holder. 1.1020 + // %%% Not yet implemented. Would throw minor asserts at present. 1.1021 + // %%% The most common wins are already gained by +UseUniqueSubclasses. 1.1022 + // To fix, put the higher_equal check at the call of this routine, 1.1023 + // and add a CheckCastPP to the receiver. 1.1024 + if (TraceDependencies) { 1.1025 + tty->print_cr("found unique CHA method, but could not cast up"); 1.1026 + tty->print(" method = "); 1.1027 + cha_monomorphic_target->print(); 1.1028 + tty->cr(); 1.1029 + } 1.1030 + if (log() != NULL) { 1.1031 + log()->elem("missed_CHA_opportunity klass='%d' method='%d'", 1.1032 + log()->identify(klass), 1.1033 + log()->identify(cha_monomorphic_target)); 1.1034 + } 1.1035 + cha_monomorphic_target = NULL; 1.1036 + } 1.1037 + } 1.1038 + if (cha_monomorphic_target != NULL) { 1.1039 + // Hardwiring a virtual. 1.1040 + // If we inlined because CHA revealed only a single target method, 1.1041 + // then we are dependent on that target method not getting overridden 1.1042 + // by dynamic class loading. Be sure to test the "static" receiver 1.1043 + // dest_method here, as opposed to the actual receiver, which may 1.1044 + // falsely lead us to believe that the receiver is final or private. 1.1045 + dependencies()->assert_unique_concrete_method(actual_receiver, cha_monomorphic_target); 1.1046 + return cha_monomorphic_target; 1.1047 + } 1.1048 + 1.1049 + // If the type is exact, we can still bind the method w/o a vcall. 1.1050 + // (This case comes after CHA so we can see how much extra work it does.) 1.1051 + if (actual_receiver_is_exact) { 1.1052 + // In case of evolution, there is a dependence on every inlined method, since each 1.1053 + // such method can be changed when its class is redefined. 1.1054 + ciMethod* exact_method = callee->resolve_invoke(calling_klass, actual_receiver); 1.1055 + if (exact_method != NULL) { 1.1056 +#ifndef PRODUCT 1.1057 + if (PrintOpto) { 1.1058 + tty->print(" Calling method via exact type @%d --- ", bci); 1.1059 + exact_method->print_name(); 1.1060 + tty->cr(); 1.1061 + } 1.1062 +#endif 1.1063 + return exact_method; 1.1064 + } 1.1065 + } 1.1066 + 1.1067 + return NULL; 1.1068 +}