[llvm] [SLP] Reject 2-element vectorization when vector inst count exceeds scalar (PR #190414)

Ryan Buchner via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 08:46:25 PDT 2026


================
@@ -12829,29 +12829,83 @@ bool BoUpSLP::areAllUsersVectorized(
          });
 }
 
+static InstructionCost canConvertToFMA(ArrayRef<Value *> VL,
+                                       const InstructionsState &S,
+                                       DominatorTree &DT, const DataLayout &DL,
+                                       TargetTransformInfo &TTI,
+                                       const TargetLibraryInfo &TLI);
+
 unsigned BoUpSLP::getNumScalarInsts() const {
   unsigned Count = 0;
   for (const std::unique_ptr<TreeEntry> &Ptr : VectorizableTree) {
     const TreeEntry &TE = *Ptr;
     if (DeletedNodes.contains(&TE))
       continue;
     if (TE.isGather() || TransformedToGatherNodes.contains(&TE)) {
-      // Count instruction scalars in gathers — they exist in the scalar
+      // Count extractelement scalars in gathers — they exist in the scalar
       // code regardless of vectorization. ExtractElement instructions
       // become free when the vector input is used directly.
       for (Value *V : TE.Scalars)
-        if (isa<Instruction>(V))
+        if (isa<ExtractElementInst>(V))
           ++Count;
       continue;
     }
+    // CombinedVectorize entries (e.g. the fmul child of an FMulAdd, or the
+    // cmp child of a MinMax select) are absorbed into the parent on both
+    // scalar and vector sides. The backend fuses fadd+fmul → fma and
+    // select+cmp → smin/smax even for scalar code, so skip to avoid
+    // double-counting.
+    if (TE.State == TreeEntry::CombinedVectorize)
+      continue;
     // Each vectorize entry represents a bundle of scalar instructions.
     // Count per-entry without cross-entry deduplication, since shared
     // scalars across entries still represent separate work in scalar code.
     for (Value *V : TE.Scalars) {
-      auto *I = dyn_cast<Instruction>(V);
-      if (!I || (TE.hasCopyableElements() && TE.isCopyableElement(V)))
+      if (!isa<Instruction>(V) ||
+          (TE.hasCopyableElements() && TE.isCopyableElement(V)))
         continue;
       ++Count;
+      // Calculate calls/divs/rems twice, they may cost higher, so better to
+      // include their count twice to mimic slightly real cost here.
+      auto *I = dyn_cast<Instruction>(V);
+      if (I && (isa<CallInst>(I) || I->isIntDivRem() || I->isFPDivRem()))
+        ++Count;
+    }
----------------
bababuck wrote:

Doesn't this essentially mix `InstructionCost::CodeSize` with `InstructionCost::RecipThroughput`? Or are you suggesting that the code size for these instructions might be greater than one?

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


More information about the llvm-commits mailing list