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   /// \brief Skips all children of the current node and traverses to next node
00163   ///
00164   /// Note: This function takes care of incrementing the iterator. If you
00165   /// always increment and call this function, you risk walking off the end.
00166   df_iterator &skipChildren() {
00167     VisitStack.pop_back();
00168     if (!VisitStack.empty())
00169       toNext();
00170     return *this;
00171   }
00172 
00173   df_iterator operator++(int) { // Postincrement
00174     df_iterator tmp = *this;
00175     ++*this;
00176     return tmp;
00177   }
00178 
00179   // nodeVisited - return true if this iterator has already visited the
00180   // specified node.  This is public, and will probably be used to iterate over
00181   // nodes that a depth first iteration did not find: ie unreachable nodes.
00182   //
00183   bool nodeVisited(NodeType *Node) const {
00184     return this->Visited.count(Node) != 0;
00185   }
00186 
00187   /// getPathLength - Return the length of the path from the entry node to the
00188   /// current node, counting both nodes.
00189   unsigned getPathLength() const { return VisitStack.size(); }
00190 
00191   /// getPath - Return the n'th node in the path from the entry node to the
00192   /// current node.
00193   NodeType *getPath(unsigned n) const {
00194     return VisitStack[n].first.getPointer();
00195   }
00196 };
00197 
00198 
00199 // Provide global constructors that automatically figure out correct types...
00200 //
00201 template <class T>
00202 df_iterator<T> df_begin(const T& G) {
00203   return df_iterator<T>::begin(G);
00204 }
00205 
00206 template <class T>
00207 df_iterator<T> df_end(const T& G) {
00208   return df_iterator<T>::end(G);
00209 }
00210 
00211 // Provide an accessor method to use them in range-based patterns.
00212 template <class T>
00213 iterator_range<df_iterator<T>> depth_first(const T& G) {
00214   return make_range(df_begin(G), df_end(G));
00215 }
00216 
00217 // Provide global definitions of external depth first iterators...
00218 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00219 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
00220   df_ext_iterator(const df_iterator<T, SetTy, true> &V)
00221     : df_iterator<T, SetTy, true>(V) {}
00222 };
00223 
00224 template <class T, class SetTy>
00225 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
00226   return df_ext_iterator<T, SetTy>::begin(G, S);
00227 }
00228 
00229 template <class T, class SetTy>
00230 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
00231   return df_ext_iterator<T, SetTy>::end(G, S);
00232 }
00233 
00234 template <class T, class SetTy>
00235 iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
00236                                                           SetTy &S) {
00237   return make_range(df_ext_begin(G, S), df_ext_end(G, S));
00238 }
00239 
00240 
00241 // Provide global definitions of inverse depth first iterators...
00242 template <class T,
00243   class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
00244           bool External = false>
00245 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
00246   idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
00247     : df_iterator<Inverse<T>, SetTy, External>(V) {}
00248 };
00249 
00250 template <class T>
00251 idf_iterator<T> idf_begin(const T& G) {
00252   return idf_iterator<T>::begin(Inverse<T>(G));
00253 }
00254 
00255 template <class T>
00256 idf_iterator<T> idf_end(const T& G){
00257   return idf_iterator<T>::end(Inverse<T>(G));
00258 }
00259 
00260 // Provide an accessor method to use them in range-based patterns.
00261 template <class T>
00262 iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
00263   return make_range(idf_begin(G), idf_end(G));
00264 }
00265 
00266 // Provide global definitions of external inverse depth first iterators...
00267 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00268 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
00269   idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
00270     : idf_iterator<T, SetTy, true>(V) {}
00271   idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
00272     : idf_iterator<T, SetTy, true>(V) {}
00273 };
00274 
00275 template <class T, class SetTy>
00276 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
00277   return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
00278 }
00279 
00280 template <class T, class SetTy>
00281 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
00282   return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
00283 }
00284 
00285 template <class T, class SetTy>
00286 iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
00287                                                                    SetTy &S) {
00288   return make_range(idf_ext_begin(G, S), idf_ext_end(G, S));
00289 }
00290 
00291 } // End llvm namespace
00292 
00293 #endif