[llvm] d344146 - [InstCombine] reduce code duplication in visitMul(); NFC
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 29 06:27:36 PDT 2022
Author: Sanjay Patel
Date: 2022-10-29T09:26:02-04:00
New Revision: d344146857aa105a24e06f2526d88e96c5865d00
URL: https://github.com/llvm/llvm-project/commit/d344146857aa105a24e06f2526d88e96c5865d00
DIFF: https://github.com/llvm/llvm-project/commit/d344146857aa105a24e06f2526d88e96c5865d00.diff
LOG: [InstCombine] reduce code duplication in visitMul(); NFC
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 829b619f3c3bd..7df8e7f936ad5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -141,8 +141,8 @@ static Value *foldMulSelectToNegate(BinaryOperator &I,
}
Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
- if (Value *V = simplifyMulInst(I.getOperand(0), I.getOperand(1),
- SQ.getWithInstruction(&I)))
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ if (Value *V = simplifyMulInst(Op0, Op1, SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);
if (SimplifyAssociativeOrCommutative(I))
@@ -157,16 +157,15 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
if (Value *V = SimplifyUsingDistributiveLaws(I))
return replaceInstUsesWith(I, V);
- Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Type *Ty = I.getType();
- unsigned BitWidth = Ty->getScalarSizeInBits();
+ const unsigned BitWidth = Ty->getScalarSizeInBits();
+ const bool HasNSW = I.hasNoSignedWrap();
+ const bool HasNUW = I.hasNoUnsignedWrap();
- // X * -1 == 0 - X
+ // X * -1 --> 0 - X
if (match(Op1, m_AllOnes())) {
- BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
- if (I.hasNoSignedWrap())
- BO->setHasNoSignedWrap();
- return BO;
+ return HasNSW ? BinaryOperator::CreateNSWNeg(Op0)
+ : BinaryOperator::CreateNeg(Op0);
}
// Also allow combining multiply instructions on vectors.
@@ -181,10 +180,9 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
Constant *Shl = ConstantExpr::getShl(C1, C2);
BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
- if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
+ if (HasNUW && Mul->hasNoUnsignedWrap())
BO->setHasNoUnsignedWrap();
- if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
- Shl->isNotMinSignedValue())
+ if (HasNSW && Mul->hasNoSignedWrap() && Shl->isNotMinSignedValue())
BO->setHasNoSignedWrap();
return BO;
}
@@ -194,9 +192,9 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
if (Constant *NewCst = ConstantExpr::getExactLogBase2(C1)) {
BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
- if (I.hasNoUnsignedWrap())
+ if (HasNUW)
Shl->setHasNoUnsignedWrap();
- if (I.hasNoSignedWrap()) {
+ if (HasNSW) {
const APInt *V;
if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1)
Shl->setHasNoSignedWrap();
@@ -257,7 +255,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
(BOp0->getOpcode() == Instruction::Or || BOp0->hasNoUnsignedWrap());
Value *NewMul = Builder.CreateMul(X, MulC);
auto *BO = BinaryOperator::CreateAdd(NewMul, NewC);
- if (I.hasNoUnsignedWrap() && Op0NUW) {
+ if (HasNUW && Op0NUW) {
// If NewMulBO is constant we also can set BO to nuw.
if (auto *NewMulBO = dyn_cast<BinaryOperator>(NewMul))
NewMulBO->setHasNoUnsignedWrap();
@@ -288,8 +286,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
// -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<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
+ if (HasNSW && cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap())
NewMul->setHasNoSignedWrap();
return NewMul;
@@ -359,10 +356,8 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
}
if (BO) {
- if (I.hasNoUnsignedWrap())
- BO->setHasNoUnsignedWrap();
- if (I.hasNoSignedWrap() && ShlNSW)
- BO->setHasNoSignedWrap();
+ BO->setHasNoUnsignedWrap(HasNUW);
+ BO->setHasNoSignedWrap(HasNSW && ShlNSW);
return BO;
}
}
@@ -436,8 +431,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
m_One()),
m_Deferred(X)))) {
Value *Abs = Builder.CreateBinaryIntrinsic(
- Intrinsic::abs, X,
- ConstantInt::getBool(I.getContext(), I.hasNoSignedWrap()));
+ Intrinsic::abs, X, ConstantInt::getBool(I.getContext(), HasNSW));
Abs->takeName(&I);
return replaceInstUsesWith(I, Abs);
}
@@ -446,12 +440,12 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
return Ext;
bool Changed = false;
- if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) {
+ if (!HasNSW && willNotOverflowSignedMul(Op0, Op1, I)) {
Changed = true;
I.setHasNoSignedWrap(true);
}
- if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) {
+ if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1, I)) {
Changed = true;
I.setHasNoUnsignedWrap(true);
}
More information about the llvm-commits
mailing list