[llvm] [KnownBits][SelectionDAG] Add KnownBits::clmul. Support trailing bits. NFC (PR #177517)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 09:25:18 PST 2026


================
@@ -602,6 +602,41 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
   return Known;
 }
 
+KnownBits KnownBits::clmul(const KnownBits &LHS, const KnownBits &RHS) {
+  unsigned BitWidth = LHS.getBitWidth();
+
+  // An m*n result will always fit in m+n-1 bits since there are no carries.
+  unsigned ActiveBits = std::min(
+      BitWidth, LHS.countMaxActiveBits() + RHS.countMaxActiveBits() - 1);
+
+  // The result of the bottom bits of a clmul can be inferred by looking at the
+  // bottom bits of both operands and carryless multiplying them together. The
+  // number of bits we can determine follows the same logic as KnownBits::mul.
+  const APInt &Bottom0 = LHS.One;
+  const APInt &Bottom1 = RHS.One;
+
+  unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countr_one();
+  unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countr_one();
+  unsigned TrailZero0 = LHS.countMinTrailingZeros();
+  unsigned TrailZero1 = RHS.countMinTrailingZeros();
+  unsigned TrailZ = TrailZero0 + TrailZero1;
+
+  // Figure out the fewest known-bits operand.
+  unsigned SmallestOperand =
+      std::min(TrailBitsKnown0 - TrailZero0, TrailBitsKnown1 - TrailZero1);
+  unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
+
+  APInt BottomKnown = APIntOps::clmul(Bottom0.getLoBits(TrailBitsKnown0),
+                                      Bottom1.getLoBits(TrailBitsKnown1));
----------------
jayfoad wrote:

Yes but not necessarily in this PR.

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


More information about the llvm-commits mailing list