1 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs lightweight instruction simplification on loop bodies.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "loop-instsimplify"
15 #include "llvm/Analysis/Dominators.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Support/Debug.h"
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"
26 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
29 class LoopInstSimplify : public LoopPass {
31 static char ID; // Pass ID, replacement for typeid
32 LoopInstSimplify() : LoopPass(ID) {
33 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
36 bool runOnLoop(Loop*, LPPassManager&);
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<LoopInfo>();
41 AU.addRequiredID(LoopSimplifyID);
42 AU.addPreservedID(LCSSAID);
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)
56 Pass* llvm::createLoopInstSimplifyPass() {
57 return new LoopInstSimplify();
60 bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
61 DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
62 LoopInfo *LI = &getAnalysis<LoopInfo>();
63 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
65 SmallVector<BasicBlock*, 8> ExitBlocks;
66 L->getUniqueExitBlocks(ExitBlocks);
67 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
69 SmallVector<BasicBlock*, 16> VisitStack;
70 SmallPtrSet<BasicBlock*, 32> Visited;
80 VisitStack.push_back(L->getHeader());
82 while (!VisitStack.empty()) {
83 BasicBlock *BB = VisitStack.back();
84 VisitStack.pop_back();
86 // Simplify instructions in the current basic block.
87 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
88 Instruction *I = BI++;
89 // Don't bother simplifying unused instructions.
90 if (!I->use_empty()) {
91 Value *V = SimplifyInstruction(I, TD, DT);
92 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
93 I->replaceAllUsesWith(V);
98 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
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;
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);
112 Changed |= LocalChanged;
113 } while (LocalChanged);