1 //===- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*-===//
3 // This file builds on the Support/GraphTraits.h file to build a generic graph
4 // post order iterator. This should work over any graph type that has a
5 // GraphTraits specialization.
7 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_POSTORDERITERATOR_H
10 #define SUPPORT_POSTORDERITERATOR_H
12 #include "Support/GraphTraits.h"
13 #include "Support/iterator"
17 template<class GraphT, class GT = GraphTraits<GraphT> >
18 class po_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
19 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
20 typedef typename GT::NodeType NodeType;
21 typedef typename GT::ChildIteratorType ChildItTy;
23 std::set<NodeType *> Visited; // All of the blocks visited so far...
24 // VisitStack - Used to maintain the ordering. Top = current block
25 // First element is basic block pointer, second is the 'next child' to visit
26 std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
28 void traverseChild() {
29 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
30 NodeType *BB = *VisitStack.top().second++;
31 if (!Visited.count(BB)) { // If the block is not visited...
33 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
38 inline po_iterator(NodeType *BB) {
40 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
43 inline po_iterator() { /* End is when stack is empty */ }
45 typedef typename super::pointer pointer;
46 typedef po_iterator<GraphT, GT> _Self;
48 // Provide static "constructors"...
49 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
50 static inline _Self end (GraphT G) { return _Self(); }
52 inline bool operator==(const _Self& x) const {
53 return VisitStack == x.VisitStack;
55 inline bool operator!=(const _Self& x) const { return !operator==(x); }
57 inline pointer operator*() const {
58 return VisitStack.top().first;
61 // This is a nonstandard operator-> that dereferences the pointer an extra
62 // time... so that you can actually call methods ON the BasicBlock, because
63 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
65 inline NodeType *operator->() const { return operator*(); }
67 inline _Self& operator++() { // Preincrement
69 if (!VisitStack.empty())
74 inline _Self operator++(int) { // Postincrement
75 _Self tmp = *this; ++*this; return tmp;
79 // Provide global constructors that automatically figure out correct types...
82 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
84 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
86 // Provide global definitions of inverse post order iterators...
88 struct ipo_iterator : public po_iterator<Inverse<T> > {
89 ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
93 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
94 return ipo_iterator<T>::begin(G, Reverse);
98 ipo_iterator<T> ipo_end(T G){
99 return ipo_iterator<T>::end(G);
103 //===--------------------------------------------------------------------===//
104 // Reverse Post Order CFG iterator code
105 //===--------------------------------------------------------------------===//
107 // This is used to visit basic blocks in a method in reverse post order. This
108 // class is awkward to use because I don't know a good incremental algorithm to
109 // computer RPO from a graph. Because of this, the construction of the
110 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
111 // with a postorder iterator to build the data structures). The moral of this
112 // story is: Don't create more ReversePostOrderTraversal classes than necessary.
114 // This class should be used like this:
116 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
117 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
120 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
126 template<class GraphT, class GT = GraphTraits<GraphT> >
127 class ReversePostOrderTraversal {
128 typedef typename GT::NodeType NodeType;
129 std::vector<NodeType*> Blocks; // Block list in normal PO order
130 inline void Initialize(NodeType *BB) {
131 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
134 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
136 inline ReversePostOrderTraversal(GraphT G) {
137 Initialize(GT::getEntryNode(G));
140 // Because we want a reverse post order, use reverse iterators from the vector
141 inline rpo_iterator begin() { return Blocks.rbegin(); }
142 inline rpo_iterator end() { return Blocks.rend(); }