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) {
Reduction BranchElimination::ReduceMerge(Node* node) {
// Shortcut for the case when we do not know anything about some
// input.
for (int i = 0; i < node->InputCount(); i++) {
if (node_conditions_.Get(node->InputAt(i)) == nullptr) {
for (Node* input : node->inputs()) {
if (node_conditions_.Get(input) == nullptr) {
return UpdateConditions(node, nullptr);
}
}
......
......@@ -61,8 +61,7 @@ Reduction EscapeAnalysisReducer::ReduceNode(Node* node) {
break;
}
bool depends_on_object_state = false;
for (int i = 0; i < node->InputCount(); i++) {
Node* input = node->InputAt(i);
for (Node* input : node->inputs()) {
switch (input->opcode()) {
case IrOpcode::kAllocate:
case IrOpcode::kFinishRegion:
......
......@@ -115,15 +115,25 @@ void GraphReducer::ReduceTop() {
// Recurse on an input if necessary.
int start = entry.input_index < node->InputCount() ? entry.input_index : 0;
for (int i = start; i < node->InputCount(); i++) {
Node* input = node->InputAt(i);
entry.input_index = i + 1;
if (input != node && Recurse(input)) return;
Node::Inputs node_inputs = node->inputs();
auto node_inputs_begin = node_inputs.begin();
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++) {
Node* input = node->InputAt(i);
entry.input_index = i + 1;
if (input != node && Recurse(input)) return;
for (auto it = node_inputs_begin; it != node_inputs_begin + start; ++it) {
Node* input = *it;
if (input != node && Recurse(input)) {
entry.input_index = (it - node_inputs_begin) + 1;
return;
}
}
// Remember the max node id before reduction.
......@@ -139,10 +149,15 @@ void GraphReducer::ReduceTop() {
Node* const replacement = reduction.replacement();
if (replacement == node) {
// In-place update of {node}, may need to recurse on an input.
for (int i = 0; i < node->InputCount(); ++i) {
Node* input = node->InputAt(i);
entry.input_index = i + 1;
if (input != node && Recurse(input)) return;
Node::Inputs node_inputs = node->inputs();
auto node_inputs_begin = node_inputs.begin();
auto node_inputs_end = node_inputs.end();
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 {
return *this;
}
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:
friend class Node;
......@@ -417,6 +428,9 @@ class Node::InputEdges::iterator final {
explicit iterator(Node* from, int index = 0)
: use_(from->GetUsePtr(index)), input_ptr_(from->GetInputPtr(index)) {}
explicit iterator(Use* use, Node** input_ptr)
: use_(use), input_ptr_(input_ptr) {}
Use* use_;
Node** input_ptr_;
};
......@@ -455,12 +469,24 @@ class Node::Inputs::const_iterator final {
return *this;
}
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:
friend class Node::Inputs;
const_iterator(Node* node, int index) : iter_(node, index) {}
explicit const_iterator(Node::InputEdges::iterator iter) : iter_(iter) {}
Node::InputEdges::iterator iter_;
};
......
......@@ -18,8 +18,8 @@ namespace {
size_t HashCode(Node* node) {
size_t h = base::hash_combine(node->op()->HashCode(), node->InputCount());
for (int j = 0; j < node->InputCount(); ++j) {
h = base::hash_combine(h, node->InputAt(j)->id());
for (Node* input : node->inputs()) {
h = base::hash_combine(h, input->id());
}
return h;
}
......@@ -32,10 +32,17 @@ bool Equals(Node* a, Node* b) {
DCHECK_NOT_NULL(b->op());
if (!a->op()->Equals(b->op())) return false;
if (a->InputCount() != b->InputCount()) return false;
for (int j = 0; j < a->InputCount(); ++j) {
DCHECK_NOT_NULL(a->InputAt(j));
DCHECK_NOT_NULL(b->InputAt(j));
if (a->InputAt(j)->id() != b->InputAt(j)->id()) return false;
Node::Inputs aInputs = a->inputs();
Node::Inputs bInputs = b->inputs();
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;
}
......
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