-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[InstCombine] Fold (A & ~B) | (A ^ B) -> A ^ B #171047
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
base: main
Are you sure you want to change the base?
Conversation
This patch removes the m_OneUse constraint for the fold: (A & ~B) | (A ^ B) -> A ^ B Previously, this optimization was limited to cases where the operands had a single use. However, replacing the 'or' with the existing 'xor' operand is always profitable because it strictly removes one instruction (the 'or'), regardless of whether the intermediate 'and' or 'xor' are used elsewhere. Tests have been updated to reflect this multi-use optimization. Signed-off-by: wentywenty <[email protected]>
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-transforms Author: wentywenty (wentywenty) Changes[InstCombine] Drop one-use check for (A & ~B) | (A ^ B) -> A ^ B This patch removes the m_OneUse constraint for the fold: Previously, this optimization was limited to cases where the operands had Tests have been updated to reflect this multi-use optimization. Related issue: #113711 Full diff: https://github.com/llvm/llvm-project/pull/171047.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index ba5568b00441b..821086879339c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1982,6 +1982,11 @@ static Instruction *foldOrToXor(BinaryOperator &I,
match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))
return BinaryOperator::CreateXor(A, B);
+ // (A & ~B) | (A ^ B) -> A ^ B
+ if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
+ match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
+ return replaceInstUsesWith(I, Op1);
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/or-xor-fold.ll b/llvm/test/Transforms/InstCombine/or-xor-fold.ll
new file mode 100644
index 0000000000000..57797a33335b8
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/or-xor-fold.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare void @use(i32)
+
+; (A & ~B) | (A ^ B) -> A ^ B
+
+define i32 @test_basic(i32 %a, i32 %b) {
+; CHECK-LABEL: @test_basic(
+; CHECK-NEXT: [[RES:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+ %not_b = xor i32 %b, -1
+ %and = and i32 %a, %not_b
+ %xor = xor i32 %a, %b
+ %res = or i32 %and, %xor
+ ret i32 %res
+}
+
+define <2 x i32> @test_vector(<2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: @test_vector(
+; CHECK-NEXT: [[RES:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret <2 x i32> [[RES]]
+;
+ %not_b = xor <2 x i32> %b, <i32 -1, i32 -1>
+ %and = and <2 x i32> %a, %not_b
+ %xor = xor <2 x i32> %a, %b
+ %res = or <2 x i32> %and, %xor
+ ret <2 x i32> %res
+}
+
+define i32 @test_commute(i32 %a, i32 %b) {
+; CHECK-LABEL: @test_commute(
+; CHECK-NEXT: [[RES:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+ %not_b = xor i32 %b, -1
+ %and = and i32 %a, %not_b
+ %xor = xor i32 %a, %b
+ %res = or i32 %xor, %and ;
+ ret i32 %res
+}
+
+define i32 @test_multiuse(i32 %a, i32 %b) {
+; CHECK-LABEL: @test_multiuse(
+; CHECK-NEXT: [[NOT_B:%.*]] = xor i32 [[B:%.*]], -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[NOT_B]]
+; CHECK-NEXT: call void @use(i32 [[AND]])
+; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[XOR]]
+;
+ %not_b = xor i32 %b, -1
+ %and = and i32 %a, %not_b
+ call void @use(i32 %and)
+ %xor = xor i32 %a, %b
+ %res = or i32 %and, %xor
+ ret i32 %res
+}
|
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.
Please see https://llvm.org/docs/InstCombineContributorGuide.html#precommit-tests.
All of your tests already fold without your patch: https://llvm.godbolt.org/z/ffs3MYGGT
This patch implements the fold:
(A & ~B) | (A ^ B) -> A ^ B
The term (A & ~B) implies (A=1, B=0), which is already covered by
the XOR operation (A=1, B=0) | (A=0, B=1).
Therefore, the OR and the AND are redundant, leaving just A ^ B.
Related issue: #113711