[llvm] 4b30076 - [InstSimplify] add logic fold for 'or'
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 30 11:15:19 PST 2021
Author: Sanjay Patel
Date: 2021-11-30T14:08:54-05:00
New Revision: 4b30076f16fc55891da8952bda3f2d81dbeec5a5
URL: https://github.com/llvm/llvm-project/commit/4b30076f16fc55891da8952bda3f2d81dbeec5a5
DIFF: https://github.com/llvm/llvm-project/commit/4b30076f16fc55891da8952bda3f2d81dbeec5a5.diff
LOG: [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'.
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/or.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 1f568ad6e839..9eb1ccd823de 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -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;
}
diff --git a/llvm/test/Transforms/InstSimplify/or.ll b/llvm/test/Transforms/InstSimplify/or.ll
index 1d9d741fef2b..e73f6d273dc1 100644
--- a/llvm/test/Transforms/InstSimplify/or.ll
+++ b/llvm/test/Transforms/InstSimplify/or.ll
@@ -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
More information about the llvm-commits
mailing list