1,074 changes: 1,056 additions & 18 deletions llvm/test/Transforms/InstCombine/and-xor-or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,265 @@ define i32 @and_not_or_or_not_or_xor_use6(i32 %a, i32 %b, i32 %c) {
ret i32 %or3
}

; (a | ~(b & c)) & ~(a & (b ^ c)) --> ~(a | b) | (a ^ b ^ c)

define i32 @or_not_and_and_not_and_xor(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_commute1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_commute1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[C:%.*]], [[B:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %c, %b
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_commute2(i32 %a0, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_commute2(
; CHECK-NEXT: [[A:%.*]] = sdiv i32 42, [[A0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[A]], [[NOT1]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%a = sdiv i32 42, %a0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %a, %not1
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_commute3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_commute3(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[C]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %c, %b
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_commute4(i32 %a0, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_commute4(
; CHECK-NEXT: [[A:%.*]] = sdiv i32 42, [[A0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[A]], [[NOT1]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[A]], [[XOR1]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%a = sdiv i32 42, %a0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %a, %not1
%xor1 = xor i32 %b, %c
%and2 = and i32 %a, %xor1
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_commute5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_commute5(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %not2, %or1
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[AND1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %and1)
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use2(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %not1)
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use3(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[OR1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %or1)
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use4(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use4(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[XOR1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %xor1)
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use5(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[AND2]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %and2)
ret i32 %and3
}

define i32 @or_not_and_and_not_and_xor_use6(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor_use6(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[XOR1]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND2]], -1
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR1]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%not1 = xor i32 %and1, -1
%or1 = or i32 %not1, %a
%xor1 = xor i32 %b, %c
%and2 = and i32 %xor1, %a
%not2 = xor i32 %and2, -1
%and3 = and i32 %or1, %not2
call void @use(i32 %not2)
ret i32 %and3
}

; (~a & b & c) | ~(a | b | c) -> ~(a | (b ^ c))

define i32 @not_and_and_or_not_or_or(i32 %a, i32 %b, i32 %c) {
Expand Down Expand Up @@ -2665,27 +2924,348 @@ define i32 @not_and_and_or_not_or_or_use5(i32 %a, i32 %b, i32 %c) {
ret i32 %or3
}

define i32 @not_and_and_or_not_or_or_use6(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_and_or_not_or_or_use6(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR2]], -1
define i32 @not_and_and_or_not_or_or_use6(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_and_or_not_or_or_use6(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR2]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C]]
; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[AND2]])
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %b, %a
%or2 = or i32 %or1, %c
%not1 = xor i32 %or2, -1
%not2 = xor i32 %a, -1
%and1 = and i32 %not2, %b
%and2 = and i32 %and1, %c
%or3 = or i32 %and2, %not1
call void @use(i32 %and2)
ret i32 %or3
}

; (~a | b | c) & ~(a & b & c) -> ~a | (b ^ c)

define i32 @not_or_or_and_not_and_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute1_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute1_and(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[C:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[B:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %c, %a
%and2 = and i32 %and1, %b
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute2_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute2_and(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %c
%and2 = and i32 %and1, %a
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute1_or(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute1_or(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[B]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %c
%or2 = or i32 %or1, %b
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute2_or(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute2_or(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[C]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[NOT2]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %b, %c
%or2 = or i32 %or1, %not2
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %a, %b
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute2(i32 %a, i32 %b, i32 %c0) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute2(
; CHECK-NEXT: [[C:%.*]] = sdiv i32 42, [[C0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[C]], [[AND1]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%c = sdiv i32 42, %c0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %a
%and2 = and i32 %c, %and1
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute3(i32 %a, i32 %b0, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute3(
; CHECK-NEXT: [[B:%.*]] = sdiv i32 42, [[B0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[NOT2]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%b = sdiv i32 42, %b0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %b, %not2
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_commute4(i32 %a, i32 %b, i32 %c0) {
; CHECK-LABEL: @not_or_or_and_not_and_and_commute4(
; CHECK-NEXT: [[C:%.*]] = sdiv i32 42, [[C0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[C]], [[OR1]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND3]]
;
%c = sdiv i32 42, %c0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %c, %or1
%and3 = and i32 %or2, %not1
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[AND1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %and1)
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use2(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[AND2]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %and2)
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use3(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND2]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %not1)
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use4(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use4(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %not2)
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use5(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[OR1]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %or1)
ret i32 %and3
}

define i32 @not_or_or_and_not_and_and_use6(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_not_and_and_use6(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[C]]
; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[AND2]])
; CHECK-NEXT: ret i32 [[OR3]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C]]
; CHECK-NEXT: [[AND3:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[OR2]])
; CHECK-NEXT: ret i32 [[AND3]]
;
%or1 = or i32 %b, %a
%or2 = or i32 %or1, %c
%not1 = xor i32 %or2, -1
%not2 = xor i32 %a, -1
%and1 = and i32 %not2, %b
%and1 = and i32 %b, %a
%and2 = and i32 %and1, %c
%or3 = or i32 %and2, %not1
call void @use(i32 %and2)
ret i32 %or3
%not1 = xor i32 %and2, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and3 = and i32 %or2, %not1
call void @use(i32 %or2)
ret i32 %and3
}

; (~a & b & c) | ~(a | b) -> (c | ~b) & ~a
Expand Down Expand Up @@ -2976,6 +3556,289 @@ define i32 @not_and_and_or_no_or_use8(i32 %a, i32 %b, i32 %c) {
ret i32 %or2
}

; (~a | b | c) & ~(a & b) -> (c & ~b) | ~a

define i32 @not_or_or_and_no_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_commute1_or(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_commute1_or(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[C:%.*]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[NOT2]]
; CHECK-NEXT: [[AND2:%.*]] = xor i32 [[AND1]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %c, %b
%or2 = or i32 %or1, %not2
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_commute2_or(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_commute2_or(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[C:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = xor i32 [[AND1]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %c
%or2 = or i32 %or1, %b
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_commute1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_commute1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %a, %b
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_commute2(i32 %a, i32 %b0, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_commute2(
; CHECK-NEXT: [[B:%.*]] = sdiv i32 42, [[B0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[NOT2]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%b = sdiv i32 42, %b0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %b, %not2
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_commute3(i32 %a, i32 %b, i32 %c0) {
; CHECK-LABEL: @not_or_or_and_no_and_commute3(
; CHECK-NEXT: [[C:%.*]] = sdiv i32 42, [[C0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[C]], [[OR1]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: ret i32 [[AND2]]
;
%c = sdiv i32 42, %c0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %c, %or1
%and2 = and i32 %or2, %not1
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
call void @use(i32 %not2)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use2(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[C:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[NOT2]]
; CHECK-NEXT: [[AND2:%.*]] = xor i32 [[AND1]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %b, %c
%or2 = or i32 %or1, %not2
%and2 = and i32 %or2, %not1
call void @use(i32 %not2)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use3(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[C:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = xor i32 [[AND1]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %c
%or2 = or i32 %or1, %b
%and2 = and i32 %or2, %not1
call void @use(i32 %not2)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use4(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use4(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[C:%.*]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[B]]
; CHECK-NEXT: [[AND2:%.*]] = xor i32 [[AND1]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT2]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %c
%or2 = or i32 %or1, %b
%and2 = and i32 %or2, %not1
call void @use(i32 %not2)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use5(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[AND1]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
call void @use(i32 %and1)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use6(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use6(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
call void @use(i32 %not1)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use7(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use7(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[OR1]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
call void @use(i32 %or1)
ret i32 %and2
}

define i32 @not_or_or_and_no_and_use8(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_or_or_and_no_and_use8(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND1]], -1
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[A]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOT2]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[OR1]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[OR2]], [[NOT1]]
; CHECK-NEXT: call void @use(i32 [[OR2]])
; CHECK-NEXT: ret i32 [[AND2]]
;
%and1 = and i32 %b, %a
%not1 = xor i32 %and1, -1
%not2 = xor i32 %a, -1
%or1 = or i32 %not2, %b
%or2 = or i32 %or1, %c
%and2 = and i32 %or2, %not1
call void @use(i32 %or2)
ret i32 %and2
}

; (~(a | b) & c) | ~(a | (b | c)) -> ~(a | b)
; (~(a | b) & c) | ~(b | (a | c)) -> ~(a | b)

Expand Down Expand Up @@ -3156,3 +4019,178 @@ define i32 @not_or_and_or_not_or_or_use2(i32 %a, i32 %b, i32 %c) {
call void @use(i32 %not1)
ret i32 %or4
}

; (~(a & b) | c) & ~(a & (b & c)) -> ~(a & b)
; (~(a & b) | c) & ~(b & (a & c)) -> ~(a & b)

define i32 @not_and_or_and_not_and_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[A:%.*]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%and1 = and i32 %b, %c
%and2 = and i32 %and1, %a
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_commute1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_commute1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[B:%.*]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%and1 = and i32 %a, %c
%and2 = and i32 %and1, %b
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_commute2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_commute2(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[C:%.*]], [[B:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[A:%.*]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%and1 = and i32 %c, %b
%and2 = and i32 %and1, %a
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_commute3(i32 %a0, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_commute3(
; CHECK-NEXT: [[A:%.*]] = sdiv i32 42, [[A0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[A]], [[AND1]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[A]], [[B]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%a = sdiv i32 42, %a0 ; thwart complexity-based canonicalization
%and1 = and i32 %b, %c
%and2 = and i32 %a, %and1
%not1 = xor i32 %and2, -1
%and3 = and i32 %a, %b
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_commute4(i32 %a, i32 %b0, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_commute4(
; CHECK-NEXT: [[B:%.*]] = sdiv i32 42, [[B0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[B]], [[AND1]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%b = sdiv i32 42, %b0 ; thwart complexity-based canonicalization
%and1 = and i32 %a, %c
%and2 = and i32 %b, %and1
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_commute5(i32 %a, i32 %b, i32 %c0) {
; CHECK-LABEL: @not_and_or_and_not_and_and_commute5(
; CHECK-NEXT: [[C:%.*]] = sdiv i32 42, [[C0:%.*]]
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[C]], [[A:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[B:%.*]]
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[C]], [[NOT2]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: ret i32 [[AND4]]
;
%c = sdiv i32 42, %c0 ; thwart complexity-based canonicalization
%and1 = and i32 %c, %a
%and2 = and i32 %and1, %b
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %c, %not2
%and4 = and i32 %or2, %not1
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_use1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_use1(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[A:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND2]], -1
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[AND4]]
;
%and1 = and i32 %b, %c
%and2 = and i32 %and1, %a
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
call void @use(i32 %not1)
ret i32 %and4
}

define i32 @not_and_or_and_not_and_and_use2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @not_and_or_and_not_and_and_use2(
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], [[C:%.*]]
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[AND1]], [[B:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[AND2]], -1
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[B]], [[A]]
; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[AND3]], -1
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOT2]], [[C]]
; CHECK-NEXT: [[AND4:%.*]] = xor i32 [[AND2]], [[OR2]]
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[AND4]]
;
%and1 = and i32 %a, %c
%and2 = and i32 %and1, %b
%not1 = xor i32 %and2, -1
%and3 = and i32 %b, %a
%not2 = xor i32 %and3, -1
%or2 = or i32 %not2, %c
%and4 = and i32 %or2, %not1
call void @use(i32 %not1)
ret i32 %and4
}