[llvm] Simplify `(a % b) lt/ge (b-1)` into `(a % b) eq/ne (b-1)` (PR #72504)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 13 00:55:00 PST 2024


================
@@ -10,6 +10,307 @@ declare void @use_i8(i8)
 declare void @use_i32(i32)
 declare void @use_i64(i64)
 
+; tests for (x % c) >=/ < (c - 1), where c >= 0
+define i1 @srem_sgt_test1(i64 %x) {
+; CHECK-LABEL: @srem_sgt_test1(
+; CHECK-NEXT:    [[Y:%.*]] = srem i64 [[X:%.*]], 34360750831
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[Y]], 34360750830
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i64 %x, 34360750831
+  %cmp = icmp sgt i64 %y, 34360750829
+  ret i1 %cmp
+}
+
+define i1 @srem_slt_test1(i64 %x) {
+; CHECK-LABEL: @srem_slt_test1(
+; CHECK-NEXT:    [[Y:%.*]] = srem i64 [[X:%.*]], 34360750831
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i64 [[Y]], 34360750830
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i64 %x, 34360750831
+  %cmp = icmp slt i64 %y, 34360750830
+  ret i1 %cmp
+}
+
+define i1 @srem_sgt_test2(i32 %x) {
+; CHECK-LABEL: @srem_sgt_test2(
+; CHECK-NEXT:    [[Y:%.*]] = srem i32 [[X:%.*]], 1074977277
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[Y]], 1074977276
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i32 %x, 1074977277
+  %cmp = icmp sgt i32 %y, 1074977275
+  ret i1 %cmp
+}
+
+define i1 @srem_slt_test2(i32 %x) {
+; CHECK-LABEL: @srem_slt_test2(
+; CHECK-NEXT:    [[Y:%.*]] = srem i32 [[X:%.*]], 1074977277
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[Y]], 1074977276
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i32 %x, 1074977277
+  %cmp = icmp slt i32 %y, 1074977276
+  ret i1 %cmp
+}
+
+define i1 @srem_sgt_test3(i16 %x) {
+; CHECK-LABEL: @srem_sgt_test3(
+; CHECK-NEXT:    [[Y:%.*]] = srem i16 [[X:%.*]], 2259
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 [[Y]], 2258
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i16 %x, 2259
+  %cmp = icmp sgt i16 %y, 2257
+  ret i1 %cmp
+}
+
+define i1 @srem_slt_test3(i16 %x) {
+; CHECK-LABEL: @srem_slt_test3(
+; CHECK-NEXT:    [[Y:%.*]] = srem i16 [[X:%.*]], 2259
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i16 [[Y]], 2258
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i16 %x, 2259
+  %cmp = icmp slt i16 %y, 2258
+  ret i1 %cmp
+}
+
+define i1 @srem_sgt_test4(i8 %x) {
+; CHECK-LABEL: @srem_sgt_test4(
+; CHECK-NEXT:    ret i1 true
+;
+  %y = srem i8 %x, 133
+  %cmp = icmp sgt i8 %y, 131
+  ret i1 %cmp
+}
+
+define i1 @srem_slt_test4(i8 %x) {
+; CHECK-LABEL: @srem_slt_test4(
+; CHECK-NEXT:    ret i1 false
+;
+  %y = srem i8 %x, 133
+  %cmp = icmp slt i8 %y, 132
+  ret i1 %cmp
+}
+
+define i1 @test_srem_slt_constant(i32 %a) {
+; CHECK-LABEL: @test_srem_slt_constant(
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -2147483137
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i32 [[TMP1]], 511
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i32 %a, 512
+  %cmp = icmp slt i32 %y, 511
+  ret i1 %cmp
+}
+
+define i1 @test_srem_sgt_constant(i32 %a) {
+; CHECK-LABEL: @test_srem_sgt_constant(
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -2147483137
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[TMP1]], 511
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %y = srem i32 %a, 512
+  %cmp = icmp sgt i32 %y, 510
+  ret i1 %cmp
+}
+
+define <2 x i1> @test_srem_slt_constant_splat(<2 x i32> %a) {
+; CHECK-LABEL: @test_srem_slt_constant_splat(
+; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], <i32 -2147483137, i32 -2147483137>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i32> [[TMP1]], <i32 511, i32 511>
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %y = srem <2 x i32> %a, <i32 512, i32 512>
+  %cmp = icmp slt <2 x i32> %y, <i32 511, i32 511>
+  ret <2 x i1> %cmp
+}
+
+define <2 x i1> @test_srem_sgt_constant_splat(<2 x i32> %a) {
+; CHECK-LABEL: @test_srem_sgt_constant_splat(
+; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], <i32 -2147483137, i32 -2147483137>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[TMP1]], <i32 511, i32 511>
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %y = srem <2 x i32> %a, <i32 512, i32 512>
+  %cmp = icmp sgt <2 x i32> %y, <i32 510, i32 510>
+  ret <2 x i1> %cmp
+}
+
+; tests for (x % c) <=/> (c + 1), where y < 0
+define i1 @srem_sgt_test(i16 %x) {
+; CHECK-LABEL: @srem_sgt_test(
+; CHECK-NEXT:    [[Y:%.*]] = srem i16 [[X:%.*]], 2259
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i16 [[Y]], -2258
----------------
nikic wrote:

You can see it in this test. The sign of the RHS of the srem has been flipped, and the fold does not apply, even though it was supposed to.

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


More information about the llvm-commits mailing list