1*0b57cec5SDimitry Andric //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This pass performs lightweight instruction simplification on loop bodies.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
17*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
18*0b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
19*0b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
20*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
21*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopIterator.h"
22*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h"
23*0b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
24*0b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h"
25*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
26*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
27*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
28*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
29*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
30*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
31*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
32*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
33*0b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
34*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
35*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
36bdd1243dSDimitry Andric #include <optional>
37*0b57cec5SDimitry Andric #include <utility>
38*0b57cec5SDimitry Andric
39*0b57cec5SDimitry Andric using namespace llvm;
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric #define DEBUG_TYPE "loop-instsimplify"
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric STATISTIC(NumSimplified, "Number of redundant instructions simplified");
44*0b57cec5SDimitry Andric
simplifyLoopInst(Loop & L,DominatorTree & DT,LoopInfo & LI,AssumptionCache & AC,const TargetLibraryInfo & TLI,MemorySSAUpdater * MSSAU)45*0b57cec5SDimitry Andric static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
46*0b57cec5SDimitry Andric AssumptionCache &AC, const TargetLibraryInfo &TLI,
47*0b57cec5SDimitry Andric MemorySSAUpdater *MSSAU) {
48*0b57cec5SDimitry Andric const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
49*0b57cec5SDimitry Andric SimplifyQuery SQ(DL, &TLI, &DT, &AC);
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric // On the first pass over the loop body we try to simplify every instruction.
52*0b57cec5SDimitry Andric // On subsequent passes, we can restrict this to only simplifying instructions
53*0b57cec5SDimitry Andric // where the inputs have been updated. We end up needing two sets: one
54*0b57cec5SDimitry Andric // containing the instructions we are simplifying in *this* pass, and one for
55*0b57cec5SDimitry Andric // the instructions we will want to simplify in the *next* pass. We use
56*0b57cec5SDimitry Andric // pointers so we can swap between two stably allocated sets.
57*0b57cec5SDimitry Andric SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
58*0b57cec5SDimitry Andric
59*0b57cec5SDimitry Andric // Track the PHI nodes that have already been visited during each iteration so
60*0b57cec5SDimitry Andric // that we can identify when it is necessary to iterate.
61*0b57cec5SDimitry Andric SmallPtrSet<PHINode *, 4> VisitedPHIs;
62*0b57cec5SDimitry Andric
63*0b57cec5SDimitry Andric // While simplifying we may discover dead code or cause code to become dead.
64*0b57cec5SDimitry Andric // Keep track of all such instructions and we will delete them at the end.
655ffd83dbSDimitry Andric SmallVector<WeakTrackingVH, 8> DeadInsts;
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric // First we want to create an RPO traversal of the loop body. By processing in
68*0b57cec5SDimitry Andric // RPO we can ensure that definitions are processed prior to uses (for non PHI
69*0b57cec5SDimitry Andric // uses) in all cases. This ensures we maximize the simplifications in each
70*0b57cec5SDimitry Andric // iteration over the loop and minimizes the possible causes for continuing to
71*0b57cec5SDimitry Andric // iterate.
72*0b57cec5SDimitry Andric LoopBlocksRPO RPOT(&L);
73*0b57cec5SDimitry Andric RPOT.perform(&LI);
74*0b57cec5SDimitry Andric MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
75*0b57cec5SDimitry Andric
76*0b57cec5SDimitry Andric bool Changed = false;
77*0b57cec5SDimitry Andric for (;;) {
78*0b57cec5SDimitry Andric if (MSSAU && VerifyMemorySSA)
79*0b57cec5SDimitry Andric MSSA->verifyMemorySSA();
80*0b57cec5SDimitry Andric for (BasicBlock *BB : RPOT) {
81*0b57cec5SDimitry Andric for (Instruction &I : *BB) {
82*0b57cec5SDimitry Andric if (auto *PI = dyn_cast<PHINode>(&I))
83*0b57cec5SDimitry Andric VisitedPHIs.insert(PI);
84*0b57cec5SDimitry Andric
85*0b57cec5SDimitry Andric if (I.use_empty()) {
86*0b57cec5SDimitry Andric if (isInstructionTriviallyDead(&I, &TLI))
87*0b57cec5SDimitry Andric DeadInsts.push_back(&I);
88*0b57cec5SDimitry Andric continue;
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric
91*0b57cec5SDimitry Andric // We special case the first iteration which we can detect due to the
92*0b57cec5SDimitry Andric // empty `ToSimplify` set.
93*0b57cec5SDimitry Andric bool IsFirstIteration = ToSimplify->empty();
94*0b57cec5SDimitry Andric
95*0b57cec5SDimitry Andric if (!IsFirstIteration && !ToSimplify->count(&I))
96*0b57cec5SDimitry Andric continue;
97*0b57cec5SDimitry Andric
9881ad6265SDimitry Andric Value *V = simplifyInstruction(&I, SQ.getWithInstruction(&I));
99*0b57cec5SDimitry Andric if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
100*0b57cec5SDimitry Andric continue;
101*0b57cec5SDimitry Andric
102349cc55cSDimitry Andric for (Use &U : llvm::make_early_inc_range(I.uses())) {
103*0b57cec5SDimitry Andric auto *UserI = cast<Instruction>(U.getUser());
104*0b57cec5SDimitry Andric U.set(V);
105*0b57cec5SDimitry Andric
10681ad6265SDimitry Andric // Do not bother dealing with unreachable code.
10781ad6265SDimitry Andric if (!DT.isReachableFromEntry(UserI->getParent()))
10881ad6265SDimitry Andric continue;
10981ad6265SDimitry Andric
110*0b57cec5SDimitry Andric // If the instruction is used by a PHI node we have already processed
111*0b57cec5SDimitry Andric // we'll need to iterate on the loop body to converge, so add it to
112*0b57cec5SDimitry Andric // the next set.
113*0b57cec5SDimitry Andric if (auto *UserPI = dyn_cast<PHINode>(UserI))
114*0b57cec5SDimitry Andric if (VisitedPHIs.count(UserPI)) {
115*0b57cec5SDimitry Andric Next->insert(UserPI);
116*0b57cec5SDimitry Andric continue;
117*0b57cec5SDimitry Andric }
118*0b57cec5SDimitry Andric
119*0b57cec5SDimitry Andric // If we are only simplifying targeted instructions and the user is an
120*0b57cec5SDimitry Andric // instruction in the loop body, add it to our set of targeted
121*0b57cec5SDimitry Andric // instructions. Because we process defs before uses (outside of PHIs)
122*0b57cec5SDimitry Andric // we won't have visited it yet.
123*0b57cec5SDimitry Andric //
124*0b57cec5SDimitry Andric // We also skip any uses outside of the loop being simplified. Those
125*0b57cec5SDimitry Andric // should always be PHI nodes due to LCSSA form, and we don't want to
126*0b57cec5SDimitry Andric // try to simplify those away.
127*0b57cec5SDimitry Andric assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
128*0b57cec5SDimitry Andric "Uses outside the loop should be PHI nodes due to LCSSA!");
129*0b57cec5SDimitry Andric if (!IsFirstIteration && L.contains(UserI))
130*0b57cec5SDimitry Andric ToSimplify->insert(UserI);
131*0b57cec5SDimitry Andric }
132*0b57cec5SDimitry Andric
133*0b57cec5SDimitry Andric if (MSSAU)
134*0b57cec5SDimitry Andric if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V))
135*0b57cec5SDimitry Andric if (MemoryAccess *MA = MSSA->getMemoryAccess(&I))
136*0b57cec5SDimitry Andric if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI))
137*0b57cec5SDimitry Andric MA->replaceAllUsesWith(ReplacementMA);
138*0b57cec5SDimitry Andric
139*0b57cec5SDimitry Andric assert(I.use_empty() && "Should always have replaced all uses!");
140*0b57cec5SDimitry Andric if (isInstructionTriviallyDead(&I, &TLI))
141*0b57cec5SDimitry Andric DeadInsts.push_back(&I);
142*0b57cec5SDimitry Andric ++NumSimplified;
143*0b57cec5SDimitry Andric Changed = true;
144*0b57cec5SDimitry Andric }
145*0b57cec5SDimitry Andric }
146*0b57cec5SDimitry Andric
147*0b57cec5SDimitry Andric // Delete any dead instructions found thus far now that we've finished an
148*0b57cec5SDimitry Andric // iteration over all instructions in all the loop blocks.
149*0b57cec5SDimitry Andric if (!DeadInsts.empty()) {
150*0b57cec5SDimitry Andric Changed = true;
151*0b57cec5SDimitry Andric RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU);
152*0b57cec5SDimitry Andric }
153*0b57cec5SDimitry Andric
154*0b57cec5SDimitry Andric if (MSSAU && VerifyMemorySSA)
155*0b57cec5SDimitry Andric MSSA->verifyMemorySSA();
156*0b57cec5SDimitry Andric
157*0b57cec5SDimitry Andric // If we never found a PHI that needs to be simplified in the next
158*0b57cec5SDimitry Andric // iteration, we're done.
159*0b57cec5SDimitry Andric if (Next->empty())
160*0b57cec5SDimitry Andric break;
161*0b57cec5SDimitry Andric
162*0b57cec5SDimitry Andric // Otherwise, put the next set in place for the next iteration and reset it
163*0b57cec5SDimitry Andric // and the visited PHIs for that iteration.
164*0b57cec5SDimitry Andric std::swap(Next, ToSimplify);
165*0b57cec5SDimitry Andric Next->clear();
166*0b57cec5SDimitry Andric VisitedPHIs.clear();
167*0b57cec5SDimitry Andric DeadInsts.clear();
168*0b57cec5SDimitry Andric }
169*0b57cec5SDimitry Andric
170*0b57cec5SDimitry Andric return Changed;
171*0b57cec5SDimitry Andric }
172*0b57cec5SDimitry Andric
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater &)173*0b57cec5SDimitry Andric PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
174*0b57cec5SDimitry Andric LoopStandardAnalysisResults &AR,
175*0b57cec5SDimitry Andric LPMUpdater &) {
176bdd1243dSDimitry Andric std::optional<MemorySSAUpdater> MSSAU;
177*0b57cec5SDimitry Andric if (AR.MSSA) {
178*0b57cec5SDimitry Andric MSSAU = MemorySSAUpdater(AR.MSSA);
179480093f4SDimitry Andric if (VerifyMemorySSA)
180*0b57cec5SDimitry Andric AR.MSSA->verifyMemorySSA();
181*0b57cec5SDimitry Andric }
182*0b57cec5SDimitry Andric if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI,
183bdd1243dSDimitry Andric MSSAU ? &*MSSAU : nullptr))
184*0b57cec5SDimitry Andric return PreservedAnalyses::all();
185*0b57cec5SDimitry Andric
186*0b57cec5SDimitry Andric auto PA = getLoopPassPreservedAnalyses();
187*0b57cec5SDimitry Andric PA.preserveSet<CFGAnalyses>();
1888bcb0991SDimitry Andric if (AR.MSSA)
189*0b57cec5SDimitry Andric PA.preserve<MemorySSAAnalysis>();
190*0b57cec5SDimitry Andric return PA;
191*0b57cec5SDimitry Andric }
192