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 #include <functional>
6 #include <limits>
7
8 #include "src/compiler/graph.h"
9 #include "src/compiler/graph-reducer.h"
10 #include "src/compiler/node.h"
11 #include "src/compiler/node-properties.h"
12 #include "src/compiler/verifier.h"
13
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17
18 enum class GraphReducer::State : uint8_t {
19 kUnvisited,
20 kRevisit,
21 kOnStack,
22 kVisited
23 };
24
25
Finalize()26 void Reducer::Finalize() {}
27
28
GraphReducer(Zone * zone,Graph * graph,Node * dead)29 GraphReducer::GraphReducer(Zone* zone, Graph* graph, Node* dead)
30 : graph_(graph),
31 dead_(dead),
32 state_(graph, 4),
33 reducers_(zone),
34 revisit_(zone),
35 stack_(zone) {}
36
37
~GraphReducer()38 GraphReducer::~GraphReducer() {}
39
40
AddReducer(Reducer * reducer)41 void GraphReducer::AddReducer(Reducer* reducer) {
42 reducers_.push_back(reducer);
43 }
44
45
ReduceNode(Node * node)46 void GraphReducer::ReduceNode(Node* node) {
47 DCHECK(stack_.empty());
48 DCHECK(revisit_.empty());
49 Push(node);
50 for (;;) {
51 if (!stack_.empty()) {
52 // Process the node on the top of the stack, potentially pushing more or
53 // popping the node off the stack.
54 ReduceTop();
55 } else if (!revisit_.empty()) {
56 // If the stack becomes empty, revisit any nodes in the revisit queue.
57 Node* const node = revisit_.top();
58 revisit_.pop();
59 if (state_.Get(node) == State::kRevisit) {
60 // state can change while in queue.
61 Push(node);
62 }
63 } else {
64 // Run all finalizers.
65 for (Reducer* const reducer : reducers_) reducer->Finalize();
66
67 // Check if we have new nodes to revisit.
68 if (revisit_.empty()) break;
69 }
70 }
71 DCHECK(revisit_.empty());
72 DCHECK(stack_.empty());
73 }
74
75
ReduceGraph()76 void GraphReducer::ReduceGraph() { ReduceNode(graph()->end()); }
77
78
Reduce(Node * const node)79 Reduction GraphReducer::Reduce(Node* const node) {
80 auto skip = reducers_.end();
81 for (auto i = reducers_.begin(); i != reducers_.end();) {
82 if (i != skip) {
83 Reduction reduction = (*i)->Reduce(node);
84 if (!reduction.Changed()) {
85 // No change from this reducer.
86 } else if (reduction.replacement() == node) {
87 // {replacement} == {node} represents an in-place reduction. Rerun
88 // all the other reducers for this node, as now there may be more
89 // opportunities for reduction.
90 skip = i;
91 i = reducers_.begin();
92 continue;
93 } else {
94 // {node} was replaced by another node.
95 return reduction;
96 }
97 }
98 ++i;
99 }
100 if (skip == reducers_.end()) {
101 // No change from any reducer.
102 return Reducer::NoChange();
103 }
104 // At least one reducer did some in-place reduction.
105 return Reducer::Changed(node);
106 }
107
108
ReduceTop()109 void GraphReducer::ReduceTop() {
110 NodeState& entry = stack_.top();
111 Node* node = entry.node;
112 DCHECK(state_.Get(node) == State::kOnStack);
113
114 if (node->IsDead()) return Pop(); // Node was killed while on stack.
115
116 // Recurse on an input if necessary.
117 int start = entry.input_index < node->InputCount() ? entry.input_index : 0;
118 for (int i = start; i < node->InputCount(); i++) {
119 Node* input = node->InputAt(i);
120 entry.input_index = i + 1;
121 if (input != node && Recurse(input)) return;
122 }
123 for (int i = 0; i < start; i++) {
124 Node* input = node->InputAt(i);
125 entry.input_index = i + 1;
126 if (input != node && Recurse(input)) return;
127 }
128
129 // Remember the max node id before reduction.
130 NodeId const max_id = static_cast<NodeId>(graph()->NodeCount() - 1);
131
132 // All inputs should be visited or on stack. Apply reductions to node.
133 Reduction reduction = Reduce(node);
134
135 // If there was no reduction, pop {node} and continue.
136 if (!reduction.Changed()) return Pop();
137
138 // Check if the reduction is an in-place update of the {node}.
139 Node* const replacement = reduction.replacement();
140 if (replacement == node) {
141 // In-place update of {node}, may need to recurse on an input.
142 for (int i = 0; i < node->InputCount(); ++i) {
143 Node* input = node->InputAt(i);
144 entry.input_index = i + 1;
145 if (input != node && Recurse(input)) return;
146 }
147 }
148
149 // After reducing the node, pop it off the stack.
150 Pop();
151
152 // Check if we have a new replacement.
153 if (replacement != node) {
154 Replace(node, replacement, max_id);
155 } else {
156 // Revisit all uses of the node.
157 for (Node* const user : node->uses()) {
158 // Don't revisit this node if it refers to itself.
159 if (user != node) Revisit(user);
160 }
161 }
162 }
163
164
Replace(Node * node,Node * replacement)165 void GraphReducer::Replace(Node* node, Node* replacement) {
166 Replace(node, replacement, std::numeric_limits<NodeId>::max());
167 }
168
169
Replace(Node * node,Node * replacement,NodeId max_id)170 void GraphReducer::Replace(Node* node, Node* replacement, NodeId max_id) {
171 if (FLAG_trace_turbo_reduction) {
172 OFStream os(stdout);
173 os << "- Replacing " << *node << " with " << *replacement << std::endl;
174 }
175 if (node == graph()->start()) graph()->SetStart(replacement);
176 if (node == graph()->end()) graph()->SetEnd(replacement);
177 if (replacement->id() <= max_id) {
178 // {replacement} is an old node, so unlink {node} and assume that
179 // {replacement} was already reduced and finish.
180 for (Edge edge : node->use_edges()) {
181 Node* const user = edge.from();
182 Verifier::VerifyEdgeInputReplacement(edge, replacement);
183 edge.UpdateTo(replacement);
184 // Don't revisit this node if it refers to itself.
185 if (user != node) Revisit(user);
186 }
187 node->Kill();
188 } else {
189 // Replace all old uses of {node} with {replacement}, but allow new nodes
190 // created by this reduction to use {node}.
191 for (Edge edge : node->use_edges()) {
192 Node* const user = edge.from();
193 if (user->id() <= max_id) {
194 edge.UpdateTo(replacement);
195 // Don't revisit this node if it refers to itself.
196 if (user != node) Revisit(user);
197 }
198 }
199 // Unlink {node} if it's no longer used.
200 if (node->uses().empty()) node->Kill();
201
202 // If there was a replacement, reduce it after popping {node}.
203 Recurse(replacement);
204 }
205 }
206
207
ReplaceWithValue(Node * node,Node * value,Node * effect,Node * control)208 void GraphReducer::ReplaceWithValue(Node* node, Node* value, Node* effect,
209 Node* control) {
210 if (effect == nullptr && node->op()->EffectInputCount() > 0) {
211 effect = NodeProperties::GetEffectInput(node);
212 }
213 if (control == nullptr && node->op()->ControlInputCount() > 0) {
214 control = NodeProperties::GetControlInput(node);
215 }
216
217 // Requires distinguishing between value, effect and control edges.
218 for (Edge edge : node->use_edges()) {
219 Node* const user = edge.from();
220 DCHECK(!user->IsDead());
221 if (NodeProperties::IsControlEdge(edge)) {
222 if (user->opcode() == IrOpcode::kIfSuccess) {
223 Replace(user, control);
224 } else if (user->opcode() == IrOpcode::kIfException) {
225 DCHECK_NOT_NULL(dead_);
226 edge.UpdateTo(dead_);
227 Revisit(user);
228 } else {
229 DCHECK_NOT_NULL(control);
230 edge.UpdateTo(control);
231 Revisit(user);
232 // TODO(jarin) Check that the node cannot throw (otherwise, it
233 // would have to be connected via IfSuccess/IfException).
234 }
235 } else if (NodeProperties::IsEffectEdge(edge)) {
236 DCHECK_NOT_NULL(effect);
237 edge.UpdateTo(effect);
238 Revisit(user);
239 } else {
240 DCHECK_NOT_NULL(value);
241 edge.UpdateTo(value);
242 Revisit(user);
243 }
244 }
245 }
246
247
Pop()248 void GraphReducer::Pop() {
249 Node* node = stack_.top().node;
250 state_.Set(node, State::kVisited);
251 stack_.pop();
252 }
253
254
Push(Node * const node)255 void GraphReducer::Push(Node* const node) {
256 DCHECK(state_.Get(node) != State::kOnStack);
257 state_.Set(node, State::kOnStack);
258 stack_.push({node, 0});
259 }
260
261
Recurse(Node * node)262 bool GraphReducer::Recurse(Node* node) {
263 if (state_.Get(node) > State::kRevisit) return false;
264 Push(node);
265 return true;
266 }
267
268
Revisit(Node * node)269 void GraphReducer::Revisit(Node* node) {
270 if (state_.Get(node) == State::kVisited) {
271 state_.Set(node, State::kRevisit);
272 revisit_.push(node);
273 }
274 }
275
276 } // namespace compiler
277 } // namespace internal
278 } // namespace v8
279