LLVM API Documentation
00001 //===- llvm/ADT/DepthFirstIterator.h - Depth First 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 generic depth 00011 // first graph iterator. This file exposes the following functions/types: 00012 // 00013 // df_begin/df_end/df_iterator 00014 // * Normal depth-first iteration - visit a node and then all of its children. 00015 // 00016 // idf_begin/idf_end/idf_iterator 00017 // * Depth-first iteration on the 'inverse' graph. 00018 // 00019 // df_ext_begin/df_ext_end/df_ext_iterator 00020 // * Normal depth-first iteration - visit a node and then all of its children. 00021 // This iterator stores the 'visited' set in an external set, which allows 00022 // it to be more efficient, and allows external clients to use the set for 00023 // other purposes. 00024 // 00025 // idf_ext_begin/idf_ext_end/idf_ext_iterator 00026 // * Depth-first iteration on the 'inverse' graph. 00027 // This iterator stores the 'visited' set in an external set, which allows 00028 // it to be more efficient, and allows external clients to use the set for 00029 // other purposes. 00030 // 00031 //===----------------------------------------------------------------------===// 00032 00033 #ifndef LLVM_ADT_DEPTHFIRSTITERATOR_H 00034 #define LLVM_ADT_DEPTHFIRSTITERATOR_H 00035 00036 #include "llvm/ADT/GraphTraits.h" 00037 #include "llvm/ADT/iterator.h" 00038 #include "llvm/ADT/SmallPtrSet.h" 00039 #include <vector> 00040 #include <set> 00041 00042 namespace llvm { 00043 00044 // df_iterator_storage - A private class which is used to figure out where to 00045 // store the visited set. 00046 template<class SetType, bool External> // Non-external set 00047 class df_iterator_storage { 00048 public: 00049 SetType Visited; 00050 }; 00051 00052 template<class SetType> 00053 class df_iterator_storage<SetType, true> { 00054 public: 00055 df_iterator_storage(SetType &VSet) : Visited(VSet) {} 00056 df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {} 00057 SetType &Visited; 00058 }; 00059 00060 00061 // Generic Depth First Iterator 00062 template<class GraphT, 00063 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>, 00064 bool ExtStorage = false, class GT = GraphTraits<GraphT> > 00065 class df_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t>, 00066 public df_iterator_storage<SetType, ExtStorage> { 00067 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super; 00068 00069 typedef typename GT::NodeType NodeType; 00070 typedef typename GT::ChildIteratorType ChildItTy; 00071 00072 // VisitStack - Used to maintain the ordering. Top = current block 00073 // First element is node pointer, second is the 'next child' to visit 00074 std::vector<std::pair<NodeType *, ChildItTy> > VisitStack; 00075 private: 00076 inline df_iterator(NodeType *Node) { 00077 this->Visited.insert(Node); 00078 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node))); 00079 } 00080 inline df_iterator() { /* End is when stack is empty */ } 00081 00082 inline df_iterator(NodeType *Node, SetType &S) 00083 : df_iterator_storage<SetType, ExtStorage>(S) { 00084 if (!S.count(Node)) { 00085 this->Visited.insert(Node); 00086 VisitStack.push_back(std::make_pair(Node, GT::child_begin(Node))); 00087 } 00088 } 00089 inline df_iterator(SetType &S) 00090 : df_iterator_storage<SetType, ExtStorage>(S) { 00091 // End is when stack is empty 00092 } 00093 00094 public: 00095 typedef typename super::pointer pointer; 00096 typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self; 00097 00098 // Provide static begin and end methods as our public "constructors" 00099 static inline _Self begin(const GraphT& G) { 00100 return _Self(GT::getEntryNode(G)); 00101 } 00102 static inline _Self end(const GraphT& G) { return _Self(); } 00103 00104 // Static begin and end methods as our public ctors for external iterators 00105 static inline _Self begin(const GraphT& G, SetType &S) { 00106 return _Self(GT::getEntryNode(G), S); 00107 } 00108 static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); } 00109 00110 inline bool operator==(const _Self& x) const { 00111 return VisitStack.size() == x.VisitStack.size() && 00112 VisitStack == x.VisitStack; 00113 } 00114 inline bool operator!=(const _Self& x) const { return !operator==(x); } 00115 00116 inline pointer operator*() const { 00117 return VisitStack.back().first; 00118 } 00119 00120 // This is a nonstandard operator-> that dereferences the pointer an extra 00121 // time... so that you can actually call methods ON the Node, because 00122 // the contained type is a pointer. This allows BBIt->getTerminator() f.e. 00123 // 00124 inline NodeType *operator->() const { return operator*(); } 00125 00126 inline _Self& operator++() { // Preincrement 00127 do { 00128 std::pair<NodeType *, ChildItTy> &Top = VisitStack.back(); 00129 NodeType *Node = Top.first; 00130 ChildItTy &It = Top.second; 00131 00132 while (It != GT::child_end(Node)) { 00133 NodeType *Next = *It++; 00134 if (!this->Visited.count(Next)) { // Has our next sibling been visited? 00135 // No, do it now. 00136 this->Visited.insert(Next); 00137 VisitStack.push_back(std::make_pair(Next, GT::child_begin(Next))); 00138 return *this; 00139 } 00140 } 00141 00142 // Oops, ran out of successors... go up a level on the stack. 00143 VisitStack.pop_back(); 00144 } while (!VisitStack.empty()); 00145 return *this; 00146 } 00147 00148 inline _Self operator++(int) { // Postincrement 00149 _Self tmp = *this; ++*this; return tmp; 00150 } 00151 00152 // nodeVisited - return true if this iterator has already visited the 00153 // specified node. This is public, and will probably be used to iterate over 00154 // nodes that a depth first iteration did not find: ie unreachable nodes. 00155 // 00156 inline bool nodeVisited(NodeType *Node) const { 00157 return this->Visited.count(Node) != 0; 00158 } 00159 }; 00160 00161 00162 // Provide global constructors that automatically figure out correct types... 00163 // 00164 template <class T> 00165 df_iterator<T> df_begin(const T& G) { 00166 return df_iterator<T>::begin(G); 00167 } 00168 00169 template <class T> 00170 df_iterator<T> df_end(const T& G) { 00171 return df_iterator<T>::end(G); 00172 } 00173 00174 // Provide global definitions of external depth first iterators... 00175 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> > 00176 struct df_ext_iterator : public df_iterator<T, SetTy, true> { 00177 df_ext_iterator(const df_iterator<T, SetTy, true> &V) 00178 : df_iterator<T, SetTy, true>(V) {} 00179 }; 00180 00181 template <class T, class SetTy> 00182 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) { 00183 return df_ext_iterator<T, SetTy>::begin(G, S); 00184 } 00185 00186 template <class T, class SetTy> 00187 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) { 00188 return df_ext_iterator<T, SetTy>::end(G, S); 00189 } 00190 00191 00192 // Provide global definitions of inverse depth first iterators... 00193 template <class T, 00194 class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>, 00195 bool External = false> 00196 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> { 00197 idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V) 00198 : df_iterator<Inverse<T>, SetTy, External>(V) {} 00199 }; 00200 00201 template <class T> 00202 idf_iterator<T> idf_begin(const T& G) { 00203 Inverse<T> DummyG; 00204 return idf_iterator<T>::begin(DummyG); 00205 } 00206 00207 template <class T> 00208 idf_iterator<T> idf_end(const T& G){ 00209 Inverse<T> DummyG; 00210 return idf_iterator<T>::end(DummyG); 00211 } 00212 00213 // Provide global definitions of external inverse depth first iterators... 00214 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> > 00215 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> { 00216 idf_ext_iterator(const idf_iterator<T, SetTy, true> &V) 00217 : idf_iterator<T, SetTy, true>(V) {} 00218 idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V) 00219 : idf_iterator<T, SetTy, true>(V) {} 00220 }; 00221 00222 template <class T, class SetTy> 00223 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) { 00224 Inverse<T> DummyG(G); 00225 return idf_ext_iterator<T, SetTy>::begin(DummyG, S); 00226 } 00227 00228 template <class T, class SetTy> 00229 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) { 00230 Inverse<T> DummyG(G); 00231 return idf_ext_iterator<T, SetTy>::end(DummyG, S); 00232 } 00233 00234 } // End llvm namespace 00235 00236 #endif