Chromium Code Reviews| 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/flow_graph_builder.h" | 7 #include "vm/flow_graph_builder.h" |
| 8 #include "vm/il_printer.h" | 8 #include "vm/il_printer.h" |
| 9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 10 #include "vm/parser.h" | 10 #include "vm/parser.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 144 |
| 145 | 145 |
| 146 static void RemovePushArguments(InstanceCallComp* comp) { | 146 static void RemovePushArguments(InstanceCallComp* comp) { |
| 147 // Remove original push arguments. | 147 // Remove original push arguments. |
| 148 for (intptr_t i = 0; i < comp->ArgumentCount(); ++i) { | 148 for (intptr_t i = 0; i < comp->ArgumentCount(); ++i) { |
| 149 comp->ArgumentAt(i)->RemoveFromGraph(); | 149 comp->ArgumentAt(i)->RemoveFromGraph(); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 | 153 |
| 154 // Returns true if all targets are the same. | |
| 155 // TODO(srdjan): if targets are native use their C_function to compare. | |
| 156 static bool HasOneTarget(const ICData& ic_data) { | |
| 157 ASSERT(ic_data.NumberOfChecks() > 0); | |
| 158 const Function& first_target = Function::Handle(ic_data.GetTargetAt(0)); | |
| 159 Function& test_target = Function::Handle(); | |
| 160 for (intptr_t i = 1; i < ic_data.NumberOfChecks(); i++) { | |
| 161 test_target = ic_data.GetTargetAt(i); | |
| 162 if (first_target.raw() != test_target.raw()) { | |
| 163 return false; | |
| 164 } | |
| 165 } | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 | |
| 170 static intptr_t ReceiverClassId(Computation* comp) { | |
| 171 if (!comp->HasICData()) return kIllegalCid; | |
| 172 | |
| 173 const ICData& ic_data = *comp->ic_data(); | |
| 174 | |
| 175 if (ic_data.NumberOfChecks() == 0) return kIllegalCid; | |
| 176 // TODO(vegorov): Add multiple receiver type support. | |
| 177 if (ic_data.NumberOfChecks() != 1) return kIllegalCid; | |
| 178 ASSERT(HasOneTarget(ic_data)); | |
| 179 | |
| 180 Function& target = Function::Handle(); | |
| 181 intptr_t class_id; | |
| 182 ic_data.GetOneClassCheckAt(0, &class_id, &target); | |
| 183 return class_id; | |
| 184 } | |
| 185 | |
| 186 | |
| 187 bool FlowGraphOptimizer::TryReplaceWithArrayOp(BindInstr* instr, | |
| 188 InstanceCallComp* comp, | |
| 189 Token::Kind op_kind) { | |
| 190 // TODO(fschneider): Optimize []= operator in checked mode as well. | |
| 191 if (op_kind == Token::kASSIGN_INDEX && FLAG_enable_type_checks) return false; | |
| 192 | |
| 193 const intptr_t class_id = ReceiverClassId(comp); | |
| 194 switch (class_id) { | |
| 195 case kArrayCid: | |
| 196 case kImmutableArrayCid: | |
|
Vyacheslav Egorov (Google)
2012/08/15 13:16:09
I don't think StoreIndexedComp correctly handles s
Florian Schneider
2012/08/15 13:53:51
Thanks for the catch. Done.
| |
| 197 case kGrowableObjectArrayCid: { | |
| 198 Computation* array_op = NULL; | |
| 199 if (op_kind == Token::kINDEX) { | |
| 200 array_op = new LoadIndexedComp(comp->ArgumentAt(0)->value(), | |
| 201 comp->ArgumentAt(1)->value(), | |
| 202 class_id, | |
| 203 comp); | |
| 204 } else { | |
| 205 array_op = new StoreIndexedComp(comp->ArgumentAt(0)->value(), | |
| 206 comp->ArgumentAt(1)->value(), | |
| 207 comp->ArgumentAt(2)->value(), | |
| 208 class_id, | |
| 209 comp); | |
| 210 } | |
| 211 array_op->set_ic_data(comp->ic_data()); | |
| 212 instr->set_computation(array_op); | |
| 213 RemovePushArguments(comp); | |
| 214 return true; | |
| 215 } | |
| 216 default: | |
| 217 return false; | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 | |
| 154 bool FlowGraphOptimizer::TryReplaceWithBinaryOp(BindInstr* instr, | 222 bool FlowGraphOptimizer::TryReplaceWithBinaryOp(BindInstr* instr, |
| 155 InstanceCallComp* comp, | 223 InstanceCallComp* comp, |
| 156 Token::Kind op_kind) { | 224 Token::Kind op_kind) { |
| 157 BinaryOpComp::OperandsType operands_type = BinaryOpComp::kDynamicOperands; | 225 BinaryOpComp::OperandsType operands_type = BinaryOpComp::kDynamicOperands; |
| 158 ASSERT(comp->HasICData()); | 226 ASSERT(comp->HasICData()); |
| 159 const ICData& ic_data = *comp->ic_data(); | 227 const ICData& ic_data = *comp->ic_data(); |
| 160 switch (op_kind) { | 228 switch (op_kind) { |
| 161 case Token::kADD: | 229 case Token::kADD: |
| 162 case Token::kSUB: | 230 case Token::kSUB: |
| 163 case Token::kMUL: | 231 case Token::kMUL: |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 } | 306 } |
| 239 if (unary_op == NULL) return false; | 307 if (unary_op == NULL) return false; |
| 240 | 308 |
| 241 unary_op->set_ic_data(comp->ic_data()); | 309 unary_op->set_ic_data(comp->ic_data()); |
| 242 instr->set_computation(unary_op); | 310 instr->set_computation(unary_op); |
| 243 RemovePushArguments(comp); | 311 RemovePushArguments(comp); |
| 244 return true; | 312 return true; |
| 245 } | 313 } |
| 246 | 314 |
| 247 | 315 |
| 248 // Returns true if all targets are the same. | |
| 249 // TODO(srdjan): if targets are native use their C_function to compare. | |
| 250 static bool HasOneTarget(const ICData& ic_data) { | |
| 251 ASSERT(ic_data.NumberOfChecks() > 0); | |
| 252 const Function& first_target = Function::Handle(ic_data.GetTargetAt(0)); | |
| 253 Function& test_target = Function::Handle(); | |
| 254 for (intptr_t i = 1; i < ic_data.NumberOfChecks(); i++) { | |
| 255 test_target = ic_data.GetTargetAt(i); | |
| 256 if (first_target.raw() != test_target.raw()) { | |
| 257 return false; | |
| 258 } | |
| 259 } | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 | |
| 264 // Using field class | 316 // Using field class |
| 265 static RawField* GetField(intptr_t class_id, const String& field_name) { | 317 static RawField* GetField(intptr_t class_id, const String& field_name) { |
| 266 Class& cls = Class::Handle(Isolate::Current()->class_table()->At(class_id)); | 318 Class& cls = Class::Handle(Isolate::Current()->class_table()->At(class_id)); |
| 267 Field& field = Field::Handle(); | 319 Field& field = Field::Handle(); |
| 268 while (!cls.IsNull()) { | 320 while (!cls.IsNull()) { |
| 269 field = cls.LookupInstanceField(field_name); | 321 field = cls.LookupInstanceField(field_name); |
| 270 if (!field.IsNull()) { | 322 if (!field.IsNull()) { |
| 271 return field.raw(); | 323 return field.raw(); |
| 272 } | 324 } |
| 273 cls = cls.SuperClass(); | 325 cls = cls.SuperClass(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 instr->set_computation(coerce); | 447 instr->set_computation(coerce); |
| 396 RemovePushArguments(comp); | 448 RemovePushArguments(comp); |
| 397 return true; | 449 return true; |
| 398 } | 450 } |
| 399 | 451 |
| 400 | 452 |
| 401 void FlowGraphOptimizer::VisitInstanceCall(InstanceCallComp* comp, | 453 void FlowGraphOptimizer::VisitInstanceCall(InstanceCallComp* comp, |
| 402 BindInstr* instr) { | 454 BindInstr* instr) { |
| 403 if (comp->HasICData() && (comp->ic_data()->NumberOfChecks() > 0)) { | 455 if (comp->HasICData() && (comp->ic_data()->NumberOfChecks() > 0)) { |
| 404 const Token::Kind op_kind = comp->token_kind(); | 456 const Token::Kind op_kind = comp->token_kind(); |
| 457 if (Token::IsIndexOperator(op_kind) && | |
| 458 TryReplaceWithArrayOp(instr, comp, op_kind)) { | |
| 459 return; | |
| 460 } | |
| 405 if (Token::IsBinaryToken(op_kind) && | 461 if (Token::IsBinaryToken(op_kind) && |
| 406 TryReplaceWithBinaryOp(instr, comp, op_kind)) { | 462 TryReplaceWithBinaryOp(instr, comp, op_kind)) { |
| 407 return; | 463 return; |
| 408 } | 464 } |
| 409 if (Token::IsUnaryToken(op_kind) && | 465 if (Token::IsUnaryToken(op_kind) && |
| 410 TryReplaceWithUnaryOp(instr, comp, op_kind)) { | 466 TryReplaceWithUnaryOp(instr, comp, op_kind)) { |
| 411 return; | 467 return; |
| 412 } | 468 } |
| 413 if ((op_kind == Token::kGET) && TryInlineInstanceGetter(instr, comp)) { | 469 if ((op_kind == Token::kGET) && TryInlineInstanceGetter(instr, comp)) { |
| 414 return; | 470 return; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 comp->ArgumentAt(0)->value(), | 534 comp->ArgumentAt(0)->value(), |
| 479 comp->ArgumentAt(1)->value(), | 535 comp->ArgumentAt(1)->value(), |
| 480 comp); | 536 comp); |
| 481 store->set_ic_data(comp->ic_data()); | 537 store->set_ic_data(comp->ic_data()); |
| 482 instr->set_computation(store); | 538 instr->set_computation(store); |
| 483 RemovePushArguments(comp); | 539 RemovePushArguments(comp); |
| 484 return true; | 540 return true; |
| 485 } | 541 } |
| 486 | 542 |
| 487 | 543 |
| 488 enum IndexedAccessType { | |
| 489 kIndexedLoad, | |
| 490 kIndexedStore | |
| 491 }; | |
| 492 | |
| 493 | |
| 494 static intptr_t ReceiverClassId(Computation* comp) { | |
| 495 if (!comp->HasICData()) return kIllegalCid; | |
| 496 | |
| 497 const ICData& ic_data = *comp->ic_data(); | |
| 498 | |
| 499 if (ic_data.NumberOfChecks() == 0) return kIllegalCid; | |
| 500 // TODO(vegorov): Add multiple receiver type support. | |
| 501 if (ic_data.NumberOfChecks() != 1) return kIllegalCid; | |
| 502 ASSERT(HasOneTarget(ic_data)); | |
| 503 | |
| 504 Function& target = Function::Handle(); | |
| 505 intptr_t class_id; | |
| 506 ic_data.GetOneClassCheckAt(0, &class_id, &target); | |
| 507 return class_id; | |
| 508 } | |
| 509 | |
| 510 | |
| 511 void FlowGraphOptimizer::VisitLoadIndexed(LoadIndexedComp* comp, | |
| 512 BindInstr* instr) { | |
| 513 const intptr_t class_id = ReceiverClassId(comp); | |
| 514 switch (class_id) { | |
| 515 case kArrayCid: | |
| 516 case kImmutableArrayCid: | |
| 517 case kGrowableObjectArrayCid: | |
| 518 comp->set_receiver_type(class_id); | |
| 519 } | |
| 520 } | |
| 521 | |
| 522 | |
| 523 void FlowGraphOptimizer::VisitStoreIndexed(StoreIndexedComp* comp, | |
| 524 BindInstr* instr) { | |
| 525 if (FLAG_enable_type_checks) return; | |
| 526 | |
| 527 const intptr_t class_id = ReceiverClassId(comp); | |
| 528 switch (class_id) { | |
| 529 case kArrayCid: | |
| 530 case kGrowableObjectArrayCid: | |
| 531 comp->set_receiver_type(class_id); | |
| 532 } | |
| 533 } | |
| 534 | |
| 535 | |
| 536 void FlowGraphOptimizer::VisitRelationalOp(RelationalOpComp* comp, | 544 void FlowGraphOptimizer::VisitRelationalOp(RelationalOpComp* comp, |
| 537 BindInstr* instr) { | 545 BindInstr* instr) { |
| 538 if (!comp->HasICData()) return; | 546 if (!comp->HasICData()) return; |
| 539 | 547 |
| 540 const ICData& ic_data = *comp->ic_data(); | 548 const ICData& ic_data = *comp->ic_data(); |
| 541 if (ic_data.NumberOfChecks() == 0) return; | 549 if (ic_data.NumberOfChecks() == 0) return; |
| 542 // TODO(srdjan): Add multiple receiver type support. | 550 // TODO(srdjan): Add multiple receiver type support. |
| 543 if (ic_data.NumberOfChecks() != 1) return; | 551 if (ic_data.NumberOfChecks() != 1) return; |
| 544 ASSERT(HasOneTarget(ic_data)); | 552 ASSERT(HasOneTarget(ic_data)); |
| 545 | 553 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 718 LocationSummary* locs = it.Current()->locs(); | 726 LocationSummary* locs = it.Current()->locs(); |
| 719 if ((locs != NULL) && locs->is_call()) { | 727 if ((locs != NULL) && locs->is_call()) { |
| 720 is_leaf_ = false; | 728 is_leaf_ = false; |
| 721 return; | 729 return; |
| 722 } | 730 } |
| 723 } | 731 } |
| 724 } | 732 } |
| 725 } | 733 } |
| 726 | 734 |
| 727 } // namespace dart | 735 } // namespace dart |
| OLD | NEW |