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" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopInfo.h" |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopPass.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopPassManager.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ScalarEvolution.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Scalar.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/LoopUtils.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "loop-instsimplify" |
| 33 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 34 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 35 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 36 | static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI, |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 37 | const TargetLibraryInfo *TLI) { |
| 38 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 39 | L->getUniqueExitBlocks(ExitBlocks); |
| 40 | array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 41 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 42 | SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 43 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 44 | // The bit we are stealing from the pointer represents whether this basic |
| 45 | // 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] | 46 | typedef PointerIntPair<BasicBlock *, 1> WorklistItem; |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 47 | SmallVector<WorklistItem, 16> VisitStack; |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 48 | SmallPtrSet<BasicBlock *, 32> Visited; |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 49 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 50 | bool Changed = false; |
| 51 | bool LocalChanged; |
| 52 | do { |
| 53 | LocalChanged = false; |
| 54 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 55 | VisitStack.clear(); |
| 56 | Visited.clear(); |
| 57 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 58 | VisitStack.push_back(WorklistItem(L->getHeader(), false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 59 | |
| 60 | while (!VisitStack.empty()) { |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 61 | WorklistItem Item = VisitStack.pop_back_val(); |
Cameron Zwarich | b4ab257 | 2011-01-08 17:07:11 +0000 | [diff] [blame] | 62 | BasicBlock *BB = Item.getPointer(); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 63 | bool IsSubloopHeader = Item.getInt(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 64 | const DataLayout &DL = L->getHeader()->getModule()->getDataLayout(); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 65 | |
| 66 | // Simplify instructions in the current basic block. |
| 67 | 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] | 68 | Instruction *I = &*BI++; |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 69 | |
| 70 | // The first time through the loop ToSimplify is empty and we try to |
| 71 | // simplify all instructions. On later iterations ToSimplify is not |
| 72 | // empty and we only bother simplifying instructions that are in it. |
| 73 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 74 | continue; |
| 75 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 76 | // Don't bother simplifying unused instructions. |
| 77 | if (!I->use_empty()) { |
Hal Finkel | 3ca4a6b | 2016-12-15 03:02:15 +0000 | [diff] [blame] | 78 | Value *V = SimplifyInstruction(I, DL, TLI, DT); |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 79 | if (V && LI->replacementPreservesLCSSAForm(I, V)) { |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 80 | // Mark all uses for resimplification next time round the loop. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 81 | for (User *U : I->users()) |
| 82 | Next->insert(cast<Instruction>(U)); |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 83 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 84 | I->replaceAllUsesWith(V); |
| 85 | LocalChanged = true; |
| 86 | ++NumSimplified; |
| 87 | } |
| 88 | } |
Chad Rosier | 074ce83 | 2016-04-06 13:27:13 +0000 | [diff] [blame] | 89 | if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) { |
| 90 | // RecursivelyDeleteTriviallyDeadInstruction can remove more than one |
| 91 | // instruction, so simply incrementing the iterator does not work. |
| 92 | // When instructions get deleted re-iterate instead. |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 93 | BI = BB->begin(); |
| 94 | BE = BB->end(); |
Chad Rosier | 074ce83 | 2016-04-06 13:27:13 +0000 | [diff] [blame] | 95 | LocalChanged = true; |
Gerolf Hoflehner | af7a87d | 2014-04-26 05:58:11 +0000 | [diff] [blame] | 96 | } |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 97 | |
| 98 | if (IsSubloopHeader && !isa<PHINode>(I)) |
| 99 | break; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 100 | } |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 101 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 102 | // 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] | 103 | // bodies of subloops. We visit the headers of loops so that we can |
| 104 | // process |
| 105 | // their phis, but we contract the rest of the subloop body and only |
| 106 | // follow |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 107 | // edges leading back to the original loop. |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 108 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; |
| 109 | ++SI) { |
| 110 | BasicBlock *SuccBB = *SI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 111 | if (!Visited.insert(SuccBB).second) |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 112 | continue; |
| 113 | |
| 114 | const Loop *SuccLoop = LI->getLoopFor(SuccBB); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 115 | if (SuccLoop && SuccLoop->getHeader() == SuccBB && |
| 116 | L->contains(SuccLoop)) { |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 117 | VisitStack.push_back(WorklistItem(SuccBB, true)); |
| 118 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 119 | SmallVector<BasicBlock *, 8> SubLoopExitBlocks; |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 120 | SuccLoop->getExitBlocks(SubLoopExitBlocks); |
| 121 | |
| 122 | for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { |
| 123 | BasicBlock *ExitBB = SubLoopExitBlocks[i]; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 124 | if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second) |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 125 | VisitStack.push_back(WorklistItem(ExitBB, false)); |
| 126 | } |
| 127 | |
| 128 | continue; |
| 129 | } |
| 130 | |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 131 | bool IsExitBlock = |
| 132 | std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 133 | if (IsExitBlock) |
| 134 | continue; |
| 135 | |
| 136 | VisitStack.push_back(WorklistItem(SuccBB, false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 140 | // Place the list of instructions to simplify on the next loop iteration |
| 141 | // into ToSimplify. |
| 142 | std::swap(ToSimplify, Next); |
| 143 | Next->clear(); |
| 144 | |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 145 | Changed |= LocalChanged; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 146 | } while (LocalChanged); |
| 147 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 148 | return Changed; |
| 149 | } |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 150 | |
| 151 | namespace { |
| 152 | class LoopInstSimplifyLegacyPass : public LoopPass { |
| 153 | public: |
| 154 | static char ID; // Pass ID, replacement for typeid |
| 155 | LoopInstSimplifyLegacyPass() : LoopPass(ID) { |
| 156 | initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 157 | } |
| 158 | |
| 159 | bool runOnLoop(Loop *L, LPPassManager &LPM) override { |
| 160 | if (skipLoop(L)) |
| 161 | return false; |
| 162 | DominatorTreeWrapperPass *DTWP = |
| 163 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 164 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 165 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 166 | const TargetLibraryInfo *TLI = |
| 167 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 168 | |
Hal Finkel | 3ca4a6b | 2016-12-15 03:02:15 +0000 | [diff] [blame] | 169 | return SimplifyLoopInst(L, DT, LI, TLI); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 173 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 174 | AU.setPreservesCFG(); |
| 175 | getLoopAnalysisUsage(AU); |
| 176 | } |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, |
Sean Silva | 0746f3b | 2016-08-09 00:28:52 +0000 | [diff] [blame] | 181 | LoopAnalysisManager &AM) { |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 182 | const auto &FAM = |
| 183 | AM.getResult<FunctionAnalysisManagerLoopProxy>(L).getManager(); |
| 184 | Function *F = L.getHeader()->getParent(); |
| 185 | |
| 186 | // Use getCachedResult because Loop pass cannot trigger a function analysis. |
| 187 | auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(*F); |
| 188 | auto *LI = FAM.getCachedResult<LoopAnalysis>(*F); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 189 | const auto *TLI = FAM.getCachedResult<TargetLibraryAnalysis>(*F); |
Hal Finkel | 3ca4a6b | 2016-12-15 03:02:15 +0000 | [diff] [blame] | 190 | assert((LI && TLI) && "Analyses for Loop Inst Simplify not available"); |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 191 | |
Hal Finkel | 3ca4a6b | 2016-12-15 03:02:15 +0000 | [diff] [blame] | 192 | if (!SimplifyLoopInst(&L, DT, LI, TLI)) |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 193 | return PreservedAnalyses::all(); |
| 194 | |
| 195 | return getLoopPassPreservedAnalyses(); |
| 196 | } |
| 197 | |
| 198 | char LoopInstSimplifyLegacyPass::ID = 0; |
| 199 | INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify", |
| 200 | "Simplify instructions in loops", false, false) |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 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 | } |