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/iterator_range.h"
00037 #include "llvm/ADT/GraphTraits.h"
00038 #include "llvm/ADT/PointerIntPair.h"
00039 #include "llvm/ADT/SmallPtrSet.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.count(Next)) {  
00117           // No, do it now.
00118           this->Visited.insert(Next);
00119           VisitStack.push_back(std::make_pair(PointerIntTy(Next, 0), 
00120                                               GT::child_begin(Next)));
00121           return;
00122         }
00123       }
00124 
00125       // Oops, ran out of successors... go up a level on the stack.
00126       VisitStack.pop_back();
00127     } while (!VisitStack.empty());
00128   }
00129 
00130 public:
00131   typedef typename super::pointer pointer;
00132   typedef df_iterator<GraphT, SetType, ExtStorage, GT> _Self;
00133 
00134   // Provide static begin and end methods as our public "constructors"
00135   static inline _Self begin(const GraphT& G) {
00136     return _Self(GT::getEntryNode(G));
00137   }
00138   static inline _Self end(const GraphT& G) { return _Self(); }
00139 
00140   // Static begin and end methods as our public ctors for external iterators
00141   static inline _Self begin(const GraphT& G, SetType &S) {
00142     return _Self(GT::getEntryNode(G), S);
00143   }
00144   static inline _Self end(const GraphT& G, SetType &S) { return _Self(S); }
00145 
00146   inline bool operator==(const _Self& x) const {
00147     return VisitStack == x.VisitStack;
00148   }
00149   inline bool operator!=(const _Self& x) const { return !operator==(x); }
00150 
00151   inline pointer operator*() const {
00152     return VisitStack.back().first.getPointer();
00153   }
00154 
00155   // This is a nonstandard operator-> that dereferences the pointer an extra
00156   // time... so that you can actually call methods ON the Node, because
00157   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
00158   //
00159   inline NodeType *operator->() const { return operator*(); }
00160 
00161   inline _Self& operator++() {   // Preincrement
00162     toNext();
00163     return *this;
00164   }
00165 
00166   // skips all children of the current node and traverses to next node
00167   //
00168   inline _Self& skipChildren() {  
00169     VisitStack.pop_back();
00170     if (!VisitStack.empty())
00171       toNext();
00172     return *this;
00173   }
00174 
00175   inline _Self operator++(int) { // Postincrement
00176     _Self tmp = *this; ++*this; 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   inline 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 iterator_range<df_iterator<T>>(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 iterator_range<df_ext_iterator<T, SetTy>>(df_ext_begin(G, S),
00238                                                    df_ext_end(G, S));
00239 }
00240 
00241 
00242 // Provide global definitions of inverse depth first iterators...
00243 template <class T,
00244   class SetTy = llvm::SmallPtrSet<typename GraphTraits<T>::NodeType*, 8>,
00245           bool External = false>
00246 struct idf_iterator : public df_iterator<Inverse<T>, SetTy, External> {
00247   idf_iterator(const df_iterator<Inverse<T>, SetTy, External> &V)
00248     : df_iterator<Inverse<T>, SetTy, External>(V) {}
00249 };
00250 
00251 template <class T>
00252 idf_iterator<T> idf_begin(const T& G) {
00253   return idf_iterator<T>::begin(Inverse<T>(G));
00254 }
00255 
00256 template <class T>
00257 idf_iterator<T> idf_end(const T& G){
00258   return idf_iterator<T>::end(Inverse<T>(G));
00259 }
00260 
00261 // Provide an accessor method to use them in range-based patterns.
00262 template <class T>
00263 iterator_range<idf_iterator<T>> inverse_depth_first(const T& G) {
00264   return iterator_range<idf_iterator<T>>(idf_begin(G), idf_end(G));
00265 }
00266 
00267 // Provide global definitions of external inverse depth first iterators...
00268 template <class T, class SetTy = std::set<typename GraphTraits<T>::NodeType*> >
00269 struct idf_ext_iterator : public idf_iterator<T, SetTy, true> {
00270   idf_ext_iterator(const idf_iterator<T, SetTy, true> &V)
00271     : idf_iterator<T, SetTy, true>(V) {}
00272   idf_ext_iterator(const df_iterator<Inverse<T>, SetTy, true> &V)
00273     : idf_iterator<T, SetTy, true>(V) {}
00274 };
00275 
00276 template <class T, class SetTy>
00277 idf_ext_iterator<T, SetTy> idf_ext_begin(const T& G, SetTy &S) {
00278   return idf_ext_iterator<T, SetTy>::begin(Inverse<T>(G), S);
00279 }
00280 
00281 template <class T, class SetTy>
00282 idf_ext_iterator<T, SetTy> idf_ext_end(const T& G, SetTy &S) {
00283   return idf_ext_iterator<T, SetTy>::end(Inverse<T>(G), S);
00284 }
00285 
00286 template <class T, class SetTy>
00287 iterator_range<idf_ext_iterator<T, SetTy>> inverse_depth_first_ext(const T& G,
00288                                                                    SetTy &S) {
00289   return iterator_range<idf_ext_iterator<T, SetTy>>(idf_ext_begin(G, S),
00290                                                     idf_ext_end(G, S));
00291 }
00292 
00293 } // End llvm namespace
00294 
00295 #endif