[llvm] [InstCombine] Fold out-of-range bits for squaring signed integers (PR #153484)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 20 05:36:43 PDT 2025
================
@@ -423,3 +423,35 @@ define i64 @smear_set_bit_different_dest_type_wider_dst(i32 %x) {
%s = sext i8 %a to i64
ret i64 %s
}
+
+; Test known bits for (sext i8 x) * (sext i8 x)
+
+define i1 @sext_square_bit30(i8 %x) {
+; CHECK-LABEL: @sext_square_bit30(
+; CHECK: ret i1 false
+ %sx = sext i8 %x to i32
+ %mul = mul nsw i32 %sx, %sx
+ %and = and i32 %mul, 1073741824 ; 1 << 30
+ %cmp = icmp ne i32 %and, 0
+ ret i1 %cmp
+}
+
+define i1 @sext_square_bit15(i8 %x) {
+; CHECK-LABEL: @sext_square_bit15(
+; CHECK: ret i1 false
+ %sx = sext i8 %x to i32
+ %mul = mul nsw i32 %sx, %sx
+ %and = and i32 %mul, 32768 ; 1 << 15
+ %cmp = icmp ne i32 %and, 0
+ ret i1 %cmp
+}
+
+define i1 @sext_square_bit14(i8 %x) {
+; CHECK-LABEL: @sext_square_bit14(
+; CHECK-NOT: ret i1 false
+ %sx = sext i8 %x to i32
+ %mul = mul nsw i32 %sx, %sx
+ %and = and i32 %mul, 16384 ; 1 << 14
+ %cmp = icmp ne i32 %and, 0
+ ret i1 %cmp
+}
----------------
nikic wrote:
Though I don't really get why this case is folding. We should have TyBits = 32, SignBits = 9, OutValidBits = 16, OutSignBits = 17. So I would not expect bit 1<<14 to be set to zero.
https://github.com/llvm/llvm-project/pull/153484
More information about the llvm-commits
mailing list