Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2113,14 +2113,18 @@ } // (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::CreateOr(Op0, 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::CreateOr(Op1, C); Index: test/Transforms/InstCombine/or-xor.ll =================================================================== --- test/Transforms/InstCombine/or-xor.ll +++ test/Transforms/InstCombine/or-xor.ll @@ -136,3 +136,23 @@ ; CHECK-NEXT: %and = and i32 %x, %y ; CHECK-NEXT: ret i32 %and } + +define i32 @test14(i32 %x, i32 %y) { + %nega = xor i32 %x, -1 + %xor = xor i32 %nega, %y + %xor1 = xor i32 %x, %y + %or = or i32 %xor, %xor1 + ret i32 %or +; CHECK-LABEL: @test14( +; CHECK-NEXT: ret i32 -1 +} + +define i32 @test15(i32 %x, i32 %y) { + %xor = xor i32 %x, %y + %nega = xor i32 %x, -1 + %xor1 = xor i32 %nega, %y + %or = or i32 %xor, %xor1 + ret i32 %or +; CHECK-LABEL: @test15( +; CHECK-NEXT: ret i32 -1 +}