Address most of Duncan's review comments. Also, make LoopInstSimplify a simple
[oota-llvm.git] / lib / Transforms / Scalar / LoopInstSimplify.cpp
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/Function.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/LoopInfo.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"
24 using namespace llvm;
25
26 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
27
28 namespace {
29   class LoopInstSimplify : public FunctionPass {
30   public:
31     static char ID; // Pass ID, replacement for typeid
32     LoopInstSimplify() : FunctionPass(ID) {
33       initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
34     }
35
36     bool runOnFunction(Function&);
37
38     virtual void getAnalysisUsage(AnalysisUsage& AU) const {
39       AU.setPreservesCFG();
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::runOnFunction(Function& F) {
61   DominatorTree* DT = getAnalysisIfAvailable<DominatorTree>();
62   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     for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
71          DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
72       for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
73         Instruction* I = BI++;
74         // Don't bother simplifying unused instructions.
75         if (!I->use_empty()) {
76           Value* V = SimplifyInstruction(I, TD, DT);
77           if (V && LI->replacementPreservesLCSSAForm(I, V)) {
78             I->replaceAllUsesWith(V);
79             LocalChanged = true;
80             ++NumSimplified;
81           }
82         }
83         LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
84       }
85
86     Changed |= LocalChanged;
87   } while (LocalChanged);
88
89   return Changed;
90 }