2010-08-31 07:48:34 +00:00
|
|
|
//===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
|
2010-08-27 23:31:36 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-08-31 07:48:34 +00:00
|
|
|
// This file implements the Correlated Value Propagation pass.
|
2010-08-27 23:31:36 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-31 07:48:34 +00:00
|
|
|
#define DEBUG_TYPE "correlated-value-propagation"
|
2010-08-27 23:31:36 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Analysis/LazyValueInfo.h"
|
2010-09-03 22:47:08 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2010-08-27 23:31:36 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2010-09-16 18:35:07 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2010-08-30 22:45:55 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-08-27 23:31:36 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-03 19:08:37 +00:00
|
|
|
STATISTIC(NumPhis, "Number of phis propagated");
|
|
|
|
STATISTIC(NumSelects, "Number of selects propagated");
|
|
|
|
STATISTIC(NumMemAccess, "Number of memory access targets propagated");
|
2010-09-03 22:47:08 +00:00
|
|
|
STATISTIC(NumCmps, "Number of comparisons propagated");
|
2010-08-30 22:45:55 +00:00
|
|
|
|
2010-08-27 23:31:36 +00:00
|
|
|
namespace {
|
2010-08-31 07:48:34 +00:00
|
|
|
class CorrelatedValuePropagation : public FunctionPass {
|
2010-08-27 23:31:36 +00:00
|
|
|
LazyValueInfo *LVI;
|
|
|
|
|
|
|
|
bool processSelect(SelectInst *SI);
|
|
|
|
bool processPHI(PHINode *P);
|
2010-09-03 19:08:37 +00:00
|
|
|
bool processMemAccess(Instruction *I);
|
2010-09-03 22:47:08 +00:00
|
|
|
bool processCmp(CmpInst *C);
|
2010-08-27 23:31:36 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-10-19 17:21:58 +00:00
|
|
|
CorrelatedValuePropagation(): FunctionPass(ID) {
|
|
|
|
initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-08-27 23:31:36 +00:00
|
|
|
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LazyValueInfo>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-08-31 07:48:34 +00:00
|
|
|
char CorrelatedValuePropagation::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
|
|
|
|
"Value Propagation", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LazyValueInfo)
|
|
|
|
INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Value Propagation", false, false)
|
2010-08-27 23:31:36 +00:00
|
|
|
|
|
|
|
// Public interface to the Value Propagation pass
|
2010-08-31 07:48:34 +00:00
|
|
|
Pass *llvm::createCorrelatedValuePropagationPass() {
|
|
|
|
return new CorrelatedValuePropagation();
|
2010-08-27 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 07:48:34 +00:00
|
|
|
bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
|
2010-08-30 23:34:17 +00:00
|
|
|
if (S->getType()->isVectorTy()) return false;
|
2010-09-03 19:08:37 +00:00
|
|
|
if (isa<Constant>(S->getOperand(0))) return false;
|
2010-08-30 23:34:17 +00:00
|
|
|
|
2010-08-27 23:31:36 +00:00
|
|
|
Constant *C = LVI->getConstant(S->getOperand(0), S->getParent());
|
|
|
|
if (!C) return false;
|
|
|
|
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(C);
|
|
|
|
if (!CI) return false;
|
|
|
|
|
2010-08-30 23:34:17 +00:00
|
|
|
S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
|
|
|
|
S->eraseFromParent();
|
|
|
|
|
2010-08-30 22:45:55 +00:00
|
|
|
++NumSelects;
|
|
|
|
|
2010-08-27 23:31:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-31 07:48:34 +00:00
|
|
|
bool CorrelatedValuePropagation::processPHI(PHINode *P) {
|
2010-08-30 23:34:17 +00:00
|
|
|
bool Changed = false;
|
2010-08-27 23:31:36 +00:00
|
|
|
|
|
|
|
BasicBlock *BB = P->getParent();
|
2010-08-30 23:34:17 +00:00
|
|
|
for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
|
|
|
|
Value *Incoming = P->getIncomingValue(i);
|
|
|
|
if (isa<Constant>(Incoming)) continue;
|
|
|
|
|
2010-08-27 23:31:36 +00:00
|
|
|
Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
|
|
|
|
P->getIncomingBlock(i),
|
|
|
|
BB);
|
2010-08-30 23:34:17 +00:00
|
|
|
if (!C) continue;
|
2010-08-27 23:31:36 +00:00
|
|
|
|
|
|
|
P->setIncomingValue(i, C);
|
2010-08-30 23:34:17 +00:00
|
|
|
Changed = true;
|
2010-08-27 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Value *ConstVal = P->hasConstantValue()) {
|
|
|
|
P->replaceAllUsesWith(ConstVal);
|
|
|
|
P->eraseFromParent();
|
2010-08-30 23:34:17 +00:00
|
|
|
Changed = true;
|
2010-08-27 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 22:45:55 +00:00
|
|
|
++NumPhis;
|
|
|
|
|
2010-08-30 23:34:17 +00:00
|
|
|
return Changed;
|
2010-08-27 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-03 19:08:37 +00:00
|
|
|
bool CorrelatedValuePropagation::processMemAccess(Instruction *I) {
|
|
|
|
Value *Pointer = 0;
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(I))
|
|
|
|
Pointer = L->getPointerOperand();
|
|
|
|
else
|
|
|
|
Pointer = cast<StoreInst>(I)->getPointerOperand();
|
|
|
|
|
|
|
|
if (isa<Constant>(Pointer)) return false;
|
|
|
|
|
|
|
|
Constant *C = LVI->getConstant(Pointer, I->getParent());
|
|
|
|
if (!C) return false;
|
|
|
|
|
|
|
|
++NumMemAccess;
|
|
|
|
I->replaceUsesOfWith(Pointer, C);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-03 22:47:08 +00:00
|
|
|
/// processCmp - If the value of this comparison could be determined locally,
|
|
|
|
/// constant propagation would already have figured it out. Instead, walk
|
|
|
|
/// the predecessors and statically evaluate the comparison based on information
|
|
|
|
/// available on that edge. If a given static evaluation is true on ALL
|
|
|
|
/// incoming edges, then it's true universally and we can simplify the compare.
|
|
|
|
bool CorrelatedValuePropagation::processCmp(CmpInst *C) {
|
|
|
|
Value *Op0 = C->getOperand(0);
|
|
|
|
if (isa<Instruction>(Op0) &&
|
|
|
|
cast<Instruction>(Op0)->getParent() == C->getParent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
|
|
|
|
if (!Op1) return false;
|
|
|
|
|
|
|
|
pred_iterator PI = pred_begin(C->getParent()), PE = pred_end(C->getParent());
|
|
|
|
if (PI == PE) return false;
|
|
|
|
|
|
|
|
LazyValueInfo::Tristate Result = LVI->getPredicateOnEdge(C->getPredicate(),
|
|
|
|
C->getOperand(0), Op1, *PI, C->getParent());
|
|
|
|
if (Result == LazyValueInfo::Unknown) return false;
|
|
|
|
|
|
|
|
++PI;
|
|
|
|
while (PI != PE) {
|
|
|
|
LazyValueInfo::Tristate Res = LVI->getPredicateOnEdge(C->getPredicate(),
|
|
|
|
C->getOperand(0), Op1, *PI, C->getParent());
|
|
|
|
if (Res != Result) return false;
|
|
|
|
++PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
++NumCmps;
|
|
|
|
|
|
|
|
if (Result == LazyValueInfo::True)
|
|
|
|
C->replaceAllUsesWith(ConstantInt::getTrue(C->getContext()));
|
|
|
|
else
|
|
|
|
C->replaceAllUsesWith(ConstantInt::getFalse(C->getContext()));
|
|
|
|
|
|
|
|
C->eraseFromParent();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-31 07:48:34 +00:00
|
|
|
bool CorrelatedValuePropagation::runOnFunction(Function &F) {
|
2010-08-27 23:31:36 +00:00
|
|
|
LVI = &getAnalysis<LazyValueInfo>();
|
|
|
|
|
2010-08-31 18:55:52 +00:00
|
|
|
bool FnChanged = false;
|
2010-08-27 23:31:36 +00:00
|
|
|
|
2010-09-16 18:35:07 +00:00
|
|
|
// Perform a depth-first walk of the CFG so that we don't waste time
|
|
|
|
// optimizing unreachable blocks.
|
|
|
|
for (df_iterator<BasicBlock*> FI = df_begin(&F.getEntryBlock()),
|
|
|
|
FE = df_end(&F.getEntryBlock()); FI != FE; ++FI) {
|
2010-08-31 18:55:52 +00:00
|
|
|
bool BBChanged = false;
|
2010-08-27 23:31:36 +00:00
|
|
|
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
|
|
|
|
Instruction *II = BI++;
|
2010-09-03 19:08:37 +00:00
|
|
|
switch (II->getOpcode()) {
|
|
|
|
case Instruction::Select:
|
|
|
|
BBChanged |= processSelect(cast<SelectInst>(II));
|
|
|
|
break;
|
|
|
|
case Instruction::PHI:
|
|
|
|
BBChanged |= processPHI(cast<PHINode>(II));
|
|
|
|
break;
|
2010-09-03 22:47:08 +00:00
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
|
|
|
BBChanged |= processCmp(cast<CmpInst>(II));
|
|
|
|
break;
|
2010-09-03 19:08:37 +00:00
|
|
|
case Instruction::Load:
|
|
|
|
case Instruction::Store:
|
|
|
|
BBChanged |= processMemAccess(II);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-27 23:31:36 +00:00
|
|
|
}
|
2010-08-31 07:55:56 +00:00
|
|
|
|
2010-08-31 18:55:52 +00:00
|
|
|
// Propagating correlated values might leave cruft around.
|
|
|
|
// Try to clean it up before we continue.
|
|
|
|
if (BBChanged)
|
2010-09-16 18:35:07 +00:00
|
|
|
SimplifyInstructionsInBlock(*FI);
|
2010-08-31 18:55:52 +00:00
|
|
|
|
|
|
|
FnChanged |= BBChanged;
|
2010-08-31 07:55:56 +00:00
|
|
|
}
|
2010-08-27 23:31:36 +00:00
|
|
|
|
2010-08-31 18:55:52 +00:00
|
|
|
return FnChanged;
|
2010-08-28 00:11:12 +00:00
|
|
|
}
|