[InstCombine] Generalize existing pattern from "abs(val) != 0 -> val != 0" to "abs(val) == 0 -> val == 0" #233763
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @Misasasa :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
The InstCombine Contributor Guide lays out a series of rules that contributions to InstCombine (and other middle-end components) should follow.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
@llvm/pr-subscribers-llvm-transforms
Author: Archmushroom (Misasasa)
<details>
<summary>Changes</summary>
Summary
Fixes #<!-- -->233753
There has been an available fold:
define i1 @<!-- -->src(i32 %x, i1 %cond) {
%negx = sub i32 0, %x
%somex = select i1 %cond, i32 %negx, i32 %x
%ret = icmp ne i32 %somex, 0
ret i1 %ret
}
define i1 @<!-- -->tgt(i32 %x, i1 %cond) {
%ret = icmp ne i32 %x, 0
ret i1 %ret
}
Likewise, this fold (ne replaced with eq) is also correct but missed:
define i1 @<!-- -->src(i32 %x, i1 %cond) {
%negx = sub i32 0, %x
%somex = select i1 %cond, i32 %negx, i32 %x
%ret = icmp eq i32 %somex, 0
ret i1 %ret
}
define i1 @<!-- -->tgt(i32 %x, i1 %cond) {
%ret = icmp eq i32 %x, 0
ret i1 %ret
}
https://alive2.llvm.org/ce/z/7WQeXT
This PR generalizes the corresponding pattern for both cases.
Motivation
The missed pattern is noticed in Google Abseil's demangler.
Assisted-by
Qwen3.7-Plus, which helps me locate the existing pattern instead of opening a new pattern.
---
Full diff: https://github.com/llvm/llvm-project/pull/233763.diff
2 Files Affected:
````````````diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 8893744821f7e6..23648ca1a1a65c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8013,9 +8013,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Value *V = simplifyICmpInst(I.getCmpPredicate(), Op0, Op1, Q))
return replaceInstUsesWith(I, V);
- // Comparing -val or val with non-zero is the same as just comparing val
- // ie, abs(val) != 0 -> val != 0
- if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) {
+ // Comparing -val or val with zero is the same as just comparing val
+ // ie, abs(val) == 0 -> val == 0, abs(val) != 0 -> val != 0
+ if (ICmpInst::isEquality(I.getPredicate()) && match(Op1, m_Zero())) {
Value *Cond, *SelectTrue, *SelectFalse;
if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
m_Value(SelectFalse)))) {
diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll
index c7bae58a0b1974..c49b2d6c7fc3b3 100644
--- a/llvm/test/Transforms/InstCombine/icmp.ll
+++ b/llvm/test/Transforms/InstCombine/icmp.ll
@@ -1279,8 +1279,8 @@ define i1 @test57(i32 %a) {
}
; rdar://problem/10482509
-define zeroext i1 @cmpabs1(i64 %val) {
-; CHECK-LABEL: define zeroext i1 @cmpabs1(
+define zeroext i1 @cmpabs_ne1(i64 %val) {
+; CHECK-LABEL: define zeroext i1 @cmpabs_ne1(
; CHECK-SAME: i64 [[VAL:%.*]]) {
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i64 [[VAL]], 0
; CHECK-NEXT: ret i1 [[TOBOOL]]
@@ -1292,8 +1292,8 @@ define zeroext i1 @cmpabs1(i64 %val) {
ret i1 %tobool
}
-define zeroext i1 @cmpabs2(i64 %val) {
-; CHECK-LABEL: define zeroext i1 @cmpabs2(
+define zeroext i1 @cmpabs_ne2(i64 %val) {
+; CHECK-LABEL: define zeroext i1 @cmpabs_ne2(
; CHECK-SAME: i64 [[VAL:%.*]]) {
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i64 [[VAL]], 0
; CHECK-NEXT: ret i1 [[TOBOOL]]
@@ -1305,6 +1305,32 @@ define zeroext i1 @cmpabs2(i64 %val) {
ret i1 %tobool
}
+define zeroext i1 @cmpabs_eq1(i64 %val) {
+; CHECK-LABEL: define zeroext i1 @cmpabs_eq1(
+; CHECK-SAME: i64 [[VAL:%.*]]) {
+; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i64 [[VAL]], 0
+; CHECK-NEXT: ret i1 [[TOBOOL]]
+;
+ %sub = sub nsw i64 0, %val
+ %cmp = icmp slt i64 %val, 0
+ %sub.val = select i1 %cmp, i64 %sub, i64 %val
+ %tobool = icmp eq i64 %sub.val, 0
+ ret i1 %tobool
+}
+
+define zeroext i1 @cmpabs_eq2(i64 %val) {
+; CHECK-LABEL: define zeroext i1 @cmpabs_eq2(
+; CHECK-SAME: i64 [[VAL:%.*]]) {
+; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i64 [[VAL]], 0
+; CHECK-NEXT: ret i1 [[TOBOOL]]
+;
+ %sub = sub nsw i64 0, %val
+ %cmp = icmp slt i64 %val, 0
+ %sub.val = select i1 %cmp, i64 %val, i64 %sub
+ %tobool = icmp eq i64 %sub.val, 0
+ ret i1 %tobool
+}
+
define i1 @abs_intrin_eq_zero(i8 %x) {
; CHECK-LABEL: define i1 @abs_intrin_eq_zero(
; CHECK-SAME: i8 [[X:%.*]]) {
</details>
Can you please move this pattern into stripNullTest?
Done. But one thing I'm not sure about.
Previously, dyn_castNegVal (unfortunately unavailable in ValueTracking)
matches both variables and constants, so I refactor it into two blocks:
https://github.com/Misasasa/llvm-project/blob/f362b329b57b151816dc3cec3f7ecf2778567e0d/llvm/lib/Analysis/ValueTracking.cpp#L11125-L11130
and
https://github.com/Misasasa/llvm-project/blob/f362b329b57b151816dc3cec3f7ecf2778567e0d/llvm/lib/Analysis/ValueTracking.cpp#L11131-L11140
However, I find that even without the constant block, related optimizations still work, like:
This is why I'm not sure whether the constant block is necessary.
Constant path is unnecessary. InstSimplify can fold the comparison into true/false.
match(V, m_c_Select(m_Value(X), m_Neg(m_Deferred(X)))).
Done!
This is a weild failure. My changes should not affect tests like lld :: COFF/pdb-thinlto.ll. Could anyone please help me with these checks? @dtcxzyw @RKSimon
This is a weild failure. My changes should not affect tests like lld :: COFF/pdb-thinlto.ll. Could anyone please help me with these checks? @dtcxzyw @RKSimon
Its most likely a flakey test - trying merging against latest to kick off a fresh CI build
These tests already fold without your changes? https://llvm.godbolt.org/z/To4asGcGa
Please follow https://llvm.org/docs/InstCombineContributorGuide.html#precommit-tests.
Presumably you just need to replace the condition with an i1 %c argument or similar, so the select doesn't fold to abs.
Thanks for your suggestion! Now tests cannot fold without changes: https://llvm.godbolt.org/z/WP1W7Khvq
%sub = sub i64 0, %val
These nsw's should not be necessary for the fold.
LGTM with one nit.
Thanks for your suggestion! I have removed nsw's in both test cases.
LGTM
This commit will be authored by 45325799+Misasasa@users.noreply.github.com.
Can you please disable the private email option in your GitHub settings? See https://llvm.org/docs/DeveloperPolicy.html#email-addresses.
> This commit will be authored by [45325799+Misasasa@users.noreply.github.com](mailto:45325799+Misasasa@users.noreply.github.com).
> Can you please disable the private email option in your GitHub settings? See https://llvm.org/docs/DeveloperPolicy.html#email-addresses.
Done. Thanks for reminding me this!
<!--LLVM BUILDBOT INFORMATION COMMENT-->
@Misasasa Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.
Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.
How to do this, and the rest of the post-merge process, is covered in detail here.
If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.
If you don't get any reports, no action is required from you. Your changes are working as expected, well done!
nikic
Misasasa
Generalize fold of
(c ? x : -x) != 0tox != 0by moving the pattern to stripNullTest() and thus also supporting==predicates and other patterns supported by that helper.The missed pattern is noticed in Google Abseil's demangler (https://github.com/abseil/abseil-cpp/blob/master/absl/debugging/internal/demangle.cc).
Proof: https://alive2.llvm.org/ce/z/7WQeXT
Fixes #233753
Assisted-by: Qwen3.7-Plus, which helps me locate the existing pattern instead of opening a new pattern.