1 //===- Support/PostOrderIterator.h - Generic PostOrder iterator -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef SUPPORT_POSTORDERITERATOR_H
17 #define SUPPORT_POSTORDERITERATOR_H
19 #include "Support/GraphTraits.h"
20 #include "Support/iterator"
24 template<class GraphT, class GT = GraphTraits<GraphT> >
25 class po_iterator : public forward_iterator<typename GT::NodeType, ptrdiff_t> {
26 typedef forward_iterator<typename GT::NodeType, ptrdiff_t> super;
27 typedef typename GT::NodeType NodeType;
28 typedef typename GT::ChildIteratorType ChildItTy;
30 std::set<NodeType *> Visited; // All of the blocks visited so far...
31 // VisitStack - Used to maintain the ordering. Top = current block
32 // First element is basic block pointer, second is the 'next child' to visit
33 std::stack<std::pair<NodeType *, ChildItTy> > VisitStack;
35 void traverseChild() {
36 while (VisitStack.top().second != GT::child_end(VisitStack.top().first)) {
37 NodeType *BB = *VisitStack.top().second++;
38 if (!Visited.count(BB)) { // If the block is not visited...
40 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
45 inline po_iterator(NodeType *BB) {
47 VisitStack.push(make_pair(BB, GT::child_begin(BB)));
50 inline po_iterator() { /* End is when stack is empty */ }
52 typedef typename super::pointer pointer;
53 typedef po_iterator<GraphT, GT> _Self;
55 // Provide static "constructors"...
56 static inline _Self begin(GraphT G) { return _Self(GT::getEntryNode(G)); }
57 static inline _Self end (GraphT G) { return _Self(); }
59 inline bool operator==(const _Self& x) const {
60 return VisitStack == x.VisitStack;
62 inline bool operator!=(const _Self& x) const { return !operator==(x); }
64 inline pointer operator*() const {
65 return VisitStack.top().first;
68 // This is a nonstandard operator-> that dereferences the pointer an extra
69 // time... so that you can actually call methods ON the BasicBlock, because
70 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
72 inline NodeType *operator->() const { return operator*(); }
74 inline _Self& operator++() { // Preincrement
76 if (!VisitStack.empty())
81 inline _Self operator++(int) { // Postincrement
82 _Self tmp = *this; ++*this; return tmp;
86 // Provide global constructors that automatically figure out correct types...
89 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
91 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
93 // Provide global definitions of inverse post order iterators...
95 struct ipo_iterator : public po_iterator<Inverse<T> > {
96 ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
100 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
101 return ipo_iterator<T>::begin(G, Reverse);
105 ipo_iterator<T> ipo_end(T G){
106 return ipo_iterator<T>::end(G);
110 //===--------------------------------------------------------------------===//
111 // Reverse Post Order CFG iterator code
112 //===--------------------------------------------------------------------===//
114 // This is used to visit basic blocks in a method in reverse post order. This
115 // class is awkward to use because I don't know a good incremental algorithm to
116 // computer RPO from a graph. Because of this, the construction of the
117 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
118 // with a postorder iterator to build the data structures). The moral of this
119 // story is: Don't create more ReversePostOrderTraversal classes than necessary.
121 // This class should be used like this:
123 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
124 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
127 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
133 template<class GraphT, class GT = GraphTraits<GraphT> >
134 class ReversePostOrderTraversal {
135 typedef typename GT::NodeType NodeType;
136 std::vector<NodeType*> Blocks; // Block list in normal PO order
137 inline void Initialize(NodeType *BB) {
138 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
141 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
143 inline ReversePostOrderTraversal(GraphT G) {
144 Initialize(GT::getEntryNode(G));
147 // Because we want a reverse post order, use reverse iterators from the vector
148 inline rpo_iterator begin() { return Blocks.rbegin(); }
149 inline rpo_iterator end() { return Blocks.rend(); }