[llvm] eb42b67 - [InstCombine][InstSimplify] add tests for i1/i2 mul with no-wrap; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 18 07:17:17 PST 2023


Author: Sanjay Patel
Date: 2023-01-18T10:17:06-05:00
New Revision: eb42b67e8c8e37ef59d03897bcf118dd0cf5f394

URL: https://github.com/llvm/llvm-project/commit/eb42b67e8c8e37ef59d03897bcf118dd0cf5f394
DIFF: https://github.com/llvm/llvm-project/commit/eb42b67e8c8e37ef59d03897bcf118dd0cf5f394.diff

LOG: [InstCombine][InstSimplify] add tests for i1/i2 mul with no-wrap; NFC

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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 31a162f39e8dd..155c3afad2e31 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -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) {

diff  --git a/llvm/test/Transforms/InstSimplify/mul.ll b/llvm/test/Transforms/InstSimplify/mul.ll
index 068b61c574d30..902bde54841f6 100644
--- a/llvm/test/Transforms/InstSimplify/mul.ll
+++ b/llvm/test/Transforms/InstSimplify/mul.ll
@@ -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
+}


        


More information about the llvm-commits mailing list