1 //===- CFG.h - Process LLVM structures as graphs ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines specializations of GraphTraits that allow Function and
11 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
13 //===----------------------------------------------------------------------===//
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"
25 //===----------------------------------------------------------------------===//
26 // BasicBlock pred_iterator definition
27 //===----------------------------------------------------------------------===//
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*,
34 typedef PredIterator<Ptr, USE_iterator> Self;
37 inline void advancePastNonTerminators() {
38 // Loop to ignore non-terminator uses (for example BlockAddresses).
39 while (!It.atEnd() && !isa<TerminatorInst>(*It))
44 typedef typename super::pointer pointer;
45 typedef typename super::reference reference;
48 explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
49 advancePastNonTerminators();
51 inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
53 inline bool operator==(const Self& x) const { return It == x.It; }
54 inline bool operator!=(const Self& x) const { return !operator==(x); }
56 inline reference operator*() const {
57 assert(!It.atEnd() && "pred_iterator out of range!");
58 return cast<TerminatorInst>(*It)->getParent();
60 inline pointer *operator->() const { return &operator*(); }
62 inline Self& operator++() { // Preincrement
63 assert(!It.atEnd() && "pred_iterator out of range!");
64 ++It; advancePastNonTerminators();
68 inline Self operator++(int) { // Postincrement
69 Self tmp = *this; ++*this; return tmp;
72 /// getOperandNo - Return the operand number in the predecessor's
73 /// terminator of the successor.
74 unsigned getOperandNo() const {
75 return It.getOperandNo();
78 /// getUse - Return the operand Use in the predecessor's terminator
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;
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);
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);
99 inline bool pred_empty(const BasicBlock *BB) {
100 return pred_begin(BB) == pred_end(BB);
102 inline pred_range predecessors(BasicBlock *BB) {
103 return pred_range(pred_begin(BB), pred_end(BB));
105 inline pred_const_range predecessors(const BasicBlock *BB) {
106 return pred_const_range(pred_begin(BB), pred_end(BB));
109 //===----------------------------------------------------------------------===//
110 // BasicBlock succ_iterator definition
111 //===----------------------------------------------------------------------===//
113 template <class Term_, class BB_> // Successor Iterator
114 class SuccIterator : public std::iterator<std::random_access_iterator_tag, BB_,
116 typedef std::iterator<std::random_access_iterator_tag, BB_, int, BB_ *, BB_ *>
120 typedef typename super::pointer pointer;
121 typedef typename super::reference reference;
126 typedef SuccIterator<Term_, BB_> Self;
128 inline bool index_is_valid(int idx) {
129 return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
132 /// \brief Proxy object to allow write access in operator[]
133 class SuccessorProxy {
137 explicit SuccessorProxy(const Self &it) : it(it) {}
139 SuccessorProxy &operator=(SuccessorProxy r) {
140 *this = reference(r);
144 SuccessorProxy &operator=(reference r) {
145 it.Term->setSuccessor(it.idx, r);
149 operator reference() const { return *it; }
153 explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
155 inline SuccIterator(Term_ T, bool) // end iterator
158 idx = Term->getNumSuccessors();
160 // Term == NULL happens, if a basic block is not fully constructed and
161 // consequently getTerminator() returns NULL. In this case we construct a
162 // SuccIterator which describes a basic block that has zero successors.
163 // Defining SuccIterator for incomplete and malformed CFGs is especially
164 // useful for debugging.
168 inline const Self &operator=(const Self &I) {
169 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
174 /// getSuccessorIndex - This is used to interface between code that wants to
175 /// operate on terminator instructions directly.
176 unsigned getSuccessorIndex() const { return idx; }
178 inline bool operator==(const Self& x) const { return idx == x.idx; }
179 inline bool operator!=(const Self& x) const { return !operator==(x); }
181 inline reference operator*() const { return Term->getSuccessor(idx); }
182 inline pointer operator->() const { return operator*(); }
184 inline Self& operator++() { ++idx; return *this; } // Preincrement
186 inline Self operator++(int) { // Postincrement
187 Self tmp = *this; ++*this; return tmp;
190 inline Self& operator--() { --idx; return *this; } // Predecrement
191 inline Self operator--(int) { // Postdecrement
192 Self tmp = *this; --*this; return tmp;
195 inline bool operator<(const Self& x) const {
196 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
200 inline bool operator<=(const Self& x) const {
201 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
204 inline bool operator>=(const Self& x) const {
205 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
209 inline bool operator>(const Self& x) const {
210 assert(Term == x.Term && "Cannot compare iterators of different blocks!");
214 inline Self& operator+=(int Right) {
215 unsigned new_idx = idx + Right;
216 assert(index_is_valid(new_idx) && "Iterator index out of bound");
221 inline Self operator+(int Right) const {
227 inline Self& operator-=(int Right) {
228 return operator+=(-Right);
231 inline Self operator-(int Right) const {
232 return operator+(-Right);
235 inline int operator-(const Self& x) const {
236 assert(Term == x.Term && "Cannot work on iterators of different blocks!");
237 int distance = idx - x.idx;
241 inline SuccessorProxy operator[](int offset) {
244 return SuccessorProxy(tmp);
247 /// Get the source BB of this iterator.
248 inline BB_ *getSource() {
249 assert(Term && "Source not available, if basic block was malformed");
250 return Term->getParent();
254 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
255 typedef SuccIterator<const TerminatorInst*,
256 const BasicBlock> succ_const_iterator;
257 typedef llvm::iterator_range<succ_iterator> succ_range;
258 typedef llvm::iterator_range<succ_const_iterator> succ_const_range;
260 inline succ_iterator succ_begin(BasicBlock *BB) {
261 return succ_iterator(BB->getTerminator());
263 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
264 return succ_const_iterator(BB->getTerminator());
266 inline succ_iterator succ_end(BasicBlock *BB) {
267 return succ_iterator(BB->getTerminator(), true);
269 inline succ_const_iterator succ_end(const BasicBlock *BB) {
270 return succ_const_iterator(BB->getTerminator(), true);
272 inline bool succ_empty(const BasicBlock *BB) {
273 return succ_begin(BB) == succ_end(BB);
275 inline succ_range successors(BasicBlock *BB) {
276 return succ_range(succ_begin(BB), succ_end(BB));
278 inline succ_const_range successors(const BasicBlock *BB) {
279 return succ_const_range(succ_begin(BB), succ_end(BB));
283 template <typename T, typename U> struct isPodLike<SuccIterator<T, U> > {
284 static const bool value = isPodLike<T>::value;
289 //===--------------------------------------------------------------------===//
290 // GraphTraits specializations for basic block graphs (CFGs)
291 //===--------------------------------------------------------------------===//
293 // Provide specializations of GraphTraits to be able to treat a function as a
294 // graph of basic blocks...
296 template <> struct GraphTraits<BasicBlock*> {
297 typedef BasicBlock NodeType;
298 typedef succ_iterator ChildIteratorType;
300 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
301 static inline ChildIteratorType child_begin(NodeType *N) {
302 return succ_begin(N);
304 static inline ChildIteratorType child_end(NodeType *N) {
309 template <> struct GraphTraits<const BasicBlock*> {
310 typedef const BasicBlock NodeType;
311 typedef succ_const_iterator ChildIteratorType;
313 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
315 static inline ChildIteratorType child_begin(NodeType *N) {
316 return succ_begin(N);
318 static inline ChildIteratorType child_end(NodeType *N) {
323 // Provide specializations of GraphTraits to be able to treat a function as a
324 // graph of basic blocks... and to walk it in inverse order. Inverse order for
325 // a function is considered to be when traversing the predecessor edges of a BB
326 // instead of the successor edges.
328 template <> struct GraphTraits<Inverse<BasicBlock*> > {
329 typedef BasicBlock NodeType;
330 typedef pred_iterator ChildIteratorType;
331 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
332 static inline ChildIteratorType child_begin(NodeType *N) {
333 return pred_begin(N);
335 static inline ChildIteratorType child_end(NodeType *N) {
340 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
341 typedef const BasicBlock NodeType;
342 typedef const_pred_iterator ChildIteratorType;
343 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
346 static inline ChildIteratorType child_begin(NodeType *N) {
347 return pred_begin(N);
349 static inline ChildIteratorType child_end(NodeType *N) {
356 //===--------------------------------------------------------------------===//
357 // GraphTraits specializations for function basic block graphs (CFGs)
358 //===--------------------------------------------------------------------===//
360 // Provide specializations of GraphTraits to be able to treat a function as a
361 // graph of basic blocks... these are the same as the basic block iterators,
362 // except that the root node is implicitly the first node of the function.
364 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
365 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
367 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
368 typedef Function::iterator nodes_iterator;
369 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
370 static nodes_iterator nodes_end (Function *F) { return F->end(); }
371 static size_t size (Function *F) { return F->size(); }
373 template <> struct GraphTraits<const Function*> :
374 public GraphTraits<const BasicBlock*> {
375 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
377 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
378 typedef Function::const_iterator nodes_iterator;
379 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
380 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
381 static size_t size (const Function *F) { return F->size(); }
385 // Provide specializations of GraphTraits to be able to treat a function as a
386 // graph of basic blocks... and to walk it in inverse order. Inverse order for
387 // a function is considered to be when traversing the predecessor edges of a BB
388 // instead of the successor edges.
390 template <> struct GraphTraits<Inverse<Function*> > :
391 public GraphTraits<Inverse<BasicBlock*> > {
392 static NodeType *getEntryNode(Inverse<Function*> G) {
393 return &G.Graph->getEntryBlock();
396 template <> struct GraphTraits<Inverse<const Function*> > :
397 public GraphTraits<Inverse<const BasicBlock*> > {
398 static NodeType *getEntryNode(Inverse<const Function *> G) {
399 return &G.Graph->getEntryBlock();
403 } // End llvm namespace