[llvm] [InstCombine] simplify `icmp pred x, ~x` (PR #73990)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 03:04:47 PDT 2024


================
@@ -5,6 +5,521 @@ declare void @llvm.assume(i1)
 declare void @barrier()
 declare void @use.i8(i8)
 
+; X op ~X tests. op signed
+; X s< ~X   -->      X s< 0
+define i1 @src_slt(i8 %x) {
+; CHECK-LABEL: @src_slt(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp slt i8 %x, %not
+  ret i1 %cmp
+}
+define i1 @src_slt_i128(i128 %x) {
+; CHECK-LABEL: @src_slt_i128(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i128 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i128 %x, -1
+  %cmp = icmp slt i128 %x, %not
+  ret i1 %cmp
+}
+; X s> ~X   -->     X s> -1
+define i1 @src_sgt(i8 %x) {
+; CHECK-LABEL: @src_sgt(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp sgt i8 %x, %not
+  ret i1 %cmp
+}
+define i1 @src_sgt_i128(i128 %x) {
+; CHECK-LABEL: @src_sgt_i128(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i128 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i128 %x, -1
+  %cmp = icmp sgt i128 %x, %not
+  ret i1 %cmp
+}
+; X s<= ~X   -->      X s< 0
+define i1 @src_sle(i8 %x) {
----------------
nikic wrote:

This comment comment is not done. The tests still don't properly check the commuted case -- and it looks like the implementation doesn't handle the commuted case either. It would be probably be better to move the code inside foldICmpXorXX(), because that already has handling for the commutation.

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


More information about the llvm-commits mailing list