LLVM  4.0.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"
19 #include "llvm/ADT/iterator.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Support/Casting.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <iterator>
30 
31 namespace llvm {
32 
33 //===----------------------------------------------------------------------===//
34 // BasicBlock pred_iterator definition
35 //===----------------------------------------------------------------------===//
36 
37 template <class Ptr, class USE_iterator> // Predecessor Iterator
38 class PredIterator : public std::iterator<std::forward_iterator_tag,
39  Ptr, ptrdiff_t, Ptr*, Ptr*> {
40  typedef std::iterator<std::forward_iterator_tag, Ptr, ptrdiff_t, Ptr*,
41  Ptr*> super;
43  USE_iterator It;
44 
45  inline void advancePastNonTerminators() {
46  // Loop to ignore non-terminator uses (for example BlockAddresses).
47  while (!It.atEnd() && !isa<TerminatorInst>(*It))
48  ++It;
49  }
50 
51 public:
52  typedef typename super::pointer pointer;
53  typedef typename super::reference reference;
54 
55  PredIterator() = default;
56  explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
57  advancePastNonTerminators();
58  }
59  inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
60 
61  inline bool operator==(const Self& x) const { return It == x.It; }
62  inline bool operator!=(const Self& x) const { return !operator==(x); }
63 
64  inline reference operator*() const {
65  assert(!It.atEnd() && "pred_iterator out of range!");
66  return cast<TerminatorInst>(*It)->getParent();
67  }
68  inline pointer *operator->() const { return &operator*(); }
69 
70  inline Self& operator++() { // Preincrement
71  assert(!It.atEnd() && "pred_iterator out of range!");
72  ++It; advancePastNonTerminators();
73  return *this;
74  }
75 
76  inline Self operator++(int) { // Postincrement
77  Self tmp = *this; ++*this; return tmp;
78  }
79 
80  /// getOperandNo - Return the operand number in the predecessor's
81  /// terminator of the successor.
82  unsigned getOperandNo() const {
83  return It.getOperandNo();
84  }
85 
86  /// getUse - Return the operand Use in the predecessor's terminator
87  /// of the successor.
88  Use &getUse() const {
89  return It.getUse();
90  }
91 };
92 
94 typedef PredIterator<const BasicBlock,
98 
99 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
101  return const_pred_iterator(BB);
102 }
103 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
105  return const_pred_iterator(BB, true);
106 }
107 inline bool pred_empty(const BasicBlock *BB) {
108  return pred_begin(BB) == pred_end(BB);
109 }
111  return pred_range(pred_begin(BB), pred_end(BB));
112 }
114  return pred_const_range(pred_begin(BB), pred_end(BB));
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // BasicBlock succ_iterator helpers
119 //===----------------------------------------------------------------------===//
120 
121 typedef TerminatorInst::SuccIterator<TerminatorInst *, BasicBlock>
127 
129  return succ_iterator(BB->getTerminator());
130 }
132  return succ_const_iterator(BB->getTerminator());
133 }
135  return succ_iterator(BB->getTerminator(), true);
136 }
138  return succ_const_iterator(BB->getTerminator(), true);
139 }
140 inline bool succ_empty(const BasicBlock *BB) {
141  return succ_begin(BB) == succ_end(BB);
142 }
144  return succ_range(succ_begin(BB), succ_end(BB));
145 }
147  return succ_const_range(succ_begin(BB), succ_end(BB));
148 }
149 
150 template <typename T, typename U>
151 struct isPodLike<TerminatorInst::SuccIterator<T, U>> {
152  static const bool value = isPodLike<T>::value;
153 };
154 
155 //===--------------------------------------------------------------------===//
156 // GraphTraits specializations for basic block graphs (CFGs)
157 //===--------------------------------------------------------------------===//
158 
159 // Provide specializations of GraphTraits to be able to treat a function as a
160 // graph of basic blocks...
161 
162 template <> struct GraphTraits<BasicBlock*> {
163  typedef BasicBlock *NodeRef;
165 
166  static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
167  static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
168  static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
169 };
170 
171 template <> struct GraphTraits<const BasicBlock*> {
172  typedef const BasicBlock *NodeRef;
174 
175  static NodeRef getEntryNode(const BasicBlock *BB) { return BB; }
176 
177  static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
178  static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
179 };
180 
181 // Provide specializations of GraphTraits to be able to treat a function as a
182 // graph of basic blocks... and to walk it in inverse order. Inverse order for
183 // a function is considered to be when traversing the predecessor edges of a BB
184 // instead of the successor edges.
185 //
186 template <> struct GraphTraits<Inverse<BasicBlock*>> {
187  typedef BasicBlock *NodeRef;
189  static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
190  static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
191  static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
192 };
193 
194 template <> struct GraphTraits<Inverse<const BasicBlock*>> {
195  typedef const BasicBlock *NodeRef;
197  static NodeRef getEntryNode(Inverse<const BasicBlock *> G) { return G.Graph; }
198  static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
199  static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
200 };
201 
202 //===--------------------------------------------------------------------===//
203 // GraphTraits specializations for function basic block graphs (CFGs)
204 //===--------------------------------------------------------------------===//
205 
206 // Provide specializations of GraphTraits to be able to treat a function as a
207 // graph of basic blocks... these are the same as the basic block iterators,
208 // except that the root node is implicitly the first node of the function.
209 //
210 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
211  static NodeRef getEntryNode(Function *F) { return &F->getEntryBlock(); }
212 
213  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
215 
216  static nodes_iterator nodes_begin(Function *F) {
217  return nodes_iterator(F->begin());
218  }
219 
220  static nodes_iterator nodes_end(Function *F) {
221  return nodes_iterator(F->end());
222  }
223 
224  static size_t size(Function *F) { return F->size(); }
225 };
226 template <> struct GraphTraits<const Function*> :
228  static NodeRef getEntryNode(const Function *F) { return &F->getEntryBlock(); }
229 
230  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
232 
233  static nodes_iterator nodes_begin(const Function *F) {
234  return nodes_iterator(F->begin());
235  }
236 
237  static nodes_iterator nodes_end(const Function *F) {
238  return nodes_iterator(F->end());
239  }
240 
241  static size_t size(const Function *F) { return F->size(); }
242 };
243 
244 // Provide specializations of GraphTraits to be able to treat a function as a
245 // graph of basic blocks... and to walk it in inverse order. Inverse order for
246 // a function is considered to be when traversing the predecessor edges of a BB
247 // instead of the successor edges.
248 //
249 template <> struct GraphTraits<Inverse<Function*>> :
252  return &G.Graph->getEntryBlock();
253  }
254 };
255 template <> struct GraphTraits<Inverse<const Function*>> :
258  return &G.Graph->getEntryBlock();
259  }
260 };
261 
262 } // end namespace llvm
263 
264 #endif // LLVM_IR_CFG_H
iterator_range< succ_const_iterator > succ_const_range
Definition: IR/CFG.h:126
static NodeRef getEntryNode(const BasicBlock *BB)
Definition: IR/CFG.h:175
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
Definition: IR/CFG.h:95
Various leaf nodes.
Definition: ISDOpcodes.h:60
iterator end()
Definition: Function.h:537
static NodeRef getEntryNode(Inverse< const Function * > G)
Definition: IR/CFG.h:257
static NodeRef getEntryNode(BasicBlock *BB)
Definition: IR/CFG.h:166
bool operator==(const Self &x) const
Definition: IR/CFG.h:61
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static ChildIteratorType child_begin(NodeRef N)
Definition: IR/CFG.h:177
pointer_iterator< Function::iterator > nodes_iterator
Definition: IR/CFG.h:214
static ChildIteratorType child_end(NodeRef N)
Definition: IR/CFG.h:191
static const bool value
Definition: type_traits.h:48
PredIterator(Ptr *bb, bool)
Definition: IR/CFG.h:59
Use & getUse() const
getUse - Return the operand Use in the predecessor's terminator of the successor. ...
Definition: IR/CFG.h:88
static ChildIteratorType child_end(NodeRef N)
Definition: IR/CFG.h:199
iterator_range< pred_iterator > pred_range
Definition: IR/CFG.h:96
super::pointer pointer
Definition: IR/CFG.h:52
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
succ_const_iterator ChildIteratorType
Definition: IR/CFG.h:173
static NodeRef getEntryNode(Inverse< BasicBlock * > G)
Definition: IR/CFG.h:189
super::reference reference
Definition: IR/CFG.h:53
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:106
succ_iterator ChildIteratorType
Definition: IR/CFG.h:164
static ChildIteratorType child_begin(NodeRef N)
Definition: IR/CFG.h:198
#define F(x, y, z)
Definition: MD5.cpp:51
unsigned getOperandNo() const
getOperandNo - Return the operand number in the predecessor's terminator of the successor.
Definition: IR/CFG.h:82
iterator begin()
Definition: Function.h:535
static ChildIteratorType child_end(NodeRef N)
Definition: IR/CFG.h:178
static ChildIteratorType child_begin(NodeRef N)
Definition: IR/CFG.h:167
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:109
static nodes_iterator nodes_begin(const Function *F)
Definition: IR/CFG.h:233
reference operator*() const
Definition: IR/CFG.h:64
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:52
TerminatorInst::SuccIterator< const TerminatorInst *, const BasicBlock > succ_const_iterator
Definition: IR/CFG.h:124
static NodeRef getEntryNode(Inverse< const BasicBlock * > G)
Definition: IR/CFG.h:197
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
Self & operator++()
Definition: IR/CFG.h:70
static NodeRef getEntryNode(Inverse< Function * > G)
Definition: IR/CFG.h:251
bool succ_empty(const BasicBlock *BB)
Definition: IR/CFG.h:140
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:116
size_t size() const
Definition: Function.h:540
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:119
static ChildIteratorType child_end(NodeRef N)
Definition: IR/CFG.h:168
bool pred_empty(const BasicBlock *BB)
Definition: IR/CFG.h:107
iterator_range< const_pred_iterator > pred_const_range
Definition: IR/CFG.h:97
static nodes_iterator nodes_end(const Function *F)
Definition: IR/CFG.h:237
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:507
const GraphType & Graph
Definition: GraphTraits.h:80
const DataFlowGraph & G
Definition: RDFGraph.cpp:206
static NodeRef getEntryNode(Function *F)
Definition: IR/CFG.h:211
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:110
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
A range adaptor for a pair of iterators.
PredIterator(Ptr *bb)
Definition: IR/CFG.h:56
static size_t size(const Function *F)
Definition: IR/CFG.h:241
static NodeRef getEntryNode(const Function *F)
Definition: IR/CFG.h:228
TerminatorInst::SuccIterator< TerminatorInst *, BasicBlock > succ_iterator
Definition: IR/CFG.h:122
static ChildIteratorType child_begin(NodeRef N)
Definition: IR/CFG.h:190
pointer_iterator< Function::const_iterator > nodes_iterator
Definition: IR/CFG.h:231
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:341
bool operator!=(const Self &x) const
Definition: IR/CFG.h:62
#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
PredIterator< BasicBlock, Value::user_iterator > pred_iterator
Definition: IR/CFG.h:93
static nodes_iterator nodes_end(Function *F)
Definition: IR/CFG.h:220
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
PredIterator()=default
iterator_range< succ_iterator > succ_range
Definition: IR/CFG.h:125
aarch64 promote const
static nodes_iterator nodes_begin(Function *F)
Definition: IR/CFG.h:216
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:143
Self operator++(int)
Definition: IR/CFG.h:76
static size_t size(Function *F)
Definition: IR/CFG.h:224
pointer * operator->() const
Definition: IR/CFG.h:68
int * Ptr