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