-
Notifications
You must be signed in to change notification settings - Fork 11.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ConstraintElim] Add facts for llvm.abs >= 0 #79070
Merged
alexander-shaposhnikov
merged 1 commit into
llvm:main
from
alexander-shaposhnikov:abs_nonnegative
Feb 6, 2024
Merged
[ConstraintElim] Add facts for llvm.abs >= 0 #79070
alexander-shaposhnikov
merged 1 commit into
llvm:main
from
alexander-shaposhnikov:abs_nonnegative
Feb 6, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-transforms Author: Alexander Shaposhnikov (alexander-shaposhnikov) ChangesAdd facts for llvm.abs >= 0. Full diff: https://github.com/llvm/llvm-project/pull/79070.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8f09569d0d9cc9f..6d0f57c1f6c8a41 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1725,7 +1725,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
if (!CB.isConditionFact()) {
Value *X;
if (match(CB.Inst, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) {
- // TODO: Add CB.Inst >= 0 fact.
+ // If is_int_min_poison is true then we may assume llvm.abs >= 0.
+ if (!cast<ConstantInt>(CB.Inst->getOperand(1))->isZero())
+ AddFact(CmpInst::ICMP_SGE, CB.Inst,
+ ConstantInt::get(CB.Inst->getType(), 0));
AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
continue;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/abs.ll b/llvm/test/Transforms/ConstraintElimination/abs.ll
index c3162ba48c2e4af..9fc68b0e72663fc 100644
--- a/llvm/test/Transforms/ConstraintElimination/abs.ll
+++ b/llvm/test/Transforms/ConstraintElimination/abs.ll
@@ -114,5 +114,50 @@ 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) {
+; CHECK-LABEL: define i1 @abs_is_nonnegative_except_for_int_min_if_int_min_is_not_poison(
+; CHECK-SAME: i32 [[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_not_strictly_positive(i32 %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-NEXT: [[CMP:%.*]] = icmp sgt i32 [[ABS]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
+ %cmp = icmp sgt i32 %abs, 0
+ ret i1 %cmp
+}
+
+define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
+; CHECK-LABEL: define i1 @abs_is_nonnegative_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, 0
+ ret i1 %cmp
+}
+
+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
+;
+ %abs = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
+ %cmp = icmp sge i32 %abs, 0
+ ret i1 %cmp
+}
+
declare i32 @llvm.abs.i32(i32, i1 immarg)
declare void @llvm.assume(i1)
|
alexander-shaposhnikov
added a commit
that referenced
this pull request
Jan 29, 2024
Add tests for llvm.abs >= 0. This is a preparation for #79070 .
alexander-shaposhnikov
force-pushed
the
abs_nonnegative
branch
from
January 29, 2024 22:24
2dfde8b
to
618b7e2
Compare
@fhahn - I've just rebased the patch |
gentle ping @fhahn |
nikic
approved these changes
Feb 6, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
alexander-shaposhnikov
force-pushed
the
abs_nonnegative
branch
from
February 6, 2024 21:46
618b7e2
to
da728ca
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add facts for llvm.abs >= 0.
https://alive2.llvm.org/ce/z/GXnMHu