[InstSimplify] fold xor logic of 2 variables, part 2
authorSanjay Patel <spatel@rotateright.com>
Tue, 23 Nov 2021 22:19:12 +0000 (17:19 -0500)
committerSanjay Patel <spatel@rotateright.com>
Wed, 24 Nov 2021 13:15:47 +0000 (08:15 -0500)
(~a & b) ^ (a | b) --> a

This is the swapped and/or (Demorgan?) sibling fold for
the fold added with D114462 ( 892648b18a8c ).

This case is easier to specify because we are returning
a root value, not a 'not':
https://alive2.llvm.org/ce/z/SRzj4f

llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/xor.ll

index 863533cf0cb082b735e5652f437dc82092d926da..a411c4338e2355aef379ca83f991b13799cebbe1 100644 (file)
@@ -2414,23 +2414,30 @@ static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
       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;
index 615e02d94506fb671c78a958fda8dfc8abaca807..f5062a53aa0e0c2038ae6705ca8eb03e3c482a12 100644 (file)
@@ -158,11 +158,7 @@ define <2 x i4> @xor_and_or_not_undef_elt(<2 x i4> %a, <2 x i4> %b) {
 
 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
@@ -173,11 +169,7 @@ define i4 @xor_or_and_not_commute0(i4 %a, i4 %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
@@ -188,11 +180,7 @@ define <2 x i4> @xor_or_and_not_commute1(<2 x i4> %a, <2 x i4> %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
@@ -203,11 +191,7 @@ define i74 @xor_or_and_not_commute2(i74 %a, i74 %b) {
 
 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
@@ -218,11 +202,7 @@ define <2 x i4> @xor_or_and_not_commute3(<2 x i4> %a, <2 x i4> %b) {
 
 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
@@ -233,11 +213,7 @@ define i8 @xor_or_and_not_commute4(i8 %a, i8 %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
@@ -248,11 +224,7 @@ define i8 @xor_or_and_not_commute5(i8 %a, i8 %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
@@ -263,11 +235,7 @@ define i8 @xor_or_and_not_commute6(i8 %a, i8 %b) {
 
 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
@@ -311,11 +279,7 @@ define i4 @xor_or_and_not_wrong_val2(i4 %a, i4 %b, i4 %c) {
 
 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