Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / include / Support / PostOrderIterator.h
1 //===- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file builds on the Support/GraphTraits.h file to build a generic graph
11 // post order iterator.  This should work over any graph type that has a
12 // GraphTraits specialization.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_POSTORDERITERATOR_H
17 #define SUPPORT_POSTORDERITERATOR_H
18
19 #include "Support/GraphTraits.h"
20 #include "Support/iterator"
21 #include <stack>
22 #include <set>
23
24 namespace llvm {
25
26 template<class GraphT, class GT = GraphTraits<GraphT> >
27 class po_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
28   typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
29   typedef typename GT::NodeType          NodeType;
30   typedef typename GT::ChildIteratorType ChildItTy;
31
32   std::set<NodeType *> Visited;    // All of the blocks visited so far...
33   // VisitStack - Used to maintain the ordering.  Top = current block
34   // First element is basic block pointer, second is the 'next child' to visit
35   std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
36
37   void traverseChild() {
38     while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
39       NodeType *BB = *VisitStack.top().second++;
40       if (!Visited.count(BB)) {  // If the block is not visited...
41         Visited.insert(BB);
42         VisitStack.push(make_pair(BB, GT::child_begin(BB)));
43       }
44     }
45   }
46
47   inline po_iterator(NodeType *BB) {
48     Visited.insert(BB);
49     VisitStack.push(make_pair(BB, GT::child_begin(BB)));
50     traverseChild();
51   }
52   inline po_iterator() { /* End is when stack is empty */ }
53 public:
54   typedef typename super::pointer pointer;
55   typedef po_iterator<GraphT, GT> _Self;
56
57   // Provide static "constructors"...
58   static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
59   static inline _Self end  (GraphT G) { return _Self(); }
60
61   inline bool operator==(const _Self& x) const { 
62     return VisitStack == x.VisitStack;
63   }
64   inline bool operator!=(const _Self& x) const { return !operator==(x); }
65
66   inline pointer operator*() const { 
67     return VisitStack.top().first;
68   }
69
70   // This is a nonstandard operator-> that dereferences the pointer an extra
71   // time... so that you can actually call methods ON the BasicBlock, because
72   // the contained type is a pointer.  This allows BBIt->getTerminator() f.e.
73   //
74   inline NodeType *operator->() const { return operator*(); }
75
76   inline _Self& operator++() {   // Preincrement
77     VisitStack.pop();
78     if (!VisitStack.empty())
79       traverseChild();
80     return *this; 
81   }
82
83   inline _Self operator++(int) { // Postincrement
84     _Self tmp = *this; ++*this; return tmp; 
85   }
86 };
87
88 // Provide global constructors that automatically figure out correct types...
89 //
90 template <class T>
91 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
92 template <class T>
93 po_iterator<T> po_end  (T G) { return po_iterator<T>::end(G); }
94
95 // Provide global definitions of inverse post order iterators...
96 template <class T>
97 struct ipo_iterator : public po_iterator<Inverse<T> > {
98   ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
99 };
100
101 template <class T>
102 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
103   return ipo_iterator<T>::begin(G, Reverse);
104 }
105
106 template <class T>
107 ipo_iterator<T> ipo_end(T G){
108   return ipo_iterator<T>::end(G);
109 }
110
111
112 //===--------------------------------------------------------------------===//
113 // Reverse Post Order CFG iterator code
114 //===--------------------------------------------------------------------===//
115 // 
116 // This is used to visit basic blocks in a method in reverse post order.  This
117 // class is awkward to use because I don't know a good incremental algorithm to
118 // computer RPO from a graph.  Because of this, the construction of the 
119 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
120 // with a postorder iterator to build the data structures).  The moral of this
121 // story is: Don't create more ReversePostOrderTraversal classes than necessary.
122 //
123 // This class should be used like this:
124 // {
125 //   ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
126 //   for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
127 //      ...
128 //   }
129 //   for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
130 //      ...
131 //   }
132 // }
133 //
134
135 template<class GraphT, class GT = GraphTraits<GraphT> >
136 class ReversePostOrderTraversal {
137   typedef typename GT::NodeType NodeType;
138   std::vector<NodeType*> Blocks;       // Block list in normal PO order
139   inline void Initialize(NodeType *BB) {
140     copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
141   }
142 public:
143   typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
144
145   inline ReversePostOrderTraversal(GraphT G) {
146     Initialize(GT::getEntryNode(G));
147   }
148
149   // Because we want a reverse post order, use reverse iterators from the vector
150   inline rpo_iterator begin() { return Blocks.rbegin(); }
151   inline rpo_iterator end()   { return Blocks.rend(); }
152 };
153
154 } // End llvm namespace
155
156 #endif