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