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