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