Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1431,11 +1431,11 @@ Z2 = dyn_cast(B2); B = B1; } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) { Z1 = dyn_cast(B1); B = B2; - Z2 = dyn_cast(A2); A = A1; + Z2 = dyn_cast(A2); A = A1; } - - if (Z1 && Z2 && - (I.hasNoSignedZeros() || + + if (Z1 && Z2 && + (I.hasNoSignedZeros() || (Z1->isNegativeZeroValue() && Z2->isNegativeZeroValue()))) { return SelectInst::Create(C, A, B); } @@ -1621,6 +1621,15 @@ return BinaryOperator::CreateNeg(Y); } + // (sub (or A, B) (xor A, B)) --> (and A, B) + { + Value *A = nullptr, *B = nullptr; + if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && + (match(Op0, m_Or(m_Specific(A), m_Specific(B))) || + match(Op0, m_Or(m_Specific(B), m_Specific(A))))) + return BinaryOperator::CreateAnd(A, B); + } + if (Op1->hasOneUse()) { Value *X = nullptr, *Y = nullptr, *Z = nullptr; Constant *C = nullptr; Index: test/Transforms/InstCombine/sub.ll =================================================================== --- test/Transforms/InstCombine/sub.ll +++ test/Transforms/InstCombine/sub.ll @@ -529,3 +529,13 @@ ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 %x, -32768 ; CHECK: ret i32 [[ADD]] } + +define i32 @test45(i32 %x, i32 %y) { + %or = or i32 %x, %y + %xor = xor i32 %x, %y + %sub = sub i32 %or, %xor + ret i32 %sub +; CHECK-LABEL: @test45( +; CHECK-NEXT: %sub = and i32 %x, %y +; CHECK: ret i32 %sub +}