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.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, |
| 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(); | |
|
srdjan
2012/10/24 15:10:23
const intptr_t old_index (and pred_count). The cod
| |
| 752 ASSERT(old_index >= 0); | |
| 753 ASSERT(old_index < pred_count); | |
| 754 // Find the new predecessor index while reordering the predecessors. | |
| 755 intptr_t new_id = new_block->block_id(); | |
|
srdjan
2012/10/24 15:10:23
ditto for newid
| |
| 756 intptr_t new_index = old_index; | |
| 757 if (old_block->block_id() < new_id) { | |
|
srdjan
2012/10/24 15:10:23
Add a comment that you expect the blocks to be sor
| |
| 758 for (; new_index < pred_count - 1; ++new_index) { | |
| 759 if (join->predecessors_[new_index + 1]->block_id() > new_id) break; | |
| 760 join->predecessors_[new_index] = join->predecessors_[new_index + 1]; | |
| 761 } | |
| 762 } else { | |
| 763 for (; new_index > 0; --new_index) { | |
| 764 if (join->predecessors_[new_index - 1]->block_id() < new_id) break; | |
| 765 join->predecessors_[new_index] = join->predecessors_[new_index - 1]; | |
| 766 } | |
| 767 } | |
| 768 join->predecessors_[new_index] = new_block; | |
| 769 // If the new and old predecessor index match there is nothing to update. | |
| 770 if ((join->phis() == NULL) || (old_index == new_index)) return; | |
| 771 // Otherwise, reorder the predecessor uses in each phi. | |
| 772 for (intptr_t i = 0; i < join->phis()->length(); ++i) { | |
| 773 PhiInstr* phi = (*join->phis())[i]; | |
| 774 if (phi == NULL) continue; | |
| 775 ASSERT(pred_count == phi->InputCount()); | |
| 776 // Save the predecessor use. | |
| 777 Value* pred_use = phi->InputAt(old_index); | |
| 778 // Move uses between old and new. | |
| 779 intptr_t step = (old_index < new_index) ? 1 : -1; | |
| 780 for (intptr_t use_idx = old_index; | |
| 781 use_idx != new_index; | |
| 782 use_idx += step) { | |
| 783 Value* use = phi->InputAt(use_idx + step); | |
| 784 phi->SetInputAt(use_idx, use); | |
| 785 use->set_use_index(use_idx); | |
| 786 } | |
| 787 // Write the predecessor use. | |
| 788 phi->SetInputAt(new_index, pred_use); | |
| 789 pred_use->set_use_index(new_index); | |
| 790 } | |
| 759 } | 791 } |
| 760 } | 792 } |
| 761 | 793 |
| 762 | 794 |
| 763 // Helper to sort a list of blocks. | 795 // Helper to sort a list of blocks. |
| 764 static int LowestBlockIdFirst(BlockEntryInstr* const* a, | 796 static int LowestBlockIdFirst(BlockEntryInstr* const* a, |
| 765 BlockEntryInstr* const* b) { | 797 BlockEntryInstr* const* b) { |
| 766 return (*a)->block_id() - (*b)->block_id(); | 798 return (*a)->block_id() - (*b)->block_id(); |
| 767 } | 799 } |
| 768 | 800 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 if (callee_exits->is_empty()) { | 845 if (callee_exits->is_empty()) { |
| 814 // TODO(zerny): Add support for non-local exits, such as throw. | 846 // TODO(zerny): Add support for non-local exits, such as throw. |
| 815 UNREACHABLE(); | 847 UNREACHABLE(); |
| 816 } else if (callee_exits->length() == 1) { | 848 } else if (callee_exits->length() == 1) { |
| 817 ReturnInstr* exit = (*callee_exits)[0]; | 849 ReturnInstr* exit = (*callee_exits)[0]; |
| 818 ASSERT(exit->previous() != NULL); | 850 ASSERT(exit->previous() != NULL); |
| 819 // For just one exit, replace the uses and remove the call from the graph. | 851 // For just one exit, replace the uses and remove the call from the graph. |
| 820 call->ReplaceUsesWith(exit->value()->definition()); | 852 call->ReplaceUsesWith(exit->value()->definition()); |
| 821 call->previous()->LinkTo(callee_entry->next()); | 853 call->previous()->LinkTo(callee_entry->next()); |
| 822 exit->previous()->LinkTo(call->next()); | 854 exit->previous()->LinkTo(call->next()); |
| 823 // In case of control flow, locally update the dominator tree. | 855 // In case of control flow, locally update the predecessors, phis and |
| 856 // dominator tree. | |
| 857 // TODO(zerny): should we leave the dominator tree since we recompute it | |
| 858 // after a full inlining pass? | |
| 824 if (callee_graph->preorder().length() > 2) { | 859 if (callee_graph->preorder().length() > 2) { |
| 825 // The caller block is split and the new block id is that of the exit | 860 BlockEntryInstr* exit_block = exit->GetBlock(); |
| 826 // block. If the caller block had outgoing edges, reorder the phis so they | 861 // Pictorially, the graph structure is: |
| 827 // are still ordered by block id. | 862 // |
| 828 ReorderPhis(caller_entry); | 863 // Bc : caller_entry Bi : callee_entry |
| 829 // The callee return is now the immediate dominator of blocks whose | 864 // before_call inlined_head |
| 865 // call ... other blocks ... | |
| 866 // after_call Be : exit_block | |
| 867 // inlined_foot | |
| 868 // And becomes: | |
| 869 // | |
| 870 // Bc : caller_entry | |
| 871 // before_call | |
| 872 // inlined_head | |
| 873 // ... other blocks ... | |
| 874 // Be : exit_block | |
| 875 // inlined_foot | |
| 876 // after_call | |
| 877 // | |
| 878 // For 'after_call', caller entry (Bc) is replaced by callee exit (Be). | |
| 879 ReplaceBlock(caller_entry, exit_block); | |
| 880 // For 'inlined_head', callee entry (Bi) is replaced by caller entry (Bc). | |
| 881 ReplaceBlock(callee_entry, caller_entry); | |
| 882 // The callee exit is now the immediate dominator of blocks whose | |
| 830 // immediate dominator was the caller entry. | 883 // immediate dominator was the caller entry. |
| 831 BlockEntryInstr* exit_block = exit->GetBlock(); | |
| 832 ASSERT(exit_block->dominated_blocks().is_empty()); | 884 ASSERT(exit_block->dominated_blocks().is_empty()); |
| 833 for (intptr_t i = 0; i < caller_entry->dominated_blocks().length(); ++i) { | 885 for (intptr_t i = 0; i < caller_entry->dominated_blocks().length(); ++i) { |
| 834 BlockEntryInstr* block = caller_entry->dominated_blocks()[i]; | 886 BlockEntryInstr* block = caller_entry->dominated_blocks()[i]; |
| 835 block->set_dominator(exit_block); | 887 block->set_dominator(exit_block); |
| 836 exit_block->AddDominatedBlock(block); | 888 exit_block->AddDominatedBlock(block); |
| 837 } | 889 } |
| 838 // The caller entry is now the immediate dominator of blocks whose | 890 // The caller entry is now the immediate dominator of blocks whose |
| 839 // immediate dominator was the callee entry. | 891 // immediate dominator was the callee entry. |
| 840 caller_entry->ClearDominatedBlocks(); | 892 caller_entry->ClearDominatedBlocks(); |
| 841 for (intptr_t i = 0; i < callee_entry->dominated_blocks().length(); ++i) { | 893 for (intptr_t i = 0; i < callee_entry->dominated_blocks().length(); ++i) { |
| 842 BlockEntryInstr* block = callee_entry->dominated_blocks()[i]; | 894 BlockEntryInstr* block = callee_entry->dominated_blocks()[i]; |
| 843 block->set_dominator(caller_entry); | 895 block->set_dominator(caller_entry); |
| 844 caller_entry->AddDominatedBlock(block); | 896 caller_entry->AddDominatedBlock(block); |
| 845 } | 897 } |
| 846 // Recompute the block orders. | |
| 847 DiscoverBlocks(); | |
| 848 } | 898 } |
| 849 } else { | 899 } else { |
| 850 // Sort the list of exits by block id. | 900 // Sort the list of exits by block id. |
| 851 GrowableArray<BlockEntryInstr*> exits(callee_exits->length()); | 901 GrowableArray<BlockEntryInstr*> exits(callee_exits->length()); |
| 852 for (intptr_t i = 0; i < callee_exits->length(); ++i) { | 902 for (intptr_t i = 0; i < callee_exits->length(); ++i) { |
| 853 exits.Add((*callee_exits)[i]->GetBlock()); | 903 exits.Add((*callee_exits)[i]->GetBlock()); |
| 854 } | 904 } |
| 855 exits.Sort(LowestBlockIdFirst); | 905 exits.Sort(LowestBlockIdFirst); |
| 856 // Create a join of the returns. | 906 // Create a join of the returns. |
| 857 JoinEntryInstr* join = | 907 JoinEntryInstr* join = |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 877 ReturnInstr* exit_instr = exits[i]->last_instruction()->AsReturn(); | 927 ReturnInstr* exit_instr = exits[i]->last_instruction()->AsReturn(); |
| 878 ASSERT(exit_instr != NULL); | 928 ASSERT(exit_instr != NULL); |
| 879 Value* use = exit_instr->value(); | 929 Value* use = exit_instr->value(); |
| 880 phi->SetInputAt(i, use); | 930 phi->SetInputAt(i, use); |
| 881 use->set_instruction(phi); | 931 use->set_instruction(phi); |
| 882 use->set_use_index(i); | 932 use->set_use_index(i); |
| 883 } | 933 } |
| 884 // Replace uses of the call with the phi. | 934 // Replace uses of the call with the phi. |
| 885 call->ReplaceUsesWith(phi); | 935 call->ReplaceUsesWith(phi); |
| 886 } | 936 } |
| 887 // Remove the call from the graph. | 937 // Remove the call from the graph. |
| 888 call->previous()->LinkTo(callee_entry->next()); | 938 call->previous()->LinkTo(callee_entry->next()); |
| 889 join->LinkTo(call->next()); | 939 join->LinkTo(call->next()); |
| 890 // The caller block is split and the new block id is that of the join | 940 // 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 | 941 ReplaceBlock(caller_entry, join); |
| 892 // are still ordered by block id. | 942 ReplaceBlock(callee_entry, caller_entry); |
| 893 ReorderPhis(caller_entry); | 943 // Update the last instruction pointers on each exit (ie, to the new goto). |
| 894 // Adjust pre/post orders and update the dominator tree. | 944 for (intptr_t i = 0; i < exits.length(); ++i) { |
| 895 DiscoverBlocks(); | 945 exits[i]->set_last_instruction( |
| 946 exits[i]->last_instruction()->previous()->next()); | |
| 947 } | |
| 948 // Mark that the dominator tree is invalid. | |
| 896 // TODO(zerny): Compute the dominator frontier locally. | 949 // TODO(zerny): Compute the dominator frontier locally. |
| 950 invalid_dominator_tree_ = true; | |
| 951 } | |
| 952 } | |
| 953 | |
| 954 | |
| 955 void FlowGraph::RepairGraphAfterInlining() { | |
| 956 DiscoverBlocks(); | |
| 957 if (invalid_dominator_tree_) { | |
| 897 GrowableArray<BitVector*> dominance_frontier; | 958 GrowableArray<BitVector*> dominance_frontier; |
| 898 ComputeDominators(&dominance_frontier); | 959 ComputeDominators(&dominance_frontier); |
| 899 } | 960 } |
| 900 } | 961 } |
| 901 | 962 |
| 902 | 963 |
| 903 intptr_t FlowGraph::InstructionCount() const { | 964 intptr_t FlowGraph::InstructionCount() const { |
| 904 intptr_t size = 0; | 965 intptr_t size = 0; |
| 905 // Iterate each block, skipping the graph entry. | 966 // Iterate each block, skipping the graph entry. |
| 906 for (intptr_t i = 1; i < preorder_.length(); ++i) { | 967 for (intptr_t i = 1; i < preorder_.length(); ++i) { |
| 907 for (ForwardInstructionIterator it(preorder_[i]); | 968 for (ForwardInstructionIterator it(preorder_[i]); |
| 908 !it.Done(); | 969 !it.Done(); |
| 909 it.Advance()) { | 970 it.Advance()) { |
| 910 ++size; | 971 ++size; |
| 911 } | 972 } |
| 912 } | 973 } |
| 913 return size; | 974 return size; |
| 914 } | 975 } |
| 915 | 976 |
| 916 | 977 |
| 917 } // namespace dart | 978 } // namespace dart |
| OLD | NEW |