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