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

Side by Side Diff: src/compiler/graph-reducer.cc

Issue 768763002: [turbofan] Add TraversalState and GraphTraversal and use them in GraphReducer. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: base embedded. 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 | « src/compiler/graph-reducer.h ('k') | src/compiler/node.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/compiler/graph-reducer.h" 5 #include "src/compiler/graph-reducer.h"
6 6
7 #include <functional> 7 #include <functional>
8 8
9 #include "src/compiler/graph-inl.h" 9 #include "src/compiler/graph-inl.h"
10 10
11 namespace v8 { 11 namespace v8 {
12 namespace internal { 12 namespace internal {
13 namespace compiler { 13 namespace compiler {
14 14
15 enum class GraphReducer::State : uint8_t { 15 enum class GraphReducer::State : uint8_t {
16 kUnvisited, 16 kUnvisited,
17 kRevisit, 17 kRevisit,
18 kOnStack, 18 kOnStack,
19 kVisited 19 kVisited
20 }; 20 };
21 21
22 22
23 GraphReducer::GraphReducer(Graph* graph, Zone* zone) 23 GraphReducer::GraphReducer(Graph* graph, Zone* zone)
24 : graph_(graph), 24 : graph_(graph),
25 state_(graph, 4),
25 reducers_(zone), 26 reducers_(zone),
26 revisit_(zone), 27 revisit_(zone),
27 stack_(zone), 28 stack_(zone) {}
28 state_(zone) {}
29 29
30 30
31 void GraphReducer::AddReducer(Reducer* reducer) { 31 void GraphReducer::AddReducer(Reducer* reducer) {
32 reducers_.push_back(reducer); 32 reducers_.push_back(reducer);
33 } 33 }
34 34
35 35
36 void GraphReducer::ReduceNode(Node* node) { 36 void GraphReducer::ReduceNode(Node* node) {
37 DCHECK(stack_.empty()); 37 DCHECK(stack_.empty());
38 DCHECK(revisit_.empty()); 38 DCHECK(revisit_.empty());
39 std::fill(state_.begin(), state_.end(), State::kUnvisited);
40 Push(node); 39 Push(node);
41 for (;;) { 40 for (;;) {
42 DCHECK(!stack_.empty() ||
43 std::find(state_.begin(), state_.end(), State::kOnStack) ==
44 state_.end());
45 if (!stack_.empty()) { 41 if (!stack_.empty()) {
46 // Process the node on the top of the stack, potentially pushing more or 42 // Process the node on the top of the stack, potentially pushing more or
47 // popping the node off the stack. 43 // popping the node off the stack.
48 ReduceTop(); 44 ReduceTop();
49 } else if (!revisit_.empty()) { 45 } else if (!revisit_.empty()) {
50 // If the stack becomes empty, revisit any nodes in the revisit queue. 46 // If the stack becomes empty, revisit any nodes in the revisit queue.
51 Node* const node = revisit_.top(); 47 Node* const node = revisit_.top();
52 revisit_.pop(); 48 revisit_.pop();
53 if (state_[node->id()] == State::kRevisit) { 49 if (state_.Get(node) == State::kRevisit) {
54 // state can change while in queue. 50 // state can change while in queue.
55 Push(node); 51 Push(node);
56 } 52 }
57 } else { 53 } else {
58 break; 54 break;
59 } 55 }
60 } 56 }
61 DCHECK(std::find(state_.begin(), state_.end(), State::kOnStack) ==
62 state_.end());
63 DCHECK(revisit_.empty()); 57 DCHECK(revisit_.empty());
64 DCHECK(stack_.empty()); 58 DCHECK(stack_.empty());
65 } 59 }
66 60
67 61
68 void GraphReducer::ReduceGraph() { ReduceNode(graph()->end()); } 62 void GraphReducer::ReduceGraph() { ReduceNode(graph()->end()); }
69 63
70 64
71 Reduction GraphReducer::Reduce(Node* const node) { 65 Reduction GraphReducer::Reduce(Node* const node) {
72 auto skip = reducers_.end(); 66 auto skip = reducers_.end();
(...skipping 21 matching lines...) Expand all
94 return Reducer::NoChange(); 88 return Reducer::NoChange();
95 } 89 }
96 // At least one reducer did some in-place reduction. 90 // At least one reducer did some in-place reduction.
97 return Reducer::Changed(node); 91 return Reducer::Changed(node);
98 } 92 }
99 93
100 94
101 void GraphReducer::ReduceTop() { 95 void GraphReducer::ReduceTop() {
102 NodeState& entry = stack_.top(); 96 NodeState& entry = stack_.top();
103 Node* node = entry.node; 97 Node* node = entry.node;
104 DCHECK(state_[node->id()] == State::kOnStack); 98 DCHECK(state_.Get(node) == State::kOnStack);
105 99
106 if (node->IsDead()) return Pop(); // Node was killed while on stack. 100 if (node->IsDead()) return Pop(); // Node was killed while on stack.
107 101
108 // Recurse on an input if necessary. 102 // Recurse on an input if necessary.
109 int start = entry.input_index < node->InputCount() ? entry.input_index : 0; 103 int start = entry.input_index < node->InputCount() ? entry.input_index : 0;
110 for (int i = start; i < node->InputCount(); i++) { 104 for (int i = start; i < node->InputCount(); i++) {
111 Node* input = node->InputAt(i); 105 Node* input = node->InputAt(i);
112 entry.input_index = i + 1; 106 entry.input_index = i + 1;
113 if (input != node && Recurse(input)) return; 107 if (input != node && Recurse(input)) return;
114 } 108 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 151
158 // If there was a replacement, reduce it after popping {node}. 152 // If there was a replacement, reduce it after popping {node}.
159 Recurse(replacement); 153 Recurse(replacement);
160 } 154 }
161 } 155 }
162 } 156 }
163 } 157 }
164 158
165 159
166 void GraphReducer::Pop() { 160 void GraphReducer::Pop() {
167 Node* const node = stack_.top().node; 161 Node* node = stack_.top().node;
168 state_[node->id()] = State::kVisited; 162 state_.Set(node, State::kVisited);
169 stack_.pop(); 163 stack_.pop();
170 } 164 }
171 165
172 166
173 void GraphReducer::Push(Node* const node) { 167 void GraphReducer::Push(Node* const node) {
174 size_t const id = static_cast<size_t>(node->id()); 168 DCHECK(state_.Get(node) != State::kOnStack);
175 if (id >= state_.size()) state_.resize(id + 1); 169 state_.Set(node, State::kOnStack);
176 DCHECK(id < state_.size());
177 DCHECK(state_[id] != State::kOnStack);
178 state_[id] = State::kOnStack;
179 stack_.push({node, 0}); 170 stack_.push({node, 0});
180 } 171 }
181 172
182 173
183 bool GraphReducer::Recurse(Node* const node) { 174 bool GraphReducer::Recurse(Node* node) {
184 size_t const id = static_cast<size_t>(node->id()); 175 if (state_.Get(node) > State::kRevisit) return false;
185 if (id < state_.size() && state_[id] > State::kRevisit) return false;
186 Push(node); 176 Push(node);
187 return true; 177 return true;
188 } 178 }
189 179
190 180
191 void GraphReducer::Revisit(Node* const node) { 181 void GraphReducer::Revisit(Node* node) {
192 size_t const id = static_cast<size_t>(node->id()); 182 if (state_.Get(node) == State::kVisited) {
193 if (id < state_.size() && state_[id] == State::kVisited) { 183 state_.Set(node, State::kRevisit);
194 state_[id] = State::kRevisit;
195 revisit_.push(node); 184 revisit_.push(node);
196 } 185 }
197 } 186 }
198 187
199 } // namespace compiler 188 } // namespace compiler
200 } // namespace internal 189 } // namespace internal
201 } // namespace v8 190 } // namespace v8
OLDNEW
« no previous file with comments | « src/compiler/graph-reducer.h ('k') | src/compiler/node.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698