[llvm] d9ce80d - [SLP]FIx order of bool logical ops, if the right op is used in the first reduction operarion

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 28 11:54:40 PST 2025


Author: Alexey Bataev
Date: 2025-12-28T11:52:44-08:00
New Revision: d9ce80db7aaa985160276aa59a405e5f47fd7354

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

LOG: [SLP]FIx order of bool logical ops, if the right op is used in the first reduction operarion

If the LHS of the first reduction op is not a first operand, but RHS is,
and RHS is the second operand of the first reductoin op, still need to
emit RHS as a second reduction operand, though without freeze of the
LHS operand

https://alive2.llvm.org/ce/z/2_JLBu

Fixes #173784

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    llvm/test/Transforms/SLPVectorizer/boolean-op-with-poisons.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 6e05fcfe421cb..a7b41631594d2 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -25024,13 +25024,21 @@ class HorizontalReduction {
                                         getRdxOperand(RedOp1, 0) == LHS ||
                                         isGuaranteedNotToBePoison(LHS, AC)))
             return;
+          bool NeedFreeze = LHS != VectorizedTree;
           if (isBoolLogicOp(RedOp2) && ((!InitStep && RHS == VectorizedTree) ||
                                         getRdxOperand(RedOp2, 0) == RHS ||
                                         isGuaranteedNotToBePoison(RHS, AC))) {
-            std::swap(LHS, RHS);
-            return;
+            if ((InitStep || RHS != VectorizedTree) &&
+                getRdxOperand(RedOp2, 0) == RHS &&
+                (!isBoolLogicOp(RedOp1) ||
+                 getRdxOperand(RedOp1, 1) == RedOp2)) {
+              NeedFreeze = false;
+            } else {
+              std::swap(LHS, RHS);
+              return;
+            }
           }
-          if (LHS != VectorizedTree)
+          if (NeedFreeze)
             LHS = Builder.CreateFreeze(LHS);
         };
     // Finish the reduction.

diff  --git a/llvm/test/Transforms/SLPVectorizer/boolean-op-with-poisons.ll b/llvm/test/Transforms/SLPVectorizer/boolean-op-with-poisons.ll
index 3666cb7667a16..e65de2fc0ca29 100644
--- a/llvm/test/Transforms/SLPVectorizer/boolean-op-with-poisons.ll
+++ b/llvm/test/Transforms/SLPVectorizer/boolean-op-with-poisons.ll
@@ -11,7 +11,7 @@ define i1 @test_explicit_poison_leaves(i1 %a, i1 %b) {
 ; CHECK-LABEL: define i1 @test_explicit_poison_leaves(
 ; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
 ; CHECK-NEXT:    [[OP_RDX:%.*]] = select i1 [[A]], i1 true, i1 poison
-; CHECK-NEXT:    [[OP_RDX1:%.*]] = select i1 [[B]], i1 true, i1 [[OP_RDX]]
+; CHECK-NEXT:    [[OP_RDX1:%.*]] = select i1 [[OP_RDX]], i1 true, i1 [[B]]
 ; CHECK-NEXT:    ret i1 [[OP_RDX1]]
 ;
   %op1 = select i1 %a, i1 true, i1 poison


        


More information about the llvm-commits mailing list