[llvm] fdbf2bb - [InstSimplify] (x || y) && (x || !y) --> x
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 7 09:26:39 PDT 2021
Author: Sanjay Patel
Date: 2021-10-07T12:25:25-04:00
New Revision: fdbf2bb4eed187a7267780edee003483dee12739
URL: https://github.com/llvm/llvm-project/commit/fdbf2bb4eed187a7267780edee003483dee12739
DIFF: https://github.com/llvm/llvm-project/commit/fdbf2bb4eed187a7267780edee003483dee12739.diff
LOG: [InstSimplify] (x || y) && (x || !y) --> x
https://alive2.llvm.org/ce/z/4BE33w
This is the logical (select-form) equivalent of the bitwise logic fold:
e36d351d19b1
This is another part of solving the regression from:
https://llvm.org/PR52077
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/select-logical.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 0f114b50bf9c..97cb0c9c86c0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4254,14 +4254,27 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
return FalseVal;
}
- // select i1 Cond, i1 true, i1 false --> i1 Cond
assert(Cond->getType()->isIntOrIntVectorTy(1) &&
"Select must have bool or bool vector condition");
assert(TrueVal->getType() == FalseVal->getType() &&
"Select must have same types for true/false ops");
- if (Cond->getType() == TrueVal->getType() &&
- match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
- return Cond;
+
+ if (Cond->getType() == TrueVal->getType()) {
+ // select i1 Cond, i1 true, i1 false --> i1 Cond
+ if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
+ return Cond;
+
+ // (X || Y) && (X || !Y) --> X (commuted 8 ways)
+ Value *X, *Y;
+ if (match(FalseVal, m_ZeroInt())) {
+ if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
+ match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
+ return X;
+ if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
+ match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
+ return X;
+ }
+ }
// select ?, X, X -> X
if (TrueVal == FalseVal)
diff --git a/llvm/test/Transforms/InstSimplify/select-logical.ll b/llvm/test/Transforms/InstSimplify/select-logical.ll
index 0ca0f8f69ea5..28ae8c41069c 100644
--- a/llvm/test/Transforms/InstSimplify/select-logical.ll
+++ b/llvm/test/Transforms/InstSimplify/select-logical.ll
@@ -3,11 +3,7 @@
define i1 @logical_and_of_or_commute0(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute0(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[X]], i1 true, i1 [[YNOT]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORY]], i1 [[XORYNOT]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %x, i1 true, i1 %y
@@ -18,11 +14,7 @@ define i1 @logical_and_of_or_commute0(i1 %x, i1 %y) {
define <2 x i1> @logical_and_of_or_commute1(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @logical_and_of_or_commute1(
-; CHECK-NEXT: [[YNOT:%.*]] = xor <2 x i1> [[Y:%.*]], <i1 true, i1 poison>
-; CHECK-NEXT: [[XORY:%.*]] = select <2 x i1> [[Y]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[X:%.*]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select <2 x i1> [[X]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[YNOT]]
-; CHECK-NEXT: [[AND:%.*]] = select <2 x i1> [[XORY]], <2 x i1> [[XORYNOT]], <2 x i1> zeroinitializer
-; CHECK-NEXT: ret <2 x i1> [[AND]]
+; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
;
%ynot = xor <2 x i1> %y, <i1 -1, i1 poison>
%xory = select <2 x i1> %y, <2 x i1> <i1 true, i1 true>, <2 x i1> %x
@@ -33,11 +25,7 @@ define <2 x i1> @logical_and_of_or_commute1(<2 x i1> %x, <2 x i1> %y) {
define i1 @logical_and_of_or_commute2(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute2(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORY]], i1 [[XORYNOT]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %x, i1 true, i1 %y
@@ -48,11 +36,7 @@ define i1 @logical_and_of_or_commute2(i1 %x, i1 %y) {
define i1 @logical_and_of_or_commute3(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute3(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[Y]], i1 true, i1 [[X:%.*]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORY]], i1 [[XORYNOT]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %y, i1 true, i1 %x
@@ -63,11 +47,7 @@ define i1 @logical_and_of_or_commute3(i1 %x, i1 %y) {
define i1 @logical_and_of_or_commute4(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute4(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[X]], i1 true, i1 [[YNOT]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORY]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %x, i1 true, i1 %y
@@ -78,11 +58,7 @@ define i1 @logical_and_of_or_commute4(i1 %x, i1 %y) {
define i1 @logical_and_of_or_commute5(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute5(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[Y]], i1 true, i1 [[X:%.*]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[X]], i1 true, i1 [[YNOT]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORY]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %y, i1 true, i1 %x
@@ -93,11 +69,7 @@ define i1 @logical_and_of_or_commute5(i1 %x, i1 %y) {
define i1 @logical_and_of_or_commute6(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute6(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORY]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %x, i1 true, i1 %y
@@ -108,11 +80,7 @@ define i1 @logical_and_of_or_commute6(i1 %x, i1 %y) {
define i1 @logical_and_of_or_commute7(i1 %x, i1 %y) {
; CHECK-LABEL: @logical_and_of_or_commute7(
-; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
-; CHECK-NEXT: [[XORY:%.*]] = select i1 [[Y]], i1 true, i1 [[X:%.*]]
-; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
-; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORY]], i1 false
-; CHECK-NEXT: ret i1 [[AND]]
+; CHECK-NEXT: ret i1 [[X:%.*]]
;
%ynot = xor i1 %y, -1
%xory = select i1 %y, i1 true, i1 %x
@@ -120,3 +88,37 @@ define i1 @logical_and_of_or_commute7(i1 %x, i1 %y) {
%and = select i1 %xorynot, i1 %xory, i1 false
ret i1 %and
}
+
+; negative test - wrong logic op
+
+define i1 @logical_and_of_or_and(i1 %x, i1 %y) {
+; CHECK-LABEL: @logical_and_of_or_and(
+; CHECK-NEXT: [[XANDY:%.*]] = select i1 [[Y:%.*]], i1 [[X:%.*]], i1 false
+; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y]], true
+; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[YNOT]], i1 true, i1 [[X]]
+; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XANDY]], i1 false
+; CHECK-NEXT: ret i1 [[AND]]
+;
+ %xandy = select i1 %y, i1 %x, i1 false
+ %ynot = xor i1 %y, -1
+ %xorynot = select i1 %ynot, i1 true, i1 %x
+ %and = select i1 %xorynot, i1 %xandy, i1 false
+ ret i1 %and
+}
+
+; negative test - must have common operands
+
+define i1 @logical_and_of_or_no_common_op(i1 %x, i1 %y, i1 %z) {
+; CHECK-LABEL: @logical_and_of_or_no_common_op(
+; CHECK-NEXT: [[XORZ:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Z:%.*]]
+; CHECK-NEXT: [[YNOT:%.*]] = xor i1 [[Y:%.*]], true
+; CHECK-NEXT: [[XORYNOT:%.*]] = select i1 [[X]], i1 true, i1 [[YNOT]]
+; CHECK-NEXT: [[AND:%.*]] = select i1 [[XORYNOT]], i1 [[XORZ]], i1 false
+; CHECK-NEXT: ret i1 [[AND]]
+;
+ %xorz = select i1 %x, i1 true, i1 %z
+ %ynot = xor i1 %y, -1
+ %xorynot = select i1 %x, i1 true, i1 %ynot
+ %and = select i1 %xorynot, i1 %xorz, i1 false
+ ret i1 %and
+}
More information about the llvm-commits
mailing list