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"
32480093f4SDimitry Andric #include "llvm/InitializePasses.h"
33*0b57cec5SDimitry Andric #include "llvm/Pass.h"
34*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
35*0b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
36*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
37*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
38*0b57cec5SDimitry Andric #include <utility>
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric using namespace llvm;
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric #define DEBUG_TYPE "loop-instsimplify"
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric STATISTIC(NumSimplified, "Number of redundant instructions simplified");
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
47*0b57cec5SDimitry Andric                              AssumptionCache &AC, const TargetLibraryInfo &TLI,
48*0b57cec5SDimitry Andric                              MemorySSAUpdater *MSSAU) {
49*0b57cec5SDimitry Andric   const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
50*0b57cec5SDimitry Andric   SimplifyQuery SQ(DL, &TLI, &DT, &AC);
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   // On the first pass over the loop body we try to simplify every instruction.
53*0b57cec5SDimitry Andric   // On subsequent passes, we can restrict this to only simplifying instructions
54*0b57cec5SDimitry Andric   // where the inputs have been updated. We end up needing two sets: one
55*0b57cec5SDimitry Andric   // containing the instructions we are simplifying in *this* pass, and one for
56*0b57cec5SDimitry Andric   // the instructions we will want to simplify in the *next* pass. We use
57*0b57cec5SDimitry Andric   // pointers so we can swap between two stably allocated sets.
58*0b57cec5SDimitry Andric   SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric   // Track the PHI nodes that have already been visited during each iteration so
61*0b57cec5SDimitry Andric   // that we can identify when it is necessary to iterate.
62*0b57cec5SDimitry Andric   SmallPtrSet<PHINode *, 4> VisitedPHIs;
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric   // While simplifying we may discover dead code or cause code to become dead.
65*0b57cec5SDimitry Andric   // Keep track of all such instructions and we will delete them at the end.
665ffd83dbSDimitry Andric   SmallVector<WeakTrackingVH, 8> DeadInsts;
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric   // First we want to create an RPO traversal of the loop body. By processing in
69*0b57cec5SDimitry Andric   // RPO we can ensure that definitions are processed prior to uses (for non PHI
70*0b57cec5SDimitry Andric   // uses) in all cases. This ensures we maximize the simplifications in each
71*0b57cec5SDimitry Andric   // iteration over the loop and minimizes the possible causes for continuing to
72*0b57cec5SDimitry Andric   // iterate.
73*0b57cec5SDimitry Andric   LoopBlocksRPO RPOT(&L);
74*0b57cec5SDimitry Andric   RPOT.perform(&LI);
75*0b57cec5SDimitry Andric   MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric   bool Changed = false;
78*0b57cec5SDimitry Andric   for (;;) {
79*0b57cec5SDimitry Andric     if (MSSAU && VerifyMemorySSA)
80*0b57cec5SDimitry Andric       MSSA->verifyMemorySSA();
81*0b57cec5SDimitry Andric     for (BasicBlock *BB : RPOT) {
82*0b57cec5SDimitry Andric       for (Instruction &I : *BB) {
83*0b57cec5SDimitry Andric         if (auto *PI = dyn_cast<PHINode>(&I))
84*0b57cec5SDimitry Andric           VisitedPHIs.insert(PI);
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric         if (I.use_empty()) {
87*0b57cec5SDimitry Andric           if (isInstructionTriviallyDead(&I, &TLI))
88*0b57cec5SDimitry Andric             DeadInsts.push_back(&I);
89*0b57cec5SDimitry Andric           continue;
90*0b57cec5SDimitry Andric         }
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric         // We special case the first iteration which we can detect due to the
93*0b57cec5SDimitry Andric         // empty `ToSimplify` set.
94*0b57cec5SDimitry Andric         bool IsFirstIteration = ToSimplify->empty();
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric         if (!IsFirstIteration && !ToSimplify->count(&I))
97*0b57cec5SDimitry Andric           continue;
98*0b57cec5SDimitry Andric 
9981ad6265SDimitry Andric         Value *V = simplifyInstruction(&I, SQ.getWithInstruction(&I));
100*0b57cec5SDimitry Andric         if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
101*0b57cec5SDimitry Andric           continue;
102*0b57cec5SDimitry Andric 
103349cc55cSDimitry Andric         for (Use &U : llvm::make_early_inc_range(I.uses())) {
104*0b57cec5SDimitry Andric           auto *UserI = cast<Instruction>(U.getUser());
105*0b57cec5SDimitry Andric           U.set(V);
106*0b57cec5SDimitry Andric 
10781ad6265SDimitry Andric           // Do not bother dealing with unreachable code.
10881ad6265SDimitry Andric           if (!DT.isReachableFromEntry(UserI->getParent()))
10981ad6265SDimitry Andric             continue;
11081ad6265SDimitry Andric 
111*0b57cec5SDimitry Andric           // If the instruction is used by a PHI node we have already processed
112*0b57cec5SDimitry Andric           // we'll need to iterate on the loop body to converge, so add it to
113*0b57cec5SDimitry Andric           // the next set.
114*0b57cec5SDimitry Andric           if (auto *UserPI = dyn_cast<PHINode>(UserI))
115*0b57cec5SDimitry Andric             if (VisitedPHIs.count(UserPI)) {
116*0b57cec5SDimitry Andric               Next->insert(UserPI);
117*0b57cec5SDimitry Andric               continue;
118*0b57cec5SDimitry Andric             }
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric           // If we are only simplifying targeted instructions and the user is an
121*0b57cec5SDimitry Andric           // instruction in the loop body, add it to our set of targeted
122*0b57cec5SDimitry Andric           // instructions. Because we process defs before uses (outside of PHIs)
123*0b57cec5SDimitry Andric           // we won't have visited it yet.
124*0b57cec5SDimitry Andric           //
125*0b57cec5SDimitry Andric           // We also skip any uses outside of the loop being simplified. Those
126*0b57cec5SDimitry Andric           // should always be PHI nodes due to LCSSA form, and we don't want to
127*0b57cec5SDimitry Andric           // try to simplify those away.
128*0b57cec5SDimitry Andric           assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
129*0b57cec5SDimitry Andric                  "Uses outside the loop should be PHI nodes due to LCSSA!");
130*0b57cec5SDimitry Andric           if (!IsFirstIteration && L.contains(UserI))
131*0b57cec5SDimitry Andric             ToSimplify->insert(UserI);
132*0b57cec5SDimitry Andric         }
133*0b57cec5SDimitry Andric 
134*0b57cec5SDimitry Andric         if (MSSAU)
135*0b57cec5SDimitry Andric           if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V))
136*0b57cec5SDimitry Andric             if (MemoryAccess *MA = MSSA->getMemoryAccess(&I))
137*0b57cec5SDimitry Andric               if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI))
138*0b57cec5SDimitry Andric                 MA->replaceAllUsesWith(ReplacementMA);
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric         assert(I.use_empty() && "Should always have replaced all uses!");
141*0b57cec5SDimitry Andric         if (isInstructionTriviallyDead(&I, &TLI))
142*0b57cec5SDimitry Andric           DeadInsts.push_back(&I);
143*0b57cec5SDimitry Andric         ++NumSimplified;
144*0b57cec5SDimitry Andric         Changed = true;
145*0b57cec5SDimitry Andric       }
146*0b57cec5SDimitry Andric     }
147*0b57cec5SDimitry Andric 
148*0b57cec5SDimitry Andric     // Delete any dead instructions found thus far now that we've finished an
149*0b57cec5SDimitry Andric     // iteration over all instructions in all the loop blocks.
150*0b57cec5SDimitry Andric     if (!DeadInsts.empty()) {
151*0b57cec5SDimitry Andric       Changed = true;
152*0b57cec5SDimitry Andric       RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU);
153*0b57cec5SDimitry Andric     }
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric     if (MSSAU && VerifyMemorySSA)
156*0b57cec5SDimitry Andric       MSSA->verifyMemorySSA();
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric     // If we never found a PHI that needs to be simplified in the next
159*0b57cec5SDimitry Andric     // iteration, we're done.
160*0b57cec5SDimitry Andric     if (Next->empty())
161*0b57cec5SDimitry Andric       break;
162*0b57cec5SDimitry Andric 
163*0b57cec5SDimitry Andric     // Otherwise, put the next set in place for the next iteration and reset it
164*0b57cec5SDimitry Andric     // and the visited PHIs for that iteration.
165*0b57cec5SDimitry Andric     std::swap(Next, ToSimplify);
166*0b57cec5SDimitry Andric     Next->clear();
167*0b57cec5SDimitry Andric     VisitedPHIs.clear();
168*0b57cec5SDimitry Andric     DeadInsts.clear();
169*0b57cec5SDimitry Andric   }
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric   return Changed;
172*0b57cec5SDimitry Andric }
173*0b57cec5SDimitry Andric 
174*0b57cec5SDimitry Andric namespace {
175*0b57cec5SDimitry Andric 
176*0b57cec5SDimitry Andric class LoopInstSimplifyLegacyPass : public LoopPass {
177*0b57cec5SDimitry Andric public:
178*0b57cec5SDimitry Andric   static char ID; // Pass ID, replacement for typeid
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric   LoopInstSimplifyLegacyPass() : LoopPass(ID) {
181*0b57cec5SDimitry Andric     initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
182*0b57cec5SDimitry Andric   }
183*0b57cec5SDimitry Andric 
184*0b57cec5SDimitry Andric   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
185*0b57cec5SDimitry Andric     if (skipLoop(L))
186*0b57cec5SDimitry Andric       return false;
187*0b57cec5SDimitry Andric     DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
188*0b57cec5SDimitry Andric     LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
189*0b57cec5SDimitry Andric     AssumptionCache &AC =
190*0b57cec5SDimitry Andric         getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
191*0b57cec5SDimitry Andric             *L->getHeader()->getParent());
192*0b57cec5SDimitry Andric     const TargetLibraryInfo &TLI =
1938bcb0991SDimitry Andric         getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
1948bcb0991SDimitry Andric             *L->getHeader()->getParent());
195349cc55cSDimitry Andric     MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
196349cc55cSDimitry Andric     MemorySSAUpdater MSSAU(MSSA);
197*0b57cec5SDimitry Andric 
198349cc55cSDimitry Andric     return simplifyLoopInst(*L, DT, LI, AC, TLI, &MSSAU);
199*0b57cec5SDimitry Andric   }
200*0b57cec5SDimitry Andric 
201*0b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
202*0b57cec5SDimitry Andric     AU.addRequired<AssumptionCacheTracker>();
203*0b57cec5SDimitry Andric     AU.addRequired<DominatorTreeWrapperPass>();
204*0b57cec5SDimitry Andric     AU.addRequired<TargetLibraryInfoWrapperPass>();
205*0b57cec5SDimitry Andric     AU.setPreservesCFG();
206*0b57cec5SDimitry Andric     AU.addRequired<MemorySSAWrapperPass>();
207*0b57cec5SDimitry Andric     AU.addPreserved<MemorySSAWrapperPass>();
208*0b57cec5SDimitry Andric     getLoopAnalysisUsage(AU);
209*0b57cec5SDimitry Andric   }
210*0b57cec5SDimitry Andric };
211*0b57cec5SDimitry Andric 
212*0b57cec5SDimitry Andric } // end anonymous namespace
213*0b57cec5SDimitry Andric 
214*0b57cec5SDimitry Andric PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
215*0b57cec5SDimitry Andric                                             LoopStandardAnalysisResults &AR,
216*0b57cec5SDimitry Andric                                             LPMUpdater &) {
217*0b57cec5SDimitry Andric   Optional<MemorySSAUpdater> MSSAU;
218*0b57cec5SDimitry Andric   if (AR.MSSA) {
219*0b57cec5SDimitry Andric     MSSAU = MemorySSAUpdater(AR.MSSA);
220480093f4SDimitry Andric     if (VerifyMemorySSA)
221*0b57cec5SDimitry Andric       AR.MSSA->verifyMemorySSA();
222*0b57cec5SDimitry Andric   }
223*0b57cec5SDimitry Andric   if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI,
22481ad6265SDimitry Andric                         MSSAU ? MSSAU.getPointer() : nullptr))
225*0b57cec5SDimitry Andric     return PreservedAnalyses::all();
226*0b57cec5SDimitry Andric 
227*0b57cec5SDimitry Andric   auto PA = getLoopPassPreservedAnalyses();
228*0b57cec5SDimitry Andric   PA.preserveSet<CFGAnalyses>();
2298bcb0991SDimitry Andric   if (AR.MSSA)
230*0b57cec5SDimitry Andric     PA.preserve<MemorySSAAnalysis>();
231*0b57cec5SDimitry Andric   return PA;
232*0b57cec5SDimitry Andric }
233*0b57cec5SDimitry Andric 
234*0b57cec5SDimitry Andric char LoopInstSimplifyLegacyPass::ID = 0;
235*0b57cec5SDimitry Andric 
236*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
237*0b57cec5SDimitry Andric                       "Simplify instructions in loops", false, false)
238*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
239*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopPass)
240*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
241*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
242*0b57cec5SDimitry Andric INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
243*0b57cec5SDimitry Andric                     "Simplify instructions in loops", false, false)
244*0b57cec5SDimitry Andric 
245*0b57cec5SDimitry Andric Pass *llvm::createLoopInstSimplifyPass() {
246*0b57cec5SDimitry Andric   return new LoopInstSimplifyLegacyPass();
247*0b57cec5SDimitry Andric }
248