Commit 0b10c044 authored by leszeks's avatar leszeks Committed by Commit bot

[turbofan] Use Node input iterators where possible

Changes some for loops to use node->inputs() instead of iterating over
InputCount and accessing InputAt(i). This saves some checks for
"has_inline_inputs" and so some branches.

Review-Url: https://codereview.chromium.org/2585713002
Cr-Commit-Position: refs/heads/master@{#42024}
parent e3ac5a31
...@@ -143,8 +143,8 @@ Reduction BranchElimination::ReduceLoop(Node* node) { ...@@ -143,8 +143,8 @@ Reduction BranchElimination::ReduceLoop(Node* node) {
Reduction BranchElimination::ReduceMerge(Node* node) { Reduction BranchElimination::ReduceMerge(Node* node) {
// Shortcut for the case when we do not know anything about some // Shortcut for the case when we do not know anything about some
// input. // input.
for (int i = 0; i < node->InputCount(); i++) { for (Node* input : node->inputs()) {
if (node_conditions_.Get(node->InputAt(i)) == nullptr) { if (node_conditions_.Get(input) == nullptr) {
return UpdateConditions(node, nullptr); return UpdateConditions(node, nullptr);
} }
} }
......
...@@ -61,8 +61,7 @@ Reduction EscapeAnalysisReducer::ReduceNode(Node* node) { ...@@ -61,8 +61,7 @@ Reduction EscapeAnalysisReducer::ReduceNode(Node* node) {
break; break;
} }
bool depends_on_object_state = false; bool depends_on_object_state = false;
for (int i = 0; i < node->InputCount(); i++) { for (Node* input : node->inputs()) {
Node* input = node->InputAt(i);
switch (input->opcode()) { switch (input->opcode()) {
case IrOpcode::kAllocate: case IrOpcode::kAllocate:
case IrOpcode::kFinishRegion: case IrOpcode::kFinishRegion:
......
...@@ -115,15 +115,25 @@ void GraphReducer::ReduceTop() { ...@@ -115,15 +115,25 @@ void GraphReducer::ReduceTop() {
// Recurse on an input if necessary. // Recurse on an input if necessary.
int start = entry.input_index < node->InputCount() ? entry.input_index : 0; int start = entry.input_index < node->InputCount() ? entry.input_index : 0;
for (int i = start; i < node->InputCount(); i++) {
Node* input = node->InputAt(i); Node::Inputs node_inputs = node->inputs();
entry.input_index = i + 1; auto node_inputs_begin = node_inputs.begin();
if (input != node && Recurse(input)) return; auto node_inputs_end = node_inputs.end();
DCHECK(node_inputs_end == node_inputs_begin + node->InputCount());
for (auto it = node_inputs_begin + start; it != node_inputs_end; ++it) {
Node* input = *it;
if (input != node && Recurse(input)) {
entry.input_index = (it - node_inputs_begin) + 1;
return;
}
} }
for (int i = 0; i < start; i++) { for (auto it = node_inputs_begin; it != node_inputs_begin + start; ++it) {
Node* input = node->InputAt(i); Node* input = *it;
entry.input_index = i + 1; if (input != node && Recurse(input)) {
if (input != node && Recurse(input)) return; entry.input_index = (it - node_inputs_begin) + 1;
return;
}
} }
// Remember the max node id before reduction. // Remember the max node id before reduction.
...@@ -139,10 +149,15 @@ void GraphReducer::ReduceTop() { ...@@ -139,10 +149,15 @@ void GraphReducer::ReduceTop() {
Node* const replacement = reduction.replacement(); Node* const replacement = reduction.replacement();
if (replacement == node) { if (replacement == node) {
// In-place update of {node}, may need to recurse on an input. // In-place update of {node}, may need to recurse on an input.
for (int i = 0; i < node->InputCount(); ++i) { Node::Inputs node_inputs = node->inputs();
Node* input = node->InputAt(i); auto node_inputs_begin = node_inputs.begin();
entry.input_index = i + 1; auto node_inputs_end = node_inputs.end();
if (input != node && Recurse(input)) return; for (auto it = node_inputs_begin; it != node_inputs_end; ++it) {
Node* input = *it;
if (input != node && Recurse(input)) {
entry.input_index = (it - node_inputs_begin) + 1;
return;
}
} }
} }
......
...@@ -410,6 +410,17 @@ class Node::InputEdges::iterator final { ...@@ -410,6 +410,17 @@ class Node::InputEdges::iterator final {
return *this; return *this;
} }
iterator operator++(int); iterator operator++(int);
iterator& operator+=(difference_type offset) {
input_ptr_ += offset;
use_ -= offset;
return *this;
}
iterator operator+(difference_type offset) const {
return iterator(use_ - offset, input_ptr_ + offset);
}
difference_type operator-(const iterator& other) const {
return static_cast<difference_type>(input_ptr_ - other.input_ptr_);
}
private: private:
friend class Node; friend class Node;
...@@ -417,6 +428,9 @@ class Node::InputEdges::iterator final { ...@@ -417,6 +428,9 @@ class Node::InputEdges::iterator final {
explicit iterator(Node* from, int index = 0) explicit iterator(Node* from, int index = 0)
: use_(from->GetUsePtr(index)), input_ptr_(from->GetInputPtr(index)) {} : use_(from->GetUsePtr(index)), input_ptr_(from->GetInputPtr(index)) {}
explicit iterator(Use* use, Node** input_ptr)
: use_(use), input_ptr_(input_ptr) {}
Use* use_; Use* use_;
Node** input_ptr_; Node** input_ptr_;
}; };
...@@ -455,12 +469,24 @@ class Node::Inputs::const_iterator final { ...@@ -455,12 +469,24 @@ class Node::Inputs::const_iterator final {
return *this; return *this;
} }
const_iterator operator++(int); const_iterator operator++(int);
const_iterator& operator+=(difference_type offset) {
iter_ += offset;
return *this;
}
const_iterator operator+(difference_type offset) const {
return const_iterator(iter_ + offset);
}
difference_type operator-(const const_iterator& other) const {
return iter_ - other.iter_;
}
private: private:
friend class Node::Inputs; friend class Node::Inputs;
const_iterator(Node* node, int index) : iter_(node, index) {} const_iterator(Node* node, int index) : iter_(node, index) {}
explicit const_iterator(Node::InputEdges::iterator iter) : iter_(iter) {}
Node::InputEdges::iterator iter_; Node::InputEdges::iterator iter_;
}; };
......
...@@ -18,8 +18,8 @@ namespace { ...@@ -18,8 +18,8 @@ namespace {
size_t HashCode(Node* node) { size_t HashCode(Node* node) {
size_t h = base::hash_combine(node->op()->HashCode(), node->InputCount()); size_t h = base::hash_combine(node->op()->HashCode(), node->InputCount());
for (int j = 0; j < node->InputCount(); ++j) { for (Node* input : node->inputs()) {
h = base::hash_combine(h, node->InputAt(j)->id()); h = base::hash_combine(h, input->id());
} }
return h; return h;
} }
...@@ -32,10 +32,17 @@ bool Equals(Node* a, Node* b) { ...@@ -32,10 +32,17 @@ bool Equals(Node* a, Node* b) {
DCHECK_NOT_NULL(b->op()); DCHECK_NOT_NULL(b->op());
if (!a->op()->Equals(b->op())) return false; if (!a->op()->Equals(b->op())) return false;
if (a->InputCount() != b->InputCount()) return false; if (a->InputCount() != b->InputCount()) return false;
for (int j = 0; j < a->InputCount(); ++j) { Node::Inputs aInputs = a->inputs();
DCHECK_NOT_NULL(a->InputAt(j)); Node::Inputs bInputs = b->inputs();
DCHECK_NOT_NULL(b->InputAt(j));
if (a->InputAt(j)->id() != b->InputAt(j)->id()) return false; auto aIt = aInputs.begin();
auto bIt = bInputs.begin();
auto aEnd = aInputs.end();
for (; aIt != aEnd; ++aIt, ++bIt) {
DCHECK_NOT_NULL(*aIt);
DCHECK_NOT_NULL(*bIt);
if ((*aIt)->id() != (*bIt)->id()) return false;
} }
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment