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/SmallPtrSet.h" 00021 #include <set> 00022 #include <vector> 00023 00024 namespace llvm { 00025 00026 // The po_iterator_storage template provides access to the set of already 00027 // visited nodes during the po_iterator's depth-first traversal. 00028 // 00029 // The default implementation simply contains a set of visited nodes, while 00030 // the Extended=true version uses a reference to an external set. 00031 // 00032 // It is possible to prune the depth-first traversal in several ways: 00033 // 00034 // - When providing an external set that already contains some graph nodes, 00035 // those nodes won't be visited again. This is useful for restarting a 00036 // post-order traversal on a graph with nodes that aren't dominated by a 00037 // single node. 00038 // 00039 // - By providing a custom SetType class, unwanted graph nodes can be excluded 00040 // by having the insert() function return false. This could for example 00041 // confine a CFG traversal to blocks in a specific loop. 00042 // 00043 // - Finally, by specializing the po_iterator_storage template itself, graph 00044 // edges can be pruned by returning false in the insertEdge() function. This 00045 // could be used to remove loop back-edges from the CFG seen by po_iterator. 00046 // 00047 // A specialized po_iterator_storage class can observe both the pre-order and 00048 // the post-order. The insertEdge() function is called in a pre-order, while 00049 // the finishPostorder() function is called just before the po_iterator moves 00050 // on to the next node. 00051 00052 /// Default po_iterator_storage implementation with an internal set object. 00053 template<class SetType, bool External> 00054 class po_iterator_storage { 00055 SetType Visited; 00056 public: 00057 // Return true if edge destination should be visited. 00058 template<typename NodeType> 00059 bool insertEdge(NodeType *From, NodeType *To) { 00060 return Visited.insert(To); 00061 } 00062 00063 // Called after all children of BB have been visited. 00064 template<typename NodeType> 00065 void finishPostorder(NodeType *BB) {} 00066 }; 00067 00068 /// Specialization of po_iterator_storage that references an external set. 00069 template<class SetType> 00070 class po_iterator_storage<SetType, true> { 00071 SetType &Visited; 00072 public: 00073 po_iterator_storage(SetType &VSet) : Visited(VSet) {} 00074 po_iterator_storage(const po_iterator_storage &S) : Visited(S.Visited) {} 00075 00076 // Return true if edge destination should be visited, called with From = 0 for 00077 // the root node. 00078 // Graph edges can be pruned by specializing this function. 00079 template<class NodeType> 00080 bool insertEdge(NodeType *From, NodeType *To) { return Visited.insert(To); } 00081 00082 // Called after all children of BB have been visited. 00083 template<class NodeType> 00084 void finishPostorder(NodeType *BB) {} 00085 }; 00086 00087 template<class GraphT, 00088 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>, 00089 bool ExtStorage = false, 00090 class GT = GraphTraits<GraphT> > 00091 class po_iterator : public std::iterator<std::forward_iterator_tag, 00092 typename GT::NodeType, ptrdiff_t>, 00093 public po_iterator_storage<SetType, ExtStorage> { 00094 typedef std::iterator<std::forward_iterator_tag, 00095 typename GT::NodeType, ptrdiff_t> super; 00096 typedef typename GT::NodeType NodeType; 00097 typedef typename GT::ChildIteratorType ChildItTy; 00098 00099 // VisitStack - Used to maintain the ordering. Top = current block 00100 // First element is basic block pointer, second is the 'next child' to visit 00101 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack; 00102 00103 void traverseChild() { 00104 while (VisitStack.back().second != GT::child_end(VisitStack.back().first)) { 00105 NodeType *BB = *VisitStack.back().second++; 00106 if (this->insertEdge(VisitStack.back().first, BB)) { 00107 // If the block is not visited... 00108 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB))); 00109 } 00110 } 00111 } 00112 00113 inline po_iterator(NodeType *BB) { 00114 this->insertEdge((NodeType*)nullptr, BB); 00115 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB))); 00116 traverseChild(); 00117 } 00118 inline po_iterator() {} // End is when stack is empty. 00119 00120 inline po_iterator(NodeType *BB, SetType &S) : 00121 po_iterator_storage<SetType, ExtStorage>(S) { 00122 if (this->insertEdge((NodeType*)nullptr, BB)) { 00123 VisitStack.push_back(std::make_pair(BB, GT::child_begin(BB))); 00124 traverseChild(); 00125 } 00126 } 00127 00128 inline po_iterator(SetType &S) : 00129 po_iterator_storage<SetType, ExtStorage>(S) { 00130 } // End is when stack is empty. 00131 public: 00132 typedef typename super::pointer pointer; 00133 typedef po_iterator<GraphT, SetType, ExtStorage, GT> _Self; 00134 00135 // Provide static "constructors"... 00136 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); } 00137 static inline _Self end (GraphT G) { return _Self(); } 00138 00139 static inline _Self begin(GraphT G, SetType &S) { 00140 return _Self(GT::getEntryNode(G), S); 00141 } 00142 static inline _Self end (GraphT G, SetType &S) { return _Self(S); } 00143 00144 inline bool operator==(const _Self& x) const { 00145 return VisitStack == x.VisitStack; 00146 } 00147 inline bool operator!=(const _Self& x) const { return !operator==(x); } 00148 00149 inline pointer operator*() const { 00150 return VisitStack.back().first; 00151 } 00152 00153 // This is a nonstandard operator-> that dereferences the pointer an extra 00154 // time... so that you can actually call methods ON the BasicBlock, because 00155 // the contained type is a pointer. This allows BBIt->getTerminator() f.e. 00156 // 00157 inline NodeType *operator->() const { return operator*(); } 00158 00159 inline _Self& operator++() { // Preincrement 00160 this->finishPostorder(VisitStack.back().first); 00161 VisitStack.pop_back(); 00162 if (!VisitStack.empty()) 00163 traverseChild(); 00164 return *this; 00165 } 00166 00167 inline _Self operator++(int) { // Postincrement 00168 _Self tmp = *this; ++*this; return tmp; 00169 } 00170 }; 00171 00172 // Provide global constructors that automatically figure out correct types... 00173 // 00174 template <class T> 00175 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); } 00176 template <class T> 00177 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); } 00178 00179 // Provide global definitions of external postorder iterators... 00180 template<class T, class SetType=std::set<typename GraphTraits<T>::NodeType*> > 00181 struct po_ext_iterator : public po_iterator<T, SetType, true> { 00182 po_ext_iterator(const po_iterator<T, SetType, true> &V) : 00183 po_iterator<T, SetType, true>(V) {} 00184 }; 00185 00186 template<class T, class SetType> 00187 po_ext_iterator<T, SetType> po_ext_begin(T G, SetType &S) { 00188 return po_ext_iterator<T, SetType>::begin(G, S); 00189 } 00190 00191 template<class T, class SetType> 00192 po_ext_iterator<T, SetType> po_ext_end(T G, SetType &S) { 00193 return po_ext_iterator<T, SetType>::end(G, S); 00194 } 00195 00196 // Provide global definitions of inverse post order iterators... 00197 template <class T, 00198 class SetType = std::set<typename GraphTraits<T>::NodeType*>, 00199 bool External = false> 00200 struct ipo_iterator : public po_iterator<Inverse<T>, SetType, External > { 00201 ipo_iterator(const po_iterator<Inverse<T>, SetType, External> &V) : 00202 po_iterator<Inverse<T>, SetType, External> (V) {} 00203 }; 00204 00205 template <class T> 00206 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) { 00207 return ipo_iterator<T>::begin(G, Reverse); 00208 } 00209 00210 template <class T> 00211 ipo_iterator<T> ipo_end(T G){ 00212 return ipo_iterator<T>::end(G); 00213 } 00214 00215 // Provide global definitions of external inverse postorder iterators... 00216 template <class T, 00217 class SetType = std::set<typename GraphTraits<T>::NodeType*> > 00218 struct ipo_ext_iterator : public ipo_iterator<T, SetType, true> { 00219 ipo_ext_iterator(const ipo_iterator<T, SetType, true> &V) : 00220 ipo_iterator<T, SetType, true>(V) {} 00221 ipo_ext_iterator(const po_iterator<Inverse<T>, SetType, true> &V) : 00222 ipo_iterator<T, SetType, true>(V) {} 00223 }; 00224 00225 template <class T, class SetType> 00226 ipo_ext_iterator<T, SetType> ipo_ext_begin(T G, SetType &S) { 00227 return ipo_ext_iterator<T, SetType>::begin(G, S); 00228 } 00229 00230 template <class T, class SetType> 00231 ipo_ext_iterator<T, SetType> ipo_ext_end(T G, SetType &S) { 00232 return ipo_ext_iterator<T, SetType>::end(G, S); 00233 } 00234 00235 //===--------------------------------------------------------------------===// 00236 // Reverse Post Order CFG iterator code 00237 //===--------------------------------------------------------------------===// 00238 // 00239 // This is used to visit basic blocks in a method in reverse post order. This 00240 // class is awkward to use because I don't know a good incremental algorithm to 00241 // computer RPO from a graph. Because of this, the construction of the 00242 // ReversePostOrderTraversal object is expensive (it must walk the entire graph 00243 // with a postorder iterator to build the data structures). The moral of this 00244 // story is: Don't create more ReversePostOrderTraversal classes than necessary. 00245 // 00246 // This class should be used like this: 00247 // { 00248 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create 00249 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) { 00250 // ... 00251 // } 00252 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) { 00253 // ... 00254 // } 00255 // } 00256 // 00257 00258 template<class GraphT, class GT = GraphTraits<GraphT> > 00259 class ReversePostOrderTraversal { 00260 typedef typename GT::NodeType NodeType; 00261 std::vector<NodeType*> Blocks; // Block list in normal PO order 00262 inline void Initialize(NodeType *BB) { 00263 std::copy(po_begin(BB), po_end(BB), std::back_inserter(Blocks)); 00264 } 00265 public: 00266 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator; 00267 00268 inline ReversePostOrderTraversal(GraphT G) { 00269 Initialize(GT::getEntryNode(G)); 00270 } 00271 00272 // Because we want a reverse post order, use reverse iterators from the vector 00273 inline rpo_iterator begin() { return Blocks.rbegin(); } 00274 inline rpo_iterator end() { return Blocks.rend(); } 00275 }; 00276 00277 } // End llvm namespace 00278 00279 #endif