From 8be336f7ed1c44abc299fb00a25ed593e5564de1 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 14 Feb 2018 16:50:55 +0000 Subject: [PATCH] [InstCombine] refactor folds for mul with negated operands; NFCI This keeps with our current usage of 'match' and is easier to see that the optional NSW only applies in the non-constant operand case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325140 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstCombineMulDivRem.cpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index f6907841016..f884221bff1 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -270,15 +270,20 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { } } - if (Value *Op0v = dyn_castNegVal(Op0)) { // -X * -Y = X*Y - if (Value *Op1v = dyn_castNegVal(Op1)) { - BinaryOperator *BO = BinaryOperator::CreateMul(Op0v, Op1v); - if (I.hasNoSignedWrap() && - match(Op0, m_NSWSub(m_Value(), m_Value())) && - match(Op1, m_NSWSub(m_Value(), m_Value()))) - BO->setHasNoSignedWrap(); - return BO; - } + // -X * C --> X * -C + Value *X, *Y; + Constant *Op1C; + if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C))) + return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C)); + + // -X * -Y --> X * Y + if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) { + auto *NewMul = BinaryOperator::CreateMul(X, Y); + if (I.hasNoSignedWrap() && + cast(Op0)->hasNoSignedWrap() && + cast(Op1)->hasNoSignedWrap()) + NewMul->setHasNoSignedWrap(); + return NewMul; } // (X / Y) * Y = X - (X % Y) @@ -342,7 +347,6 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { // (bool X) * Y --> X ? Y : 0 // Y * (bool X) --> X ? Y : 0 - Value *X; if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0)); if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))