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 LLVM_SUPPORT_POSTORDER_ITERATOR_H
10 #define LLVM_SUPPORT_POSTORDER_ITERATOR_H
12 #include "Support/GraphTraits.h"
17 template<class GraphT, class GT = GraphTraits<GraphT> >
18 class po_iterator : public std::forward_iterator<typename GT::NodeType,
20 typedef typename GT::NodeType NodeType;
21 typedef typename GT::ChildIteratorType ChildItTy;
23 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 stack<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 po_iterator<GraphT, GT> _Self;
47 // Provide static "constructors"...
48 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
49 static inline _Self end (GraphT G) { return _Self(); }
51 inline bool operator==(const _Self& x) const {
52 return VisitStack == x.VisitStack;
54 inline bool operator!=(const _Self& x) const { return !operator==(x); }
56 inline pointer operator*() const {
57 return VisitStack.top().first;
60 // This is a nonstandard operator-> that dereferences the pointer an extra
61 // time... so that you can actually call methods ON the BasicBlock, because
62 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
64 inline NodeType *operator->() const { return operator*(); }
66 inline _Self& operator++() { // Preincrement
68 if (!VisitStack.empty())
73 inline _Self operator++(int) { // Postincrement
74 _Self tmp = *this; ++*this; return tmp;
78 // Provide global constructors that automatically figure out correct types...
81 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
83 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
85 // Provide global definitions of inverse post order iterators...
87 struct ipo_iterator : public po_iterator<Inverse<T> > {
88 ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
92 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
93 return ipo_iterator<T>::begin(G, Reverse);
97 ipo_iterator<T> ipo_end(T G){
98 return ipo_iterator<T>::end(G);
102 //===--------------------------------------------------------------------===//
103 // Reverse Post Order CFG iterator code
104 //===--------------------------------------------------------------------===//
106 // This is used to visit basic blocks in a method in reverse post order. This
107 // class is awkward to use because I don't know a good incremental algorithm to
108 // computer RPO from a graph. Because of this, the construction of the
109 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
110 // with a postorder iterator to build the data structures). The moral of this
111 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
113 // This class should be used like this:
115 // cfg::ReversePostOrderTraversal RPOT(MethodPtr); // Expensive to create
116 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
119 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
125 typedef reverse_iterator<vector<BasicBlock*>::iterator> rpo_iterator;
126 // TODO: FIXME: ReversePostOrderTraversal is not generic!
127 class ReversePostOrderTraversal {
128 vector<BasicBlock*> Blocks; // Block list in normal PO order
129 inline void Initialize(BasicBlock *BB) {
130 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
133 inline ReversePostOrderTraversal(Method *M) {
134 Initialize(M->front());
136 inline ReversePostOrderTraversal(BasicBlock *BB) {
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(); }