[llvm] [ConstraintElim] Add facts implied by llvm.abs (PR #73189)
Alexander Shaposhnikov via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 28 23:38:03 PST 2023
https://github.com/alexander-shaposhnikov updated https://github.com/llvm/llvm-project/pull/73189
>From dbc9d62e36ea144e9108f0784d556bdcf0a03352 Mon Sep 17 00:00:00 2001
From: Alexander Shaposhnikov <ashaposhnikov at google.com>
Date: Sun, 3 Dec 2023 13:42:45 +0000
Subject: [PATCH] [ConstraintElim] Add facts implied by llvm.abs
---
.../Scalar/ConstraintElimination.cpp | 48 ++++---
.../Transforms/ConstraintElimination/abs.ll | 118 ++++++++++++++++++
2 files changed, 149 insertions(+), 17 deletions(-)
create mode 100644 llvm/test/Transforms/ConstraintElimination/abs.ll
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 7aadd810c1da38..31c969a14867a5 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -984,22 +984,14 @@ void State::addInfoFor(BasicBlock &BB) {
continue;
}
- if (match(&I, m_Intrinsic<Intrinsic::ssub_with_overflow>())) {
- WorkList.push_back(
- FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
- continue;
- }
-
- if (isa<MinMaxIntrinsic>(&I)) {
- WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
- continue;
- }
-
- Value *A, *B;
- CmpInst::Predicate Pred;
- // For now, just handle assumes with a single compare as condition.
- if (match(&I, m_Intrinsic<Intrinsic::assume>(
- m_ICmp(Pred, m_Value(A), m_Value(B))))) {
+ auto *II = dyn_cast<IntrinsicInst>(&I);
+ Intrinsic::ID ID = II ? II->getIntrinsicID() : Intrinsic::not_intrinsic;
+ switch (ID) {
+ case Intrinsic::assume: {
+ Value *A, *B;
+ CmpInst::Predicate Pred;
+ if (!match(I.getOperand(0), m_ICmp(Pred, m_Value(A), m_Value(B))))
+ break;
if (GuaranteedToExecute) {
// The assume is guaranteed to execute when BB is entered, hence Cond
// holds on entry to BB.
@@ -1009,7 +1001,23 @@ void State::addInfoFor(BasicBlock &BB) {
WorkList.emplace_back(
FactOrCheck::getInstFact(DT.getNode(I.getParent()), &I));
}
+ break;
}
+ // Enqueue ssub_with_overflow for simplification.
+ case Intrinsic::ssub_with_overflow:
+ WorkList.push_back(
+ FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
+ break;
+ // Enqueue the intrinsics to add extra info.
+ case Intrinsic::abs:
+ case Intrinsic::umin:
+ case Intrinsic::umax:
+ case Intrinsic::smin:
+ case Intrinsic::smax:
+ WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
+ break;
+ }
+
GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
}
@@ -1253,7 +1261,7 @@ static std::optional<bool> checkCondition(CmpInst *Cmp, ConstraintInfo &Info,
Value *B = Cmp->getOperand(1);
auto R = Info.getConstraintForSolving(Pred, A, B);
- if (R.empty() || !R.isValid(Info)){
+ if (R.empty() || !R.isValid(Info)) {
LLVM_DEBUG(dbgs() << " failed to decompose condition\n");
return std::nullopt;
}
@@ -1629,6 +1637,12 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
ICmpInst::Predicate Pred;
if (!CB.isConditionFact()) {
+ if (Value *X; match(CB.Inst, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) {
+ // TODO: Add CB.Inst >= 0 fact.
+ AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
+ continue;
+ }
+
if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(CB.Inst)) {
Pred = ICmpInst::getNonStrictPredicate(MinMax->getPredicate());
AddFact(Pred, MinMax, MinMax->getLHS());
diff --git a/llvm/test/Transforms/ConstraintElimination/abs.ll b/llvm/test/Transforms/ConstraintElimination/abs.ll
new file mode 100644
index 00000000000000..c3162ba48c2e4a
--- /dev/null
+++ b/llvm/test/Transforms/ConstraintElimination/abs.ll
@@ -0,0 +1,118 @@
+; 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) {
+; CHECK-LABEL: define i1 @abs_int_min_is_not_poison(
+; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 false)
+; CHECK-NEXT: ret i1 true
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 false)
+ %cmp = icmp sge i32 %abs, %arg
+ ret i1 %cmp
+}
+
+define i1 @abs_int_min_is_poison(i32 %arg) {
+; CHECK-LABEL: define i1 @abs_int_min_is_poison(
+; CHECK-SAME: i32 [[ARG:%.*]]) {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
+; CHECK-NEXT: ret i1 true
+;
+ %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)
+; 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_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:%.*]]) {
+; 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: ret i1 true
+;
+ %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(i32 %arg) {
+; CHECK-LABEL: define i1 @arg_minus_one_strict_greater(
+; CHECK-SAME: i32 [[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: ret i1 false
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %arg_minus_one = add nsw i32 %arg, -1
+ %cmp = icmp sgt i32 %arg_minus_one, %abs
+ 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:%.*]]) {
+; 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: 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_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:%.*]]) {
+; 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]]
+;
+ %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_constant_negative_arg() {
+; CHECK-LABEL: define i1 @abs_constant_negative_arg() {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 false)
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 -3, i1 false)
+ %cmp = icmp sge i32 %abs, 3
+ ret i1 %cmp
+}
+
+define i1 @abs_constant_positive_arg() {
+; CHECK-LABEL: define i1 @abs_constant_positive_arg() {
+; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 3, i1 false)
+; CHECK-NEXT: ret i1 true
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 3, i1 false)
+ %cmp = icmp sge i32 %abs, 3
+ ret i1 %cmp
+}
+
+declare i32 @llvm.abs.i32(i32, i1 immarg)
+declare void @llvm.assume(i1)
More information about the llvm-commits
mailing list