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