Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(612)

Side by Side Diff: runtime/vm/flow_graph_optimizer.cc

Issue 23549020: Optimize conditional branches that have same true/false targets. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/flow_graph_builder.h" 10 #include "vm/flow_graph_builder.h"
(...skipping 6680 matching lines...) Expand 10 before | Expand all | Expand 10 after
6691 } else { 6691 } else {
6692 // No new information: Assume both targets are reachable. 6692 // No new information: Assume both targets are reachable.
6693 SetReachable(branch->true_successor()); 6693 SetReachable(branch->true_successor());
6694 SetReachable(branch->false_successor()); 6694 SetReachable(branch->false_successor());
6695 } 6695 }
6696 } 6696 }
6697 } 6697 }
6698 } 6698 }
6699 6699
6700 6700
6701 static bool IsEmpty(BlockEntryInstr* block) {
6702 return block->next()->IsGoto()
Kevin Millikin (Google) 2013/09/27 11:03:43 I swear we have exactly this same function somewhe
Florian Schneider 2013/09/30 12:19:23 We have one that deals with blocks after translate
Kevin Millikin (Google) 2013/09/30 12:27:41 Nah, it's not necessary.
6703 && (!block->IsJoinEntry() || (block->AsJoinEntry()->phis() == NULL));
6704 }
6705
6706
6707 // Traverses a chain of empty blocks and return the first
srdjan 2013/09/27 15:44:11 Why only 60 chars per line? s/return/returns/
Florian Schneider 2013/09/30 12:19:23 Done.
6708 // reachable non-empty block. Empty blocks are added to
6709 // the supplied bit vector.
6710 static BlockEntryInstr* FindFirstNonEmptySuccessor(
6711 TargetEntryInstr* block,
6712 BitVector* empty_blocks) {
6713 BlockEntryInstr* current = block;
6714 while (IsEmpty(current)) {
6715 empty_blocks->Add(current->preorder_number());
6716 current = current->next()->AsGoto()->successor();
6717 }
6718 return current;
6719 }
6720
6721 void ConstantPropagator::RemoveRedundantBranches(FlowGraph* graph) {
6722 GrowableArray<BlockEntryInstr*> ignored;
6723 ConstantPropagator cp(graph, ignored);
6724 cp.EliminateRedundantBranches();
6725 }
6726
6727
6728 void ConstantPropagator::EliminateRedundantBranches() {
6729 // Canonicalize branches that have no side-effects and
6730 // where true- and false-target are the same.
srdjan 2013/09/27 15:44:11 s/target/targets/
Florian Schneider 2013/09/30 12:19:23 Done.
6731 BitVector* empty_blocks = new BitVector(graph_->preorder().length());
6732 for (BlockIterator b = graph_->postorder_iterator();
6733 !b.Done();
6734 b.Advance()) {
6735 BlockEntryInstr* block = b.Current();
6736 BranchInstr* branch = block->last_instruction()->AsBranch();
6737 empty_blocks->Clear();
6738 if ((branch != NULL) && branch->Effects().IsNone()) {
6739 ASSERT(branch->previous() != NULL); // Not already eliminated.
6740 BlockEntryInstr* if_true =
6741 FindFirstNonEmptySuccessor(branch->true_successor(), empty_blocks);
6742 BlockEntryInstr* if_false =
6743 FindFirstNonEmptySuccessor(branch->false_successor(), empty_blocks);
6744 if (if_true == if_false) {
6745 // Replace the branch with a jump to the common successor.
6746 // Drop the comparison, which does not have side effects
6747 JoinEntryInstr* join = if_true->AsJoinEntry();
6748 if (join->phis() == NULL) {
6749 GotoInstr* jump = new GotoInstr(if_true->AsJoinEntry());
6750 jump->InheritDeoptTarget(branch);
6751
6752 Instruction* previous = branch->previous();
6753 branch->set_previous(NULL);
6754 previous->LinkTo(jump);
6755
6756 // Remove uses from branch and all the empty blocks that
6757 // are now unreachable.
6758 branch->UnuseAllInputs();
6759 for (BitVector::Iterator it(empty_blocks); !it.Done(); it.Advance()) {
6760 BlockEntryInstr* empty_block = graph_->preorder()[it.Current()];
6761 empty_block->UnuseAllInstructions();
6762 }
6763
6764 if (FLAG_trace_constant_propagation) {
6765 OS::Print("Eliminated branch in B%"Pd" common target B%"Pd"\n",
6766 block->block_id(), join->block_id());
6767 }
6768 }
6769 }
6770 }
6771 }
6772
6773 graph_->DiscoverBlocks();
6774 GrowableArray<BitVector*> dominance_frontier;
6775 graph_->ComputeDominators(&dominance_frontier);
6776 }
6777
6778
6701 void ConstantPropagator::Transform() { 6779 void ConstantPropagator::Transform() {
6702 if (FLAG_trace_constant_propagation) { 6780 if (FLAG_trace_constant_propagation) {
6703 OS::Print("\n==== Before constant propagation ====\n"); 6781 OS::Print("\n==== Before constant propagation ====\n");
6704 FlowGraphPrinter printer(*graph_); 6782 FlowGraphPrinter printer(*graph_);
6705 printer.PrintBlocks(); 6783 printer.PrintBlocks();
6706 } 6784 }
6707 6785
6708 GrowableArray<PhiInstr*> redundant_phis(10); 6786 GrowableArray<PhiInstr*> redundant_phis(10);
6709 6787
6710 // We will recompute dominators, block ordering, block ids, block last 6788 // We will recompute dominators, block ordering, block ids, block last
6711 // instructions, previous pointers, predecessors, etc. after eliminating 6789 // instructions, previous pointers, predecessors, etc. after eliminating
6712 // unreachable code. We do not maintain those properties during the 6790 // unreachable code. We do not maintain those properties during the
6713 // transformation. 6791 // transformation.
6714 for (BlockIterator b = graph_->reverse_postorder_iterator(); 6792 for (BlockIterator b = graph_->reverse_postorder_iterator();
6715 !b.Done(); 6793 !b.Done();
6716 b.Advance()) { 6794 b.Advance()) {
6717 BlockEntryInstr* block = b.Current(); 6795 BlockEntryInstr* block = b.Current();
6718 JoinEntryInstr* join = block->AsJoinEntry();
6719 if (!reachable_->Contains(block->preorder_number())) { 6796 if (!reachable_->Contains(block->preorder_number())) {
6720 if (FLAG_trace_constant_propagation) { 6797 if (FLAG_trace_constant_propagation) {
6721 OS::Print("Unreachable B%" Pd "\n", block->block_id()); 6798 OS::Print("Unreachable B%" Pd "\n", block->block_id());
6722 } 6799 }
6723 // Remove all uses in unreachable blocks. 6800 // Remove all uses in unreachable blocks.
6724 if (join != NULL) { 6801 block->UnuseAllInstructions();
6725 for (PhiIterator it(join); !it.Done(); it.Advance()) {
6726 it.Current()->UnuseAllInputs();
6727 }
6728 }
6729 block->UnuseAllInputs();
6730 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
6731 it.Current()->UnuseAllInputs();
6732 }
6733 continue; 6802 continue;
6734 } 6803 }
6735 6804
6805 JoinEntryInstr* join = block->AsJoinEntry();
6736 if (join != NULL) { 6806 if (join != NULL) {
6737 // Remove phi inputs corresponding to unreachable predecessor blocks. 6807 // Remove phi inputs corresponding to unreachable predecessor blocks.
6738 // Predecessors will be recomputed (in block id order) after removing 6808 // Predecessors will be recomputed (in block id order) after removing
6739 // unreachable code so we merely have to keep the phi inputs in order. 6809 // unreachable code so we merely have to keep the phi inputs in order.
6740 ZoneGrowableArray<PhiInstr*>* phis = join->phis(); 6810 ZoneGrowableArray<PhiInstr*>* phis = join->phis();
6741 if ((phis != NULL) && !phis->is_empty()) { 6811 if ((phis != NULL) && !phis->is_empty()) {
6742 intptr_t pred_count = join->PredecessorCount(); 6812 intptr_t pred_count = join->PredecessorCount();
6743 intptr_t live_count = 0; 6813 intptr_t live_count = 0;
6744 for (intptr_t pred_idx = 0; pred_idx < pred_count; ++pred_idx) { 6814 for (intptr_t pred_idx = 0; pred_idx < pred_count; ++pred_idx) {
6745 if (reachable_->Contains( 6815 if (reachable_->Contains(
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after
7483 } 7553 }
7484 7554
7485 // Insert materializations at environment uses. 7555 // Insert materializations at environment uses.
7486 for (intptr_t i = 0; i < exits.length(); i++) { 7556 for (intptr_t i = 0; i < exits.length(); i++) {
7487 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *fields); 7557 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *fields);
7488 } 7558 }
7489 } 7559 }
7490 7560
7491 7561
7492 } // namespace dart 7562 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698