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

Side by Side Diff: src/compiler/control-equivalence.h

Issue 738613005: Restrict floating control to minimal control-connected component. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@local_scheduler-loop-1
Patch Set: Rebased and adapted. Created 6 years 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
« no previous file with comments | « BUILD.gn ('k') | src/compiler/scheduler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_COMPILER_CONTROL_EQUIVALENCE_H_
6 #define V8_COMPILER_CONTROL_EQUIVALENCE_H_
7
8 #include "src/v8.h"
9
10 #include "src/compiler/graph.h"
11 #include "src/compiler/node.h"
12 #include "src/compiler/node-properties.h"
13 #include "src/zone-containers.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
19 // Determines control dependence equivalence classes for control nodes. Any two
20 // nodes having the same set of control dependences land in one class. These
21 // classes can in turn be used to:
22 // - Build a program structure tree (PST) for controls in the graph.
23 // - Determine single-entry single-exit (SESE) regions within the graph.
24 //
25 // Note that this implementation actually uses cycle equivalence to establish
26 // class numbers. Any two nodes are cycle equivalent if they occur in the same
27 // set of cycles. It can be shown that control dependence equivalence reduces
28 // to undirected cycle equivalence for strongly connected control flow graphs.
29 //
30 // The algorithm is based on the paper, "The program structure tree: computing
31 // control regions in linear time" by Johnson, Pearson & Pingali (PLDI94) which
32 // also contains proofs for the aforementioned equivalence. References to line
33 // numbers in the algorithm from figure 4 have been added [line:x].
34 class ControlEquivalence : public ZoneObject {
35 public:
36 ControlEquivalence(Zone* zone, Graph* graph)
37 : zone_(zone),
38 graph_(graph),
39 dfs_number_(0),
40 class_number_(1),
41 node_data_(graph->NodeCount(), EmptyData(), zone) {}
42
43 // Run the main algorithm starting from the {exit} control node. This causes
44 // the following iterations over control edges of the graph:
45 // 1) A breadth-first backwards traversal to determine the set of nodes that
46 // participate in the next step. Takes O(E) time and O(N) space.
47 // 2) An undirected depth-first backwards traversal that determines class
48 // numbers for all participating nodes. Takes O(E) time and O(N) space.
49 void Run(Node* exit) {
50 if (GetClass(exit) != kInvalidClass) return;
51 DetermineParticipation(exit);
52 RunUndirectedDFS(exit);
53 }
54
55 // Retrieves a previously computed class number.
56 size_t ClassOf(Node* node) {
57 DCHECK(GetClass(node) != kInvalidClass);
58 return GetClass(node);
59 }
60
61 private:
62 static const size_t kInvalidClass = static_cast<size_t>(-1);
63 typedef enum { kInputDirection, kUseDirection } DFSDirection;
64
65 struct Bracket {
66 DFSDirection direction; // Direction in which this bracket was added.
67 size_t recent_class; // Cached class when bracket was topmost.
68 size_t recent_size; // Cached set-size when bracket was topmost.
69 Node* from; // Node that this bracket originates from.
70 Node* to; // Node that this bracket points to.
71 };
72
73 // The set of brackets for each node during the DFS walk.
74 typedef ZoneLinkedList<Bracket> BracketList;
75
76 struct DFSStackEntry {
77 DFSDirection direction; // Direction currently used in DFS walk.
78 Node::InputEdges::iterator input; // Iterator used for "input" direction.
79 Node::UseEdges::iterator use; // Iterator used for "use" direction.
80 Node* parent_node; // Parent node of entry during DFS walk.
81 Node* node; // Node that this stack entry belongs to.
82 };
83
84 // The stack is used during the undirected DFS walk.
85 typedef ZoneStack<DFSStackEntry> DFSStack;
86
87 struct NodeData {
88 size_t class_number; // Equivalence class number assigned to node.
89 size_t dfs_number; // Pre-order DFS number assigned to node.
90 bool on_stack; // Indicates node is on DFS stack during walk.
91 bool participates; // Indicates node participates in DFS walk.
92 BracketList blist; // List of brackets per node.
93 };
94
95 // The per-node data computed during the DFS walk.
96 typedef ZoneVector<NodeData> Data;
97
98 // Called at pre-visit during DFS walk.
99 void VisitPre(Node* node) {
100 Trace("CEQ: Pre-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
101
102 // Dispense a new pre-order number.
103 SetNumber(node, NewDFSNumber());
104 Trace(" Assigned DFS number is %d\n", GetNumber(node));
105 }
106
107 // Called at mid-visit during DFS walk.
108 void VisitMid(Node* node, DFSDirection direction) {
109 Trace("CEQ: Mid-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
110 BracketList& blist = GetBracketList(node);
111
112 // Remove brackets pointing to this node [line:19].
113 BracketListDelete(blist, node, direction);
114
115 // Potentially introduce artificial dependency from start to end.
116 if (blist.empty()) {
117 DCHECK_EQ(graph_->start(), node);
118 DCHECK_EQ(kInputDirection, direction);
119 VisitBackedge(graph_->start(), graph_->end(), kInputDirection);
120 }
121
122 // Potentially start a new equivalence class [line:37].
123 BracketListTrace(blist);
124 Bracket* recent = &blist.back();
125 if (recent->recent_size != blist.size()) {
126 recent->recent_size = blist.size();
127 recent->recent_class = NewClassNumber();
128 }
129
130 // Assign equivalence class to node.
131 SetClass(node, recent->recent_class);
132 Trace(" Assigned class number is %d\n", GetClass(node));
133 }
134
135 // Called at post-visit during DFS walk.
136 void VisitPost(Node* node, Node* parent_node, DFSDirection direction) {
137 Trace("CEQ: Post-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
138 BracketList& blist = GetBracketList(node);
139
140 // Remove brackets pointing to this node [line:19].
141 BracketListDelete(blist, node, direction);
142
143 // Propagate bracket list up the DFS tree [line:13].
144 if (parent_node != NULL) {
145 BracketList& parent_blist = GetBracketList(parent_node);
146 parent_blist.splice(parent_blist.end(), blist);
147 }
148 }
149
150 // Called when hitting a back edge in the DFS walk.
151 void VisitBackedge(Node* from, Node* to, DFSDirection direction) {
152 Trace("CEQ: Backedge from #%d:%s to #%d:%s\n", from->id(),
153 from->op()->mnemonic(), to->id(), to->op()->mnemonic());
154
155 // Push backedge onto the bracket list [line:25].
156 Bracket bracket = {direction, kInvalidClass, 0, from, to};
157 GetBracketList(from).push_back(bracket);
158 }
159
160 // Performs and undirected DFS walk of the graph. Conceptually all nodes are
161 // expanded, splitting "input" and "use" out into separate nodes. During the
162 // traversal, edges towards the representative nodes are preferred.
163 //
164 // \ / - Pre-visit: When N1 is visited in direction D the preferred
165 // x N1 edge towards N is taken next, calling VisitPre(N).
166 // | - Mid-visit: After all edges out of N2 in direction D have
167 // | N been visited, we switch the direction and start considering
168 // | edges out of N1 now, and we call VisitMid(N).
169 // x N2 - Post-visit: After all edges out of N1 in direction opposite
170 // / \ to D have been visited, we pop N and call VisitPost(N).
171 //
172 // This will yield a true spanning tree (without cross or forward edges) and
173 // also discover proper back edges in both directions.
174 void RunUndirectedDFS(Node* exit) {
175 ZoneStack<DFSStackEntry> stack(zone_);
176 DFSPush(stack, exit, NULL, kInputDirection);
177 VisitPre(exit);
178
179 while (!stack.empty()) { // Undirected depth-first backwards traversal.
180 DFSStackEntry& entry = stack.top();
181 Node* node = entry.node;
182
183 if (entry.direction == kInputDirection) {
184 if (entry.input != node->input_edges().end()) {
185 Edge edge = *entry.input;
186 Node* input = edge.to();
187 ++(entry.input);
188 if (NodeProperties::IsControlEdge(edge) &&
189 NodeProperties::IsControl(input)) {
190 // Visit next control input.
191 if (!GetData(input)->participates) continue;
192 if (GetData(input)->on_stack) {
193 // Found backedge if input is on stack.
194 if (input != entry.parent_node) {
195 VisitBackedge(node, input, kInputDirection);
196 }
197 } else {
198 // Push input onto stack.
199 DFSPush(stack, input, node, kInputDirection);
200 VisitPre(input);
201 }
202 }
203 continue;
204 }
205 if (entry.use != node->use_edges().end()) {
206 // Switch direction to uses.
207 entry.direction = kUseDirection;
208 VisitMid(node, kInputDirection);
209 continue;
210 }
211 }
212
213 if (entry.direction == kUseDirection) {
214 if (entry.use != node->use_edges().end()) {
215 Edge edge = *entry.use;
216 Node* use = edge.from();
217 ++(entry.use);
218 if (NodeProperties::IsControlEdge(edge) &&
219 NodeProperties::IsControl(use)) {
220 // Visit next control use.
221 if (!GetData(use)->participates) continue;
222 if (GetData(use)->on_stack) {
223 // Found backedge if use is on stack.
224 if (use != entry.parent_node) {
225 VisitBackedge(node, use, kUseDirection);
226 }
227 } else {
228 // Push use onto stack.
229 DFSPush(stack, use, node, kUseDirection);
230 VisitPre(use);
231 }
232 }
233 continue;
234 }
235 if (entry.input != node->input_edges().end()) {
236 // Switch direction to inputs.
237 entry.direction = kInputDirection;
238 VisitMid(node, kUseDirection);
239 continue;
240 }
241 }
242
243 // Pop node from stack when done with all inputs and uses.
244 DCHECK(entry.input == node->input_edges().end());
245 DCHECK(entry.use == node->use_edges().end());
246 DFSPop(stack, node);
247 VisitPost(node, entry.parent_node, entry.direction);
248 }
249 }
250
251 void DetermineParticipationEnqueue(ZoneQueue<Node*>& queue, Node* node) {
252 if (!GetData(node)->participates) {
253 GetData(node)->participates = true;
254 queue.push(node);
255 }
256 }
257
258 void DetermineParticipation(Node* exit) {
259 ZoneQueue<Node*> queue(zone_);
260 DetermineParticipationEnqueue(queue, exit);
261 while (!queue.empty()) { // Breadth-first backwards traversal.
262 Node* node = queue.front();
263 queue.pop();
264 int max = NodeProperties::PastControlIndex(node);
265 for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
266 DetermineParticipationEnqueue(queue, node->InputAt(i));
267 }
268 }
269 }
270
271 private:
272 NodeData* GetData(Node* node) { return &node_data_[node->id()]; }
273 int NewClassNumber() { return class_number_++; }
274 int NewDFSNumber() { return dfs_number_++; }
275
276 // Template used to initialize per-node data.
277 NodeData EmptyData() {
278 return {kInvalidClass, 0, false, false, BracketList(zone_)};
279 }
280
281 // Accessors for the DFS number stored within the per-node data.
282 size_t GetNumber(Node* node) { return GetData(node)->dfs_number; }
283 void SetNumber(Node* node, size_t number) {
284 GetData(node)->dfs_number = number;
285 }
286
287 // Accessors for the equivalence class stored within the per-node data.
288 size_t GetClass(Node* node) { return GetData(node)->class_number; }
289 void SetClass(Node* node, size_t number) {
290 GetData(node)->class_number = number;
291 }
292
293 // Accessors for the bracket list stored within the per-node data.
294 BracketList& GetBracketList(Node* node) { return GetData(node)->blist; }
295 void SetBracketList(Node* node, BracketList& list) {
296 GetData(node)->blist = list;
297 }
298
299 // Mutates the DFS stack by pushing an entry.
300 void DFSPush(DFSStack& stack, Node* node, Node* from, DFSDirection dir) {
301 DCHECK(GetData(node)->participates);
302 GetData(node)->on_stack = true;
303 Node::InputEdges::iterator input = node->input_edges().begin();
304 Node::UseEdges::iterator use = node->use_edges().begin();
305 stack.push({dir, input, use, from, node});
306 }
307
308 // Mutates the DFS stack by popping an entry.
309 void DFSPop(DFSStack& stack, Node* node) {
310 DCHECK_EQ(stack.top().node, node);
311 GetData(node)->on_stack = false;
312 GetData(node)->participates = false;
313 stack.pop();
314 }
315
316 // TODO(mstarzinger): Optimize this to avoid linear search.
317 void BracketListDelete(BracketList& blist, Node* to, DFSDirection direction) {
318 for (BracketList::iterator i = blist.begin(); i != blist.end(); /*nop*/) {
319 if (i->to == to && i->direction != direction) {
320 Trace(" BList erased: {%d->%d}\n", i->from->id(), i->to->id());
321 i = blist.erase(i);
322 } else {
323 ++i;
324 }
325 }
326 }
327
328 void BracketListTrace(BracketList& blist) {
329 if (FLAG_trace_turbo_scheduler) {
330 Trace(" BList: ");
331 for (Bracket bracket : blist) {
332 Trace("{%d->%d} ", bracket.from->id(), bracket.to->id());
333 }
334 Trace("\n");
335 }
336 }
337
338 void Trace(const char* msg, ...) {
339 if (FLAG_trace_turbo_scheduler) {
340 va_list arguments;
341 va_start(arguments, msg);
342 base::OS::VPrint(msg, arguments);
343 va_end(arguments);
344 }
345 }
346
347 Zone* zone_;
348 Graph* graph_;
349 int dfs_number_; // Generates new DFS pre-order numbers on demand.
350 int class_number_; // Generates new equivalence class numbers on demand.
351 Data node_data_; // Per-node data stored as a side-table.
352 };
353
354 } // namespace compiler
355 } // namespace internal
356 } // namespace v8
357
358 #endif // V8_COMPILER_CONTROL_EQUIVALENCE_H_
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | src/compiler/scheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698