[llvm] 3073074 - [InstCombine] allow more commutative matches for logical-and to select fold

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 21 10:32:23 PDT 2022


Author: Sanjay Patel
Date: 2022-10-21T13:29:13-04:00
New Revision: 307307456277f62f963e494de85b2b24f9c4afe2

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

LOG: [InstCombine] allow more commutative matches for logical-and to select fold

When the common value is part of either select condition,
this is safe to reduce. Otherwise, it is not poison-safe
(with the select form of the pattern):
https://alive2.llvm.org/ce/z/FxQTzB

This is another patch motivated by issue #58313.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/select-safe-transforms.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 6bed09df218a..4e0ca608837a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2851,11 +2851,19 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
       Value *C;
 
       // (C && A) || (!C && B) --> sel C, A, B
+      // (A && C) || (!C && B) --> sel C, A, B
       if (match(FalseVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
           match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A))))
         return SelectInst::Create(C, A, B);
 
+      // (C && A) || (B && !C) --> sel C, A, B
+      // TODO: (A && C) || (B && !C) is safe to transform with real 'and' ops.
+      if (match(FalseVal, m_LogicalAnd(m_Value(B), m_Not(m_Value(C)))) &&
+          match(CondVal, m_LogicalAnd(m_Specific(C), m_Value(A))))
+        return SelectInst::Create(C, A, B);
+
       // (!C && A) || (C && B) --> sel C, B, A
+      // (!C && A) || (B && C) --> sel C, B, A
       if (match(CondVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) &&
           match(FalseVal, m_c_LogicalAnd(m_Specific(C), m_Value(B))))
         return SelectInst::Create(C, B, A);

diff  --git a/llvm/test/Transforms/InstCombine/select-safe-transforms.ll b/llvm/test/Transforms/InstCombine/select-safe-transforms.ll
index 852675823836..b0b8108ac599 100644
--- a/llvm/test/Transforms/InstCombine/select-safe-transforms.ll
+++ b/llvm/test/Transforms/InstCombine/select-safe-transforms.ll
@@ -479,10 +479,7 @@ define i1 @bools2_logical_commute1_and1_and2(i1 %a, i1 %b, i1 %c) {
 
 define i1 @bools2_logical_commute2(i1 %a, i1 %b, i1 %c) {
 ; CHECK-LABEL: @bools2_logical_commute2(
-; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT:    [[AND1:%.*]] = select i1 [[C]], i1 [[A:%.*]], i1 false
-; CHECK-NEXT:    [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
 ; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %not = xor i1 %c, -1
@@ -494,10 +491,7 @@ define i1 @bools2_logical_commute2(i1 %a, i1 %b, i1 %c) {
 
 define i1 @bools2_logical_commute2_and1(i1 %a, i1 %b, i1 %c) {
 ; CHECK-LABEL: @bools2_logical_commute2_and1(
-; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT:    [[AND1:%.*]] = and i1 [[C]], [[A:%.*]]
-; CHECK-NEXT:    [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
 ; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %not = xor i1 %c, -1
@@ -510,10 +504,7 @@ define i1 @bools2_logical_commute2_and1(i1 %a, i1 %b, i1 %c) {
 define i1 @bools2_logical_commute2_and2(i1 %a, i1 %c) {
 ; CHECK-LABEL: @bools2_logical_commute2_and2(
 ; CHECK-NEXT:    [[B:%.*]] = call i1 @gen1()
-; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT:    [[AND1:%.*]] = select i1 [[C]], i1 [[A:%.*]], i1 false
-; CHECK-NEXT:    [[AND2:%.*]] = and i1 [[B]], [[NOT]]
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B]]
 ; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %b = call i1 @gen1()
@@ -527,10 +518,7 @@ define i1 @bools2_logical_commute2_and2(i1 %a, i1 %c) {
 define i1 @bools2_logical_commute2_and1_and2(i1 %a, i1 %c) {
 ; CHECK-LABEL: @bools2_logical_commute2_and1_and2(
 ; CHECK-NEXT:    [[B:%.*]] = call i1 @gen1()
-; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT:    [[AND1:%.*]] = and i1 [[C]], [[A:%.*]]
-; CHECK-NEXT:    [[AND2:%.*]] = and i1 [[B]], [[NOT]]
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B]]
 ; CHECK-NEXT:    ret i1 [[OR]]
 ;
   %b = call i1 @gen1()


        


More information about the llvm-commits mailing list