[llvm] [InstCombine] simplify `x (comp) ~x` (PR #73990)

via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 00:54:23 PST 2023


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

>From 46777dc3b6b8a19e0f023455649469c20144efac Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Fri, 1 Dec 2023 06:28:23 +0900
Subject: [PATCH 1/2] [InstCombine] Add test for simplify `x (comp) ~x`

simplify compare between specific variable `X` and  `NOT(X)`

Proof: https://alive2.llvm.org/ce/z/89XAvd
---
 .../Transforms/InstCombine/icmp-of-xor-x.ll   | 143 ++++++++++++++++++
 1 file changed, 143 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
index ef4f2bfecfd8ed9..9919c59bc9fd286 100644
--- a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
@@ -5,6 +5,149 @@ declare void @llvm.assume(i1)
 declare void @barrier()
 declare void @use.i8(i8)
 
+; X s< ~X --> X s< 0
+define i1 @src_slt_xnx_slt_0(i8 %x) {
+; CHECK-LABEL: @src_slt_xnx_slt_0(
+; 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
+}
+; X s> ~X  -->  X s> -1
+define i1 @src_sgt_xnx_sgt_nega1(i8 %x) {
+; CHECK-LABEL: @src_sgt_xnx_sgt_nega1(
+; 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
+}
+; X (compare) ~X can never be equal.
+; X s<= ~X  -->  X s< ~X
+define i1 @src_sle_xnx_to_slt_0(i8 %x) {
+; CHECK-LABEL: @src_sle_xnx_to_slt_0(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp sle i8 %x, %not
+  ret i1 %cmp
+}
+; X s>= ~X  -->  X s> ~X
+define i1 @src_sge_xnx_to_sgt_nega1(i8 %x) {
+; CHECK-LABEL: @src_sge_xnx_to_sgt_nega1(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp sge i8 %x, %not
+  ret i1 %cmp
+}
+; slt or sgt can be converted to the other by swapping the true and false clauses
+; ~X s< X  -->  X s> ~X
+define i1 @src_slt_nxx_to_sgt_nega1(i8 %x) {
+; CHECK-LABEL: @src_slt_nxx_to_sgt_nega1(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp slt i8 %not, %x
+  ret i1 %cmp
+}
+; ~X s> X  -->  X s< ~X
+define i1 @src_sgt_nxx_to_slt_0(i8 %x) {
+; CHECK-LABEL: @src_sgt_nxx_to_slt_0(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp sgt i8 %not, %x
+  ret i1 %cmp
+}
+; X u< ~X  -->  X u> SIGNBIT_OF(X) --> X s> -1
+define i1 @src_ult_xnx_to_ugt_128(i8 %x) {
+; CHECK-LABEL: @src_ult_xnx_to_ugt_128(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ult i8 %x, %not
+  ret i1 %cmp
+}
+; X u> ~X  -->  X u< SIGNBIT_OF(X)
+define i1 @src_ugt_xnx_to_ult_128(i8 %x) {
+; CHECK-LABEL: @src_ugt_xnx_to_ult_128(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], -128
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ugt i8 %x, %not
+  ret i1 %cmp
+}
+; X (compare) ~X can never be equal.
+; X u<= ~X  -->  X u< ~X  -->  X u> SIGNBIT_OF(X) --> X s> -1
+define i1 @src_ule_xnx_to_ugt(i8 %x) {
+; CHECK-LABEL: @src_ule_xnx_to_ugt(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ule i8 %x, %not
+  ret i1 %cmp
+}
+; X u>= ~X  -->  X u> ~X --> X u< SIGNBIT_OF(X)
+define i1 @src_uge_xnx_to_ult(i8 %x) {
+; CHECK-LABEL: @src_uge_xnx_to_ult(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], -128
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp uge i8 %x, %not
+  ret i1 %cmp
+}
+; ult or ugt can be converted to the other by swapping the true and false clauses
+; ~X u< X  -->  X u> ~X -->  X u< SIGNBIT_OF(X)
+define i1 @src_ult_nxx_to_ult(i8 %x) {
+; CHECK-LABEL: @src_ult_nxx_to_ult(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], -128
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ult i8 %not, %x
+  ret i1 %cmp
+}
+; ~X u> X  -->  X u< ~X  -->  X u> SIGNBIT_OF(X)  --> X s> -1
+define i1 @src_ugt_nxx_to_ugt(i8 %x) {
+; CHECK-LABEL: @src_ugt_nxx_to_ugt(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ugt i8 %not, %x
+  ret i1 %cmp
+}
+; X == ~X  ->  false
+define i1 @src_eq_xnx_to_0(i8 %x) {
+; CHECK-LABEL: @src_eq_xnx_to_0(
+; CHECK-NEXT:    ret i1 false
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp eq i8 %x, %not
+  ret i1 %cmp
+}
+; X != ~X  ->  true
+define i1 @src_ne_xnx_to_1(i8 %x) {
+; CHECK-LABEL: @src_ne_xnx_to_1(
+; CHECK-NEXT:    ret i1 true
+;
+  %not = xor i8 %x, -1
+  %cmp = icmp ne i8 %x, %not
+  ret i1 %cmp
+}
+
 ; test for (~x ^ y) < ~z
 define i1 @test_xor1(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @test_xor1(

>From 5e5a7c44de560df95d7cbe77b02af949ff1f55d6 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Fri, 1 Dec 2023 06:32:35 +0900
Subject: [PATCH 2/2] [InstCombine] simplify `x (comp) ~x`

simplify compare between specific variable `X` and  `NOT(X)`

Proof: https://alive2.llvm.org/ce/z/89XAvd

Fixed #57532.
---
 .../InstCombine/InstCombineCompares.cpp       | 48 +++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9bc84c7dd6e1539..dc77fad6e3390a5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7035,6 +7035,54 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
       return new ICmpInst(I.getSwappedPredicate(Pred), Builder.CreateXor(X, Y),
                           Z);
 
+    // Transform X s<  ~X  -->   X s<  0
+    //           X s>  ~X  -->   X s>  -1
+    //           X s>= ~X  -->   X s>  -1
+    //           X s<= ~X  -->   X s<  0
+    //           X u<  ~X  -->   X u<  (SIGNBIT(X))
+    //           X u>  ~X  -->   X u>  (SIGNBIT(X))
+    //           X u<= ~X  -->   X u<  (SIGNBIT(X))
+    //           X u>= ~X  -->   X u>  (SIGNBIT(X))
+    //           X ==  ~X  -->   false
+    //           X !=  ~X  -->   true
+    if (match(&I, m_c_ICmp(Pred, m_Value(X), m_Not(m_Deferred(X))))) {
+      // if `~X (comp) X`, replace it with `X (Swapped-comp) ~X`.
+      // ex) ~X s< X  -->  X s> ~X
+      if (match(I.getOperand(0), m_Not(m_Specific(X))))
+        Pred = I.getSwappedPredicate();
+
+      CmpInst::Predicate PredEqRemoved;
+      Constant *Const;
+      APInt C;
+      switch (Pred) {
+      case ICmpInst::ICMP_EQ:
+        return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
+        break;
+      case ICmpInst::ICMP_NE:
+        return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
+        break;
+      case ICmpInst::ICMP_UGT:
+      case ICmpInst::ICMP_UGE:
+      case ICmpInst::ICMP_ULT:
+      case ICmpInst::ICMP_ULE:
+        PredEqRemoved = CmpInst::getStrictPredicate(Pred);
+        C = APInt::getSignMask(X->getType()->getScalarSizeInBits());
+        Const = ConstantInt::get(X->getType(), C);
+        break;
+      case ICmpInst::ICMP_SGT:
+      case ICmpInst::ICMP_SGE:
+      case ICmpInst::ICMP_SLT:
+      case ICmpInst::ICMP_SLE:
+        PredEqRemoved = CmpInst::getStrictPredicate(Pred);
+        Const = ConstantInt::get(X->getType(),
+                                 PredEqRemoved == ICmpInst::ICMP_SGT ? -1 : 0);
+        break;
+      default:
+        llvm_unreachable("not a valid predicate");
+      }
+      return new ICmpInst(PredEqRemoved, X, Const);
+    }
+
     // ~X < ~Y --> Y < X
     // ~X < C -->  X > ~C
     if (match(Op0, m_Not(m_Value(X)))) {



More information about the llvm-commits mailing list