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" |
| 15 | #include "llvm/Analysis/LoopPass.h" |
| 16 | #include "llvm/Analysis/Dominators.h" |
| 17 | #include "llvm/Analysis/InstructionSimplify.h" |
| 18 | #include "llvm/Target/TargetData.h" |
| 19 | #include "llvm/Transforms/Scalar.h" |
| 20 | #include "llvm/Transforms/Utils/Local.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | STATISTIC(NumSimplified, "Number of redundant instructions simplified"); |
| 25 | |
| 26 | namespace { |
| 27 | class LoopInstSimplify : public LoopPass { |
| 28 | public: |
| 29 | static char ID; // Pass ID, replacement for typeid |
| 30 | LoopInstSimplify() : LoopPass(ID) { |
| 31 | initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); |
| 32 | } |
| 33 | |
| 34 | bool runOnLoop(Loop*, LPPassManager&); |
| 35 | |
| 36 | virtual void getAnalysisUsage(AnalysisUsage& AU) const { |
| 37 | AU.setPreservesCFG(); |
| 38 | AU.addRequired<DominatorTree>(); |
| 39 | AU.addPreserved<DominatorTree>(); |
| 40 | AU.addRequired<LoopInfo>(); |
| 41 | AU.addPreserved<LoopInfo>(); |
| 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 | |
| 60 | bool LoopInstSimplify::runOnLoop(Loop* L, LPPassManager& LPM) { |
| 61 | DominatorTree* DT = &getAnalysis<DominatorTree>(); |
| 62 | const LoopInfo* LI = &getAnalysis<LoopInfo>(); |
| 63 | const TargetData* TD = getAnalysisIfAvailable<TargetData>(); |
| 64 | |
| 65 | bool Changed = false; |
| 66 | bool LocalChanged; |
| 67 | do { |
| 68 | LocalChanged = false; |
| 69 | |
| 70 | SmallPtrSet<BasicBlock*, 32> Visited; |
| 71 | SmallVector<BasicBlock*, 32> VisitStack; |
| 72 | |
| 73 | VisitStack.push_back(L->getHeader()); |
| 74 | |
| 75 | while (!VisitStack.empty()) { |
| 76 | BasicBlock* BB = VisitStack.back(); |
| 77 | VisitStack.pop_back(); |
| 78 | |
| 79 | if (Visited.count(BB)) |
| 80 | continue; |
| 81 | Visited.insert(BB); |
| 82 | |
| 83 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 84 | Instruction* I = BI++; |
| 85 | // Don't bother simplifying unused instructions. |
| 86 | if (!I->use_empty()) { |
| 87 | if (Value* V = SimplifyInstruction(I, TD, DT)) { |
| 88 | I->replaceAllUsesWith(V); |
| 89 | LocalChanged = true; |
| 90 | ++NumSimplified; |
| 91 | } |
| 92 | } |
| 93 | LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I); |
| 94 | } |
| 95 | Changed |= LocalChanged; |
| 96 | |
| 97 | DomTreeNode* Node = DT->getNode(BB); |
| 98 | const std::vector<DomTreeNode*>& Children = Node->getChildren(); |
| 99 | for (unsigned i = 0; i < Children.size(); ++i) { |
| 100 | // Only visit children that are in the same loop. |
| 101 | BasicBlock* ChildBB = Children[i]->getBlock(); |
| 102 | if (!Visited.count(ChildBB) && LI->getLoopFor(ChildBB) == L) |
| 103 | VisitStack.push_back(ChildBB); |
| 104 | } |
| 105 | } |
| 106 | } while (LocalChanged); |
| 107 | |
| 108 | // Nothing that SimplifyInstruction() does should invalidate LCSSA form. |
| 109 | assert(L->isLCSSAForm(*DT)); |
| 110 | |
| 111 | return Changed; |
| 112 | } |