Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1285,14 +1285,18 @@ return BinaryOperator::CreateAnd(A, Op0); // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C - if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) - if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) + // (B ^ A) & ((B ^ C) ^ A) -> (A ^ B) & ~C + if (match(Op1, m_Xor(m_Xor(m_Value(B), m_Value(C)), m_Value(A)))) + if (match(Op0, m_Xor(m_Specific(A), m_Specific(B))) || + match(Op0, m_Xor(m_Specific(B), m_Specific(A)))) if (Op1->hasOneUse() || cast(Op1)->hasOneUse()) return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C)); // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C + // ((A ^ C) ^ B) & (A ^ B) -> (B ^ A) & ~C if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) - if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) + if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))) || + match(Op1, m_Xor(m_Specific(A), m_Specific(B)))) if (Op0->hasOneUse() || cast(Op0)->hasOneUse()) return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C)); Index: test/Transforms/InstCombine/xor2.ll =================================================================== --- test/Transforms/InstCombine/xor2.ll +++ test/Transforms/InstCombine/xor2.ll @@ -167,3 +167,23 @@ ; CHECK-NEXT: %1 = and i32 %a, %b ; CHECK-NEXT: %xor = xor i32 %1, -1 } + +define i32 @test15(i32 %x, i32 %y) { + %nega = xor i32 %x, -1 + %xor = xor i32 %nega, %y + %xor1 = xor i32 %x, %y + %and = and i32 %xor, %xor1 + ret i32 %and +; CHECK-LABEL: @test15( +; CHECK-NEXT: ret i32 0 +} + +define i32 @test16(i32 %x, i32 %y) { + %xor = xor i32 %x, %y + %nega = xor i32 %x, -1 + %xor1 = xor i32 %nega, %y + %and = and i32 %xor, %xor1 + ret i32 %and +; CHECK-LABEL: @test16( +; CHECK-NEXT: ret i32 0 +}