[llvm] [VectorCombine] Optimize vector combine in fold binop of reduction (PR #179416)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 9 00:12:09 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Anjian-Wen (Anjian-Wen)

<details>
<summary>Changes</summary>

move the reduction op close by simple commutative property of Addition and Associative Property of Subtraction, which can pave the way for combining the next two instructions in some case and saving a reduction instruction if possible.

---
Full diff: https://github.com/llvm/llvm-project/pull/179416.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+51) 
- (modified) llvm/test/Transforms/VectorCombine/fold-binop-of-reductions.ll (+60) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 1746d3e4b06f4..74bf337c1767a 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -1754,6 +1754,57 @@ bool VectorCombine::foldBinopOfReductions(Instruction &I) {
     return nullptr;
   };
 
+  // sub (add (vector_reduce_add b, a), vector_reduce_add c) ->
+  // add (sub (vector_reduce_add b, vector_reduce_add c), a)
+  //
+  // sub (sub (vector_reduce_add b, a), vector_reduce_add c) ->
+  // sub (sub (vector_reduce_add b, vector_reduce_add c), a)
+  //
+  // sub (sub (a, vector_reduce_add b), vector_reduce_add c) ->
+  // sub (a, add (vector_reduce_add b, vector_reduce_add c))
+  //
+  // sub (add (a, vector_reduce_add b), vector_reduce_add c) ->
+  // add (a, sub (vector_reduce_add b, vector_reduce_add c))
+  if (BinOpOpc == Instruction::Sub) {
+    auto *II = dyn_cast<BinaryOperator>(I.getOperand(0));
+    Value *V1 = checkIntrinsicAndGetItsArgument(I.getOperand(1), ReductionIID);
+
+    if (II && V1 &&
+        (II->getOpcode() == Instruction::Add ||
+         II->getOpcode() == Instruction::Sub)) {
+      Instruction *I0 = dyn_cast<Instruction>(I.getOperand(0));
+      Value *V00 =
+          checkIntrinsicAndGetItsArgument(I0->getOperand(0), ReductionIID);
+      Value *V01 =
+          checkIntrinsicAndGetItsArgument(I0->getOperand(1), ReductionIID);
+
+      if (V00 && !V01) {
+        Value *CombineNode = Builder.CreateBinOp(
+            Instruction::Sub, I0->getOperand(0), I.getOperand(1));
+        Value *NewBinNode = Builder.CreateBinOp(II->getOpcode(), CombineNode,
+                                                I0->getOperand(1));
+        replaceValue(I, *NewBinNode);
+        return true;
+      } else if (V01 && !V00) {
+        if (II->getOpcode() == Instruction::Sub) {
+          Value *CombineNode = Builder.CreateBinOp(
+              Instruction::Add, I0->getOperand(1), I.getOperand(1));
+          Value *NewBinNode = Builder.CreateBinOp(
+              Instruction::Sub, I0->getOperand(0), CombineNode);
+          replaceValue(I, *NewBinNode);
+          return true;
+        } else if (II->getOpcode() == Instruction::Add) {
+          Value *CombineNode = Builder.CreateBinOp(
+              Instruction::Sub, I0->getOperand(1), I.getOperand(1));
+          Value *NewBinNode = Builder.CreateBinOp(
+              Instruction::Add, I0->getOperand(0), CombineNode);
+          replaceValue(I, *NewBinNode);
+          return true;
+        }
+      }
+    }
+  }
+
   Value *V0 = checkIntrinsicAndGetItsArgument(I.getOperand(0), ReductionIID);
   if (!V0)
     return false;
diff --git a/llvm/test/Transforms/VectorCombine/fold-binop-of-reductions.ll b/llvm/test/Transforms/VectorCombine/fold-binop-of-reductions.ll
index 5f29af9de5a39..22960119ce056 100644
--- a/llvm/test/Transforms/VectorCombine/fold-binop-of-reductions.ll
+++ b/llvm/test/Transforms/VectorCombine/fold-binop-of-reductions.ll
@@ -205,3 +205,63 @@ define i32 @element_counts_do_not_match_vscale(<vscale x 16 x i32> %v0, <vscale
   %res = add i32 %v0_red, %v1_red
   ret i32 %res
 }
+
+define i32 @sub_add_reduction_s_reduction(<vscale x 8 x i32> %v0, <vscale x 8 x i32> %v1, i32 %s1) {
+; CHECK-LABEL: define i32 @sub_add_reduction_s_reduction(
+; CHECK-SAME: <vscale x 8 x i32> [[V0:%.*]], <vscale x 8 x i32> [[V1:%.*]], i32 [[S1:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub <vscale x 8 x i32> [[V0]], [[V1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.nxv8i32(<vscale x 8 x i32> [[TMP1]])
+; CHECK-NEXT:    [[RES:%.*]] = add i32 [[TMP2]], [[S1]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %v0_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v0)
+  %v1_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v1)
+  %add1 = add i32 %v0_red, %s1
+  %res = sub i32 %add1, %v1_red
+  ret i32 %res
+}
+
+define i32 @sub_add_s_reduction_reduction(<vscale x 8 x i32> %v0, <vscale x 8 x i32> %v1, i32 %s1) {
+; CHECK-LABEL: define i32 @sub_add_s_reduction_reduction(
+; CHECK-SAME: <vscale x 8 x i32> [[V0:%.*]], <vscale x 8 x i32> [[V1:%.*]], i32 [[S1:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub <vscale x 8 x i32> [[V0]], [[V1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.nxv8i32(<vscale x 8 x i32> [[TMP1]])
+; CHECK-NEXT:    [[RES:%.*]] = add i32 [[S1]], [[TMP2]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %v0_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v0)
+  %v1_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v1)
+  %add1 = add i32 %s1, %v0_red
+  %res = sub i32 %add1, %v1_red
+  ret i32 %res
+}
+
+define i32 @sub_sub_reduction_s_reduction(<vscale x 8 x i32> %v0, <vscale x 8 x i32> %v1, i32 %s1) {
+; CHECK-LABEL: define i32 @sub_sub_reduction_s_reduction(
+; CHECK-SAME: <vscale x 8 x i32> [[V0:%.*]], <vscale x 8 x i32> [[V1:%.*]], i32 [[S1:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sub <vscale x 8 x i32> [[V0]], [[V1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.nxv8i32(<vscale x 8 x i32> [[TMP1]])
+; CHECK-NEXT:    [[RES:%.*]] = sub i32 [[TMP2]], [[S1]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %v0_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v0)
+  %v1_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v1)
+  %sub1 = sub i32 %v0_red, %s1
+  %res = sub i32 %sub1, %v1_red
+  ret i32 %res
+}
+
+define i32 @sub_sub_s_reduction_reduction(<vscale x 8 x i32> %v0, <vscale x 8 x i32> %v1, i32 %s1) {
+; CHECK-LABEL: define i32 @sub_sub_s_reduction_reduction(
+; CHECK-SAME: <vscale x 8 x i32> [[V0:%.*]], <vscale x 8 x i32> [[V1:%.*]], i32 [[S1:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = add <vscale x 8 x i32> [[V0]], [[V1]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.nxv8i32(<vscale x 8 x i32> [[TMP1]])
+; CHECK-NEXT:    [[RES:%.*]] = sub i32 [[S1]], [[TMP2]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %v0_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v0)
+  %v1_red = tail call i32 @llvm.vector.reduce.add.v8i32(<vscale x 8 x i32> %v1)
+  %sub1 = sub i32 %s1, %v0_red
+  %res = sub i32 %sub1, %v1_red
+  ret i32 %res
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/179416


More information about the llvm-commits mailing list