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

Panagiotis Karouzakis via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 2 11:03:09 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);
----------------
karouzakisp wrote:

Additionally, I added similar logic from here to InstCombineSimplifyDemanded and observed an improvement in code size of up to 1.0% with the O2 pipeline. This is a work in progress, though, that's why there isn't a pull request yet. 

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


More information about the llvm-commits mailing list