[InstCombine] Fold zero-test of "(X + Y) | Y" to "X | Y" #230431
<!--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-transforms
Author: Archmushroom (Misasasa)
<details>
<summary>Changes</summary>
Fixes #<!-- -->230424
Fold icmp eq/ne (or (add X, Y), Y), 0 into icmp eq/ne (or X, Y), 0
because or(A, B) == 0 iff A == 0 && B == 0, and(X + Y) == 0 && Y == 0 iff X == 0 && Y == 0.
Added regression test inllvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll,
and all InstCombine lit tests pass.
---
Full diff: https://github.com/llvm/llvm-project/pull/230431.diff
2 Files Affected:
````````````diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 8893744821f7e6..7a878cbb09ba52 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1296,6 +1296,25 @@ Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
// will fold to a constant elsewhere.
}
+ // (icmp eq/ne (or (add X, Y), Y), 0) -> (icmp eq/ne (or X, Y), 0)
+ // Because or(A, B) == 0 iff A == 0 and B == 0, and (X + Y) == 0 and Y == 0
+ // iff X == 0 and Y == 0, the zero-test is preserved.
+ if (ICmpInst::isEquality(Pred)) {
+ Value *OrOp0, *OrOp1;
+ if (match(Cmp.getOperand(0), m_c_Or(m_Value(OrOp0), m_Value(OrOp1)))) {
+ Value *X, *Y;
+ // Check if one operand is add(X, Y) and the other is Y
+ if (match(OrOp0, m_Add(m_Value(X), m_Value(Y))) && Y == OrOp1) {
+ Value *NewOr = Builder.CreateOr(X, Y);
+ return new ICmpInst(Pred, NewOr, Cmp.getOperand(1));
+ }
+ if (match(OrOp1, m_Add(m_Value(X), m_Value(Y))) && Y == OrOp0) {
+ Value *NewOr = Builder.CreateOr(X, Y);
+ return new ICmpInst(Pred, NewOr, Cmp.getOperand(1));
+ }
+ }
+ }
+
// (icmp eq/ne f(X), 0) -> (icmp eq/ne X, 0)
// where f(X) == 0 if and only if X == 0
if (ICmpInst::isEquality(Pred))
diff --git a/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll b/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll
index 43afd3c69f7c30..81392fbb995232 100644
--- a/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-or-of-select-with-zero.ll
@@ -310,3 +310,42 @@ entry:
%cmp = icmp ne <4 x i32> %or, zeroinitializer
ret <4 x i1> %cmp
}
+
+; ==============================================================================
+; Zero-test on or(add(X, Y), Y): the "or(add(X,Y),Y) == 0" sub-expression is
+; folded to "or(X, Y)". The add is redundant because it does not contribute
+; bits that are not already present in Y (adding X to Y keeps all bits of Y
+; that are tested by the zero-check; the fold relies on the later zero-icmp).
+; (Generated from real missed-optimization candidates.)
+; ==============================================================================
+define i1 @zero_or_add_eq(i64 %x, i64 %y) {
+; CHECK-LABEL: @zero_or_add_eq(
+; CHECK-NEXT: [[N0:%.*]] = tail call i64 @llvm.umax.i64(i64 [[X:%.*]], i64 [[Y:%.*]])
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[N0]], [[X]]
+; CHECK-NEXT: [[RET:%.*]] = icmp eq i64 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %n0 = tail call i64 @llvm.umax.i64(i64 %x, i64 %y)
+ %n1 = add nsw i64 %n0, %x
+ %n2 = icmp ult i64 %n1, %x
+ %n3 = tail call i64 @llvm.umin.i64(i64 %n1, i64 576460752303423487)
+ %n4 = select i1 %n2, i64 576460752303423487, i64 %n3
+ %n5 = icmp eq i64 %n4, 0
+ ret i1 %n5
+}
+
+define i1 @zero_or_add_ne(i1 %c, i64 %y) {
+; CHECK-LABEL: @zero_or_add_ne(
+; CHECK-NEXT: [[N0:%.*]] = select i1 [[C:%.*]], i64 1, i64 [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = or i64 [[N0]], [[Y]]
+; CHECK-NEXT: [[RET:%.*]] = icmp ne i64 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %n0 = select i1 %c, i64 1, i64 %y
+ %n1 = add nsw i64 %n0, %y
+ %n2 = icmp ult i64 %n1, %y
+ %n3 = tail call i64 @llvm.umin.i64(i64 %n1, i64 288230376151711743)
+ %n4 = select i1 %n2, i64 288230376151711743, i64 %n3
+ %n5 = icmp ne i64 %n4, 0
+ ret i1 %n5
+}
</details>
match(Cmp.getOperand(0), m_c_Or(m_c_Add(m_Value(X), m_Value(Y)), m_Deferred(Y)))
Please make the comment concise.
Only add tests with the pattern handled by this patch. See https://llvm.org/docs/InstCombineContributorGuide.html#tests.
Please write the PR description yourself and follow the instructions in https://llvm.org/docs/AIToolPolicy.html and https://llvm.org/docs/InstCombineContributorGuide.html.
Done.
Done.
Done.
Thanks for your review! I have rewritten my PR description and revised my changes as suggested.
@dtcxzyw Thanks for your review! I have reworked my PR as suggested, including codes, tests, and the PR description itself. Would you mind taking another look?
dtcxzyw
Misasasa
Summary
Fixes: #230424
Fold zero-test of
X | (X + Y)intoX | Y:X | (X + Y) == 0intoX | Y == 0(Proof: https://alive2.llvm.org/ce/z/Cty8_F)X | (X + Y) != 0intoX | Y != 0(Proof: https://alive2.llvm.org/ce/z/RzM9rG)Motivation
In standard library functions like
std::vector<short>::_M_fill_insertorstd::vector<char>::_M_range_insert<...>, I find patterns likewhich can be optimized into (discovered via super-optimizer)
(https://alive2.llvm.org/ce/z/bQqGyS)
This optimization becomes available under this patch.
Assisted-by
Qwen-3.7-Plus, which helps me locate/sketch the patch and write tests.