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 | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Dominators.h" |
| 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 18 | #include "llvm/Analysis/LoopPass.h" |
| 19 | #include "llvm/Support/Debug.h" |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/Transforms/Scalar.h" |
| 22 | #include "llvm/Transforms/Utils/Local.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 27 | |
| 28 | namespace { |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 29 | class LoopInstSimplify : public LoopPass { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 30 | public: |
| 31 | static char ID; // Pass ID, replacement for typeid |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 32 | LoopInstSimplify() : LoopPass(ID) { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 33 | initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); |
| 34 | } |
| 35 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 36 | bool runOnLoop(Loop*, LPPassManager&); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 37 | |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 38 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 39 | AU.setPreservesCFG(); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 40 | AU.addRequired<LoopInfo>(); |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 41 | AU.addRequiredID(LoopSimplifyID); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 42 | AU.addPreservedID(LCSSAID); |
| 43 | } |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | char LoopInstSimplify::ID = 0; |
| 48 | INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify", |
| 49 | "Simplify instructions in loops", false, false) |
| 50 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 51 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 52 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 53 | INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify", |
| 54 | "Simplify instructions in loops", false, false) |
| 55 | |
| 56 | Pass* llvm::createLoopInstSimplifyPass() { |
| 57 | return new LoopInstSimplify(); |
| 58 | } |
| 59 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 60 | bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 61 | DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>(); |
| 62 | LoopInfo *LI = &getAnalysis<LoopInfo>(); |
| 63 | const TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 64 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 65 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 66 | L->getUniqueExitBlocks(ExitBlocks); |
| 67 | array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 68 | |
| 69 | SmallVector<BasicBlock*, 16> VisitStack; |
| 70 | SmallPtrSet<BasicBlock*, 32> Visited; |
| 71 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 72 | bool Changed = false; |
| 73 | bool LocalChanged; |
| 74 | do { |
| 75 | LocalChanged = false; |
| 76 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 77 | VisitStack.clear(); |
| 78 | Visited.clear(); |
| 79 | |
| 80 | VisitStack.push_back(L->getHeader()); |
| 81 | |
| 82 | while (!VisitStack.empty()) { |
| 83 | BasicBlock *BB = VisitStack.back(); |
| 84 | VisitStack.pop_back(); |
| 85 | |
| 86 | // Simplify instructions in the current basic block. |
| 87 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 88 | Instruction *I = BI++; |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 89 | // Don't bother simplifying unused instructions. |
| 90 | if (!I->use_empty()) { |
Cameron Zwarich | 64573ae | 2011-01-04 18:19:19 +0000 | [diff] [blame] | 91 | Value *V = SimplifyInstruction(I, TD, DT); |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 92 | if (V && LI->replacementPreservesLCSSAForm(I, V)) { |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 93 | I->replaceAllUsesWith(V); |
| 94 | LocalChanged = true; |
| 95 | ++NumSimplified; |
| 96 | } |
| 97 | } |
| 98 | LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I); |
| 99 | } |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 100 | |
Cameron Zwarich | e389ab1 | 2011-01-05 05:15:53 +0000 | [diff] [blame^] | 101 | // Add all successors to the worklist, except for loop exit blocks. |
| 102 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; |
| 103 | ++SI) { |
| 104 | BasicBlock *SuccBB = *SI; |
| 105 | bool IsExitBlock = std::binary_search(ExitBlocks.begin(), |
| 106 | ExitBlocks.end(), SuccBB); |
| 107 | if (!IsExitBlock && Visited.insert(SuccBB)) |
| 108 | VisitStack.push_back(SuccBB); |
| 109 | } |
| 110 | } |
| 111 | |
Cameron Zwarich | a1cb585 | 2011-01-04 00:12:46 +0000 | [diff] [blame] | 112 | Changed |= LocalChanged; |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 113 | } while (LocalChanged); |
| 114 | |
Cameron Zwarich | 832f611 | 2011-01-03 00:25:16 +0000 | [diff] [blame] | 115 | return Changed; |
| 116 | } |