0a58104ee4f49f2b13c4af1e7b923b86ca47a6c4
[oota-llvm.git] / include / llvm / Support / CFG.h
1 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines specializations of GraphTraits that allow Function and
11 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_CFG_H
16 #define LLVM_SUPPORT_CFG_H
17
18 #include "Support/GraphTraits.h"
19 #include "llvm/Function.h"
20 #include "llvm/InstrTypes.h"
21 #include "Support/iterator"
22
23 //===--------------------------------------------------------------------===//
24 // BasicBlock pred_iterator definition
25 //===--------------------------------------------------------------------===//
26
27 template <class _Ptr,  class _USE_iterator> // Predecessor Iterator
28 class PredIterator : public bidirectional_iterator<_Ptr, ptrdiff_t> {
29   typedef bidirectional_iterator<_Ptr, ptrdiff_t> super;
30   _Ptr *BB;
31   _USE_iterator It;
32 public:
33   typedef PredIterator<_Ptr,_USE_iterator> _Self;
34   typedef typename super::pointer pointer;
35   
36   inline void advancePastConstants() {
37     // Loop to ignore non terminator uses (for example PHI nodes)...
38     while (It != BB->use_end() && !isa<TerminatorInst>(*It))
39       ++It;
40   }
41   
42   inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
43     advancePastConstants();
44   }
45   inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
46     
47   inline bool operator==(const _Self& x) const { return It == x.It; }
48   inline bool operator!=(const _Self& x) const { return !operator==(x); }
49   
50   inline pointer operator*() const { 
51     assert(It != BB->use_end() && "pred_iterator out of range!");
52     return cast<TerminatorInst>(*It)->getParent(); 
53   }
54   inline pointer *operator->() const { return &(operator*()); }
55   
56   inline _Self& operator++() {   // Preincrement
57     assert(It != BB->use_end() && "pred_iterator out of range!");
58     ++It; advancePastConstants();
59     return *this; 
60   }
61   
62   inline _Self operator++(int) { // Postincrement
63     _Self tmp = *this; ++*this; return tmp; 
64   }
65   
66   inline _Self& operator--() { --It; return *this; }  // Predecrement
67   inline _Self operator--(int) { // Postdecrement
68     _Self tmp = *this; --*this; return tmp;
69   }
70 };
71
72 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
73 typedef PredIterator<const BasicBlock, 
74                      Value::use_const_iterator> pred_const_iterator;
75
76 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
77 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
78   return pred_const_iterator(BB);
79 }
80 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
81 inline pred_const_iterator pred_end(const BasicBlock *BB) {
82   return pred_const_iterator(BB, true);
83 }
84
85
86
87 //===--------------------------------------------------------------------===//
88 // BasicBlock succ_iterator definition
89 //===--------------------------------------------------------------------===//
90
91 template <class _Term, class _BB>           // Successor Iterator
92 class SuccIterator : public bidirectional_iterator<_BB, ptrdiff_t> {
93   const _Term Term;
94   unsigned idx;
95   typedef bidirectional_iterator<_BB, ptrdiff_t> super;
96 public:
97   typedef SuccIterator<_Term, _BB> _Self;
98   typedef typename super::pointer pointer;
99   // TODO: This can be random access iterator, need operator+ and stuff tho
100     
101   inline SuccIterator(_Term T) : Term(T), idx(0) {         // begin iterator
102     assert(T && "getTerminator returned null!");
103   }
104   inline SuccIterator(_Term T, bool)                       // end iterator
105     : Term(T), idx(Term->getNumSuccessors()) {
106     assert(T && "getTerminator returned null!");
107   }
108
109   inline const _Self &operator=(const _Self &I) {
110     assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
111     idx = I.idx;
112     return *this;
113   }
114
115   /// getSuccessorIndex - This is used to interface between code that wants to
116   /// operate on terminator instructions directly.
117   unsigned getSuccessorIndex() const { return Idx; }
118     
119   inline bool operator==(const _Self& x) const { return idx == x.idx; }
120   inline bool operator!=(const _Self& x) const { return !operator==(x); }
121   
122   inline pointer operator*() const { return Term->getSuccessor(idx); }
123   inline pointer operator->() const { return operator*(); }
124   
125   inline _Self& operator++() { ++idx; return *this; } // Preincrement
126   inline _Self operator++(int) { // Postincrement
127     _Self tmp = *this; ++*this; return tmp; 
128   }
129     
130   inline _Self& operator--() { --idx; return *this; }  // Predecrement
131   inline _Self operator--(int) { // Postdecrement
132     _Self tmp = *this; --*this; return tmp;
133   }
134 };
135
136 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
137 typedef SuccIterator<const TerminatorInst*,
138                      const BasicBlock> succ_const_iterator;
139
140 inline succ_iterator succ_begin(BasicBlock *BB) {
141   return succ_iterator(BB->getTerminator());
142 }
143 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
144   return succ_const_iterator(BB->getTerminator());
145 }
146 inline succ_iterator succ_end(BasicBlock *BB) {
147   return succ_iterator(BB->getTerminator(), true);
148 }
149 inline succ_const_iterator succ_end(const BasicBlock *BB) {
150   return succ_const_iterator(BB->getTerminator(), true);
151 }
152
153
154
155 //===--------------------------------------------------------------------===//
156 // GraphTraits specializations for basic block graphs (CFGs)
157 //===--------------------------------------------------------------------===//
158
159 // Provide specializations of GraphTraits to be able to treat a function as a 
160 // graph of basic blocks...
161
162 template <> struct GraphTraits<BasicBlock*> {
163   typedef BasicBlock NodeType;
164   typedef succ_iterator ChildIteratorType;
165
166   static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
167   static inline ChildIteratorType child_begin(NodeType *N) { 
168     return succ_begin(N);
169   }
170   static inline ChildIteratorType child_end(NodeType *N) { 
171     return succ_end(N);
172   }
173 };
174
175 template <> struct GraphTraits<const BasicBlock*> {
176   typedef const BasicBlock NodeType;
177   typedef succ_const_iterator ChildIteratorType;
178
179   static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
180
181   static inline ChildIteratorType child_begin(NodeType *N) { 
182     return succ_begin(N);
183   }
184   static inline ChildIteratorType child_end(NodeType *N) { 
185     return succ_end(N);
186   }
187 };
188
189 // Provide specializations of GraphTraits to be able to treat a function as a 
190 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
191 // a function is considered to be when traversing the predecessor edges of a BB
192 // instead of the successor edges.
193 //
194 template <> struct GraphTraits<Inverse<BasicBlock*> > {
195   typedef BasicBlock NodeType;
196   typedef pred_iterator ChildIteratorType;
197   static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
198   static inline ChildIteratorType child_begin(NodeType *N) { 
199     return pred_begin(N);
200   }
201   static inline ChildIteratorType child_end(NodeType *N) { 
202     return pred_end(N);
203   }
204 };
205
206 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
207   typedef const BasicBlock NodeType;
208   typedef pred_const_iterator ChildIteratorType;
209   static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
210     return G.Graph; 
211   }
212   static inline ChildIteratorType child_begin(NodeType *N) { 
213     return pred_begin(N);
214   }
215   static inline ChildIteratorType child_end(NodeType *N) { 
216     return pred_end(N);
217   }
218 };
219
220
221
222 //===--------------------------------------------------------------------===//
223 // GraphTraits specializations for function basic block graphs (CFGs)
224 //===--------------------------------------------------------------------===//
225
226 // Provide specializations of GraphTraits to be able to treat a function as a 
227 // graph of basic blocks... these are the same as the basic block iterators,
228 // except that the root node is implicitly the first node of the function.
229 //
230 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
231   static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
232
233   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
234   typedef Function::iterator nodes_iterator;
235   static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
236   static nodes_iterator nodes_end  (Function *F) { return F->end(); }
237 };
238 template <> struct GraphTraits<const Function*> :
239   public GraphTraits<const BasicBlock*> {
240   static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
241
242   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
243   typedef Function::const_iterator nodes_iterator;
244   static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
245   static nodes_iterator nodes_end  (const Function *F) { return F->end(); }
246 };
247
248
249 // Provide specializations of GraphTraits to be able to treat a function as a 
250 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
251 // a function is considered to be when traversing the predecessor edges of a BB
252 // instead of the successor edges.
253 //
254 template <> struct GraphTraits<Inverse<Function*> > :
255   public GraphTraits<Inverse<BasicBlock*> > {
256   static NodeType *getEntryNode(Inverse<Function*> G) {
257     return &G.Graph->getEntryBlock();
258   }
259 };
260 template <> struct GraphTraits<Inverse<const Function*> > :
261   public GraphTraits<Inverse<const BasicBlock*> > {
262   static NodeType *getEntryNode(Inverse<const Function *> G) {
263     return &G.Graph->getEntryBlock();
264   }
265 };
266
267 #endif