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