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