Skip to content

Commit 246d414

Browse files
committed
use ComputeNumSignBits() instead and remove zext handling
1 parent f5f6d15 commit 246d414

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -430,40 +430,13 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
430430
// possible square. This allows InstCombine and other passes to fold (x * x) &
431431
// (1 << N) to 0 when N is out of range.
432432
const Value *A = nullptr;
433-
// Only handle the case where both operands are the same extension of the same
434-
// value.
435-
if ((match(Op0, m_SExt(m_Value(A))) && match(Op1, m_SExt(m_Specific(A)))) ||
436-
(match(Op0, m_ZExt(m_Value(A))) && match(Op1, m_ZExt(m_Specific(A))))) {
437-
Type *FromTy = A->getType();
438-
Type *ToTy = Op0->getType();
439-
if (FromTy->isIntegerTy() && ToTy->isIntegerTy() &&
440-
FromTy->getScalarSizeInBits() < ToTy->getScalarSizeInBits()) {
441-
unsigned FromBits = FromTy->getScalarSizeInBits();
442-
unsigned ToBits = ToTy->getScalarSizeInBits();
443-
// For both signed and unsigned, the maximum absolute value is max(|min|,
444-
// |max|)
445-
APInt minVal(FromBits, 0), maxVal(FromBits, 0);
446-
bool isSigned = isa<SExtInst>(Op0);
447-
if (isSigned) {
448-
minVal = APInt::getSignedMinValue(FromBits);
449-
maxVal = APInt::getSignedMaxValue(FromBits);
450-
} else {
451-
minVal = APInt::getMinValue(FromBits);
452-
maxVal = APInt::getMaxValue(FromBits);
453-
}
454-
APInt absMin = minVal.abs();
455-
APInt absMax = maxVal.abs();
456-
APInt maxAbs = absMin.ugt(absMax) ? absMin : absMax;
457-
APInt maxSquare = maxAbs.zext(ToBits);
458-
maxSquare = maxSquare * maxSquare;
459-
// All bits above the highest set bit in maxSquare are known zero.
460-
unsigned MaxBit = maxSquare.isZero() ? 0 : maxSquare.logBase2();
461-
if (MaxBit + 1 < ToBits) {
462-
APInt KnownZeroMask =
463-
APInt::getHighBitsSet(ToBits, ToBits - (MaxBit + 1));
464-
Known.Zero |= KnownZeroMask;
465-
}
466-
}
433+
434+
if (match(Op0, m_SExt(m_Value(A))) && match(Op1, m_SExt(m_Specific(A)))) {
435+
// Product of (sext x) * (sext x) is always non-negative.
436+
// So we know the sign bit itself is zero.
437+
unsigned SignBits = ComputeNumSignBits(Op0, Q, Depth);
438+
if (SignBits > 1)
439+
Known.Zero.setHighBits(SignBits - 1);
467440
}
468441
}
469442

0 commit comments

Comments
 (0)