[llvm] [InstCombine] Simplifiy `sdiv -X, X` into `X == INT_MIN ? 1 : -1` (PR #71768)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 15 01:40:23 PST 2023


================
@@ -1432,6 +1432,61 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
   ret <2 x i8> %r
 }
 
+define i32 @sdiv_sub1(i32 %arg) {
+; CHECK-LABEL: @sdiv_sub1(
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
+; CHECK-NEXT:    [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
+; CHECK-NEXT:    ret i32 [[DIV]]
+;
+  %neg = sub i32 0, %arg
+  %div = sdiv i32 %neg, %arg
+  ret i32 %div
+}
+
+define i32 @sdiv_sub2(i32 %arg) {
+; CHECK-LABEL: @sdiv_sub2(
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
+; CHECK-NEXT:    [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
+; CHECK-NEXT:    ret i32 [[DIV]]
+;
+  %neg = sub i32 0, %arg
+  %div = sdiv i32 %arg, %neg
+  ret i32 %div
+}
----------------
nikic wrote:

Please also add a multi-use test (extra use of `%neg`). As a div is removed, we want to allow the transform with multi-use.

Please also test the case `(x - y) / (y - x)`, which is one of the special cases isKnownNegation() handles.

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


More information about the llvm-commits mailing list