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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 10 01:39:48 PDT 2025


================
@@ -76,6 +76,19 @@ void DemandedBits::determineLiveOperandBits(
           computeKnownBits(V2, Known2, DL, &AC, UserI, &DT);
         }
       };
+  auto GetShiftedRange = [&](uint64_t Min, uint64_t Max, bool ShiftLeft) {
+    auto ShiftF = [ShiftLeft](const APInt &Mask, unsigned ShiftAmnt) {
+      return ShiftLeft ? Mask.shl(ShiftAmnt) : Mask.lshr(ShiftAmnt);
+    };
+    AB = APInt::getZero(BitWidth);
+    uint64_t LoopRange = Max - Min;
+    APInt Mask = AOut;
+    for (unsigned ShiftAmnt = 1; ShiftAmnt <= LoopRange; ShiftAmnt <<= 1) {
----------------
dtcxzyw wrote:

> computeKnownBits may tell us that the lsb(s) of the shift amount are known to be zero or one. This loop is trying to evaluate all possible shift amounts, but we don't exclude cases that would be impossible based on known values of the lsb(s).

Enumerating all possible shift amounts may be expensive (see my previous comment https://github.com/llvm/llvm-project/pull/148880#discussion_r2230036744). But I am fine to reuse the KnownBits' logic. `~KnownBits::shl/lshr/ashr(AOut, ShAmt).Zeros` is exactly what we want.



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


More information about the llvm-commits mailing list