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

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 30 04:54:39 PDT 2025


================
@@ -217,6 +259,26 @@ void DemandedBits::determineLiveOperandBits(
         // (they must be zero).
         if (cast<AShrOperator>(UserI)->isExact())
           AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
+      } else {
+        ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
+        unsigned Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
+        unsigned Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
+        GetShiftedRange(Min, Max, /*ShiftLeft=*/true);
+        if (Max) {
+          // Suppose AOut = 0011 1100
+          // [min, max] = [1, 3]
+          // ShiftAmount = 1 : Mask is 1000 0000
+          // ShiftAmount = 2 : Mask is 1100 0000
+          // ShiftAmount = 3 : Mask is 1110 0000
+          // The Mask with Max covers every case in [min, max],
+          // so we are done
+          if ((AOut & APInt::getHighBitsSet(BitWidth, Max)).getBoolValue())
+            AB.setSignBit();
+        }
----------------
artagnon wrote:

```suggestion
        if (Max && (AOut & APInt::getHighBitsSet(BitWidth, Max)).getBoolValue()) {
          // Suppose AOut = 0011 1100
          // [min, max] = [1, 3]
          // ShiftAmount = 1 : Mask is 1000 0000
          // ShiftAmount = 2 : Mask is 1100 0000
          // ShiftAmount = 3 : Mask is 1110 0000
          // The Mask with Max covers every case in [min, max],
          // so we are done
           AB.setSignBit();
        }
```

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


More information about the llvm-commits mailing list