diff options
| author | Davide Italiano <davide@freebsd.org> | 2016-07-13 23:20:04 +0000 |
|---|---|---|
| committer | Davide Italiano <davide@freebsd.org> | 2016-07-13 23:20:04 +0000 |
| commit | 296e9785ba820372831ef7a74f93a4399507d3eb (patch) | |
| tree | 718e59c2d143cbde483151da5daead44f691faaf /llvm/lib/Transforms | |
| parent | ef345e1d3f982ef1c88fa363a6bec568b804ce1c (diff) | |
| download | bcm5719-llvm-296e9785ba820372831ef7a74f93a4399507d3eb.tar.gz bcm5719-llvm-296e9785ba820372831ef7a74f93a4399507d3eb.zip | |
[SCCP] Have the logic for replacing insts with constant in a single place.
The code was pretty much copy-pasted between SCCP and IPSCCP. The situation
became clearly worse after I introduced the support for folding structs in
SCCP. This commit is NFC as we currently (still) skip the replacement
step in IPSCCP, but I'll change this soon.
llvm-svn: 275339
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/SCCP.cpp | 103 |
1 files changed, 50 insertions, 53 deletions
diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp index 236bb076df2..6d7a6c1b648 100644 --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -1510,6 +1510,43 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) { return false; } +static bool tryToReplaceInstWithConstant(SCCPSolver Solver, Instruction *Inst, + bool shouldEraseFromParent) { + Constant *Const = nullptr; + if (Inst->getType()->isStructTy()) { + std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(Inst); + if (std::any_of(IVs.begin(), IVs.end(), + [](LatticeVal &LV) { return LV.isOverdefined(); })) + return false; + std::vector<Constant *> ConstVals; + StructType *ST = dyn_cast<StructType>(Inst->getType()); + for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { + LatticeVal V = IVs[i]; + ConstVals.push_back(V.isConstant() + ? V.getConstant() + : UndefValue::get(ST->getElementType(i))); + } + Const = ConstantStruct::get(ST, ConstVals); + } else { + LatticeVal IV = Solver.getLatticeValueFor(Inst); + if (IV.isOverdefined()) + return false; + + Const = + IV.isConstant() ? IV.getConstant() : UndefValue::get(Inst->getType()); + } + assert(Const && "Constant is nullptr here!"); + DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n'); + + // Replaces all of the uses of a variable with uses of the constant. + Inst->replaceAllUsesWith(Const); + + // Delete the instruction. + if (shouldEraseFromParent) + Inst->eraseFromParent(); + return true; +} + // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm, // and return true if the function was modified. // @@ -1558,41 +1595,12 @@ static bool runSCCP(Function &F, const DataLayout &DL, if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst)) continue; - Constant *Const = nullptr; - if (Inst->getType()->isStructTy()) { - std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(Inst); - if (std::any_of(IVs.begin(), IVs.end(), - [](LatticeVal &LV) { return LV.isOverdefined(); })) - continue; - std::vector<Constant *> ConstVals; - StructType *ST = dyn_cast<StructType>(Inst->getType()); - for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { - LatticeVal V = IVs[i]; - ConstVals.push_back(V.isConstant() - ? V.getConstant() - : UndefValue::get(ST->getElementType(i))); - } - Const = ConstantStruct::get(ST, ConstVals); - } else { - LatticeVal IV = Solver.getLatticeValueFor(Inst); - if (IV.isOverdefined()) - continue; - - Const = IV.isConstant() ? IV.getConstant() - : UndefValue::get(Inst->getType()); + if (tryToReplaceInstWithConstant(Solver, Inst, + true /* shouldEraseFromParent */)) { + // Hey, we just changed something! + MadeChanges = true; + ++NumInstRemoved; } - assert(Const && "Constant is nullptr here!"); - DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n'); - - // Replaces all of the uses of a variable with uses of the constant. - Inst->replaceAllUsesWith(Const); - - // Delete the instruction. - Inst->eraseFromParent(); - - // Hey, we just changed something! - MadeChanges = true; - ++NumInstRemoved; } } @@ -1795,25 +1803,14 @@ static bool runIPSCCP(Module &M, const DataLayout &DL, // TODO: Could use getStructLatticeValueFor to find out if the entire // result is a constant and replace it entirely if so. - LatticeVal IV = Solver.getLatticeValueFor(Inst); - if (IV.isOverdefined()) - continue; - - Constant *Const = IV.isConstant() - ? IV.getConstant() : UndefValue::get(Inst->getType()); - DEBUG(dbgs() << " Constant: " << *Const << " = " << *Inst << '\n'); - - // Replaces all of the uses of a variable with uses of the - // constant. - Inst->replaceAllUsesWith(Const); - - // Delete the instruction. - if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst)) - Inst->eraseFromParent(); - - // Hey, we just changed something! - MadeChanges = true; - ++IPNumInstRemoved; + if (tryToReplaceInstWithConstant( + Solver, Inst, + !isa<CallInst>(Inst) && + !isa<TerminatorInst>(Inst) /* shouldEraseFromParent */)) { + // Hey, we just changed something! + MadeChanges = true; + ++IPNumInstRemoved; + } } } |

