LLVM API Documentation

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   typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
00132 
00133   // Provide static begin and end methods as our public "constructors"
00134   static inline _Self begin(const GraphT& G) {
00135     return _Self(GT::getEntryNode(G));
00136   }
00137   static inline _Self end(const GraphT& G) { return _Self(); }
00138 
00139   // Static begin and end methods as our public ctors for external iterators
00140   static inline _Self begin(const GraphT& G, SetType &S) {
00141     return _Self(GT::getEntryNode(G), S);
00142   }
00143   static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
00144 
00145   inline bool operator==(const _Self& x) const {
00146     return VisitStack == x.VisitStack;
00147   }
00148   inline bool operator!=(const _Self& x) const { return !operator==(x); }
00149 
00150   inline pointer operator*() const {
00151     return VisitStack.back().first.getPointer();
00152   }
00153 
00154   // This is a nonstandard operator-> that dereferences the pointer an extra
00155   // time... so that you can actually call methods ON the Node, because
00156   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
00157   //
00158   inline NodeType *operator->() const { return operator*(); }
00159 
00160   inline _Self& operator++() {   // Preincrement
00161     toNext();
00162     return *this;
00163   }
00164 
00165   // skips all children of the current node and traverses to next node
00166   //
00167   inline _Self& skipChildren() {  
00168     VisitStack.pop_back();
00169     if (!VisitStack.empty())
00170       toNext();
00171     return *this;
00172   }
00173 
00174   inline _Self operator++(int) { // Postincrement
00175     _Self tmp = *this; ++*this; return tmp;
00176   }
00177 
00178   // nodeVisited - return true if this iterator has already visited the
00179   // specified node.  This is public, and will probably be used to iterate over
00180   // nodes that a depth first iteration did not find: ie unreachable nodes.
00181   //
00182   inline bool nodeVisited(NodeType *Node) const {
00183     return this->Visited.count(Node) != 0;
00184   }
00185 
00186   /// getPathLength - Return the length of the path from the entry node to the
00187   /// current node, counting both nodes.
00188   unsigned getPathLength() const { return VisitStack.size(); }
00189 
00190   /// getPath - Return the n'th node in the path from the entry node to the
00191   /// current node.
00192   NodeType *getPath(unsigned n) const {
00193     return VisitStack[n].first.getPointer();
00194   }
00195 };
00196 
00197 
00198 // Provide global constructors that automatically figure out correct types...
00199 //
00200 template <class T>
00201 df_iterator<T> df_begin(const T& G) {
00202   return df_iterator<T>::begin(G);
00203 }
00204 
00205 template <class T>
00206 df_iterator<T> df_end(const T& G) {
00207   return df_iterator<T>::end(G);
00208 }
00209 
00210 // Provide an accessor method to use them in range-based patterns.
00211 template <class T>
00212 iterator_range<df_iterator<T>> depth_first(const T& G) {
00213   return iterator_range<df_iterator<T>>(df_begin(G), df_end(G));
00214 }
00215 
00216 // Provide global definitions of external depth first iterators...
00217 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00218 struct df_ext_iterator : public df_iterator<T, SetTy, true> {
00219   df_ext_iterator(const df_iterator<T, SetTy, true> &V)
00220     : df_iterator<T, SetTy, true>(V) {}
00221 };
00222 
00223 template <class T, class SetTy>
00224 df_ext_iterator<T, SetTy> df_ext_begin(const T& G, SetTy &S) {
00225   return df_ext_iterator<T, SetTy>::begin(G, S);
00226 }
00227 
00228 template <class T, class SetTy>
00229 df_ext_iterator<T, SetTy> df_ext_end(const T& G, SetTy &S) {
00230   return df_ext_iterator<T, SetTy>::end(G, S);
00231 }
00232 
00233 template <class T, class SetTy>
00234 iterator_range<df_ext_iterator<T, SetTy>> depth_first_ext(const T& G,
00235                                                           SetTy &S) {
00236   return iterator_range<df_ext_iterator<T, SetTy>>(df_ext_begin(G, S),
00237                                                    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 iterator_range<idf_iterator<T>>(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 iterator_range<idf_ext_iterator<T, SetTy>>(idf_ext_begin(G, S),
00289                                                     idf_ext_end(G, S));
00290 }
00291 
00292 } // End llvm namespace
00293 
00294 #endif