[InstCombine][InstSimplify] add tests for i1/i2 mul with no-wrap; NFC
authorSanjay Patel <spatel@rotateright.com>
Wed, 18 Jan 2023 15:03:36 +0000 (10:03 -0500)
committerSanjay Patel <spatel@rotateright.com>
Wed, 18 Jan 2023 15:17:06 +0000 (10:17 -0500)
A bug was introduced with 68c197f07eeae71 as noted in the
post-commit review comments, and there are potentially
missed smaller transforms/simplifications because no-wrap
multiply with only 1 or 2 bits eliminates some potential
results.

llvm/test/Transforms/InstCombine/sub.ll
llvm/test/Transforms/InstSimplify/mul.ll

index 31a162f..155c3af 100644 (file)
@@ -2465,6 +2465,60 @@ define <2 x i5> @diff_of_squares_partial_nsw(<2 x i5> %x, <2 x i5> %y) {
   ret <2 x i5> %r
 }
 
+; TODO: This should simplify more.
+
+define i1 @diff_of_squares_nsw_i1(i1 %x, i1 %y) {
+; CHECK-LABEL: @diff_of_squares_nsw_i1(
+; CHECK-NEXT:    [[R:%.*]] = xor i1 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %x2 = mul nsw i1 %x, %x
+  %y2 = mul nsw i1 %y, %y
+  %r = sub nsw i1 %x2, %y2
+  ret i1 %r
+}
+
+define i1 @diff_of_squares_nuw_i1(i1 %x, i1 %y) {
+; CHECK-LABEL: @diff_of_squares_nuw_i1(
+; CHECK-NEXT:    [[R:%.*]] = xor i1 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %x2 = mul nuw i1 %x, %x
+  %y2 = mul nuw i1 %y, %y
+  %r = sub nuw i1 %x2, %y2
+  ret i1 %r
+}
+
+; FIXME: It is not correct to propagate nsw.
+
+define i2 @diff_of_squares_nsw_i2(i2 %x, i2 %y) {
+; CHECK-LABEL: @diff_of_squares_nsw_i2(
+; CHECK-NEXT:    [[ADD:%.*]] = add nsw i2 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i2 [[X]], [[Y]]
+; CHECK-NEXT:    [[R:%.*]] = mul nsw i2 [[ADD]], [[SUB]]
+; CHECK-NEXT:    ret i2 [[R]]
+;
+  %x2 = mul nsw i2 %x, %x
+  %y2 = mul nsw i2 %y, %y
+  %r = sub nsw i2 %x2, %y2
+  ret i2 %r
+}
+
+; TODO: This should reduce more.
+
+define i2 @diff_of_squares_nuw_i2(i2 %x, i2 %y) {
+; CHECK-LABEL: @diff_of_squares_nuw_i2(
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw i2 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub nuw i2 [[X]], [[Y]]
+; CHECK-NEXT:    [[R:%.*]] = mul nuw i2 [[ADD]], [[SUB]]
+; CHECK-NEXT:    ret i2 [[R]]
+;
+  %x2 = mul nuw i2 %x, %x
+  %y2 = mul nuw i2 %y, %y
+  %r = sub nuw i2 %x2, %y2
+  ret i2 %r
+}
+
 ; negative test
 
 define i8 @diff_of_squares_use1(i8 %x, i8 %y) {
index 068b61c..902bde5 100644 (file)
@@ -49,3 +49,27 @@ define i32 @poison(i32 %x) {
   %v = mul i32 %x, poison
   ret i32 %v
 }
+
+define i1 @square_i1(i1 %x) {
+; CHECK-LABEL: @square_i1(
+; CHECK-NEXT:    ret i1 [[X:%.*]]
+;
+  %r = mul i1 %x, %x
+  ret i1 %x
+}
+
+define i1 @square_i1_nsw(i1 %x) {
+; CHECK-LABEL: @square_i1_nsw(
+; CHECK-NEXT:    ret i1 [[X:%.*]]
+;
+  %r = mul nsw i1 %x, %x
+  ret i1 %x
+}
+
+define i1 @square_i1_nuw(i1 %x) {
+; CHECK-LABEL: @square_i1_nuw(
+; CHECK-NEXT:    ret i1 [[X:%.*]]
+;
+  %r = mul nuw i1 %x, %x
+  ret i1 %x
+}