Fix PR8790, another instance where unreachable code can cause instruction simplificat...
[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   Value *ReplaceWith = S->getOperand(1);
77   Value *Other = S->getOperand(2);
78   if (!CI->isOne()) std::swap(ReplaceWith, Other);
79   if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType());
80
81   S->replaceAllUsesWith(ReplaceWith);
82   S->eraseFromParent();
83
84   ++NumSelects;
85
86   return true;
87 }
88
89 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
90   bool Changed = false;
91
92   BasicBlock *BB = P->getParent();
93   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
94     Value *Incoming = P->getIncomingValue(i);
95     if (isa<Constant>(Incoming)) continue;
96
97     Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
98                                          P->getIncomingBlock(i),
99                                          BB);
100     if (!C) continue;
101
102     P->setIncomingValue(i, C);
103     Changed = true;
104   }
105
106   if (Value *V = SimplifyInstruction(P)) {
107     P->replaceAllUsesWith(V);
108     P->eraseFromParent();
109     Changed = true;
110   }
111
112   ++NumPhis;
113
114   return Changed;
115 }
116
117 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
118   Value *Pointer = 0;
119   if (LoadInst *L = dyn_cast<LoadInst>(I))
120     Pointer = L->getPointerOperand();
121   else
122     Pointer = cast<StoreInst>(I)->getPointerOperand();
123
124   if (isa<Constant>(Pointer)) return false;
125
126   Constant *C = LVI->getConstant(Pointer, I->getParent());
127   if (!C) return false;
128
129   ++NumMemAccess;
130   I->replaceUsesOfWith(Pointer, C);
131   return true;
132 }
133
134 /// processCmp - If the value of this comparison could be determined locally,
135 /// constant propagation would already have figured it out.  Instead, walk
136 /// the predecessors and statically evaluate the comparison based on information
137 /// available on that edge.  If a given static evaluation is true on ALL
138 /// incoming edges, then it's true universally and we can simplify the compare.
139 bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
140   Value *Op0 = C->getOperand(0);
141   if (isa<Instruction>(Op0) &&
142       cast<Instruction>(Op0)->getParent() == C->getParent())
143     return false;
144
145   Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
146   if (!Op1) return false;
147
148   pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
149   if (PI == PE) return false;
150
151   LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
152                                     C->getOperand(0), Op1, *PI, C->getParent());
153   if (Result == LazyValueInfo::Unknown) return false;
154
155   ++PI;
156   while (PI != PE) {
157     LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
158                                     C->getOperand(0), Op1, *PI, C->getParent());
159     if (Res != Result) return false;
160     ++PI;
161   }
162
163   ++NumCmps;
164
165   if (Result == LazyValueInfo::True)
166     C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
167   else
168     C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
169
170   C->eraseFromParent();
171
172   return true;
173 }
174
175 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
176   LVI = &getAnalysis<LazyValueInfo>();
177
178   bool FnChanged = false;
179
180   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
181     bool BBChanged = false;
182     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
183       Instruction *II = BI++;
184       switch (II->getOpcode()) {
185       case Instruction::Select:
186         BBChanged |= processSelect(cast<SelectInst>(II));
187         break;
188       case Instruction::PHI:
189         BBChanged |= processPHI(cast<PHINode>(II));
190         break;
191       case Instruction::ICmp:
192       case Instruction::FCmp:
193         BBChanged |= processCmp(cast<CmpInst>(II));
194         break;
195       case Instruction::Load:
196       case Instruction::Store:
197         BBChanged |= processMemAccess(II);
198         break;
199       }
200     }
201
202     FnChanged |= BBChanged;
203   }
204
205   return FnChanged;
206 }