[llvm] [GlobalISel] Add `G_FSHL` and `G_FSHR` to computeKnownBits (PR #191260)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 21 09:13:12 PDT 2026


================
@@ -603,6 +603,60 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
   return Known;
 }
 
+KnownBits KnownBits::fshl(const KnownBits &LHS, const KnownBits &RHS,
+                          const KnownBits &Amt) {
+  if (Amt.isConstant()) {
+    const APInt &ShAmt = Amt.getConstant();
+    return KnownBits(APIntOps::fshl(LHS.Zero, RHS.Zero, ShAmt),
+                     APIntOps::fshl(LHS.One, RHS.One, ShAmt));
+  }
+  unsigned BitWidth = LHS.getBitWidth();
+  unsigned MinAmt = Amt.getMinValue().getZExtValue();
+  unsigned MaxAmt = Amt.getMaxValue().getZExtValue();
+  unsigned ShiftAmtZeroMask = Amt.Zero.getZExtValue();
+  unsigned ShiftAmtOneMask = Amt.One.getZExtValue();
+  KnownBits Known(BitWidth);
+  Known.setAllConflict();
+
+  for (unsigned ShiftAmt = MinAmt; ShiftAmt <= MaxAmt; ++ShiftAmt) {
+    if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
+        (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
+      continue;
+    APInt SA = APInt(BitWidth, ShiftAmt % BitWidth);
+    KnownBits Tmp(APIntOps::fshl(LHS.Zero, RHS.Zero, SA),
+                  APIntOps::fshl(LHS.One, RHS.One, SA));
+    Known = Known.intersectWith(Tmp);
+  }
+  return Known;
+}
+
+KnownBits KnownBits::fshr(const KnownBits &LHS, const KnownBits &RHS,
+                          const KnownBits &Amt) {
+  if (Amt.isConstant()) {
+    const APInt &ShAmt = Amt.getConstant();
+    return KnownBits(APIntOps::fshr(LHS.Zero, RHS.Zero, ShAmt),
+                     APIntOps::fshr(LHS.One, RHS.One, ShAmt));
+  }
+  unsigned BitWidth = LHS.getBitWidth();
+  unsigned MinAmt = Amt.getMinValue().getZExtValue();
+  unsigned MaxAmt = Amt.getMaxValue().getZExtValue();
+  unsigned ShiftAmtZeroMask = Amt.Zero.getZExtValue();
+  unsigned ShiftAmtOneMask = Amt.One.getZExtValue();
+  KnownBits Known(BitWidth);
+  Known.setAllConflict();
+
+  for (unsigned ShiftAmt = MinAmt; ShiftAmt <= MaxAmt; ++ShiftAmt) {
+    if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
+        (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
----------------
def3r wrote:

The masks are `unsigned`, and this is consistent with `KnownBits::shl` and `KnownBits::lshr`

https://github.com/llvm/llvm-project/blob/980ddce138a367cc8c4a6586f8aac4c255424fe8/llvm/lib/Support/KnownBits.cpp#L470-L483

Should I instead use `APInt` for it?


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


More information about the llvm-commits mailing list