[llvm] 9444252 - [InstSimplify] with poison-safe logical ops: (X && Y) || X --> X
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 20 14:36:51 PST 2023
Author: Sanjay Patel
Date: 2023-01-20T17:35:24-05:00
New Revision: 9444252a674df5952bb5af2b76348ae4b45a263d
URL: https://github.com/llvm/llvm-project/commit/9444252a674df5952bb5af2b76348ae4b45a263d
DIFF: https://github.com/llvm/llvm-project/commit/9444252a674df5952bb5af2b76348ae4b45a263d.diff
LOG: [InstSimplify] with poison-safe logical ops: (X && Y) || X --> X
https://alive2.llvm.org/ce/z/ptZcJH
issue #60167
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
llvm/test/Transforms/InstSimplify/select-inseltpoison.ll
llvm/test/Transforms/InstSimplify/select-logical.ll
llvm/test/Transforms/InstSimplify/select.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 3f3f6c6e0d1a9..4854b68a8c975 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4607,6 +4607,15 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
return X;
}
+
+ if (match(TrueVal, m_One())) {
+ // (X && Y) || Y --> Y (commuted 2 ways)
+ if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value())))
+ return FalseVal;
+ // Y || (X && Y) --> Y (commuted 2 ways)
+ if (match(FalseVal, m_c_LogicalAnd(m_Specific(Cond), m_Value())))
+ return Cond;
+ }
}
// select ?, X, X -> X
diff --git a/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll b/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
index 3366775ee4ea6..98e2689831d18 100644
--- a/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
+++ b/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
@@ -57,9 +57,7 @@ define i1 @land_lor_left1(i1 %A, i1 %B) {
}
define i1 @land_lor_left2(i1 %A, i1 %B) {
; CHECK-LABEL: @land_lor_left2(
-; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 [[A:%.*]], i1 false
-; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i1 true, i1 [[A]]
-; CHECK-NEXT: ret i1 [[RES]]
+; CHECK-NEXT: ret i1 [[A:%.*]]
;
%c = select i1 %B, i1 %A, i1 false
%res = select i1 %c, i1 true, i1 %A
diff --git a/llvm/test/Transforms/InstSimplify/select-inseltpoison.ll b/llvm/test/Transforms/InstSimplify/select-inseltpoison.ll
index 9511b6993ddad..5b2f31a4b1a15 100644
--- a/llvm/test/Transforms/InstSimplify/select-inseltpoison.ll
+++ b/llvm/test/Transforms/InstSimplify/select-inseltpoison.ll
@@ -684,9 +684,7 @@ define i1 @or_cmps(float %x) {
define <2 x i1> @or_logic_vector(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_logic_vector(
-; CHECK-NEXT: [[A:%.*]] = and <2 x i1> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[X]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[A]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
;
%a = and <2 x i1> %x, %y
%r = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %a
diff --git a/llvm/test/Transforms/InstSimplify/select-logical.ll b/llvm/test/Transforms/InstSimplify/select-logical.ll
index 4819ade6ae44e..0d019dfb77304 100644
--- a/llvm/test/Transforms/InstSimplify/select-logical.ll
+++ b/llvm/test/Transforms/InstSimplify/select-logical.ll
@@ -588,9 +588,7 @@ define i1 @always_false_same_op(i1 %x) {
define i1 @or_and_common_op_commute0(i1 %x, i1 %y) {
; CHECK-LABEL: @or_and_common_op_commute0(
-; CHECK-NEXT: [[A:%.*]] = select i1 [[X:%.*]], i1 [[Y:%.*]], i1 false
-; CHECK-NEXT: [[R:%.*]] = select i1 [[A]], i1 true, i1 [[Y]]
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 [[Y:%.*]]
;
%a = select i1 %x, i1 %y, i1 false
%r = select i1 %a, i1 true, i1 %y
@@ -599,9 +597,7 @@ define i1 @or_and_common_op_commute0(i1 %x, i1 %y) {
define <2 x i1> @or_and_common_op_commute1(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_and_common_op_commute1(
-; CHECK-NEXT: [[A:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> [[X:%.*]], <2 x i1> zeroinitializer
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[A]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[Y]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> [[Y:%.*]]
;
%a = select <2 x i1> %y, <2 x i1> %x, <2 x i1> zeroinitializer
%r = select <2 x i1> %a, <2 x i1> <i1 true, i1 true>, <2 x i1> %y
@@ -610,6 +606,17 @@ define <2 x i1> @or_and_common_op_commute1(<2 x i1> %x, <2 x i1> %y) {
define <2 x i1> @or_and_common_op_commute2(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_and_common_op_commute2(
+; CHECK-NEXT: ret <2 x i1> [[Y:%.*]]
+;
+ %a = select <2 x i1> %x, <2 x i1> %y, <2 x i1> zeroinitializer
+ %r = select <2 x i1> %y, <2 x i1> <i1 true, i1 true>, <2 x i1> %a
+ ret <2 x i1> %r
+}
+
+; TODO: this could fold the same as above
+
+define <2 x i1> @or_and_common_op_commute2_poison(<2 x i1> %x, <2 x i1> %y) {
+; CHECK-LABEL: @or_and_common_op_commute2_poison(
; CHECK-NEXT: [[A:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> [[Y:%.*]], <2 x i1> <i1 false, i1 poison>
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[Y]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[A]]
; CHECK-NEXT: ret <2 x i1> [[R]]
@@ -621,15 +628,15 @@ define <2 x i1> @or_and_common_op_commute2(<2 x i1> %x, <2 x i1> %y) {
define <2 x i1> @or_and_common_op_commute3(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_and_common_op_commute3(
-; CHECK-NEXT: [[A:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> [[X:%.*]], <2 x i1> zeroinitializer
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[Y]], <2 x i1> <i1 poison, i1 true>, <2 x i1> [[A]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> [[Y:%.*]]
;
%a = select <2 x i1> %y, <2 x i1> %x, <2 x i1> zeroinitializer
%r = select <2 x i1> %y, <2 x i1> <i1 poison, i1 true>, <2 x i1> %a
ret <2 x i1> %r
}
+; negative test
+
define i1 @or_and_not_common_op(i1 %x, i1 %y, i1 %z) {
; CHECK-LABEL: @or_and_not_common_op(
; CHECK-NEXT: [[A:%.*]] = select i1 [[X:%.*]], i1 [[Y:%.*]], i1 false
diff --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll
index b34d4840e72c0..8a1dec27df80f 100644
--- a/llvm/test/Transforms/InstSimplify/select.ll
+++ b/llvm/test/Transforms/InstSimplify/select.ll
@@ -684,9 +684,7 @@ define i1 @or_cmps(float %x) {
define <2 x i1> @or_logic_vector(<2 x i1> %x, <2 x i1> %y) {
; CHECK-LABEL: @or_logic_vector(
-; CHECK-NEXT: [[A:%.*]] = and <2 x i1> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[X]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[A]]
-; CHECK-NEXT: ret <2 x i1> [[R]]
+; CHECK-NEXT: ret <2 x i1> [[X:%.*]]
;
%a = and <2 x i1> %x, %y
%r = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %a
More information about the llvm-commits
mailing list