| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/flow_graph_optimizer.h" | 5 #include "vm/flow_graph_optimizer.h" |
| 6 | 6 |
| 7 #include "vm/bit_vector.h" | 7 #include "vm/bit_vector.h" |
| 8 #include "vm/cha.h" | 8 #include "vm/cha.h" |
| 9 #include "vm/flow_graph_builder.h" | 9 #include "vm/flow_graph_builder.h" |
| 10 #include "vm/flow_graph_compiler.h" | 10 #include "vm/flow_graph_compiler.h" |
| (...skipping 1231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1242 } | 1242 } |
| 1243 call->ReplaceWith(d2i_instr, current_iterator()); | 1243 call->ReplaceWith(d2i_instr, current_iterator()); |
| 1244 RemovePushArguments(call); | 1244 RemovePushArguments(call); |
| 1245 return true; | 1245 return true; |
| 1246 } | 1246 } |
| 1247 | 1247 |
| 1248 return false; | 1248 return false; |
| 1249 } | 1249 } |
| 1250 | 1250 |
| 1251 | 1251 |
| 1252 // Returns a Boolean constant if all classes in ic_data yield the same type-test |
| 1253 // result and the type tests do not depend on type arguments. Otherwise return |
| 1254 // Bool::null(). |
| 1255 RawBool* FlowGraphOptimizer::InstanceOfAsBool(const ICData& ic_data, |
| 1256 const AbstractType& type) const { |
| 1257 ASSERT(ic_data.num_args_tested() == 1); // Unary checks only. |
| 1258 if (!type.IsInstantiated() || type.IsMalformed()) return Bool::null(); |
| 1259 const Class& type_class = Class::Handle(type.type_class()); |
| 1260 if (type_class.HasTypeArguments()) return Bool::null(); |
| 1261 const ClassTable& class_table = *Isolate::Current()->class_table(); |
| 1262 Bool& prev = Bool::Handle(); |
| 1263 Class& cls = Class::Handle(); |
| 1264 for (int i = 0; i < ic_data.NumberOfChecks(); i++) { |
| 1265 cls = class_table.At(ic_data.GetReceiverClassIdAt(i)); |
| 1266 if (cls.HasTypeArguments()) return Bool::null(); |
| 1267 bool is_subtype = false; |
| 1268 if (cls.IsNullClass()) { |
| 1269 is_subtype = type_class.IsDynamicClass() || type_class.IsObjectClass(); |
| 1270 } else { |
| 1271 is_subtype = cls.IsSubtypeOf(TypeArguments::Handle(), |
| 1272 type_class, |
| 1273 TypeArguments::Handle(), |
| 1274 NULL); |
| 1275 } |
| 1276 if (prev.IsNull()) { |
| 1277 prev = is_subtype ? Bool::True().raw() : Bool::False().raw(); |
| 1278 } else { |
| 1279 if (is_subtype != prev.value()) return Bool::null(); |
| 1280 } |
| 1281 } |
| 1282 return prev.raw(); |
| 1283 } |
| 1284 |
| 1285 |
| 1252 // TODO(srdjan): Use ICData to check if always true or false. | 1286 // TODO(srdjan): Use ICData to check if always true or false. |
| 1253 void FlowGraphOptimizer::ReplaceWithInstanceOf(InstanceCallInstr* call) { | 1287 void FlowGraphOptimizer::ReplaceWithInstanceOf(InstanceCallInstr* call) { |
| 1254 ASSERT(Token::IsTypeTestOperator(call->token_kind())); | 1288 ASSERT(Token::IsTypeTestOperator(call->token_kind())); |
| 1255 Value* left_val = call->ArgumentAt(0)->value(); | 1289 Value* left_val = call->ArgumentAt(0)->value(); |
| 1256 Value* instantiator_val = call->ArgumentAt(1)->value(); | 1290 Value* instantiator_val = call->ArgumentAt(1)->value(); |
| 1257 Value* type_args_val = call->ArgumentAt(2)->value(); | 1291 Value* type_args_val = call->ArgumentAt(2)->value(); |
| 1258 const AbstractType& type = | 1292 const AbstractType& type = |
| 1259 AbstractType::Cast(call->ArgumentAt(3)->value()->BoundConstant()); | 1293 AbstractType::Cast(call->ArgumentAt(3)->value()->BoundConstant()); |
| 1260 const Bool& negate = | 1294 const bool negate = |
| 1261 Bool::Cast(call->ArgumentAt(4)->value()->BoundConstant()); | 1295 Bool::Cast(call->ArgumentAt(4)->value()->BoundConstant()).value(); |
| 1296 const ICData& unary_checks = |
| 1297 ICData::ZoneHandle(call->ic_data()->AsUnaryClassChecks()); |
| 1298 if (unary_checks.NumberOfChecks() <= FLAG_max_polymorphic_checks) { |
| 1299 Bool& as_bool = Bool::ZoneHandle(InstanceOfAsBool(unary_checks, type)); |
| 1300 if (!as_bool.IsNull()) { |
| 1301 AddCheckClass(call, left_val->Copy()); |
| 1302 if (negate) { |
| 1303 as_bool = as_bool.value() ? Bool::False().raw() : Bool::True().raw(); |
| 1304 } |
| 1305 ConstantInstr* bool_const = new ConstantInstr(as_bool); |
| 1306 call->ReplaceWith(bool_const, current_iterator()); |
| 1307 RemovePushArguments(call); |
| 1308 return; |
| 1309 } |
| 1310 } |
| 1262 InstanceOfInstr* instance_of = | 1311 InstanceOfInstr* instance_of = |
| 1263 new InstanceOfInstr(call->token_pos(), | 1312 new InstanceOfInstr(call->token_pos(), |
| 1264 left_val, | 1313 left_val, |
| 1265 instantiator_val, | 1314 instantiator_val, |
| 1266 type_args_val, | 1315 type_args_val, |
| 1267 type, | 1316 type, |
| 1268 negate.value()); | 1317 negate); |
| 1269 call->ReplaceWith(instance_of, current_iterator()); | 1318 call->ReplaceWith(instance_of, current_iterator()); |
| 1270 RemovePushArguments(call); | 1319 RemovePushArguments(call); |
| 1271 } | 1320 } |
| 1272 | 1321 |
| 1273 | 1322 |
| 1274 // Tries to optimize instance call by replacing it with a faster instruction | 1323 // Tries to optimize instance call by replacing it with a faster instruction |
| 1275 // (e.g, binary op, field load, ..). | 1324 // (e.g, binary op, field load, ..). |
| 1276 void FlowGraphOptimizer::VisitInstanceCall(InstanceCallInstr* instr) { | 1325 void FlowGraphOptimizer::VisitInstanceCall(InstanceCallInstr* instr) { |
| 1277 if (!instr->HasICData() || (instr->ic_data()->NumberOfChecks() == 0)) { | 1326 if (!instr->HasICData() || (instr->ic_data()->NumberOfChecks() == 0)) { |
| 1278 // An instance call without ICData will trigger deoptimization. | 1327 // An instance call without ICData will trigger deoptimization. |
| (...skipping 3099 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4378 | 4427 |
| 4379 if (FLAG_trace_constant_propagation) { | 4428 if (FLAG_trace_constant_propagation) { |
| 4380 OS::Print("\n==== After constant propagation ====\n"); | 4429 OS::Print("\n==== After constant propagation ====\n"); |
| 4381 FlowGraphPrinter printer(*graph_); | 4430 FlowGraphPrinter printer(*graph_); |
| 4382 printer.PrintBlocks(); | 4431 printer.PrintBlocks(); |
| 4383 } | 4432 } |
| 4384 } | 4433 } |
| 4385 | 4434 |
| 4386 | 4435 |
| 4387 } // namespace dart | 4436 } // namespace dart |
| OLD | NEW |