| 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 994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1005 instr_it.RemoveCurrentFromGraph(); | 1005 instr_it.RemoveCurrentFromGraph(); |
| 1006 } | 1006 } |
| 1007 } | 1007 } |
| 1008 } | 1008 } |
| 1009 } | 1009 } |
| 1010 | 1010 |
| 1011 | 1011 |
| 1012 // Find the natural loop for the back edge m->n and attach loop information | 1012 // Find the natural loop for the back edge m->n and attach loop information |
| 1013 // to block n (loop header). The algorithm is described in "Advanced Compiler | 1013 // to block n (loop header). The algorithm is described in "Advanced Compiler |
| 1014 // Design & Implementation" (Muchnick) p192. | 1014 // Design & Implementation" (Muchnick) p192. |
| 1015 void FlowGraph::FindLoop(BlockEntryInstr* m, BlockEntryInstr* n) { | 1015 BitVector* FlowGraph::FindLoop(BlockEntryInstr* m, BlockEntryInstr* n) { |
| 1016 GrowableArray<BlockEntryInstr*> stack; | 1016 GrowableArray<BlockEntryInstr*> stack; |
| 1017 BitVector* loop = new BitVector(preorder_.length()); | 1017 BitVector* loop = new BitVector(preorder_.length()); |
| 1018 | 1018 |
| 1019 loop->Add(n->preorder_number()); | 1019 loop->Add(n->preorder_number()); |
| 1020 if (n != m) { | 1020 if (n != m) { |
| 1021 loop->Add(m->preorder_number()); | 1021 loop->Add(m->preorder_number()); |
| 1022 stack.Add(m); | 1022 stack.Add(m); |
| 1023 } | 1023 } |
| 1024 | 1024 |
| 1025 while (!stack.is_empty()) { | 1025 while (!stack.is_empty()) { |
| 1026 BlockEntryInstr* p = stack.RemoveLast(); | 1026 BlockEntryInstr* p = stack.RemoveLast(); |
| 1027 for (intptr_t i = 0; i < p->PredecessorCount(); ++i) { | 1027 for (intptr_t i = 0; i < p->PredecessorCount(); ++i) { |
| 1028 BlockEntryInstr* q = p->PredecessorAt(i); | 1028 BlockEntryInstr* q = p->PredecessorAt(i); |
| 1029 if (!loop->Contains(q->preorder_number())) { | 1029 if (!loop->Contains(q->preorder_number())) { |
| 1030 loop->Add(q->preorder_number()); | 1030 loop->Add(q->preorder_number()); |
| 1031 stack.Add(q); | 1031 stack.Add(q); |
| 1032 } | 1032 } |
| 1033 } | 1033 } |
| 1034 } | 1034 } |
| 1035 n->set_loop_info(loop); | 1035 return loop; |
| 1036 if (FLAG_trace_optimization) { | |
| 1037 for (BitVector::Iterator it(loop); !it.Done(); it.Advance()) { | |
| 1038 OS::Print(" B%" Pd "\n", preorder_[it.Current()]->block_id()); | |
| 1039 } | |
| 1040 } | |
| 1041 } | 1036 } |
| 1042 | 1037 |
| 1043 | 1038 |
| 1044 ZoneGrowableArray<BlockEntryInstr*>* FlowGraph::ComputeLoops() { | 1039 ZoneGrowableArray<BlockEntryInstr*>* FlowGraph::ComputeLoops() { |
| 1045 ZoneGrowableArray<BlockEntryInstr*>* loop_headers = | 1040 ZoneGrowableArray<BlockEntryInstr*>* loop_headers = |
| 1046 new ZoneGrowableArray<BlockEntryInstr*>(); | 1041 new ZoneGrowableArray<BlockEntryInstr*>(); |
| 1047 | 1042 |
| 1048 for (BlockIterator it = postorder_iterator(); | 1043 for (BlockIterator it = postorder_iterator(); |
| 1049 !it.Done(); | 1044 !it.Done(); |
| 1050 it.Advance()) { | 1045 it.Advance()) { |
| 1051 BlockEntryInstr* block = it.Current(); | 1046 BlockEntryInstr* block = it.Current(); |
| 1052 for (intptr_t i = 0; i < block->PredecessorCount(); ++i) { | 1047 for (intptr_t i = 0; i < block->PredecessorCount(); ++i) { |
| 1053 BlockEntryInstr* pred = block->PredecessorAt(i); | 1048 BlockEntryInstr* pred = block->PredecessorAt(i); |
| 1054 if (block->Dominates(pred)) { | 1049 if (block->Dominates(pred)) { |
| 1055 if (FLAG_trace_optimization) { | 1050 if (FLAG_trace_optimization) { |
| 1056 OS::Print("Back edge B%" Pd " -> B%" Pd "\n", pred->block_id(), | 1051 OS::Print("Back edge B%" Pd " -> B%" Pd "\n", pred->block_id(), |
| 1057 block->block_id()); | 1052 block->block_id()); |
| 1058 } | 1053 } |
| 1059 FindLoop(pred, block); | 1054 BitVector* loop_info = FindLoop(pred, block); |
| 1060 loop_headers->Add(block); | 1055 // Loops that share the same loop header are treated as one loop. |
| 1056 BlockEntryInstr* header = NULL; |
| 1057 for (intptr_t i = 0; i < loop_headers->length(); ++i) { |
| 1058 if ((*loop_headers)[i] == block) { |
| 1059 header = (*loop_headers)[i]; |
| 1060 break; |
| 1061 } |
| 1062 } |
| 1063 if (header != NULL) { |
| 1064 header->loop_info()->AddAll(loop_info); |
| 1065 } else { |
| 1066 block->set_loop_info(loop_info); |
| 1067 loop_headers->Add(block); |
| 1068 } |
| 1061 } | 1069 } |
| 1062 } | 1070 } |
| 1063 } | 1071 } |
| 1064 | 1072 if (FLAG_trace_optimization) { |
| 1073 for (intptr_t i = 0; i < loop_headers->length(); ++i) { |
| 1074 BlockEntryInstr* header = (*loop_headers)[i]; |
| 1075 OS::Print("Loop header B%" Pd "\n", header->block_id()); |
| 1076 for (BitVector::Iterator it(header->loop_info()); |
| 1077 !it.Done(); |
| 1078 it.Advance()) { |
| 1079 OS::Print(" B%" Pd "\n", preorder_[it.Current()]->block_id()); |
| 1080 } |
| 1081 } |
| 1082 } |
| 1065 return loop_headers; | 1083 return loop_headers; |
| 1066 } | 1084 } |
| 1067 | 1085 |
| 1068 | 1086 |
| 1069 void FlowGraph::Bailout(const char* reason) const { | 1087 void FlowGraph::Bailout(const char* reason) const { |
| 1070 const Function& function = parsed_function_.function(); | 1088 const Function& function = parsed_function_.function(); |
| 1071 const Error& error = Error::Handle( | 1089 const Error& error = Error::Handle( |
| 1072 LanguageError::NewFormatted(Error::Handle(), // No previous error. | 1090 LanguageError::NewFormatted(Error::Handle(), // No previous error. |
| 1073 Script::Handle(function.script()), | 1091 Script::Handle(function.script()), |
| 1074 function.token_pos(), | 1092 function.token_pos(), |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1198 } | 1216 } |
| 1199 | 1217 |
| 1200 | 1218 |
| 1201 bool BlockEffects::IsSideEffectFreePath(BlockEntryInstr* from, | 1219 bool BlockEffects::IsSideEffectFreePath(BlockEntryInstr* from, |
| 1202 BlockEntryInstr* to) const { | 1220 BlockEntryInstr* to) const { |
| 1203 return available_at_[to->postorder_number()]->Contains( | 1221 return available_at_[to->postorder_number()]->Contains( |
| 1204 from->postorder_number()); | 1222 from->postorder_number()); |
| 1205 } | 1223 } |
| 1206 | 1224 |
| 1207 } // namespace dart | 1225 } // namespace dart |
| OLD | NEW |