Genericize the ReversePostOrderIterator.
[oota-llvm.git] / include / Support / PostOrderIterator.h
1 //===-- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*--=//
2 //
3 // This file builds on the Support/GraphTraits.h file to build a generic graph
4 // post order iterator.  This should work over any graph type that has a
5 // GraphTraits specialization.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_POSTORDER_ITERATOR_H
10 #define LLVM_SUPPORT_POSTORDER_ITERATOR_H
11
12 #include "Support/GraphTraits.h"
13 #include <iterator>
14 #include <stack>
15 #include <set>
16
17 template<class GraphT, class GT = GraphTraits<GraphT> >
18 class po_iterator : public std::forward_iterator<typename GT::NodeType,
19                                                  ptrdiff_t> {
20   typedef typename GT::NodeType          NodeType;
21   typedef typename GT::ChildIteratorType ChildItTy;
22
23   std::set<NodeType *> Visited;    // All of the blocks visited so far...
24   // VisitStack - Used to maintain the ordering.  Top = current block
25   // First element is basic block pointer, second is the 'next child' to visit
26   std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
27
28   void traverseChild() {
29     while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
30       NodeType *BB = *VisitStack.top().second++;
31       if (!Visited.count(BB)) {  // If the block is not visited...
32         Visited.insert(BB);
33         VisitStack.push(make_pair(BB, GT::child_begin(BB)));
34       }
35     }
36   }
37
38   inline po_iterator(NodeType *BB) {
39     Visited.insert(BB);
40     VisitStack.push(make_pair(BB, GT::child_begin(BB)));
41     traverseChild();
42   }
43   inline po_iterator() { /* End is when stack is empty */ }
44 public:
45   typedef po_iterator<GraphT, GT> _Self;
46
47   // Provide static "constructors"...
48   static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
49   static inline _Self end  (GraphT G) { return _Self(); }
50
51   inline bool operator==(const _Self& x) const { 
52     return VisitStack == x.VisitStack;
53   }
54   inline bool operator!=(const _Self& x) const { return !operator==(x); }
55
56   inline pointer operator*() const { 
57     return VisitStack.top().first;
58   }
59
60   // This is a nonstandard operator-> that dereferences the pointer an extra
61   // time... so that you can actually call methods ON the BasicBlock, because
62   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
63   //
64   inline NodeType *operator->() const { return operator*(); }
65
66   inline _Self& operator++() {   // Preincrement
67     VisitStack.pop();
68     if (!VisitStack.empty())
69       traverseChild();
70     return *this; 
71   }
72
73   inline _Self operator++(int) { // Postincrement
74     _Self tmp = *this; ++*this; return tmp; 
75   }
76 };
77
78 // Provide global constructors that automatically figure out correct types...
79 //
80 template <class T>
81 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
82 template <class T>
83 po_iterator<T> po_end  (T G) { return po_iterator<T>::end(G); }
84
85 // Provide global definitions of inverse post order iterators...
86 template <class T>
87 struct ipo_iterator : public po_iterator<Inverse<T> > {
88   ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
89 };
90
91 template <class T>
92 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
93   return ipo_iterator<T>::begin(G, Reverse);
94 }
95
96 template <class T>
97 ipo_iterator<T> ipo_end(T G){
98   return ipo_iterator<T>::end(G);
99 }
100
101
102 //===--------------------------------------------------------------------===//
103 // Reverse Post Order CFG iterator code
104 //===--------------------------------------------------------------------===//
105 // 
106 // This is used to visit basic blocks in a method in reverse post order.  This
107 // class is awkward to use because I don't know a good incremental algorithm to
108 // computer RPO from a graph.  Because of this, the construction of the 
109 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
110 // with a postorder iterator to build the data structures).  The moral of this
111 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
112 //
113 // This class should be used like this:
114 // {
115 //   ReversePostOrderTraversal<Method*> RPOT(MethodPtr); // Expensive to create
116 //   for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
117 //      ...
118 //   }
119 //   for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
120 //      ...
121 //   }
122 // }
123 //
124
125 template<class GraphT, class GT = GraphTraits<GraphT> >
126 class ReversePostOrderTraversal {
127   typedef typename GT::NodeType NodeType;
128   std::vector<NodeType*> Blocks;       // Block list in normal PO order
129   inline void Initialize(NodeType *BB) {
130     copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
131   }
132 public:
133   typedef std::vector<NodeType*>::reverse_iterator rpo_iterator;
134
135   inline ReversePostOrderTraversal(GraphT G) {
136     Initialize(GT::getEntryNode(G));
137   }
138
139   // Because we want a reverse post order, use reverse iterators from the vector
140   inline rpo_iterator begin() { return Blocks.rbegin(); }
141   inline rpo_iterator end()   { return Blocks.rend(); }
142 };
143
144 #endif