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