Skip to content

Commit 882df05

Browse files
authored
[InstCombine] Fold (A | B) ^ (A & C) --> A ? ~C : B (#121906)
Closes #121773.
1 parent ca5fd06 commit 882df05

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4964,8 +4964,8 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
49644964

49654965
// (A & B) ^ (A | C) --> A ? ~B : C -- There are 4 commuted variants.
49664966
if (I.getType()->isIntOrIntVectorTy(1) &&
4967-
match(Op0, m_OneUse(m_LogicalAnd(m_Value(A), m_Value(B)))) &&
4968-
match(Op1, m_OneUse(m_LogicalOr(m_Value(C), m_Value(D))))) {
4967+
match(&I, m_c_Xor(m_OneUse(m_LogicalAnd(m_Value(A), m_Value(B))),
4968+
m_OneUse(m_LogicalOr(m_Value(C), m_Value(D)))))) {
49694969
bool NeedFreeze = isa<SelectInst>(Op0) && isa<SelectInst>(Op1) && B == D;
49704970
if (B == C || B == D)
49714971
std::swap(A, B);

llvm/test/Transforms/InstCombine/xor-and-or.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ define i1 @xor_logic_and_logic_or2(i1 %c, i1 %x, i1 %y) {
2525
ret i1 %r
2626
}
2727

28+
define i1 @xor_logic_and_logic_or2_commuted(i1 %c, i1 %x, i1 %y) {
29+
; CHECK-LABEL: @xor_logic_and_logic_or2_commuted(
30+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X:%.*]], true
31+
; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[TMP1]], i1 [[Y:%.*]]
32+
; CHECK-NEXT: ret i1 [[R]]
33+
;
34+
%o = select i1 %y, i1 true, i1 %c
35+
%a = select i1 %c, i1 %x, i1 false
36+
%r = xor i1 %o, %a
37+
ret i1 %r
38+
}
39+
2840
define i1 @xor_logic_and_logic_or3(i1 %c, i1 %x, i1 %y) {
2941
; CHECK-LABEL: @xor_logic_and_logic_or3(
3042
; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[C:%.*]]

0 commit comments

Comments
 (0)