Have a few places that want to simplify phi nodes use SimplifyInstruction
[oota-llvm.git] / lib / Transforms / Scalar / CorrelatedValuePropagation.cpp
1 //===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
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 file implements the Correlated Value Propagation pass.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "correlated-value-propagation"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Function.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/LazyValueInfo.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Transforms/Utils/Local.h"
23 #include "llvm/ADT/Statistic.h"
24 using namespace llvm;
25
26 STATISTIC(NumPhis,      "Number of phis propagated");
27 STATISTIC(NumSelects,   "Number of selects propagated");
28 STATISTIC(NumMemAccess, "Number of memory access targets propagated");
29 STATISTIC(NumCmps,      "Number of comparisons propagated");
30
31 namespace {
32   class CorrelatedValuePropagation : public FunctionPass {
33     LazyValueInfo *LVI;
34     
35     bool processSelect(SelectInst *SI);
36     bool processPHI(PHINode *P);
37     bool processMemAccess(Instruction *I);
38     bool processCmp(CmpInst *C);
39     
40   public:
41     static char ID;
42     CorrelatedValuePropagation(): FunctionPass(ID) {
43      initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
44     }
45     
46     bool runOnFunction(Function &F);
47     
48     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49       AU.addRequired<LazyValueInfo>();
50     }
51   };
52 }
53
54 char CorrelatedValuePropagation::ID = 0;
55 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
56                 "Value Propagation", false, false)
57 INITIALIZE_PASS_DEPENDENCY(LazyValueInfo)
58 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
59                 "Value Propagation", false, false)
60
61 // Public interface to the Value Propagation pass
62 Pass *llvm::createCorrelatedValuePropagationPass() {
63   return new CorrelatedValuePropagation();
64 }
65
66 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
67   if (S->getType()->isVectorTy()) return false;
68   if (isa<Constant>(S->getOperand(0))) return false;
69   
70   Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
71   if (!C) return false;
72   
73   ConstantInt *CI = dyn_cast<ConstantInt>(C);
74   if (!CI) return false;
75   
76   S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
77   S->eraseFromParent();
78
79   ++NumSelects;
80   
81   return true;
82 }
83
84 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
85   bool Changed = false;
86   
87   BasicBlock *BB = P->getParent();
88   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
89     Value *Incoming = P->getIncomingValue(i);
90     if (isa<Constant>(Incoming)) continue;
91     
92     Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
93                                          P->getIncomingBlock(i),
94                                          BB);
95     if (!C) continue;
96     
97     P->setIncomingValue(i, C);
98     Changed = true;
99   }
100
101   if (Value *V = SimplifyInstruction(P)) {
102     P->replaceAllUsesWith(V);
103     P->eraseFromParent();
104     Changed = true;
105   }
106
107   ++NumPhis;
108   
109   return Changed;
110 }
111
112 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
113   Value *Pointer = 0;
114   if (LoadInst *L = dyn_cast<LoadInst>(I))
115     Pointer = L->getPointerOperand();
116   else
117     Pointer = cast<StoreInst>(I)->getPointerOperand();
118   
119   if (isa<Constant>(Pointer)) return false;
120   
121   Constant *C = LVI->getConstant(Pointer, I->getParent());
122   if (!C) return false;
123   
124   ++NumMemAccess;
125   I->replaceUsesOfWith(Pointer, C);
126   return true;
127 }
128
129 /// processCmp - If the value of this comparison could be determined locally,
130 /// constant propagation would already have figured it out.  Instead, walk
131 /// the predecessors and statically evaluate the comparison based on information
132 /// available on that edge.  If a given static evaluation is true on ALL
133 /// incoming edges, then it's true universally and we can simplify the compare.
134 bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
135   Value *Op0 = C->getOperand(0);
136   if (isa<Instruction>(Op0) &&
137       cast<Instruction>(Op0)->getParent() == C->getParent())
138     return false;
139   
140   Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
141   if (!Op1) return false;
142   
143   pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
144   if (PI == PE) return false;
145   
146   LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(), 
147                                     C->getOperand(0), Op1, *PI, C->getParent());
148   if (Result == LazyValueInfo::Unknown) return false;
149
150   ++PI;
151   while (PI != PE) {
152     LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(), 
153                                     C->getOperand(0), Op1, *PI, C->getParent());
154     if (Res != Result) return false;
155     ++PI;
156   }
157   
158   ++NumCmps;
159   
160   if (Result == LazyValueInfo::True)
161     C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
162   else
163     C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
164   
165   C->eraseFromParent();
166
167   return true;
168 }
169
170 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
171   LVI = &getAnalysis<LazyValueInfo>();
172   
173   bool FnChanged = false;
174   
175   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
176     bool BBChanged = false;
177     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
178       Instruction *II = BI++;
179       switch (II->getOpcode()) {
180       case Instruction::Select:
181         BBChanged |= processSelect(cast<SelectInst>(II));
182         break;
183       case Instruction::PHI:
184         BBChanged |= processPHI(cast<PHINode>(II));
185         break;
186       case Instruction::ICmp:
187       case Instruction::FCmp:
188         BBChanged |= processCmp(cast<CmpInst>(II));
189         break;
190       case Instruction::Load:
191       case Instruction::Store:
192         BBChanged |= processMemAccess(II);
193         break;
194       }
195     }
196     
197     FnChanged |= BBChanged;
198   }
199   
200   return FnChanged;
201 }