OLD | NEW |
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 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 if (skip == reducers_.end()) { | 92 if (skip == reducers_.end()) { |
93 // No change from any reducer. | 93 // No change from any reducer. |
94 return Reducer::NoChange(); | 94 return Reducer::NoChange(); |
95 } | 95 } |
96 // At least one reducer did some in-place reduction. | 96 // At least one reducer did some in-place reduction. |
97 return Reducer::Changed(node); | 97 return Reducer::Changed(node); |
98 } | 98 } |
99 | 99 |
100 | 100 |
101 void GraphReducer::ReduceTop() { | 101 void GraphReducer::ReduceTop() { |
102 Node* const node = Top(); | 102 NodeState& entry = stack_.top(); |
| 103 Node* node = entry.node; |
| 104 DCHECK(state_[node->id()] == State::kOnStack); |
| 105 |
103 if (node->IsDead()) return Pop(); // Node was killed while on stack. | 106 if (node->IsDead()) return Pop(); // Node was killed while on stack. |
104 | 107 |
105 // Recurse on an input if necessary. | 108 // Recurse on an input if necessary. |
106 for (auto const input : node->inputs()) { | 109 int start = entry.input_index < node->InputCount() ? entry.input_index : 0; |
| 110 for (int i = start; i < node->InputCount(); i++) { |
| 111 Node* input = node->InputAt(i); |
| 112 entry.input_index = i + 1; |
| 113 if (input != node && Recurse(input)) return; |
| 114 } |
| 115 for (int i = 0; i < start; i++) { |
| 116 Node* input = node->InputAt(i); |
| 117 entry.input_index = i + 1; |
107 if (input != node && Recurse(input)) return; | 118 if (input != node && Recurse(input)) return; |
108 } | 119 } |
109 | 120 |
110 // Remember the node count before reduction. | 121 // Remember the node count before reduction. |
111 const int node_count = graph()->NodeCount(); | 122 const int node_count = graph()->NodeCount(); |
112 | 123 |
113 // All inputs should be visited or on stack. Apply reductions to node. | 124 // All inputs should be visited or on stack. Apply reductions to node. |
114 Reduction reduction = Reduce(node); | 125 Reduction reduction = Reduce(node); |
115 | 126 |
116 // After reducing the node, pop it off the stack. | 127 // After reducing the node, pop it off the stack. |
(...skipping 29 matching lines...) Expand all Loading... |
146 | 157 |
147 // If there was a replacement, reduce it after popping {node}. | 158 // If there was a replacement, reduce it after popping {node}. |
148 Recurse(replacement); | 159 Recurse(replacement); |
149 } | 160 } |
150 } | 161 } |
151 } | 162 } |
152 } | 163 } |
153 | 164 |
154 | 165 |
155 void GraphReducer::Pop() { | 166 void GraphReducer::Pop() { |
156 Node* const node = Top(); | 167 Node* const node = stack_.top().node; |
157 state_[node->id()] = State::kVisited; | 168 state_[node->id()] = State::kVisited; |
158 stack_.pop(); | 169 stack_.pop(); |
159 } | 170 } |
160 | 171 |
161 | 172 |
162 void GraphReducer::Push(Node* const node) { | 173 void GraphReducer::Push(Node* const node) { |
163 size_t const id = static_cast<size_t>(node->id()); | 174 size_t const id = static_cast<size_t>(node->id()); |
164 if (id >= state_.size()) state_.resize(id + 1); | 175 if (id >= state_.size()) state_.resize(id + 1); |
165 DCHECK(id < state_.size()); | 176 DCHECK(id < state_.size()); |
166 DCHECK(state_[id] != State::kOnStack); | 177 DCHECK(state_[id] != State::kOnStack); |
167 state_[id] = State::kOnStack; | 178 state_[id] = State::kOnStack; |
168 stack_.push(node); | 179 stack_.push({node, 0}); |
169 } | 180 } |
170 | 181 |
171 | 182 |
172 Node* GraphReducer::Top() const { | |
173 DCHECK(!stack_.empty()); | |
174 Node* const node = stack_.top(); | |
175 size_t const id = static_cast<size_t>(node->id()); | |
176 DCHECK(id < state_.size()); | |
177 DCHECK(state_[id] == State::kOnStack); | |
178 USE(id); | |
179 return node; | |
180 } | |
181 | |
182 | |
183 bool GraphReducer::Recurse(Node* const node) { | 183 bool GraphReducer::Recurse(Node* const node) { |
184 size_t const id = static_cast<size_t>(node->id()); | 184 size_t const id = static_cast<size_t>(node->id()); |
185 if (id < state_.size() && state_[id] > State::kRevisit) return false; | 185 if (id < state_.size() && state_[id] > State::kRevisit) return false; |
186 Push(node); | 186 Push(node); |
187 return true; | 187 return true; |
188 } | 188 } |
189 | 189 |
190 | 190 |
191 void GraphReducer::Revisit(Node* const node) { | 191 void GraphReducer::Revisit(Node* const node) { |
192 size_t const id = static_cast<size_t>(node->id()); | 192 size_t const id = static_cast<size_t>(node->id()); |
193 if (id < state_.size() && state_[id] == State::kVisited) { | 193 if (id < state_.size() && state_[id] == State::kVisited) { |
194 state_[id] = State::kRevisit; | 194 state_[id] = State::kRevisit; |
195 revisit_.push(node); | 195 revisit_.push(node); |
196 } | 196 } |
197 } | 197 } |
198 | 198 |
199 } // namespace compiler | 199 } // namespace compiler |
200 } // namespace internal | 200 } // namespace internal |
201 } // namespace v8 | 201 } // namespace v8 |
OLD | NEW |