1 //===-- llvm/CFG.h - CFG definitions and useful classes ----------*- C++ -*--=//
3 // This file contains the class definitions useful for operating on the control
6 // Currently it contains functionality for these three applications:
8 // 1. Iterate over the predecessors of a basic block:
9 // pred_iterator, pred_const_iterator, pred_begin, pred_end
10 // 2. Iterate over the successors of a basic block:
11 // succ_iterator, succ_const_iterator, succ_begin, succ_end
12 // 3. Iterate over the basic blocks of a method in depth first ordering or
13 // reverse depth first order. df_iterator, df_const_iterator,
14 // df_begin, df_end. df_begin takes an arg to specify reverse or not.
15 // 4. Iterator over the basic blocks of a method in post order.
16 // 5. Iterator over a method in reverse post order.
18 //===----------------------------------------------------------------------===//
23 #include "llvm/CFGdecls.h" // See this file for concise interface info
27 #include "llvm/Method.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/InstrTypes.h"
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
38 // Basic Block Predecessor Iterator
41 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
42 class PredIterator : public std::bidirectional_iterator<_Ptr, ptrdiff_t> {
46 typedef PredIterator<_Ptr,_USE_iterator> _Self;
48 inline void advancePastConstPool() {
50 // Loop to ignore constant pool references
51 while (It != BB->use_end() &&
52 ((!(*It)->isInstruction()) ||
53 !(((Instruction*)(*It))->isTerminator())))
57 inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
58 advancePastConstPool();
60 inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
62 inline bool operator==(const _Self& x) const { return It == x.It; }
63 inline bool operator!=(const _Self& x) const { return !operator==(x); }
65 inline pointer operator*() const {
66 return (*It)->castInstructionAsserting()->getParent();
68 inline pointer *operator->() const { return &(operator*()); }
70 inline _Self& operator++() { // Preincrement
71 ++It; advancePastConstPool();
75 inline _Self operator++(int) { // Postincrement
76 _Self tmp = *this; ++*this; return tmp;
79 inline _Self& operator--() { --It; return *this; } // Predecrement
80 inline _Self operator--(int) { // Postdecrement
81 _Self tmp = *this; --*this; return tmp;
85 inline pred_iterator pred_begin( BasicBlock *BB) {
86 return pred_iterator(BB);
88 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
89 return pred_const_iterator(BB);
91 inline pred_iterator pred_end( BasicBlock *BB) {
92 return pred_iterator(BB,true);
94 inline pred_const_iterator pred_end(const BasicBlock *BB) {
95 return pred_const_iterator(BB,true);
99 //===----------------------------------------------------------------------===//
100 // Basic Block Successor Iterator
103 template <class _Term, class _BB> // Successor Iterator
104 class SuccIterator : public std::bidirectional_iterator<_BB, ptrdiff_t> {
108 typedef SuccIterator<_Term, _BB> _Self;
109 // TODO: This can be random access iterator, need operator+ and stuff tho
111 inline SuccIterator(_Term T) : Term(T), idx(0) { // begin iterator
112 assert(T && "getTerminator returned null!");
114 inline SuccIterator(_Term T, bool) // end iterator
115 : Term(T), idx(Term->getNumSuccessors()) {
116 assert(T && "getTerminator returned null!");
119 inline bool operator==(const _Self& x) const { return idx == x.idx; }
120 inline bool operator!=(const _Self& x) const { return !operator==(x); }
122 inline pointer operator*() const { return Term->getSuccessor(idx); }
123 inline pointer operator->() const { return operator*(); }
125 inline _Self& operator++() { ++idx; return *this; } // Preincrement
126 inline _Self operator++(int) { // Postincrement
127 _Self tmp = *this; ++*this; return tmp;
130 inline _Self& operator--() { --idx; return *this; } // Predecrement
131 inline _Self operator--(int) { // Postdecrement
132 _Self tmp = *this; --*this; return tmp;
136 inline succ_iterator succ_begin( BasicBlock *BB) {
137 return succ_iterator(BB->getTerminator());
139 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
140 return succ_const_iterator(BB->getTerminator());
142 inline succ_iterator succ_end( BasicBlock *BB) {
143 return succ_iterator(BB->getTerminator(),true);
145 inline succ_const_iterator succ_end(const BasicBlock *BB) {
146 return succ_const_iterator(BB->getTerminator(),true);
150 //===----------------------------------------------------------------------===//
151 // Graph Type Declarations
153 // BasicBlockGraph - Represent a standard traversal of a CFG
154 // ConstBasicBlockGraph - Represent a standard traversal of a const CFG
155 // InverseBasicBlockGraph - Represent a inverse traversal of a CFG
156 // ConstInverseBasicBlockGraph - Represent a inverse traversal of a const CFG
158 // An Inverse traversal of a graph is where we chase predecessors, instead of
161 struct BasicBlockGraph {
162 typedef BasicBlock NodeType;
163 typedef succ_iterator ChildIteratorType;
164 static inline ChildIteratorType child_begin(NodeType *N) {
165 return succ_begin(N);
167 static inline ChildIteratorType child_end(NodeType *N) {
172 struct ConstBasicBlockGraph {
173 typedef const BasicBlock NodeType;
174 typedef succ_const_iterator ChildIteratorType;
175 static inline ChildIteratorType child_begin(NodeType *N) {
176 return succ_begin(N);
178 static inline ChildIteratorType child_end(NodeType *N) {
183 struct InverseBasicBlockGraph {
184 typedef BasicBlock NodeType;
185 typedef pred_iterator ChildIteratorType;
186 static inline ChildIteratorType child_begin(NodeType *N) {
187 return pred_begin(N);
189 static inline ChildIteratorType child_end(NodeType *N) {
194 struct ConstInverseBasicBlockGraph {
195 typedef const BasicBlock NodeType;
196 typedef pred_const_iterator ChildIteratorType;
197 static inline ChildIteratorType child_begin(NodeType *N) {
198 return pred_begin(N);
200 static inline ChildIteratorType child_end(NodeType *N) {
206 //===----------------------------------------------------------------------===//
207 // Depth First Iterator
210 // Generic Depth First Iterator
212 class DFIterator : public std::forward_iterator<typename GI::NodeType,
214 typedef typename GI::NodeType NodeType;
215 typedef typename GI::ChildIteratorType ChildItTy;
217 set<NodeType *> Visited; // All of the blocks visited so far...
218 // VisitStack - Used to maintain the ordering. Top = current block
219 // First element is node pointer, second is the 'next child' to visit
220 stack<pair<NodeType *, ChildItTy> > VisitStack;
221 const bool Reverse; // Iterate over children before self?
223 void reverseEnterNode() {
224 pair<NodeType *, ChildItTy> &Top = VisitStack.top();
225 NodeType *Node = Top.first;
226 ChildItTy &It = Top.second;
227 for (; It != GI::child_end(Node); ++It) {
228 NodeType *Child = *It;
229 if (!Visited.count(Child)) {
230 Visited.insert(Child);
231 VisitStack.push(make_pair(Child, GI::child_begin(Child)));
238 typedef DFIterator<GI> _Self;
240 inline DFIterator(NodeType *Node, bool reverse) : Reverse(reverse) {
241 Visited.insert(Node);
242 VisitStack.push(make_pair(Node, GI::child_begin(Node)));
243 if (Reverse) reverseEnterNode();
245 inline DFIterator() { /* End is when stack is empty */ }
247 inline bool operator==(const _Self& x) const {
248 return VisitStack == x.VisitStack;
250 inline bool operator!=(const _Self& x) const { return !operator==(x); }
252 inline pointer operator*() const {
253 return VisitStack.top().first;
256 // This is a nonstandard operator-> that dereferences the pointer an extra
257 // time... so that you can actually call methods ON the Node, because
258 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
260 inline NodeType *operator->() const { return operator*(); }
262 inline _Self& operator++() { // Preincrement
263 if (Reverse) { // Reverse Depth First Iterator
264 if (VisitStack.top().second == GI::child_end(VisitStack.top().first))
266 if (!VisitStack.empty())
268 } else { // Normal Depth First Iterator
270 pair<NodeType *, ChildItTy> &Top = VisitStack.top();
271 NodeType *Node = Top.first;
272 ChildItTy &It = Top.second;
274 while (It != GI::child_end(Node)) {
275 NodeType *Next = *It++;
276 if (!Visited.count(Next)) { // Has our next sibling been visited?
278 Visited.insert(Next);
279 VisitStack.push(make_pair(Next, GI::child_begin(Next)));
284 // Oops, ran out of successors... go up a level on the stack.
286 } while (!VisitStack.empty());
291 inline _Self operator++(int) { // Postincrement
292 _Self tmp = *this; ++*this; return tmp;
295 // nodeVisited - return true if this iterator has already visited the
296 // specified node. This is public, and will probably be used to iterate over
297 // nodes that a depth first iteration did not find: ie unreachable nodes.
299 inline bool nodeVisited(NodeType *Node) const {
300 return Visited.count(Node) != 0;
304 inline df_iterator df_begin(Method *M, bool Reverse = false) {
305 return df_iterator(M->front(), Reverse);
308 inline df_const_iterator df_begin(const Method *M, bool Reverse = false) {
309 return df_const_iterator(M->front(), Reverse);
311 inline df_iterator df_end(Method*) {
312 return df_iterator();
314 inline df_const_iterator df_end(const Method*) {
315 return df_const_iterator();
318 inline df_iterator df_begin(BasicBlock *BB, bool Reverse = false) {
319 return df_iterator(BB, Reverse);
321 inline df_const_iterator df_begin(const BasicBlock *BB, bool Reverse = false) {
322 return df_const_iterator(BB, Reverse);
325 inline df_iterator df_end(BasicBlock*) {
326 return df_iterator();
328 inline df_const_iterator df_end(const BasicBlock*) {
329 return df_const_iterator();
334 inline idf_iterator idf_begin(BasicBlock *BB, bool Reverse = false) {
335 return idf_iterator(BB, Reverse);
337 inline idf_const_iterator idf_begin(const BasicBlock *BB, bool Reverse = false) {
338 return idf_const_iterator(BB, Reverse);
341 inline idf_iterator idf_end(BasicBlock*) {
342 return idf_iterator();
344 inline idf_const_iterator idf_end(const BasicBlock*) {
345 return idf_const_iterator();
348 //===----------------------------------------------------------------------===//
349 // Post Order CFG iterator code
352 template<class BBType, class SuccItTy>
353 class POIterator : public std::forward_iterator<BBType, ptrdiff_t> {
354 set<BBType *> Visited; // All of the blocks visited so far...
355 // VisitStack - Used to maintain the ordering. Top = current block
356 // First element is basic block pointer, second is the 'next child' to visit
357 stack<pair<BBType *, SuccItTy> > VisitStack;
359 void traverseChild() {
360 while (VisitStack.top().second != succ_end(VisitStack.top().first)) {
361 BBType *BB = *VisitStack.top().second++;
362 if (!Visited.count(BB)) { // If the block is not visited...
364 VisitStack.push(make_pair(BB, succ_begin(BB)));
369 typedef POIterator<BBType, SuccItTy> _Self;
371 inline POIterator(BBType *BB) {
373 VisitStack.push(make_pair(BB, succ_begin(BB)));
376 inline POIterator() { /* End is when stack is empty */ }
378 inline bool operator==(const _Self& x) const {
379 return VisitStack == x.VisitStack;
381 inline bool operator!=(const _Self& x) const { return !operator==(x); }
383 inline pointer operator*() const {
384 return VisitStack.top().first;
387 // This is a nonstandard operator-> that dereferences the pointer an extra
388 // time... so that you can actually call methods ON the BasicBlock, because
389 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
391 inline BBType *operator->() const { return operator*(); }
393 inline _Self& operator++() { // Preincrement
395 if (!VisitStack.empty())
400 inline _Self operator++(int) { // Postincrement
401 _Self tmp = *this; ++*this; return tmp;
405 inline po_iterator po_begin( Method *M) {
406 return po_iterator(M->front());
408 inline po_const_iterator po_begin(const Method *M) {
409 return po_const_iterator(M->front());
411 inline po_iterator po_end ( Method *M) {
412 return po_iterator();
414 inline po_const_iterator po_end (const Method *M) {
415 return po_const_iterator();
418 inline po_iterator po_begin( BasicBlock *BB) {
419 return po_iterator(BB);
421 inline po_const_iterator po_begin(const BasicBlock *BB) {
422 return po_const_iterator(BB);
424 inline po_iterator po_end ( BasicBlock *BB) {
425 return po_iterator();
427 inline po_const_iterator po_end (const BasicBlock *BB) {
428 return po_const_iterator();
432 //===----------------------------------------------------------------------===//
433 // Reverse Post Order CFG iterator code
436 class ReversePostOrderTraversal {
437 vector<BasicBlock*> Blocks; // Block list in normal PO order
438 inline void Initialize(BasicBlock *BB) {
439 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
442 inline ReversePostOrderTraversal(Method *M) {
443 Initialize(M->front());
445 inline ReversePostOrderTraversal(BasicBlock *BB) {
449 // Because we want a reverse post order, use reverse iterators from the vector
450 inline rpo_iterator begin() { return Blocks.rbegin(); }
451 inline rpo_iterator end() { return Blocks.rend(); }
454 } // End namespace cfg