[llvm] InstCombine/Demanded: simplify srem case (NFC) (PR #110260)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 27 06:26:57 PDT 2024
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/110260
>From b5f09564a844450153ab56447c40700177f311fc Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Fri, 27 Sep 2024 13:45:53 +0100
Subject: [PATCH 1/2] InstCombine/Demanded: simplify srem case (NFC)
The srem case of SimplifyDemandedUseBits partially duplicates
KnownBits::srem. It is guarded by a statement that takes the absolute
value of the RHS and checks whether it is a power of 2, but the abs()
call here useless, since an srem with a negative RHS is flipped into one
with a positive RHS, adjusting LHS appropriately. Stripping the abs call
allows us to call KnownBits::srem instead of partially duplicating it.
---
.../InstCombineSimplifyDemanded.cpp | 23 ++++---------------
1 file changed, 4 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index c66db9285c799a..8018ffc7b6752b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -861,30 +861,15 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
// X % -1 demands all the bits because we don't want to introduce
// INT_MIN % -1 (== undef) by accident.
if (match(I->getOperand(1), m_APInt(Rem)) && !Rem->isAllOnes()) {
- APInt RA = Rem->abs();
- if (RA.isPowerOf2()) {
- if (DemandedMask.ult(RA)) // srem won't affect demanded bits
+ if (Rem->isPowerOf2()) {
+ if (DemandedMask.ult(*Rem)) // srem won't affect demanded bits
return I->getOperand(0);
- APInt LowBits = RA - 1;
+ APInt LowBits = *Rem - 1;
APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
return I;
-
- // The low bits of LHS are unchanged by the srem.
- Known.Zero = LHSKnown.Zero & LowBits;
- Known.One = LHSKnown.One & LowBits;
-
- // If LHS is non-negative or has all low bits zero, then the upper bits
- // are all zero.
- if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
- Known.Zero |= ~LowBits;
-
- // If LHS is negative and not all low bits are zero, then the upper bits
- // are all one.
- if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
- Known.One |= ~LowBits;
-
+ Known = KnownBits::srem(LHSKnown, KnownBits::makeConstant(*Rem));
break;
}
}
>From 7d0b7ca9d56a064b68952281593da840e7eba674 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Fri, 27 Sep 2024 14:25:49 +0100
Subject: [PATCH 2/2] InstCombine/Demanded: address review
---
.../InstCombineSimplifyDemanded.cpp | 22 ++++++++-----------
1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 8018ffc7b6752b..dd31bfa7e65f50 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -858,20 +858,16 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
}
case Instruction::SRem: {
const APInt *Rem;
- // X % -1 demands all the bits because we don't want to introduce
- // INT_MIN % -1 (== undef) by accident.
- if (match(I->getOperand(1), m_APInt(Rem)) && !Rem->isAllOnes()) {
- if (Rem->isPowerOf2()) {
- if (DemandedMask.ult(*Rem)) // srem won't affect demanded bits
- return I->getOperand(0);
+ if (match(I->getOperand(1), m_APInt(Rem)) && Rem->isPowerOf2()) {
+ if (DemandedMask.ult(*Rem)) // srem won't affect demanded bits
+ return I->getOperand(0);
- APInt LowBits = *Rem - 1;
- APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
- if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
- return I;
- Known = KnownBits::srem(LHSKnown, KnownBits::makeConstant(*Rem));
- break;
- }
+ APInt LowBits = *Rem - 1;
+ APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
+ if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
+ return I;
+ Known = KnownBits::srem(LHSKnown, KnownBits::makeConstant(*Rem));
+ break;
}
llvm::computeKnownBits(I, Known, Depth, Q);
More information about the llvm-commits
mailing list