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

Panagiotis Karouzakis via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 25 06:08:28 PDT 2025


================
@@ -76,6 +76,15 @@ void DemandedBits::determineLiveOperandBits(
           computeKnownBits(V2, Known2, DL, &AC, UserI, &DT);
         }
       };
+  auto GetShiftedRange = [&](unsigned Min, unsigned Max, bool ShiftLeft) {
+    using ShiftFn = APInt (APInt::*)(unsigned) const;
+    auto Shift = ShiftLeft ? static_cast<ShiftFn>(&APInt::shl)
+                           : static_cast<ShiftFn>(&APInt::lshr);
+    AB = APInt::getZero(BitWidth);
+    for (unsigned ShiftAmount = Min; ShiftAmount <= Max; ++ShiftAmount) {
----------------
karouzakisp wrote:

I think we can keep intermediate results to avoid doing one by one shifts and then doing the OR at the end of each, here is the alive proof for the shl case --> https://alive2.llvm.org/ce/z/SgUc78

this reduces the steps of the loop to log(max - min + 1), resulting to L log(max-min +1) time complexity.

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


More information about the llvm-commits mailing list