[llvm] 86ef039 - [InstCombine] Simplify compare abs(X) and X. (#76385)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 5 01:08:53 PST 2024
Author: Z572
Date: 2024-01-05T17:08:49+08:00
New Revision: 86ef039220de4b7d2e2f1d5e93874ae7a242730e
URL: https://github.com/llvm/llvm-project/commit/86ef039220de4b7d2e2f1d5e93874ae7a242730e
DIFF: https://github.com/llvm/llvm-project/commit/86ef039220de4b7d2e2f1d5e93874ae7a242730e.diff
LOG: [InstCombine] Simplify compare abs(X) and X. (#76385)
fix https://github.com/llvm/llvm-project/issues/72653
proof: https://alive2.llvm.org/ce/z/LZzZaj
Added:
llvm/test/Transforms/InstCombine/icmp-abs.ll
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 2dc0b0b87f60a7..a2ff8f3eef8199 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6868,6 +6868,57 @@ 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());
+ const APInt SMin =
+ 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.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, ConstantInt::get(X->getType(), SMin)));
+ }
+ 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..f608fb1b914d1c
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-abs.ll
@@ -0,0 +1,219 @@
+; 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
+}
+
+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