1e6c30fddSChandler Carruth //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===// 2e6c30fddSChandler Carruth // 3e6c30fddSChandler Carruth // The LLVM Compiler Infrastructure 4e6c30fddSChandler Carruth // 5e6c30fddSChandler Carruth // This file is distributed under the University of Illinois Open Source 6e6c30fddSChandler Carruth // License. See LICENSE.TXT for details. 7e6c30fddSChandler Carruth // 8e6c30fddSChandler Carruth //===----------------------------------------------------------------------===// 9e6c30fddSChandler Carruth // 10e6c30fddSChandler Carruth // This pass performs lightweight instruction simplification on loop bodies. 11e6c30fddSChandler Carruth // 12e6c30fddSChandler Carruth //===----------------------------------------------------------------------===// 13e6c30fddSChandler Carruth 14e6c30fddSChandler Carruth #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 15e6c30fddSChandler Carruth #include "llvm/ADT/PointerIntPair.h" 16e6c30fddSChandler Carruth #include "llvm/ADT/STLExtras.h" 17e6c30fddSChandler Carruth #include "llvm/ADT/SmallPtrSet.h" 18e6c30fddSChandler Carruth #include "llvm/ADT/SmallVector.h" 19e6c30fddSChandler Carruth #include "llvm/ADT/Statistic.h" 20e6c30fddSChandler Carruth #include "llvm/Analysis/AssumptionCache.h" 21e6c30fddSChandler Carruth #include "llvm/Analysis/InstructionSimplify.h" 22e6c30fddSChandler Carruth #include "llvm/Analysis/LoopInfo.h" 23*4cbcbb07SChandler Carruth #include "llvm/Analysis/LoopIterator.h" 24e6c30fddSChandler Carruth #include "llvm/Analysis/LoopPass.h" 25e6c30fddSChandler Carruth #include "llvm/Analysis/TargetLibraryInfo.h" 26e6c30fddSChandler Carruth #include "llvm/Analysis/Utils/Local.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" 36e6c30fddSChandler Carruth #include "llvm/Pass.h" 37e6c30fddSChandler Carruth #include "llvm/Support/Casting.h" 38e6c30fddSChandler Carruth #include "llvm/Transforms/Scalar.h" 39e6c30fddSChandler Carruth #include "llvm/Transforms/Utils/LoopUtils.h" 40e6c30fddSChandler Carruth #include <algorithm> 41e6c30fddSChandler Carruth #include <utility> 42e6c30fddSChandler Carruth 43e6c30fddSChandler Carruth using namespace llvm; 44e6c30fddSChandler Carruth 45e6c30fddSChandler Carruth #define DEBUG_TYPE "loop-instsimplify" 46e6c30fddSChandler Carruth 47e6c30fddSChandler Carruth STATISTIC(NumSimplified, "Number of redundant instructions simplified"); 48e6c30fddSChandler Carruth 49*4cbcbb07SChandler Carruth static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI, 50*4cbcbb07SChandler Carruth AssumptionCache &AC, 51*4cbcbb07SChandler Carruth const TargetLibraryInfo &TLI) { 52*4cbcbb07SChandler Carruth const DataLayout &DL = L.getHeader()->getModule()->getDataLayout(); 53*4cbcbb07SChandler Carruth SimplifyQuery SQ(DL, &TLI, &DT, &AC); 54e6c30fddSChandler Carruth 55*4cbcbb07SChandler Carruth // On the first pass over the loop body we try to simplify every instruction. 56*4cbcbb07SChandler Carruth // On subsequent passes, we can restrict this to only simplifying instructions 57*4cbcbb07SChandler Carruth // where the inputs have been updated. We end up needing two sets: one 58*4cbcbb07SChandler Carruth // containing the instructions we are simplifying in *this* pass, and one for 59*4cbcbb07SChandler Carruth // the instructions we will want to simplify in the *next* pass. We use 60*4cbcbb07SChandler Carruth // pointers so we can swap between two stably allocated sets. 61e6c30fddSChandler Carruth SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 62e6c30fddSChandler Carruth 63*4cbcbb07SChandler Carruth // Track the PHI nodes that have already been visited during each iteration so 64*4cbcbb07SChandler Carruth // that we can identify when it is necessary to iterate. 65*4cbcbb07SChandler Carruth SmallPtrSet<PHINode *, 4> VisitedPHIs; 66*4cbcbb07SChandler Carruth 67*4cbcbb07SChandler Carruth // While simplifying we may discover dead code or cause code to become dead. 68*4cbcbb07SChandler Carruth // Keep track of all such instructions and we will delete them at the end. 69*4cbcbb07SChandler Carruth SmallVector<Instruction *, 8> DeadInsts; 70*4cbcbb07SChandler Carruth 71*4cbcbb07SChandler Carruth // First we want to create an RPO traversal of the loop body. By processing in 72*4cbcbb07SChandler Carruth // RPO we can ensure that definitions are processed prior to uses (for non PHI 73*4cbcbb07SChandler Carruth // uses) in all cases. This ensures we maximize the simplifications in each 74*4cbcbb07SChandler Carruth // iteration over the loop and minimizes the possible causes for continuing to 75*4cbcbb07SChandler Carruth // iterate. 76*4cbcbb07SChandler Carruth LoopBlocksRPO RPOT(&L); 77*4cbcbb07SChandler Carruth RPOT.perform(&LI); 78e6c30fddSChandler Carruth 79e6c30fddSChandler Carruth bool Changed = false; 80*4cbcbb07SChandler Carruth for (;;) { 81*4cbcbb07SChandler Carruth for (BasicBlock *BB : RPOT) { 82*4cbcbb07SChandler Carruth for (Instruction &I : *BB) { 83*4cbcbb07SChandler Carruth if (auto *PI = dyn_cast<PHINode>(&I)) 84*4cbcbb07SChandler Carruth VisitedPHIs.insert(PI); 85e6c30fddSChandler Carruth 86*4cbcbb07SChandler Carruth if (I.use_empty()) { 87*4cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI)) 88*4cbcbb07SChandler Carruth DeadInsts.push_back(&I); 89*4cbcbb07SChandler Carruth continue; 90*4cbcbb07SChandler Carruth } 91e6c30fddSChandler Carruth 92*4cbcbb07SChandler Carruth // We special case the first iteration which we can detect due to the 93*4cbcbb07SChandler Carruth // empty `ToSimplify` set. 94*4cbcbb07SChandler Carruth bool IsFirstIteration = ToSimplify->empty(); 95e6c30fddSChandler Carruth 96*4cbcbb07SChandler Carruth if (!IsFirstIteration && !ToSimplify->count(&I)) 97e6c30fddSChandler Carruth continue; 98e6c30fddSChandler Carruth 99*4cbcbb07SChandler Carruth Value *V = SimplifyInstruction(&I, SQ.getWithInstruction(&I)); 100*4cbcbb07SChandler Carruth if (!V || !LI.replacementPreservesLCSSAForm(&I, V)) 101*4cbcbb07SChandler Carruth continue; 102e6c30fddSChandler Carruth 103*4cbcbb07SChandler Carruth for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); 104*4cbcbb07SChandler Carruth UI != UE;) { 105*4cbcbb07SChandler Carruth Use &U = *UI++; 106*4cbcbb07SChandler Carruth auto *UserI = cast<Instruction>(U.getUser()); 107*4cbcbb07SChandler Carruth U.set(V); 108*4cbcbb07SChandler Carruth 109*4cbcbb07SChandler Carruth // If the instruction is used by a PHI node we have already processed 110*4cbcbb07SChandler Carruth // we'll need to iterate on the loop body to converge, so add it to 111*4cbcbb07SChandler Carruth // the next set. 112*4cbcbb07SChandler Carruth if (auto *UserPI = dyn_cast<PHINode>(UserI)) 113*4cbcbb07SChandler Carruth if (VisitedPHIs.count(UserPI)) { 114*4cbcbb07SChandler Carruth Next->insert(UserPI); 115*4cbcbb07SChandler Carruth continue; 116*4cbcbb07SChandler Carruth } 117*4cbcbb07SChandler Carruth 118*4cbcbb07SChandler Carruth // If we are only simplifying targeted instructions and the user is an 119*4cbcbb07SChandler Carruth // instruction in the loop body, add it to our set of targeted 120*4cbcbb07SChandler Carruth // instructions. Because we process defs before uses (outside of PHIs) 121*4cbcbb07SChandler Carruth // we won't have visited it yet. 122*4cbcbb07SChandler Carruth // 123*4cbcbb07SChandler Carruth // We also skip any uses outside of the loop being simplified. Those 124*4cbcbb07SChandler Carruth // should always be PHI nodes due to LCSSA form, and we don't want to 125*4cbcbb07SChandler Carruth // try to simplify those away. 126*4cbcbb07SChandler Carruth assert((L.contains(UserI) || isa<PHINode>(UserI)) && 127*4cbcbb07SChandler Carruth "Uses outside the loop should be PHI nodes due to LCSSA!"); 128*4cbcbb07SChandler Carruth if (!IsFirstIteration && L.contains(UserI)) 129*4cbcbb07SChandler Carruth ToSimplify->insert(UserI); 130*4cbcbb07SChandler Carruth } 131*4cbcbb07SChandler Carruth 132*4cbcbb07SChandler Carruth assert(I.use_empty() && "Should always have replaced all uses!"); 133*4cbcbb07SChandler Carruth if (isInstructionTriviallyDead(&I, &TLI)) 134*4cbcbb07SChandler Carruth DeadInsts.push_back(&I); 135e6c30fddSChandler Carruth ++NumSimplified; 136*4cbcbb07SChandler Carruth Changed = true; 137e6c30fddSChandler Carruth } 138e6c30fddSChandler Carruth } 139e6c30fddSChandler Carruth 140*4cbcbb07SChandler Carruth // Delete any dead instructions found thus far now that we've finished an 141*4cbcbb07SChandler Carruth // iteration over all instructions in all the loop blocks. 142*4cbcbb07SChandler Carruth if (!DeadInsts.empty()) { 143*4cbcbb07SChandler Carruth Changed = true; 144*4cbcbb07SChandler Carruth RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI); 145*4cbcbb07SChandler Carruth } 146*4cbcbb07SChandler Carruth 147*4cbcbb07SChandler Carruth // If we never found a PHI that needs to be simplified in the next 148*4cbcbb07SChandler Carruth // iteration, we're done. 149*4cbcbb07SChandler Carruth if (Next->empty()) 150e6c30fddSChandler Carruth break; 151e6c30fddSChandler Carruth 152*4cbcbb07SChandler Carruth // Otherwise, put the next set in place for the next iteration and reset it 153*4cbcbb07SChandler Carruth // and the visited PHIs for that iteration. 154*4cbcbb07SChandler Carruth std::swap(Next, ToSimplify); 155e6c30fddSChandler Carruth Next->clear(); 156*4cbcbb07SChandler Carruth VisitedPHIs.clear(); 157*4cbcbb07SChandler Carruth DeadInsts.clear(); 158*4cbcbb07SChandler Carruth } 159e6c30fddSChandler Carruth 160e6c30fddSChandler Carruth return Changed; 161e6c30fddSChandler Carruth } 162e6c30fddSChandler Carruth 163e6c30fddSChandler Carruth namespace { 164e6c30fddSChandler Carruth 165e6c30fddSChandler Carruth class LoopInstSimplifyLegacyPass : public LoopPass { 166e6c30fddSChandler Carruth public: 167e6c30fddSChandler Carruth static char ID; // Pass ID, replacement for typeid 168e6c30fddSChandler Carruth 169e6c30fddSChandler Carruth LoopInstSimplifyLegacyPass() : LoopPass(ID) { 170e6c30fddSChandler Carruth initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry()); 171e6c30fddSChandler Carruth } 172e6c30fddSChandler Carruth 173e6c30fddSChandler Carruth bool runOnLoop(Loop *L, LPPassManager &LPM) override { 174e6c30fddSChandler Carruth if (skipLoop(L)) 175e6c30fddSChandler Carruth return false; 176*4cbcbb07SChandler Carruth DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 177*4cbcbb07SChandler Carruth LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 178*4cbcbb07SChandler Carruth AssumptionCache &AC = 179*4cbcbb07SChandler Carruth getAnalysis<AssumptionCacheTracker>().getAssumptionCache( 180e6c30fddSChandler Carruth *L->getHeader()->getParent()); 181*4cbcbb07SChandler Carruth const TargetLibraryInfo &TLI = 182*4cbcbb07SChandler Carruth getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 183e6c30fddSChandler Carruth 184*4cbcbb07SChandler Carruth return simplifyLoopInst(*L, DT, LI, AC, TLI); 185e6c30fddSChandler Carruth } 186e6c30fddSChandler Carruth 187e6c30fddSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 188e6c30fddSChandler Carruth AU.addRequired<AssumptionCacheTracker>(); 189*4cbcbb07SChandler Carruth AU.addRequired<DominatorTreeWrapperPass>(); 190e6c30fddSChandler Carruth AU.addRequired<TargetLibraryInfoWrapperPass>(); 191e6c30fddSChandler Carruth AU.setPreservesCFG(); 192e6c30fddSChandler Carruth getLoopAnalysisUsage(AU); 193e6c30fddSChandler Carruth } 194e6c30fddSChandler Carruth }; 195e6c30fddSChandler Carruth 196e6c30fddSChandler Carruth } // end anonymous namespace 197e6c30fddSChandler Carruth 198e6c30fddSChandler Carruth PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM, 199e6c30fddSChandler Carruth LoopStandardAnalysisResults &AR, 200e6c30fddSChandler Carruth LPMUpdater &) { 201*4cbcbb07SChandler Carruth if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI)) 202e6c30fddSChandler Carruth return PreservedAnalyses::all(); 203e6c30fddSChandler Carruth 204e6c30fddSChandler Carruth auto PA = getLoopPassPreservedAnalyses(); 205e6c30fddSChandler Carruth PA.preserveSet<CFGAnalyses>(); 206e6c30fddSChandler Carruth return PA; 207e6c30fddSChandler Carruth } 208e6c30fddSChandler Carruth 209e6c30fddSChandler Carruth char LoopInstSimplifyLegacyPass::ID = 0; 210e6c30fddSChandler Carruth 211e6c30fddSChandler Carruth INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify", 212e6c30fddSChandler Carruth "Simplify instructions in loops", false, false) 213e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 214e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopPass) 215e6c30fddSChandler Carruth INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 216e6c30fddSChandler Carruth INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify", 217e6c30fddSChandler Carruth "Simplify instructions in loops", false, false) 218e6c30fddSChandler Carruth 219e6c30fddSChandler Carruth Pass *llvm::createLoopInstSimplifyPass() { 220e6c30fddSChandler Carruth return new LoopInstSimplifyLegacyPass(); 221e6c30fddSChandler Carruth } 222