Add new -d option to tblgen. It writes a make(1)-style dependency file.
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/ADT/GraphTraits.h"
19 #include "llvm/Function.h"
20 #include "llvm/InstrTypes.h"
21
22 namespace llvm {
23
24 //===----------------------------------------------------------------------===//
25 // BasicBlock pred_iterator definition
26 //===----------------------------------------------------------------------===//
27
28 template <class Ptr, class USE_iterator> // Predecessor Iterator
29 class PredIterator : public std::iterator<std::forward_iterator_tag,
30                                           Ptr, ptrdiff_t> {
31   typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t> super;
32   typedef PredIterator<Ptr, USE_iterator> Self;
33   USE_iterator It;
34
35   inline void advancePastNonTerminators() {
36     // Loop to ignore non terminator uses (for example PHI nodes).
37     while (!It.atEnd() && !isa<TerminatorInst>(*It))
38       ++It;
39   }
40
41 public:
42   typedef typename super::pointer pointer;
43
44   PredIterator() {}
45   explicit inline PredIterator(Ptr *bb) : It(bb->use_begin()) {
46     advancePastNonTerminators();
47   }
48   inline PredIterator(Ptr *bb, bool) : It(bb->use_end()) {}
49
50   inline bool operator==(const Self& x) const { return It == x.It; }
51   inline bool operator!=(const Self& x) const { return !operator==(x); }
52
53   inline pointer operator*() const {
54     assert(!It.atEnd() && "pred_iterator out of range!");
55     return cast<TerminatorInst>(*It)->getParent();
56   }
57   inline pointer *operator->() const { return &operator*(); }
58
59   inline Self& operator++() {   // Preincrement
60     assert(!It.atEnd() && "pred_iterator out of range!");
61     ++It; advancePastNonTerminators();
62     return *this;
63   }
64
65   inline Self operator++(int) { // Postincrement
66     Self tmp = *this; ++*this; return tmp;
67   }
68
69   /// getOperandNo - Return the operand number in the predecessor's
70   /// terminator of the successor.
71   unsigned getOperandNo() const {
72     return It.getOperandNo();
73   }
74 };
75
76 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
77 typedef PredIterator<const BasicBlock,
78                      Value::const_use_iterator> const_pred_iterator;
79
80 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
81 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
82   return const_pred_iterator(BB);
83 }
84 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
85 inline const_pred_iterator pred_end(const BasicBlock *BB) {
86   return const_pred_iterator(BB, true);
87 }
88
89
90
91 //===----------------------------------------------------------------------===//
92 // BasicBlock succ_iterator definition
93 //===----------------------------------------------------------------------===//
94
95 template <class Term_, class BB_>           // Successor Iterator
96 class SuccIterator : public std::iterator<std::bidirectional_iterator_tag,
97                                           BB_, ptrdiff_t> {
98   const Term_ Term;
99   unsigned idx;
100   typedef std::iterator<std::bidirectional_iterator_tag, BB_, ptrdiff_t> super;
101   typedef SuccIterator<Term_, BB_> Self;
102
103   inline bool index_is_valid(int idx) {
104     return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
105   }
106
107 public:
108   typedef typename super::pointer pointer;
109   // TODO: This can be random access iterator, only operator[] missing.
110
111   explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
112     assert(T && "getTerminator returned null!");
113   }
114   inline SuccIterator(Term_ T, bool)                       // end iterator
115     : Term(T), idx(Term->getNumSuccessors()) {
116     assert(T && "getTerminator returned null!");
117   }
118
119   inline const Self &operator=(const Self &I) {
120     assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
121     idx = I.idx;
122     return *this;
123   }
124
125   /// getSuccessorIndex - This is used to interface between code that wants to
126   /// operate on terminator instructions directly.
127   unsigned getSuccessorIndex() const { return idx; }
128
129   inline bool operator==(const Self& x) const { return idx == x.idx; }
130   inline bool operator!=(const Self& x) const { return !operator==(x); }
131
132   inline pointer operator*() const { return Term->getSuccessor(idx); }
133   inline pointer operator->() const { return operator*(); }
134
135   inline Self& operator++() { ++idx; return *this; } // Preincrement
136
137   inline Self operator++(int) { // Postincrement
138     Self tmp = *this; ++*this; return tmp;
139   }
140
141   inline Self& operator--() { --idx; return *this; }  // Predecrement
142   inline Self operator--(int) { // Postdecrement
143     Self tmp = *this; --*this; return tmp;
144   }
145
146   inline bool operator<(const Self& x) const {
147     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
148     return idx < x.idx;
149   }
150
151   inline bool operator<=(const Self& x) const {
152     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
153     return idx <= x.idx;
154   }
155   inline bool operator>=(const Self& x) const {
156     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
157     return idx >= x.idx;
158   }
159
160   inline bool operator>(const Self& x) const {
161     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
162     return idx > x.idx;
163   }
164
165   inline Self& operator+=(int Right) {
166     unsigned new_idx = idx + Right;
167     assert(index_is_valid(new_idx) && "Iterator index out of bound");
168     idx = new_idx;
169     return *this;
170   }
171
172   inline Self operator+(int Right) {
173     Self tmp = *this;
174     tmp += Right;
175     return tmp;
176   }
177
178   inline Self& operator-=(int Right) {
179     return operator+=(-Right);
180   }
181
182   inline Self operator-(int Right) {
183     return operator+(-Right);
184   }
185
186   inline int operator-(const Self& x) {
187     assert(Term == x.Term && "Cannot work on iterators of different blocks!");
188     int distance = idx - x.idx;
189     return distance;
190   }
191
192   // This works for read access, however write access is difficult as changes
193   // to Term are only possible with Term->setSuccessor(idx). Pointers that can
194   // be modified are not available.
195   //
196   // inline pointer operator[](int offset) {
197   //  Self tmp = *this;
198   //  tmp += offset;
199   //  return tmp.operator*();
200   // }
201
202   /// Get the source BB of this iterator.
203   inline BB_ *getSource() {
204     return Term->getParent();
205   }
206 };
207
208 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
209 typedef SuccIterator<const TerminatorInst*,
210                      const BasicBlock> succ_const_iterator;
211
212 inline succ_iterator succ_begin(BasicBlock *BB) {
213   return succ_iterator(BB->getTerminator());
214 }
215 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
216   return succ_const_iterator(BB->getTerminator());
217 }
218 inline succ_iterator succ_end(BasicBlock *BB) {
219   return succ_iterator(BB->getTerminator(), true);
220 }
221 inline succ_const_iterator succ_end(const BasicBlock *BB) {
222   return succ_const_iterator(BB->getTerminator(), true);
223 }
224
225
226
227 //===--------------------------------------------------------------------===//
228 // GraphTraits specializations for basic block graphs (CFGs)
229 //===--------------------------------------------------------------------===//
230
231 // Provide specializations of GraphTraits to be able to treat a function as a
232 // graph of basic blocks...
233
234 template <> struct GraphTraits<BasicBlock*> {
235   typedef BasicBlock NodeType;
236   typedef succ_iterator ChildIteratorType;
237
238   static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
239   static inline ChildIteratorType child_begin(NodeType *N) {
240     return succ_begin(N);
241   }
242   static inline ChildIteratorType child_end(NodeType *N) {
243     return succ_end(N);
244   }
245 };
246
247 template <> struct GraphTraits<const BasicBlock*> {
248   typedef const BasicBlock NodeType;
249   typedef succ_const_iterator ChildIteratorType;
250
251   static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
252
253   static inline ChildIteratorType child_begin(NodeType *N) {
254     return succ_begin(N);
255   }
256   static inline ChildIteratorType child_end(NodeType *N) {
257     return succ_end(N);
258   }
259 };
260
261 // Provide specializations of GraphTraits to be able to treat a function as a
262 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
263 // a function is considered to be when traversing the predecessor edges of a BB
264 // instead of the successor edges.
265 //
266 template <> struct GraphTraits<Inverse<BasicBlock*> > {
267   typedef BasicBlock NodeType;
268   typedef pred_iterator ChildIteratorType;
269   static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
270   static inline ChildIteratorType child_begin(NodeType *N) {
271     return pred_begin(N);
272   }
273   static inline ChildIteratorType child_end(NodeType *N) {
274     return pred_end(N);
275   }
276 };
277
278 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
279   typedef const BasicBlock NodeType;
280   typedef const_pred_iterator ChildIteratorType;
281   static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
282     return G.Graph;
283   }
284   static inline ChildIteratorType child_begin(NodeType *N) {
285     return pred_begin(N);
286   }
287   static inline ChildIteratorType child_end(NodeType *N) {
288     return pred_end(N);
289   }
290 };
291
292
293
294 //===--------------------------------------------------------------------===//
295 // GraphTraits specializations for function basic block graphs (CFGs)
296 //===--------------------------------------------------------------------===//
297
298 // Provide specializations of GraphTraits to be able to treat a function as a
299 // graph of basic blocks... these are the same as the basic block iterators,
300 // except that the root node is implicitly the first node of the function.
301 //
302 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
303   static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
304
305   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
306   typedef Function::iterator nodes_iterator;
307   static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
308   static nodes_iterator nodes_end  (Function *F) { return F->end(); }
309 };
310 template <> struct GraphTraits<const Function*> :
311   public GraphTraits<const BasicBlock*> {
312   static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
313
314   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
315   typedef Function::const_iterator nodes_iterator;
316   static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
317   static nodes_iterator nodes_end  (const Function *F) { return F->end(); }
318 };
319
320
321 // Provide specializations of GraphTraits to be able to treat a function as a
322 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
323 // a function is considered to be when traversing the predecessor edges of a BB
324 // instead of the successor edges.
325 //
326 template <> struct GraphTraits<Inverse<Function*> > :
327   public GraphTraits<Inverse<BasicBlock*> > {
328   static NodeType *getEntryNode(Inverse<Function*> G) {
329     return &G.Graph->getEntryBlock();
330   }
331 };
332 template <> struct GraphTraits<Inverse<const Function*> > :
333   public GraphTraits<Inverse<const BasicBlock*> > {
334   static NodeType *getEntryNode(Inverse<const Function *> G) {
335     return &G.Graph->getEntryBlock();
336   }
337 };
338
339 } // End llvm namespace
340
341 #endif