[InstCombine] move constant check into foldBinOpIntoSelectOrPhi; NFCI

Also, rename 'foldOpWithConstantIntoOperand' because that's annoyingly 
vague. The constant check is redundant in some cases, but it allows 
removing duplication for most of the calls.

llvm-svn: 326329
This commit is contained in:
Sanjay Patel 2018-02-28 16:36:24 +00:00
parent 9de940b93b
commit 8fdd87f929
6 changed files with 29 additions and 34 deletions

View File

@ -964,7 +964,7 @@ Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) {
if (!match(Op1, m_Constant(Op1C)))
return nullptr;
if (Instruction *NV = foldOpWithConstantIntoOperand(Add))
if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add))
return NV;
Value *X;
@ -1304,9 +1304,8 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (isa<Constant>(RHS))
if (Instruction *FoldedFAdd = foldOpWithConstantIntoOperand(I))
return FoldedFAdd;
if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I))
return FoldedFAdd;
// -A + B --> B - A
if (Value *LHSV = dyn_castFNegVal(LHS))

View File

@ -1359,9 +1359,8 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
if (Instruction *Z = narrowMaskedBinOp(I))
return Z;
if (isa<Constant>(Op1))
if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
return FoldedLogic;
if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
return FoldedLogic;
if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
return DeMorgan;
@ -1850,9 +1849,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) {
if (Value *V = SimplifyBSwap(I, Builder))
return replaceInstUsesWith(I, V);
if (isa<Constant>(Op1))
if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
return FoldedLogic;
if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
return FoldedLogic;
// Given an OR instruction, check to see if this is a bswap.
if (Instruction *BSwap = MatchBSwap(I))
@ -2377,9 +2375,8 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) {
}
}
if (isa<Constant>(Op1))
if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I))
return FoldedLogic;
if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
return FoldedLogic;
{
Value *A, *B;

View File

@ -692,7 +692,7 @@ private:
Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
/// This is a convenience wrapper function for the above two functions.
Instruction *foldOpWithConstantIntoOperand(BinaryOperator &I);
Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
Instruction *foldAddWithConstant(BinaryOperator &Add);

View File

@ -251,22 +251,20 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
}
}
if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
return FoldedMul;
// Simplify mul instructions with a constant RHS.
if (isa<Constant>(Op1)) {
if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
return FoldedMul;
// Canonicalize (X+C1)*CI -> X*CI+C1*CI.
{
Value *X;
Constant *C1;
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
Value *Mul = Builder.CreateMul(C1, Op1);
// Only go forward with the transform if C1*CI simplifies to a tidier
// constant.
if (!match(Mul, m_Mul(m_Value(), m_Value())))
return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul);
}
Value *X;
Constant *C1;
if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
Value *Mul = Builder.CreateMul(C1, Op1);
// Only go forward with the transform if C1*CI simplifies to a tidier
// constant.
if (!match(Mul, m_Mul(m_Value(), m_Value())))
return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul);
}
}
@ -555,11 +553,11 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
bool AllowReassociate = I.isFast();
if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
return FoldedMul;
// Simplify mul instructions with a constant RHS.
if (auto *C = dyn_cast<Constant>(Op1)) {
if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I))
return FoldedMul;
// -X * C --> X * -C
Value *X;
if (match(Op0, m_FNeg(m_Value(X))))
@ -900,7 +898,7 @@ Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
}
if (!C2->isNullValue()) // avoid X udiv 0
if (Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I))
if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I))
return FoldedDiv;
}

View File

@ -370,7 +370,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
assert(!Op1C->uge(TypeBits) &&
"Shift over the type width should have been removed already");
if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I))
if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
return FoldedShift;
// Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))

View File

@ -1089,8 +1089,9 @@ Instruction *InstCombiner::foldOpIntoPhi(Instruction &I, PHINode *PN) {
return replaceInstUsesWith(I, NewPN);
}
Instruction *InstCombiner::foldOpWithConstantIntoOperand(BinaryOperator &I) {
assert(isa<Constant>(I.getOperand(1)) && "Unexpected operand type");
Instruction *InstCombiner::foldBinOpIntoSelectOrPhi(BinaryOperator &I) {
if (!isa<Constant>(I.getOperand(1)))
return nullptr;
if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(0))) {
if (Instruction *NewSel = FoldOpIntoSelect(I, Sel))