[InstSimplify] add logic fold for 'or'

https://alive2.llvm.org/ce/z/4PaPDy

There's a related fold where the inner 'or' is replaced by 'and',
but that needs to be more careful about matching a 'not'.
This commit is contained in:
Sanjay Patel
2021-11-30 13:59:39 -05:00
parent c49ef1448d
commit 4b30076f16
2 changed files with 19 additions and 25 deletions

View File

@@ -2220,6 +2220,12 @@ static Value *simplifyOrLogic(Value *X, Value *Y) {
match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
return Y;
// ~(A ^ B) | (A | B) --> -1
// ~(A ^ B) | (B | A) --> -1
if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
return ConstantInt::getAllOnesValue(Ty);
return nullptr;
}

View File

@@ -748,13 +748,11 @@ define <2 x i4> @or_nxor_and_undef_elt(<2 x i4> %a, <2 x i4> %b) {
ret <2 x i4> %r
}
; ~(A ^ B) | (A | B) --> -1
define i4 @or_nxor_or_commute0(i4 %a, i4 %b) {
; CHECK-LABEL: @or_nxor_or_commute0(
; CHECK-NEXT: [[OR:%.*]] = or i4 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A]], [[B]]
; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
; CHECK-NEXT: [[R:%.*]] = or i4 [[NOT]], [[OR]]
; CHECK-NEXT: ret i4 [[R]]
; CHECK-NEXT: ret i4 -1
;
%or = or i4 %a, %b
%xor = xor i4 %a, %b
@@ -765,11 +763,7 @@ define i4 @or_nxor_or_commute0(i4 %a, i4 %b) {
define <2 x i4> @or_nxor_or_commute1(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @or_nxor_or_commute1(
; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A]], [[B]]
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 -1>
; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[OR]], [[NOT]]
; CHECK-NEXT: ret <2 x i4> [[R]]
; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
;
%or = or <2 x i4> %a, %b
%xor = xor <2 x i4> %a, %b
@@ -780,11 +774,7 @@ define <2 x i4> @or_nxor_or_commute1(<2 x i4> %a, <2 x i4> %b) {
define i74 @or_nxor_or_commute2(i74 %a, i74 %b) {
; CHECK-LABEL: @or_nxor_or_commute2(
; CHECK-NEXT: [[OR:%.*]] = or i74 [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[XOR:%.*]] = xor i74 [[A]], [[B]]
; CHECK-NEXT: [[NOT:%.*]] = xor i74 [[XOR]], -1
; CHECK-NEXT: [[R:%.*]] = or i74 [[NOT]], [[OR]]
; CHECK-NEXT: ret i74 [[R]]
; CHECK-NEXT: ret i74 -1
;
%or = or i74 %b, %a
%xor = xor i74 %a, %b
@@ -795,11 +785,7 @@ define i74 @or_nxor_or_commute2(i74 %a, i74 %b) {
define <2 x i4> @or_nxor_or_commute3(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @or_nxor_or_commute3(
; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A]], [[B]]
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 -1>
; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[OR]], [[NOT]]
; CHECK-NEXT: ret <2 x i4> [[R]]
; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
;
%or = or <2 x i4> %b, %a
%xor = xor <2 x i4> %a, %b
@@ -808,6 +794,8 @@ define <2 x i4> @or_nxor_or_commute3(<2 x i4> %a, <2 x i4> %b) {
ret <2 x i4> %r
}
; negative test - must have common operands
define i4 @or_nxor_or_wrong_val1(i4 %a, i4 %b, i4 %c) {
; CHECK-LABEL: @or_nxor_or_wrong_val1(
; CHECK-NEXT: [[OR:%.*]] = or i4 [[A:%.*]], [[C:%.*]]
@@ -823,6 +811,8 @@ define i4 @or_nxor_or_wrong_val1(i4 %a, i4 %b, i4 %c) {
ret i4 %r
}
; negative test - must have common operands
define i4 @or_nxor_or_wrong_val2(i4 %a, i4 %b, i4 %c) {
; CHECK-LABEL: @or_nxor_or_wrong_val2(
; CHECK-NEXT: [[OR:%.*]] = or i4 [[C:%.*]], [[B:%.*]]
@@ -838,13 +828,11 @@ define i4 @or_nxor_or_wrong_val2(i4 %a, i4 %b, i4 %c) {
ret i4 %r
}
; undef in 'not' is allowed
define <2 x i4> @or_nxor_or_undef_elt(<2 x i4> %a, <2 x i4> %b) {
; CHECK-LABEL: @or_nxor_or_undef_elt(
; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[B:%.*]], [[A:%.*]]
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A]], [[B]]
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 undef>
; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[OR]], [[NOT]]
; CHECK-NEXT: ret <2 x i4> [[R]]
; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
;
%or = or <2 x i4> %b, %a
%xor = xor <2 x i4> %a, %b