Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 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 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar/LoopInstSimplify.h" |
Jakub Staszak | f23980a | 2013-02-09 01:04:28 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AssumptionCache.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopPass.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopPassManager.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ScalarEvolution.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Scalar.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "loop-instsimplify" |
| 34 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 35 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 36 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 37 | static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI, |
| 38 | AssumptionCache *AC, |
| 39 | const TargetLibraryInfo *TLI) { |
| 40 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 41 | L->getUniqueExitBlocks(ExitBlocks); |
| 42 | array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 43 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 44 | SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 45 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 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. |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 48 | typedef PointerIntPair<BasicBlock *, 1> WorklistItem; |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 49 | SmallVector<WorklistItem, 16> VisitStack; |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 50 | SmallPtrSet<BasicBlock *, 32> Visited; |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 51 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 52 | bool Changed = false; |
| 53 | bool LocalChanged; |
| 54 | do { |
| 55 | LocalChanged = false; |
| 56 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 57 | VisitStack.clear(); |
| 58 | Visited.clear(); |
| 59 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 60 | VisitStack.push_back(WorklistItem(L->getHeader(), false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 61 | |
| 62 | while (!VisitStack.empty()) { |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 63 | WorklistItem Item = VisitStack.pop_back_val(); |
Cameron Zwarich | b4ab257 | 2011-01-08 17:07:11 +0000 | [diff] [blame] | 64 | BasicBlock *BB = Item.getPointer(); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 65 | bool IsSubloopHeader = Item.getInt(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 66 | const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 67 | |
| 68 | // Simplify instructions in the current basic block. |
| 69 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 70 | Instruction *I = &*BI++; |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 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 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 78 | // Don't bother simplifying unused instructions. |
| 79 | if (!I->use_empty()) { |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 80 | Value *V = SimplifyInstruction(I, DL, TLI, DT, AC); |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 81 | if (V && LI->replacementPreservesLCSSAForm(I, V)) { |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 82 | // Mark all uses for resimplification next time round the loop. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 83 | for (User *U : I->users()) |
| 84 | Next->insert(cast<Instruction>(U)); |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 85 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 86 | I->replaceAllUsesWith(V); |
| 87 | LocalChanged = true; |
| 88 | ++NumSimplified; |
| 89 | } |
| 90 | } |
Chad Rosier | 074ce83 | 2016-04-06 13:27:13 +0000 | [diff] [blame] | 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. |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 95 | BI = BB->begin(); |
| 96 | BE = BB->end(); |
Chad Rosier | 074ce83 | 2016-04-06 13:27:13 +0000 | [diff] [blame] | 97 | LocalChanged = true; |
Gerolf Hoflehner | af7a87d | 2014-04-26 05:58:11 +0000 | [diff] [blame] | 98 | } |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 99 | |
| 100 | if (IsSubloopHeader && !isa<PHINode>(I)) |
| 101 | break; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 102 | } |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 103 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 104 | // Add all successors to the worklist, except for loop exit blocks and the |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 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 |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 109 | // edges leading back to the original loop. |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 110 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; |
| 111 | ++SI) { |
| 112 | BasicBlock *SuccBB = *SI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 113 | if (!Visited.insert(SuccBB).second) |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 114 | continue; |
| 115 | |
| 116 | const Loop *SuccLoop = LI->getLoopFor(SuccBB); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 117 | if (SuccLoop && SuccLoop->getHeader() == SuccBB && |
| 118 | L->contains(SuccLoop)) { |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 119 | VisitStack.push_back(WorklistItem(SuccBB, true)); |
| 120 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 121 | SmallVector<BasicBlock *, 8> SubLoopExitBlocks; |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 122 | SuccLoop->getExitBlocks(SubLoopExitBlocks); |
| 123 | |
| 124 | for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { |
| 125 | BasicBlock *ExitBB = SubLoopExitBlocks[i]; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 126 | if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second) |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 127 | VisitStack.push_back(WorklistItem(ExitBB, false)); |
| 128 | } |
| 129 | |
| 130 | continue; |
| 131 | } |
| 132 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 133 | bool IsExitBlock = |
| 134 | std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 135 | if (IsExitBlock) |
| 136 | continue; |
| 137 | |
| 138 | VisitStack.push_back(WorklistItem(SuccBB, false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 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 | |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 147 | Changed |= LocalChanged; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 148 | } while (LocalChanged); |
| 149 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 150 | return Changed; |
| 151 | } |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 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, |
| 187 | AnalysisManager<Loop> &AM) { |
| 188 | const auto &FAM = |
| 189 | AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager(); |
| 190 | Function *F = L.getHeader()->getParent(); |
| 191 | |
| 192 | // Use getCachedResult because Loop pass cannot trigger a function analysis. |
| 193 | auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(*F); |
| 194 | auto *LI = FAM.getCachedResult<LoopAnalysis>(*F); |
| 195 | auto *AC = FAM.getCachedResult<AssumptionAnalysis>(*F); |
| 196 | const auto *TLI = FAM.getCachedResult<TargetLibraryAnalysis>(*F); |
| 197 | assert((LI && AC && TLI) && "Analyses for Loop Inst Simplify not available"); |
| 198 | |
| 199 | if (!SimplifyLoopInst(&L, DT, LI, AC, TLI)) |
| 200 | return PreservedAnalyses::all(); |
| 201 | |
| 202 | return getLoopPassPreservedAnalyses(); |
| 203 | } |
| 204 | |
| 205 | char LoopInstSimplifyLegacyPass::ID = 0; |
| 206 | INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify", |
| 207 | "Simplify instructions in loops", false, false) |
| 208 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 209 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
| 210 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 211 | INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify", |
| 212 | "Simplify instructions in loops", false, false) |
| 213 | |
| 214 | Pass *llvm::createLoopInstSimplifyPass() { |
| 215 | return new LoopInstSimplifyLegacyPass(); |
| 216 | } |