OLD | NEW |
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/block_scheduler.h" | 5 #include "vm/block_scheduler.h" |
6 | 6 |
7 #include "vm/allocation.h" | 7 #include "vm/allocation.h" |
8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/flow_graph.h" | 10 #include "vm/flow_graph.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 static intptr_t GetEdgeCount(const Array& edge_counters, intptr_t edge_id) { | 14 static intptr_t GetEdgeCount(const Array& edge_counters, intptr_t edge_id) { |
15 if (!FLAG_reorder_basic_blocks) { | 15 if (!FLAG_reorder_basic_blocks) { |
16 // Assume everything was visited once. | 16 // Assume everything was visited once. |
17 return 1; | 17 return 1; |
18 } | 18 } |
19 return Smi::Value(Smi::RawCast(edge_counters.At(edge_id))); | 19 return Smi::Value(Smi::RawCast(edge_counters.At(edge_id))); |
20 } | 20 } |
21 | 21 |
22 | |
23 // There is an edge from instruction->successor. Set its weight (edge count | 22 // There is an edge from instruction->successor. Set its weight (edge count |
24 // per function entry). | 23 // per function entry). |
25 static void SetEdgeWeight(BlockEntryInstr* block, | 24 static void SetEdgeWeight(BlockEntryInstr* block, |
26 BlockEntryInstr* successor, | 25 BlockEntryInstr* successor, |
27 const Array& edge_counters, | 26 const Array& edge_counters, |
28 intptr_t entry_count) { | 27 intptr_t entry_count) { |
29 TargetEntryInstr* target = successor->AsTargetEntry(); | 28 TargetEntryInstr* target = successor->AsTargetEntry(); |
30 if (target != NULL) { | 29 if (target != NULL) { |
31 // If this block ends in a goto, the edge count of this edge is the same | 30 // If this block ends in a goto, the edge count of this edge is the same |
32 // as the count on the single outgoing edge. This is true as long as the | 31 // as the count on the single outgoing edge. This is true as long as the |
(...skipping 10 matching lines...) Expand all Loading... |
43 intptr_t count = GetEdgeCount(edge_counters, block->preorder_number()); | 42 intptr_t count = GetEdgeCount(edge_counters, block->preorder_number()); |
44 if ((count >= 0) && (entry_count != 0)) { | 43 if ((count >= 0) && (entry_count != 0)) { |
45 double weight = | 44 double weight = |
46 static_cast<double>(count) / static_cast<double>(entry_count); | 45 static_cast<double>(count) / static_cast<double>(entry_count); |
47 jump->set_edge_weight(weight); | 46 jump->set_edge_weight(weight); |
48 } | 47 } |
49 } | 48 } |
50 } | 49 } |
51 } | 50 } |
52 | 51 |
53 | |
54 void BlockScheduler::AssignEdgeWeights() const { | 52 void BlockScheduler::AssignEdgeWeights() const { |
55 if (!FLAG_reorder_basic_blocks) { | 53 if (!FLAG_reorder_basic_blocks) { |
56 return; | 54 return; |
57 } | 55 } |
58 | 56 |
59 const Array& ic_data_array = | 57 const Array& ic_data_array = |
60 Array::Handle(flow_graph()->zone(), | 58 Array::Handle(flow_graph()->zone(), |
61 flow_graph()->parsed_function().function().ic_data_array()); | 59 flow_graph()->parsed_function().function().ic_data_array()); |
62 if (Compiler::IsBackgroundCompilation() && ic_data_array.IsNull()) { | 60 if (Compiler::IsBackgroundCompilation() && ic_data_array.IsNull()) { |
63 // Deferred loading cleared ic_data_array. | 61 // Deferred loading cleared ic_data_array. |
(...skipping 16 matching lines...) Expand all Loading... |
80 !it.Done(); it.Advance()) { | 78 !it.Done(); it.Advance()) { |
81 BlockEntryInstr* block = it.Current(); | 79 BlockEntryInstr* block = it.Current(); |
82 Instruction* last = block->last_instruction(); | 80 Instruction* last = block->last_instruction(); |
83 for (intptr_t i = 0; i < last->SuccessorCount(); ++i) { | 81 for (intptr_t i = 0; i < last->SuccessorCount(); ++i) { |
84 BlockEntryInstr* succ = last->SuccessorAt(i); | 82 BlockEntryInstr* succ = last->SuccessorAt(i); |
85 SetEdgeWeight(block, succ, edge_counters, entry_count); | 83 SetEdgeWeight(block, succ, edge_counters, entry_count); |
86 } | 84 } |
87 } | 85 } |
88 } | 86 } |
89 | 87 |
90 | |
91 // A weighted control-flow graph edge. | 88 // A weighted control-flow graph edge. |
92 struct Edge { | 89 struct Edge { |
93 Edge(BlockEntryInstr* source, BlockEntryInstr* target, double weight) | 90 Edge(BlockEntryInstr* source, BlockEntryInstr* target, double weight) |
94 : source(source), target(target), weight(weight) {} | 91 : source(source), target(target), weight(weight) {} |
95 | 92 |
96 static int LowestWeightFirst(const Edge* a, const Edge* b); | 93 static int LowestWeightFirst(const Edge* a, const Edge* b); |
97 | 94 |
98 BlockEntryInstr* source; | 95 BlockEntryInstr* source; |
99 BlockEntryInstr* target; | 96 BlockEntryInstr* target; |
100 double weight; | 97 double weight; |
101 }; | 98 }; |
102 | 99 |
103 | |
104 // A linked list node in a chain of blocks. | 100 // A linked list node in a chain of blocks. |
105 struct Link : public ZoneAllocated { | 101 struct Link : public ZoneAllocated { |
106 Link(BlockEntryInstr* block, Link* next) : block(block), next(next) {} | 102 Link(BlockEntryInstr* block, Link* next) : block(block), next(next) {} |
107 | 103 |
108 BlockEntryInstr* block; | 104 BlockEntryInstr* block; |
109 Link* next; | 105 Link* next; |
110 }; | 106 }; |
111 | 107 |
112 | |
113 // A chain of blocks with first and last pointers for fast concatenation and | 108 // A chain of blocks with first and last pointers for fast concatenation and |
114 // a length to support adding a shorter chain's links to a longer chain. | 109 // a length to support adding a shorter chain's links to a longer chain. |
115 struct Chain : public ZoneAllocated { | 110 struct Chain : public ZoneAllocated { |
116 explicit Chain(BlockEntryInstr* block) | 111 explicit Chain(BlockEntryInstr* block) |
117 : first(new Link(block, NULL)), last(first), length(1) {} | 112 : first(new Link(block, NULL)), last(first), length(1) {} |
118 | 113 |
119 Link* first; | 114 Link* first; |
120 Link* last; | 115 Link* last; |
121 intptr_t length; | 116 intptr_t length; |
122 }; | 117 }; |
123 | 118 |
124 | |
125 int Edge::LowestWeightFirst(const Edge* a, const Edge* b) { | 119 int Edge::LowestWeightFirst(const Edge* a, const Edge* b) { |
126 if (a->weight < b->weight) { | 120 if (a->weight < b->weight) { |
127 return -1; | 121 return -1; |
128 } | 122 } |
129 return (a->weight > b->weight) ? 1 : 0; | 123 return (a->weight > b->weight) ? 1 : 0; |
130 } | 124 } |
131 | 125 |
132 | |
133 // Combine two chains by adding the shorter chain's links to the longer | 126 // Combine two chains by adding the shorter chain's links to the longer |
134 // chain. | 127 // chain. |
135 static void Union(GrowableArray<Chain*>* chains, | 128 static void Union(GrowableArray<Chain*>* chains, |
136 Chain* source_chain, | 129 Chain* source_chain, |
137 Chain* target_chain) { | 130 Chain* target_chain) { |
138 if (source_chain->length < target_chain->length) { | 131 if (source_chain->length < target_chain->length) { |
139 for (Link* link = source_chain->first; link != NULL; link = link->next) { | 132 for (Link* link = source_chain->first; link != NULL; link = link->next) { |
140 (*chains)[link->block->postorder_number()] = target_chain; | 133 (*chains)[link->block->postorder_number()] = target_chain; |
141 } | 134 } |
142 // Link the chains. | 135 // Link the chains. |
143 source_chain->last->next = target_chain->first; | 136 source_chain->last->next = target_chain->first; |
144 // Update the state of the longer chain. | 137 // Update the state of the longer chain. |
145 target_chain->first = source_chain->first; | 138 target_chain->first = source_chain->first; |
146 target_chain->length += source_chain->length; | 139 target_chain->length += source_chain->length; |
147 } else { | 140 } else { |
148 for (Link* link = target_chain->first; link != NULL; link = link->next) { | 141 for (Link* link = target_chain->first; link != NULL; link = link->next) { |
149 (*chains)[link->block->postorder_number()] = source_chain; | 142 (*chains)[link->block->postorder_number()] = source_chain; |
150 } | 143 } |
151 source_chain->last->next = target_chain->first; | 144 source_chain->last->next = target_chain->first; |
152 source_chain->last = target_chain->last; | 145 source_chain->last = target_chain->last; |
153 source_chain->length += target_chain->length; | 146 source_chain->length += target_chain->length; |
154 } | 147 } |
155 } | 148 } |
156 | 149 |
157 | |
158 void BlockScheduler::ReorderBlocks() const { | 150 void BlockScheduler::ReorderBlocks() const { |
159 // Add every block to a chain of length 1 and compute a list of edges | 151 // Add every block to a chain of length 1 and compute a list of edges |
160 // sorted by weight. | 152 // sorted by weight. |
161 intptr_t block_count = flow_graph()->preorder().length(); | 153 intptr_t block_count = flow_graph()->preorder().length(); |
162 GrowableArray<Edge> edges(2 * block_count); | 154 GrowableArray<Edge> edges(2 * block_count); |
163 | 155 |
164 // A map from a block's postorder number to the chain it is in. Used to | 156 // A map from a block's postorder number to the chain it is in. Used to |
165 // implement a simple (ordered) union-find data structure. Chains are | 157 // implement a simple (ordered) union-find data structure. Chains are |
166 // stored by pointer so that they are aliased (mutating one mutates all | 158 // stored by pointer so that they are aliased (mutating one mutates all |
167 // shared ones). Find(n) is simply chains[n]. | 159 // shared ones). Find(n) is simply chains[n]. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 for (intptr_t i = block_count - 1; i >= 0; --i) { | 202 for (intptr_t i = block_count - 1; i >= 0; --i) { |
211 if (chains[i]->first->block == flow_graph()->postorder()[i]) { | 203 if (chains[i]->first->block == flow_graph()->postorder()[i]) { |
212 for (Link* link = chains[i]->first; link != NULL; link = link->next) { | 204 for (Link* link = chains[i]->first; link != NULL; link = link->next) { |
213 flow_graph()->CodegenBlockOrder(true)->Add(link->block); | 205 flow_graph()->CodegenBlockOrder(true)->Add(link->block); |
214 } | 206 } |
215 } | 207 } |
216 } | 208 } |
217 } | 209 } |
218 | 210 |
219 } // namespace dart | 211 } // namespace dart |
OLD | NEW |