[llvm] b326c05 - [InstSimplify] fold xor logic of 2 variables, part 2
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 24 05:15:58 PST 2021
Author: Sanjay Patel
Date: 2021-11-24T08:15:47-05:00
New Revision: b326c058146fbd5d89f7c8ce9fb932b3851200d7
URL: https://github.com/llvm/llvm-project/commit/b326c058146fbd5d89f7c8ce9fb932b3851200d7
DIFF: https://github.com/llvm/llvm-project/commit/b326c058146fbd5d89f7c8ce9fb932b3851200d7.diff
LOG: [InstSimplify] fold xor logic of 2 variables, part 2
(~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
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/xor.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 863533cf0cb08..a411c4338e235 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -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;
diff --git a/llvm/test/Transforms/InstSimplify/xor.ll b/llvm/test/Transforms/InstSimplify/xor.ll
index 615e02d94506f..f5062a53aa0e0 100644
--- a/llvm/test/Transforms/InstSimplify/xor.ll
+++ b/llvm/test/Transforms/InstSimplify/xor.ll
@@ -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
More information about the llvm-commits
mailing list