1cfca06d7SDimitry Andric //===- UnifyLoopExits.cpp - Redirect exiting edges to one block -*- C++ -*-===//
2cfca06d7SDimitry Andric //
3cfca06d7SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cfca06d7SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5cfca06d7SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cfca06d7SDimitry Andric //
7cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
8cfca06d7SDimitry Andric //
9cfca06d7SDimitry Andric // For each natural loop with multiple exit blocks, this pass creates a new
10cfca06d7SDimitry Andric // block N such that all exiting blocks now branch to N, and then control flow
11cfca06d7SDimitry Andric // is redistributed to all the original exit blocks.
12cfca06d7SDimitry Andric //
13cfca06d7SDimitry Andric // Limitation: This assumes that all terminators in the CFG are direct branches
14cfca06d7SDimitry Andric // (the "br" instruction). The presence of any other control flow
15cfca06d7SDimitry Andric // such as indirectbr, switch or callbr will cause an assert.
16cfca06d7SDimitry Andric //
17cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
18cfca06d7SDimitry Andric
19b60736ecSDimitry Andric #include "llvm/Transforms/Utils/UnifyLoopExits.h"
20b60736ecSDimitry Andric #include "llvm/ADT/MapVector.h"
21145449b1SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h"
22cfca06d7SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
23145449b1SDimitry Andric #include "llvm/IR/Constants.h"
24cfca06d7SDimitry Andric #include "llvm/IR/Dominators.h"
25cfca06d7SDimitry Andric #include "llvm/InitializePasses.h"
26e3b55780SDimitry Andric #include "llvm/Support/CommandLine.h"
27cfca06d7SDimitry Andric #include "llvm/Transforms/Utils.h"
28cfca06d7SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
29cfca06d7SDimitry Andric
30cfca06d7SDimitry Andric #define DEBUG_TYPE "unify-loop-exits"
31cfca06d7SDimitry Andric
32cfca06d7SDimitry Andric using namespace llvm;
33cfca06d7SDimitry Andric
34e3b55780SDimitry Andric static cl::opt<unsigned> MaxBooleansInControlFlowHub(
35e3b55780SDimitry Andric "max-booleans-in-control-flow-hub", cl::init(32), cl::Hidden,
36e3b55780SDimitry Andric cl::desc("Set the maximum number of outgoing blocks for using a boolean "
37e3b55780SDimitry Andric "value to record the exiting block in CreateControlFlowHub."));
38e3b55780SDimitry Andric
39cfca06d7SDimitry Andric namespace {
40b60736ecSDimitry Andric struct UnifyLoopExitsLegacyPass : public FunctionPass {
41cfca06d7SDimitry Andric static char ID;
UnifyLoopExitsLegacyPass__anon438f63550111::UnifyLoopExitsLegacyPass42b60736ecSDimitry Andric UnifyLoopExitsLegacyPass() : FunctionPass(ID) {
43b60736ecSDimitry Andric initializeUnifyLoopExitsLegacyPassPass(*PassRegistry::getPassRegistry());
44cfca06d7SDimitry Andric }
45cfca06d7SDimitry Andric
getAnalysisUsage__anon438f63550111::UnifyLoopExitsLegacyPass46cfca06d7SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
47cfca06d7SDimitry Andric AU.addRequired<LoopInfoWrapperPass>();
48cfca06d7SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
49cfca06d7SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>();
50cfca06d7SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
51cfca06d7SDimitry Andric }
52cfca06d7SDimitry Andric
53cfca06d7SDimitry Andric bool runOnFunction(Function &F) override;
54cfca06d7SDimitry Andric };
55cfca06d7SDimitry Andric } // namespace
56cfca06d7SDimitry Andric
57b60736ecSDimitry Andric char UnifyLoopExitsLegacyPass::ID = 0;
58cfca06d7SDimitry Andric
createUnifyLoopExitsPass()59b60736ecSDimitry Andric FunctionPass *llvm::createUnifyLoopExitsPass() {
60b60736ecSDimitry Andric return new UnifyLoopExitsLegacyPass();
61b60736ecSDimitry Andric }
62cfca06d7SDimitry Andric
63b60736ecSDimitry Andric INITIALIZE_PASS_BEGIN(UnifyLoopExitsLegacyPass, "unify-loop-exits",
64cfca06d7SDimitry Andric "Fixup each natural loop to have a single exit block",
65cfca06d7SDimitry Andric false /* Only looks at CFG */, false /* Analysis Pass */)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)66cfca06d7SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
67cfca06d7SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
68b60736ecSDimitry Andric INITIALIZE_PASS_END(UnifyLoopExitsLegacyPass, "unify-loop-exits",
69cfca06d7SDimitry Andric "Fixup each natural loop to have a single exit block",
70cfca06d7SDimitry Andric false /* Only looks at CFG */, false /* Analysis Pass */)
71cfca06d7SDimitry Andric
72cfca06d7SDimitry Andric // The current transform introduces new control flow paths which may break the
73cfca06d7SDimitry Andric // SSA requirement that every def must dominate all its uses. For example,
74cfca06d7SDimitry Andric // consider a value D defined inside the loop that is used by some instruction
75cfca06d7SDimitry Andric // U outside the loop. It follows that D dominates U, since the original
76cfca06d7SDimitry Andric // program has valid SSA form. After merging the exits, all paths from D to U
77cfca06d7SDimitry Andric // now flow through the unified exit block. In addition, there may be other
78cfca06d7SDimitry Andric // paths that do not pass through D, but now reach the unified exit
79cfca06d7SDimitry Andric // block. Thus, D no longer dominates U.
80cfca06d7SDimitry Andric //
81cfca06d7SDimitry Andric // Restore the dominance by creating a phi for each such D at the new unified
82cfca06d7SDimitry Andric // loop exit. But when doing this, ignore any uses U that are in the new unified
83cfca06d7SDimitry Andric // loop exit, since those were introduced specially when the block was created.
84cfca06d7SDimitry Andric //
85cfca06d7SDimitry Andric // The use of SSAUpdater seems like overkill for this operation. The location
86cfca06d7SDimitry Andric // for creating the new PHI is well-known, and also the set of incoming blocks
87cfca06d7SDimitry Andric // to the new PHI.
88cfca06d7SDimitry Andric static void restoreSSA(const DominatorTree &DT, const Loop *L,
89cfca06d7SDimitry Andric const SetVector<BasicBlock *> &Incoming,
90cfca06d7SDimitry Andric BasicBlock *LoopExitBlock) {
91cfca06d7SDimitry Andric using InstVector = SmallVector<Instruction *, 8>;
92b60736ecSDimitry Andric using IIMap = MapVector<Instruction *, InstVector>;
93cfca06d7SDimitry Andric IIMap ExternalUsers;
94e3b55780SDimitry Andric for (auto *BB : L->blocks()) {
95cfca06d7SDimitry Andric for (auto &I : *BB) {
96cfca06d7SDimitry Andric for (auto &U : I.uses()) {
97cfca06d7SDimitry Andric auto UserInst = cast<Instruction>(U.getUser());
98cfca06d7SDimitry Andric auto UserBlock = UserInst->getParent();
99cfca06d7SDimitry Andric if (UserBlock == LoopExitBlock)
100cfca06d7SDimitry Andric continue;
101cfca06d7SDimitry Andric if (L->contains(UserBlock))
102cfca06d7SDimitry Andric continue;
103cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "added ext use for " << I.getName() << "("
104cfca06d7SDimitry Andric << BB->getName() << ")"
105cfca06d7SDimitry Andric << ": " << UserInst->getName() << "("
106cfca06d7SDimitry Andric << UserBlock->getName() << ")"
107cfca06d7SDimitry Andric << "\n");
108cfca06d7SDimitry Andric ExternalUsers[&I].push_back(UserInst);
109cfca06d7SDimitry Andric }
110cfca06d7SDimitry Andric }
111cfca06d7SDimitry Andric }
112cfca06d7SDimitry Andric
1137fa27ce4SDimitry Andric for (const auto &II : ExternalUsers) {
114cfca06d7SDimitry Andric // For each Def used outside the loop, create NewPhi in
115cfca06d7SDimitry Andric // LoopExitBlock. NewPhi receives Def only along exiting blocks that
116cfca06d7SDimitry Andric // dominate it, while the remaining values are undefined since those paths
117cfca06d7SDimitry Andric // didn't exist in the original CFG.
118cfca06d7SDimitry Andric auto Def = II.first;
119cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "externally used: " << Def->getName() << "\n");
120e3b55780SDimitry Andric auto NewPhi =
121e3b55780SDimitry Andric PHINode::Create(Def->getType(), Incoming.size(),
122ac9a064cSDimitry Andric Def->getName() + ".moved", LoopExitBlock->begin());
123e3b55780SDimitry Andric for (auto *In : Incoming) {
124cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "predecessor " << In->getName() << ": ");
125cfca06d7SDimitry Andric if (Def->getParent() == In || DT.dominates(Def, In)) {
126cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "dominated\n");
127cfca06d7SDimitry Andric NewPhi->addIncoming(Def, In);
128cfca06d7SDimitry Andric } else {
129cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "not dominated\n");
1307fa27ce4SDimitry Andric NewPhi->addIncoming(PoisonValue::get(Def->getType()), In);
131cfca06d7SDimitry Andric }
132cfca06d7SDimitry Andric }
133cfca06d7SDimitry Andric
134cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "external users:");
135e3b55780SDimitry Andric for (auto *U : II.second) {
136cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << " " << U->getName());
137cfca06d7SDimitry Andric U->replaceUsesOfWith(Def, NewPhi);
138cfca06d7SDimitry Andric }
139cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "\n");
140cfca06d7SDimitry Andric }
141cfca06d7SDimitry Andric }
142cfca06d7SDimitry Andric
unifyLoopExits(DominatorTree & DT,LoopInfo & LI,Loop * L)143cfca06d7SDimitry Andric static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
144cfca06d7SDimitry Andric // To unify the loop exits, we need a list of the exiting blocks as
145cfca06d7SDimitry Andric // well as exit blocks. The functions for locating these lists both
146cfca06d7SDimitry Andric // traverse the entire loop body. It is more efficient to first
147cfca06d7SDimitry Andric // locate the exiting blocks and then examine their successors to
148cfca06d7SDimitry Andric // locate the exit blocks.
149cfca06d7SDimitry Andric SetVector<BasicBlock *> ExitingBlocks;
150cfca06d7SDimitry Andric SetVector<BasicBlock *> Exits;
151cfca06d7SDimitry Andric
152cfca06d7SDimitry Andric // We need SetVectors, but the Loop API takes a vector, so we use a temporary.
153cfca06d7SDimitry Andric SmallVector<BasicBlock *, 8> Temp;
154cfca06d7SDimitry Andric L->getExitingBlocks(Temp);
155e3b55780SDimitry Andric for (auto *BB : Temp) {
156cfca06d7SDimitry Andric ExitingBlocks.insert(BB);
157e3b55780SDimitry Andric for (auto *S : successors(BB)) {
158cfca06d7SDimitry Andric auto SL = LI.getLoopFor(S);
159cfca06d7SDimitry Andric // A successor is not an exit if it is directly or indirectly in the
160cfca06d7SDimitry Andric // current loop.
161cfca06d7SDimitry Andric if (SL == L || L->contains(SL))
162cfca06d7SDimitry Andric continue;
163cfca06d7SDimitry Andric Exits.insert(S);
164cfca06d7SDimitry Andric }
165cfca06d7SDimitry Andric }
166cfca06d7SDimitry Andric
167cfca06d7SDimitry Andric LLVM_DEBUG(
168cfca06d7SDimitry Andric dbgs() << "Found exit blocks:";
169cfca06d7SDimitry Andric for (auto Exit : Exits) {
170cfca06d7SDimitry Andric dbgs() << " " << Exit->getName();
171cfca06d7SDimitry Andric }
172cfca06d7SDimitry Andric dbgs() << "\n";
173cfca06d7SDimitry Andric
174cfca06d7SDimitry Andric dbgs() << "Found exiting blocks:";
175cfca06d7SDimitry Andric for (auto EB : ExitingBlocks) {
176cfca06d7SDimitry Andric dbgs() << " " << EB->getName();
177cfca06d7SDimitry Andric }
1784b4fe385SDimitry Andric dbgs() << "\n";);
179cfca06d7SDimitry Andric
180cfca06d7SDimitry Andric if (Exits.size() <= 1) {
181cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "loop does not have multiple exits; nothing to do\n");
182cfca06d7SDimitry Andric return false;
183cfca06d7SDimitry Andric }
184cfca06d7SDimitry Andric
185cfca06d7SDimitry Andric SmallVector<BasicBlock *, 8> GuardBlocks;
186cfca06d7SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
187e3b55780SDimitry Andric auto LoopExitBlock =
188e3b55780SDimitry Andric CreateControlFlowHub(&DTU, GuardBlocks, ExitingBlocks, Exits, "loop.exit",
189e3b55780SDimitry Andric MaxBooleansInControlFlowHub.getValue());
190cfca06d7SDimitry Andric
191cfca06d7SDimitry Andric restoreSSA(DT, L, ExitingBlocks, LoopExitBlock);
192cfca06d7SDimitry Andric
193cfca06d7SDimitry Andric #if defined(EXPENSIVE_CHECKS)
194cfca06d7SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Full));
195cfca06d7SDimitry Andric #else
196cfca06d7SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Fast));
197cfca06d7SDimitry Andric #endif // EXPENSIVE_CHECKS
198cfca06d7SDimitry Andric L->verifyLoop();
199cfca06d7SDimitry Andric
200cfca06d7SDimitry Andric // The guard blocks were created outside the loop, so they need to become
201cfca06d7SDimitry Andric // members of the parent loop.
202cfca06d7SDimitry Andric if (auto ParentLoop = L->getParentLoop()) {
203e3b55780SDimitry Andric for (auto *G : GuardBlocks) {
204cfca06d7SDimitry Andric ParentLoop->addBasicBlockToLoop(G, LI);
205cfca06d7SDimitry Andric }
206cfca06d7SDimitry Andric ParentLoop->verifyLoop();
207cfca06d7SDimitry Andric }
208cfca06d7SDimitry Andric
209cfca06d7SDimitry Andric #if defined(EXPENSIVE_CHECKS)
210cfca06d7SDimitry Andric LI.verify(DT);
211cfca06d7SDimitry Andric #endif // EXPENSIVE_CHECKS
212cfca06d7SDimitry Andric
213cfca06d7SDimitry Andric return true;
214cfca06d7SDimitry Andric }
215cfca06d7SDimitry Andric
runImpl(LoopInfo & LI,DominatorTree & DT)216b60736ecSDimitry Andric static bool runImpl(LoopInfo &LI, DominatorTree &DT) {
217cfca06d7SDimitry Andric
218cfca06d7SDimitry Andric bool Changed = false;
219cfca06d7SDimitry Andric auto Loops = LI.getLoopsInPreorder();
220e3b55780SDimitry Andric for (auto *L : Loops) {
221cfca06d7SDimitry Andric LLVM_DEBUG(dbgs() << "Loop: " << L->getHeader()->getName() << " (depth: "
222cfca06d7SDimitry Andric << LI.getLoopDepth(L->getHeader()) << ")\n");
223cfca06d7SDimitry Andric Changed |= unifyLoopExits(DT, LI, L);
224cfca06d7SDimitry Andric }
225cfca06d7SDimitry Andric return Changed;
226cfca06d7SDimitry Andric }
227b60736ecSDimitry Andric
runOnFunction(Function & F)228b60736ecSDimitry Andric bool UnifyLoopExitsLegacyPass::runOnFunction(Function &F) {
229b60736ecSDimitry Andric LLVM_DEBUG(dbgs() << "===== Unifying loop exits in function " << F.getName()
230b60736ecSDimitry Andric << "\n");
231b60736ecSDimitry Andric auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
232b60736ecSDimitry Andric auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
233b60736ecSDimitry Andric
234b1c73532SDimitry Andric assert(hasOnlySimpleTerminator(F) && "Unsupported block terminator.");
235b1c73532SDimitry Andric
236b60736ecSDimitry Andric return runImpl(LI, DT);
237b60736ecSDimitry Andric }
238b60736ecSDimitry Andric
239b60736ecSDimitry Andric namespace llvm {
240b60736ecSDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)241b60736ecSDimitry Andric PreservedAnalyses UnifyLoopExitsPass::run(Function &F,
242b60736ecSDimitry Andric FunctionAnalysisManager &AM) {
243b60736ecSDimitry Andric auto &LI = AM.getResult<LoopAnalysis>(F);
244b60736ecSDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
245b60736ecSDimitry Andric
246b60736ecSDimitry Andric if (!runImpl(LI, DT))
247b60736ecSDimitry Andric return PreservedAnalyses::all();
248b60736ecSDimitry Andric PreservedAnalyses PA;
249b60736ecSDimitry Andric PA.preserve<LoopAnalysis>();
250b60736ecSDimitry Andric PA.preserve<DominatorTreeAnalysis>();
251b60736ecSDimitry Andric return PA;
252b60736ecSDimitry Andric }
253b60736ecSDimitry Andric } // namespace llvm
254