OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_GENERIC_ALGORITHM_H_ | 5 #ifndef V8_COMPILER_GENERIC_ALGORITHM_H_ |
6 #define V8_COMPILER_GENERIC_ALGORITHM_H_ | 6 #define V8_COMPILER_GENERIC_ALGORITHM_H_ |
7 | 7 |
8 #include <stack> | 8 #include <stack> |
9 | 9 |
10 #include "src/compiler/generic-graph.h" | 10 #include "src/compiler/generic-graph.h" |
11 #include "src/compiler/generic-node.h" | 11 #include "src/compiler/generic-node.h" |
12 #include "src/zone-containers.h" | 12 #include "src/zone-containers.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 namespace compiler { | 16 namespace compiler { |
17 | 17 |
18 // GenericGraphVisit allows visitation of graphs of nodes and edges in pre- and | 18 // GenericGraphVisit allows visitation of graphs of nodes and edges in pre- and |
19 // post-order. Visitation uses an explicitly allocated stack rather than the | 19 // post-order. Visitation uses an explicitly allocated stack rather than the |
20 // execution stack to avoid stack overflow. Although GenericGraphVisit is | 20 // execution stack to avoid stack overflow. Although GenericGraphVisit is |
21 // primarily intended to traverse networks of nodes through their | 21 // primarily intended to traverse networks of nodes through their |
22 // dependencies and uses, it also can be used to visit any graph-like network | 22 // dependencies and uses, it also can be used to visit any graph-like network |
23 // by specifying custom traits. | 23 // by specifying custom traits. |
24 class GenericGraphVisit { | 24 class GenericGraphVisit { |
25 public: | 25 public: |
26 enum Control { | |
27 CONTINUE = 0x0, // Continue depth-first normally | |
28 SKIP = 0x1, // Skip this node and its successors | |
29 REENTER = 0x2, // Allow reentering this node | |
30 DEFER = SKIP | REENTER | |
31 }; | |
32 | |
33 // struct Visitor { | 26 // struct Visitor { |
34 // Control Pre(Traits::Node* current); | 27 // void Pre(Traits::Node* current); |
35 // Control Post(Traits::Node* current); | 28 // void Post(Traits::Node* current); |
36 // void PreEdge(Traits::Node* from, int index, Traits::Node* to); | 29 // void PreEdge(Traits::Node* from, int index, Traits::Node* to); |
37 // void PostEdge(Traits::Node* from, int index, Traits::Node* to); | 30 // void PostEdge(Traits::Node* from, int index, Traits::Node* to); |
38 // } | 31 // } |
39 template <class Visitor, class Traits, class RootIterator> | 32 template <class Visitor, class Traits, class RootIterator> |
40 static void Visit(GenericGraphBase* graph, Zone* zone, | 33 static void Visit(GenericGraphBase* graph, Zone* zone, |
41 RootIterator root_begin, RootIterator root_end, | 34 RootIterator root_begin, RootIterator root_end, |
42 Visitor* visitor) { | 35 Visitor* visitor) { |
43 typedef typename Traits::Node Node; | 36 typedef typename Traits::Node Node; |
44 typedef typename Traits::Iterator Iterator; | 37 typedef typename Traits::Iterator Iterator; |
45 typedef std::pair<Iterator, Iterator> NodeState; | 38 typedef std::pair<Iterator, Iterator> NodeState; |
46 typedef std::stack<NodeState, ZoneDeque<NodeState> > NodeStateStack; | 39 typedef std::stack<NodeState, ZoneDeque<NodeState> > NodeStateStack; |
47 NodeStateStack stack((ZoneDeque<NodeState>(zone))); | 40 NodeStateStack stack((ZoneDeque<NodeState>(zone))); |
48 BoolVector visited(Traits::max_id(graph), false, zone); | 41 BoolVector visited(Traits::max_id(graph), false, zone); |
49 Node* current = *root_begin; | 42 Node* current = *root_begin; |
50 while (true) { | 43 while (true) { |
51 DCHECK(current != NULL); | 44 DCHECK(current != NULL); |
52 const int id = current->id(); | 45 const int id = current->id(); |
53 DCHECK(id >= 0); | 46 DCHECK(id >= 0); |
54 DCHECK(id < Traits::max_id(graph)); // Must be a valid id. | 47 DCHECK(id < Traits::max_id(graph)); // Must be a valid id. |
55 bool visit = !GetVisited(&visited, id); | 48 bool visit = !GetVisited(&visited, id); |
56 if (visit) { | 49 if (visit) { |
57 Control control = visitor->Pre(current); | 50 visitor->Pre(current); |
58 visit = !IsSkip(control); | 51 SetVisited(&visited, id, true); |
Jarin
2014/11/03 10:06:43
How about removing the last parameter to SetVisite
Michael Starzinger
2014/11/03 10:23:06
Done.
| |
59 if (!IsReenter(control)) SetVisited(&visited, id, true); | |
60 } | 52 } |
61 Iterator begin(visit ? Traits::begin(current) : Traits::end(current)); | 53 Iterator begin(visit ? Traits::begin(current) : Traits::end(current)); |
62 Iterator end(Traits::end(current)); | 54 Iterator end(Traits::end(current)); |
63 stack.push(NodeState(begin, end)); | 55 stack.push(NodeState(begin, end)); |
64 Node* post_order_node = current; | 56 Node* post_order_node = current; |
65 while (true) { | 57 while (true) { |
66 NodeState top = stack.top(); | 58 NodeState top = stack.top(); |
67 if (top.first == top.second) { | 59 if (top.first == top.second) { |
68 if (visit) { | 60 if (visit) { |
69 Control control = visitor->Post(post_order_node); | 61 visitor->Post(post_order_node); |
70 DCHECK(!IsSkip(control)); | 62 SetVisited(&visited, post_order_node->id(), true); |
71 SetVisited(&visited, post_order_node->id(), !IsReenter(control)); | |
72 } | 63 } |
73 stack.pop(); | 64 stack.pop(); |
74 if (stack.empty()) { | 65 if (stack.empty()) { |
75 if (++root_begin == root_end) return; | 66 if (++root_begin == root_end) return; |
76 current = *root_begin; | 67 current = *root_begin; |
77 break; | 68 break; |
78 } | 69 } |
79 post_order_node = Traits::from(stack.top().first); | 70 post_order_node = Traits::from(stack.top().first); |
80 visit = true; | 71 visit = true; |
81 } else { | 72 } else { |
(...skipping 12 matching lines...) Expand all Loading... | |
94 | 85 |
95 template <class Visitor, class Traits> | 86 template <class Visitor, class Traits> |
96 static void Visit(GenericGraphBase* graph, Zone* zone, | 87 static void Visit(GenericGraphBase* graph, Zone* zone, |
97 typename Traits::Node* current, Visitor* visitor) { | 88 typename Traits::Node* current, Visitor* visitor) { |
98 typename Traits::Node* array[] = {current}; | 89 typename Traits::Node* array[] = {current}; |
99 Visit<Visitor, Traits>(graph, zone, &array[0], &array[1], visitor); | 90 Visit<Visitor, Traits>(graph, zone, &array[0], &array[1], visitor); |
100 } | 91 } |
101 | 92 |
102 template <class B, class S> | 93 template <class B, class S> |
103 struct NullNodeVisitor { | 94 struct NullNodeVisitor { |
104 Control Pre(GenericNode<B, S>* node) { return CONTINUE; } | 95 void Pre(GenericNode<B, S>* node) {} |
105 Control Post(GenericNode<B, S>* node) { return CONTINUE; } | 96 void Post(GenericNode<B, S>* node) {} |
106 void PreEdge(GenericNode<B, S>* from, int index, GenericNode<B, S>* to) {} | 97 void PreEdge(GenericNode<B, S>* from, int index, GenericNode<B, S>* to) {} |
107 void PostEdge(GenericNode<B, S>* from, int index, GenericNode<B, S>* to) {} | 98 void PostEdge(GenericNode<B, S>* from, int index, GenericNode<B, S>* to) {} |
108 }; | 99 }; |
109 | 100 |
110 private: | 101 private: |
111 static bool IsSkip(Control c) { return c & SKIP; } | |
112 static bool IsReenter(Control c) { return c & REENTER; } | |
113 | |
114 // TODO(turbofan): resizing could be optionally templatized away. | 102 // TODO(turbofan): resizing could be optionally templatized away. |
115 static void SetVisited(BoolVector* visited, int id, bool value) { | 103 static void SetVisited(BoolVector* visited, int id, bool value) { |
116 if (id >= static_cast<int>(visited->size())) { | 104 if (id >= static_cast<int>(visited->size())) { |
117 // Resize and set all values to unvisited. | 105 // Resize and set all values to unvisited. |
118 visited->resize((3 * id) / 2, false); | 106 visited->resize((3 * id) / 2, false); |
119 } | 107 } |
120 visited->at(id) = value; | 108 visited->at(id) = value; |
121 } | 109 } |
122 | 110 |
123 static bool GetVisited(BoolVector* visited, int id) { | 111 static bool GetVisited(BoolVector* visited, int id) { |
124 if (id >= static_cast<int>(visited->size())) return false; | 112 if (id >= static_cast<int>(visited->size())) return false; |
125 return visited->at(id); | 113 return visited->at(id); |
126 } | 114 } |
127 }; | 115 }; |
128 } | 116 } |
129 } | 117 } |
130 } // namespace v8::internal::compiler | 118 } // namespace v8::internal::compiler |
131 | 119 |
132 #endif // V8_COMPILER_GENERIC_ALGORITHM_H_ | 120 #endif // V8_COMPILER_GENERIC_ALGORITHM_H_ |
OLD | NEW |