From 76178f1ddb98fc6cd9df0865d93c4995903210cb Mon Sep 17 00:00:00 2001 From: titzer Date: Thu, 27 Nov 2014 08:24:08 -0800 Subject: [PATCH] [turbofan] Avoid repeatedly revisiting inputs in GraphReducer. R=mstarzinger@chromium.org BUG= Review URL: https://codereview.chromium.org/753073009 Cr-Commit-Position: refs/heads/master@{#25551} --- src/compiler/graph-reducer.cc | 30 +++++++++++++++--------------- src/compiler/graph-reducer.h | 7 +++++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/compiler/graph-reducer.cc b/src/compiler/graph-reducer.cc index ff04f6ba5f..26163bf416 100644 --- a/src/compiler/graph-reducer.cc +++ b/src/compiler/graph-reducer.cc @@ -99,11 +99,22 @@ Reduction GraphReducer::Reduce(Node* const node) { void GraphReducer::ReduceTop() { - Node* const node = Top(); + NodeState& entry = stack_.top(); + Node* node = entry.node; + DCHECK(state_[node->id()] == State::kOnStack); + if (node->IsDead()) return Pop(); // Node was killed while on stack. // Recurse on an input if necessary. - for (auto const input : node->inputs()) { + 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; + } + for (int i = 0; i < start; i++) { + Node* input = node->InputAt(i); + entry.input_index = i + 1; if (input != node && Recurse(input)) return; } @@ -153,7 +164,7 @@ void GraphReducer::ReduceTop() { void GraphReducer::Pop() { - Node* const node = Top(); + Node* const node = stack_.top().node; state_[node->id()] = State::kVisited; stack_.pop(); } @@ -165,18 +176,7 @@ void GraphReducer::Push(Node* const node) { DCHECK(id < state_.size()); DCHECK(state_[id] != State::kOnStack); state_[id] = State::kOnStack; - stack_.push(node); -} - - -Node* GraphReducer::Top() const { - DCHECK(!stack_.empty()); - Node* const node = stack_.top(); - size_t const id = static_cast(node->id()); - DCHECK(id < state_.size()); - DCHECK(state_[id] == State::kOnStack); - USE(id); - return node; + stack_.push({node, 0}); } diff --git a/src/compiler/graph-reducer.h b/src/compiler/graph-reducer.h index d54c3186c0..f07fb2bebf 100644 --- a/src/compiler/graph-reducer.h +++ b/src/compiler/graph-reducer.h @@ -68,6 +68,10 @@ class GraphReducer FINAL { private: enum class State : uint8_t; + struct NodeState { + Node* node; + int input_index; + }; // Reduce a single node. Reduction Reduce(Node* const); @@ -77,7 +81,6 @@ class GraphReducer FINAL { // Node stack operations. void Pop(); void Push(Node* const); - Node* Top() const; // Revisit queue operations. bool Recurse(Node* const); @@ -86,7 +89,7 @@ class GraphReducer FINAL { Graph* graph_; ZoneVector reducers_; ZoneStack revisit_; - ZoneStack stack_; + ZoneStack stack_; ZoneDeque state_; DISALLOW_COPY_AND_ASSIGN(GraphReducer);