Skip to content

Conversation

@wentywenty
Copy link

@wentywenty wentywenty commented Dec 7, 2025

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

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]>
@wentywenty wentywenty requested a review from nikic as a code owner December 7, 2025 17:35
@github-actions
Copy link

github-actions bot commented Dec 7, 2025

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Dec 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 7, 2025

@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:
(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.

Related issue: #113711


Full diff: https://github.com/llvm/llvm-project/pull/171047.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+5)
  • (added) llvm/test/Transforms/InstCombine/or-xor-fold.ll (+58)
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
+}

@wentywenty wentywenty changed the title [InstCombine] Drop one-use check for (A & ~B) | (A ^ B) -> A ^ B [InstCombine] Fold (A & ~B) | (A ^ B) -> A ^ B Dec 7, 2025
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants