| 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 <functional> | 5 #include <functional> |
| 6 #include <limits> | 6 #include <limits> |
| 7 | 7 |
| 8 #include "src/compiler/graph.h" | 8 #include "src/compiler/graph.h" |
| 9 #include "src/compiler/graph-reducer.h" | 9 #include "src/compiler/graph-reducer.h" |
| 10 #include "src/compiler/node.h" | 10 #include "src/compiler/node.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 | 108 |
| 109 void GraphReducer::ReduceTop() { | 109 void GraphReducer::ReduceTop() { |
| 110 NodeState& entry = stack_.top(); | 110 NodeState& entry = stack_.top(); |
| 111 Node* node = entry.node; | 111 Node* node = entry.node; |
| 112 DCHECK(state_.Get(node) == State::kOnStack); | 112 DCHECK(state_.Get(node) == State::kOnStack); |
| 113 | 113 |
| 114 if (node->IsDead()) return Pop(); // Node was killed while on stack. | 114 if (node->IsDead()) return Pop(); // Node was killed while on stack. |
| 115 | 115 |
| 116 // Recurse on an input if necessary. | 116 // Recurse on an input if necessary. |
| 117 int start = entry.input_index < node->InputCount() ? entry.input_index : 0; | 117 int start = entry.input_index < node->InputCount() ? entry.input_index : 0; |
| 118 for (int i = start; i < node->InputCount(); i++) { | 118 |
| 119 Node* input = node->InputAt(i); | 119 Node::Inputs node_inputs = node->inputs(); |
| 120 entry.input_index = i + 1; | 120 auto node_inputs_begin = node_inputs.begin(); |
| 121 if (input != node && Recurse(input)) return; | 121 auto node_inputs_end = node_inputs.end(); |
| 122 DCHECK(node_inputs_end == node_inputs_begin + node->InputCount()); |
| 123 |
| 124 for (auto it = node_inputs_begin + start; it != node_inputs_end; ++it) { |
| 125 Node* input = *it; |
| 126 if (input != node && Recurse(input)) { |
| 127 entry.input_index = (it - node_inputs_begin) + 1; |
| 128 return; |
| 129 } |
| 122 } | 130 } |
| 123 for (int i = 0; i < start; i++) { | 131 for (auto it = node_inputs_begin; it != node_inputs_begin + start; ++it) { |
| 124 Node* input = node->InputAt(i); | 132 Node* input = *it; |
| 125 entry.input_index = i + 1; | 133 if (input != node && Recurse(input)) { |
| 126 if (input != node && Recurse(input)) return; | 134 entry.input_index = (it - node_inputs_begin) + 1; |
| 135 return; |
| 136 } |
| 127 } | 137 } |
| 128 | 138 |
| 129 // Remember the max node id before reduction. | 139 // Remember the max node id before reduction. |
| 130 NodeId const max_id = static_cast<NodeId>(graph()->NodeCount() - 1); | 140 NodeId const max_id = static_cast<NodeId>(graph()->NodeCount() - 1); |
| 131 | 141 |
| 132 // All inputs should be visited or on stack. Apply reductions to node. | 142 // All inputs should be visited or on stack. Apply reductions to node. |
| 133 Reduction reduction = Reduce(node); | 143 Reduction reduction = Reduce(node); |
| 134 | 144 |
| 135 // If there was no reduction, pop {node} and continue. | 145 // If there was no reduction, pop {node} and continue. |
| 136 if (!reduction.Changed()) return Pop(); | 146 if (!reduction.Changed()) return Pop(); |
| 137 | 147 |
| 138 // Check if the reduction is an in-place update of the {node}. | 148 // Check if the reduction is an in-place update of the {node}. |
| 139 Node* const replacement = reduction.replacement(); | 149 Node* const replacement = reduction.replacement(); |
| 140 if (replacement == node) { | 150 if (replacement == node) { |
| 141 // In-place update of {node}, may need to recurse on an input. | 151 // In-place update of {node}, may need to recurse on an input. |
| 142 for (int i = 0; i < node->InputCount(); ++i) { | 152 Node::Inputs node_inputs = node->inputs(); |
| 143 Node* input = node->InputAt(i); | 153 auto node_inputs_begin = node_inputs.begin(); |
| 144 entry.input_index = i + 1; | 154 auto node_inputs_end = node_inputs.end(); |
| 145 if (input != node && Recurse(input)) return; | 155 for (auto it = node_inputs_begin; it != node_inputs_end; ++it) { |
| 156 Node* input = *it; |
| 157 if (input != node && Recurse(input)) { |
| 158 entry.input_index = (it - node_inputs_begin) + 1; |
| 159 return; |
| 160 } |
| 146 } | 161 } |
| 147 } | 162 } |
| 148 | 163 |
| 149 // After reducing the node, pop it off the stack. | 164 // After reducing the node, pop it off the stack. |
| 150 Pop(); | 165 Pop(); |
| 151 | 166 |
| 152 // Check if we have a new replacement. | 167 // Check if we have a new replacement. |
| 153 if (replacement != node) { | 168 if (replacement != node) { |
| 154 Replace(node, replacement, max_id); | 169 Replace(node, replacement, max_id); |
| 155 } else { | 170 } else { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 void GraphReducer::Revisit(Node* node) { | 284 void GraphReducer::Revisit(Node* node) { |
| 270 if (state_.Get(node) == State::kVisited) { | 285 if (state_.Get(node) == State::kVisited) { |
| 271 state_.Set(node, State::kRevisit); | 286 state_.Set(node, State::kRevisit); |
| 272 revisit_.push(node); | 287 revisit_.push(node); |
| 273 } | 288 } |
| 274 } | 289 } |
| 275 | 290 |
| 276 } // namespace compiler | 291 } // namespace compiler |
| 277 } // namespace internal | 292 } // namespace internal |
| 278 } // namespace v8 | 293 } // namespace v8 |
| OLD | NEW |