Skip to content

Commit c5f139c

Browse files
artagnonpuja2196
authored andcommitted
InstCombine/Demanded: simplify srem case (NFC) (#110260)
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.
1 parent 485d227 commit c5f139c

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -858,35 +858,16 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
858858
}
859859
case Instruction::SRem: {
860860
const APInt *Rem;
861-
// X % -1 demands all the bits because we don't want to introduce
862-
// INT_MIN % -1 (== undef) by accident.
863-
if (match(I->getOperand(1), m_APInt(Rem)) && !Rem->isAllOnes()) {
864-
APInt RA = Rem->abs();
865-
if (RA.isPowerOf2()) {
866-
if (DemandedMask.ult(RA)) // srem won't affect demanded bits
867-
return I->getOperand(0);
868-
869-
APInt LowBits = RA - 1;
870-
APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
871-
if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
872-
return I;
873-
874-
// The low bits of LHS are unchanged by the srem.
875-
Known.Zero = LHSKnown.Zero & LowBits;
876-
Known.One = LHSKnown.One & LowBits;
877-
878-
// If LHS is non-negative or has all low bits zero, then the upper bits
879-
// are all zero.
880-
if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero))
881-
Known.Zero |= ~LowBits;
861+
if (match(I->getOperand(1), m_APInt(Rem)) && Rem->isPowerOf2()) {
862+
if (DemandedMask.ult(*Rem)) // srem won't affect demanded bits
863+
return I->getOperand(0);
882864

883-
// If LHS is negative and not all low bits are zero, then the upper bits
884-
// are all one.
885-
if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One))
886-
Known.One |= ~LowBits;
887-
888-
break;
889-
}
865+
APInt LowBits = *Rem - 1;
866+
APInt Mask2 = LowBits | APInt::getSignMask(BitWidth);
867+
if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1, Q))
868+
return I;
869+
Known = KnownBits::srem(LHSKnown, KnownBits::makeConstant(*Rem));
870+
break;
890871
}
891872

892873
llvm::computeKnownBits(I, Known, Depth, Q);

0 commit comments

Comments
 (0)