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

Panagiotis Karouzakis via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 10 09:20:44 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) {
----------------
karouzakisp wrote:

@dtcxzyw The loop is optimized.
for a 128 bit value we only need  2 * 8 + 1 = 17 shifts and 17 ors. 
KnownBits::shl/lshr/ashr does an intersection; we don't do an intersection, we do a union. I am not certain that we can employ the same approach as they do since we use a different loop. In their loop, they increment by 1
`  for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;           
       ++ShiftAmt) {                                                             `
       
Instead, we increment by the next power of two
`    for (unsigned ShiftAmnt = 1; ShiftAmnt <= LoopRange; ShiftAmnt <<= 1) {`




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


More information about the llvm-commits mailing list