From d178c15caccda8537c6440ea6ba76bd25cbd8d8c Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 22 Jan 2023 09:43:35 -0500 Subject: [PATCH] [InstSimplify] (X || Y) && Y --> Y (for poison-safe logical ops) https://alive2.llvm.org/ce/z/oT_tEh This is the conjugate/sibling pattern suggested in post-commit feedback for: 9444252a674df5952bb5af2b76348ae4b45 issue #60167 --- llvm/lib/Analysis/InstructionSimplify.cpp | 11 ++++++++++- .../InstCombine/select-safe-bool-transforms.ll | 4 +--- llvm/test/Transforms/InstSimplify/select-logical.ll | 20 ++++++++------------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 4854b68..c83eb96 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4593,13 +4593,21 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, match(TrueVal, m_ZeroInt())) return ConstantInt::getFalse(Cond->getType()); - Value *X, *Y; + // Match patterns that end in logical-and. if (match(FalseVal, m_ZeroInt())) { // !(X || Y) && X --> false (commuted 2 ways) if (match(Cond, m_Not(m_c_LogicalOr(m_Specific(TrueVal), m_Value())))) return ConstantInt::getFalse(Cond->getType()); + // (X || Y) && Y --> Y (commuted 2 ways) + if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Value()))) + return TrueVal; + // Y && (X || Y) --> Y (commuted 2 ways) + if (match(TrueVal, m_c_LogicalOr(m_Specific(Cond), m_Value()))) + return Cond; + // (X || Y) && (X || !Y) --> X (commuted 8 ways) + Value *X, *Y; 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; @@ -4608,6 +4616,7 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, return X; } + // Match patterns that end in logical-or. if (match(TrueVal, m_One())) { // (X && Y) || Y --> Y (commuted 2 ways) if (match(Cond, m_c_LogicalAnd(m_Specific(FalseVal), m_Value()))) diff --git a/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll b/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll index 98e2689..9de9150 100644 --- a/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll +++ b/llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll @@ -131,9 +131,7 @@ define i1 @lor_land_left1(i1 %A, i1 %B) { } define i1 @lor_land_left2(i1 %A, i1 %B) { ; CHECK-LABEL: @lor_land_left2( -; CHECK-NEXT: [[C:%.*]] = select i1 [[B:%.*]], i1 true, i1 [[A:%.*]] -; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i1 [[A]], i1 false -; CHECK-NEXT: ret i1 [[RES]] +; CHECK-NEXT: ret i1 [[A:%.*]] ; %c = select i1 %B, i1 true, i1 %A %res = select i1 %c, i1 %A, i1 false diff --git a/llvm/test/Transforms/InstSimplify/select-logical.ll b/llvm/test/Transforms/InstSimplify/select-logical.ll index 3cb7901..b0fc867 100644 --- a/llvm/test/Transforms/InstSimplify/select-logical.ll +++ b/llvm/test/Transforms/InstSimplify/select-logical.ll @@ -654,9 +654,7 @@ define i1 @or_and_not_common_op(i1 %x, i1 %y, i1 %z) { define i1 @and_or_common_op_commute0(i1 %x, i1 %y) { ; CHECK-LABEL: @and_or_common_op_commute0( -; CHECK-NEXT: [[O:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select i1 [[O]], i1 [[Y]], i1 false -; CHECK-NEXT: ret i1 [[R]] +; CHECK-NEXT: ret i1 [[Y:%.*]] ; %o = select i1 %x, i1 true, i1 %y %r = select i1 %o, i1 %y, i1 false @@ -665,9 +663,7 @@ define i1 @and_or_common_op_commute0(i1 %x, i1 %y) { define <2 x i1> @and_or_common_op_commute1(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @and_or_common_op_commute1( -; CHECK-NEXT: [[O:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> , <2 x i1> [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[O]], <2 x i1> [[Y]], <2 x i1> zeroinitializer -; CHECK-NEXT: ret <2 x i1> [[R]] +; CHECK-NEXT: ret <2 x i1> [[Y:%.*]] ; %o = select <2 x i1> %y, <2 x i1> , <2 x i1> %x %r = select <2 x i1> %o, <2 x i1> %y, <2 x i1> zeroinitializer @@ -677,9 +673,7 @@ define <2 x i1> @and_or_common_op_commute1(<2 x i1> %x, <2 x i1> %y) { define <2 x i1> @and_or_common_op_commute2(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @and_or_common_op_commute2( -; CHECK-NEXT: [[O:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> , <2 x i1> [[Y:%.*]] -; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[Y]], <2 x i1> [[O]], <2 x i1> -; CHECK-NEXT: ret <2 x i1> [[R]] +; CHECK-NEXT: ret <2 x i1> [[Y:%.*]] ; %o = select <2 x i1> %x, <2 x i1> , <2 x i1> %y %r = select <2 x i1> %y, <2 x i1> %o, <2 x i1> @@ -688,15 +682,15 @@ define <2 x i1> @and_or_common_op_commute2(<2 x i1> %x, <2 x i1> %y) { define <2 x i1> @and_or_common_op_commute3(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @and_or_common_op_commute3( -; CHECK-NEXT: [[O:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> , <2 x i1> [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[Y]], <2 x i1> [[O]], <2 x i1> zeroinitializer -; CHECK-NEXT: ret <2 x i1> [[R]] +; CHECK-NEXT: ret <2 x i1> [[Y:%.*]] ; %o = select <2 x i1> %y, <2 x i1> , <2 x i1> %x %r = select <2 x i1> %y, <2 x i1> %o, <2 x i1> zeroinitializer ret <2 x i1> %r } +; TODO: this could fold the same as above + define <2 x i1> @and_or_common_op_commute3_poison(<2 x i1> %x, <2 x i1> %y) { ; CHECK-LABEL: @and_or_common_op_commute3_poison( ; CHECK-NEXT: [[O:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i1> , <2 x i1> [[X:%.*]] @@ -708,6 +702,8 @@ define <2 x i1> @and_or_common_op_commute3_poison(<2 x i1> %x, <2 x i1> %y) { ret <2 x i1> %r } +; negative test + define i1 @and_or_not_common_op(i1 %x, i1 %y, i1 %z) { ; CHECK-LABEL: @and_or_not_common_op( ; CHECK-NEXT: [[O:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]] -- 2.7.4