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