-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[ConstraintElim] Apply add with neg constant first during decomp. #164791
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
Conversation
We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma
|
@llvm/pr-subscribers-llvm-transforms Author: Florian Hahn (fhahn) ChangesWe have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma Full diff: https://github.com/llvm/llvm-project/pull/164791.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 4acc3f2d84690..d347cedb42988 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -614,6 +614,16 @@ static Decomposition decompose(Value *V,
return {V, IsKnownNonNegative};
}
+ if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
+ canUseSExt(CI)) {
+ Preconditions.emplace_back(
+ CmpInst::ICMP_UGE, Op0,
+ ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
+ if (auto Decomp = MergeResults(Op0, CI, true))
+ return *Decomp;
+ return {V, IsKnownNonNegative};
+ }
+
if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
if (!isKnownNonNegative(Op0, DL))
Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
@@ -627,16 +637,6 @@ static Decomposition decompose(Value *V,
return {V, IsKnownNonNegative};
}
- if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
- canUseSExt(CI)) {
- Preconditions.emplace_back(
- CmpInst::ICMP_UGE, Op0,
- ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
- if (auto Decomp = MergeResults(Op0, CI, true))
- return *Decomp;
- return {V, IsKnownNonNegative};
- }
-
// Decompose or as an add if there are no common bits between the operands.
if (match(V, m_DisjointOr(m_Value(Op0), m_ConstantInt(CI)))) {
if (auto Decomp = MergeResults(Op0, CI, IsSigned))
diff --git a/llvm/test/Transforms/ConstraintElimination/add-nsw.ll b/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
index 5127e92a7c345..4b8ac09801ab2 100644
--- a/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
+++ b/llvm/test/Transforms/ConstraintElimination/add-nsw.ll
@@ -757,8 +757,7 @@ define i1 @add_neg_1_known_sge_ult_1(i32 %a) {
; CHECK-NEXT: [[A_SGE:%.*]] = icmp sge i32 [[A:%.*]], 1
; CHECK-NEXT: call void @llvm.assume(i1 [[A_SGE]])
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[A]], -1
-; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
-; CHECK-NEXT: ret i1 [[C]]
+; CHECK-NEXT: ret i1 true
;
entry:
%a.sge = icmp sge i32 %a, 1
@@ -823,8 +822,7 @@ define i1 @add_neg_3_known_sge_ult_1(i32 %a) {
; CHECK-NEXT: [[A_SGE:%.*]] = icmp sge i32 [[A:%.*]], 3
; CHECK-NEXT: call void @llvm.assume(i1 [[A_SGE]])
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[A]], -3
-; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
-; CHECK-NEXT: ret i1 [[C]]
+; CHECK-NEXT: ret i1 true
;
entry:
%a.sge = icmp sge i32 %a, 3
diff --git a/llvm/test/Transforms/ConstraintElimination/gep-arithmetic-add.ll b/llvm/test/Transforms/ConstraintElimination/gep-arithmetic-add.ll
index 52adc78b4e159..8dcac7832179a 100644
--- a/llvm/test/Transforms/ConstraintElimination/gep-arithmetic-add.ll
+++ b/llvm/test/Transforms/ConstraintElimination/gep-arithmetic-add.ll
@@ -389,8 +389,7 @@ define i1 @gep_count_add_1_sge_known_ult_1(i32 %count, ptr %p) {
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[COUNT]], -1
; CHECK-NEXT: [[SUB_EXT:%.*]] = zext i32 [[SUB]] to i64
; CHECK-NEXT: [[GEP_SUB:%.*]] = getelementptr inbounds i32, ptr [[P]], i64 [[SUB_EXT]]
-; CHECK-NEXT: [[C:%.*]] = icmp ult ptr [[GEP_SUB]], [[GEP_COUNT]]
-; CHECK-NEXT: ret i1 [[C]]
+; CHECK-NEXT: ret i1 true
;
entry:
%sge = icmp sge i32 %count, 1
@@ -415,8 +414,7 @@ define i1 @gep_count_add_1_sge_known_uge_1(i32 %count, ptr %p) {
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[COUNT]], -1
; CHECK-NEXT: [[SUB_EXT:%.*]] = zext i32 [[SUB]] to i64
; CHECK-NEXT: [[GEP_SUB:%.*]] = getelementptr inbounds i32, ptr [[P]], i64 [[SUB_EXT]]
-; CHECK-NEXT: [[C:%.*]] = icmp uge ptr [[GEP_SUB]], [[P]]
-; CHECK-NEXT: ret i1 [[C]]
+; CHECK-NEXT: ret i1 true
;
entry:
%sge = icmp sge i32 %count, 1
|
nikic
left a comment
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
…decomp. (#164791) We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma PR: llvm/llvm-project#164791
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/65/builds/24418 Here is the relevant piece of the build log for the reference |
…vm#164791) We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma PR: llvm#164791
…vm#164791) We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma PR: llvm#164791
…vm#164791) We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma PR: llvm#164791
We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice.
Update the code to first apply the pattern with negative constants.
Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968
Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma