1 //===- llvm/ADT/PostOrderIterator.h - 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 ADT/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 LLVM_ADT_POSTORDERITERATOR_H
17 #define LLVM_ADT_POSTORDERITERATOR_H
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/iterator"
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;
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;
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...
42 VisitStack.push(std::make_pair(BB, GT::child_begin(BB)));
47 inline po_iterator(NodeType *BB) {
49 VisitStack.push(std::make_pair(BB, GT::child_begin(BB)));
52 inline po_iterator() { /* End is when stack is empty */ }
54 typedef typename super::pointer pointer;
55 typedef po_iterator<GraphT, GT> _Self;
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(); }
61 inline bool operator==(const _Self& x) const {
62 return VisitStack == x.VisitStack;
64 inline bool operator!=(const _Self& x) const { return !operator==(x); }
66 inline pointer operator*() const {
67 return VisitStack.top().first;
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.
74 inline NodeType *operator->() const { return operator*(); }
76 inline _Self& operator++() { // Preincrement
78 if (!VisitStack.empty())
83 inline _Self operator++(int) { // Postincrement
84 _Self tmp = *this; ++*this; return tmp;
88 // Provide global constructors that automatically figure out correct types...
91 po_iterator<T> po_begin(T G) { return po_iterator<T>::begin(G); }
93 po_iterator<T> po_end (T G) { return po_iterator<T>::end(G); }
95 // Provide global definitions of inverse post order iterators...
97 struct ipo_iterator : public po_iterator<Inverse<T> > {
98 ipo_iterator(const po_iterator<Inverse<T> > &V) :po_iterator<Inverse<T> >(V){}
102 ipo_iterator<T> ipo_begin(T G, bool Reverse = false) {
103 return ipo_iterator<T>::begin(G, Reverse);
107 ipo_iterator<T> ipo_end(T G){
108 return ipo_iterator<T>::end(G);
112 //===--------------------------------------------------------------------===//
113 // Reverse Post Order CFG iterator code
114 //===--------------------------------------------------------------------===//
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.
123 // This class should be used like this:
125 // ReversePostOrderTraversal<Function*> RPOT(FuncPtr); // Expensive to create
126 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
129 // for (rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
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));
143 typedef typename std::vector<NodeType*>::reverse_iterator rpo_iterator;
145 inline ReversePostOrderTraversal(GraphT G) {
146 Initialize(GT::getEntryNode(G));
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(); }
154 } // End llvm namespace