| 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 | 10 |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 | 484 |
| 485 const intptr_t class_id = ReceiverClassId(comp); | 485 const intptr_t class_id = ReceiverClassId(comp); |
| 486 switch (class_id) { | 486 switch (class_id) { |
| 487 case kArray: | 487 case kArray: |
| 488 case kGrowableObjectArray: | 488 case kGrowableObjectArray: |
| 489 comp->set_receiver_type(static_cast<ObjectKind>(class_id)); | 489 comp->set_receiver_type(static_cast<ObjectKind>(class_id)); |
| 490 } | 490 } |
| 491 } | 491 } |
| 492 | 492 |
| 493 | 493 |
| 494 static void TryFuseComparisonWithBranch(BindInstr* instr, | |
| 495 ComparisonComp* comp) { | |
| 496 Instruction* next_instr = instr->next(); | |
| 497 if ((next_instr != NULL) && next_instr->IsBranch()) { | |
| 498 BranchInstr* branch = next_instr->AsBranch(); | |
| 499 UseVal* use = branch->value()->AsUse(); | |
| 500 if (instr == use->definition()) { | |
| 501 comp->MarkFusedWithBranch(branch); | |
| 502 branch->MarkFusedWithComparison(comp); | |
| 503 | |
| 504 // Remove comparison from the graph. | |
| 505 branch->set_previous(instr->previous()); | |
| 506 instr->previous()->set_next(branch); | |
| 507 return; | |
| 508 } | |
| 509 } | |
| 510 if ((next_instr != NULL) && next_instr->IsBind()) { | |
| 511 Computation* next_comp = next_instr->AsBind()->computation(); | |
| 512 if (next_comp->IsBooleanNegate()) { | |
| 513 Instruction* next_next_instr = next_instr->next(); | |
| 514 if ((next_next_instr != NULL) && next_next_instr->IsBranch()) { | |
| 515 BooleanNegateComp* negate = next_comp->AsBooleanNegate(); | |
| 516 BranchInstr* branch = next_next_instr->AsBranch(); | |
| 517 if ((branch->value()->AsUse()->definition() == next_instr) && | |
| 518 (negate->value()->AsUse()->definition() == instr)) { | |
| 519 comp->MarkFusedWithBranch(branch); | |
| 520 branch->MarkFusedWithComparison(comp); | |
| 521 branch->set_is_negated(true); | |
| 522 // Remove the comparison and the boolean negation from the graph. | |
| 523 branch->set_previous(instr->previous()); | |
| 524 instr->previous()->set_next(branch); | |
| 525 return; | |
| 526 } | |
| 527 } | |
| 528 } | |
| 529 } | |
| 530 } | |
| 531 | |
| 532 | |
| 533 void FlowGraphOptimizer::VisitRelationalOp(RelationalOpComp* comp, | 494 void FlowGraphOptimizer::VisitRelationalOp(RelationalOpComp* comp, |
| 534 BindInstr* instr) { | 495 BindInstr* instr) { |
| 535 if (!comp->HasICData()) return; | 496 if (!comp->HasICData()) return; |
| 536 | 497 |
| 537 const ICData& ic_data = *comp->ic_data(); | 498 const ICData& ic_data = *comp->ic_data(); |
| 538 if (ic_data.NumberOfChecks() == 0) return; | 499 if (ic_data.NumberOfChecks() == 0) return; |
| 539 // TODO(srdjan): Add multiple receiver type support. | 500 // TODO(srdjan): Add multiple receiver type support. |
| 540 if (ic_data.NumberOfChecks() != 1) return; | 501 if (ic_data.NumberOfChecks() != 1) return; |
| 541 ASSERT(HasOneTarget(ic_data)); | 502 ASSERT(HasOneTarget(ic_data)); |
| 542 | 503 |
| 543 if (HasOnlyTwoSmi(ic_data)) { | 504 if (HasOnlyTwoSmi(ic_data)) { |
| 544 comp->set_operands_class_id(kSmi); | 505 comp->set_operands_class_id(kSmi); |
| 545 } else if (HasOnlyTwoDouble(ic_data)) { | 506 } else if (HasOnlyTwoDouble(ic_data)) { |
| 546 comp->set_operands_class_id(kDouble); | 507 comp->set_operands_class_id(kDouble); |
| 547 } else { | 508 } else { |
| 548 return; | 509 return; |
| 549 } | 510 } |
| 550 | |
| 551 // For smi and double comparisons if the next instruction is a conditional | |
| 552 // branch that uses the value of this comparison mark them as fused together | |
| 553 // to avoid materializing a boolean value. | |
| 554 TryFuseComparisonWithBranch(instr, comp); | |
| 555 } | |
| 556 | |
| 557 | |
| 558 void FlowGraphOptimizer::VisitStrictCompare(StrictCompareComp* comp, | |
| 559 BindInstr* instr) { | |
| 560 TryFuseComparisonWithBranch(instr, comp); | |
| 561 } | 511 } |
| 562 | 512 |
| 563 | 513 |
| 564 void FlowGraphOptimizer::VisitEqualityCompare(EqualityCompareComp* comp, | 514 void FlowGraphOptimizer::VisitEqualityCompare(EqualityCompareComp* comp, |
| 565 BindInstr* instr) { | 515 BindInstr* instr) { |
| 566 if (comp->HasICData() && (comp->ic_data()->NumberOfChecks() == 1)) { | 516 if (comp->HasICData() && (comp->ic_data()->NumberOfChecks() == 1)) { |
| 567 ASSERT(comp->ic_data()->num_args_tested() == 2); | 517 ASSERT(comp->ic_data()->num_args_tested() == 2); |
| 568 GrowableArray<intptr_t> class_ids; | 518 GrowableArray<intptr_t> class_ids; |
| 569 Function& target = Function::Handle(); | 519 Function& target = Function::Handle(); |
| 570 comp->ic_data()->GetCheckAt(0, &class_ids, &target); | 520 comp->ic_data()->GetCheckAt(0, &class_ids, &target); |
| 571 // TODO(srdjan): allow for mixed mode comparison. | 521 // TODO(srdjan): allow for mixed mode comparison. |
| 572 if ((class_ids[0] == kSmi) && (class_ids[1] == kSmi)) { | 522 if ((class_ids[0] == kSmi) && (class_ids[1] == kSmi)) { |
| 573 comp->set_receiver_class_id(kSmi); | 523 comp->set_receiver_class_id(kSmi); |
| 574 } else if ((class_ids[0] == kDouble) && (class_ids[1] == kDouble)) { | 524 } else if ((class_ids[0] == kDouble) && (class_ids[1] == kDouble)) { |
| 575 comp->set_receiver_class_id(kDouble); | 525 comp->set_receiver_class_id(kDouble); |
| 576 } | 526 } |
| 577 } | 527 } |
| 578 TryFuseComparisonWithBranch(instr, comp); | |
| 579 } | 528 } |
| 580 | 529 |
| 581 | 530 |
| 582 void FlowGraphOptimizer::VisitBind(BindInstr* instr) { | 531 void FlowGraphOptimizer::VisitBind(BindInstr* instr) { |
| 583 instr->computation()->Accept(this, instr); | 532 instr->computation()->Accept(this, instr); |
| 584 } | 533 } |
| 585 | 534 |
| 586 | 535 |
| 587 | 536 |
| 588 void FlowGraphAnalyzer::Analyze() { | 537 void FlowGraphAnalyzer::Analyze() { |
| 589 is_leaf_ = true; | 538 is_leaf_ = true; |
| 590 for (intptr_t i = 0; i < blocks_.length(); ++i) { | 539 for (intptr_t i = 0; i < blocks_.length(); ++i) { |
| 591 BlockEntryInstr* entry = blocks_[i]; | 540 BlockEntryInstr* entry = blocks_[i]; |
| 592 for (ForwardInstructionIterator it(entry); !it.Done(); it.Advance()) { | 541 for (ForwardInstructionIterator it(entry); !it.Done(); it.Advance()) { |
| 593 LocationSummary* locs = it.Current()->locs(); | 542 LocationSummary* locs = it.Current()->locs(); |
| 594 if ((locs != NULL) && locs->is_call()) { | 543 if ((locs != NULL) && locs->is_call()) { |
| 595 is_leaf_ = false; | 544 is_leaf_ = false; |
| 596 return; | 545 return; |
| 597 } | 546 } |
| 598 } | 547 } |
| 599 } | 548 } |
| 600 } | 549 } |
| 601 | 550 |
| 602 } // namespace dart | 551 } // namespace dart |
| OLD | NEW |