Commit 0672b64d authored by mstarzinger's avatar mstarzinger Committed by Commit bot

Make generic algorithm a little less generic.

R=titzer@chromium.org

Review URL: https://codereview.chromium.org/760493003

Cr-Commit-Position: refs/heads/master@{#25620}
parent 4f3d27e6
......@@ -497,7 +497,6 @@ source_set("v8_base") {
"src/compiler/frame.h",
"src/compiler/gap-resolver.cc",
"src/compiler/gap-resolver.h",
"src/compiler/generic-algorithm-inl.h",
"src/compiler/generic-algorithm.h",
"src/compiler/graph-builder.cc",
"src/compiler/graph-builder.h",
......
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_COMPILER_GENERIC_ALGORITHM_INL_H_
#define V8_COMPILER_GENERIC_ALGORITHM_INL_H_
#include <vector>
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/graph.h"
#include "src/compiler/node.h"
namespace v8 {
namespace internal {
namespace compiler {
template <class N>
class NodeInputIterationTraits {
public:
typedef N Node;
typedef typename N::InputEdges::iterator Iterator;
static Iterator begin(Node* node) { return node->input_edges().begin(); }
static Iterator end(Node* node) { return node->input_edges().end(); }
static int max_id(Graph* graph) { return graph->NodeCount(); }
static Node* to(Iterator iterator) { return (*iterator).to(); }
static Node* from(Iterator iterator) { return (*iterator).from(); }
};
} // namespace compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_GENERIC_ALGORITHM_INL_H_
......@@ -6,7 +6,10 @@
#define V8_COMPILER_GENERIC_ALGORITHM_H_
#include <stack>
#include <vector>
#include "src/compiler/graph.h"
#include "src/compiler/node.h"
#include "src/zone-containers.h"
namespace v8 {
......@@ -18,40 +21,37 @@ class Node;
// GenericGraphVisit allows visitation of graphs of nodes and edges in pre- and
// post-order. Visitation uses an explicitly allocated stack rather than the
// execution stack to avoid stack overflow. Although GenericGraphVisit is
// primarily intended to traverse networks of nodes through their
// dependencies and uses, it also can be used to visit any graph-like network
// by specifying custom traits.
// execution stack to avoid stack overflow.
class GenericGraphVisit {
public:
// struct Visitor {
// void Pre(Traits::Node* current);
// void Post(Traits::Node* current);
// void PreEdge(Traits::Node* from, int index, Traits::Node* to);
// void PostEdge(Traits::Node* from, int index, Traits::Node* to);
// void Pre(Node* current);
// void Post(Node* current);
// void PreEdge(Node* from, int index, Node* to);
// void PostEdge(Node* from, int index, Node* to);
// }
template <class Visitor, class Traits, class RootIterator>
static void Visit(Graph* graph, Zone* zone, RootIterator root_begin,
RootIterator root_end, Visitor* visitor) {
typedef typename Traits::Node Node;
typedef typename Traits::Iterator Iterator;
template <class Visitor>
static void Visit(Graph* graph, Zone* zone, Node** root_begin,
Node** root_end, Visitor* visitor) {
typedef typename Node::InputEdges::iterator Iterator;
typedef std::pair<Iterator, Iterator> NodeState;
typedef std::stack<NodeState, ZoneDeque<NodeState> > NodeStateStack;
NodeStateStack stack((ZoneDeque<NodeState>(zone)));
BoolVector visited(Traits::max_id(graph), false, zone);
BoolVector visited(graph->NodeCount(), false, zone);
Node* current = *root_begin;
while (true) {
DCHECK(current != NULL);
const int id = current->id();
DCHECK(id >= 0);
DCHECK(id < Traits::max_id(graph)); // Must be a valid id.
DCHECK(id < graph->NodeCount()); // Must be a valid id.
bool visit = !GetVisited(&visited, id);
if (visit) {
visitor->Pre(current);
SetVisited(&visited, id);
}
Iterator begin(visit ? Traits::begin(current) : Traits::end(current));
Iterator end(Traits::end(current));
Iterator begin(visit ? current->input_edges().begin()
: current->input_edges().end());
Iterator end(current->input_edges().end());
stack.push(NodeState(begin, end));
Node* post_order_node = current;
while (true) {
......@@ -67,27 +67,26 @@ class GenericGraphVisit {
current = *root_begin;
break;
}
post_order_node = Traits::from(stack.top().first);
post_order_node = (*stack.top().first).from();
visit = true;
} else {
visitor->PreEdge(Traits::from(top.first), (*top.first).index(),
Traits::to(top.first));
current = Traits::to(top.first);
visitor->PreEdge((*top.first).from(), (*top.first).index(),
(*top.first).to());
current = (*top.first).to();
if (!GetVisited(&visited, current->id())) break;
}
top = stack.top();
visitor->PostEdge(Traits::from(top.first), (*top.first).index(),
Traits::to(top.first));
visitor->PostEdge((*top.first).from(), (*top.first).index(),
(*top.first).to());
++stack.top().first;
}
}
}
template <class Visitor, class Traits>
static void Visit(Graph* graph, Zone* zone, typename Traits::Node* current,
Visitor* visitor) {
typename Traits::Node* array[] = {current};
Visit<Visitor, Traits>(graph, zone, &array[0], &array[1], visitor);
template <class Visitor>
static void Visit(Graph* graph, Zone* zone, Node* current, Visitor* visitor) {
Node* array[] = {current};
Visit<Visitor>(graph, zone, &array[0], &array[1], visitor);
}
struct NullNodeVisitor {
......@@ -111,8 +110,11 @@ class GenericGraphVisit {
return visited->at(id);
}
};
}
}
} // namespace v8::internal::compiler
typedef GenericGraphVisit::NullNodeVisitor NullNodeVisitor;
} // namespace compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_GENERIC_ALGORITHM_H_
......@@ -5,7 +5,7 @@
#ifndef V8_COMPILER_GRAPH_INL_H_
#define V8_COMPILER_GRAPH_INL_H_
#include "src/compiler/generic-algorithm-inl.h"
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/graph.h"
namespace v8 {
......@@ -15,8 +15,7 @@ namespace compiler {
template <class Visitor>
void Graph::VisitNodeInputsFromEnd(Visitor* visitor) {
Zone tmp_zone(zone()->isolate());
GenericGraphVisit::Visit<Visitor, NodeInputIterationTraits<Node> >(
this, &tmp_zone, end(), visitor);
GenericGraphVisit::Visit<Visitor>(this, &tmp_zone, end(), visitor);
}
} // namespace compiler
......
......@@ -5,6 +5,7 @@
#ifndef V8_COMPILER_GRAPH_REPLAY_H_
#define V8_COMPILER_GRAPH_REPLAY_H_
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/node.h"
namespace v8 {
......
......@@ -8,7 +8,6 @@
#include <map>
#include <set>
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/node.h"
#include "src/compiler/node-aux-data.h"
#include "src/compiler/source-position.h"
......
......@@ -11,7 +11,6 @@
#include "src/v8.h"
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/opcodes.h"
#include "src/compiler/operator.h"
#include "src/types.h"
......@@ -426,8 +425,6 @@ class Node::Uses::iterator {
std::ostream& operator<<(std::ostream& os, const Node& n);
typedef GenericGraphVisit::NullNodeVisitor NullNodeVisitor;
typedef std::set<Node*, std::less<Node*>, zone_allocator<Node*> > NodeSet;
typedef NodeSet::iterator NodeSetIter;
typedef NodeSet::reverse_iterator NodeSetRIter;
......
......@@ -429,7 +429,6 @@
'../../src/compiler/frame.h',
'../../src/compiler/gap-resolver.cc',
'../../src/compiler/gap-resolver.h',
'../../src/compiler/generic-algorithm-inl.h',
'../../src/compiler/generic-algorithm.h',
'../../src/compiler/graph-builder.cc',
'../../src/compiler/graph-builder.h',
......
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