[InstCombine] reduce code duplication with commutative matcher; NFC

This commit is contained in:
Sanjay Patel 2021-10-31 12:12:37 -04:00
parent 701923a60f
commit 511ee8759f
1 changed files with 14 additions and 18 deletions

View File

@ -2728,28 +2728,24 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
// (A & C1)|(B & C2) // (A & C1)|(B & C2)
ConstantInt *C1, *C2; ConstantInt *C1, *C2;
if (match(C, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2))) { if (match(C, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2))) {
Value *V1 = nullptr, *V2 = nullptr; Value *N;
if ((C1->getValue() & C2->getValue()).isZero()) { if ((C1->getValue() & C2->getValue()).isZero()) {
// ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2) // ((B | N) & C1) | (B & C2) --> (B | N) & (C1 | C2)
// iff (C1&C2) == 0 and (N&~C1) == 0 // iff (C1 & C2) == 0 and (N & ~C1) == 0
if (match(A, m_Or(m_Value(V1), m_Value(V2))) && if (match(A, m_c_Or(m_Specific(B), m_Value(N))) &&
((V1 == B && MaskedValueIsZero(N, ~C1->getValue(), 0, &I))
MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N) return BinaryOperator::CreateAnd(
(V2 == B && A, Builder.getInt(C1->getValue() | C2->getValue()));
MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V) // (A & C1) | ((A | N) & C2) --> (A | N) & (C1 | C2)
return BinaryOperator::CreateAnd(A, // iff (C1 & C2) == 0 and (N & ~C1) == 0
Builder.getInt(C1->getValue()|C2->getValue())); if (match(B, m_c_Or(m_Specific(A), m_Value(N))) &&
// Or commutes, try both ways. MaskedValueIsZero(N, ~C2->getValue(), 0, &I))
if (match(B, m_Or(m_Value(V1), m_Value(V2))) && return BinaryOperator::CreateAnd(
((V1 == A && B, Builder.getInt(C1->getValue() | C2->getValue()));
MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N)
(V2 == A &&
MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V)
return BinaryOperator::CreateAnd(B,
Builder.getInt(C1->getValue()|C2->getValue()));
// ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2) // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2)
// iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0. // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0.
Value *V1 = nullptr, *V2 = nullptr;
ConstantInt *C3 = nullptr, *C4 = nullptr; ConstantInt *C3 = nullptr, *C4 = nullptr;
if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) && if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) &&
(C3->getValue() & ~C1->getValue()).isZero() && (C3->getValue() & ~C1->getValue()).isZero() &&