Skip to content

Commit 1c83a0b

Browse files
committed
[InstCombine] Add commuted variants for (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
Because xor is communative and associative, we can match all variants using m_combineOr.
1 parent 05ca207 commit 1c83a0b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,7 +2584,10 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
25842584

25852585
// (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
25862586
if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
2587-
match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) {
2587+
match(Op1, m_CombineOr(m_c_Xor(m_c_Xor(m_Specific(A), m_Value(C)),
2588+
m_Specific(B)),
2589+
m_c_Xor(m_c_Xor(m_Specific(B), m_Value(C)),
2590+
m_Specific(A))))) {
25882591
Value *NotC = Op1->hasOneUse()
25892592
? Builder.CreateNot(C)
25902593
: getFreelyInverted(C, C->hasOneUse(), &Builder);
@@ -2593,13 +2596,16 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
25932596
}
25942597

25952598
// ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2596-
if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))) &&
2597-
match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) {
2599+
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
2600+
match(Op0, m_CombineOr(m_c_Xor(m_c_Xor(m_Specific(A), m_Value(C)),
2601+
m_Specific(B)),
2602+
m_c_Xor(m_c_Xor(m_Specific(B), m_Value(C)),
2603+
m_Specific(A))))) {
25982604
Value *NotC = Op0->hasOneUse()
25992605
? Builder.CreateNot(C)
26002606
: getFreelyInverted(C, C->hasOneUse(), &Builder);
26012607
if (NotC != nullptr)
2602-
return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2608+
return BinaryOperator::CreateAnd(Op1, NotC);
26032609
}
26042610

26052611
// (A | B) & (~A ^ B) -> A & B

0 commit comments

Comments
 (0)