1 //===- llvm/ADT/PostOrderIterator.h - PostOrder iterator --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file builds on the ADT/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 LLVM_ADT_POSTORDERITERATOR_H
17 #define LLVM_ADT_POSTORDERITERATOR_H
18
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include <set>
22 #include <vector>
23
24 namespace llvm {
25
26 template<class SetType, bool External> // Non-external set
27 class po_iterator_storage {
28 public:
29 SetType Visited;
30 };
31
32 /// DFSetTraits - Allow the SetType used to record depth-first search results to
33 /// optionally record node postorder.
34 template<class SetType>
35 struct DFSetTraits {
finishPostorderDFSetTraits36 static void finishPostorder(
37 typename SetType::iterator::value_type, SetType &) {}
38 };
39
40 template<class SetType>
41 class po_iterator_storage<SetType, true> {
42 public:
po_iterator_storage(SetType & VSet)43 po_iterator_storage(SetType &VSet) : Visited(VSet) {}
po_iterator_storage(const po_iterator_storage & S)44 po_iterator_storage(const po_iterator_storage &S) : Visited(S.Visited) {}
45 SetType &Visited;
46 };
47
48 template<class GraphT,
49 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
50 bool ExtStorage = false,
51 class GT = GraphTraits<GraphT> >
52 class po_iterator : public std::iterator<std::forward_iterator_tag,
53 typename GT::NodeType, ptrdiff_t>,
54 public po_iterator_storage<SetType, ExtStorage> {
55 typedef std::iterator<std::forward_iterator_tag,
56 typename GT::NodeType, ptrdiff_t> super;
57 typedef typename GT::NodeType NodeType;
58 typedef typename GT::ChildIteratorType ChildItTy;
59
60 // VisitStack - Used to maintain the ordering. Top = current block
61 // First element is basic block pointer, second is the 'next child' to visit
62 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack;
63
traverseChild()64 void traverseChild() {
65 while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) {
66 NodeType *BB = *VisitStack.back().second++;
67 if (this->Visited.insert(BB)) { // If the block is not visited...
68 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
69 }
70 }
71 }
72
po_iterator(NodeType * BB)73 inline po_iterator(NodeType *BB) {
74 this->Visited.insert(BB);
75 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
76 traverseChild();
77 }
po_iterator()78 inline po_iterator() {} // End is when stack is empty.
79
po_iterator(NodeType * BB,SetType & S)80 inline po_iterator(NodeType *BB, SetType &S) :
81 po_iterator_storage<SetType, ExtStorage>(S) {
82 if (this->Visited.insert(BB)) {
83 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB)));
84 traverseChild();
85 }
86 }
87
po_iterator(SetType & S)88 inline po_iterator(SetType &S) :
89 po_iterator_storage<SetType, ExtStorage>(S) {
90 } // End is when stack is empty.
91 public:
92 typedef typename super::pointer pointer;
93 typedef po_iterator<GraphT, SetType, ExtStorage, GT> _Self;
94
95 // Provide static "constructors"...
begin(GraphT G)96 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
end(GraphT G)97 static inline _Self end (GraphT G) { return _Self(); }
98
begin(GraphT G,SetType & S)99 static inline _Self begin(GraphT G, SetType &S) {
100 return _Self(GT::getEntryNode(G), S);
101 }
end(GraphT G,SetType & S)102 static inline _Self end (GraphT G, SetType &S) { return _Self(S); }
103
104 inline bool operator==(const _Self& x) const {
105 return VisitStack == x.VisitStack;
106 }
107 inline bool operator!=(const _Self& x) const { return !operator==(x); }
108
109 inline pointer operator*() const {
110 return VisitStack.back().first;
111 }
112
113 // This is a nonstandard operator-> that dereferences the pointer an extra
114 // time... so that you can actually call methods ON the BasicBlock, because
115 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
116 //
117 inline NodeType *operator->() const { return operator*(); }
118
119 inline _Self& operator++() { // Preincrement
120 DFSetTraits<SetType>::finishPostorder(VisitStack.back().first,
121 this->Visited);
122 VisitStack.pop_back();
123 if (!VisitStack.empty())
124 traverseChild();
125 return *this;
126 }
127
128 inline _Self operator++(int) { // Postincrement
129 _Self tmp = *this; ++*this; return tmp;
130 }
131 };
132
133 // Provide global constructors that automatically figure out correct types...
134 //
135 template <class T>
po_begin(T G)136 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
137 template <class T>
po_end(T G)138 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
139
140 // Provide global definitions of external postorder iterators...
141 template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> >
142 struct po_ext_iterator : public po_iterator<T, SetType, true> {
po_ext_iteratorpo_ext_iterator143 po_ext_iterator(const po_iterator<T, SetType, true> &V) :
144 po_iterator<T, SetType, true>(V) {}
145 };
146
147 template<class T, class SetType>
po_ext_begin(T G,SetType & S)148 po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) {
149 return po_ext_iterator<T, SetType>::begin(G, S);
150 }
151
152 template<class T, class SetType>
po_ext_end(T G,SetType & S)153 po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) {
154 return po_ext_iterator<T, SetType>::end(G, S);
155 }
156
157 // Provide global definitions of inverse post order iterators...
158 template <class T,
159 class SetType = std::set<typename GraphTraits<T>::NodeType*>,
160 bool External = false>
161 struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External > {
ipo_iteratoripo_iterator162 ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) :
163 po_iterator<Inverse<T>, SetType, External> (V) {}
164 };
165
166 template <class T>
167 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
168 return ipo_iterator<T>::begin(G, Reverse);
169 }
170
171 template <class T>
ipo_end(T G)172 ipo_iterator<T> ipo_end(T G){
173 return ipo_iterator<T>::end(G);
174 }
175
176 //Provide global definitions of external inverse postorder iterators...
177 template <class T,
178 class SetType = std::set<typename GraphTraits<T>::NodeType*> >
179 struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> {
ipo_ext_iteratoripo_ext_iterator180 ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) :
181 ipo_iterator<T, SetType, true>(&V) {}
ipo_ext_iteratoripo_ext_iterator182 ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) :
183 ipo_iterator<T, SetType, true>(&V) {}
184 };
185
186 template <class T, class SetType>
ipo_ext_begin(T G,SetType & S)187 ipo_ext_iterator<T, SetType> ipo_ext_begin(T G, SetType &S) {
188 return ipo_ext_iterator<T, SetType>::begin(G, S);
189 }
190
191 template <class T, class SetType>
ipo_ext_end(T G,SetType & S)192 ipo_ext_iterator<T, SetType> ipo_ext_end(T G, SetType &S) {
193 return ipo_ext_iterator<T, SetType>::end(G, S);
194 }
195
196 //===--------------------------------------------------------------------===//
197 // Reverse Post Order CFG iterator code
198 //===--------------------------------------------------------------------===//
199 //
200 // This is used to visit basic blocks in a method in reverse post order. This
201 // class is awkward to use because I don't know a good incremental algorithm to
202 // computer RPO from a graph. Because of this, the construction of the
203 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
204 // with a postorder iterator to build the data structures). The moral of this
205 // story is: Don't create more ReversePostOrderTraversal classes than necessary.
206 //
207 // This class should be used like this:
208 // {
209 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
210 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
211 // ...
212 // }
213 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
214 // ...
215 // }
216 // }
217 //
218
219 template<class GraphT, class GT = GraphTraits<GraphT> >
220 class ReversePostOrderTraversal {
221 typedef typename GT::NodeType NodeType;
222 std::vector<NodeType*> Blocks; // Block list in normal PO order
Initialize(NodeType * BB)223 inline void Initialize(NodeType *BB) {
224 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
225 }
226 public:
227 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
228
ReversePostOrderTraversal(GraphT G)229 inline ReversePostOrderTraversal(GraphT G) {
230 Initialize(GT::getEntryNode(G));
231 }
232
233 // Because we want a reverse post order, use reverse iterators from the vector
begin()234 inline rpo_iterator begin() { return Blocks.rbegin(); }
end()235 inline rpo_iterator end() { return Blocks.rend(); }
236 };
237
238 } // End llvm namespace
239
240 #endif
241