1e6c30fddSChandler Carruth //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
2e6c30fddSChandler Carruth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6c30fddSChandler Carruth //
7e6c30fddSChandler Carruth //===----------------------------------------------------------------------===//
8e6c30fddSChandler Carruth //
9e6c30fddSChandler Carruth // This pass performs lightweight instruction simplification on loop bodies.
10e6c30fddSChandler Carruth //
11e6c30fddSChandler Carruth //===----------------------------------------------------------------------===//
12e6c30fddSChandler Carruth
13e6c30fddSChandler Carruth #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
14e6c30fddSChandler Carruth #include "llvm/ADT/STLExtras.h"
15e6c30fddSChandler Carruth #include "llvm/ADT/SmallPtrSet.h"
16e6c30fddSChandler Carruth #include "llvm/ADT/SmallVector.h"
17e6c30fddSChandler Carruth #include "llvm/ADT/Statistic.h"
18e6c30fddSChandler Carruth #include "llvm/Analysis/AssumptionCache.h"
19e6c30fddSChandler Carruth #include "llvm/Analysis/InstructionSimplify.h"
20e6c30fddSChandler Carruth #include "llvm/Analysis/LoopInfo.h"
214cbcbb07SChandler Carruth #include "llvm/Analysis/LoopIterator.h"
22e6c30fddSChandler Carruth #include "llvm/Analysis/LoopPass.h"
23c1a216b2SAlina Sbirlea #include "llvm/Analysis/MemorySSA.h"
24c1a216b2SAlina Sbirlea #include "llvm/Analysis/MemorySSAUpdater.h"
25e6c30fddSChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h"
26e6c30fddSChandler Carruth #include "llvm/IR/BasicBlock.h"
27e6c30fddSChandler Carruth #include "llvm/IR/Dominators.h"
28e6c30fddSChandler Carruth #include "llvm/IR/Instruction.h"
29e6c30fddSChandler Carruth #include "llvm/IR/Instructions.h"
30e6c30fddSChandler Carruth #include "llvm/IR/Module.h"
31e6c30fddSChandler Carruth #include "llvm/IR/PassManager.h"
3205da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
33e6c30fddSChandler Carruth #include "llvm/Pass.h"
34e6c30fddSChandler Carruth #include "llvm/Support/Casting.h"
35e6c30fddSChandler Carruth #include "llvm/Transforms/Scalar.h"
36c1a216b2SAlina Sbirlea #include "llvm/Transforms/Utils/Local.h"
37e6c30fddSChandler Carruth #include "llvm/Transforms/Utils/LoopUtils.h"
38e6c30fddSChandler Carruth #include <utility>
39e6c30fddSChandler Carruth
40e6c30fddSChandler Carruth using namespace llvm;
41e6c30fddSChandler Carruth
42e6c30fddSChandler Carruth #define DEBUG_TYPE "loop-instsimplify"
43e6c30fddSChandler Carruth
44e6c30fddSChandler Carruth STATISTIC(NumSimplified, "Number of redundant instructions simplified");
45e6c30fddSChandler Carruth
simplifyLoopInst(Loop & L,DominatorTree & DT,LoopInfo & LI,AssumptionCache & AC,const TargetLibraryInfo & TLI,MemorySSAUpdater * MSSAU)464cbcbb07SChandler Carruth static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
47c1a216b2SAlina Sbirlea AssumptionCache &AC, const TargetLibraryInfo &TLI,
48c1a216b2SAlina Sbirlea MemorySSAUpdater *MSSAU) {
494cbcbb07SChandler Carruth const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
504cbcbb07SChandler Carruth SimplifyQuery SQ(DL, &TLI, &DT, &AC);
51e6c30fddSChandler Carruth
524cbcbb07SChandler Carruth // On the first pass over the loop body we try to simplify every instruction.
534cbcbb07SChandler Carruth // On subsequent passes, we can restrict this to only simplifying instructions
544cbcbb07SChandler Carruth // where the inputs have been updated. We end up needing two sets: one
554cbcbb07SChandler Carruth // containing the instructions we are simplifying in *this* pass, and one for
564cbcbb07SChandler Carruth // the instructions we will want to simplify in the *next* pass. We use
574cbcbb07SChandler Carruth // pointers so we can swap between two stably allocated sets.
58e6c30fddSChandler Carruth SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
59e6c30fddSChandler Carruth
604cbcbb07SChandler Carruth // Track the PHI nodes that have already been visited during each iteration so
614cbcbb07SChandler Carruth // that we can identify when it is necessary to iterate.
624cbcbb07SChandler Carruth SmallPtrSet<PHINode *, 4> VisitedPHIs;
634cbcbb07SChandler Carruth
644cbcbb07SChandler Carruth // While simplifying we may discover dead code or cause code to become dead.
654cbcbb07SChandler Carruth // Keep track of all such instructions and we will delete them at the end.
669e66c4ecSAlina Sbirlea SmallVector<WeakTrackingVH, 8> DeadInsts;
674cbcbb07SChandler Carruth
684cbcbb07SChandler Carruth // First we want to create an RPO traversal of the loop body. By processing in
694cbcbb07SChandler Carruth // RPO we can ensure that definitions are processed prior to uses (for non PHI
704cbcbb07SChandler Carruth // uses) in all cases. This ensures we maximize the simplifications in each
714cbcbb07SChandler Carruth // iteration over the loop and minimizes the possible causes for continuing to
724cbcbb07SChandler Carruth // iterate.
734cbcbb07SChandler Carruth LoopBlocksRPO RPOT(&L);
744cbcbb07SChandler Carruth RPOT.perform(&LI);
75c1a216b2SAlina Sbirlea MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr;
76e6c30fddSChandler Carruth
77e6c30fddSChandler Carruth bool Changed = false;
784cbcbb07SChandler Carruth for (;;) {
79c1a216b2SAlina Sbirlea if (MSSAU && VerifyMemorySSA)
80c1a216b2SAlina Sbirlea MSSA->verifyMemorySSA();
814cbcbb07SChandler Carruth for (BasicBlock *BB : RPOT) {
824cbcbb07SChandler Carruth for (Instruction &I : *BB) {
834cbcbb07SChandler Carruth if (auto *PI = dyn_cast<PHINode>(&I))
844cbcbb07SChandler Carruth VisitedPHIs.insert(PI);
85e6c30fddSChandler Carruth
864cbcbb07SChandler Carruth if (I.use_empty()) {
874cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI))
884cbcbb07SChandler Carruth DeadInsts.push_back(&I);
894cbcbb07SChandler Carruth continue;
904cbcbb07SChandler Carruth }
91e6c30fddSChandler Carruth
924cbcbb07SChandler Carruth // We special case the first iteration which we can detect due to the
934cbcbb07SChandler Carruth // empty `ToSimplify` set.
944cbcbb07SChandler Carruth bool IsFirstIteration = ToSimplify->empty();
95e6c30fddSChandler Carruth
964cbcbb07SChandler Carruth if (!IsFirstIteration && !ToSimplify->count(&I))
97e6c30fddSChandler Carruth continue;
98e6c30fddSChandler Carruth
99b8c2781fSSimon Moll Value *V = simplifyInstruction(&I, SQ.getWithInstruction(&I));
1004cbcbb07SChandler Carruth if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
1014cbcbb07SChandler Carruth continue;
102e6c30fddSChandler Carruth
1038e86c0e4SKazu Hirata for (Use &U : llvm::make_early_inc_range(I.uses())) {
1044cbcbb07SChandler Carruth auto *UserI = cast<Instruction>(U.getUser());
1054cbcbb07SChandler Carruth U.set(V);
1064cbcbb07SChandler Carruth
107606a000dSMax Kazantsev // Do not bother dealing with unreachable code.
108606a000dSMax Kazantsev if (!DT.isReachableFromEntry(UserI->getParent()))
109606a000dSMax Kazantsev continue;
110606a000dSMax Kazantsev
1114cbcbb07SChandler Carruth // If the instruction is used by a PHI node we have already processed
1124cbcbb07SChandler Carruth // we'll need to iterate on the loop body to converge, so add it to
1134cbcbb07SChandler Carruth // the next set.
1144cbcbb07SChandler Carruth if (auto *UserPI = dyn_cast<PHINode>(UserI))
1154cbcbb07SChandler Carruth if (VisitedPHIs.count(UserPI)) {
1164cbcbb07SChandler Carruth Next->insert(UserPI);
1174cbcbb07SChandler Carruth continue;
1184cbcbb07SChandler Carruth }
1194cbcbb07SChandler Carruth
1204cbcbb07SChandler Carruth // If we are only simplifying targeted instructions and the user is an
1214cbcbb07SChandler Carruth // instruction in the loop body, add it to our set of targeted
1224cbcbb07SChandler Carruth // instructions. Because we process defs before uses (outside of PHIs)
1234cbcbb07SChandler Carruth // we won't have visited it yet.
1244cbcbb07SChandler Carruth //
1254cbcbb07SChandler Carruth // We also skip any uses outside of the loop being simplified. Those
1264cbcbb07SChandler Carruth // should always be PHI nodes due to LCSSA form, and we don't want to
1274cbcbb07SChandler Carruth // try to simplify those away.
1284cbcbb07SChandler Carruth assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
1294cbcbb07SChandler Carruth "Uses outside the loop should be PHI nodes due to LCSSA!");
1304cbcbb07SChandler Carruth if (!IsFirstIteration && L.contains(UserI))
1314cbcbb07SChandler Carruth ToSimplify->insert(UserI);
1324cbcbb07SChandler Carruth }
1334cbcbb07SChandler Carruth
134c1a216b2SAlina Sbirlea if (MSSAU)
135c1a216b2SAlina Sbirlea if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V))
136c1a216b2SAlina Sbirlea if (MemoryAccess *MA = MSSA->getMemoryAccess(&I))
137c1a216b2SAlina Sbirlea if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI))
138c1a216b2SAlina Sbirlea MA->replaceAllUsesWith(ReplacementMA);
139c1a216b2SAlina Sbirlea
1404cbcbb07SChandler Carruth assert(I.use_empty() && "Should always have replaced all uses!");
1414cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI))
1424cbcbb07SChandler Carruth DeadInsts.push_back(&I);
143e6c30fddSChandler Carruth ++NumSimplified;
1444cbcbb07SChandler Carruth Changed = true;
145e6c30fddSChandler Carruth }
146e6c30fddSChandler Carruth }
147e6c30fddSChandler Carruth
1484cbcbb07SChandler Carruth // Delete any dead instructions found thus far now that we've finished an
1494cbcbb07SChandler Carruth // iteration over all instructions in all the loop blocks.
1504cbcbb07SChandler Carruth if (!DeadInsts.empty()) {
1514cbcbb07SChandler Carruth Changed = true;
152c1a216b2SAlina Sbirlea RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU);
1534cbcbb07SChandler Carruth }
1544cbcbb07SChandler Carruth
155c1a216b2SAlina Sbirlea if (MSSAU && VerifyMemorySSA)
156c1a216b2SAlina Sbirlea MSSA->verifyMemorySSA();
157c1a216b2SAlina Sbirlea
1584cbcbb07SChandler Carruth // If we never found a PHI that needs to be simplified in the next
1594cbcbb07SChandler Carruth // iteration, we're done.
1604cbcbb07SChandler Carruth if (Next->empty())
161e6c30fddSChandler Carruth break;
162e6c30fddSChandler Carruth
1634cbcbb07SChandler Carruth // Otherwise, put the next set in place for the next iteration and reset it
1644cbcbb07SChandler Carruth // and the visited PHIs for that iteration.
1654cbcbb07SChandler Carruth std::swap(Next, ToSimplify);
166e6c30fddSChandler Carruth Next->clear();
1674cbcbb07SChandler Carruth VisitedPHIs.clear();
1684cbcbb07SChandler Carruth DeadInsts.clear();
1694cbcbb07SChandler Carruth }
170e6c30fddSChandler Carruth
171e6c30fddSChandler Carruth return Changed;
172e6c30fddSChandler Carruth }
173e6c30fddSChandler Carruth
174e6c30fddSChandler Carruth namespace {
175e6c30fddSChandler Carruth
176e6c30fddSChandler Carruth class LoopInstSimplifyLegacyPass : public LoopPass {
177e6c30fddSChandler Carruth public:
178e6c30fddSChandler Carruth static char ID; // Pass ID, replacement for typeid
179e6c30fddSChandler Carruth
LoopInstSimplifyLegacyPass()180e6c30fddSChandler Carruth LoopInstSimplifyLegacyPass() : LoopPass(ID) {
181e6c30fddSChandler Carruth initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
182e6c30fddSChandler Carruth }
183e6c30fddSChandler Carruth
runOnLoop(Loop * L,LPPassManager & LPM)184e6c30fddSChandler Carruth bool runOnLoop(Loop *L, LPPassManager &LPM) override {
185e6c30fddSChandler Carruth if (skipLoop(L))
186e6c30fddSChandler Carruth return false;
1874cbcbb07SChandler Carruth DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1884cbcbb07SChandler Carruth LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1894cbcbb07SChandler Carruth AssumptionCache &AC =
1904cbcbb07SChandler Carruth getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
191e6c30fddSChandler Carruth *L->getHeader()->getParent());
1924cbcbb07SChandler Carruth const TargetLibraryInfo &TLI =
1939c27b59cSTeresa Johnson getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
1949c27b59cSTeresa Johnson *L->getHeader()->getParent());
195735a5904SNikita Popov MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
196735a5904SNikita Popov MemorySSAUpdater MSSAU(MSSA);
197e6c30fddSChandler Carruth
198735a5904SNikita Popov return simplifyLoopInst(*L, DT, LI, AC, TLI, &MSSAU);
199e6c30fddSChandler Carruth }
200e6c30fddSChandler Carruth
getAnalysisUsage(AnalysisUsage & AU) const201e6c30fddSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override {
202e6c30fddSChandler Carruth AU.addRequired<AssumptionCacheTracker>();
2034cbcbb07SChandler Carruth AU.addRequired<DominatorTreeWrapperPass>();
204e6c30fddSChandler Carruth AU.addRequired<TargetLibraryInfoWrapperPass>();
205e6c30fddSChandler Carruth AU.setPreservesCFG();
206c1a216b2SAlina Sbirlea AU.addRequired<MemorySSAWrapperPass>();
207c1a216b2SAlina Sbirlea AU.addPreserved<MemorySSAWrapperPass>();
208e6c30fddSChandler Carruth getLoopAnalysisUsage(AU);
209e6c30fddSChandler Carruth }
210e6c30fddSChandler Carruth };
211e6c30fddSChandler Carruth
212e6c30fddSChandler Carruth } // end anonymous namespace
213e6c30fddSChandler Carruth
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater &)214e6c30fddSChandler Carruth PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
215e6c30fddSChandler Carruth LoopStandardAnalysisResults &AR,
216e6c30fddSChandler Carruth LPMUpdater &) {
217c1a216b2SAlina Sbirlea Optional<MemorySSAUpdater> MSSAU;
218c1a216b2SAlina Sbirlea if (AR.MSSA) {
219c1a216b2SAlina Sbirlea MSSAU = MemorySSAUpdater(AR.MSSA);
220fa09ddddSAlina Sbirlea if (VerifyMemorySSA)
221c1a216b2SAlina Sbirlea AR.MSSA->verifyMemorySSA();
222c1a216b2SAlina Sbirlea }
223c1a216b2SAlina Sbirlea if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI,
224*0916d96dSKazu Hirata MSSAU ? MSSAU.getPointer() : nullptr))
225e6c30fddSChandler Carruth return PreservedAnalyses::all();
226e6c30fddSChandler Carruth
227e6c30fddSChandler Carruth auto PA = getLoopPassPreservedAnalyses();
228e6c30fddSChandler Carruth PA.preserveSet<CFGAnalyses>();
229f92109dcSAlina Sbirlea if (AR.MSSA)
2303cef1f7dSAlina Sbirlea PA.preserve<MemorySSAAnalysis>();
231e6c30fddSChandler Carruth return PA;
232e6c30fddSChandler Carruth }
233e6c30fddSChandler Carruth
234e6c30fddSChandler Carruth char LoopInstSimplifyLegacyPass::ID = 0;
235e6c30fddSChandler Carruth
236e6c30fddSChandler Carruth INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
237e6c30fddSChandler Carruth "Simplify instructions in loops", false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)238e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
239e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopPass)
240c1a216b2SAlina Sbirlea INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
241e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
242e6c30fddSChandler Carruth INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
243e6c30fddSChandler Carruth "Simplify instructions in loops", false, false)
244e6c30fddSChandler Carruth
245e6c30fddSChandler Carruth Pass *llvm::createLoopInstSimplifyPass() {
246e6c30fddSChandler Carruth return new LoopInstSimplifyLegacyPass();
247e6c30fddSChandler Carruth }
248