[llvm] [KnownBits] Fix add() SelfAdd assertion for bitwidths >= 512 (PR #202769)

Paweł Bylica via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 10 02:34:51 PDT 2026


================
@@ -363,10 +363,10 @@ struct KnownBits {
                        bool SelfAdd = false) {
     // ADD(X,X) is equivalent to SHL(X,1), the low bit is always zero.
     if (SelfAdd) {
-      // Shift amount bitwidth is independent of src bitwidth (and we're
-      // just shifting by one so don't have any bounds issues).
+      // The shift amount must be wide enough that shl's getMaxShiftAmount can
+      // extract Log2_32(BitWidth) bits from it, so match the source bitwidth.
       assert(LHS == RHS && "Expected matching knownbits");
-      KnownBits Amt = KnownBits::makeConstant(APInt(8, 1));
+      KnownBits Amt = KnownBits::makeConstant(APInt(LHS.getBitWidth(), 1));
----------------
chfast wrote:

Or alternatively we can use `Log2_32(BitWidth)` or `max(Log2_32(BitWidth), 8)` not to create big APInt.

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


More information about the llvm-commits mailing list