LLVM API Documentation
00001 //===- llvm/ADT/PostOrderIterator.h - PostOrder iterator --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file builds on the ADT/GraphTraits.h file to build a generic graph 00011 // post order iterator. This should work over any graph type that has a 00012 // GraphTraits specialization. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_ADT_POSTORDERITERATOR_H 00017 #define LLVM_ADT_POSTORDERITERATOR_H 00018 00019 #include "llvm/ADT/GraphTraits.h" 00020 #include "llvm/ADT/iterator.h" 00021 #include "llvm/ADT/SmallPtrSet.h" 00022 #include <set> 00023 #include <stack> 00024 #include <vector> 00025 00026 namespace llvm { 00027 00028 template<class SetType, bool External> // Non-external set 00029 class po_iterator_storage { 00030 public: 00031 SetType Visited; 00032 }; 00033 00034 template<class SetType> 00035 class po_iterator_storage<SetType, true> { 00036 public: 00037 po_iterator_storage(SetType &VSet) : Visited(VSet) {} 00038 po_iterator_storage(const po_iterator_storage &S) : Visited(S.Visited) {} 00039 SetType &Visited; 00040 }; 00041 00042 template<class GraphT, 00043 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>, 00044 bool ExtStorage = false, 00045 class GT = GraphTraits<GraphT> > 00046 class po_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>, 00047 public po_iterator_storage<SetType, ExtStorage> { 00048 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super; 00049 typedef typename GT::NodeType NodeType; 00050 typedef typename GT::ChildIteratorType ChildItTy; 00051 00052 // VisitStack - Used to maintain the ordering. Top = current block 00053 // First element is basic block pointer, second is the 'next child' to visit 00054 std::stack<std::pair<NodeType *, ChildItTy> > VisitStack; 00055 00056 void traverseChild() { 00057 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) { 00058 NodeType *BB = *VisitStack.top().second++; 00059 if (!this->Visited.count(BB)) { // If the block is not visited... 00060 this->Visited.insert(BB); 00061 VisitStack.push(std::make_pair(BB, GT::child_begin(BB))); 00062 } 00063 } 00064 } 00065 00066 inline po_iterator(NodeType *BB) { 00067 this->Visited.insert(BB); 00068 VisitStack.push(std::make_pair(BB, GT::child_begin(BB))); 00069 traverseChild(); 00070 } 00071 inline po_iterator() {} // End is when stack is empty. 00072 00073 inline po_iterator(NodeType *BB, SetType &S) : 00074 po_iterator_storage<SetType, ExtStorage>(S) { 00075 if(!S.count(BB)) { 00076 this->Visited.insert(BB); 00077 VisitStack.push(std::make_pair(BB, GT::child_begin(BB))); 00078 traverseChild(); 00079 } 00080 } 00081 00082 inline po_iterator(SetType &S) : 00083 po_iterator_storage<SetType, ExtStorage>(S) { 00084 } // End is when stack is empty. 00085 public: 00086 typedef typename super::pointer pointer; 00087 typedef po_iterator<GraphT, SetType, ExtStorage, GT> _Self; 00088 00089 // Provide static "constructors"... 00090 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); } 00091 static inline _Self end (GraphT G) { return _Self(); } 00092 00093 static inline _Self begin(GraphT G, SetType &S) { 00094 return _Self(GT::getEntryNode(G), S); 00095 } 00096 static inline _Self end (GraphT G, SetType &S) { return _Self(S); } 00097 00098 inline bool operator==(const _Self& x) const { 00099 return VisitStack == x.VisitStack; 00100 } 00101 inline bool operator!=(const _Self& x) const { return !operator==(x); } 00102 00103 inline pointer operator*() const { 00104 return VisitStack.top().first; 00105 } 00106 00107 // This is a nonstandard operator-> that dereferences the pointer an extra 00108 // time... so that you can actually call methods ON the BasicBlock, because 00109 // the contained type is a pointer. This allows BBIt->getTerminator() f.e. 00110 // 00111 inline NodeType *operator->() const { return operator*(); } 00112 00113 inline _Self& operator++() { // Preincrement 00114 VisitStack.pop(); 00115 if (!VisitStack.empty()) 00116 traverseChild(); 00117 return *this; 00118 } 00119 00120 inline _Self operator++(int) { // Postincrement 00121 _Self tmp = *this; ++*this; return tmp; 00122 } 00123 }; 00124 00125 // Provide global constructors that automatically figure out correct types... 00126 // 00127 template <class T> 00128 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); } 00129 template <class T> 00130 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); } 00131 00132 // Provide global definitions of external postorder iterators... 00133 template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> > 00134 struct po_ext_iterator : public po_iterator<T, SetType, true> { 00135 po_ext_iterator(const po_iterator<T, SetType, true> &V) : 00136 po_iterator<T, SetType, true>(V) {} 00137 }; 00138 00139 template<class T, class SetType> 00140 po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) { 00141 return po_ext_iterator<T, SetType>::begin(G, S); 00142 } 00143 00144 template<class T, class SetType> 00145 po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) { 00146 return po_ext_iterator<T, SetType>::end(G, S); 00147 } 00148 00149 // Provide global definitions of inverse post order iterators... 00150 template <class T, 00151 class SetType = std::set<typename GraphTraits<T>::NodeType*>, 00152 bool External = false> 00153 struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External > { 00154 ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) : 00155 po_iterator<Inverse<T>, SetType, External> (V) {} 00156 }; 00157 00158 template <class T> 00159 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) { 00160 return ipo_iterator<T>::begin(G, Reverse); 00161 } 00162 00163 template <class T> 00164 ipo_iterator<T> ipo_end(T G){ 00165 return ipo_iterator<T>::end(G); 00166 } 00167 00168 //Provide global definitions of external inverse postorder iterators... 00169 template <class T, 00170 class SetType = std::set<typename GraphTraits<T>::NodeType*> > 00171 struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> { 00172 ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) : 00173 ipo_iterator<T, SetType, true>(&V) {} 00174 ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) : 00175 ipo_iterator<T, SetType, true>(&V) {} 00176 }; 00177 00178 template <class T, class SetType> 00179 ipo_ext_iterator<T, SetType> ipo_ext_begin(T G, SetType &S) { 00180 return ipo_ext_iterator<T, SetType>::begin(G, S); 00181 } 00182 00183 template <class T, class SetType> 00184 ipo_ext_iterator<T, SetType> ipo_ext_end(T G, SetType &S) { 00185 return ipo_ext_iterator<T, SetType>::end(G, S); 00186 } 00187 00188 //===--------------------------------------------------------------------===// 00189 // Reverse Post Order CFG iterator code 00190 //===--------------------------------------------------------------------===// 00191 // 00192 // This is used to visit basic blocks in a method in reverse post order. This 00193 // class is awkward to use because I don't know a good incremental algorithm to 00194 // computer RPO from a graph. Because of this, the construction of the 00195 // ReversePostOrderTraversal object is expensive (it must walk the entire graph 00196 // with a postorder iterator to build the data structures). The moral of this 00197 // story is: Don't create more ReversePostOrderTraversal classes than necessary. 00198 // 00199 // This class should be used like this: 00200 // { 00201 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create 00202 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) { 00203 // ... 00204 // } 00205 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) { 00206 // ... 00207 // } 00208 // } 00209 // 00210 00211 template<class GraphT, class GT = GraphTraits<GraphT> > 00212 class ReversePostOrderTraversal { 00213 typedef typename GT::NodeType NodeType; 00214 std::vector<NodeType*> Blocks; // Block list in normal PO order 00215 inline void Initialize(NodeType *BB) { 00216 copy(po_begin(BB), po_end(BB), back_inserter(Blocks)); 00217 } 00218 public: 00219 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator; 00220 00221 inline ReversePostOrderTraversal(GraphT G) { 00222 Initialize(GT::getEntryNode(G)); 00223 } 00224 00225 // Because we want a reverse post order, use reverse iterators from the vector 00226 inline rpo_iterator begin() { return Blocks.rbegin(); } 00227 inline rpo_iterator end() { return Blocks.rend(); } 00228 }; 00229 00230 } // End llvm namespace 00231 00232 #endif