[SCCP] Generalize tryToReplaceInstWithConstant to work also with arguments.

llvm-svn: 275357
This commit is contained in:
Davide Italiano 2016-07-14 01:27:29 +00:00
parent 284652beec
commit 6ed6d77950
1 changed files with 18 additions and 22 deletions

View File

@ -1510,16 +1510,16 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
return false; return false;
} }
static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst, template <typename ArgOrInst>
bool shouldEraseFromParent) { static bool tryToReplaceWithConstant(SCCPSolver Solver, ArgOrInst *AI) {
Constant *Const = nullptr; Constant *Const = nullptr;
if (Inst->getType()->isStructTy()) { if (AI->getType()->isStructTy()) {
std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(Inst); std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(AI);
if (std::any_of(IVs.begin(), IVs.end(), if (std::any_of(IVs.begin(), IVs.end(),
[](LatticeVal &LV) { return LV.isOverdefined(); })) [](LatticeVal &LV) { return LV.isOverdefined(); }))
return false; return false;
std::vector<Constant *> ConstVals; std::vector<Constant *> ConstVals;
StructType *ST = dyn_cast<StructType>(Inst->getType()); StructType *ST = dyn_cast<StructType>(AI->getType());
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
LatticeVal V = IVs[i]; LatticeVal V = IVs[i];
ConstVals.push_back(V.isConstant() ConstVals.push_back(V.isConstant()
@ -1528,18 +1528,23 @@ static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst,
} }
Const = ConstantStruct::get(ST, ConstVals); Const = ConstantStruct::get(ST, ConstVals);
} else { } else {
LatticeVal IV = Solver.getLatticeValueFor(Inst); LatticeVal IV = Solver.getLatticeValueFor(AI);
if (IV.isOverdefined()) if (IV.isOverdefined())
return false; return false;
Const = IV.isConstant() ? IV.getConstant() : UndefValue::get(AI->getType());
Const =
IV.isConstant() ? IV.getConstant() : UndefValue::get(Inst->getType());
} }
assert(Const && "Constant is nullptr here!"); assert(Const && "Constant is nullptr here!");
DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n'); DEBUG(dbgs() << " Constant: " << *Const << " = " << *AI << '\n');
// Replaces all of the uses of a variable with uses of the constant. // Replaces all of the uses of a variable with uses of the constant.
Inst->replaceAllUsesWith(Const); AI->replaceAllUsesWith(Const);
return true;
}
static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst,
bool shouldEraseFromParent) {
if (!tryToReplaceWithConstant(Solver, Inst))
return false;
// Delete the instruction. // Delete the instruction.
if (shouldEraseFromParent) if (shouldEraseFromParent)
@ -1766,16 +1771,7 @@ static bool runIPSCCP(Module &M, const DataLayout &DL,
// TODO: Could use getStructLatticeValueFor to find out if the entire // TODO: Could use getStructLatticeValueFor to find out if the entire
// result is a constant and replace it entirely if so. // result is a constant and replace it entirely if so.
LatticeVal IV = Solver.getLatticeValueFor(&*AI); if (tryToReplaceWithConstant(Solver, &*AI))
if (IV.isOverdefined()) continue;
Constant *CST = IV.isConstant() ?
IV.getConstant() : UndefValue::get(AI->getType());
DEBUG(dbgs() << "*** Arg " << *AI << " = " << *CST <<"\n");
// Replaces all of the uses of a variable with uses of the
// constant.
AI->replaceAllUsesWith(CST);
++IPNumArgsElimed; ++IPNumArgsElimed;
} }
} }