2010-08-31 15:48:34 +08:00
|
|
|
//===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
|
2010-08-28 07:31:36 +08: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 15:48:34 +08:00
|
|
|
// This file implements the Correlated Value Propagation pass.
|
2010-08-28 07:31:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-31 15:48:34 +08:00
|
|
|
#define DEBUG_TYPE "correlated-value-propagation"
|
2010-08-28 07:31:36 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Analysis/LazyValueInfo.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2010-08-31 06:45:55 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2010-08-28 07:31:36 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-04 03:08:37 +08:00
|
|
|
STATISTIC(NumPhis, "Number of phis propagated");
|
|
|
|
STATISTIC(NumSelects, "Number of selects propagated");
|
|
|
|
STATISTIC(NumMemAccess, "Number of memory access targets propagated");
|
2010-08-31 06:45:55 +08:00
|
|
|
|
2010-08-28 07:31:36 +08:00
|
|
|
namespace {
|
2010-08-31 15:48:34 +08:00
|
|
|
class CorrelatedValuePropagation : public FunctionPass {
|
2010-08-28 07:31:36 +08:00
|
|
|
LazyValueInfo *LVI;
|
|
|
|
|
|
|
|
bool processSelect(SelectInst *SI);
|
|
|
|
bool processPHI(PHINode *P);
|
2010-09-04 03:08:37 +08:00
|
|
|
bool processMemAccess(Instruction *I);
|
2010-08-28 07:31:36 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2010-08-31 15:48:34 +08:00
|
|
|
CorrelatedValuePropagation(): FunctionPass(ID) { }
|
2010-08-28 07:31:36 +08:00
|
|
|
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<LazyValueInfo>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:48:34 +08:00
|
|
|
char CorrelatedValuePropagation::ID = 0;
|
|
|
|
INITIALIZE_PASS(CorrelatedValuePropagation, "correlated-propagation",
|
2010-08-28 07:31:36 +08:00
|
|
|
"Value Propagation", false, false);
|
|
|
|
|
|
|
|
// Public interface to the Value Propagation pass
|
2010-08-31 15:48:34 +08:00
|
|
|
Pass *llvm::createCorrelatedValuePropagationPass() {
|
|
|
|
return new CorrelatedValuePropagation();
|
2010-08-28 07:31:36 +08:00
|
|
|
}
|
|
|
|
|
2010-08-31 15:48:34 +08:00
|
|
|
bool CorrelatedValuePropagation::processSelect(SelectInst *S) {
|
2010-08-31 07:34:17 +08:00
|
|
|
if (S->getType()->isVectorTy()) return false;
|
2010-09-04 03:08:37 +08:00
|
|
|
if (isa<Constant>(S->getOperand(0))) return false;
|
2010-08-31 07:34:17 +08:00
|
|
|
|
2010-08-28 07:31:36 +08: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-31 07:34:17 +08:00
|
|
|
S->replaceAllUsesWith(S->getOperand(CI->isOne() ? 1 : 2));
|
|
|
|
S->eraseFromParent();
|
|
|
|
|
2010-08-31 06:45:55 +08:00
|
|
|
++NumSelects;
|
|
|
|
|
2010-08-28 07:31:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:48:34 +08:00
|
|
|
bool CorrelatedValuePropagation::processPHI(PHINode *P) {
|
2010-08-31 07:34:17 +08:00
|
|
|
bool Changed = false;
|
2010-08-28 07:31:36 +08:00
|
|
|
|
|
|
|
BasicBlock *BB = P->getParent();
|
2010-08-31 07:34:17 +08:00
|
|
|
for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
|
|
|
|
Value *Incoming = P->getIncomingValue(i);
|
|
|
|
if (isa<Constant>(Incoming)) continue;
|
|
|
|
|
2010-08-28 07:31:36 +08:00
|
|
|
Constant *C = LVI->getConstantOnEdge(P->getIncomingValue(i),
|
|
|
|
P->getIncomingBlock(i),
|
|
|
|
BB);
|
2010-08-31 07:34:17 +08:00
|
|
|
if (!C) continue;
|
2010-08-28 07:31:36 +08:00
|
|
|
|
|
|
|
P->setIncomingValue(i, C);
|
2010-08-31 07:34:17 +08:00
|
|
|
Changed = true;
|
2010-08-28 07:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Value *ConstVal = P->hasConstantValue()) {
|
|
|
|
P->replaceAllUsesWith(ConstVal);
|
|
|
|
P->eraseFromParent();
|
2010-08-31 07:34:17 +08:00
|
|
|
Changed = true;
|
2010-08-28 07:31:36 +08:00
|
|
|
}
|
|
|
|
|
2010-08-31 06:45:55 +08:00
|
|
|
++NumPhis;
|
|
|
|
|
2010-08-31 07:34:17 +08:00
|
|
|
return Changed;
|
2010-08-28 07:31:36 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 03:08:37 +08: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-08-31 15:48:34 +08:00
|
|
|
bool CorrelatedValuePropagation::runOnFunction(Function &F) {
|
2010-08-28 07:31:36 +08:00
|
|
|
LVI = &getAnalysis<LazyValueInfo>();
|
|
|
|
|
2010-09-01 02:55:52 +08:00
|
|
|
bool FnChanged = false;
|
2010-08-28 07:31:36 +08:00
|
|
|
|
2010-08-31 15:55:56 +08:00
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
|
2010-09-01 02:55:52 +08:00
|
|
|
bool BBChanged = false;
|
2010-08-28 07:31:36 +08:00
|
|
|
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
|
|
|
|
Instruction *II = BI++;
|
2010-09-04 03:08:37 +08:00
|
|
|
switch (II->getOpcode()) {
|
|
|
|
case Instruction::Select:
|
|
|
|
BBChanged |= processSelect(cast<SelectInst>(II));
|
|
|
|
break;
|
|
|
|
case Instruction::PHI:
|
|
|
|
BBChanged |= processPHI(cast<PHINode>(II));
|
|
|
|
break;
|
|
|
|
case Instruction::Load:
|
|
|
|
case Instruction::Store:
|
|
|
|
BBChanged |= processMemAccess(II);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-28 07:31:36 +08:00
|
|
|
}
|
2010-08-31 15:55:56 +08:00
|
|
|
|
2010-09-01 02:55:52 +08:00
|
|
|
// Propagating correlated values might leave cruft around.
|
|
|
|
// Try to clean it up before we continue.
|
|
|
|
if (BBChanged)
|
|
|
|
SimplifyInstructionsInBlock(FI);
|
|
|
|
|
|
|
|
FnChanged |= BBChanged;
|
2010-08-31 15:55:56 +08:00
|
|
|
}
|
2010-08-28 07:31:36 +08:00
|
|
|
|
2010-09-01 02:55:52 +08:00
|
|
|
return FnChanged;
|
2010-08-28 08:11:12 +08:00
|
|
|
}
|