1.1 --- a/src/share/vm/opto/graphKit.cpp Wed Oct 23 10:00:39 2013 +0200 1.2 +++ b/src/share/vm/opto/graphKit.cpp Wed Oct 23 12:40:23 2013 +0200 1.3 @@ -2098,6 +2098,104 @@ 1.4 } 1.5 } 1.6 1.7 +/** 1.8 + * Record profiling data exact_kls for Node n with the type system so 1.9 + * that it can propagate it (speculation) 1.10 + * 1.11 + * @param n node that the type applies to 1.12 + * @param exact_kls type from profiling 1.13 + * 1.14 + * @return node with improved type 1.15 + */ 1.16 +Node* GraphKit::record_profile_for_speculation(Node* n, ciKlass* exact_kls) { 1.17 + const TypeOopPtr* current_type = _gvn.type(n)->isa_oopptr(); 1.18 + assert(UseTypeSpeculation, "type speculation must be on"); 1.19 + if (exact_kls != NULL && 1.20 + // nothing to improve if type is already exact 1.21 + (current_type == NULL || 1.22 + (!current_type->klass_is_exact() && 1.23 + (current_type->speculative() == NULL || 1.24 + !current_type->speculative()->klass_is_exact())))) { 1.25 + const TypeKlassPtr* tklass = TypeKlassPtr::make(exact_kls); 1.26 + const TypeOopPtr* xtype = tklass->as_instance_type(); 1.27 + assert(xtype->klass_is_exact(), "Should be exact"); 1.28 + 1.29 + // Build a type with a speculative type (what we think we know 1.30 + // about the type but will need a guard when we use it) 1.31 + const TypeOopPtr* spec_type = TypeOopPtr::make(TypePtr::BotPTR, Type::OffsetBot, TypeOopPtr::InstanceBot, xtype); 1.32 + // We're changing the type, we need a new cast node to carry the 1.33 + // new type. The new type depends on the control: what profiling 1.34 + // tells us is only valid from here as far as we can tell. 1.35 + Node* cast = new(C) CastPPNode(n, spec_type); 1.36 + cast->init_req(0, control()); 1.37 + cast = _gvn.transform(cast); 1.38 + replace_in_map(n, cast); 1.39 + n = cast; 1.40 + } 1.41 + return n; 1.42 +} 1.43 + 1.44 +/** 1.45 + * Record profiling data from receiver profiling at an invoke with the 1.46 + * type system so that it can propagate it (speculation) 1.47 + * 1.48 + * @param n receiver node 1.49 + * 1.50 + * @return node with improved type 1.51 + */ 1.52 +Node* GraphKit::record_profiled_receiver_for_speculation(Node* n) { 1.53 + if (!UseTypeSpeculation) { 1.54 + return n; 1.55 + } 1.56 + ciKlass* exact_kls = profile_has_unique_klass(); 1.57 + return record_profile_for_speculation(n, exact_kls); 1.58 +} 1.59 + 1.60 +/** 1.61 + * Record profiling data from argument profiling at an invoke with the 1.62 + * type system so that it can propagate it (speculation) 1.63 + * 1.64 + * @param dest_method target method for the call 1.65 + * @param bc what invoke bytecode is this? 1.66 + */ 1.67 +void GraphKit::record_profiled_arguments_for_speculation(ciMethod* dest_method, Bytecodes::Code bc) { 1.68 + if (!UseTypeSpeculation) { 1.69 + return; 1.70 + } 1.71 + const TypeFunc* tf = TypeFunc::make(dest_method); 1.72 + int nargs = tf->_domain->_cnt - TypeFunc::Parms; 1.73 + int skip = Bytecodes::has_receiver(bc) ? 1 : 0; 1.74 + for (int j = skip, i = 0; j < nargs && i < TypeProfileArgsLimit; j++) { 1.75 + const Type *targ = tf->_domain->field_at(j + TypeFunc::Parms); 1.76 + if (targ->basic_type() == T_OBJECT || targ->basic_type() == T_ARRAY) { 1.77 + ciKlass* better_type = method()->argument_profiled_type(bci(), i); 1.78 + if (better_type != NULL) { 1.79 + record_profile_for_speculation(argument(j), better_type); 1.80 + } 1.81 + i++; 1.82 + } 1.83 + } 1.84 +} 1.85 + 1.86 +/** 1.87 + * Record profiling data from parameter profiling at an invoke with 1.88 + * the type system so that it can propagate it (speculation) 1.89 + */ 1.90 +void GraphKit::record_profiled_parameters_for_speculation() { 1.91 + if (!UseTypeSpeculation) { 1.92 + return; 1.93 + } 1.94 + for (int i = 0, j = 0; i < method()->arg_size() ; i++) { 1.95 + if (_gvn.type(local(i))->isa_oopptr()) { 1.96 + ciKlass* better_type = method()->parameter_profiled_type(j); 1.97 + if (better_type != NULL) { 1.98 + record_profile_for_speculation(local(i), better_type); 1.99 + } 1.100 + j++; 1.101 + } 1.102 + } 1.103 +} 1.104 + 1.105 void GraphKit::round_double_result(ciMethod* dest_method) { 1.106 // A non-strict method may return a double value which has an extended 1.107 // exponent, but this must not be visible in a caller which is 'strict' 1.108 @@ -2635,10 +2733,10 @@ 1.109 // If the profile has seen exactly one type, narrow to exactly that type. 1.110 // Subsequent type checks will always fold up. 1.111 Node* GraphKit::maybe_cast_profiled_receiver(Node* not_null_obj, 1.112 - ciProfileData* data, 1.113 - ciKlass* require_klass) { 1.114 + ciKlass* require_klass, 1.115 + ciKlass* spec_klass, 1.116 + bool safe_for_replace) { 1.117 if (!UseTypeProfile || !TypeProfileCasts) return NULL; 1.118 - if (data == NULL) return NULL; 1.119 1.120 // Make sure we haven't already deoptimized from this tactic. 1.121 if (too_many_traps(Deoptimization::Reason_class_check)) 1.122 @@ -2646,15 +2744,15 @@ 1.123 1.124 // (No, this isn't a call, but it's enough like a virtual call 1.125 // to use the same ciMethod accessor to get the profile info...) 1.126 - ciCallProfile profile = method()->call_profile_at_bci(bci()); 1.127 - if (profile.count() >= 0 && // no cast failures here 1.128 - profile.has_receiver(0) && 1.129 - profile.morphism() == 1) { 1.130 - ciKlass* exact_kls = profile.receiver(0); 1.131 + // If we have a speculative type use it instead of profiling (which 1.132 + // may not help us) 1.133 + ciKlass* exact_kls = spec_klass == NULL ? profile_has_unique_klass() : spec_klass; 1.134 + if (exact_kls != NULL) {// no cast failures here 1.135 if (require_klass == NULL || 1.136 static_subtype_check(require_klass, exact_kls) == SSC_always_true) { 1.137 - // If we narrow the type to match what the type profile sees, 1.138 - // we can then remove the rest of the cast. 1.139 + // If we narrow the type to match what the type profile sees or 1.140 + // the speculative type, we can then remove the rest of the 1.141 + // cast. 1.142 // This is a win, even if the exact_kls is very specific, 1.143 // because downstream operations, such as method calls, 1.144 // will often benefit from the sharper type. 1.145 @@ -2666,7 +2764,9 @@ 1.146 uncommon_trap(Deoptimization::Reason_class_check, 1.147 Deoptimization::Action_maybe_recompile); 1.148 } 1.149 - replace_in_map(not_null_obj, exact_obj); 1.150 + if (safe_for_replace) { 1.151 + replace_in_map(not_null_obj, exact_obj); 1.152 + } 1.153 return exact_obj; 1.154 } 1.155 // assert(ssc == SSC_always_true)... except maybe the profile lied to us. 1.156 @@ -2675,11 +2775,59 @@ 1.157 return NULL; 1.158 } 1.159 1.160 +/** 1.161 + * Cast obj to type and emit guard unless we had too many traps here 1.162 + * already 1.163 + * 1.164 + * @param obj node being casted 1.165 + * @param type type to cast the node to 1.166 + * @param not_null true if we know node cannot be null 1.167 + */ 1.168 +Node* GraphKit::maybe_cast_profiled_obj(Node* obj, 1.169 + ciKlass* type, 1.170 + bool not_null) { 1.171 + // type == NULL if profiling tells us this object is always null 1.172 + if (type != NULL) { 1.173 + if (!too_many_traps(Deoptimization::Reason_null_check) && 1.174 + !too_many_traps(Deoptimization::Reason_class_check)) { 1.175 + Node* not_null_obj = NULL; 1.176 + // not_null is true if we know the object is not null and 1.177 + // there's no need for a null check 1.178 + if (!not_null) { 1.179 + Node* null_ctl = top(); 1.180 + not_null_obj = null_check_oop(obj, &null_ctl, true, true); 1.181 + assert(null_ctl->is_top(), "no null control here"); 1.182 + } else { 1.183 + not_null_obj = obj; 1.184 + } 1.185 + 1.186 + Node* exact_obj = not_null_obj; 1.187 + ciKlass* exact_kls = type; 1.188 + Node* slow_ctl = type_check_receiver(exact_obj, exact_kls, 1.0, 1.189 + &exact_obj); 1.190 + { 1.191 + PreserveJVMState pjvms(this); 1.192 + set_control(slow_ctl); 1.193 + uncommon_trap(Deoptimization::Reason_class_check, 1.194 + Deoptimization::Action_maybe_recompile); 1.195 + } 1.196 + replace_in_map(not_null_obj, exact_obj); 1.197 + obj = exact_obj; 1.198 + } 1.199 + } else { 1.200 + if (!too_many_traps(Deoptimization::Reason_null_assert)) { 1.201 + Node* exact_obj = null_assert(obj); 1.202 + replace_in_map(obj, exact_obj); 1.203 + obj = exact_obj; 1.204 + } 1.205 + } 1.206 + return obj; 1.207 +} 1.208 1.209 //-------------------------------gen_instanceof-------------------------------- 1.210 // Generate an instance-of idiom. Used by both the instance-of bytecode 1.211 // and the reflective instance-of call. 1.212 -Node* GraphKit::gen_instanceof(Node* obj, Node* superklass) { 1.213 +Node* GraphKit::gen_instanceof(Node* obj, Node* superklass, bool safe_for_replace) { 1.214 kill_dead_locals(); // Benefit all the uncommon traps 1.215 assert( !stopped(), "dead parse path should be checked in callers" ); 1.216 assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()), 1.217 @@ -2692,10 +2840,8 @@ 1.218 C->set_has_split_ifs(true); // Has chance for split-if optimization 1.219 1.220 ciProfileData* data = NULL; 1.221 - bool safe_for_replace = false; 1.222 if (java_bc() == Bytecodes::_instanceof) { // Only for the bytecode 1.223 data = method()->method_data()->bci_to_data(bci()); 1.224 - safe_for_replace = true; 1.225 } 1.226 bool never_see_null = (ProfileDynamicTypes // aggressive use of profile 1.227 && seems_never_null(obj, data)); 1.228 @@ -2719,14 +2865,37 @@ 1.229 phi ->del_req(_null_path); 1.230 } 1.231 1.232 - if (ProfileDynamicTypes && data != NULL) { 1.233 - Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, data, NULL); 1.234 - if (stopped()) { // Profile disagrees with this path. 1.235 - set_control(null_ctl); // Null is the only remaining possibility. 1.236 - return intcon(0); 1.237 + // Do we know the type check always succeed? 1.238 + bool known_statically = false; 1.239 + if (_gvn.type(superklass)->singleton()) { 1.240 + ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass(); 1.241 + ciKlass* subk = _gvn.type(obj)->is_oopptr()->klass(); 1.242 + if (subk != NULL && subk->is_loaded()) { 1.243 + int static_res = static_subtype_check(superk, subk); 1.244 + known_statically = (static_res == SSC_always_true || static_res == SSC_always_false); 1.245 } 1.246 - if (cast_obj != NULL) 1.247 - not_null_obj = cast_obj; 1.248 + } 1.249 + 1.250 + if (known_statically && UseTypeSpeculation) { 1.251 + // If we know the type check always succeed then we don't use the 1.252 + // profiling data at this bytecode. Don't lose it, feed it to the 1.253 + // type system as a speculative type. 1.254 + not_null_obj = record_profiled_receiver_for_speculation(not_null_obj); 1.255 + } else { 1.256 + const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr(); 1.257 + // We may not have profiling here or it may not help us. If we 1.258 + // have a speculative type use it to perform an exact cast. 1.259 + ciKlass* spec_obj_type = obj_type->speculative_type(); 1.260 + if (spec_obj_type != NULL || (ProfileDynamicTypes && data != NULL)) { 1.261 + Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, NULL, spec_obj_type, safe_for_replace); 1.262 + if (stopped()) { // Profile disagrees with this path. 1.263 + set_control(null_ctl); // Null is the only remaining possibility. 1.264 + return intcon(0); 1.265 + } 1.266 + if (cast_obj != NULL) { 1.267 + not_null_obj = cast_obj; 1.268 + } 1.269 + } 1.270 } 1.271 1.272 // Load the object's klass 1.273 @@ -2773,7 +2942,10 @@ 1.274 if (objtp != NULL && objtp->klass() != NULL) { 1.275 switch (static_subtype_check(tk->klass(), objtp->klass())) { 1.276 case SSC_always_true: 1.277 - return obj; 1.278 + // If we know the type check always succeed then we don't use 1.279 + // the profiling data at this bytecode. Don't lose it, feed it 1.280 + // to the type system as a speculative type. 1.281 + return record_profiled_receiver_for_speculation(obj); 1.282 case SSC_always_false: 1.283 // It needs a null check because a null will *pass* the cast check. 1.284 // A non-null value will always produce an exception. 1.285 @@ -2822,12 +2994,17 @@ 1.286 } 1.287 1.288 Node* cast_obj = NULL; 1.289 - if (data != NULL && 1.290 - // Counter has never been decremented (due to cast failure). 1.291 - // ...This is a reasonable thing to expect. It is true of 1.292 - // all casts inserted by javac to implement generic types. 1.293 - data->as_CounterData()->count() >= 0) { 1.294 - cast_obj = maybe_cast_profiled_receiver(not_null_obj, data, tk->klass()); 1.295 + const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr(); 1.296 + // We may not have profiling here or it may not help us. If we have 1.297 + // a speculative type use it to perform an exact cast. 1.298 + ciKlass* spec_obj_type = obj_type->speculative_type(); 1.299 + if (spec_obj_type != NULL || 1.300 + (data != NULL && 1.301 + // Counter has never been decremented (due to cast failure). 1.302 + // ...This is a reasonable thing to expect. It is true of 1.303 + // all casts inserted by javac to implement generic types. 1.304 + data->as_CounterData()->count() >= 0)) { 1.305 + cast_obj = maybe_cast_profiled_receiver(not_null_obj, tk->klass(), spec_obj_type, safe_for_replace); 1.306 if (cast_obj != NULL) { 1.307 if (failure_control != NULL) // failure is now impossible 1.308 (*failure_control) = top();