1 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass performs lightweight instruction simplification on loop bodies. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/Analysis/AssumptionCache.h" 18 #include "llvm/Analysis/InstructionSimplify.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/LoopPass.h" 21 #include "llvm/Analysis/ScalarEvolution.h" 22 #include "llvm/Analysis/TargetLibraryInfo.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/Dominators.h" 25 #include "llvm/IR/Instructions.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Transforms/Scalar.h" 28 #include "llvm/Transforms/Scalar/LoopPassManager.h" 29 #include "llvm/Transforms/Utils/Local.h" 30 #include "llvm/Transforms/Utils/LoopUtils.h" 31 using namespace llvm; 32 33 #define DEBUG_TYPE "loop-instsimplify" 34 35 STATISTIC(NumSimplified, "Number of redundant instructions simplified"); 36 37 static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI, 38 AssumptionCache *AC, 39 const TargetLibraryInfo *TLI) { 40 SmallVector<BasicBlock *, 8> ExitBlocks; 41 L->getUniqueExitBlocks(ExitBlocks); 42 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); 43 44 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 45 46 // The bit we are stealing from the pointer represents whether this basic 47 // block is the header of a subloop, in which case we only process its phis. 48 typedef PointerIntPair<BasicBlock *, 1> WorklistItem; 49 SmallVector<WorklistItem, 16> VisitStack; 50 SmallPtrSet<BasicBlock *, 32> Visited; 51 52 bool Changed = false; 53 bool LocalChanged; 54 do { 55 LocalChanged = false; 56 57 VisitStack.clear(); 58 Visited.clear(); 59 60 VisitStack.push_back(WorklistItem(L->getHeader(), false)); 61 62 while (!VisitStack.empty()) { 63 WorklistItem Item = VisitStack.pop_back_val(); 64 BasicBlock *BB = Item.getPointer(); 65 bool IsSubloopHeader = Item.getInt(); 66 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); 67 68 // Simplify instructions in the current basic block. 69 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { 70 Instruction *I = &*BI++; 71 72 // The first time through the loop ToSimplify is empty and we try to 73 // simplify all instructions. On later iterations ToSimplify is not 74 // empty and we only bother simplifying instructions that are in it. 75 if (!ToSimplify->empty() && !ToSimplify->count(I)) 76 continue; 77 78 // Don't bother simplifying unused instructions. 79 if (!I->use_empty()) { 80 Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC}); 81 if (V && LI->replacementPreservesLCSSAForm(I, V)) { 82 // Mark all uses for resimplification next time round the loop. 83 for (User *U : I->users()) 84 Next->insert(cast<Instruction>(U)); 85 86 I->replaceAllUsesWith(V); 87 LocalChanged = true; 88 ++NumSimplified; 89 } 90 } 91 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) { 92 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one 93 // instruction, so simply incrementing the iterator does not work. 94 // When instructions get deleted re-iterate instead. 95 BI = BB->begin(); 96 BE = BB->end(); 97 LocalChanged = true; 98 } 99 100 if (IsSubloopHeader && !isa<PHINode>(I)) 101 break; 102 } 103 104 // Add all successors to the worklist, except for loop exit blocks and the 105 // bodies of subloops. We visit the headers of loops so that we can 106 // process 107 // their phis, but we contract the rest of the subloop body and only 108 // follow 109 // edges leading back to the original loop. 110 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; 111 ++SI) { 112 BasicBlock *SuccBB = *SI; 113 if (!Visited.insert(SuccBB).second) 114 continue; 115 116 const Loop *SuccLoop = LI->getLoopFor(SuccBB); 117 if (SuccLoop && SuccLoop->getHeader() == SuccBB && 118 L->contains(SuccLoop)) { 119 VisitStack.push_back(WorklistItem(SuccBB, true)); 120 121 SmallVector<BasicBlock *, 8> SubLoopExitBlocks; 122 SuccLoop->getExitBlocks(SubLoopExitBlocks); 123 124 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { 125 BasicBlock *ExitBB = SubLoopExitBlocks[i]; 126 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second) 127 VisitStack.push_back(WorklistItem(ExitBB, false)); 128 } 129 130 continue; 131 } 132 133 bool IsExitBlock = 134 std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB); 135 if (IsExitBlock) 136 continue; 137 138 VisitStack.push_back(WorklistItem(SuccBB, false)); 139 } 140 } 141 142 // Place the list of instructions to simplify on the next loop iteration 143 // into ToSimplify. 144 std::swap(ToSimplify, Next); 145 Next->clear(); 146 147 Changed |= LocalChanged; 148 } while (LocalChanged); 149 150 return Changed; 151 } 152 153 namespace { 154 class LoopInstSimplifyLegacyPass : public LoopPass { 155 public: 156 static char ID; // Pass ID, replacement for typeid 157 LoopInstSimplifyLegacyPass() : LoopPass(ID) { 158 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry()); 159 } 160 161 bool runOnLoop(Loop *L, LPPassManager &LPM) override { 162 if (skipLoop(L)) 163 return false; 164 DominatorTreeWrapperPass *DTWP = 165 getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 166 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; 167 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 168 AssumptionCache *AC = 169 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache( 170 *L->getHeader()->getParent()); 171 const TargetLibraryInfo *TLI = 172 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 173 174 return SimplifyLoopInst(L, DT, LI, AC, TLI); 175 } 176 177 void getAnalysisUsage(AnalysisUsage &AU) const override { 178 AU.addRequired<AssumptionCacheTracker>(); 179 AU.addRequired<TargetLibraryInfoWrapperPass>(); 180 AU.setPreservesCFG(); 181 getLoopAnalysisUsage(AU); 182 } 183 }; 184 } 185 186 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM, 187 LoopStandardAnalysisResults &AR, 188 LPMUpdater &) { 189 if (!SimplifyLoopInst(&L, &AR.DT, &AR.LI, &AR.AC, &AR.TLI)) 190 return PreservedAnalyses::all(); 191 192 auto PA = getLoopPassPreservedAnalyses(); 193 PA.preserveSet<CFGAnalyses>(); 194 return PA; 195 } 196 197 char LoopInstSimplifyLegacyPass::ID = 0; 198 INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify", 199 "Simplify instructions in loops", false, false) 200 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 201 INITIALIZE_PASS_DEPENDENCY(LoopPass) 202 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 203 INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify", 204 "Simplify instructions in loops", false, false) 205 206 Pass *llvm::createLoopInstSimplifyPass() { 207 return new LoopInstSimplifyLegacyPass(); 208 } 209