[llvm] [InstCombine] Simplify compare abs(X) and X. (PR #76385)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 25 23:52:09 PST 2023
https://github.com/Z572 created https://github.com/llvm/llvm-project/pull/76385
fix https://github.com/llvm/llvm-project/issues/72653
proof: https://alive2.llvm.org/ce/z/zPNyUb
>From 5f80b2732b69e7fa4411caac62838c3f314c44d1 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] [InstCombine] Simplify compare abs(X) and X.
proof: https://alive2.llvm.org/ce/z/zPNyUb
---
.../InstCombine/InstCombineCompares.cpp | 41 ++++
llvm/test/Transforms/InstCombine/icmp-abs.ll | 212 ++++++++++++++++++
2 files changed, 253 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 289976718e52f3..19ddffe75ff9b6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7109,6 +7109,47 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
}
}
+ {
+ Value *X;
+ Constant *C;
+ if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X),
+ m_Constant(C)))) &&
+ match(Op1, m_Specific(X))) {
+ auto *NullValue = Constant::getNullValue(X->getType());
+ auto *MinVal = ConstantInt::get(
+ X->getType(),
+ APInt::getSignedMinValue(X->getType()->getScalarSizeInBits()));
+ bool IsIntMinPosion = C->isZeroValue();
+ switch (Pred) {
+ case CmpInst::ICMP_ULE:
+ case CmpInst::ICMP_SGE:
+ return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
+ case CmpInst::ICMP_UGT:
+ case CmpInst::ICMP_SLT:
+ return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
+ case CmpInst::ICMP_UGE:
+ case CmpInst::ICMP_SLE:
+ case CmpInst::ICMP_EQ: {
+ auto *SGE = Builder.CreateICmpSGE(X, NullValue);
+ return replaceInstUsesWith(
+ I, IsIntMinPosion
+ ? Builder.CreateOr(SGE, Builder.CreateICmpEQ(X, MinVal))
+ : SGE);
+ }
+ case CmpInst::ICMP_ULT:
+ case CmpInst::ICMP_NE:
+ case CmpInst::ICMP_SGT: {
+ auto *SLT = Builder.CreateICmpSLT(X, NullValue);
+ return replaceInstUsesWith(
+ I, IsIntMinPosion
+ ? Builder.CreateAnd(SLT, Builder.CreateICmpNE(X, MinVal))
+ : SLT);
+ }
+ default:
+ break;
+ }
+ }
+ }
if (Instruction *Res = foldICmpEquality(I))
return Res;
diff --git a/llvm/test/Transforms/InstCombine/icmp-abs.ll b/llvm/test/Transforms/InstCombine/icmp-abs.ll
new file mode 100644
index 00000000000000..382dab800dd942
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-abs.ll
@@ -0,0 +1,212 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-p1:16:16:16-p2:32:32:32-p3:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+
+declare i4 @llvm.abs.i4(i4, i1)
+declare void @use_i4(i4)
+
+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_sgt_abs_use(i4 %arg) {
+; CHECK-LABEL: @icmp_sgt_abs_use(
+; CHECK-NEXT: [[ABS:%.*]] = call i4 @llvm.abs.i4(i4 [[ARG:%.*]], i1 false)
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i4 [[ABS]], [[ARG]]
+; CHECK-NEXT: call void @use_i4(i4 [[ABS]])
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = call i4 @llvm.abs.i4(i4 %arg, i1 false)
+ %cmp = icmp sgt i4 %abs, %arg
+ call void @use_i4(i4 %abs)
+ ret i1 %cmp
+}
More information about the llvm-commits
mailing list