[llvm] d178c15 - [InstSimplify] (X || Y) && Y --> Y (for poison-safe logical ops)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 22 07:50:15 PST 2023


Author: Sanjay Patel
Date: 2023-01-22T10:44:45-05:00
New Revision: d178c15caccda8537c6440ea6ba76bd25cbd8d8c

URL: https://github.com/llvm/llvm-project/commit/d178c15caccda8537c6440ea6ba76bd25cbd8d8c
DIFF: https://github.com/llvm/llvm-project/commit/d178c15caccda8537c6440ea6ba76bd25cbd8d8c.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
    llvm/test/Transforms/InstSimplify/select-logical.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 4854b68a8c97..c83eb96bbc69 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 98e2689831d1..9de9150f62ed 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 3cb7901a0185..b0fc8678ca14 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> <i1 true, i1 true>, <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> <i1 true, i1 true>, <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> <i1 true, i1 true>, <2 x i1> [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[Y]], <2 x i1> [[O]], <2 x i1> <i1 false, i1 poison>
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> [[Y:%.*]]
 ;
   %o = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %y
   %r = select <2 x i1> %y, <2 x i1> %o, <2 x i1> <i1 0, i1 poison>
@@ -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> <i1 true, i1 true>, <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> <i1 true, i1 true>, <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> <i1 poison, i1 true>, <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:%.*]]


        


More information about the llvm-commits mailing list