namespace internal {
namespace compiler {
-GraphReducer::GraphReducer(Graph* graph)
- : graph_(graph), reducers_(graph->zone()) {}
+enum class GraphReducer::State : uint8_t {
+ kUnvisited,
+ kRevisit,
+ kOnStack,
+ kVisited
+};
+
+
+GraphReducer::GraphReducer(Graph* graph, Zone* zone)
+ : graph_(graph),
+ reducers_(zone),
+ revisit_(zone),
+ stack_(zone),
+ state_(zone) {}
-static bool NodeIdIsLessThan(const Node* node, NodeId id) {
- return node->id() < id;
+void GraphReducer::AddReducer(Reducer* reducer) {
+ reducers_.push_back(reducer);
}
void GraphReducer::ReduceNode(Node* node) {
- static const unsigned kMaxAttempts = 16;
- bool reduce = true;
- for (unsigned attempts = 0; attempts <= kMaxAttempts; ++attempts) {
- if (!reduce) return;
- reduce = false; // Assume we don't need to rerun any reducers.
- int before = graph_->NodeCount();
- for (ZoneVector<Reducer*>::iterator i = reducers_.begin();
- i != reducers_.end(); ++i) {
+ DCHECK(stack_.empty());
+ DCHECK(revisit_.empty());
+ std::fill(state_.begin(), state_.end(), State::kUnvisited);
+ Push(node);
+ for (;;) {
+ DCHECK(!stack_.empty() ||
+ std::find(state_.begin(), state_.end(), State::kOnStack) ==
+ state_.end());
+ if (!stack_.empty()) {
+ // Process the node on the top of the stack, potentially pushing more or
+ // popping the node off the stack.
+ ReduceTop();
+ } else if (!revisit_.empty()) {
+ // If the stack becomes empty, revisit any nodes in the revisit queue.
+ Node* const node = revisit_.top();
+ revisit_.pop();
+ if (state_[node->id()] == State::kRevisit) {
+ // state can change while in queue.
+ Push(node);
+ }
+ } else {
+ break;
+ }
+ }
+ DCHECK(std::find(state_.begin(), state_.end(), State::kOnStack) ==
+ state_.end());
+ DCHECK(revisit_.empty());
+ DCHECK(stack_.empty());
+}
+
+
+void GraphReducer::ReduceGraph() { ReduceNode(graph()->end()); }
+
+
+Reduction GraphReducer::Reduce(Node* const node) {
+ auto skip = reducers_.end();
+ for (auto i = reducers_.begin(); i != reducers_.end();) {
+ if (i != skip) {
Reduction reduction = (*i)->Reduce(node);
- Node* replacement = reduction.replacement();
- if (replacement == NULL) {
+ if (!reduction.Changed()) {
// No change from this reducer.
- } else if (replacement == node) {
- // {replacement == node} represents an in-place reduction.
- // Rerun all the reducers for this node, as now there may be more
+ } else if (reduction.replacement() == node) {
+ // {replacement} == {node} represents an in-place reduction. Rerun
+ // all the other reducers for this node, as now there may be more
// opportunities for reduction.
- reduce = true;
- break;
+ skip = i;
+ i = reducers_.begin();
+ continue;
} else {
- if (node == graph_->start()) graph_->SetStart(replacement);
- if (node == graph_->end()) graph_->SetEnd(replacement);
- // If {node} was replaced by an old node, unlink {node} and assume that
- // {replacement} was already reduced and finish.
- if (replacement->id() < before) {
- node->ReplaceUses(replacement);
- node->Kill();
- return;
- }
- // Otherwise, {node} was replaced by a new node. Replace all old uses of
+ // {node} was replaced by another node.
+ return reduction;
+ }
+ }
+ ++i;
+ }
+ if (skip == reducers_.end()) {
+ // No change from any reducer.
+ return Reducer::NoChange();
+ }
+ // At least one reducer did some in-place reduction.
+ return Reducer::Changed(node);
+}
+
+
+void GraphReducer::ReduceTop() {
+ Node* const node = Top();
+ if (node->IsDead()) return Pop(); // Node was killed while on stack.
+
+ // Recurse on an input if necessary.
+ for (auto const input : node->inputs()) {
+ if (input != node && Recurse(input)) return;
+ }
+
+ // Remember the node count before reduction.
+ const int node_count = graph()->NodeCount();
+
+ // All inputs should be visited or on stack. Apply reductions to node.
+ Reduction reduction = Reduce(node);
+
+ // After reducing the node, pop it off the stack.
+ Pop();
+
+ // If there was a reduction, revisit the uses and reduce the replacement.
+ if (reduction.Changed()) {
+ for (Node* const use : node->uses()) {
+ // Don't revisit this node if it refers to itself.
+ if (use != node) Revisit(use);
+ }
+ Node* const replacement = reduction.replacement();
+ if (replacement != node) {
+ if (node == graph()->start()) graph()->SetStart(replacement);
+ if (node == graph()->end()) graph()->SetEnd(replacement);
+ // If {node} was replaced by an old node, unlink {node} and assume that
+ // {replacement} was already reduced and finish.
+ if (replacement->id() < node_count) {
+ node->ReplaceUses(replacement);
+ node->Kill();
+ } else {
+ // Otherwise {node} was replaced by a new node. Replace all old uses of
// {node} with {replacement}. New nodes created by this reduction can
// use {node}.
- node->ReplaceUsesIf(
- std::bind2nd(std::ptr_fun(&NodeIdIsLessThan), before), replacement);
+ node->ReplaceUsesIf([node_count](Node* const node) {
+ return node->id() < node_count;
+ },
+ replacement);
// Unlink {node} if it's no longer used.
if (node->uses().empty()) {
node->Kill();
}
- // Rerun all the reductions on the {replacement}.
- node = replacement;
- reduce = true;
- break;
+
+ // If there was a replacement, reduce it after popping {node}.
+ Recurse(replacement);
}
}
}
}
-// A helper class to reuse the node traversal algorithm.
-struct GraphReducerVisitor FINAL : public NullNodeVisitor {
- explicit GraphReducerVisitor(GraphReducer* reducer) : reducer_(reducer) {}
- void Post(Node* node) { reducer_->ReduceNode(node); }
- GraphReducer* reducer_;
-};
+void GraphReducer::Pop() {
+ Node* const node = Top();
+ state_[node->id()] = State::kVisited;
+ stack_.pop();
+}
-void GraphReducer::ReduceGraph() {
- GraphReducerVisitor visitor(this);
- // Perform a post-order reduction of all nodes starting from the end.
- graph()->VisitNodeInputsFromEnd(&visitor);
+void GraphReducer::Push(Node* const node) {
+ size_t const id = static_cast<size_t>(node->id());
+ if (id >= state_.size()) state_.resize(id + 1);
+ DCHECK(id < state_.size());
+ DCHECK(state_[id] != State::kOnStack);
+ state_[id] = State::kOnStack;
+ stack_.push(node);
}
-// TODO(titzer): partial graph reductions.
+Node* GraphReducer::Top() const {
+ DCHECK(!stack_.empty());
+ Node* const node = stack_.top();
+ size_t const id = static_cast<size_t>(node->id());
+ DCHECK(id < state_.size());
+ DCHECK(state_[id] == State::kOnStack);
+ USE(id);
+ return node;
+}
+
+
+bool GraphReducer::Recurse(Node* const node) {
+ size_t const id = static_cast<size_t>(node->id());
+ if (id < state_.size() && state_[id] > State::kRevisit) return false;
+ Push(node);
+ return true;
+}
+
+
+void GraphReducer::Revisit(Node* const node) {
+ size_t const id = static_cast<size_t>(node->id());
+ if (id < state_.size() && state_[id] == State::kVisited) {
+ state_[id] = State::kRevisit;
+ revisit_.push(node);
+ }
+}
} // namespace compiler
} // namespace internal
// Performs an iterative reduction of a node graph.
class GraphReducer FINAL {
public:
- explicit GraphReducer(Graph* graph);
+ GraphReducer(Graph* graph, Zone* zone);
Graph* graph() const { return graph_; }
- void AddReducer(Reducer* reducer) { reducers_.push_back(reducer); }
+ void AddReducer(Reducer* reducer);
// Reduce a single node.
- void ReduceNode(Node* node);
+ void ReduceNode(Node* const);
// Reduce the whole graph.
void ReduceGraph();
private:
+ enum class State : uint8_t;
+
+ // Reduce a single node.
+ Reduction Reduce(Node* const);
+ // Reduce the node on top of the stack.
+ void ReduceTop();
+
+ // Node stack operations.
+ void Pop();
+ void Push(Node* const);
+ Node* Top() const;
+
+ // Revisit queue operations.
+ bool Recurse(Node* const);
+ void Revisit(Node* const);
+
Graph* graph_;
ZoneVector<Reducer*> reducers_;
+ ZoneStack<Node*> revisit_;
+ ZoneStack<Node*> stack_;
+ ZoneDeque<State> state_;
DISALLOW_COPY_AND_ASSIGN(GraphReducer);
};
ValueNumberingReducer vn_reducer(data->graph_zone());
JSTypedLowering lowering(data->jsgraph());
SimplifiedOperatorReducer simple_reducer(data->jsgraph());
- GraphReducer graph_reducer(data->graph());
+ GraphReducer graph_reducer(data->graph(), temp_zone);
graph_reducer.AddReducer(&vn_reducer);
graph_reducer.AddReducer(&lowering);
graph_reducer.AddReducer(&simple_reducer);
lowering.LowerAllNodes();
ValueNumberingReducer vn_reducer(data->graph_zone());
SimplifiedOperatorReducer simple_reducer(data->jsgraph());
- GraphReducer graph_reducer(data->graph());
+ GraphReducer graph_reducer(data->graph(), temp_zone);
graph_reducer.AddReducer(&vn_reducer);
graph_reducer.AddReducer(&simple_reducer);
graph_reducer.ReduceGraph();
SimplifiedOperatorReducer simple_reducer(data->jsgraph());
ChangeLowering lowering(data->jsgraph(), &linkage);
MachineOperatorReducer mach_reducer(data->jsgraph());
- GraphReducer graph_reducer(data->graph());
+ GraphReducer graph_reducer(data->graph(), temp_zone);
// TODO(titzer): Figure out if we should run all reducers at once here.
graph_reducer.AddReducer(&vn_reducer);
graph_reducer.AddReducer(&simple_reducer);
SourcePosition::Unknown());
JSGenericLowering generic(data->info(), data->jsgraph());
SelectLowering select(data->jsgraph()->graph(), data->jsgraph()->common());
- GraphReducer graph_reducer(data->graph());
+ GraphReducer graph_reducer(data->graph(), temp_zone);
graph_reducer.AddReducer(&generic);
graph_reducer.AddReducer(&select);
graph_reducer.ReduceGraph();
#include <deque>
#include <queue>
+#include <stack>
#include <vector>
#include "src/zone-allocator.h"
}
};
+
// A wrapper subclass std::deque to make it easy to construct one
// that uses a zone allocator.
template <typename T>
class ZoneDeque : public std::deque<T, zone_allocator<T> > {
public:
+ // Constructs an empty deque.
explicit ZoneDeque(Zone* zone)
: std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone)) {}
};
+
// A wrapper subclass for std::queue to make it easy to construct one
// that uses a zone allocator.
template <typename T>
-class ZoneQueue : public std::queue<T, std::deque<T, zone_allocator<T> > > {
+class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
public:
// Constructs an empty queue.
explicit ZoneQueue(Zone* zone)
- : std::queue<T, std::deque<T, zone_allocator<T> > >(
- std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone))) {}
+ : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
};
+
+// A wrapper subclass for std::stack to make it easy to construct one that uses
+// a zone allocator.
+template <typename T>
+class ZoneStack : public std::stack<T, ZoneDeque<T>> {
+ public:
+ // Constructs an empty stack.
+ explicit ZoneStack(Zone* zone)
+ : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
+};
+
+
// Typedefs to shorten commonly used vectors.
typedef ZoneVector<bool> BoolVector;
typedef ZoneVector<int> IntVector;
-} } // namespace v8::internal
+
+} // namespace internal
+} // namespace v8
#endif // V8_ZONE_CONTAINERS_H_
Linkage linkage(this->zone(), &info);
ChangeLowering change_lowering(&jsgraph, &linkage);
SelectLowering select_lowering(this->graph(), this->common());
- GraphReducer reducer(this->graph());
+ GraphReducer reducer(this->graph(), this->zone());
reducer.AddReducer(&change_lowering);
reducer.AddReducer(&select_lowering);
reducer.ReduceNode(change);
Node* end = graph.NewNode(&OPA1, n1);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
ReducerRecorder recorder(graph.zone());
reducer.AddReducer(&recorder);
reducer.ReduceGraph();
Node* end = graph.NewNode(&OPA2, n2, n3);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
ReducerRecorder recorder(graph.zone());
reducer.AddReducer(&recorder);
reducer.ReduceGraph();
Node* end = graph.NewNode(&OPA1, n1);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
InPlaceABReducer r;
reducer.AddReducer(&r);
Node* end = graph.NewNode(&OPA2, n2, n3);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
InPlaceABReducer r;
reducer.AddReducer(&r);
Node* end = graph.NewNode(&OPA2, n2, n3);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
NewABReducer r(&graph);
reducer.AddReducer(&r);
graph.SetEnd(end);
CHECK_EQ(1, graph.NodeCount());
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
A0Wrapper r(&graph);
reducer.AddReducer(&r);
graph.SetEnd(end);
CHECK_EQ(1, graph.NodeCount());
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
B0Wrapper r(&graph);
reducer.AddReducer(&r);
Node* end = graph.NewNode(&OPA1, n1);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
A1Forwarder r;
reducer.AddReducer(&r);
Node* end = graph.NewNode(&OPA2, n2, n3);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
A1Forwarder r;
reducer.AddReducer(&r);
}
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
A1Forwarder r;
reducer.AddReducer(&r);
Node* end = graph.NewNode(&OPA2, n2, n3);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
InPlaceABReducer r;
B1Forwarder f;
reducer.AddReducer(&r);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
reducer.AddReducer(&r);
int before = graph.NodeCount();
GenDAG(&graph, p3, p2, p1);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
AB2Sorter r1;
A1Forwarder r2;
InPlaceABReducer r3;
Node* end = graph.NewNode(&OPA1, n1);
graph.SetEnd(end);
- GraphReducer reducer(&graph);
+ GraphReducer reducer(&graph, graph.zone());
InPlaceABReducer abr;
InPlaceBCReducer bcr;
if (i == 0) {
Linkage linkage(
zone, Linkage::GetSimplifiedCDescriptor(zone, this->machine_sig_));
ChangeLowering lowering(&jsgraph, &linkage);
- GraphReducer reducer(this->graph());
+ GraphReducer reducer(this->graph(), this->zone());
reducer.AddReducer(&lowering);
reducer.ReduceGraph();
Verifier::Run(this->graph());
protected:
void ReduceNode(Node* node, Reducer* r) {
- GraphReducer reducer(graph());
+ GraphReducer reducer(graph(), zone());
reducer.AddReducer(r);
reducer.ReduceNode(node);
}
void ReduceNode(Node* node, Reducer* r1, Reducer* r2) {
- GraphReducer reducer(graph());
+ GraphReducer reducer(graph(), zone());
reducer.AddReducer(r1);
reducer.AddReducer(r2);
reducer.ReduceNode(node);
}
void ReduceNode(Node* node, Reducer* r1, Reducer* r2, Reducer* r3) {
- GraphReducer reducer(graph());
+ GraphReducer reducer(graph(), zone());
reducer.AddReducer(r1);
reducer.AddReducer(r2);
reducer.AddReducer(r3);
Node* node0 = graph()->NewNode(&OP0);
Node* node1 = graph()->NewNode(&OP1, node0);
Node* node2 = graph()->NewNode(&OP1, node0);
+ EXPECT_CALL(r, Reduce(node0)).WillOnce(Return(Reducer::NoChange()));
EXPECT_CALL(r, Reduce(node1)).WillOnce(Return(Reducer::Replace(node2)));
ReduceNode(node1, &r);
EXPECT_FALSE(node0->IsDead());
Return(Reducer::Changed(node0)));
EXPECT_CALL(r1, Reduce(node0)).InSequence(s1);
EXPECT_CALL(r2, Reduce(node0)).InSequence(s2);
- EXPECT_CALL(r3, Reduce(node0)).InSequence(s3);
ReduceNode(node0, &r1, &r2, &r3);
}