[llvm] [DemandedBits] Support non-constant shift amounts (PR #148880)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 2 10:57:17 PDT 2025


================
@@ -183,6 +196,17 @@ void DemandedBits::determineLiveOperandBits(
           AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
         else if (S->hasNoUnsignedWrap())
           AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
+      } else {
+        ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
+        uint64_t Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
+        uint64_t Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
+        // similar to Lshr case
+        GetShiftedRange(Min, Max, /*ShiftLeft=*/false);
+        const auto *S = cast<ShlOperator>(UserI);
+        if (S->hasNoSignedWrap())
+          AB |= APInt::getHighBitsSet(BitWidth, Max + 1);
+        else if (S->hasNoUnsignedWrap())
+          AB |= APInt::getHighBitsSet(BitWidth, Max);
----------------
topperc wrote:

How much of a real world difference is there between what this patch does and

```
unsigned ActiveBits = AOut.getActiveBits);
AB = APInt::getLowBitsSet(BitWidth, ActiveBits);
if (S->hasNoSignedWrap())
  AB |= APInt::getHighBitsSet(BitWidth, BitWidth);
else if (S->hasNoUnsignedWrap())
  AB |= APInt::getHighBitsSet(BitWidth, BitWidth-1);
```

That doesn't require calling computeKnownBits on the shift amount and is consistent with what InstCombineSimplifyDemanded does.

https://github.com/llvm/llvm-project/pull/148880


More information about the llvm-commits mailing list