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