[llvm] [InstCombine] Drop one-use check for (A & ~B) | (A ^ B) -> A ^ B (PR #171047)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 7 09:35:18 PST 2025
https://github.com/wentywenty created https://github.com/llvm/llvm-project/pull/171047
[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: https://github.com/llvm/llvm-project/issues/113711
>From d729962bd954c3ac45093187f1f9d53e24fe71c1 Mon Sep 17 00:00:00 2001
From: wentywenty <2321901849 at qq.com>
Date: Mon, 8 Dec 2025 01:30:18 +0800
Subject: [PATCH] [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.
Signed-off-by: wentywenty <2321901849 at qq.com>
---
.../InstCombine/InstCombineAndOrXor.cpp | 5 ++
.../Transforms/InstCombine/or-xor-fold.ll | 58 +++++++++++++++++++
2 files changed, 63 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/or-xor-fold.ll
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
+}
More information about the llvm-commits
mailing list