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