LLVM  3.7.0
IR/CFG.h
Go to the documentation of this file.
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"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/InstrTypes.h"
22 
23 namespace llvm {
24 
25 //===----------------------------------------------------------------------===//
26 // BasicBlock pred_iterator definition
27 //===----------------------------------------------------------------------===//
28 
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*,
33  Ptr*> super;
35  USE_iterator It;
36 
37  inline void advancePastNonTerminators() {
38  // Loop to ignore non-terminator uses (for example BlockAddresses).
39  while (!It.atEnd() && !isa<TerminatorInst>(*It))
40  ++It;
41  }
42 
43 public:
44  typedef typename super::pointer pointer;
45  typedef typename super::reference reference;
46 
48  explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
49  advancePastNonTerminators();
50  }
51  inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
52 
53  inline bool operator==(const Self& x) const { return It == x.It; }
54  inline bool operator!=(const Self& x) const { return !operator==(x); }
55 
56  inline reference operator*() const {
57  assert(!It.atEnd() && "pred_iterator out of range!");
58  return cast<TerminatorInst>(*It)->getParent();
59  }
60  inline pointer *operator->() const { return &operator*(); }
61 
62  inline Self& operator++() { // Preincrement
63  assert(!It.atEnd() && "pred_iterator out of range!");
64  ++It; advancePastNonTerminators();
65  return *this;
66  }
67 
68  inline Self operator++(int) { // Postincrement
69  Self tmp = *this; ++*this; return tmp;
70  }
71 
72  /// getOperandNo - Return the operand number in the predecessor's
73  /// terminator of the successor.
74  unsigned getOperandNo() const {
75  return It.getOperandNo();
76  }
77 
78  /// getUse - Return the operand Use in the predecessor's terminator
79  /// of the successor.
80  Use &getUse() const {
81  return It.getUse();
82  }
83 };
84 
86 typedef PredIterator<const BasicBlock,
90 
91 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
93  return const_pred_iterator(BB);
94 }
95 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
97  return const_pred_iterator(BB, true);
98 }
99 inline bool pred_empty(const BasicBlock *BB) {
100  return pred_begin(BB) == pred_end(BB);
101 }
103  return pred_range(pred_begin(BB), pred_end(BB));
104 }
106  return pred_const_range(pred_begin(BB), pred_end(BB));
107 }
108 
109 //===----------------------------------------------------------------------===//
110 // BasicBlock succ_iterator definition
111 //===----------------------------------------------------------------------===//
112 
113 template <class Term_, class BB_> // Successor Iterator
114 class SuccIterator : public std::iterator<std::random_access_iterator_tag, BB_,
115  int, BB_ *, BB_ *> {
116  typedef std::iterator<std::random_access_iterator_tag, BB_, int, BB_ *, BB_ *>
117  super;
118 
119 public:
120  typedef typename super::pointer pointer;
121  typedef typename super::reference reference;
122 
123 private:
124  Term_ Term;
125  unsigned idx;
126  typedef SuccIterator<Term_, BB_> Self;
127 
128  inline bool index_is_valid(int idx) {
129  return idx >= 0 && (unsigned) idx < Term->getNumSuccessors();
130  }
131 
132  /// \brief Proxy object to allow write access in operator[]
133  class SuccessorProxy {
134  Self it;
135 
136  public:
137  explicit SuccessorProxy(const Self &it) : it(it) {}
138 
139  SuccessorProxy(const SuccessorProxy&) = default;
140 
141  SuccessorProxy &operator=(SuccessorProxy r) {
142  *this = reference(r);
143  return *this;
144  }
145 
146  SuccessorProxy &operator=(reference r) {
147  it.Term->setSuccessor(it.idx, r);
148  return *this;
149  }
150 
151  operator reference() const { return *it; }
152  };
153 
154 public:
155  explicit inline SuccIterator(Term_ T) : Term(T), idx(0) {// begin iterator
156  }
157  inline SuccIterator(Term_ T, bool) // end iterator
158  : Term(T) {
159  if (Term)
160  idx = Term->getNumSuccessors();
161  else
162  // Term == NULL happens, if a basic block is not fully constructed and
163  // consequently getTerminator() returns NULL. In this case we construct a
164  // SuccIterator which describes a basic block that has zero successors.
165  // Defining SuccIterator for incomplete and malformed CFGs is especially
166  // useful for debugging.
167  idx = 0;
168  }
169 
170  /// getSuccessorIndex - This is used to interface between code that wants to
171  /// operate on terminator instructions directly.
172  unsigned getSuccessorIndex() const { return idx; }
173 
174  inline bool operator==(const Self& x) const { return idx == x.idx; }
175  inline bool operator!=(const Self& x) const { return !operator==(x); }
176 
177  inline reference operator*() const { return Term->getSuccessor(idx); }
178  inline pointer operator->() const { return operator*(); }
179 
180  inline Self& operator++() { ++idx; return *this; } // Preincrement
181 
182  inline Self operator++(int) { // Postincrement
183  Self tmp = *this; ++*this; return tmp;
184  }
185 
186  inline Self& operator--() { --idx; return *this; } // Predecrement
187  inline Self operator--(int) { // Postdecrement
188  Self tmp = *this; --*this; return tmp;
189  }
190 
191  inline bool operator<(const Self& x) const {
192  assert(Term == x.Term && "Cannot compare iterators of different blocks!");
193  return idx < x.idx;
194  }
195 
196  inline bool operator<=(const Self& x) const {
197  assert(Term == x.Term && "Cannot compare iterators of different blocks!");
198  return idx <= x.idx;
199  }
200  inline bool operator>=(const Self& x) const {
201  assert(Term == x.Term && "Cannot compare iterators of different blocks!");
202  return idx >= x.idx;
203  }
204 
205  inline bool operator>(const Self& x) const {
206  assert(Term == x.Term && "Cannot compare iterators of different blocks!");
207  return idx > x.idx;
208  }
209 
210  inline Self& operator+=(int Right) {
211  unsigned new_idx = idx + Right;
212  assert(index_is_valid(new_idx) && "Iterator index out of bound");
213  idx = new_idx;
214  return *this;
215  }
216 
217  inline Self operator+(int Right) const {
218  Self tmp = *this;
219  tmp += Right;
220  return tmp;
221  }
222 
223  inline Self& operator-=(int Right) {
224  return operator+=(-Right);
225  }
226 
227  inline Self operator-(int Right) const {
228  return operator+(-Right);
229  }
230 
231  inline int operator-(const Self& x) const {
232  assert(Term == x.Term && "Cannot work on iterators of different blocks!");
233  int distance = idx - x.idx;
234  return distance;
235  }
236 
237  inline SuccessorProxy operator[](int offset) {
238  Self tmp = *this;
239  tmp += offset;
240  return SuccessorProxy(tmp);
241  }
242 
243  /// Get the source BB of this iterator.
244  inline BB_ *getSource() {
245  assert(Term && "Source not available, if basic block was malformed");
246  return Term->getParent();
247  }
248 };
249 
251 typedef SuccIterator<const TerminatorInst*,
255 
257  return succ_iterator(BB->getTerminator());
258 }
260  return succ_const_iterator(BB->getTerminator());
261 }
263  return succ_iterator(BB->getTerminator(), true);
264 }
266  return succ_const_iterator(BB->getTerminator(), true);
267 }
268 inline bool succ_empty(const BasicBlock *BB) {
269  return succ_begin(BB) == succ_end(BB);
270 }
272  return succ_range(succ_begin(BB), succ_end(BB));
273 }
275  return succ_const_range(succ_begin(BB), succ_end(BB));
276 }
277 
278 
279 template <typename T, typename U> struct isPodLike<SuccIterator<T, U> > {
280  static const bool value = isPodLike<T>::value;
281 };
282 
283 
284 
285 //===--------------------------------------------------------------------===//
286 // GraphTraits specializations for 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...
291 
292 template <> struct GraphTraits<BasicBlock*> {
295 
296  static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
297  static inline ChildIteratorType child_begin(NodeType *N) {
298  return succ_begin(N);
299  }
300  static inline ChildIteratorType child_end(NodeType *N) {
301  return succ_end(N);
302  }
303 };
304 
305 template <> struct GraphTraits<const BasicBlock*> {
306  typedef const BasicBlock NodeType;
308 
309  static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
310 
311  static inline ChildIteratorType child_begin(NodeType *N) {
312  return succ_begin(N);
313  }
314  static inline ChildIteratorType child_end(NodeType *N) {
315  return succ_end(N);
316  }
317 };
318 
319 // Provide specializations of GraphTraits to be able to treat a function as a
320 // graph of basic blocks... and to walk it in inverse order. Inverse order for
321 // a function is considered to be when traversing the predecessor edges of a BB
322 // instead of the successor edges.
323 //
324 template <> struct GraphTraits<Inverse<BasicBlock*> > {
327  static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
328  static inline ChildIteratorType child_begin(NodeType *N) {
329  return pred_begin(N);
330  }
331  static inline ChildIteratorType child_end(NodeType *N) {
332  return pred_end(N);
333  }
334 };
335 
336 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
337  typedef const BasicBlock NodeType;
340  return G.Graph;
341  }
342  static inline ChildIteratorType child_begin(NodeType *N) {
343  return pred_begin(N);
344  }
345  static inline ChildIteratorType child_end(NodeType *N) {
346  return pred_end(N);
347  }
348 };
349 
350 
351 
352 //===--------------------------------------------------------------------===//
353 // GraphTraits specializations for function basic block graphs (CFGs)
354 //===--------------------------------------------------------------------===//
355 
356 // Provide specializations of GraphTraits to be able to treat a function as a
357 // graph of basic blocks... these are the same as the basic block iterators,
358 // except that the root node is implicitly the first node of the function.
359 //
360 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
361  static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
362 
363  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
365  static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
366  static nodes_iterator nodes_end (Function *F) { return F->end(); }
367  static size_t size (Function *F) { return F->size(); }
368 };
369 template <> struct GraphTraits<const Function*> :
371  static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
372 
373  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
375  static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
376  static nodes_iterator nodes_end (const Function *F) { return F->end(); }
377  static size_t size (const Function *F) { return F->size(); }
378 };
379 
380 
381 // Provide specializations of GraphTraits to be able to treat a function as a
382 // graph of basic blocks... and to walk it in inverse order. Inverse order for
383 // a function is considered to be when traversing the predecessor edges of a BB
384 // instead of the successor edges.
385 //
386 template <> struct GraphTraits<Inverse<Function*> > :
389  return &G.Graph->getEntryBlock();
390  }
391 };
392 template <> struct GraphTraits<Inverse<const Function*> > :
395  return &G.Graph->getEntryBlock();
396  }
397 };
398 
399 } // End llvm namespace
400 
401 #endif
static NodeType * getEntryNode(Inverse< Function * > G)
Definition: IR/CFG.h:388
SuccIterator< const TerminatorInst *, const BasicBlock > succ_const_iterator
Definition: IR/CFG.h:252
Self & operator++()
Definition: IR/CFG.h:180
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
Definition: IR/CFG.h:87
Various leaf nodes.
Definition: ISDOpcodes.h:60
bool operator==(const Self &x) const
Definition: IR/CFG.h:174
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
iterator end()
Definition: Function.h:459
Self operator--(int)
Definition: IR/CFG.h:187
bool operator==(const Self &x) const
Definition: IR/CFG.h:53
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static ChildIteratorType child_begin(NodeType *N)
Definition: IR/CFG.h:311
bool operator!=(const Self &x) const
Definition: IR/CFG.h:175
SuccIterator(Term_ T)
Definition: IR/CFG.h:155
static ChildIteratorType child_end(NodeType *N)
Definition: IR/CFG.h:331
static const bool value
Definition: type_traits.h:46
F(f)
llvm::iterator_range< pred_iterator > pred_range
Definition: IR/CFG.h:88
PredIterator(Ptr *bb, bool)
Definition: IR/CFG.h:51
Use & getUse() const
getUse - Return the operand Use in the predecessor's terminator of the successor. ...
Definition: IR/CFG.h:80
llvm::iterator_range< succ_const_iterator > succ_const_range
Definition: IR/CFG.h:254
static ChildIteratorType child_begin(NodeType *N)
Definition: IR/CFG.h:328
static ChildIteratorType child_end(NodeType *N)
Definition: IR/CFG.h:345
super::pointer pointer
Definition: IR/CFG.h:44
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
static NodeType * getEntryNode(const BasicBlock *BB)
Definition: IR/CFG.h:309
bool operator>=(const Self &x) const
Definition: IR/CFG.h:200
succ_const_iterator ChildIteratorType
Definition: IR/CFG.h:307
super::reference reference
Definition: IR/CFG.h:45
#define G(x, y, z)
Definition: MD5.cpp:52
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
succ_iterator ChildIteratorType
Definition: IR/CFG.h:294
static NodeType * getEntryNode(BasicBlock *BB)
Definition: IR/CFG.h:296
bool operator<(const Self &x) const
Definition: IR/CFG.h:191
unsigned getOperandNo() const
getOperandNo - Return the operand number in the predecessor's terminator of the successor.
Definition: IR/CFG.h:74
bool operator>(const Self &x) const
Definition: IR/CFG.h:205
llvm::iterator_range< succ_iterator > succ_range
Definition: IR/CFG.h:253
iterator begin()
Definition: Function.h:457
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
static nodes_iterator nodes_begin(const Function *F)
Definition: IR/CFG.h:375
reference operator*() const
Definition: IR/CFG.h:56
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
SuccIterator< TerminatorInst *, BasicBlock > succ_iterator
Definition: IR/CFG.h:250
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
Self & operator++()
Definition: IR/CFG.h:62
bool succ_empty(const BasicBlock *BB)
Definition: IR/CFG.h:268
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:114
size_t size() const
Definition: Function.h:462
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
bool pred_empty(const BasicBlock *BB)
Definition: IR/CFG.h:99
Self operator++(int)
Definition: IR/CFG.h:182
bool operator<=(const Self &x) const
Definition: IR/CFG.h:196
int operator-(const Self &x) const
Definition: IR/CFG.h:231
static nodes_iterator nodes_end(const Function *F)
Definition: IR/CFG.h:376
Self & operator--()
Definition: IR/CFG.h:186
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:365
static ChildIteratorType child_end(NodeType *N)
Definition: IR/CFG.h:314
static ChildIteratorType child_begin(NodeType *N)
Definition: IR/CFG.h:342
const GraphType & Graph
Definition: GraphTraits.h:79
unsigned getSuccessorIndex() const
getSuccessorIndex - This is used to interface between code that wants to operate on terminator instru...
Definition: IR/CFG.h:172
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:102
static NodeType * getEntryNode(Function *F)
Definition: IR/CFG.h:361
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
reference operator*() const
Definition: IR/CFG.h:177
BB_ * getSource()
Get the source BB of this iterator.
Definition: IR/CFG.h:244
static ChildIteratorType child_begin(NodeType *N)
Definition: IR/CFG.h:297
A range adaptor for a pair of iterators.
PredIterator(Ptr *bb)
Definition: IR/CFG.h:48
super::reference reference
Definition: IR/CFG.h:121
static size_t size(const Function *F)
Definition: IR/CFG.h:377
llvm::iterator_range< const_pred_iterator > pred_const_range
Definition: IR/CFG.h:89
pointer operator->() const
Definition: IR/CFG.h:178
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:293
SuccIterator(Term_ T, bool)
Definition: IR/CFG.h:157
bool operator!=(const Self &x) const
Definition: IR/CFG.h:54
Function::iterator nodes_iterator
Definition: IR/CFG.h:364
static NodeType * getEntryNode(const Function *F)
Definition: IR/CFG.h:371
super::pointer pointer
Definition: IR/CFG.h:120
#define N
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
SuccessorProxy operator[](int offset)
Definition: IR/CFG.h:237
PredIterator< BasicBlock, Value::user_iterator > pred_iterator
Definition: IR/CFG.h:85
static NodeType * getEntryNode(Inverse< BasicBlock * > G)
Definition: IR/CFG.h:327
static nodes_iterator nodes_end(Function *F)
Definition: IR/CFG.h:366
static ChildIteratorType child_end(NodeType *N)
Definition: IR/CFG.h:300
Self operator-(int Right) const
Definition: IR/CFG.h:227
Self & operator+=(int Right)
Definition: IR/CFG.h:210
aarch64 promote const
static nodes_iterator nodes_begin(Function *F)
Definition: IR/CFG.h:365
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:271
static NodeType * getEntryNode(Inverse< const BasicBlock * > G)
Definition: IR/CFG.h:339
static NodeType * getEntryNode(Inverse< const Function * > G)
Definition: IR/CFG.h:394
Self operator++(int)
Definition: IR/CFG.h:68
Self & operator-=(int Right)
Definition: IR/CFG.h:223
Function::const_iterator nodes_iterator
Definition: IR/CFG.h:374
static size_t size(Function *F)
Definition: IR/CFG.h:367
pointer * operator->() const
Definition: IR/CFG.h:60
Self operator+(int Right) const
Definition: IR/CFG.h:217