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/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"
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) { }
44 bool runOnFunction(Function &F);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addRequired<LazyValueInfo>();
52 char CorrelatedValuePropagation::ID = 0;
53 INITIALIZE_PASS(CorrelatedValuePropagation, "correlated-propagation",
54 "Value Propagation", false, false);
56 // Public interface to the Value Propagation pass
57 Pass *llvm::createCorrelatedValuePropagationPass() {
58 return new CorrelatedValuePropagation();
61 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
62 if (S->getType()->isVectorTy()) return false;
63 if (isa<Constant>(S->getOperand(0))) return false;
65 Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
68 ConstantInt *CI = dyn_cast<ConstantInt>(C);
69 if (!CI) return false;
71 S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
79 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
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;
87 Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
88 P->getIncomingBlock(i),
92 P->setIncomingValue(i, C);
96 if (Value *ConstVal = P->hasConstantValue()) {
97 P->replaceAllUsesWith(ConstVal);
107 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
109 if (LoadInst *L = dyn_cast<LoadInst>(I))
110 Pointer = L->getPointerOperand();
112 Pointer = cast<StoreInst>(I)->getPointerOperand();
114 if (isa<Constant>(Pointer)) return false;
116 Constant *C = LVI->getConstant(Pointer, I->getParent());
117 if (!C) return false;
120 I->replaceUsesOfWith(Pointer, C);
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())
135 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
136 if (!Op1) return false;
138 pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
139 if (PI == PE) return false;
141 LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
142 C->getOperand(0), Op1, *PI, C->getParent());
143 if (Result == LazyValueInfo::Unknown) return false;
147 LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
148 C->getOperand(0), Op1, *PI, C->getParent());
149 if (Res != Result) return false;
155 if (Result == LazyValueInfo::True)
156 C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
158 C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
160 C->eraseFromParent();
165 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
166 LVI = &getAnalysis<LazyValueInfo>();
168 bool FnChanged = false;
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));
181 case Instruction::PHI:
182 BBChanged |= processPHI(cast<PHINode>(II));
184 case Instruction::ICmp:
185 case Instruction::FCmp:
186 BBChanged |= processCmp(cast<CmpInst>(II));
188 case Instruction::Load:
189 case Instruction::Store:
190 BBChanged |= processMemAccess(II);
195 // Propagating correlated values might leave cruft around.
196 // Try to clean it up before we continue.
198 SimplifyInstructionsInBlock(*FI);
200 FnChanged |= BBChanged;