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