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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 10 04:02:16 PDT 2026


================
@@ -603,6 +603,34 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
   return Known;
 }
 
+KnownBits KnownBits::fshl(KnownBits LHS, KnownBits RHS, KnownBits Amt) {
+  // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
+  unsigned BitWidth = LHS.getBitWidth();
+  assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
+  if (!Amt.isConstant())
+    return KnownBits(BitWidth);
+  auto ShAmt = Amt.getConstant().urem(BitWidth);
+  if (ShAmt == 0)
+    return LHS;
+  LHS <<= ShAmt;
+  RHS >>= BitWidth - ShAmt;
+  return LHS.unionWith(RHS);
+}
+
+KnownBits KnownBits::fshr(KnownBits LHS, KnownBits RHS, KnownBits Amt) {
+  // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
+  unsigned BitWidth = LHS.getBitWidth();
+  assert(BitWidth == RHS.getBitWidth() && "Operand mismatch");
+  if (!Amt.isConstant())
+    return KnownBits(BitWidth);
+  auto ShAmt = Amt.getConstant().urem(BitWidth);
+  if (ShAmt == 0)
+    return RHS;
+  LHS <<= BitWidth - ShAmt;
+  RHS >>= ShAmt;
+  return LHS.unionWith(RHS);
----------------
jayfoad wrote:

This is OK, but it might be neater to make use of the APInt helpers like:
```suggestion
  return KnownBits(APIntOps::fshr(LHS.Zero, RHS.Zero, ShAmt), APIntOps::fshr(LHS.One, RHS.One, ShAmt));
```

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


More information about the llvm-commits mailing list