LLVM  mainline
DepthFirstIterator.h
Go to the documentation of this file.
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/PointerIntPair.h"
00038 #include "llvm/ADT/SmallPtrSet.h"
00039 #include "llvm/ADT/iterator_range.h"
00040 #include <set>
00041 #include <vector>
00042 
00043 namespace llvm {
00044 
00045 // df_iterator_storage - A private class which is used to figure out where to
00046 // store the visited set.
00047 template<class SetType, bool External>   // Non-external set
00048 class df_iterator_storage {
00049 public:
00050   SetType Visited;
00051 };
00052 
00053 template<class SetType>
00054 class df_iterator_storage<SetType, true> {
00055 public:
00056   df_iterator_storage(SetType &VSet) : Visited(VSet) {}
00057   df_iterator_storage(const df_iterator_storage &S) : Visited(S.Visited) {}
00058   SetType &Visited;
00059 };
00060 
00061 
00062 // Generic Depth First Iterator
00063 template<class GraphT,
00064 class SetType = llvm::SmallPtrSet<typename GraphTraits<GraphT>::NodeType*, 8>,
00065          bool ExtStorage = false, class GT = GraphTraits<GraphT> >
00066 class df_iterator : public std::iterator<std::forward_iterator_tag,
00067                                          typename GT::NodeType, ptrdiff_t>,
00068                     public df_iterator_storage<SetType, ExtStorage> {
00069   typedef std::iterator<std::forward_iterator_tag,
00070                         typename GT::NodeType, ptrdiff_t> super;
00071 
00072   typedef typename GT::NodeType          NodeType;
00073   typedef typename GT::ChildIteratorType ChildItTy;
00074   typedef PointerIntPair<NodeType*, 1>   PointerIntTy;
00075 
00076   // VisitStack - Used to maintain the ordering.  Top = current block
00077   // First element is node pointer, second is the 'next child' to visit
00078   // if the int in PointerIntTy is 0, the 'next child' to visit is invalid
00079   std::vector<std::pair<PointerIntTy, ChildItTy> > VisitStack;
00080 private:
00081   inline df_iterator(NodeType *Node) {
00082     this->Visited.insert(Node);
00083     VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
00084                                         GT::child_begin(Node)));
00085   }
00086   inline df_iterator() { 
00087     // End is when stack is empty 
00088   }
00089   inline df_iterator(NodeType *Node, SetType &S)
00090     : df_iterator_storage<SetType, ExtStorage>(S) {
00091     if (!S.count(Node)) {
00092       VisitStack.push_back(std::make_pair(PointerIntTy(Node, 0), 
00093                                           GT::child_begin(Node)));
00094       this->Visited.insert(Node);
00095     }
00096   }
00097   inline df_iterator(SetType &S)
00098     : df_iterator_storage<SetType, ExtStorage>(S) {
00099     // End is when stack is empty
00100   }
00101 
00102   inline void toNext() {
00103     do {
00104       std::pair<PointerIntTy, ChildItTy> &Top = VisitStack.back();
00105       NodeType *Node = Top.first.getPointer();
00106       ChildItTy &It  = Top.second;
00107       if (!Top.first.getInt()) {
00108         // now retrieve the real begin of the children before we dive in
00109         It = GT::child_begin(Node);
00110         Top.first.setInt(1);
00111       }
00112 
00113       while (It != GT::child_end(Node)) {
00114         NodeType *Next = *It++;
00115         // Has our next sibling been visited?
00116         if (Next && this->Visited.insert(Next).second) {
00117           // No, do it now.
00118           VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0), 
00119                                               GT::child_begin(Next)));
00120           return;
00121         }
00122       }
00123 
00124       // Oops, ran out of successors... go up a level on the stack.
00125       VisitStack.pop_back();
00126     } while (!VisitStack.empty());
00127   }
00128 
00129 public:
00130   typedef typename super::pointer pointer;
00131 
00132   // Provide static begin and end methods as our public "constructors"
00133   static df_iterator begin(const GraphT &G) {
00134     return df_iterator(GT::getEntryNode(G));
00135   }
00136   static df_iterator end(const GraphT &G) { return df_iterator(); }
00137 
00138   // Static begin and end methods as our public ctors for external iterators
00139   static df_iterator begin(const GraphT &G, SetType &S) {
00140     return df_iterator(GT::getEntryNode(G), S);
00141   }
00142   static df_iterator end(const GraphT &G, SetType &S) { return df_iterator(S); }
00143 
00144   bool operator==(const df_iterator &x) const {
00145     return VisitStack == x.VisitStack;
00146   }
00147   bool operator!=(const df_iterator &x) const { return !(*this == x); }
00148 
00149   pointer operator*() const { return VisitStack.back().first.getPointer(); }
00150 
00151   // This is a nonstandard operator-> that dereferences the pointer an extra
00152   // time... so that you can actually call methods ON the Node, because
00153   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
00154   //
00155   NodeType *operator->() const { return **this; }
00156 
00157   df_iterator &operator++() { // Preincrement
00158     toNext();
00159     return *this;
00160   }
00161 
00162   // skips all children of the current node and traverses to next node
00163   //
00164   df_iterator &skipChildren() {
00165     VisitStack.pop_back();
00166     if (!VisitStack.empty())
00167       toNext();
00168     return *this;
00169   }
00170 
00171   df_iterator operator++(int) { // Postincrement
00172     df_iterator tmp = *this;
00173     ++*this;
00174     return tmp;
00175   }
00176 
00177   // nodeVisited - return true if this iterator has already visited the
00178   // specified node.  This is public, and will probably be used to iterate over
00179   // nodes that a depth first iteration did not find: ie unreachable nodes.
00180   //
00181   bool nodeVisited(NodeType *Node) const {
00182     return this->Visited.count(Node) != 0;
00183   }
00184 
00185   /// getPathLength - Return the length of the path from the entry node to the
00186   /// current node, counting both nodes.
00187   unsigned getPathLength() const { return VisitStack.size(); }
00188 
00189   /// getPath - Return the n'th node in the path from the entry node to the
00190   /// current node.
00191   NodeType *getPath(unsigned n) const {
00192     return VisitStack[n].first.getPointer();
00193   }
00194 };
00195 
00196 
00197 // Provide global constructors that automatically figure out correct types...
00198 //
00199 template <class T>
00200 df_iterator<T> df_begin(const T& G) {
00201   return df_iterator<T>::begin(G);
00202 }
00203 
00204 template <class T>
00205 df_iterator<T> df_end(const T& G) {
00206   return df_iterator<T>::end(G);
00207 }
00208 
00209 // Provide an accessor method to use them in range-based patterns.
00210 template <class T>
00211 iterator_range<df_iterator<T>> depth_first(const T& G) {
00212   return make_range(df_begin(G), df_end(G));
00213 }
00214 
00215 // Provide global definitions of external depth first iterators...
00216 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00217 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
00218   df_ext_iterator(const df_iterator<T, SetTy, true> &V)
00219     : df_iterator<T, SetTy, true>(V) {}
00220 };
00221 
00222 template <class T, class SetTy>
00223 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
00224   return df_ext_iterator<T, SetTy>::begin(G, S);
00225 }
00226 
00227 template <class T, class SetTy>
00228 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
00229   return df_ext_iterator<T, SetTy>::end(G, S);
00230 }
00231 
00232 template <class T, class SetTy>
00233 iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
00234                                                           SetTy &S) {
00235   return make_range(df_ext_begin(G, S), df_ext_end(G, S));
00236 }
00237 
00238 
00239 // Provide global definitions of inverse depth first iterators...
00240 template <class T,
00241   class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
00242           bool External = false>
00243 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
00244   idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
00245     : df_iterator<Inverse<T>, SetTy, External>(V) {}
00246 };
00247 
00248 template <class T>
00249 idf_iterator<T> idf_begin(const T& G) {
00250   return idf_iterator<T>::begin(Inverse<T>(G));
00251 }
00252 
00253 template <class T>
00254 idf_iterator<T> idf_end(const T& G){
00255   return idf_iterator<T>::end(Inverse<T>(G));
00256 }
00257 
00258 // Provide an accessor method to use them in range-based patterns.
00259 template <class T>
00260 iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
00261   return make_range(idf_begin(G), idf_end(G));
00262 }
00263 
00264 // Provide global definitions of external inverse depth first iterators...
00265 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00266 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
00267   idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
00268     : idf_iterator<T, SetTy, true>(V) {}
00269   idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
00270     : idf_iterator<T, SetTy, true>(V) {}
00271 };
00272 
00273 template <class T, class SetTy>
00274 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
00275   return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
00276 }
00277 
00278 template <class T, class SetTy>
00279 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
00280   return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
00281 }
00282 
00283 template <class T, class SetTy>
00284 iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
00285                                                                    SetTy &S) {
00286   return make_range(idf_ext_begin(G, S), idf_ext_end(G, S));
00287 }
00288 
00289 } // End llvm namespace
00290 
00291 #endif