Now with fewer extraneous semicolons!
[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/LazyValueInfo.h"
20 #include "llvm/Support/CFG.h"
21 #include "llvm/Transforms/Utils/Local.h"
22 #include "llvm/ADT/DepthFirstIterator.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     
44     bool runOnFunction(Function &F);
45     
46     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47       AU.addRequired<LazyValueInfo>();
48     }
49   };
50 }
51
52 char CorrelatedValuePropagation::ID = 0;
53 INITIALIZE_PASS(CorrelatedValuePropagation, "correlated-propagation",
54                 "Value Propagation", false, false)
55
56 // Public interface to the Value Propagation pass
57 Pass *llvm::createCorrelatedValuePropagationPass() {
58   return new CorrelatedValuePropagation();
59 }
60
61 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
62   if (S->getType()->isVectorTy()) return false;
63   if (isa<Constant>(S->getOperand(0))) return false;
64   
65   Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
66   if (!C) return false;
67   
68   ConstantInt *CI = dyn_cast<ConstantInt>(C);
69   if (!CI) return false;
70   
71   S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
72   S->eraseFromParent();
73
74   ++NumSelects;
75   
76   return true;
77 }
78
79 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
80   bool Changed = false;
81   
82   BasicBlock *BB = P->getParent();
83   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
84     Value *Incoming = P->getIncomingValue(i);
85     if (isa<Constant>(Incoming)) continue;
86     
87     Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
88                                          P->getIncomingBlock(i),
89                                          BB);
90     if (!C) continue;
91     
92     P->setIncomingValue(i, C);
93     Changed = true;
94   }
95   
96   if (Value *ConstVal = P->hasConstantValue()) {
97     P->replaceAllUsesWith(ConstVal);
98     P->eraseFromParent();
99     Changed = true;
100   }
101   
102   ++NumPhis;
103   
104   return Changed;
105 }
106
107 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
108   Value *Pointer = 0;
109   if (LoadInst *L = dyn_cast<LoadInst>(I))
110     Pointer = L->getPointerOperand();
111   else
112     Pointer = cast<StoreInst>(I)->getPointerOperand();
113   
114   if (isa<Constant>(Pointer)) return false;
115   
116   Constant *C = LVI->getConstant(Pointer, I->getParent());
117   if (!C) return false;
118   
119   ++NumMemAccess;
120   I->replaceUsesOfWith(Pointer, C);
121   return true;
122 }
123
124 /// processCmp - If the value of this comparison could be determined locally,
125 /// constant propagation would already have figured it out.  Instead, walk
126 /// the predecessors and statically evaluate the comparison based on information
127 /// available on that edge.  If a given static evaluation is true on ALL
128 /// incoming edges, then it's true universally and we can simplify the compare.
129 bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
130   Value *Op0 = C->getOperand(0);
131   if (isa<Instruction>(Op0) &&
132       cast<Instruction>(Op0)->getParent() == C->getParent())
133     return false;
134   
135   Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
136   if (!Op1) return false;
137   
138   pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
139   if (PI == PE) return false;
140   
141   LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(), 
142                                     C->getOperand(0), Op1, *PI, C->getParent());
143   if (Result == LazyValueInfo::Unknown) return false;
144
145   ++PI;
146   while (PI != PE) {
147     LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(), 
148                                     C->getOperand(0), Op1, *PI, C->getParent());
149     if (Res != Result) return false;
150     ++PI;
151   }
152   
153   ++NumCmps;
154   
155   if (Result == LazyValueInfo::True)
156     C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
157   else
158     C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
159   
160   C->eraseFromParent();
161
162   return true;
163 }
164
165 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
166   LVI = &getAnalysis<LazyValueInfo>();
167   
168   bool FnChanged = false;
169   
170   // Perform a depth-first walk of the CFG so that we don't waste time
171   // optimizing unreachable blocks.
172   for (df_iterator<BasicBlock*> FI = df_begin(&F.getEntryBlock()),
173        FE = df_end(&F.getEntryBlock()); FI != FE; ++FI) {
174     bool BBChanged = false;
175     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
176       Instruction *II = BI++;
177       switch (II->getOpcode()) {
178       case Instruction::Select:
179         BBChanged |= processSelect(cast<SelectInst>(II));
180         break;
181       case Instruction::PHI:
182         BBChanged |= processPHI(cast<PHINode>(II));
183         break;
184       case Instruction::ICmp:
185       case Instruction::FCmp:
186         BBChanged |= processCmp(cast<CmpInst>(II));
187         break;
188       case Instruction::Load:
189       case Instruction::Store:
190         BBChanged |= processMemAccess(II);
191         break;
192       }
193     }
194     
195     // Propagating correlated values might leave cruft around.
196     // Try to clean it up before we continue.
197     if (BBChanged)
198       SimplifyInstructionsInBlock(*FI);
199     
200     FnChanged |= BBChanged;
201   }
202   
203   return FnChanged;
204 }