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}
This commit is contained in:
mstarzinger 2014-12-02 07:37:52 -08:00 committed by Commit bot
parent 4f3d27e64f
commit 0672b64d1c
8 changed files with 36 additions and 75 deletions

View File

@ -497,7 +497,6 @@ source_set("v8_base") {
"src/compiler/frame.h", "src/compiler/frame.h",
"src/compiler/gap-resolver.cc", "src/compiler/gap-resolver.cc",
"src/compiler/gap-resolver.h", "src/compiler/gap-resolver.h",
"src/compiler/generic-algorithm-inl.h",
"src/compiler/generic-algorithm.h", "src/compiler/generic-algorithm.h",
"src/compiler/graph-builder.cc", "src/compiler/graph-builder.cc",
"src/compiler/graph-builder.h", "src/compiler/graph-builder.h",

View File

@ -1,35 +0,0 @@
// 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_

View File

@ -6,7 +6,10 @@
#define V8_COMPILER_GENERIC_ALGORITHM_H_ #define V8_COMPILER_GENERIC_ALGORITHM_H_
#include <stack> #include <stack>
#include <vector>
#include "src/compiler/graph.h"
#include "src/compiler/node.h"
#include "src/zone-containers.h" #include "src/zone-containers.h"
namespace v8 { namespace v8 {
@ -18,40 +21,37 @@ class Node;
// GenericGraphVisit allows visitation of graphs of nodes and edges in pre- and // GenericGraphVisit allows visitation of graphs of nodes and edges in pre- and
// post-order. Visitation uses an explicitly allocated stack rather than the // post-order. Visitation uses an explicitly allocated stack rather than the
// execution stack to avoid stack overflow. Although GenericGraphVisit is // execution stack to avoid stack overflow.
// 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.
class GenericGraphVisit { class GenericGraphVisit {
public: public:
// struct Visitor { // struct Visitor {
// void Pre(Traits::Node* current); // void Pre(Node* current);
// void Post(Traits::Node* current); // void Post(Node* current);
// void PreEdge(Traits::Node* from, int index, Traits::Node* to); // void PreEdge(Node* from, int index, Node* to);
// void PostEdge(Traits::Node* from, int index, Traits::Node* to); // void PostEdge(Node* from, int index, Node* to);
// } // }
template <class Visitor, class Traits, class RootIterator> template <class Visitor>
static void Visit(Graph* graph, Zone* zone, RootIterator root_begin, static void Visit(Graph* graph, Zone* zone, Node** root_begin,
RootIterator root_end, Visitor* visitor) { Node** root_end, Visitor* visitor) {
typedef typename Traits::Node Node; typedef typename Node::InputEdges::iterator Iterator;
typedef typename Traits::Iterator Iterator;
typedef std::pair<Iterator, Iterator> NodeState; typedef std::pair<Iterator, Iterator> NodeState;
typedef std::stack<NodeState, ZoneDeque<NodeState> > NodeStateStack; typedef std::stack<NodeState, ZoneDeque<NodeState> > NodeStateStack;
NodeStateStack stack((ZoneDeque<NodeState>(zone))); NodeStateStack stack((ZoneDeque<NodeState>(zone)));
BoolVector visited(Traits::max_id(graph), false, zone); BoolVector visited(graph->NodeCount(), false, zone);
Node* current = *root_begin; Node* current = *root_begin;
while (true) { while (true) {
DCHECK(current != NULL); DCHECK(current != NULL);
const int id = current->id(); const int id = current->id();
DCHECK(id >= 0); 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); bool visit = !GetVisited(&visited, id);
if (visit) { if (visit) {
visitor->Pre(current); visitor->Pre(current);
SetVisited(&visited, id); SetVisited(&visited, id);
} }
Iterator begin(visit ? Traits::begin(current) : Traits::end(current)); Iterator begin(visit ? current->input_edges().begin()
Iterator end(Traits::end(current)); : current->input_edges().end());
Iterator end(current->input_edges().end());
stack.push(NodeState(begin, end)); stack.push(NodeState(begin, end));
Node* post_order_node = current; Node* post_order_node = current;
while (true) { while (true) {
@ -67,27 +67,26 @@ class GenericGraphVisit {
current = *root_begin; current = *root_begin;
break; break;
} }
post_order_node = Traits::from(stack.top().first); post_order_node = (*stack.top().first).from();
visit = true; visit = true;
} else { } else {
visitor->PreEdge(Traits::from(top.first), (*top.first).index(), visitor->PreEdge((*top.first).from(), (*top.first).index(),
Traits::to(top.first)); (*top.first).to());
current = Traits::to(top.first); current = (*top.first).to();
if (!GetVisited(&visited, current->id())) break; if (!GetVisited(&visited, current->id())) break;
} }
top = stack.top(); top = stack.top();
visitor->PostEdge(Traits::from(top.first), (*top.first).index(), visitor->PostEdge((*top.first).from(), (*top.first).index(),
Traits::to(top.first)); (*top.first).to());
++stack.top().first; ++stack.top().first;
} }
} }
} }
template <class Visitor, class Traits> template <class Visitor>
static void Visit(Graph* graph, Zone* zone, typename Traits::Node* current, static void Visit(Graph* graph, Zone* zone, Node* current, Visitor* visitor) {
Visitor* visitor) { Node* array[] = {current};
typename Traits::Node* array[] = {current}; Visit<Visitor>(graph, zone, &array[0], &array[1], visitor);
Visit<Visitor, Traits>(graph, zone, &array[0], &array[1], visitor);
} }
struct NullNodeVisitor { struct NullNodeVisitor {
@ -111,8 +110,11 @@ class GenericGraphVisit {
return visited->at(id); return visited->at(id);
} }
}; };
}
} typedef GenericGraphVisit::NullNodeVisitor NullNodeVisitor;
} // namespace v8::internal::compiler
} // namespace compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_GENERIC_ALGORITHM_H_ #endif // V8_COMPILER_GENERIC_ALGORITHM_H_

