LLVM 24.0.0git
UnifyLoopExits.cpp
Go to the documentation of this file.
1//===- UnifyLoopExits.cpp - Redirect exiting edges to one block -*- 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// For each natural loop with multiple exit blocks, this pass creates a new
10// block N such that all exiting blocks now branch to N, and then control flow
11// is redistributed to all the original exit blocks.
12//
13// Limitation: This assumes that all terminators in the CFG are direct branches
14// (the "br" instruction). The presence of any other control flow
15// such as indirectbr or switch will cause an assert.
16// The callbr and switch terminators are supported by creating
17// intermediate target blocks that unconditionally branch to the
18// original target blocks. These intermediate target blocks can then
19// be redirected through the ControlFlowHub as usual.
20//
21//===----------------------------------------------------------------------===//
22
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/MapVector.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/Dominators.h"
36
37#define DEBUG_TYPE "unify-loop-exits"
38
39using namespace llvm;
40
42 "max-booleans-in-control-flow-hub", cl::init(32), cl::Hidden,
43 cl::desc("Set the maximum number of outgoing blocks for using a boolean "
44 "value to record the exiting block in the ControlFlowHub."));
45
46namespace {
47struct UnifyLoopExitsLegacyPass : public FunctionPass {
48 static char ID;
49 UnifyLoopExitsLegacyPass() : FunctionPass(ID) {
51 }
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.addRequired<LoopInfoWrapperPass>();
55 AU.addRequired<DominatorTreeWrapperPass>();
56 AU.addPreserved<LoopInfoWrapperPass>();
57 AU.addPreserved<DominatorTreeWrapperPass>();
58 }
59
60 bool runOnFunction(Function &F) override;
61};
62} // namespace
63
64char UnifyLoopExitsLegacyPass::ID = 0;
65
67 return new UnifyLoopExitsLegacyPass();
68}
69
70INITIALIZE_PASS_BEGIN(UnifyLoopExitsLegacyPass, "unify-loop-exits",
71 "Fixup each natural loop to have a single exit block",
72 false /* Only looks at CFG */, false /* Analysis Pass */)
75INITIALIZE_PASS_END(UnifyLoopExitsLegacyPass, "unify-loop-exits",
76 "Fixup each natural loop to have a single exit block",
77 false /* Only looks at CFG */, false /* Analysis Pass */)
78
79// The current transform introduces new control flow paths which may break the
80// SSA requirement that every def must dominate all its uses. For example,
81// consider a value D defined inside the loop that is used by some instruction
82// U outside the loop. It follows that D dominates U, since the original
83// program has valid SSA form. After merging the exits, all paths from D to U
84// now flow through the unified exit block. In addition, there may be other
85// paths that do not pass through D, but now reach the unified exit
86// block. Thus, D no longer dominates U.
87//
88// Restore the dominance by creating a phi for each such D at the new unified
89// loop exit. But when doing this, ignore any uses U that are in the new unified
90// loop exit, since those were introduced specially when the block was created.
91//
92// The use of SSAUpdater seems like overkill for this operation. The location
93// for creating the new PHI is well-known, and also the set of incoming blocks
94// to the new PHI.
96 SmallVectorImpl<BasicBlock *> &Incoming,
97 BasicBlock *LoopExitBlock) {
98 using InstVector = SmallVector<Instruction *, 8>;
100 IIMap ExternalUsers;
101 for (auto *BB : L->blocks()) {
102 for (auto &I : *BB) {
103 for (Instruction *UserInst : I.users()) {
104 auto UserBlock = UserInst->getParent();
105 if (UserBlock == LoopExitBlock)
106 continue;
107 if (L->contains(UserBlock))
108 continue;
109 LLVM_DEBUG(dbgs() << "added ext use for " << I.getName() << "("
110 << BB->getName() << ")"
111 << ": " << UserInst->getName() << "("
112 << UserBlock->getName() << ")"
113 << "\n");
114 ExternalUsers[&I].push_back(UserInst);
115 }
116 }
117 }
118
119 for (const auto &II : ExternalUsers) {
120 // For each Def used outside the loop, create NewPhi in
121 // LoopExitBlock. NewPhi receives Def only along exiting blocks that
122 // dominate it, while the remaining values are undefined since those paths
123 // didn't exist in the original CFG.
124 auto Def = II.first;
125 LLVM_DEBUG(dbgs() << "externally used: " << Def->getName() << "\n");
126 auto NewPhi =
127 PHINode::Create(Def->getType(), Incoming.size(),
128 Def->getName() + ".moved", LoopExitBlock->begin());
129 for (auto *In : Incoming) {
130 LLVM_DEBUG(dbgs() << "predecessor " << In->getName() << ": ");
131 if (Def->getParent() == In || DT.dominates(Def, In)) {
132 LLVM_DEBUG(dbgs() << "dominated\n");
133 NewPhi->addIncoming(Def, In);
134 } else {
135 LLVM_DEBUG(dbgs() << "not dominated\n");
136 NewPhi->addIncoming(PoisonValue::get(Def->getType()), In);
137 }
138 }
139
140 LLVM_DEBUG(dbgs() << "external users:");
141 for (auto *U : II.second) {
142 LLVM_DEBUG(dbgs() << " " << U->getName());
143 U->replaceUsesOfWith(Def, NewPhi);
144 }
145 LLVM_DEBUG(dbgs() << "\n");
146 }
147}
148
149static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
150 // To unify the loop exits, we need a list of the exiting blocks as
151 // well as exit blocks. The functions for locating these lists both
152 // traverse the entire loop body. It is more efficient to first
153 // locate the exiting blocks and then examine their successors to
154 // locate the exit blocks.
155 SmallVector<BasicBlock *, 8> ExitingBlocks;
156 L->getExitingBlocks(ExitingBlocks);
157
158 // No exit blocks, so nothing to do. Just return.
159 if (ExitingBlocks.empty())
160 return false;
161
162 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
163 SmallVector<BasicBlock *, 8> MultiBrTargetBlocksToFix;
164
165 // Redirect exiting edges through a control flow hub.
166 ControlFlowHub CHub;
167 bool Changed = false;
168
169 unsigned NumExitingBlocks = ExitingBlocks.size();
170 for (unsigned I = 0; I < NumExitingBlocks; ++I) {
171 BasicBlock *BB = ExitingBlocks[I];
172 Instruction *Term = BB->getTerminator();
173 if (UncondBrInst *Branch = dyn_cast<UncondBrInst>(Term)) {
174 BasicBlock *Succ0 = Branch->getSuccessor(0);
175 Succ0 = L->contains(Succ0) ? nullptr : Succ0;
176 CHub.addBranch(BB, Succ0);
177
178 LLVM_DEBUG(dbgs() << "Added exiting branch: " << printBasicBlock(BB)
179 << " -> " << printBasicBlock(Succ0) << '\n');
180 } else if (CondBrInst *Branch = dyn_cast<CondBrInst>(Term)) {
181 BasicBlock *Succ0 = Branch->getSuccessor(0);
182 Succ0 = L->contains(Succ0) ? nullptr : Succ0;
183
184 BasicBlock *Succ1 = Branch->getSuccessor(1);
185 Succ1 = L->contains(Succ1) ? nullptr : Succ1;
186 CHub.addBranch(BB, Succ0, Succ1);
187
188 LLVM_DEBUG(dbgs() << "Added exiting branch: " << printBasicBlock(BB)
189 << " -> " << printBasicBlock(Succ0)
190 << (Succ0 && Succ1 ? " " : "") << printBasicBlock(Succ1)
191 << '\n');
192 } else if (isa<CallBrInst>(Term) || isa<SwitchInst>(Term)) {
194 for (unsigned J = 0; J < Term->getNumSuccessors(); ++J) {
195 BasicBlock *Succ = Term->getSuccessor(J);
196 if (L->contains(Succ))
197 continue;
198 bool UpdatedLI;
199 auto It = BrTargets.find(Succ);
200 BasicBlock *ExistingTarget =
201 (It != BrTargets.end()) ? It->second : nullptr;
202 BasicBlock *NewSucc = SplitMultiBrEdge(BB, Succ, J, ExistingTarget,
203 &DTU, nullptr, &LI, &UpdatedLI);
204
205 if (!ExistingTarget) {
206 // SplitMultiBrEdge modifies the CFG because it creates an
207 // intermediate block. So we need to set the changed flag no matter
208 // what the ControlFlowHub is going to do later.
209 Changed = true;
210 // Even if the terminator and Succ do not have a common parent loop,
211 // we need to add the new target block to the parent loop of the
212 // current loop.
213 if (!UpdatedLI)
214 MultiBrTargetBlocksToFix.push_back(NewSucc);
215 // ExitingBlocks is later used to restore SSA, so we need to make sure
216 // that the blocks used for phi nodes in the guard blocks match the
217 // predecessors of the guard blocks, which, in the case of callbr or
218 // switch terminator, are the new intermediate target blocks instead
219 // of themselves. If only one exiting block is generated, the
220 // branching block itself is overwritten, while further blocks are
221 // appended as additional exiting blocks.
222 if (BrTargets.empty())
223 ExitingBlocks[I] = NewSucc;
224 else
225 ExitingBlocks.push_back(NewSucc);
226 CHub.addBranch(NewSucc, Succ);
227 BrTargets[Succ] = NewSucc;
228 }
229 LLVM_DEBUG(dbgs() << "Added exiting branch: "
230 << printBasicBlock(NewSucc) << " -> "
231 << printBasicBlock(Succ) << '\n');
232 }
233 } else {
235 "unsupported block terminator: unify-loop-exits "
236 "only supports br, callbr, and switch instructions");
237 }
238 }
239
241 BasicBlock *LoopExitBlock;
242 bool ChangedCFG;
243 std::tie(LoopExitBlock, ChangedCFG) = CHub.finalize(
244 &DTU, GuardBlocks, "loop.exit", MaxBooleansInControlFlowHub.getValue());
245 ChangedCFG |= Changed;
246 if (!ChangedCFG)
247 return false;
248
249 restoreSSA(DT, L, ExitingBlocks, LoopExitBlock);
250
251#if defined(EXPENSIVE_CHECKS)
252 assert(DT.verify(DominatorTree::VerificationLevel::Full));
253#else
254 assert(DT.verify(DominatorTree::VerificationLevel::Fast));
255#endif // EXPENSIVE_CHECKS
256 L->verifyLoop();
257
258 // The guard blocks were created outside the loop, so they need to become
259 // members of the parent loop.
260 // Same goes for the callbr/switch target blocks. Although we try to add them
261 // to the smallest common parent loop of the branching block and the
262 // corresponding original target block, there might not have been such a loop,
263 // in which case the newly created target blocks are not part of any
264 // loop. For nested loops, this might result in them leading to a loop with
265 // multiple entry points.
266 if (auto *ParentLoop = L->getParentLoop()) {
267 for (auto *G : GuardBlocks) {
268 ParentLoop->addBasicBlockToLoop(G, LI);
269 }
270 for (auto *C : MultiBrTargetBlocksToFix) {
271 ParentLoop->addBasicBlockToLoop(C, LI);
272 }
273 ParentLoop->verifyLoop();
274 }
275
276#if defined(EXPENSIVE_CHECKS)
277 LI.verify();
278#endif // EXPENSIVE_CHECKS
279
280 return true;
281}
282
283static bool runImpl(LoopInfo &LI, DominatorTree &DT) {
284
285 bool Changed = false;
286 auto Loops = LI.getLoopsInPreorder();
287 for (auto *L : Loops) {
288 LLVM_DEBUG(dbgs() << "Processing loop:\n"; L->print(dbgs()));
289 Changed |= unifyLoopExits(DT, LI, L);
290 }
291 return Changed;
292}
293
294bool UnifyLoopExitsLegacyPass::runOnFunction(Function &F) {
295 LLVM_DEBUG(dbgs() << "===== Unifying loop exits in function " << F.getName()
296 << "\n");
297 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
298 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
299
300 return runImpl(LI, DT);
301}
302
303namespace llvm {
304
307 LLVM_DEBUG(dbgs() << "===== Unifying loop exits in function " << F.getName()
308 << "\n");
309 auto &LI = AM.getResult<LoopAnalysis>(F);
310 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
311
312 if (!runImpl(LI, DT))
313 return PreservedAnalyses::all();
317 return PA;
318}
319} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Hardware Loops
#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
This file implements a map that provides insertion order iteration.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L)
unify loop Fixup each natural loop to have a single exit static false void restoreSSA(const DominatorTree &DT, const Loop *L, SmallVectorImpl< BasicBlock * > &Incoming, BasicBlock *LoopExitBlock)
static cl::opt< unsigned > MaxBooleansInControlFlowHub("max-booleans-in-control-flow-hub", cl::init(32), cl::Hidden, cl::desc("Set the maximum number of outgoing blocks for using a boolean " "value to record the exiting block in the ControlFlowHub."))
static bool runImpl(LoopInfo &LI, DominatorTree &DT)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:236
Conditional Branch instruction.
bool empty() const
Definition DenseMap.h:716
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:598
SmallVector< LoopT *, 4 > getLoopsInPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:623
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Unconditional Branch instruction.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI BasicBlock * SplitMultiBrEdge(BasicBlock *MultiBrBlock, BasicBlock *Succ, unsigned SuccIdx, BasicBlock *BrTarget=nullptr, DomTreeUpdater *DTU=nullptr, CycleInfo *CI=nullptr, LoopInfo *LI=nullptr, bool *UpdatedLI=nullptr)
Create a new intermediate target block for a callbr or switch edge.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void initializeUnifyLoopExitsLegacyPassPass(PassRegistry &)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI FunctionPass * createUnifyLoopExitsPass()
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI Printable printBasicBlock(const BasicBlock *BB)
Print BasicBlock BB as an operand or print "<nullptr>" if BB is a nullptr.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such that the control flow from ...
void addBranch(BasicBlock *BB, BasicBlock *Succ0, BasicBlock *Succ1=nullptr)
LLVM_ABI std::pair< BasicBlock *, bool > finalize(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans=std::nullopt)
Return the unified loop exit block and a flag indicating if the CFG was changed at all.