[llvm] [ValueTracking] Let ComputeKnownSignBits handle (shl (zext X), C) (PR #97693)
Björn Pettersson via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 4 15:13:12 PDT 2024
================
@@ -3757,11 +3757,21 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
}
case Instruction::Shl: {
const APInt *ShAmt;
+ Value *X = nullptr;
if (match(U->getOperand(1), m_APInt(ShAmt))) {
// shl destroys sign bits.
- Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
- if (ShAmt->uge(TyBits) || // Bad shift.
- ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
+ if (ShAmt->uge(TyBits))
+ break; // Bad shift.
+ // We can look through a zext (more or less treating it as a sext) if
+ // all extended bits are shifted out.
+ if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
+ ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
+ Tmp = ComputeNumSignBits(X, Depth + 1, Q);
----------------
bjope wrote:
Seems to originate from this patch back in 2017: https://github.com/llvm/llvm-project/commit/22178dd33b3460207b8b1dd109339524e50feab5
So the idea actually was to avoid ASHR->LSHR rewrites in certain situations (based on number of known sign bits).
Unfortunately there are no good motivating test cases. Even the test case added in that commit would still pass if reverting the change in SimplifyDemandedUseBits.
https://github.com/llvm/llvm-project/pull/97693
More information about the llvm-commits
mailing list