match(Op1, m_Not(m_Specific(Op0))))
return Constant::getAllOnesValue(Op0->getType());
- // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
- // The 'not' op must contain a complete -1 operand (no undef elements for
- // vector) for the transform to be safe.
- auto matchNotA = [](Value *X, Value *Y) -> Value * {
- Value *A, *B, *NotA;
+ auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
+ Value *A, *B;
+ // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
+ if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
+ match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
+ return A;
+
+ // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
+ // The 'not' op must contain a complete -1 operand (no undef elements for
+ // vector) for the transform to be safe.
+ Value *NotA;
const APInt *C;
if (match(X, m_c_Or(m_CombineAnd(m_Xor(m_Value(A), m_APIntForbidUndef(C)),
m_Value(NotA)),
m_Value(B))) &&
match(Y, m_c_And(m_Specific(A), m_Specific(B))) && C->isAllOnes())
return NotA;
+
return nullptr;
};
- if (Value *NotA = matchNotA(Op0, Op1))
- return NotA;
- if (Value *NotA = matchNotA(Op1, Op0))
- return NotA;
+ if (Value *R = foldAndOrNot(Op0, Op1))
+ return R;
+ if (Value *R = foldAndOrNot(Op1, Op0))
+ return R;
if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
return V;
define i4 @xor_or_and_not_commute0(i4 %a, i4 %b) {
; CHECK-LABEL: @xor_or_and_not_commute0(
-; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i4 [[NOT]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = or i4 [[A]], [[B]]
-; CHECK-NEXT: [[R:%.*]] = xor i4 [[AND]], [[OR]]
-; CHECK-NEXT: ret i4 [[R]]
+; CHECK-NEXT: ret i4 [[A:%.*]]
;
%not = xor i4 %a, -1
%and = and i4 %not, %b
define <2 x i4> @xor_or_and_not_commute1(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @xor_or_and_not_commute1(
-; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[A:%.*]], <i4 -1, i4 -1>
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[NOT]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[A]], [[B]]
-; CHECK-NEXT: [[R:%.*]] = xor <2 x i4> [[OR]], [[AND]]
-; CHECK-NEXT: ret <2 x i4> [[R]]
+; CHECK-NEXT: ret <2 x i4> [[A:%.*]]
;
%not = xor <2 x i4> %a, <i4 -1, i4 -1>
%and = and <2 x i4> %not, %b
define i74 @xor_or_and_not_commute2(i74 %a, i74 %b) {
; CHECK-LABEL: @xor_or_and_not_commute2(
-; CHECK-NEXT: [[NOT:%.*]] = xor i74 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i74 [[B:%.*]], [[NOT]]
-; CHECK-NEXT: [[OR:%.*]] = or i74 [[A]], [[B]]
-; CHECK-NEXT: [[R:%.*]] = xor i74 [[AND]], [[OR]]
-; CHECK-NEXT: ret i74 [[R]]
+; CHECK-NEXT: ret i74 [[A:%.*]]
;
%not = xor i74 %a, -1
%and = and i74 %b, %not
define <2 x i4> @xor_or_and_not_commute3(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @xor_or_and_not_commute3(
-; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[A:%.*]], <i4 -1, i4 -1>
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[NOT]]
-; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[A]], [[B]]
-; CHECK-NEXT: [[R:%.*]] = xor <2 x i4> [[OR]], [[AND]]
-; CHECK-NEXT: ret <2 x i4> [[R]]
+; CHECK-NEXT: ret <2 x i4> [[A:%.*]]
;
%not = xor <2 x i4> %a, <i4 -1, i4 -1>
%and = and <2 x i4> %b, %not
define i8 @xor_or_and_not_commute4(i8 %a, i8 %b) {
; CHECK-LABEL: @xor_or_and_not_commute4(
-; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i8 [[NOT]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = or i8 [[B]], [[A]]
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[AND]], [[OR]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[A:%.*]]
;
%not = xor i8 %a, -1
%and = and i8 %not, %b
define i8 @xor_or_and_not_commute5(i8 %a, i8 %b) {
; CHECK-LABEL: @xor_or_and_not_commute5(
-; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i8 [[NOT]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = or i8 [[B]], [[A]]
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[OR]], [[AND]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[A:%.*]]
;
%not = xor i8 %a, -1
%and = and i8 %not, %b
define i8 @xor_or_and_not_commute6(i8 %a, i8 %b) {
; CHECK-LABEL: @xor_or_and_not_commute6(
-; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i8 [[B:%.*]], [[NOT]]
-; CHECK-NEXT: [[OR:%.*]] = or i8 [[B]], [[A]]
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[AND]], [[OR]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[A:%.*]]
;
%not = xor i8 %a, -1
%and = and i8 %b, %not
define i8 @xor_or_and_not_commute7(i8 %a, i8 %b) {
; CHECK-LABEL: @xor_or_and_not_commute7(
-; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[A:%.*]], -1
-; CHECK-NEXT: [[AND:%.*]] = and i8 [[B:%.*]], [[NOT]]
-; CHECK-NEXT: [[OR:%.*]] = or i8 [[B]], [[A]]
-; CHECK-NEXT: [[R:%.*]] = xor i8 [[OR]], [[AND]]
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[A:%.*]]
;
%not = xor i8 %a, -1
%and = and i8 %b, %not
define <2 x i4> @xor_or_and_not_undef_elt(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @xor_or_and_not_undef_elt(
-; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[A:%.*]], <i4 -1, i4 undef>
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[NOT]]
-; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[A]], [[B]]
-; CHECK-NEXT: [[R:%.*]] = xor <2 x i4> [[OR]], [[AND]]
-; CHECK-NEXT: ret <2 x i4> [[R]]
+; CHECK-NEXT: ret <2 x i4> [[A:%.*]]
;
%not = xor <2 x i4> %a, <i4 -1, i4 undef>
%and = and <2 x i4> %b, %not