View File

@ -5,7 +5,7 @@
#ifndef V8_COMPILER_GRAPH_INL_H_ #ifndef V8_COMPILER_GRAPH_INL_H_
#define 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" #include "src/compiler/graph.h"
namespace v8 { namespace v8 {
@ -15,8 +15,7 @@ namespace compiler {
template <class Visitor> template <class Visitor>
void Graph::VisitNodeInputsFromEnd(Visitor* visitor) { void Graph::VisitNodeInputsFromEnd(Visitor* visitor) {
Zone tmp_zone(zone()->isolate()); Zone tmp_zone(zone()->isolate());
GenericGraphVisit::Visit<Visitor, NodeInputIterationTraits<Node> >( GenericGraphVisit::Visit<Visitor>(this, &tmp_zone, end(), visitor);
this, &tmp_zone, end(), visitor);
} }
} // namespace compiler } // namespace compiler

View File

@ -5,6 +5,7 @@
#ifndef V8_COMPILER_GRAPH_REPLAY_H_ #ifndef V8_COMPILER_GRAPH_REPLAY_H_
#define V8_COMPILER_GRAPH_REPLAY_H_ #define V8_COMPILER_GRAPH_REPLAY_H_
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/node.h" #include "src/compiler/node.h"
namespace v8 { namespace v8 {

View File

@ -8,7 +8,6 @@
#include <map> #include <map>
#include <set> #include <set>
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/node.h" #include "src/compiler/node.h"
#include "src/compiler/node-aux-data.h" #include "src/compiler/node-aux-data.h"
#include "src/compiler/source-position.h" #include "src/compiler/source-position.h"

View File

@ -11,7 +11,6 @@
#include "src/v8.h" #include "src/v8.h"
#include "src/compiler/generic-algorithm.h"
#include "src/compiler/opcodes.h" #include "src/compiler/opcodes.h"
#include "src/compiler/operator.h" #include "src/compiler/operator.h"
#include "src/types.h" #include "src/types.h"
@ -426,8 +425,6 @@ class Node::Uses::iterator {
std::ostream& operator<<(std::ostream& os, const Node& n); 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 std::set<Node*, std::less<Node*>, zone_allocator<Node*> > NodeSet;
typedef NodeSet::iterator NodeSetIter; typedef NodeSet::iterator NodeSetIter;
typedef NodeSet::reverse_iterator NodeSetRIter; typedef NodeSet::reverse_iterator NodeSetRIter;

View File

@ -429,7 +429,6 @@
'../../src/compiler/frame.h', '../../src/compiler/frame.h',
'../../src/compiler/gap-resolver.cc', '../../src/compiler/gap-resolver.cc',
'../../src/compiler/gap-resolver.h', '../../src/compiler/gap-resolver.h',
'../../src/compiler/generic-algorithm-inl.h',
'../../src/compiler/generic-algorithm.h', '../../src/compiler/generic-algorithm.h',
'../../src/compiler/graph-builder.cc', '../../src/compiler/graph-builder.cc',
'../../src/compiler/graph-builder.h', '../../src/compiler/graph-builder.h',