1 //===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
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 file implements the Correlated Value Propagation pass.
12 //===----------------------------------------------------------------------===//
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"
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");
32 class CorrelatedValuePropagation : public FunctionPass {
35 bool processSelect(SelectInst *SI);
36 bool processPHI(PHINode *P);
37 bool processMemAccess(Instruction *I);
38 bool processCmp(CmpInst *C);
42 CorrelatedValuePropagation(): FunctionPass(ID) {
43 initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
46 bool runOnFunction(Function &F);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.addRequired<LazyValueInfo>();
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)
61 // Public interface to the Value Propagation pass
62 Pass *llvm::createCorrelatedValuePropagationPass() {
63 return new CorrelatedValuePropagation();
66 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
67 if (S->getType()->isVectorTy()) return false;
68 if (isa<Constant>(S->getOperand(0))) return false;
70 Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
73 ConstantInt *CI = dyn_cast<ConstantInt>(C);
74 if (!CI) return false;
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());
81 S->replaceAllUsesWith(ReplaceWith);
89 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
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;
97 Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
98 P->getIncomingBlock(i),
102 P->setIncomingValue(i, C);
106 if (Value *V = SimplifyInstruction(P)) {
107 P->replaceAllUsesWith(V);
108 P->eraseFromParent();
117 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
119 if (LoadInst *L = dyn_cast<LoadInst>(I))
120 Pointer = L->getPointerOperand();
122 Pointer = cast<StoreInst>(I)->getPointerOperand();
124 if (isa<Constant>(Pointer)) return false;
126 Constant *C = LVI->getConstant(Pointer, I->getParent());
127 if (!C) return false;
130 I->replaceUsesOfWith(Pointer, C);
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())
145 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
146 if (!Op1) return false;
148 pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
149 if (PI == PE) return false;
151 LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
152 C->getOperand(0), Op1, *PI, C->getParent());
153 if (Result == LazyValueInfo::Unknown) return false;
157 LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
158 C->getOperand(0), Op1, *PI, C->getParent());
159 if (Res != Result) return false;
165 if (Result == LazyValueInfo::True)
166 C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
168 C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
170 C->eraseFromParent();
175 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
176 LVI = &getAnalysis<LazyValueInfo>();
178 bool FnChanged = false;
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));
188 case Instruction::PHI:
189 BBChanged |= processPHI(cast<PHINode>(II));
191 case Instruction::ICmp:
192 case Instruction::FCmp:
193 BBChanged |= processCmp(cast<CmpInst>(II));
195 case Instruction::Load:
196 case Instruction::Store:
197 BBChanged |= processMemAccess(II);
202 FnChanged |= BBChanged;