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.h" | 5 #include "vm/flow_graph.h" |
6 | 6 |
7 #include "vm/bit_vector.h" | 7 #include "vm/bit_vector.h" |
8 #include "vm/flow_graph_builder.h" | 8 #include "vm/flow_graph_builder.h" |
9 #include "vm/intermediate_language.h" | 9 #include "vm/intermediate_language.h" |
10 #include "vm/longjump.h" | 10 #include "vm/longjump.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 current_ssa_temp_index_(0), | 23 current_ssa_temp_index_(0), |
24 max_block_id_(max_block_id), | 24 max_block_id_(max_block_id), |
25 parsed_function_(builder.parsed_function()), | 25 parsed_function_(builder.parsed_function()), |
26 num_copied_params_(builder.num_copied_params()), | 26 num_copied_params_(builder.num_copied_params()), |
27 num_non_copied_params_(builder.num_non_copied_params()), | 27 num_non_copied_params_(builder.num_non_copied_params()), |
28 num_stack_locals_(builder.num_stack_locals()), | 28 num_stack_locals_(builder.num_stack_locals()), |
29 graph_entry_(graph_entry), | 29 graph_entry_(graph_entry), |
30 preorder_(), | 30 preorder_(), |
31 postorder_(), | 31 postorder_(), |
32 reverse_postorder_(), | 32 reverse_postorder_(), |
33 exits_(NULL) { | 33 exits_(NULL), |
34 invalid_dominator_tree_(true) { | |
34 DiscoverBlocks(); | 35 DiscoverBlocks(); |
35 } | 36 } |
36 | 37 |
37 | 38 |
38 void FlowGraph::DiscoverBlocks() { | 39 void FlowGraph::DiscoverBlocks() { |
39 // Initialize state. | 40 // Initialize state. |
40 preorder_.Clear(); | 41 preorder_.Clear(); |
41 postorder_.Clear(); | 42 postorder_.Clear(); |
42 reverse_postorder_.Clear(); | 43 reverse_postorder_.Clear(); |
43 parent_.Clear(); | 44 parent_.Clear(); |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 | 298 |
298 // Compute immediate dominators and the dominance frontier for each basic | 299 // Compute immediate dominators and the dominance frontier for each basic |
299 // block. As a side effect of the algorithm, sets the immediate dominator | 300 // block. As a side effect of the algorithm, sets the immediate dominator |
300 // of each basic block. | 301 // of each basic block. |
301 // | 302 // |
302 // dominance_frontier: an output parameter encoding the dominance frontier. | 303 // dominance_frontier: an output parameter encoding the dominance frontier. |
303 // The array maps the preorder block number of a block to the set of | 304 // The array maps the preorder block number of a block to the set of |
304 // (preorder block numbers of) blocks in the dominance frontier. | 305 // (preorder block numbers of) blocks in the dominance frontier. |
305 void FlowGraph::ComputeDominators( | 306 void FlowGraph::ComputeDominators( |
306 GrowableArray<BitVector*>* dominance_frontier) { | 307 GrowableArray<BitVector*>* dominance_frontier) { |
308 invalid_dominator_tree_ = false; | |
307 // Use the SEMI-NCA algorithm to compute dominators. This is a two-pass | 309 // Use the SEMI-NCA algorithm to compute dominators. This is a two-pass |
308 // version of the Lengauer-Tarjan algorithm (LT is normally three passes) | 310 // version of the Lengauer-Tarjan algorithm (LT is normally three passes) |
309 // that eliminates a pass by using nearest-common ancestor (NCA) to | 311 // that eliminates a pass by using nearest-common ancestor (NCA) to |
310 // compute immediate dominators from semidominators. It also removes a | 312 // compute immediate dominators from semidominators. It also removes a |
311 // level of indirection in the link-eval forest data structure. | 313 // level of indirection in the link-eval forest data structure. |
312 // | 314 // |
313 // The algorithm is described in Georgiadis, Tarjan, and Werneck's | 315 // The algorithm is described in Georgiadis, Tarjan, and Werneck's |
314 // "Finding Dominators in Practice". | 316 // "Finding Dominators in Practice". |
315 // See http://www.cs.princeton.edu/~rwerneck/dominators/ . | 317 // See http://www.cs.princeton.edu/~rwerneck/dominators/ . |
316 | 318 |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 const char* function_name = parsed_function_.function().ToCString(); | 720 const char* function_name = parsed_function_.function().ToCString(); |
719 intptr_t len = OS::SNPrint(NULL, 0, kFormat, function_name, reason) + 1; | 721 intptr_t len = OS::SNPrint(NULL, 0, kFormat, function_name, reason) + 1; |
720 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); | 722 char* chars = Isolate::Current()->current_zone()->Alloc<char>(len); |
721 OS::SNPrint(chars, len, kFormat, function_name, reason); | 723 OS::SNPrint(chars, len, kFormat, function_name, reason); |
722 const Error& error = Error::Handle( | 724 const Error& error = Error::Handle( |
723 LanguageError::New(String::Handle(String::New(chars)))); | 725 LanguageError::New(String::Handle(String::New(chars)))); |
724 Isolate::Current()->long_jump_base()->Jump(1, error); | 726 Isolate::Current()->long_jump_base()->Jump(1, error); |
725 } | 727 } |
726 | 728 |
727 | 729 |
728 // Helper to reorder phis after splitting a block. The last instruction(s) of | 730 // Helper to replace a block. For each successor of 'old_block', the |
729 // the split block will now have a larger block id than any previously known | 731 // predecessors will be reordered to preserve block order sorting as well as the |
730 // blocks. If the last instruction jumps to a join, we must reorder phi inputs | 732 // phis if the successor is a join. |
731 // according to the block order, ie, we move this predecessor to the end. | 733 void FlowGraph::ReplaceBlock(BlockEntryInstr* old_block, |
Kevin Millikin (Google)
2012/10/22 14:13:11
I suggest ReplacePredecessor. It replaces old_blo
| |
732 static void ReorderPhis(BlockEntryInstr* block) { | 734 BlockEntryInstr* new_block) { |
733 GotoInstr* jump = block->last_instruction()->AsGoto(); | 735 // Set the last instruction of the new block to that of the old block. |
734 if (jump == NULL) return; | 736 Instruction* last = old_block->last_instruction(); |
735 JoinEntryInstr* join = jump->successor(); | 737 new_block->set_last_instruction(last); |
736 intptr_t pred_index = join->IndexOfPredecessor(block); | 738 // For each successor, update the predecessors. |
737 intptr_t pred_count = join->PredecessorCount(); | 739 for (intptr_t sidx = 0; sidx < last->SuccessorCount(); ++sidx) { |
738 ASSERT(pred_index >= 0); | 740 // If the successor is a target, update its predecessor. |
739 ASSERT(pred_index < pred_count); | 741 TargetEntryInstr* target = last->SuccessorAt(sidx)->AsTargetEntry(); |
740 // If the predecessor index is the last index there is nothing to update. | 742 if (target != NULL) { |
741 if ((join->phis() == NULL) || (pred_index + 1 == pred_count)) return; | 743 target->predecessor_ = new_block; |
742 // Otherwise, move the predecessor use to the end in each phi. | 744 continue; |
743 for (intptr_t i = 0; i < join->phis()->length(); ++i) { | |
744 PhiInstr* phi = (*join->phis())[i]; | |
745 if (phi == NULL) continue; | |
746 ASSERT(pred_count == phi->InputCount()); | |
747 // Save the predecessor use. | |
748 Value* pred_use = phi->InputAt(pred_index); | |
749 // Move each of the following uses back by one. | |
750 ASSERT(pred_index < pred_count - 1); // Will move at least one index. | |
751 for (intptr_t i = pred_index; i < pred_count - 1; ++i) { | |
752 Value* use = phi->InputAt(i + 1); | |
753 phi->SetInputAt(i, use); | |
754 use->set_use_index(i); | |
755 } | 745 } |
756 // Write the predecessor use at the end. | 746 // If the successor is a join, update each predecessor and the phis. |
757 phi->SetInputAt(pred_count - 1, pred_use); | 747 JoinEntryInstr* join = last->SuccessorAt(sidx)->AsJoinEntry(); |
758 pred_use->set_use_index(pred_count - 1); | 748 ASSERT(join != NULL); |
749 // Find the old predecessor index. | |
750 intptr_t old_index = join->IndexOfPredecessor(old_block); | |
751 intptr_t pred_count = join->PredecessorCount(); | |
752 ASSERT(old_index >= 0); | |
753 ASSERT(old_index < pred_count); | |
754 // Determine the iteration direction. | |
755 intptr_t new_id = new_block->block_id(); | |
756 intptr_t step, limit; | |
757 if (old_block->block_id() < new_id) { | |
758 step = 1; | |
759 limit = pred_count - 1; | |
760 } else { | |
761 step = -1; | |
762 limit = 0; | |
763 } | |
764 // Find the new predecessor index while reordering the predecessors. | |
765 intptr_t new_index = old_index; | |
766 for (; new_index != limit; new_index += step) { | |
767 if (join->predecessors_[new_index + step]->block_id() > new_id) break; | |
Kevin Millikin (Google)
2012/10/22 14:13:11
Split this loop so that the comparison works for b
| |
768 join->predecessors_[new_index] = join->predecessors_[new_index + step]; | |
769 } | |
770 join->predecessors_[new_index] = new_block; | |
771 // If the new and old predecessor index match there is nothing to update. | |
772 if ((join->phis() == NULL) || (old_index == new_index)) return; | |
773 // Otherwise, reorder the predecessor uses in each phi. | |
774 for (intptr_t i = 0; i < join->phis()->length(); ++i) { | |
775 PhiInstr* phi = (*join->phis())[i]; | |
776 if (phi == NULL) continue; | |
777 ASSERT(pred_count == phi->InputCount()); | |
778 // Save the predecessor use. | |
779 Value* pred_use = phi->InputAt(old_index); | |
780 // Move uses between old and new. | |
781 for (intptr_t use_idx = old_index; | |
782 use_idx != new_index; | |
783 use_idx += step) { | |
784 Value* use = phi->InputAt(use_idx + step); | |
785 phi->SetInputAt(use_idx, use); | |
786 use->set_use_index(use_idx); | |
787 } | |
788 // Write the predecessor use. | |
789 phi->SetInputAt(new_index, pred_use); | |
790 pred_use->set_use_index(new_index); | |
791 } | |
759 } | 792 } |
760 } | 793 } |
761 | 794 |
762 | 795 |
763 // Helper to sort a list of blocks. | 796 // Helper to sort a list of blocks. |
764 static int LowestBlockIdFirst(BlockEntryInstr* const* a, | 797 static int LowestBlockIdFirst(BlockEntryInstr* const* a, |
765 BlockEntryInstr* const* b) { | 798 BlockEntryInstr* const* b) { |
766 return (*a)->block_id() - (*b)->block_id(); | 799 return (*a)->block_id() - (*b)->block_id(); |
767 } | 800 } |
768 | 801 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
813 if (callee_exits->is_empty()) { | 846 if (callee_exits->is_empty()) { |
814 // TODO(zerny): Add support for non-local exits, such as throw. | 847 // TODO(zerny): Add support for non-local exits, such as throw. |
815 UNREACHABLE(); | 848 UNREACHABLE(); |
816 } else if (callee_exits->length() == 1) { | 849 } else if (callee_exits->length() == 1) { |
817 ReturnInstr* exit = (*callee_exits)[0]; | 850 ReturnInstr* exit = (*callee_exits)[0]; |
818 ASSERT(exit->previous() != NULL); | 851 ASSERT(exit->previous() != NULL); |
819 // For just one exit, replace the uses and remove the call from the graph. | 852 // For just one exit, replace the uses and remove the call from the graph. |
820 call->ReplaceUsesWith(exit->value()->definition()); | 853 call->ReplaceUsesWith(exit->value()->definition()); |
821 call->previous()->LinkTo(callee_entry->next()); | 854 call->previous()->LinkTo(callee_entry->next()); |
822 exit->previous()->LinkTo(call->next()); | 855 exit->previous()->LinkTo(call->next()); |
823 // In case of control flow, locally update the dominator tree. | 856 // In case of control flow, locally update the predecessors, phis and |
857 // dominator tree. | |
858 // TODO(zerny): should we leave the dominator tree since we recompute it | |
859 // after a full inlining pass? | |
824 if (callee_graph->preorder().length() > 2) { | 860 if (callee_graph->preorder().length() > 2) { |
825 // The caller block is split and the new block id is that of the exit | 861 BlockEntryInstr* exit_block = exit->GetBlock(); |
826 // block. If the caller block had outgoing edges, reorder the phis so they | 862 // Pictorially, the graph structure is: |
827 // are still ordered by block id. | 863 // |
828 ReorderPhis(caller_entry); | 864 // Bc : caller_entry Bi : callee_entry |
829 // The callee return is now the immediate dominator of blocks whose | 865 // before_call inlined_head |
866 // call ... other blocks ... | |
867 // after_call Be : exit_block | |
868 // inlined_foot | |
869 // And becomes: | |
870 // | |
871 // Bc : caller_entry | |
872 // before_call | |
873 // inlined_head | |
874 // ... other blocks ... | |
875 // Be : exit_block | |
876 // inlined_foot | |
877 // after_call | |
878 // | |
879 // For 'after_call', caller entry (Bc) is replaced by callee exit (Be). | |
880 ReplaceBlock(caller_entry, exit_block); | |
881 // For 'inlined_head', callee entry (Bi) is replaced by caller entry (Bc). | |
882 ReplaceBlock(callee_entry, caller_entry); | |
883 // The callee exit is now the immediate dominator of blocks whose | |
830 // immediate dominator was the caller entry. | 884 // immediate dominator was the caller entry. |
831 BlockEntryInstr* exit_block = exit->GetBlock(); | |
832 ASSERT(exit_block->dominated_blocks().is_empty()); | 885 ASSERT(exit_block->dominated_blocks().is_empty()); |
833 for (intptr_t i = 0; i < caller_entry->dominated_blocks().length(); ++i) { | 886 for (intptr_t i = 0; i < caller_entry->dominated_blocks().length(); ++i) { |
834 BlockEntryInstr* block = caller_entry->dominated_blocks()[i]; | 887 BlockEntryInstr* block = caller_entry->dominated_blocks()[i]; |
835 block->set_dominator(exit_block); | 888 block->set_dominator(exit_block); |
836 exit_block->AddDominatedBlock(block); | 889 exit_block->AddDominatedBlock(block); |
837 } | 890 } |
838 // The caller entry is now the immediate dominator of blocks whose | 891 // The caller entry is now the immediate dominator of blocks whose |
839 // immediate dominator was the callee entry. | 892 // immediate dominator was the callee entry. |
840 caller_entry->ClearDominatedBlocks(); | 893 caller_entry->ClearDominatedBlocks(); |
841 for (intptr_t i = 0; i < callee_entry->dominated_blocks().length(); ++i) { | 894 for (intptr_t i = 0; i < callee_entry->dominated_blocks().length(); ++i) { |
842 BlockEntryInstr* block = callee_entry->dominated_blocks()[i]; | 895 BlockEntryInstr* block = callee_entry->dominated_blocks()[i]; |
843 block->set_dominator(caller_entry); | 896 block->set_dominator(caller_entry); |
844 caller_entry->AddDominatedBlock(block); | 897 caller_entry->AddDominatedBlock(block); |
845 } | 898 } |
846 // Recompute the block orders. | |
847 DiscoverBlocks(); | |
848 } | 899 } |
849 } else { | 900 } else { |
850 // Sort the list of exits by block id. | 901 // Sort the list of exits by block id. |
851 GrowableArray<BlockEntryInstr*> exits(callee_exits->length()); | 902 GrowableArray<BlockEntryInstr*> exits(callee_exits->length()); |
852 for (intptr_t i = 0; i < callee_exits->length(); ++i) { | 903 for (intptr_t i = 0; i < callee_exits->length(); ++i) { |
853 exits.Add((*callee_exits)[i]->GetBlock()); | 904 exits.Add((*callee_exits)[i]->GetBlock()); |
854 } | 905 } |
855 exits.Sort(LowestBlockIdFirst); | 906 exits.Sort(LowestBlockIdFirst); |
856 // Create a join of the returns. | 907 // Create a join of the returns. |
857 JoinEntryInstr* join = | 908 JoinEntryInstr* join = |
(...skipping 19 matching lines...) Expand all Loading... | |
877 ReturnInstr* exit_instr = exits[i]->last_instruction()->AsReturn(); | 928 ReturnInstr* exit_instr = exits[i]->last_instruction()->AsReturn(); |
878 ASSERT(exit_instr != NULL); | 929 ASSERT(exit_instr != NULL); |
879 Value* use = exit_instr->value(); | 930 Value* use = exit_instr->value(); |
880 phi->SetInputAt(i, use); | 931 phi->SetInputAt(i, use); |
881 use->set_instruction(phi); | 932 use->set_instruction(phi); |
882 use->set_use_index(i); | 933 use->set_use_index(i); |
883 } | 934 } |
884 // Replace uses of the call with the phi. | 935 // Replace uses of the call with the phi. |
885 call->ReplaceUsesWith(phi); | 936 call->ReplaceUsesWith(phi); |
886 } | 937 } |
887 // Remove the call from the graph. | 938 // Remove the call from the graph. |
888 call->previous()->LinkTo(callee_entry->next()); | 939 call->previous()->LinkTo(callee_entry->next()); |
889 join->LinkTo(call->next()); | 940 join->LinkTo(call->next()); |
890 // The caller block is split and the new block id is that of the join | 941 // Replace the blocks after splitting (see comment in the len=1 case above). |
891 // block. If the caller block had outgoing edges, reorder the phis so they | 942 ReplaceBlock(caller_entry, join); |
892 // are still ordered by block id. | 943 ReplaceBlock(callee_entry, caller_entry); |
893 ReorderPhis(caller_entry); | 944 // Update the last instruction pointers on each exit (ie, to the new goto). |
894 // Adjust pre/post orders and update the dominator tree. | 945 for (intptr_t i = 0; i < exits.length(); ++i) { |
895 DiscoverBlocks(); | 946 exits[i]->set_last_instruction( |
947 exits[i]->last_instruction()->previous()->next()); | |
948 } | |
949 // Mark that the dominator tree is invalid. | |
896 // TODO(zerny): Compute the dominator frontier locally. | 950 // TODO(zerny): Compute the dominator frontier locally. |
951 invalid_dominator_tree_ = true; | |
952 } | |
953 } | |
954 | |
955 | |
956 void FlowGraph::RepairGraphAfterInlining() { | |
957 DiscoverBlocks(); | |
958 if (invalid_dominator_tree_) { | |
897 GrowableArray<BitVector*> dominance_frontier; | 959 GrowableArray<BitVector*> dominance_frontier; |
898 ComputeDominators(&dominance_frontier); | 960 ComputeDominators(&dominance_frontier); |
899 } | 961 } |
900 } | 962 } |
901 | 963 |
902 | 964 |
903 intptr_t FlowGraph::InstructionCount() const { | 965 intptr_t FlowGraph::InstructionCount() const { |
904 intptr_t size = 0; | 966 intptr_t size = 0; |
905 // Iterate each block, skipping the graph entry. | 967 // Iterate each block, skipping the graph entry. |
906 for (intptr_t i = 1; i < preorder_.length(); ++i) { | 968 for (intptr_t i = 1; i < preorder_.length(); ++i) { |
907 for (ForwardInstructionIterator it(preorder_[i]); | 969 for (ForwardInstructionIterator it(preorder_[i]); |
908 !it.Done(); | 970 !it.Done(); |
909 it.Advance()) { | 971 it.Advance()) { |
910 ++size; | 972 ++size; |
911 } | 973 } |
912 } | 974 } |
913 return size; | 975 return size; |
914 } | 976 } |
915 | 977 |
916 | 978 |
917 } // namespace dart | 979 } // namespace dart |
OLD | NEW |