[llvm] [InstCombine] Transform `vector.reduce.add` and `splat` into multiplication (PR #161020)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 28 10:38:21 PDT 2025


================
@@ -3761,6 +3762,24 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
             return replaceInstUsesWith(CI, Res);
           }
       }
+
+      // Handle the case where a splat is summarized. In that case we have a
+      // multpilication. For example: %2 = insertelement <4 x i32> poison, i32
+      // %0, i64 0 %3 = shufflevector <4 x i32> %2, poison, <4 x i32>
+      // zeroinitializer %4 = tail call i32 @llvm.vector.reduce.add.v4i32(%3)
+      // =>
+      // %2 = mul i32 %0, 4
+      if (Value *Splat = getSplatValue(Arg)) {
+        // It is only a multiplication if we add the same element over and over.
+        ElementCount ReducedVectorElementCount =
+            cast<VectorType>(Arg->getType())->getElementCount();
+        if (ReducedVectorElementCount.isFixed()) {
----------------
dtcxzyw wrote:

You can use `IRBuilder::CreateElementCount`.

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


More information about the llvm-commits mailing list