Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2796,6 +2796,18 @@ if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A)))) return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C)); + // (~(A | B) & C) | ~(A | C) --> ~((B & C) | A) + if (match(Op0, m_OneUse(m_c_And(m_OneUse(m_Not(m_Or(m_Value(A), m_Value(B)))), + m_Value(C))))) { + if (match(Op1, m_OneUse(m_Not(m_c_Or(m_Specific(A), m_Specific(C)))))) + return BinaryOperator::CreateNot( + Builder.CreateOr(Builder.CreateAnd(B, C), A)); + + if (match(Op1, m_OneUse(m_Not(m_c_Or(m_Specific(B), m_Specific(C)))))) + return BinaryOperator::CreateNot( + Builder.CreateOr(Builder.CreateAnd(A, C), B)); + } + if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) return DeMorgan; Index: llvm/test/Transforms/InstCombine/and-xor-or.ll =================================================================== --- llvm/test/Transforms/InstCombine/and-xor-or.ll +++ llvm/test/Transforms/InstCombine/and-xor-or.ll @@ -927,12 +927,9 @@ define i32 @or_and_not_not(i32 %a, i32 %b, i32 %c) { ; CHECK-LABEL: @or_and_not_not( -; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] -; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1 -; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C:%.*]] -; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[NOT2]], [[B]] -; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND]], [[NOT1]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 ; CHECK-NEXT: ret i32 [[OR3]] ; %or1 = or i32 %b, %a @@ -946,12 +943,9 @@ define i32 @or_and_not_not_commute1(i32 %a, i32 %b, i32 %c) { ; CHECK-LABEL: @or_and_not_not_commute1( -; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] -; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1 -; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C:%.*]] -; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[NOT2]], [[B]] -; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND]], [[NOT1]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 ; CHECK-NEXT: ret i32 [[OR3]] ; %or1 = or i32 %b, %a @@ -965,16 +959,45 @@ define i32 @or_and_not_not_commute2(i32 %a, i32 %b, i32 %c) { ; CHECK-LABEL: @or_and_not_not_commute2( -; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] -; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1 -; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C:%.*]] -; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1 -; CHECK-NEXT: [[AND:%.*]] = and i32 [[NOT2]], [[B]] -; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND]], [[NOT1]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %a, %c + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + ret i32 %or3 +} + +define i32 @or_and_not_not_commute4(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_commute4( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 ; CHECK-NEXT: ret i32 [[OR3]] ; %or1 = or i32 %b, %a %not1 = xor i32 %or1, -1 + %or2 = or i32 %c, %a + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + ret i32 %or3 +} + +define i32 @or_and_not_not_commute5(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_commute5( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %a, %b + %not1 = xor i32 %or1, -1 %or2 = or i32 %a, %c %not2 = xor i32 %or2, -1 %and = and i32 %not2, %b @@ -982,6 +1005,54 @@ ret i32 %or3 } +define i32 @or_and_not_not_commute6(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_commute6( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %a, %c + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %not1, %and + ret i32 %or3 +} + +define i32 @or_and_not_not_commute7(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_commute7( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %c, %a + %not2 = xor i32 %or2, -1 + %and = and i32 %b, %not2 + %or3 = or i32 %and, %not1 + ret i32 %or3 +} + +define i32 @or_and_not_not_commute8(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_commute8( +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A:%.*]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %a, %b + %not1 = xor i32 %or1, -1 + %or2 = or i32 %c, %a + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + ret i32 %or3 +} + define i32 @or_and_not_not_extra_not_use1(i32 %a, i32 %b, i32 %c) { ; CHECK-LABEL: @or_and_not_not_extra_not_use1( ; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] @@ -1024,6 +1095,65 @@ ret i32 %or3 } +define i32 @or_and_not_not_extra_and_use(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_extra_and_use( +; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] +; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1 +; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C:%.*]] +; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1 +; CHECK-NEXT: [[AND:%.*]] = and i32 [[NOT2]], [[B]] +; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND]], [[NOT1]] +; CHECK-NEXT: call void @use(i32 [[AND]]) +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %a, %c + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + call void @use(i32 %and) + ret i32 %or3 +} + +define i32 @or_and_not_not_extra_or_use1(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_extra_or_use1( +; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C:%.*]], [[B]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: call void @use(i32 [[OR1]]) +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %a, %c + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + call void @use(i32 %or1) + ret i32 %or3 +} + +define i32 @or_and_not_not_extra_or_use2(i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: @or_and_not_not_extra_or_use2( +; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A:%.*]], [[C:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[C]], [[B:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], [[A]] +; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP2]], -1 +; CHECK-NEXT: call void @use(i32 [[OR2]]) +; CHECK-NEXT: ret i32 [[OR3]] +; + %or1 = or i32 %b, %a + %not1 = xor i32 %or1, -1 + %or2 = or i32 %a, %c + %not2 = xor i32 %or2, -1 + %and = and i32 %not2, %b + %or3 = or i32 %and, %not1 + call void @use(i32 %or2) + ret i32 %or3 +} + define i32 @or_and_not_not_wrong_a(i32 %a, i32 %b, i32 %c, i32 %d) { ; CHECK-LABEL: @or_and_not_not_wrong_a( ; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[D:%.*]]