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