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