LLVM 23.0.0git
CFG.h
Go to the documentation of this file.
1//===-- Analysis/CFG.h - BasicBlock Analyses --------------------*- 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//
9// This family of functions performs analyses on basic blocks, and instructions
10// contained within basic blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_CFG_H
15#define LLVM_ANALYSIS_CFG_H
16
20#include <utility>
21
22namespace llvm {
23
24class BasicBlock;
25class CycleInfo;
26class DominatorTree;
27class Function;
28class Instruction;
29class LoopInfo;
30template <typename T> class SmallVectorImpl;
31
32/// Analyze the specified function to find all of the loop backedges in the
33/// function and return them. This is a relatively cheap (compared to
34/// computing dominators and loop info) analysis.
35///
36/// The output is added to Result, as pairs of <from,to> edge info.
38 const Function &F,
39 SmallVectorImpl<std::pair<const BasicBlock *, const BasicBlock *>> &Result);
40
41/// Search for the specified successor of basic block BB and return its position
42/// in the terminator instruction's list of successors. It is an error to call
43/// this with a block that is not a successor.
44LLVM_ABI unsigned GetSuccessorNumber(const BasicBlock *BB,
45 const BasicBlock *Succ);
46
47/// Return true if the specified edge is a critical edge. Critical edges are
48/// edges from a block with multiple successors to a block with multiple
49/// predecessors.
50///
51LLVM_ABI bool isCriticalEdge(const Instruction *TI, unsigned SuccNum,
52 bool AllowIdenticalEdges = false);
53LLVM_ABI bool isCriticalEdge(const Instruction *TI, const BasicBlock *Succ,
54 bool AllowIdenticalEdges = false);
55
56/// Determine whether instruction 'To' is reachable from 'From', without passing
57/// through any blocks in ExclusionSet, returning true if uncertain.
58///
59/// Determine whether there is a path from From to To within a single function.
60/// Returns false only if we can prove that once 'From' has been executed then
61/// 'To' can not be executed. Conservatively returns true.
62///
63/// This function is linear with respect to the number of blocks in the CFG,
64/// walking down successors from From to reach To, with a fixed threshold.
65/// Using DT or LI allows us to answer more quickly. LI reduces the cost of
66/// an entire loop of any number of blocks to be the same as the cost of a
67/// single block. DT reduces the cost by allowing the search to terminate when
68/// we find a block that dominates the block containing 'To'. DT is most useful
69/// on branchy code but not loops, and LI is most useful on code with loops but
70/// does not help on branchy code outside loops.
72 const Instruction *From, const Instruction *To,
73 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
74 const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr,
75 const CycleInfo *CI = nullptr);
76
77/// Determine whether block 'To' is reachable from 'From', returning
78/// true if uncertain.
79///
80/// Determine whether there is a path from From to To within a single function.
81/// Returns false only if we can prove that once 'From' has been reached then
82/// 'To' can not be executed. Conservatively returns true.
84 const BasicBlock *From, const BasicBlock *To,
85 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet = nullptr,
86 const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr,
87 const CycleInfo *CI = nullptr);
88
89/// Determine whether there is at least one path from a block in
90/// 'Worklist' to 'StopBB' without passing through any blocks in
91/// 'ExclusionSet', returning true if uncertain.
92///
93/// Determine whether there is a path from at least one block in Worklist to
94/// StopBB within a single function without passing through any of the blocks
95/// in 'ExclusionSet'. Returns false only if we can prove that once any block
96/// in 'Worklist' has been reached then 'StopBB' can not be executed.
97/// Conservatively returns true.
99 SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
100 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
101 const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr,
102 const CycleInfo *CI = nullptr);
103
104/// Determine whether there is a potentially a path from at least one block in
105/// 'Worklist' to at least one block in 'StopSet' within a single function
106/// without passing through any of the blocks in 'ExclusionSet'. Returns false
107/// only if we can prove that once any block in 'Worklist' has been reached then
108/// no blocks in 'StopSet' can be executed without passing through any blocks in
109/// 'ExclusionSet'. Conservatively returns true.
113 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
114 const DominatorTree *DT = nullptr, const LoopInfo *LI = nullptr,
115 const CycleInfo *CI = nullptr);
116
117/// Return true if the control flow in \p RPOTraversal is irreducible.
118///
119/// This is a generic implementation to detect CFG irreducibility based on loop
120/// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
121/// Function, MachineFunction, etc.) by providing an RPO traversal (\p
122/// RPOTraversal) and the loop info analysis (\p LI) of the CFG. This utility
123/// function is only recommended when loop info analysis is available. If loop
124/// info analysis isn't available, please, don't compute it explicitly for this
125/// purpose. There are more efficient ways to detect CFG irreducibility that
126/// don't require recomputing loop info analysis (e.g., T1/T2 or Tarjan's
127/// algorithm).
128///
129/// Requirements:
130/// 1) GraphTraits must be implemented for NodeT type. It is used to access
131/// NodeT successors.
132// 2) \p RPOTraversal must be a valid reverse post-order traversal of the
133/// target CFG with begin()/end() iterator interfaces.
134/// 3) \p LI must be a valid LoopInfoBase that contains up-to-date loop
135/// analysis information of the CFG.
136///
137/// This algorithm uses the information about reducible loop back-edges already
138/// computed in \p LI. When a back-edge is found during the RPO traversal, the
139/// algorithm checks whether the back-edge is one of the reducible back-edges in
140/// loop info. If it isn't, the CFG is irreducible. For example, for the CFG
141/// below (canonical irreducible graph) loop info won't contain any loop, so the
142/// algorithm will return that the CFG is irreducible when checking the B <-
143/// -> C back-edge.
144///
145/// (A->B, A->C, B->C, C->B, C->D)
146/// A
147/// / \
148/// B<- ->C
149/// |
150/// D
151///
152template <class NodeT, class RPOTraversalT, class LoopInfoT,
153 class GT = GraphTraits<NodeT>>
154bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI) {
155 /// Check whether the edge (\p Src, \p Dst) is a reducible loop backedge
156 /// according to LI. I.e., check if there exists a loop that contains Src and
157 /// where Dst is the loop header.
158 auto isProperBackedge = [&](NodeT Src, NodeT Dst) {
159 for (const auto *Lp = LI.getLoopFor(Src); Lp; Lp = Lp->getParentLoop()) {
160 if (Lp->getHeader() == Dst)
161 return true;
162 }
163 return false;
164 };
165
167 for (NodeT Node : RPOTraversal) {
168 Visited.insert(Node);
169 for (NodeT Succ : make_range(GT::child_begin(Node), GT::child_end(Node))) {
170 // Succ hasn't been visited yet
171 if (!Visited.count(Succ))
172 continue;
173 // We already visited Succ, thus Node->Succ must be a backedge. Check that
174 // the head matches what we have in the loop information. Otherwise, we
175 // have an irreducible graph.
176 if (!isProperBackedge(Node, Succ))
177 return true;
178 }
179 }
180
181 return false;
182}
183
184// Returns true if these basic blocks belong to a presplit coroutine and the
185// edge corresponds to the 'default' case in the switch statement in the
186// pattern:
187//
188// %0 = call i8 @llvm.coro.suspend(token none, i1 false)
189// switch i8 %0, label %suspend [i8 0, label %resume
190// i8 1, label %cleanup]
191//
192// i.e. the edge to the `%suspend` BB. This edge is special in that it will
193// be elided by coroutine lowering (coro-split), and the `%suspend` BB needs
194// to be kept as-is. It's not a real CFG edge - post-lowering, it will end
195// up being a `ret`, and it must be thus lowerable to support symmetric
196// transfer. For example:
197// - this edge is not a loop exit edge if encountered in a loop (and should
198// be ignored)
199// - must not be split for PGO instrumentation, for example.
200LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src,
201 const BasicBlock &Dest);
202
203/// Return true if there is at least a path through which F can return, false if
204/// there is no such path.
205LLVM_ABI bool canReturn(const Function &F);
206} // namespace llvm
207
208#endif
#define LLVM_ABI
Definition Compiler.h:213
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
This file defines the SmallPtrSet class.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI bool isManyPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const SmallPtrSetImpl< const BasicBlock * > &StopSet, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr, const CycleInfo *CI=nullptr)
Determine whether there is a potentially a path from at least one block in 'Worklist' to at least one...
Definition CFG.cpp:291
LLVM_ABI unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition CFG.cpp:90
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI)
Return true if the control flow in RPOTraversal is irreducible.
Definition CFG.h:154
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr, const CycleInfo *CI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition CFG.cpp:324
LLVM_ABI bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr, const CycleInfo *CI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
Definition CFG.cpp:282
LLVM_ABI bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition CFG.cpp:106
LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, const BasicBlock &Dest)
Definition CFG.cpp:413
LLVM_ABI void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.
Definition CFG.cpp:36
LLVM_ABI bool canReturn(const Function &F)
Return true if there is at least a path through which F can return, false if there is no such path.
Definition CFG.cpp:394