1 //===- llvm/ADT/PostOrderIterator.h - PostOrder iterator --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_POSTORDERITERATOR_H
17 #define LLVM_ADT_POSTORDERITERATOR_H
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/iterator"
26 template<class SetType, bool External> // Non-external set
27 class po_iterator_storage {
32 template<class SetType>
33 class po_iterator_storage<SetType, true> {
35 po_iterator_storage(SetType &VSet) : Visited(VSet) {}
36 po_iterator_storage(const po_iterator_storage &S) : Visited(S.Visited) {}
40 template<class GraphT,
41 class SetType = std::set<typename GraphTraits<GraphT>::NodeType*>,
42 bool ExtStorage = false,
43 class GT = GraphTraits<GraphT> >
44 class po_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>,
45 public po_iterator_storage<SetType, ExtStorage> {
46 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
47 typedef typename GT::NodeType NodeType;
48 typedef typename GT::ChildIteratorType ChildItTy;
50 // VisitStack - Used to maintain the ordering. Top = current block
51 // First element is basic block pointer, second is the 'next child' to visit
52 std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
54 void traverseChild() {
55 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
56 NodeType *BB = *VisitStack.top().second++;
57 if (!this->Visited.count(BB)) { // If the block is not visited...
58 this->Visited.insert(BB);
59 VisitStack.push(std::make_pair(BB, GT::child_begin(BB)));
64 inline po_iterator(NodeType *BB) {
65 this->Visited.insert(BB);
66 VisitStack.push(std::make_pair(BB, GT::child_begin(BB)));
69 inline po_iterator() {} // End is when stack is empty.
71 inline po_iterator(NodeType *BB, SetType &S) :
72 po_iterator_storage<SetType, ExtStorage>(&S) {
74 this->Visited.insert(BB);
75 VisitStack.push(std::make_pair(BB, GT::child_begin(BB)));
80 inline po_iterator(SetType &S) :
81 po_iterator_storage<SetType, ExtStorage>(&S) {
82 } // End is when stack is empty.
84 typedef typename super::pointer pointer;
85 typedef po_iterator<GraphT, SetType, ExtStorage, GT> _Self;
87 // Provide static "constructors"...
88 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
89 static inline _Self end (GraphT G) { return _Self(); }
91 static inline _Self begin(GraphT G, SetType &S) {
92 return _Self(GT::getEntryNode(G), S);
94 static inline _Self end (GraphT G, SetType &S) { return _Self(S); }
96 inline bool operator==(const _Self& x) const {
97 return VisitStack == x.VisitStack;
99 inline bool operator!=(const _Self& x) const { return !operator==(x); }
101 inline pointer operator*() const {
102 return VisitStack.top().first;
105 // This is a nonstandard operator-> that dereferences the pointer an extra
106 // time... so that you can actually call methods ON the BasicBlock, because
107 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
109 inline NodeType *operator->() const { return operator*(); }
111 inline _Self& operator++() { // Preincrement
113 if (!VisitStack.empty())
118 inline _Self operator++(int) { // Postincrement
119 _Self tmp = *this; ++*this; return tmp;
123 // Provide global constructors that automatically figure out correct types...
126 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
128 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
130 // Provide global definitions of external postorder iterators...
131 template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> >
132 struct po_ext_iterator : public po_iterator<T, SetType, true> {
133 po_ext_iterator(const po_iterator<T, SetType, true> &V) :
134 po_iterator<T, SetType, true>(V) {}
137 template<class T, class SetType>
138 po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) {
139 return po_ext_iterator<T, SetType>::begin(G, S);
142 template<class T, class SetType>
143 po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) {
144 return po_ext_iterator<T, SetType>::end(G, S);
147 // Provide global definitions of inverse post order iterators...
149 class SetType = std::set<typename GraphTraits<T>::NoddeType*>,
150 bool External = false>
151 struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External > {
152 ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) :
153 po_iterator<Inverse<T>, SetType, External> (V) {}
157 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
158 return ipo_iterator<T>::begin(G, Reverse);
162 ipo_iterator<T> ipo_end(T G){
163 return ipo_iterator<T>::end(G);
166 //Provide global definitions of external inverse postorder iterators...
167 template <class T, class SetType = std::set<typename GraphTraits<T>::NodeType*> >
168 struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> {
169 ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) :
170 ipo_iterator<T, SetType, true>(&V) {}
171 ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) :
172 ipo_iterator<T, SetType, true>(&V) {}
175 template <class T, class SetType>
176 ipo_ext_iterator<T, SetType> ipo_ext_begin(T G, SetType &S) {
177 return ipo_ext_iterator<T, SetType>::begin(G, S);
180 template <class T, class SetType>
181 ipo_ext_iterator<T, SetType> ipo_ext_end(T G, SetType &S) {
182 return ipo_ext_iterator<T, SetType>::end(G, S);
185 //===--------------------------------------------------------------------===//
186 // Reverse Post Order CFG iterator code
187 //===--------------------------------------------------------------------===//
189 // This is used to visit basic blocks in a method in reverse post order. This
190 // class is awkward to use because I don't know a good incremental algorithm to
191 // computer RPO from a graph. Because of this, the construction of the
192 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
193 // with a postorder iterator to build the data structures). The moral of this
194 // story is: Don't create more ReversePostOrderTraversal classes than necessary.
196 // This class should be used like this:
198 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
199 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
202 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
208 template<class GraphT, class GT = GraphTraits<GraphT> >
209 class ReversePostOrderTraversal {
210 typedef typename GT::NodeType NodeType;
211 std::vector<NodeType*> Blocks; // Block list in normal PO order
212 inline void Initialize(NodeType *BB) {
213 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
216 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
218 inline ReversePostOrderTraversal(GraphT G) {
219 Initialize(GT::getEntryNode(G));
222 // Because we want a reverse post order, use reverse iterators from the vector
223 inline rpo_iterator begin() { return Blocks.rbegin(); }
224 inline rpo_iterator end() { return Blocks.rend(); }
227 } // End llvm namespace