LLVM 23.0.0git
CFG.h
Go to the documentation of this file.
1//===- CFG.h ----------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file provides various utilities for inspecting and working with the
11/// control flow graph in LLVM IR. This includes generic facilities for
12/// iterating successors and predecessors of basic blocks, the successors of
13/// specific terminator instructions, etc. It also defines specializations of
14/// GraphTraits that allow Function and BasicBlock graphs to be treated as
15/// proper graphs for generic algorithms.
16///
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_IR_CFG_H
20#define LLVM_IR_CFG_H
21
23#include "llvm/ADT/iterator.h"
25#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/Value.h"
28#include <cassert>
29#include <cstddef>
30#include <iterator>
31
32namespace llvm {
33
34class Instruction;
35class Use;
36
37//===----------------------------------------------------------------------===//
38// BasicBlock pred_iterator definition
39//===----------------------------------------------------------------------===//
40
41template <class Ptr, class USE_iterator> // Predecessor Iterator
43public:
44 using iterator_category = std::forward_iterator_tag;
45 using value_type = Ptr *;
46 using difference_type = std::ptrdiff_t;
47 using pointer = Ptr **;
48 using reference = Ptr *;
49
50protected:
52 USE_iterator It;
53
55 // Loop to ignore non-terminator uses (for example BlockAddresses).
56 while (!It.atEnd()) {
57 if (auto *Inst = dyn_cast<Instruction>(*It)) {
58 assert(Inst->isTerminator() && "BasicBlock used in non-terminator");
59 break;
60 }
61
62 ++It;
63 }
64 }
65
66public:
67 PredIterator() = default;
68 explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
70 }
71 inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
72
73 inline bool operator==(const Self& x) const { return It == x.It; }
74 inline bool operator!=(const Self& x) const { return !operator==(x); }
75
76 inline reference operator*() const {
77 assert(!It.atEnd() && "pred_iterator out of range!");
78 return cast<Instruction>(*It)->getParent();
79 }
80 inline pointer *operator->() const { return &operator*(); }
81
82 inline Self& operator++() { // Preincrement
83 assert(!It.atEnd() && "pred_iterator out of range!");
85 return *this;
86 }
87
88 inline Self operator++(int) { // Postincrement
89 Self tmp = *this; ++*this; return tmp;
90 }
91
92 /// getOperandNo - Return the operand number in the predecessor's
93 /// terminator of the successor.
94 unsigned getOperandNo() const {
95 return It.getOperandNo();
96 }
97
98 /// getUse - Return the operand Use in the predecessor's terminator
99 /// of the successor.
100 Use &getUse() const {
101 return It.getUse();
102 }
103};
104
110
113 return const_pred_iterator(BB);
114}
115inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
117 return const_pred_iterator(BB, true);
118}
119inline bool pred_empty(const BasicBlock *BB) {
120 return pred_begin(BB) == pred_end(BB);
121}
122/// Get the number of predecessors of \p BB. This is a linear time operation.
123/// Use \ref BasicBlock::hasNPredecessors() or hasNPredecessorsOrMore if able.
124inline unsigned pred_size(const BasicBlock *BB) {
125 return std::distance(pred_begin(BB), pred_end(BB));
126}
128 return pred_range(pred_begin(BB), pred_end(BB));
129}
131 return const_pred_range(pred_begin(BB), pred_end(BB));
132}
133
134//===----------------------------------------------------------------------===//
135// Instruction and BasicBlock succ_iterator helpers
136//===----------------------------------------------------------------------===//
137
142
144 return I->successors().begin();
145}
147 return I->successors().begin();
148}
149inline succ_iterator succ_end(Instruction *I) { return I->successors().end(); }
151 return I->successors().end();
152}
153inline bool succ_empty(const Instruction *I) {
154 return succ_begin(I) == succ_end(I);
155}
156inline unsigned succ_size(const Instruction *I) {
157 return std::distance(succ_begin(I), succ_end(I));
158}
159inline succ_range successors(Instruction *I) { return I->successors(); }
161 return I->successors();
162}
163
165 return succ_begin(BB->getTerminator());
166}
168 return succ_begin(BB->getTerminator());
169}
171 return succ_end(BB->getTerminator());
172}
174 return succ_end(BB->getTerminator());
175}
176inline bool succ_empty(const BasicBlock *BB) {
177 return succ_begin(BB) == succ_end(BB);
178}
179inline unsigned succ_size(const BasicBlock *BB) {
180 return std::distance(succ_begin(BB), succ_end(BB));
181}
183 return successors(BB->getTerminator());
184}
186 return successors(BB->getTerminator());
187}
188
189//===--------------------------------------------------------------------===//
190// GraphTraits specializations for basic block graphs (CFGs)
191//===--------------------------------------------------------------------===//
192
193// Provide specializations of GraphTraits to be able to treat a function as a
194// graph of basic blocks...
195
196template <> struct GraphTraits<BasicBlock*> {
199
200 static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
203
204 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
205};
206
208 "GraphTraits getNumber() not detected");
209
210template <> struct GraphTraits<const BasicBlock*> {
211 using NodeRef = const BasicBlock *;
213
214 static NodeRef getEntryNode(const BasicBlock *BB) { return BB; }
215
218
219 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
220};
221
223 "GraphTraits getNumber() not detected");
224
225// Provide specializations of GraphTraits to be able to treat a function as a
226// graph of basic blocks... and to walk it in inverse order. Inverse order for
227// a function is considered to be when traversing the predecessor edges of a BB
228// instead of the successor edges.
229//
230template <> struct GraphTraits<Inverse<BasicBlock*>> {
233
234 static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
237
238 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
239};
240
242 "GraphTraits getNumber() not detected");
243
244template <> struct GraphTraits<Inverse<const BasicBlock*>> {
245 using NodeRef = const BasicBlock *;
247
251
252 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
253};
254
256 "GraphTraits getNumber() not detected");
257
258//===--------------------------------------------------------------------===//
259// GraphTraits specializations for function basic block graphs (CFGs)
260//===--------------------------------------------------------------------===//
261
262// Provide specializations of GraphTraits to be able to treat a function as a
263// graph of basic blocks... these are the same as the basic block iterators,
264// except that the root node is implicitly the first node of the function.
265//
266template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
267 static NodeRef getEntryNode(Function *F) { return &F->getEntryBlock(); }
268
269 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
271
273 return nodes_iterator(F->begin());
274 }
275
277 return nodes_iterator(F->end());
278 }
279
280 static size_t size(Function *F) { return F->size(); }
281
282 static unsigned getMaxNumber(const Function *F) {
283 return F->getMaxBlockNumber();
284 }
285 static unsigned getNumberEpoch(const Function *F) {
286 return F->getBlockNumberEpoch();
287 }
288};
289template <> struct GraphTraits<const Function*> :
291 static NodeRef getEntryNode(const Function *F) { return &F->getEntryBlock(); }
292
293 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
295
297 return nodes_iterator(F->begin());
298 }
299
301 return nodes_iterator(F->end());
302 }
303
304 static size_t size(const Function *F) { return F->size(); }
305
306 static unsigned getMaxNumber(const Function *F) {
307 return F->getMaxBlockNumber();
308 }
309 static unsigned getNumberEpoch(const Function *F) {
310 return F->getBlockNumberEpoch();
311 }
312};
313
314// Provide specializations of GraphTraits to be able to treat a function as a
315// graph of basic blocks... and to walk it in inverse order. Inverse order for
316// a function is considered to be when traversing the predecessor edges of a BB
317// instead of the successor edges.
318//
319template <> struct GraphTraits<Inverse<Function*>> :
322 return &G.Graph->getEntryBlock();
323 }
324
325 static unsigned getMaxNumber(const Function *F) {
326 return F->getMaxBlockNumber();
327 }
328 static unsigned getNumberEpoch(const Function *F) {
329 return F->getBlockNumberEpoch();
330 }
331};
332template <> struct GraphTraits<Inverse<const Function*>> :
335 return &G.Graph->getEntryBlock();
336 }
337
338 static unsigned getMaxNumber(const Function *F) {
339 return F->getMaxBlockNumber();
340 }
341 static unsigned getNumberEpoch(const Function *F) {
342 return F->getBlockNumberEpoch();
343 }
344};
345
346} // end namespace llvm
347
348#endif // LLVM_IR_CFG_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
LLVM Basic Block Representation.
Definition BasicBlock.h:62
unsigned getNumber() const
Definition BasicBlock.h:95
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
bool operator==(const Self &x) const
Definition CFG.h:73
Self & operator++()
Definition CFG.h:82
PredIterator(Ptr *bb)
Definition CFG.h:68
void advancePastNonTerminators()
Definition CFG.h:54
PredIterator()=default
Self operator++(int)
Definition CFG.h:88
PredIterator(Ptr *bb, bool)
Definition CFG.h:71
bool operator!=(const Self &x) const
Definition CFG.h:74
Ptr ** pointer
Definition CFG.h:47
std::forward_iterator_tag iterator_category
Definition CFG.h:44
reference operator*() const
Definition CFG.h:76
PredIterator< Ptr, USE_iterator > Self
Definition CFG.h:51
std::ptrdiff_t difference_type
Definition CFG.h:46
pointer * operator->() const
Definition CFG.h:80
unsigned getOperandNo() const
getOperandNo - Return the operand number in the predecessor's terminator of the successor.
Definition CFG.h:94
Ptr * value_type
Definition CFG.h:45
Use & getUse() const
getUse - Return the operand Use in the predecessor's terminator of the successor.
Definition CFG.h:100
Ptr * reference
Definition CFG.h:48
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
bool succ_empty(const Instruction *I)
Definition CFG.h:153
iterator_range< pred_iterator > pred_range
Definition CFG.h:108
iterator_range< succ_iterator > succ_range
Definition CFG.h:140
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
constexpr bool GraphHasNodeNumbers
Indicate whether a GraphTraits<NodeT>::getNumber() is supported.
iterator_range< const_pred_iterator > const_pred_range
Definition CFG.h:109
auto pred_size(const MachineBasicBlock *BB)
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
Definition CFG.h:106
auto succ_size(const MachineBasicBlock *BB)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
Instruction::succ_iterator succ_iterator
Definition CFG.h:138
PredIterator< BasicBlock, Value::user_iterator > pred_iterator
Definition CFG.h:105
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
Instruction::const_succ_iterator const_succ_iterator
Definition CFG.h:139
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:119
iterator_range< const_succ_iterator > const_succ_range
Definition CFG.h:141
#define N
static NodeRef getEntryNode(BasicBlock *BB)
Definition CFG.h:200
succ_iterator ChildIteratorType
Definition CFG.h:198
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:201
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:204
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:202
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:282
static nodes_iterator nodes_begin(Function *F)
Definition CFG.h:272
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:285
static nodes_iterator nodes_end(Function *F)
Definition CFG.h:276
static size_t size(Function *F)
Definition CFG.h:280
static NodeRef getEntryNode(Function *F)
Definition CFG.h:267
pointer_iterator< Function::iterator > nodes_iterator
Definition CFG.h:270
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:235
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:238
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:236
static NodeRef getEntryNode(Inverse< BasicBlock * > G)
Definition CFG.h:234
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:328
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:325
static NodeRef getEntryNode(Inverse< Function * > G)
Definition CFG.h:321
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:250
static NodeRef getEntryNode(Inverse< const BasicBlock * > G)
Definition CFG.h:248
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:249
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:252
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:338
static NodeRef getEntryNode(Inverse< const Function * > G)
Definition CFG.h:334
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:341
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:216
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:219
static NodeRef getEntryNode(const BasicBlock *BB)
Definition CFG.h:214
const_succ_iterator ChildIteratorType
Definition CFG.h:212
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:217
static size_t size(const Function *F)
Definition CFG.h:304
static nodes_iterator nodes_begin(const Function *F)
Definition CFG.h:296
static NodeRef getEntryNode(const Function *F)
Definition CFG.h:291
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:309
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:306
static nodes_iterator nodes_end(const Function *F)
Definition CFG.h:300
pointer_iterator< Function::const_iterator > nodes_iterator
Definition CFG.h:294
typename Function *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
The const version of succ_iterator.
Definition Instruction.h:94
Iterator type that casts an operand to a basic block.
Definition Instruction.h:80