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 | |
| 14 | #define DEBUG_TYPE "loop-instsimplify" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
Jakub Staszak | f23980a | 2013-02-09 01:04:28 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.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 | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetLibraryInfo.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 30 | |
| 31 | namespace { |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 32 | class LoopInstSimplify : public LoopPass { |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 33 | public: |
| 34 | static char ID; // Pass ID, replacement for typeid |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 35 | LoopInstSimplify() : LoopPass(ID) { |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 36 | initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
| 38 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 39 | bool runOnLoop(Loop*, LPPassManager&); |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 40 | |
Cameron Zwarich | b2a41e9 | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 41 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 42 | AU.setPreservesCFG(); |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 43 | AU.addRequired<LoopInfo>(); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 44 | AU.addRequiredID(LoopSimplifyID); |
Cameron Zwarich | a42e591 | 2011-01-09 12:35:16 +0000 | [diff] [blame] | 45 | AU.addPreservedID(LoopSimplifyID); |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 46 | AU.addPreservedID(LCSSAID); |
Cameron Zwarich | 25cb63c | 2011-02-11 06:08:25 +0000 | [diff] [blame] | 47 | AU.addPreserved("scalar-evolution"); |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 48 | AU.addRequired<TargetLibraryInfo>(); |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 52 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 53 | char LoopInstSimplify::ID = 0; |
| 54 | INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify", |
| 55 | "Simplify instructions in loops", false, false) |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 56 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame^] | 57 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 58 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 59 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 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) { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame^] | 68 | DominatorTreeWrapperPass *DTWP = |
| 69 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 70 | DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0; |
Cameron Zwarich | b2a41e9 | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 71 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 72 | const DataLayout *TD = getAnalysisIfAvailable<DataLayout>(); |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 73 | const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 74 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 75 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 76 | L->getUniqueExitBlocks(ExitBlocks); |
| 77 | array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 78 | |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 79 | SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
| 80 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 81 | // The bit we are stealing from the pointer represents whether this basic |
| 82 | // block is the header of a subloop, in which case we only process its phis. |
| 83 | typedef PointerIntPair<BasicBlock*, 1> WorklistItem; |
| 84 | SmallVector<WorklistItem, 16> VisitStack; |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 85 | SmallPtrSet<BasicBlock*, 32> Visited; |
| 86 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 87 | bool Changed = false; |
| 88 | bool LocalChanged; |
| 89 | do { |
| 90 | LocalChanged = false; |
| 91 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 92 | VisitStack.clear(); |
| 93 | Visited.clear(); |
| 94 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 95 | VisitStack.push_back(WorklistItem(L->getHeader(), false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 96 | |
| 97 | while (!VisitStack.empty()) { |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 98 | WorklistItem Item = VisitStack.pop_back_val(); |
Cameron Zwarich | b4ab257 | 2011-01-08 17:07:11 +0000 | [diff] [blame] | 99 | BasicBlock *BB = Item.getPointer(); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 100 | bool IsSubloopHeader = Item.getInt(); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 101 | |
| 102 | // Simplify instructions in the current basic block. |
| 103 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Cameron Zwarich | b2a41e9 | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 104 | Instruction *I = BI++; |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 105 | |
| 106 | // The first time through the loop ToSimplify is empty and we try to |
| 107 | // simplify all instructions. On later iterations ToSimplify is not |
| 108 | // empty and we only bother simplifying instructions that are in it. |
| 109 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 110 | continue; |
| 111 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 112 | // Don't bother simplifying unused instructions. |
| 113 | if (!I->use_empty()) { |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 114 | Value *V = SimplifyInstruction(I, TD, TLI, DT); |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 115 | if (V && LI->replacementPreservesLCSSAForm(I, V)) { |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 116 | // Mark all uses for resimplification next time round the loop. |
| 117 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 118 | UI != UE; ++UI) |
| 119 | Next->insert(cast<Instruction>(*UI)); |
| 120 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 121 | I->replaceAllUsesWith(V); |
| 122 | LocalChanged = true; |
| 123 | ++NumSimplified; |
| 124 | } |
| 125 | } |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 126 | LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 127 | |
| 128 | if (IsSubloopHeader && !isa<PHINode>(I)) |
| 129 | break; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 130 | } |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 131 | |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 132 | // Add all successors to the worklist, except for loop exit blocks and the |
| 133 | // bodies of subloops. We visit the headers of loops so that we can process |
| 134 | // their phis, but we contract the rest of the subloop body and only follow |
| 135 | // edges leading back to the original loop. |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 136 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; |
| 137 | ++SI) { |
| 138 | BasicBlock *SuccBB = *SI; |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 139 | if (!Visited.insert(SuccBB)) |
| 140 | continue; |
| 141 | |
| 142 | const Loop *SuccLoop = LI->getLoopFor(SuccBB); |
| 143 | if (SuccLoop && SuccLoop->getHeader() == SuccBB |
| 144 | && L->contains(SuccLoop)) { |
| 145 | VisitStack.push_back(WorklistItem(SuccBB, true)); |
| 146 | |
| 147 | SmallVector<BasicBlock*, 8> SubLoopExitBlocks; |
| 148 | SuccLoop->getExitBlocks(SubLoopExitBlocks); |
| 149 | |
| 150 | for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { |
| 151 | BasicBlock *ExitBB = SubLoopExitBlocks[i]; |
| 152 | if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB)) |
| 153 | VisitStack.push_back(WorklistItem(ExitBB, false)); |
| 154 | } |
| 155 | |
| 156 | continue; |
| 157 | } |
| 158 | |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 159 | bool IsExitBlock = std::binary_search(ExitBlocks.begin(), |
Cameron Zwarich | 80bd9af | 2011-01-08 15:52:22 +0000 | [diff] [blame] | 160 | ExitBlocks.end(), SuccBB); |
| 161 | if (IsExitBlock) |
| 162 | continue; |
| 163 | |
| 164 | VisitStack.push_back(WorklistItem(SuccBB, false)); |
Cameron Zwarich | 4c51d12 | 2011-01-05 05:15:53 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Cameron Zwarich | 5a2bb99 | 2011-01-05 05:47:47 +0000 | [diff] [blame] | 168 | // Place the list of instructions to simplify on the next loop iteration |
| 169 | // into ToSimplify. |
| 170 | std::swap(ToSimplify, Next); |
| 171 | Next->clear(); |
| 172 | |
Cameron Zwarich | e924969 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 173 | Changed |= LocalChanged; |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 174 | } while (LocalChanged); |
| 175 | |
Cameron Zwarich | cab9a0a | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 176 | return Changed; |
| 177 | } |