91cd84a15953a4d6cb47c501e243eb10bce2306b
[oota-llvm.git] / include / llvm / IR / CFG.h
1 //===- 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_IR_CFG_H
16 #define LLVM_IR_CFG_H
17
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/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, Ptr*, Ptr*> {
31   typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t, Ptr*,
32                                                                     Ptr*> super;
33   typedef PredIterator<Ptr, USE_iterator> Self;
34   USE_iterator It;
35
36   inline void advancePastNonTerminators() {
37     // Loop to ignore non-terminator uses (for example BlockAddresses).
38     while (!It.atEnd() && !isa<TerminatorInst>(*It))
39       ++It;
40   }
41
42 public:
43   typedef typename super::pointer pointer;
44   typedef typename super::reference reference;
45
46   PredIterator() {}
47   explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
48     advancePastNonTerminators();
49   }
50   inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
51
52   inline bool operator==(const Self& x) const { return It == x.It; }
53   inline bool operator!=(const Self& x) const { return !operator==(x); }
54
55   inline reference operator*() const {
56     assert(!It.atEnd() && "pred_iterator out of range!");
57     return cast<TerminatorInst>(*It)->getParent();
58   }
59   inline pointer *operator->() const { return &operator*(); }
60
61   inline Self& operator++() {   // Preincrement
62     assert(!It.atEnd() && "pred_iterator out of range!");
63     ++It; advancePastNonTerminators();
64     return *this;
65   }
66
67   inline Self operator++(int) { // Postincrement
68     Self tmp = *this; ++*this; return tmp;
69   }
70
71   /// getOperandNo - Return the operand number in the predecessor's
72   /// terminator of the successor.
73   unsigned getOperandNo() const {
74     return It.getOperandNo();
75   }
76
77   /// getUse - Return the operand Use in the predecessor's terminator
78   /// of the successor.
79   Use &getUse() const {
80     return It.getUse();
81   }
82 };
83
84 typedef PredIterator<BasicBlock, Value::user_iterator> pred_iterator;
85 typedef PredIterator<const BasicBlock,
86                      Value::const_user_iterator> const_pred_iterator;
87
88 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
89 inline const_pred_iterator pred_begin(const BasicBlock *BB) {
90   return const_pred_iterator(BB);
91 }
92 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
93 inline const_pred_iterator pred_end(const BasicBlock *BB) {
94   return const_pred_iterator(BB, true);
95 }
96 inline iterator_range<pred_iterator> predecessors(BasicBlock *BB) {
97   return make_range(pred_begin(BB), pred_end(BB));
98 }
99 inline iterator_range<const_pred_iterator> predecessors(const BasicBlock *BB) {
100   return make_range(pred_begin(BB), pred_end(BB));
101 }
102
103
104
105 //===----------------------------------------------------------------------===//
106 // BasicBlock succ_iterator definition
107 //===----------------------------------------------------------------------===//
108
109 template <class Term_, class BB_>           // Successor Iterator
110 class SuccIterator : public std::iterator<std::random_access_iterator_tag, BB_,
111                                           int, BB_ *, BB_ *> {
112   typedef std::iterator<std::random_access_iterator_tag, BB_, int, BB_ *, BB_ *>
113   super;
114
115 public:
116   typedef typename super::pointer pointer;
117   typedef typename super::reference reference;
118
119 private:
120   const Term_ Term;
121   unsigned idx;
122   typedef SuccIterator<Term_, BB_> Self;
123
124   inline bool index_is_valid(int idx) {
125     return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
126   }
127
128   /// \brief Proxy object to allow write access in operator[]
129   class SuccessorProxy {
130     Self it;
131
132   public:
133     explicit SuccessorProxy(const Self &it) : it(it) {}
134
135     SuccessorProxy &operator=(SuccessorProxy r) {
136       *this = reference(r);
137       return *this;
138     }
139
140     SuccessorProxy &operator=(reference r) {
141       it.Term->setSuccessor(it.idx, r);
142       return *this;
143     }
144
145     operator reference() const { return *it; }
146   };
147
148 public:
149   explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
150   }
151   inline SuccIterator(Term_ T, bool)                       // end iterator
152     : Term(T) {
153     if (Term)
154       idx = Term->getNumSuccessors();
155     else
156       // Term == NULL happens, if a basic block is not fully constructed and
157       // consequently getTerminator() returns NULL. In this case we construct a
158       // SuccIterator which describes a basic block that has zero successors.
159       // Defining SuccIterator for incomplete and malformed CFGs is especially
160       // useful for debugging.
161       idx = 0;
162   }
163
164   inline const Self &operator=(const Self &I) {
165     assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
166     idx = I.idx;
167     return *this;
168   }
169
170   /// getSuccessorIndex - This is used to interface between code that wants to
171   /// operate on terminator instructions directly.
172   unsigned getSuccessorIndex() const { return idx; }
173
174   inline bool operator==(const Self& x) const { return idx == x.idx; }
175   inline bool operator!=(const Self& x) const { return !operator==(x); }
176
177   inline reference operator*() const { return Term->getSuccessor(idx); }
178   inline pointer operator->() const { return operator*(); }
179
180   inline Self& operator++() { ++idx; return *this; } // Preincrement
181
182   inline Self operator++(int) { // Postincrement
183     Self tmp = *this; ++*this; return tmp;
184   }
185
186   inline Self& operator--() { --idx; return *this; }  // Predecrement
187   inline Self operator--(int) { // Postdecrement
188     Self tmp = *this; --*this; return tmp;
189   }
190
191   inline bool operator<(const Self& x) const {
192     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
193     return idx < x.idx;
194   }
195
196   inline bool operator<=(const Self& x) const {
197     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
198     return idx <= x.idx;
199   }
200   inline bool operator>=(const Self& x) const {
201     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
202     return idx >= x.idx;
203   }
204
205   inline bool operator>(const Self& x) const {
206     assert(Term == x.Term && "Cannot compare iterators of different blocks!");
207     return idx > x.idx;
208   }
209
210   inline Self& operator+=(int Right) {
211     unsigned new_idx = idx + Right;
212     assert(index_is_valid(new_idx) && "Iterator index out of bound");
213     idx = new_idx;
214     return *this;
215   }
216
217   inline Self operator+(int Right) const {
218     Self tmp = *this;
219     tmp += Right;
220     return tmp;
221   }
222
223   inline Self& operator-=(int Right) {
224     return operator+=(-Right);
225   }
226
227   inline Self operator-(int Right) const {
228     return operator+(-Right);
229   }
230
231   inline int operator-(const Self& x) const {
232     assert(Term == x.Term && "Cannot work on iterators of different blocks!");
233     int distance = idx - x.idx;
234     return distance;
235   }
236
237   inline SuccessorProxy operator[](int offset) {
238    Self tmp = *this;
239    tmp += offset;
240    return SuccessorProxy(tmp);
241   }
242
243   /// Get the source BB of this iterator.
244   inline BB_ *getSource() {
245     assert(Term && "Source not available, if basic block was malformed");
246     return Term->getParent();
247   }
248 };
249
250 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
251 typedef SuccIterator<const TerminatorInst*,
252                      const BasicBlock> succ_const_iterator;
253
254 inline succ_iterator succ_begin(BasicBlock *BB) {
255   return succ_iterator(BB->getTerminator());
256 }
257 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
258   return succ_const_iterator(BB->getTerminator());
259 }
260 inline succ_iterator succ_end(BasicBlock *BB) {
261   return succ_iterator(BB->getTerminator(), true);
262 }
263 inline succ_const_iterator succ_end(const BasicBlock *BB) {
264   return succ_const_iterator(BB->getTerminator(), true);
265 }
266 inline iterator_range<succ_iterator> successors(BasicBlock *BB) {
267   return make_range(succ_begin(BB), succ_end(BB));
268 }
269 inline iterator_range<succ_const_iterator> successors(const BasicBlock *BB) {
270   return make_range(succ_begin(BB), succ_end(BB));
271 }
272
273 template <typename T, typename U> struct isPodLike<SuccIterator<T, U> > {
274   static const bool value = isPodLike<T>::value;
275 };
276
277
278
279 //===--------------------------------------------------------------------===//
280 // GraphTraits specializations for basic block graphs (CFGs)
281 //===--------------------------------------------------------------------===//
282
283 // Provide specializations of GraphTraits to be able to treat a function as a
284 // graph of basic blocks...
285
286 template <> struct GraphTraits<BasicBlock*> {
287   typedef BasicBlock NodeType;
288   typedef succ_iterator ChildIteratorType;
289
290   static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
291   static inline ChildIteratorType child_begin(NodeType *N) {
292     return succ_begin(N);
293   }
294   static inline ChildIteratorType child_end(NodeType *N) {
295     return succ_end(N);
296   }
297 };
298
299 template <> struct GraphTraits<const BasicBlock*> {
300   typedef const BasicBlock NodeType;
301   typedef succ_const_iterator ChildIteratorType;
302
303   static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
304
305   static inline ChildIteratorType child_begin(NodeType *N) {
306     return succ_begin(N);
307   }
308   static inline ChildIteratorType child_end(NodeType *N) {
309     return succ_end(N);
310   }
311 };
312
313 // Provide specializations of GraphTraits to be able to treat a function as a
314 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
315 // a function is considered to be when traversing the predecessor edges of a BB
316 // instead of the successor edges.
317 //
318 template <> struct GraphTraits<Inverse<BasicBlock*> > {
319   typedef BasicBlock NodeType;
320   typedef pred_iterator ChildIteratorType;
321   static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
322   static inline ChildIteratorType child_begin(NodeType *N) {
323     return pred_begin(N);
324   }
325   static inline ChildIteratorType child_end(NodeType *N) {
326     return pred_end(N);
327   }
328 };
329
330 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
331   typedef const BasicBlock NodeType;
332   typedef const_pred_iterator ChildIteratorType;
333   static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
334     return G.Graph;
335   }
336   static inline ChildIteratorType child_begin(NodeType *N) {
337     return pred_begin(N);
338   }
339   static inline ChildIteratorType child_end(NodeType *N) {
340     return pred_end(N);
341   }
342 };
343
344
345
346 //===--------------------------------------------------------------------===//
347 // GraphTraits specializations for function basic block graphs (CFGs)
348 //===--------------------------------------------------------------------===//
349
350 // Provide specializations of GraphTraits to be able to treat a function as a
351 // graph of basic blocks... these are the same as the basic block iterators,
352 // except that the root node is implicitly the first node of the function.
353 //
354 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
355   static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
356
357   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
358   typedef Function::iterator nodes_iterator;
359   static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
360   static nodes_iterator nodes_end  (Function *F) { return F->end(); }
361   static size_t         size       (Function *F) { return F->size(); }
362 };
363 template <> struct GraphTraits<const Function*> :
364   public GraphTraits<const BasicBlock*> {
365   static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
366
367   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
368   typedef Function::const_iterator nodes_iterator;
369   static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
370   static nodes_iterator nodes_end  (const Function *F) { return F->end(); }
371   static size_t         size       (const Function *F) { return F->size(); }
372 };
373
374
375 // Provide specializations of GraphTraits to be able to treat a function as a
376 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
377 // a function is considered to be when traversing the predecessor edges of a BB
378 // instead of the successor edges.
379 //
380 template <> struct GraphTraits<Inverse<Function*> > :
381   public GraphTraits<Inverse<BasicBlock*> > {
382   static NodeType *getEntryNode(Inverse<Function*> G) {
383     return &G.Graph->getEntryBlock();
384   }
385 };
386 template <> struct GraphTraits<Inverse<const Function*> > :
387   public GraphTraits<Inverse<const BasicBlock*> > {
388   static NodeType *getEntryNode(Inverse<const Function *> G) {
389     return &G.Graph->getEntryBlock();
390   }
391 };
392
393 } // End llvm namespace
394
395 #endif