Skip to content

Commit 1d6458f

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 9e1eaff commit 1d6458f

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
@@ -2697,7 +2697,10 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
26972697

26982698
// (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
26992699
if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
2700-
match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) {
2700+
match(Op1, m_CombineOr(m_c_Xor(m_c_Xor(m_Specific(A), m_Value(C)),
2701+
m_Specific(B)),
2702+
m_c_Xor(m_c_Xor(m_Specific(B), m_Value(C)),
2703+
m_Specific(A))))) {
27012704
Value *NotC = Op1->hasOneUse()
27022705
? Builder.CreateNot(C)
27032706
: getFreelyInverted(C, C->hasOneUse(), &Builder);
@@ -2706,13 +2709,16 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
27062709
}
27072710

27082711
// ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2709-
if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))) &&
2710-
match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) {
2712+
if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
2713+
match(Op0, m_CombineOr(m_c_Xor(m_c_Xor(m_Specific(A), m_Value(C)),
2714+
m_Specific(B)),
2715+
m_c_Xor(m_c_Xor(m_Specific(B), m_Value(C)),
2716+
m_Specific(A))))) {
27112717
Value *NotC = Op0->hasOneUse()
27122718
? Builder.CreateNot(C)
27132719
: getFreelyInverted(C, C->hasOneUse(), &Builder);
27142720
if (NotC != nullptr)
2715-
return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2721+
return BinaryOperator::CreateAnd(Op1, NotC);
27162722
}
27172723

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

0 commit comments

Comments
 (0)