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/Transforms/Utils/Local.h"
21 #include "llvm/ADT/Statistic.h"
24 STATISTIC(NumPhis, "Number of phis propagated");
25 STATISTIC(NumSelects, "Number of selects propagated");
26 STATISTIC(NumMemAccess, "Number of memory access targets propagated");
29 class CorrelatedValuePropagation : public FunctionPass {
32 bool processSelect(SelectInst *SI);
33 bool processPHI(PHINode *P);
34 bool processMemAccess(Instruction *I);
38 CorrelatedValuePropagation(): FunctionPass(ID) { }
40 bool runOnFunction(Function &F);
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<LazyValueInfo>();
48 char CorrelatedValuePropagation::ID = 0;
49 INITIALIZE_PASS(CorrelatedValuePropagation, "correlated-propagation",
50 "Value Propagation", false, false);
52 // Public interface to the Value Propagation pass
53 Pass *llvm::createCorrelatedValuePropagationPass() {
54 return new CorrelatedValuePropagation();
57 bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
58 if (S->getType()->isVectorTy()) return false;
59 if (isa<Constant>(S->getOperand(0))) return false;
61 Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
64 ConstantInt *CI = dyn_cast<ConstantInt>(C);
65 if (!CI) return false;
67 S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
75 bool CorrelatedValuePropagation::processPHI(PHINode *P) {
78 BasicBlock *BB = P->getParent();
79 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
80 Value *Incoming = P->getIncomingValue(i);
81 if (isa<Constant>(Incoming)) continue;
83 Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
84 P->getIncomingBlock(i),
88 P->setIncomingValue(i, C);
92 if (Value *ConstVal = P->hasConstantValue()) {
93 P->replaceAllUsesWith(ConstVal);
103 bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
105 if (LoadInst *L = dyn_cast<LoadInst>(I))
106 Pointer = L->getPointerOperand();
108 Pointer = cast<StoreInst>(I)->getPointerOperand();
110 if (isa<Constant>(Pointer)) return false;
112 Constant *C = LVI->getConstant(Pointer, I->getParent());
113 if (!C) return false;
116 I->replaceUsesOfWith(Pointer, C);
120 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
121 LVI = &getAnalysis<LazyValueInfo>();
123 bool FnChanged = false;
125 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
126 bool BBChanged = false;
127 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
128 Instruction *II = BI++;
129 switch (II->getOpcode()) {
130 case Instruction::Select:
131 BBChanged |= processSelect(cast<SelectInst>(II));
133 case Instruction::PHI:
134 BBChanged |= processPHI(cast<PHINode>(II));
136 case Instruction::Load:
137 case Instruction::Store:
138 BBChanged |= processMemAccess(II);
143 // Propagating correlated values might leave cruft around.
144 // Try to clean it up before we continue.
146 SimplifyInstructionsInBlock(FI);
148 FnChanged |= BBChanged;