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/PointerIntPair.h" 15e6c30fddSChandler Carruth #include "llvm/ADT/STLExtras.h" 16e6c30fddSChandler Carruth #include "llvm/ADT/SmallPtrSet.h" 17e6c30fddSChandler Carruth #include "llvm/ADT/SmallVector.h" 18e6c30fddSChandler Carruth #include "llvm/ADT/Statistic.h" 19e6c30fddSChandler Carruth #include "llvm/Analysis/AssumptionCache.h" 20e6c30fddSChandler Carruth #include "llvm/Analysis/InstructionSimplify.h" 21e6c30fddSChandler Carruth #include "llvm/Analysis/LoopInfo.h" 224cbcbb07SChandler Carruth #include "llvm/Analysis/LoopIterator.h" 23e6c30fddSChandler Carruth #include "llvm/Analysis/LoopPass.h" 24c1a216b2SAlina Sbirlea #include "llvm/Analysis/MemorySSA.h" 25c1a216b2SAlina Sbirlea #include "llvm/Analysis/MemorySSAUpdater.h" 26e6c30fddSChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h" 27e6c30fddSChandler Carruth #include "llvm/IR/BasicBlock.h" 28e6c30fddSChandler Carruth #include "llvm/IR/CFG.h" 29e6c30fddSChandler Carruth #include "llvm/IR/DataLayout.h" 30e6c30fddSChandler Carruth #include "llvm/IR/Dominators.h" 31e6c30fddSChandler Carruth #include "llvm/IR/Instruction.h" 32e6c30fddSChandler Carruth #include "llvm/IR/Instructions.h" 33e6c30fddSChandler Carruth #include "llvm/IR/Module.h" 34e6c30fddSChandler Carruth #include "llvm/IR/PassManager.h" 35e6c30fddSChandler Carruth #include "llvm/IR/User.h" 3605da2fe5SReid Kleckner #include "llvm/InitializePasses.h" 37e6c30fddSChandler Carruth #include "llvm/Pass.h" 38e6c30fddSChandler Carruth #include "llvm/Support/Casting.h" 39e6c30fddSChandler Carruth #include "llvm/Transforms/Scalar.h" 40c1a216b2SAlina Sbirlea #include "llvm/Transforms/Utils/Local.h" 41e6c30fddSChandler Carruth #include "llvm/Transforms/Utils/LoopUtils.h" 42e6c30fddSChandler Carruth #include <algorithm> 43e6c30fddSChandler Carruth #include <utility> 44e6c30fddSChandler Carruth 45e6c30fddSChandler Carruth using namespace llvm; 46e6c30fddSChandler Carruth 47e6c30fddSChandler Carruth #define DEBUG_TYPE "loop-instsimplify" 48e6c30fddSChandler Carruth 49e6c30fddSChandler Carruth STATISTIC(NumSimplified, "Number of redundant instructions simplified"); 50e6c30fddSChandler Carruth 514cbcbb07SChandler Carruth static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI, 52c1a216b2SAlina Sbirlea AssumptionCache &AC, const TargetLibraryInfo &TLI, 53c1a216b2SAlina Sbirlea MemorySSAUpdater *MSSAU) { 544cbcbb07SChandler Carruth const DataLayout &DL = L.getHeader()->getModule()->getDataLayout(); 554cbcbb07SChandler Carruth SimplifyQuery SQ(DL, &TLI, &DT, &AC); 56e6c30fddSChandler Carruth 574cbcbb07SChandler Carruth // On the first pass over the loop body we try to simplify every instruction. 584cbcbb07SChandler Carruth // On subsequent passes, we can restrict this to only simplifying instructions 594cbcbb07SChandler Carruth // where the inputs have been updated. We end up needing two sets: one 604cbcbb07SChandler Carruth // containing the instructions we are simplifying in *this* pass, and one for 614cbcbb07SChandler Carruth // the instructions we will want to simplify in the *next* pass. We use 624cbcbb07SChandler Carruth // pointers so we can swap between two stably allocated sets. 63e6c30fddSChandler Carruth SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 64e6c30fddSChandler Carruth 654cbcbb07SChandler Carruth // Track the PHI nodes that have already been visited during each iteration so 664cbcbb07SChandler Carruth // that we can identify when it is necessary to iterate. 674cbcbb07SChandler Carruth SmallPtrSet<PHINode *, 4> VisitedPHIs; 684cbcbb07SChandler Carruth 694cbcbb07SChandler Carruth // While simplifying we may discover dead code or cause code to become dead. 704cbcbb07SChandler Carruth // Keep track of all such instructions and we will delete them at the end. 714cbcbb07SChandler Carruth SmallVector<Instruction *, 8> DeadInsts; 724cbcbb07SChandler Carruth 734cbcbb07SChandler Carruth // First we want to create an RPO traversal of the loop body. By processing in 744cbcbb07SChandler Carruth // RPO we can ensure that definitions are processed prior to uses (for non PHI 754cbcbb07SChandler Carruth // uses) in all cases. This ensures we maximize the simplifications in each 764cbcbb07SChandler Carruth // iteration over the loop and minimizes the possible causes for continuing to 774cbcbb07SChandler Carruth // iterate. 784cbcbb07SChandler Carruth LoopBlocksRPO RPOT(&L); 794cbcbb07SChandler Carruth RPOT.perform(&LI); 80c1a216b2SAlina Sbirlea MemorySSA *MSSA = MSSAU ? MSSAU->getMemorySSA() : nullptr; 81e6c30fddSChandler Carruth 82e6c30fddSChandler Carruth bool Changed = false; 834cbcbb07SChandler Carruth for (;;) { 84c1a216b2SAlina Sbirlea if (MSSAU && VerifyMemorySSA) 85c1a216b2SAlina Sbirlea MSSA->verifyMemorySSA(); 864cbcbb07SChandler Carruth for (BasicBlock *BB : RPOT) { 874cbcbb07SChandler Carruth for (Instruction &I : *BB) { 884cbcbb07SChandler Carruth if (auto *PI = dyn_cast<PHINode>(&I)) 894cbcbb07SChandler Carruth VisitedPHIs.insert(PI); 90e6c30fddSChandler Carruth 914cbcbb07SChandler Carruth if (I.use_empty()) { 924cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI)) 934cbcbb07SChandler Carruth DeadInsts.push_back(&I); 944cbcbb07SChandler Carruth continue; 954cbcbb07SChandler Carruth } 96e6c30fddSChandler Carruth 974cbcbb07SChandler Carruth // We special case the first iteration which we can detect due to the 984cbcbb07SChandler Carruth // empty `ToSimplify` set. 994cbcbb07SChandler Carruth bool IsFirstIteration = ToSimplify->empty(); 100e6c30fddSChandler Carruth 1014cbcbb07SChandler Carruth if (!IsFirstIteration && !ToSimplify->count(&I)) 102e6c30fddSChandler Carruth continue; 103e6c30fddSChandler Carruth 1044cbcbb07SChandler Carruth Value *V = SimplifyInstruction(&I, SQ.getWithInstruction(&I)); 1054cbcbb07SChandler Carruth if (!V || !LI.replacementPreservesLCSSAForm(&I, V)) 1064cbcbb07SChandler Carruth continue; 107e6c30fddSChandler Carruth 1084cbcbb07SChandler Carruth for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); 1094cbcbb07SChandler Carruth UI != UE;) { 1104cbcbb07SChandler Carruth Use &U = *UI++; 1114cbcbb07SChandler Carruth auto *UserI = cast<Instruction>(U.getUser()); 1124cbcbb07SChandler Carruth U.set(V); 1134cbcbb07SChandler Carruth 1144cbcbb07SChandler Carruth // If the instruction is used by a PHI node we have already processed 1154cbcbb07SChandler Carruth // we'll need to iterate on the loop body to converge, so add it to 1164cbcbb07SChandler Carruth // the next set. 1174cbcbb07SChandler Carruth if (auto *UserPI = dyn_cast<PHINode>(UserI)) 1184cbcbb07SChandler Carruth if (VisitedPHIs.count(UserPI)) { 1194cbcbb07SChandler Carruth Next->insert(UserPI); 1204cbcbb07SChandler Carruth continue; 1214cbcbb07SChandler Carruth } 1224cbcbb07SChandler Carruth 1234cbcbb07SChandler Carruth // If we are only simplifying targeted instructions and the user is an 1244cbcbb07SChandler Carruth // instruction in the loop body, add it to our set of targeted 1254cbcbb07SChandler Carruth // instructions. Because we process defs before uses (outside of PHIs) 1264cbcbb07SChandler Carruth // we won't have visited it yet. 1274cbcbb07SChandler Carruth // 1284cbcbb07SChandler Carruth // We also skip any uses outside of the loop being simplified. Those 1294cbcbb07SChandler Carruth // should always be PHI nodes due to LCSSA form, and we don't want to 1304cbcbb07SChandler Carruth // try to simplify those away. 1314cbcbb07SChandler Carruth assert((L.contains(UserI) || isa<PHINode>(UserI)) && 1324cbcbb07SChandler Carruth "Uses outside the loop should be PHI nodes due to LCSSA!"); 1334cbcbb07SChandler Carruth if (!IsFirstIteration && L.contains(UserI)) 1344cbcbb07SChandler Carruth ToSimplify->insert(UserI); 1354cbcbb07SChandler Carruth } 1364cbcbb07SChandler Carruth 137c1a216b2SAlina Sbirlea if (MSSAU) 138c1a216b2SAlina Sbirlea if (Instruction *SimpleI = dyn_cast_or_null<Instruction>(V)) 139c1a216b2SAlina Sbirlea if (MemoryAccess *MA = MSSA->getMemoryAccess(&I)) 140c1a216b2SAlina Sbirlea if (MemoryAccess *ReplacementMA = MSSA->getMemoryAccess(SimpleI)) 141c1a216b2SAlina Sbirlea MA->replaceAllUsesWith(ReplacementMA); 142c1a216b2SAlina Sbirlea 1434cbcbb07SChandler Carruth assert(I.use_empty() && "Should always have replaced all uses!"); 1444cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI)) 1454cbcbb07SChandler Carruth DeadInsts.push_back(&I); 146e6c30fddSChandler Carruth ++NumSimplified; 1474cbcbb07SChandler Carruth Changed = true; 148e6c30fddSChandler Carruth } 149e6c30fddSChandler Carruth } 150e6c30fddSChandler Carruth 1514cbcbb07SChandler Carruth // Delete any dead instructions found thus far now that we've finished an 1524cbcbb07SChandler Carruth // iteration over all instructions in all the loop blocks. 1534cbcbb07SChandler Carruth if (!DeadInsts.empty()) { 1544cbcbb07SChandler Carruth Changed = true; 155c1a216b2SAlina Sbirlea RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI, MSSAU); 1564cbcbb07SChandler Carruth } 1574cbcbb07SChandler Carruth 158c1a216b2SAlina Sbirlea if (MSSAU && VerifyMemorySSA) 159c1a216b2SAlina Sbirlea MSSA->verifyMemorySSA(); 160c1a216b2SAlina Sbirlea 1614cbcbb07SChandler Carruth // If we never found a PHI that needs to be simplified in the next 1624cbcbb07SChandler Carruth // iteration, we're done. 1634cbcbb07SChandler Carruth if (Next->empty()) 164e6c30fddSChandler Carruth break; 165e6c30fddSChandler Carruth 1664cbcbb07SChandler Carruth // Otherwise, put the next set in place for the next iteration and reset it 1674cbcbb07SChandler Carruth // and the visited PHIs for that iteration. 1684cbcbb07SChandler Carruth std::swap(Next, ToSimplify); 169e6c30fddSChandler Carruth Next->clear(); 1704cbcbb07SChandler Carruth VisitedPHIs.clear(); 1714cbcbb07SChandler Carruth DeadInsts.clear(); 1724cbcbb07SChandler Carruth } 173e6c30fddSChandler Carruth 174e6c30fddSChandler Carruth return Changed; 175e6c30fddSChandler Carruth } 176e6c30fddSChandler Carruth 177e6c30fddSChandler Carruth namespace { 178e6c30fddSChandler Carruth 179e6c30fddSChandler Carruth class LoopInstSimplifyLegacyPass : public LoopPass { 180e6c30fddSChandler Carruth public: 181e6c30fddSChandler Carruth static char ID; // Pass ID, replacement for typeid 182e6c30fddSChandler Carruth 183e6c30fddSChandler Carruth LoopInstSimplifyLegacyPass() : LoopPass(ID) { 184e6c30fddSChandler Carruth initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry()); 185e6c30fddSChandler Carruth } 186e6c30fddSChandler Carruth 187e6c30fddSChandler Carruth bool runOnLoop(Loop *L, LPPassManager &LPM) override { 188e6c30fddSChandler Carruth if (skipLoop(L)) 189e6c30fddSChandler Carruth return false; 1904cbcbb07SChandler Carruth DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 1914cbcbb07SChandler Carruth LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 1924cbcbb07SChandler Carruth AssumptionCache &AC = 1934cbcbb07SChandler Carruth getAnalysis<AssumptionCacheTracker>().getAssumptionCache( 194e6c30fddSChandler Carruth *L->getHeader()->getParent()); 1954cbcbb07SChandler Carruth const TargetLibraryInfo &TLI = 1969c27b59cSTeresa Johnson getAnalysis<TargetLibraryInfoWrapperPass>().getTLI( 1979c27b59cSTeresa Johnson *L->getHeader()->getParent()); 198c1a216b2SAlina Sbirlea MemorySSA *MSSA = nullptr; 199c1a216b2SAlina Sbirlea Optional<MemorySSAUpdater> MSSAU; 200c1a216b2SAlina Sbirlea if (EnableMSSALoopDependency) { 201c1a216b2SAlina Sbirlea MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA(); 202c1a216b2SAlina Sbirlea MSSAU = MemorySSAUpdater(MSSA); 203c1a216b2SAlina Sbirlea } 204e6c30fddSChandler Carruth 205c1a216b2SAlina Sbirlea return simplifyLoopInst(*L, DT, LI, AC, TLI, 206c1a216b2SAlina Sbirlea MSSAU.hasValue() ? MSSAU.getPointer() : nullptr); 207e6c30fddSChandler Carruth } 208e6c30fddSChandler Carruth 209e6c30fddSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 210e6c30fddSChandler Carruth AU.addRequired<AssumptionCacheTracker>(); 2114cbcbb07SChandler Carruth AU.addRequired<DominatorTreeWrapperPass>(); 212e6c30fddSChandler Carruth AU.addRequired<TargetLibraryInfoWrapperPass>(); 213e6c30fddSChandler Carruth AU.setPreservesCFG(); 214c1a216b2SAlina Sbirlea if (EnableMSSALoopDependency) { 215c1a216b2SAlina Sbirlea AU.addRequired<MemorySSAWrapperPass>(); 216c1a216b2SAlina Sbirlea AU.addPreserved<MemorySSAWrapperPass>(); 217c1a216b2SAlina Sbirlea } 218e6c30fddSChandler Carruth getLoopAnalysisUsage(AU); 219e6c30fddSChandler Carruth } 220e6c30fddSChandler Carruth }; 221e6c30fddSChandler Carruth 222e6c30fddSChandler Carruth } // end anonymous namespace 223e6c30fddSChandler Carruth 224e6c30fddSChandler Carruth PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM, 225e6c30fddSChandler Carruth LoopStandardAnalysisResults &AR, 226e6c30fddSChandler Carruth LPMUpdater &) { 227c1a216b2SAlina Sbirlea Optional<MemorySSAUpdater> MSSAU; 228c1a216b2SAlina Sbirlea if (AR.MSSA) { 229c1a216b2SAlina Sbirlea MSSAU = MemorySSAUpdater(AR.MSSA); 230*fa09ddddSAlina Sbirlea if (VerifyMemorySSA) 231c1a216b2SAlina Sbirlea AR.MSSA->verifyMemorySSA(); 232c1a216b2SAlina Sbirlea } 233c1a216b2SAlina Sbirlea if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI, 234c1a216b2SAlina Sbirlea MSSAU.hasValue() ? MSSAU.getPointer() : nullptr)) 235e6c30fddSChandler Carruth return PreservedAnalyses::all(); 236e6c30fddSChandler Carruth 237e6c30fddSChandler Carruth auto PA = getLoopPassPreservedAnalyses(); 238e6c30fddSChandler Carruth PA.preserveSet<CFGAnalyses>(); 239f92109dcSAlina Sbirlea if (AR.MSSA) 2403cef1f7dSAlina Sbirlea PA.preserve<MemorySSAAnalysis>(); 241e6c30fddSChandler Carruth return PA; 242e6c30fddSChandler Carruth } 243e6c30fddSChandler Carruth 244e6c30fddSChandler Carruth char LoopInstSimplifyLegacyPass::ID = 0; 245e6c30fddSChandler Carruth 246e6c30fddSChandler Carruth INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify", 247e6c30fddSChandler Carruth "Simplify instructions in loops", false, false) 248e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 249e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopPass) 250c1a216b2SAlina Sbirlea INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass) 251e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 252e6c30fddSChandler Carruth INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify", 253e6c30fddSChandler Carruth "Simplify instructions in loops", false, false) 254e6c30fddSChandler Carruth 255e6c30fddSChandler Carruth Pass *llvm::createLoopInstSimplifyPass() { 256e6c30fddSChandler Carruth return new LoopInstSimplifyLegacyPass(); 257e6c30fddSChandler Carruth } 258