[llvm] [InstCombine] Simplify compare abs(X) and X. (PR #76385)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 4 20:59:07 PST 2024


https://github.com/Z572 updated https://github.com/llvm/llvm-project/pull/76385

>From 26debb25165b43df7a21a4802a751ae406af3e23 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Mon, 25 Dec 2023 23:47:31 +0800
Subject: [PATCH 1/2] [InstCombine] Simplify compare abs(X) and X.

proof: https://alive2.llvm.org/ce/z/LZzZaj
---
 .../InstCombine/InstCombineCompares.cpp       |  48 ++++
 llvm/test/Transforms/InstCombine/icmp-abs.ll  | 208 ++++++++++++++++++
 2 files changed, 256 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/icmp-abs.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 3875e59c3ede3b..c0f87c7e509fb7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6860,6 +6860,54 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
       return foldICmpAddOpConst(X, *C, Pred);
   }
 
+  // abs(X) >=  X --> true
+  // abs(X) u<= X --> true
+  // abs(X) <   X --> false
+  // abs(X) u>  X --> false
+  // abs(X) u>= X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
+  // abs(X) <=  X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
+  // abs(X) ==  X --> IsIntMinPosion ? `X > -1`: `X u<= INTMIN`
+  // abs(X) u<  X --> IsIntMinPosion ? `X < 0` : `X >   INTMIN`
+  // abs(X) >   X --> IsIntMinPosion ? `X < 0` : `X >   INTMIN`
+  // abs(X) !=  X --> IsIntMinPosion ? `X < 0` : `X >   INTMIN`
+  {
+    Value *X;
+    Constant *C;
+    if ((match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
+         match(Op1, m_Specific(X)))) {
+      Value *NullValue = Constant::getNullValue(X->getType());
+      Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
+      Value *MinVal = ConstantInt::get(
+          X->getType(),
+          APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
+      bool IsIntMinPosion = C->isAllOnesValue();
+      switch (Pred) {
+      case CmpInst::ICMP_ULE:
+      case CmpInst::ICMP_SGE:
+        return replaceInstUsesWith(CxtI, ConstantInt::getTrue(CxtI.getType()));
+      case CmpInst::ICMP_UGT:
+      case CmpInst::ICMP_SLT:
+        return replaceInstUsesWith(CxtI, ConstantInt::getFalse(CxtI.getType()));
+      case CmpInst::ICMP_UGE:
+      case CmpInst::ICMP_SLE:
+      case CmpInst::ICMP_EQ: {
+        return replaceInstUsesWith(
+            CxtI, IsIntMinPosion ? Builder.CreateICmpSGT(X, AllOnesValue)
+                                 : Builder.CreateICmpULE(X, MinVal));
+      }
+      case CmpInst::ICMP_ULT:
+      case CmpInst::ICMP_SGT:
+      case CmpInst::ICMP_NE: {
+        return replaceInstUsesWith(
+            CxtI, IsIntMinPosion ? Builder.CreateICmpSLT(X, NullValue)
+                                 : Builder.CreateICmpUGT(X, MinVal));
+      }
+      default:
+        llvm_unreachable("Invalid predicate!");
+      }
+    }
+  }
+
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/icmp-abs.ll b/llvm/test/Transforms/InstCombine/icmp-abs.ll
new file mode 100644
index 00000000000000..497c965a6a90fb
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-abs.ll
@@ -0,0 +1,208 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare i4 @llvm.abs.i4(i4, i1)
+
+define i1 @icmp_sge_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_sge_abs(
+; CHECK-NEXT:    ret i1 true
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp sge i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sge_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_sge_abs_false(
+; CHECK-NEXT:    ret i1 true
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp sge i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_eq_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_eq_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i4 [[ARG:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp eq i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_eq_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_eq_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i4 [[ARG:%.*]], -7
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp eq i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ne_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_ne_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i4 [[ARG:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp ne i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ne_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_ne_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i4 [[ARG:%.*]], -8
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp ne i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sle_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_sle_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i4 [[ARG:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp sle i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sle_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_sle_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i4 [[ARG:%.*]], -7
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp sle i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_slt_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_slt_abs(
+; CHECK-NEXT:    ret i1 false
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp slt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_slt_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_slt_abs_false(
+; CHECK-NEXT:    ret i1 false
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp slt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sgt_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_sgt_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i4 [[ARG:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp sgt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sgt_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_sgt_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i4 [[ARG:%.*]], -8
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp sgt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ugt_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_ugt_abs(
+; CHECK-NEXT:    ret i1 false
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp ugt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ugt_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_ugt_abs_false(
+; CHECK-NEXT:    ret i1 false
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp ugt i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_uge_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_uge_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i4 [[ARG:%.*]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp uge i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_uge_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_uge_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i4 [[ARG:%.*]], -7
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp uge i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ule_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_ule_abs(
+; CHECK-NEXT:    ret i1 true
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp ule i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ule_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_ule_abs_false(
+; CHECK-NEXT:    ret i1 true
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp ule i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ult_abs(i4 %arg) {
+; CHECK-LABEL: @icmp_ult_abs(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i4 [[ARG:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp ult i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_ult_abs_false(i4 %arg) {
+; CHECK-LABEL: @icmp_ult_abs_false(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i4 [[ARG:%.*]], -8
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+  %cmp = icmp ult i4 %abs, %arg
+  ret i1 %cmp
+}
+
+define i1 @icmp_sge_abs2(i4 %arg) {
+; CHECK-LABEL: @icmp_sge_abs2(
+; CHECK-NEXT:    [[X:%.*]] = mul i4 [[ARG:%.*]], [[ARG]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i4 [[X]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %x = mul i4 %arg, %arg ; thwart complexity-based canonicalization
+  %abs = call i4 @llvm.abs.i4(i4 %x, i1 true)
+  %cmp = icmp sge i4 %x, %abs
+  ret i1 %cmp
+}

>From 229ed581407859e49b407b3c655567f823572a44 Mon Sep 17 00:00:00 2001
From: Zheng Junjie <zhengjunjie at iscas.ac.cn>
Date: Fri, 5 Jan 2024 12:51:30 +0800
Subject: [PATCH 2/2] fixup! [InstCombine] Simplify compare abs(X) and X.

---
 .../InstCombine/InstCombineCompares.cpp       | 21 +++++++++++--------
 llvm/test/Transforms/InstCombine/icmp-abs.ll  | 11 ++++++++++
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index c0f87c7e509fb7..c5c6b5eb533fe7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6873,13 +6873,12 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
   {
     Value *X;
     Constant *C;
-    if ((match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
-         match(Op1, m_Specific(X)))) {
+    if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X), m_Constant(C))) &&
+        match(Op1, m_Specific(X))) {
       Value *NullValue = Constant::getNullValue(X->getType());
       Value *AllOnesValue = Constant::getAllOnesValue(X->getType());
-      Value *MinVal = ConstantInt::get(
-          X->getType(),
-          APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
+      const APInt SMin =
+          APInt::getSignedMinValue(X->getType()->getScalarSizeInBits());
       bool IsIntMinPosion = C->isAllOnesValue();
       switch (Pred) {
       case CmpInst::ICMP_ULE:
@@ -6892,15 +6891,19 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
       case CmpInst::ICMP_SLE:
       case CmpInst::ICMP_EQ: {
         return replaceInstUsesWith(
-            CxtI, IsIntMinPosion ? Builder.CreateICmpSGT(X, AllOnesValue)
-                                 : Builder.CreateICmpULE(X, MinVal));
+            CxtI, IsIntMinPosion
+                      ? Builder.CreateICmpSGT(X, AllOnesValue)
+                      : Builder.CreateICmpULT(
+                            X, ConstantInt::get(X->getType(), SMin + 1)));
       }
       case CmpInst::ICMP_ULT:
       case CmpInst::ICMP_SGT:
       case CmpInst::ICMP_NE: {
         return replaceInstUsesWith(
-            CxtI, IsIntMinPosion ? Builder.CreateICmpSLT(X, NullValue)
-                                 : Builder.CreateICmpUGT(X, MinVal));
+            CxtI, IsIntMinPosion
+                      ? Builder.CreateICmpSLT(X, NullValue)
+                      : Builder.CreateICmpUGT(
+                            X, ConstantInt::get(X->getType(), SMin)));
       }
       default:
         llvm_unreachable("Invalid predicate!");
diff --git a/llvm/test/Transforms/InstCombine/icmp-abs.ll b/llvm/test/Transforms/InstCombine/icmp-abs.ll
index 497c965a6a90fb..f608fb1b914d1c 100644
--- a/llvm/test/Transforms/InstCombine/icmp-abs.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-abs.ll
@@ -206,3 +206,14 @@ define i1 @icmp_sge_abs2(i4 %arg) {
   %cmp = icmp sge i4 %x, %abs
   ret i1 %cmp
 }
+
+define i1 @icmp_sge_abs_mismatched_op(i4 %arg, i4 %arg2) {
+; CHECK-LABEL: @icmp_sge_abs_mismatched_op(
+; CHECK-NEXT:    [[ABS:%.*]] = call i4 @llvm.abs.i4(i4 [[ARG:%.*]], i1 true)
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sge i4 [[ABS]], [[ARG2:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %abs = call i4 @llvm.abs.i4(i4 %arg, i1 true)
+  %cmp = icmp sge i4 %abs, %arg2
+  ret i1 %cmp
+  }



More information about the llvm-commits mailing list