[llvm] Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)
Iris Shi via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 30 06:47:32 PDT 2025
https://github.com/el-ev updated https://github.com/llvm/llvm-project/pull/136467
>From f4bf37bf1ffd5f1d1a8142d39d4ea92ad52fbcdf Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Fri, 18 Apr 2025 18:36:48 +0800
Subject: [PATCH 1/3] [ConstraintElim] Fix poison check before adding intrinsic
facts
---
.../Scalar/ConstraintElimination.cpp | 4 +-
.../Transforms/ConstraintElimination/abs.ll | 146 +++++++++++++-----
.../ConstraintElimination/uadd-usub-sat.ll | 65 ++++++++
3 files changed, 178 insertions(+), 37 deletions(-)
create mode 100644 llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 58d705eb6aa96..8684420b73671 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1137,10 +1137,10 @@ void State::addInfoFor(BasicBlock &BB) {
FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
// TODO: Check if it is possible to instead only added the min/max facts
// when simplifying uses of the min/max intrinsics.
- if (!isGuaranteedNotToBePoison(&I))
- break;
[[fallthrough]];
case Intrinsic::abs:
+ if (!isGuaranteedNotToBePoison(&I))
+ break;
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
break;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/abs.ll b/llvm/test/Transforms/ConstraintElimination/abs.ll
index 9fc68b0e72663..8cbdf39102700 100644
--- a/llvm/test/Transforms/ConstraintElimination/abs.ll
+++ b/llvm/test/Transforms/ConstraintElimination/abs.ll
@@ -1,9 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
-define i1 @abs_int_min_is_not_poison(i32 %arg) {
+define i1 @abs_int_min_is_not_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_int_min_is_not_poison(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: ret i1 true
;
@@ -12,49 +12,93 @@ define i1 @abs_int_min_is_not_poison(i32 %arg) {
ret i1 %cmp
}
-define i1 @abs_int_min_is_poison(i32 %arg) {
+define i1 @abs_int_min_is_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_int_min_is_poison(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], [[ARG]]
+; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%cmp = icmp sge i32 %abs, %arg
ret i1 %cmp
}
-define i1 @abs_plus_one(i32 %arg) {
-; CHECK-LABEL: define i1 @abs_plus_one(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
-; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+define i1 @abs_plus_one_min_is_not_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_plus_one_min_is_not_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nsw i32 [[ABS]], 1
; CHECK-NEXT: ret i1 true
;
- %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
%abs_plus_one = add nsw i32 %abs, 1
%cmp = icmp sge i32 %abs_plus_one, %arg
ret i1 %cmp
}
-define i1 @arg_minus_one_strict_less(i32 %arg) {
-; CHECK-LABEL: define i1 @arg_minus_one_strict_less(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+define i1 @abs_plus_one_min_is_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_plus_one_min_is_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nsw i32 [[ABS]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS_PLUS_ONE]], [[ARG]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %abs_plus_one = add nsw i32 %abs, 1
+ %cmp = icmp sge i32 %abs_plus_one, %arg
+ ret i1 %cmp
+}
+
+define i1 @arg_minus_one_strict_less_min_is_not_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @arg_minus_one_strict_less_min_is_not_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
; CHECK-NEXT: ret i1 true
;
- %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
%arg_minus_one = add nsw i32 %arg, -1
%cmp = icmp slt i32 %arg_minus_one, %abs
ret i1 %cmp
}
-define i1 @arg_minus_one_strict_greater(i32 %arg) {
-; CHECK-LABEL: define i1 @arg_minus_one_strict_greater(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+
+define i1 @arg_minus_one_strict_less_min_is_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @arg_minus_one_strict_less_min_is_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[ARG_MINUS_ONE]], [[ABS]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %arg_minus_one = add nsw i32 %arg, -1
+ %cmp = icmp slt i32 %arg_minus_one, %abs
+ ret i1 %cmp
+}
+
+define i1 @arg_minus_one_strict_greater_min_is_not_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @arg_minus_one_strict_greater_min_is_not_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
+; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
; CHECK-NEXT: ret i1 false
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
+ %arg_minus_one = add nsw i32 %arg, -1
+ %cmp = icmp sgt i32 %arg_minus_one, %abs
+ ret i1 %cmp
+}
+
+define i1 @arg_minus_one_strict_greater_min_is_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @arg_minus_one_strict_greater_min_is_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[ARG_MINUS_ONE]], [[ABS]]
+; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%arg_minus_one = add nsw i32 %arg, -1
@@ -62,37 +106,55 @@ define i1 @arg_minus_one_strict_greater(i32 %arg) {
ret i1 %cmp
}
-define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg(i32 %arg) {
-; CHECK-LABEL: define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg_min_is_not_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg_min_is_not_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[CMP_ARG_NONNEGATIVE:%.*]] = icmp sge i32 [[ARG]], 0
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP_ARG_NONNEGATIVE]])
-; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nuw i32 [[ABS]], 1
; CHECK-NEXT: ret i1 true
;
%cmp_arg_nonnegative = icmp sge i32 %arg, 0
call void @llvm.assume(i1 %cmp_arg_nonnegative)
- %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
%abs_plus_one = add nuw i32 %abs, 1
%cmp = icmp uge i32 %abs_plus_one, %arg
ret i1 %cmp
}
-define i1 @abs_plus_one_unsigned_greater_or_equal_cannot_be_simplified(i32 %arg) {
-; CHECK-LABEL: define i1 @abs_plus_one_unsigned_greater_or_equal_cannot_be_simplified(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg_min_is_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg_min_is_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[CMP_ARG_NONNEGATIVE:%.*]] = icmp sge i32 [[ARG]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP_ARG_NONNEGATIVE]])
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nuw i32 [[ABS]], 1
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[ABS_PLUS_ONE]], [[ARG]]
; CHECK-NEXT: ret i1 [[CMP]]
;
+ %cmp_arg_nonnegative = icmp sge i32 %arg, 0
+ call void @llvm.assume(i1 %cmp_arg_nonnegative)
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%abs_plus_one = add nuw i32 %abs, 1
%cmp = icmp uge i32 %abs_plus_one, %arg
ret i1 %cmp
}
+define i1 @abs_plus_one_unsigned_greater_or_equal_cannot_be_simplified(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_plus_one_unsigned_greater_or_equal_cannot_be_simplified(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
+; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nuw i32 [[ABS]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[ABS_PLUS_ONE]], [[ARG]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
+ %abs_plus_one = add nuw i32 %abs, 1
+ %cmp = icmp uge i32 %abs_plus_one, %arg
+ ret i1 %cmp
+}
+
define i1 @abs_constant_negative_arg() {
; CHECK-LABEL: define i1 @abs_constant_negative_arg() {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 false)
@@ -114,9 +176,9 @@ define i1 @abs_constant_positive_arg() {
ret i1 %cmp
}
-define i1 @abs_is_nonnegative_except_for_int_min_if_int_min_is_not_poison(i32 %arg) {
+define i1 @abs_is_nonnegative_except_for_int_min_if_int_min_is_not_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_is_nonnegative_except_for_int_min_if_int_min_is_not_poison(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
; CHECK-NEXT: ret i1 [[CMP]]
@@ -126,23 +188,36 @@ define i1 @abs_is_nonnegative_except_for_int_min_if_int_min_is_not_poison(i32 %a
ret i1 %cmp
}
-define i1 @abs_is_not_strictly_positive(i32 %arg) {
+define i1 @abs_is_not_strictly_positive(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_is_not_strictly_positive(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
-; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[ABS]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
- %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
%cmp = icmp sgt i32 %abs, 0
ret i1 %cmp
}
-define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
+define i1 @abs_is_nonnegative_int_min_is_not_poison(i32 noundef %arg) {
+; CHECK-LABEL: define i1 @abs_is_nonnegative_int_min_is_not_poison(
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
+ %cmp = icmp sge i32 %abs, 0
+ ret i1 %cmp
+}
+
+define i1 @abs_is_nonnegative_int_min_is_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_is_nonnegative_int_min_is_poison(
-; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%cmp = icmp sge i32 %abs, 0
@@ -152,7 +227,8 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-LABEL: define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
-; CHECK-NEXT: ret i1 true
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
;
%abs = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
%cmp = icmp sge i32 %abs, 0
diff --git a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
new file mode 100644
index 0000000000000..42bbe67024e58
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
@@ -0,0 +1,65 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
+
+declare i64 @llvm.uadd.sat.i64(i64, i64)
+declare i64 @llvm.usub.sat.i64(i64, i64)
+
+define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
+; CHECK-LABEL: define i1 @uadd_sat_uge(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 [[B]])
+; CHECK-NEXT: [[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
+; CHECK-NEXT: [[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
+ %cmp1 = icmp uge i64 %add.sat, %a
+ %cmp2 = icmp uge i64 %add.sat, %b
+ %cmp = and i1 %cmp1, %cmp2
+ ret i1 %cmp
+}
+
+define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
+; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 [[B]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
+ %cmp = icmp ule i64 %sub.sat, %a
+ ret i1 %cmp
+}
+
+; Negative test
+define i1 @usub_sat_not_ule_rhs(i64 noundef %a, i64 noundef %b) {
+; CHECK-LABEL: define i1 @usub_sat_not_ule_rhs(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 [[B]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[B]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
+ %cmp = icmp ule i64 %sub.sat, %b
+ ret i1 %cmp
+}
+
+define i1 @pr135603(i64 %conv6, i64 %str.coerce, ptr %conv) {
+; CHECK-LABEL: define i1 @pr135603(
+; CHECK-SAME: i64 [[CONV6:%.*]], i64 [[STR_COERCE:%.*]], ptr [[CONV:%.*]]) {
+; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[CONV]], align 4
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A]], -1
+; CHECK-NEXT: [[CONV2:%.*]] = zext nneg i32 [[A]] to i64
+; CHECK-NEXT: [[ADD:%.*]] = add i64 [[STR_COERCE]], [[CONV6]]
+; CHECK-NEXT: [[SPEC_SELECT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[CONV2]], i64 [[ADD]])
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %a = load i32, ptr %conv, align 4
+ %cmp = icmp sgt i32 %a, -1
+ %conv2 = zext nneg i32 %a to i64
+ %add = add i64 %str.coerce, %conv6
+ %spec.select = call i64 @llvm.usub.sat.i64(i64 %conv2, i64 %add)
+ ret i1 %cmp
+}
+
>From 66a876c54233d7bf928b72753485ac2733563b0f Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Sun, 20 Apr 2025 10:32:29 +0800
Subject: [PATCH 2/3] "Recommit "[ConstraintElim] Simplify cmp after
uadd.sat/usub.sat (#135603)"
---
.../Transforms/Scalar/ConstraintElimination.cpp | 16 ++++++++++++++++
.../ConstraintElimination/uadd-usub-sat.ll | 7 ++-----
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8684420b73671..9fd6b067debdf 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1139,6 +1139,8 @@ void State::addInfoFor(BasicBlock &BB) {
// when simplifying uses of the min/max intrinsics.
[[fallthrough]];
case Intrinsic::abs:
+ case Intrinsic::uadd_sat:
+ case Intrinsic::usub_sat:
if (!isGuaranteedNotToBePoison(&I))
break;
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
@@ -1896,6 +1898,20 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
AddFact(Pred, MinMax, MinMax->getRHS());
continue;
}
+ if (auto *USatI = dyn_cast<SaturatingInst>(CB.Inst)) {
+ switch (USatI->getIntrinsicID()) {
+ default:
+ llvm_unreachable("Unexpected intrinsic.");
+ case Intrinsic::uadd_sat:
+ AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
+ AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
+ break;
+ case Intrinsic::usub_sat:
+ AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
+ break;
+ }
+ continue;
+ }
}
Value *A = nullptr, *B = nullptr;
diff --git a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
index 42bbe67024e58..c19a0548d78b2 100644
--- a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
+++ b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
@@ -8,9 +8,7 @@ define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
; CHECK-LABEL: define i1 @uadd_sat_uge(
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
; CHECK-NEXT: [[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 [[B]])
-; CHECK-NEXT: [[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
-; CHECK-NEXT: [[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT: [[CMP:%.*]] = and i1 true, true
; CHECK-NEXT: ret i1 [[CMP]]
;
%add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
@@ -24,8 +22,7 @@ define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
; CHECK-NEXT: [[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 [[B]])
-; CHECK-NEXT: [[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
%cmp = icmp ule i64 %sub.sat, %a
>From 4a30bfeedf2d6378c43543aae297f2fc4ec2e9f3 Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Wed, 30 Apr 2025 17:28:44 +0800
Subject: [PATCH 3/3] update
---
.../Scalar/ConstraintElimination.cpp | 4 ++--
.../Transforms/ConstraintElimination/abs.ll | 21 +++++++------------
2 files changed, 9 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8684420b73671..58d705eb6aa96 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1137,10 +1137,10 @@ void State::addInfoFor(BasicBlock &BB) {
FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
// TODO: Check if it is possible to instead only added the min/max facts
// when simplifying uses of the min/max intrinsics.
- [[fallthrough]];
- case Intrinsic::abs:
if (!isGuaranteedNotToBePoison(&I))
break;
+ [[fallthrough]];
+ case Intrinsic::abs:
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
break;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/abs.ll b/llvm/test/Transforms/ConstraintElimination/abs.ll
index 8cbdf39102700..b9a0c4963211f 100644
--- a/llvm/test/Transforms/ConstraintElimination/abs.ll
+++ b/llvm/test/Transforms/ConstraintElimination/abs.ll
@@ -16,8 +16,7 @@ define i1 @abs_int_min_is_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_int_min_is_poison(
; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
-; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], [[ARG]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%cmp = icmp sge i32 %abs, %arg
@@ -42,8 +41,7 @@ define i1 @abs_plus_one_min_is_poison(i32 noundef %arg) {
; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nsw i32 [[ABS]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS_PLUS_ONE]], [[ARG]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%abs_plus_one = add nsw i32 %abs, 1
@@ -70,8 +68,7 @@ define i1 @arg_minus_one_strict_less_min_is_poison(i32 noundef %arg) {
; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
-; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[ARG_MINUS_ONE]], [[ABS]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%arg_minus_one = add nsw i32 %arg, -1
@@ -97,8 +94,7 @@ define i1 @arg_minus_one_strict_greater_min_is_poison(i32 noundef %arg) {
; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ARG_MINUS_ONE:%.*]] = add nsw i32 [[ARG]], -1
-; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[ARG_MINUS_ONE]], [[ABS]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 false
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%arg_minus_one = add nsw i32 %arg, -1
@@ -130,8 +126,7 @@ define i1 @abs_plus_one_unsigned_greater_or_equal_nonnegative_arg_min_is_poison(
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP_ARG_NONNEGATIVE]])
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[ABS_PLUS_ONE:%.*]] = add nuw i32 [[ABS]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[ABS_PLUS_ONE]], [[ARG]]
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%cmp_arg_nonnegative = icmp sge i32 %arg, 0
call void @llvm.assume(i1 %cmp_arg_nonnegative)
@@ -216,8 +211,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 noundef %arg) {
; CHECK-LABEL: define i1 @abs_is_nonnegative_int_min_is_poison(
; CHECK-SAME: i32 noundef [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
-; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%cmp = icmp sge i32 %abs, 0
@@ -227,8 +221,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 noundef %arg) {
define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-LABEL: define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
-; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
-; CHECK-NEXT: ret i1 [[CMP]]
+; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
%cmp = icmp sge i32 %abs, 0
More information about the llvm-commits
mailing list