[llvm] [InstCombine] Fold xored one-complemented operand comparisons (PR #69882)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 26 00:57:39 PDT 2023
================
@@ -3,6 +3,285 @@
declare void @llvm.assume(i1)
declare void @barrier()
+declare void @use.i8(i8)
+
+; test for (~x ^ y) < ~z
+define i1 @test_xor1(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor1(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %nz = xor i8 %z, -1
+ %r = icmp slt i8 %xor2, %nz
+ ret i1 %r
+}
+
+; test for ~z <= (x ^ ~y)
+define i1 @test_xor2(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor2(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[Y:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sle i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %y, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %x
+ %r = icmp sle i8 %nz, %xor2
+ ret i1 %r
+}
+
+; test for ~z > (~x ^ y)
+define i1 @test_xor3(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor3(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp sgt i8 %nz, %xor2
+ ret i1 %r
+}
+
+; tests for equality
+define i1 @test_xor_ne(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor_ne(
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %y, -1
+ %xor2 = xor i8 %xor, %x
+ %r = icmp ne i8 %nz, %xor2
+ ret i1 %r
+}
+
+define i1 @test_xor_eq(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor_eq(
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %y, -1
+ %xor2 = xor i8 %xor, %x
+ %r = icmp eq i8 %nz, %xor2
+ ret i1 %r
+}
+
+; other tests
+define i1 @test_xor4(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor4(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sle i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp sge i8 %xor2, %nz
+ ret i1 %r
+}
+
+define i1 @test_xor5(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor5(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp ult i8 %xor2, %nz
+ ret i1 %r
+}
+
+define i1 @test_xor6(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor6(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp uge i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp ule i8 %xor2, %nz
+ ret i1 %r
+}
+
+define i1 @test_xor7(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor7(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp ugt i8 %xor2, %nz
+ ret i1 %r
+}
+
+define i1 @test_xor8(i8 %x, i8 %y, i8 %z) {
+; CHECK-LABEL: @test_xor8(
+; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], -1
+; CHECK-NEXT: call void @use.i8(i8 [[XOR]])
+; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[TMP1]], [[Z:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %nz = xor i8 %z, -1
+ %xor = xor i8 %x, -1
+ call void @use.i8(i8 %xor)
+ %xor2 = xor i8 %xor, %y
+ %r = icmp uge i8 %xor2, %nz
+ ret i1 %r
+}
+
+; test (~a ^ b) < ~a
+define i1 @test_slt_xor(i32 %0, i32 %1) {
+; CHECK-LABEL: @test_slt_xor(
+; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP0:%.*]], [[TMP1:%.*]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[TMP3]], [[TMP0]]
+; CHECK-NEXT: ret i1 [[TMP4]]
+;
+ %3 = xor i32 %0, -1
+ %4 = xor i32 %3, %1
+ %5 = icmp slt i32 %4, %3
+ ret i1 %5
+}
----------------
nikic wrote:
```suggestion
define i1 @test_slt_xor(i32 %x, i32 %y) {
; CHECK-LABEL: @test_slt_xor(
; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP0:%.*]], [[TMP1:%.*]]
; CHECK-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[TMP3]], [[TMP0]]
; CHECK-NEXT: ret i1 [[TMP4]]
;
%xor = xor i32 %x, -1
%xor2 = xor i32 %xor, %y
%r = icmp slt i32 %xor2, %xor
ret i1 %r
}
```
Looks like the tests starting from here ended up using unnamed values.
https://github.com/llvm/llvm-project/pull/69882
More information about the llvm-commits
mailing list