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

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 2 11:21:21 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:

> We need to check on the big benchmarks that @dtcxzyw already ran, because BDCE has very few triggers in the llvm test suite.
> 
> Should I create a new PR with that approach?

@dtcxzyw what do you think?

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


More information about the llvm-commits mailing list