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