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