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
54public:
55 PredIterator() = default;
56 explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {}
57 inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
58
59 inline bool operator==(const Self& x) const { return It == x.It; }
60 inline bool operator!=(const Self& x) const { return !operator==(x); }
61
62 inline reference operator*() const {
63 assert(!It.atEnd() && "pred_iterator out of range!");
64 auto *I = cast<Instruction>(*It);
65 assert(I->isTerminator() && "BasicBlock used in non-terminator");
66 return I->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;
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
98
101 return const_pred_iterator(BB);
102}
103inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
105 return const_pred_iterator(BB, true);
106}
107inline bool pred_empty(const BasicBlock *BB) {
108 return pred_begin(BB) == pred_end(BB);
109}
110/// Get the number of predecessors of \p BB. This is a linear time operation.
111/// Use \ref BasicBlock::hasNPredecessors() or hasNPredecessorsOrMore if able.
112inline unsigned pred_size(const BasicBlock *BB) {
113 return std::distance(pred_begin(BB), pred_end(BB));
114}
116 return pred_range(pred_begin(BB), pred_end(BB));
117}
119 return const_pred_range(pred_begin(BB), pred_end(BB));
120}
121
122//===----------------------------------------------------------------------===//
123// Instruction and BasicBlock succ_iterator helpers
124//===----------------------------------------------------------------------===//
125
130
132 return I->successors().begin();
133}
135 return I->successors().begin();
136}
137inline succ_iterator succ_end(Instruction *I) { return I->successors().end(); }
139 return I->successors().end();
140}
141inline bool succ_empty(const Instruction *I) {
142 return succ_begin(I) == succ_end(I);
143}
144inline unsigned succ_size(const Instruction *I) {
145 return std::distance(succ_begin(I), succ_end(I));
146}
147inline succ_range successors(Instruction *I) { return I->successors(); }
149 return I->successors();
150}
151
153 return succ_begin(BB->getTerminator());
154}
156 return succ_begin(BB->getTerminator());
157}
159 return succ_end(BB->getTerminator());
160}
162 return succ_end(BB->getTerminator());
163}
164inline bool succ_empty(const BasicBlock *BB) {
165 return succ_begin(BB) == succ_end(BB);
166}
167inline unsigned succ_size(const BasicBlock *BB) {
168 return std::distance(succ_begin(BB), succ_end(BB));
169}
171 return successors(BB->getTerminator());
172}
174 return successors(BB->getTerminator());
175}
176
177//===--------------------------------------------------------------------===//
178// GraphTraits specializations for basic block graphs (CFGs)
179//===--------------------------------------------------------------------===//
180
181// Provide specializations of GraphTraits to be able to treat a function as a
182// graph of basic blocks...
183
184template <> struct GraphTraits<BasicBlock*> {
187
188 static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
191
192 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
193};
194
196 "GraphTraits getNumber() not detected");
197
198template <> struct GraphTraits<const BasicBlock*> {
199 using NodeRef = const BasicBlock *;
201
202 static NodeRef getEntryNode(const BasicBlock *BB) { return BB; }
203
206
207 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
208};
209
211 "GraphTraits getNumber() not detected");
212
213// Provide specializations of GraphTraits to be able to treat a function as a
214// graph of basic blocks... and to walk it in inverse order. Inverse order for
215// a function is considered to be when traversing the predecessor edges of a BB
216// instead of the successor edges.
217//
218template <> struct GraphTraits<Inverse<BasicBlock*>> {
221
222 static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
225
226 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
227};
228
230 "GraphTraits getNumber() not detected");
231
232template <> struct GraphTraits<Inverse<const BasicBlock*>> {
233 using NodeRef = const BasicBlock *;
235
239
240 static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
241};
242
244 "GraphTraits getNumber() not detected");
245
246//===--------------------------------------------------------------------===//
247// GraphTraits specializations for function basic block graphs (CFGs)
248//===--------------------------------------------------------------------===//
249
250// Provide specializations of GraphTraits to be able to treat a function as a
251// graph of basic blocks... these are the same as the basic block iterators,
252// except that the root node is implicitly the first node of the function.
253//
254template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
255 static NodeRef getEntryNode(Function *F) { return &F->getEntryBlock(); }
256
257 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
259
261 return nodes_iterator(F->begin());
262 }
263
265 return nodes_iterator(F->end());
266 }
267
268 static size_t size(Function *F) { return F->size(); }
269
270 static unsigned getMaxNumber(const Function *F) {
271 return F->getMaxBlockNumber();
272 }
273 static unsigned getNumberEpoch(const Function *F) {
274 return F->getBlockNumberEpoch();
275 }
276};
277template <> struct GraphTraits<const Function*> :
279 static NodeRef getEntryNode(const Function *F) { return &F->getEntryBlock(); }
280
281 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
283
285 return nodes_iterator(F->begin());
286 }
287
289 return nodes_iterator(F->end());
290 }
291
292 static size_t size(const Function *F) { return F->size(); }
293
294 static unsigned getMaxNumber(const Function *F) {
295 return F->getMaxBlockNumber();
296 }
297 static unsigned getNumberEpoch(const Function *F) {
298 return F->getBlockNumberEpoch();
299 }
300};
301
302// Provide specializations of GraphTraits to be able to treat a function as a
303// graph of basic blocks... and to walk it in inverse order. Inverse order for
304// a function is considered to be when traversing the predecessor edges of a BB
305// instead of the successor edges.
306//
307template <> struct GraphTraits<Inverse<Function*>> :
310 return &G.Graph->getEntryBlock();
311 }
312
313 static unsigned getMaxNumber(const Function *F) {
314 return F->getMaxBlockNumber();
315 }
316 static unsigned getNumberEpoch(const Function *F) {
317 return F->getBlockNumberEpoch();
318 }
319};
320template <> struct GraphTraits<Inverse<const Function*>> :
323 return &G.Graph->getEntryBlock();
324 }
325
326 static unsigned getMaxNumber(const Function *F) {
327 return F->getMaxBlockNumber();
328 }
329 static unsigned getNumberEpoch(const Function *F) {
330 return F->getBlockNumberEpoch();
331 }
332};
333
334} // end namespace llvm
335
336#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; assumes that the block is well-formed.
Definition BasicBlock.h:237
bool operator==(const Self &x) const
Definition CFG.h:59
Self & operator++()
Definition CFG.h:70
PredIterator(Ptr *bb)
Definition CFG.h:56
PredIterator()=default
Self operator++(int)
Definition CFG.h:76
PredIterator(Ptr *bb, bool)
Definition CFG.h:57
bool operator!=(const Self &x) const
Definition CFG.h:60
Ptr ** pointer
Definition CFG.h:47
std::forward_iterator_tag iterator_category
Definition CFG.h:44
reference operator*() const
Definition CFG.h:62
PredIterator< Ptr, USE_iterator > Self
Definition CFG.h:51
std::ptrdiff_t difference_type
Definition CFG.h:46
pointer * operator->() const
Definition CFG.h:68
unsigned getOperandNo() const
getOperandNo - Return the operand number in the predecessor's terminator of the successor.
Definition CFG.h:82
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:88
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.
bool succ_empty(const Instruction *I)
Definition CFG.h:141
iterator_range< pred_iterator > pred_range
Definition CFG.h:96
iterator_range< succ_iterator > succ_range
Definition CFG.h:128
auto pred_end(const MachineBasicBlock *BB)
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:97
auto pred_size(const MachineBasicBlock *BB)
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
Definition CFG.h:94
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:126
PredIterator< BasicBlock, Value::user_iterator > pred_iterator
Definition CFG.h:93
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:127
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
iterator_range< const_succ_iterator > const_succ_range
Definition CFG.h:129
#define N
static NodeRef getEntryNode(BasicBlock *BB)
Definition CFG.h:188
succ_iterator ChildIteratorType
Definition CFG.h:186
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:189
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:192
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:190
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:270
static nodes_iterator nodes_begin(Function *F)
Definition CFG.h:260
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:273
static nodes_iterator nodes_end(Function *F)
Definition CFG.h:264
static size_t size(Function *F)
Definition CFG.h:268
static NodeRef getEntryNode(Function *F)
Definition CFG.h:255
pointer_iterator< Function::iterator > nodes_iterator
Definition CFG.h:258
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:223
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:226
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:224
static NodeRef getEntryNode(Inverse< BasicBlock * > G)
Definition CFG.h:222
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:316
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:313
static NodeRef getEntryNode(Inverse< Function * > G)
Definition CFG.h:309
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:238
static NodeRef getEntryNode(Inverse< const BasicBlock * > G)
Definition CFG.h:236
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:237
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:240
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:326
static NodeRef getEntryNode(Inverse< const Function * > G)
Definition CFG.h:322
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:329
static ChildIteratorType child_begin(NodeRef N)
Definition CFG.h:204
static unsigned getNumber(const BasicBlock *BB)
Definition CFG.h:207
static NodeRef getEntryNode(const BasicBlock *BB)
Definition CFG.h:202
const_succ_iterator ChildIteratorType
Definition CFG.h:200
static ChildIteratorType child_end(NodeRef N)
Definition CFG.h:205
static size_t size(const Function *F)
Definition CFG.h:292
static nodes_iterator nodes_begin(const Function *F)
Definition CFG.h:284
static NodeRef getEntryNode(const Function *F)
Definition CFG.h:279
static unsigned getNumberEpoch(const Function *F)
Definition CFG.h:297
static unsigned getMaxNumber(const Function *F)
Definition CFG.h:294
static nodes_iterator nodes_end(const Function *F)
Definition CFG.h:288
pointer_iterator< Function::const_iterator > nodes_iterator
Definition CFG.h:282
typename Function *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
The const version of succ_iterator.
Definition Instruction.h:96
Iterator type that casts an operand to a basic block.
Definition Instruction.h:81