[llvm] [SLP] Better track MinBWs between vectorization attempts (PR #224199)

Ryan Buchner via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 15:50:06 PDT 2026


https://github.com/bababuck updated https://github.com/llvm/llvm-project/pull/224199

>From 39fc552c4829ff8f449fea80a62a4de61edf7912 Mon Sep 17 00:00:00 2001
From: bababuck <buchner.ryan at gmail.com>
Date: Wed, 16 Sep 2026 13:29:09 -0700
Subject: [PATCH 1/5] [SLP] Better track MinBWs between vectorization attempts

When retrying vectorization with smaller VF, we may be
able to vectorize further where previously gathering
was required. This may expose [s/z]ext instruction that
would allow us to optimize via MinBWs. In such cases,
ignore AnalyzedMinBWVals and recompute MinBWs anyways.

Assisted By: Codex
---
 .../Transforms/Vectorize/SLPVectorizer.cpp    | 20 ++++++++-----
 .../SLPVectorizer/AArch64/revec-reductions.ll | 30 +++++++++----------
 2 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a0adca7aabad0..01adb4b4ea20e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -3699,9 +3699,9 @@ class slpvectorizer::BoUpSLP {
   mutable SmallDenseMap<std::tuple<Type *, Type *, unsigned>, unsigned>
       NumberOfPartsCache;
 
-  /// Values, already been analyzed for mininmal bitwidth and found to be
-  /// non-profitable.
-  DenseSet<Value *> AnalyzedMinBWVals;
+  /// Values already analyzed for minimal bitwidth and found to be
+  /// non-profitable, mapped to the size of the tree used for the analysis.
+  SmallDenseMap<Value *, unsigned> AnalyzedMinBWVals;
 
   /// A list of values that need to extracted out of the tree.
   /// This list holds pairs of (Internal Scalar : External User). External User
@@ -27528,8 +27528,14 @@ void BoUpSLP::computeMinimumValueSizes() {
     ++NodeIdx;
   }
 
+  auto IsAnalyzedMinBWVal = [&](Value *V) {
+    auto It = AnalyzedMinBWVals.find(V);
+    return It != AnalyzedMinBWVals.end() &&
+           It->second >= VectorizableTree.size();
+  };
+
   // Analyzed the reduction already and not profitable - exit.
-  if (AnalyzedMinBWVals.contains(VectorizableTree[NodeIdx]->Scalars.front()))
+  if (IsAnalyzedMinBWVal(VectorizableTree[NodeIdx]->Scalars.front()))
     return;
 
   SmallVector<unsigned> ToDemote;
@@ -27591,8 +27597,7 @@ void BoUpSLP::computeMinimumValueSizes() {
     if (!TreeRootIT)
       return 0u;
 
-    if (any_of(E.Scalars,
-               [&](Value *V) { return AnalyzedMinBWVals.contains(V); }))
+    if (any_of(E.Scalars, IsAnalyzedMinBWVal))
       return 0u;
 
     unsigned NumParts =
@@ -27834,7 +27839,8 @@ void BoUpSLP::computeMinimumValueSizes() {
             cast<IntegerType>(TreeRoot.front()->getType()->getScalarType())
                 ->getBitWidth()) {
       if (UserIgnoreList)
-        AnalyzedMinBWVals.insert_range(TreeRoot);
+        for (Value *V : TreeRoot)
+          AnalyzedMinBWVals[V] = VectorizableTree.size();
       NodesToKeepBWs.insert_range(ToDemote);
       continue;
     }
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
index e266733af51c3..0519227aa09e4 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
@@ -6,25 +6,25 @@ define <16 x i64> @test(<16 x i8> %x) {
 ; CHECK-SAME: <16 x i8> [[X:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[TMP0:%.*]] = zext <16 x i8> [[X]] to <16 x i64>
-; CHECK-NEXT:    [[TMP1:%.*]] = sub <16 x i64> zeroinitializer, [[TMP0]]
+; CHECK-NEXT:    [[TMP12:%.*]] = sub <16 x i64> zeroinitializer, [[TMP0]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP3:%.*]] = sub <16 x i64> zeroinitializer, [[TMP2]]
-; CHECK-NEXT:    [[TMP4:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP1]], <16 x i64> [[TMP3]])
-; CHECK-NEXT:    [[TMP5:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP6:%.*]] = sub <16 x i64> zeroinitializer, [[TMP5]]
-; CHECK-NEXT:    [[TMP7:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP4]], <16 x i64> [[TMP6]])
-; CHECK-NEXT:    [[TMP8:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP9:%.*]] = sub <16 x i64> zeroinitializer, [[TMP8]]
-; CHECK-NEXT:    [[TMP10:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP7]], <16 x i64> [[TMP9]])
-; CHECK-NEXT:    [[TMP11:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
-; CHECK-NEXT:    [[TMP12:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP10]], <16 x i64> [[TMP11]])
-; CHECK-NEXT:    [[TMP13:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP13:%.*]] = sub <16 x i64> zeroinitializer, [[TMP2]]
 ; CHECK-NEXT:    [[TMP14:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP12]], <16 x i64> [[TMP13]])
-; CHECK-NEXT:    [[TMP15:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP5:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
+; CHECK-NEXT:    [[TMP15:%.*]] = sub <16 x i64> zeroinitializer, [[TMP5]]
 ; CHECK-NEXT:    [[TMP16:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP14]], <16 x i64> [[TMP15]])
-; CHECK-NEXT:    [[TMP17:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP8:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
+; CHECK-NEXT:    [[TMP17:%.*]] = sub <16 x i64> zeroinitializer, [[TMP8]]
 ; CHECK-NEXT:    [[TMP48:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP16]], <16 x i64> [[TMP17]])
-; CHECK-NEXT:    ret <16 x i64> [[TMP48]]
+; CHECK-NEXT:    [[TMP11:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP19:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP48]], <16 x i64> [[TMP11]])
+; CHECK-NEXT:    [[TMP20:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP21:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP19]], <16 x i64> [[TMP20]])
+; CHECK-NEXT:    [[TMP22:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP23:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP21]], <16 x i64> [[TMP22]])
+; CHECK-NEXT:    [[TMP24:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP18:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP23]], <16 x i64> [[TMP24]])
+; CHECK-NEXT:    ret <16 x i64> [[TMP18]]
 ;
 entry:
   %0 = zext <16 x i8> %x to <16 x i64>

>From 1616ac096ac16e48d7d832e9ceec8a375822cd2d Mon Sep 17 00:00:00 2001
From: bababuck <buchner.ryan at gmail.com>
Date: Thu, 17 Sep 2026 11:55:27 -0700
Subject: [PATCH 2/5] [SLP] Check tree size exlcuding gathers when deciding to
 re-compute MinBWs

The current analysis stops at gather nodes. If a future tree is larger and
gather/split nodes become regular vectorized, the MinBWs computation can
now consider more nodes so ought to recompute.
---
 .../Transforms/Vectorize/SLPVectorizer.cpp    | 35 +++++++++++++++----
 .../SLPVectorizer/AArch64/revec-reductions.ll | 26 ++++++--------
 .../subvector-minbitwidth-unsigned-value.ll   | 19 ++++++----
 3 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 01adb4b4ea20e..438fdc0bf0c10 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -704,6 +704,27 @@ class slpvectorizer::BoUpSLP {
 
   unsigned getTreeSize() const { return VectorizableTree.size(); }
 
+  unsigned getTreeSizeExcludingGathers() const {
+    unsigned Cnt = 0;
+    SmallDenseSet<unsigned> GatherTreeNodes;
+    for (unsigned NodeIdx : seq<unsigned>(VectorizableTree.size())) {
+      auto &TE = VectorizableTree[NodeIdx];
+      if (DeletedNodes.contains(TE.get()))
+        continue;
+      auto IsGather = [&](TreeEntry *TE) {
+        return TE->isGather() || TransformedToGatherNodes.contains(TE) ||
+               GatherTreeNodes.contains(TE->Idx);
+      };
+      if (IsGather(TE.get()) || (!TE->UserTreeIndex.UserTE && NodeIdx != 0) ||
+          (TE->UserTreeIndex.UserTE && (IsGather(TE->UserTreeIndex.UserTE)))) {
+        GatherTreeNodes.insert(NodeIdx);
+        continue;
+      }
+      ++Cnt;
+    }
+    return Cnt;
+  }
+
   /// Returns the base graph size, before any transformations.
   unsigned getCanonicalGraphSize() const { return BaseGraphSize; }
 
@@ -27528,14 +27549,14 @@ void BoUpSLP::computeMinimumValueSizes() {
     ++NodeIdx;
   }
 
-  auto IsAnalyzedMinBWVal = [&](Value *V) {
+  auto IsAnalyzedMinBWVal = [&](Value *V, unsigned SizeExGathers) {
     auto It = AnalyzedMinBWVals.find(V);
-    return It != AnalyzedMinBWVals.end() &&
-           It->second >= VectorizableTree.size();
+    return It != AnalyzedMinBWVals.end() && It->second >= SizeExGathers;
   };
 
   // Analyzed the reduction already and not profitable - exit.
-  if (IsAnalyzedMinBWVal(VectorizableTree[NodeIdx]->Scalars.front()))
+  if (IsAnalyzedMinBWVal(VectorizableTree[NodeIdx]->Scalars.front(),
+                         getTreeSizeExcludingGathers()))
     return;
 
   SmallVector<unsigned> ToDemote;
@@ -27597,7 +27618,9 @@ void BoUpSLP::computeMinimumValueSizes() {
     if (!TreeRootIT)
       return 0u;
 
-    if (any_of(E.Scalars, IsAnalyzedMinBWVal))
+    unsigned SizeExGathers = getTreeSizeExcludingGathers();
+    if (any_of(E.Scalars,
+               [&](Value *V) { return IsAnalyzedMinBWVal(V, SizeExGathers); }))
       return 0u;
 
     unsigned NumParts =
@@ -27840,7 +27863,7 @@ void BoUpSLP::computeMinimumValueSizes() {
                 ->getBitWidth()) {
       if (UserIgnoreList)
         for (Value *V : TreeRoot)
-          AnalyzedMinBWVals[V] = VectorizableTree.size();
+          AnalyzedMinBWVals[V] = getTreeSizeExcludingGathers();
       NodesToKeepBWs.insert_range(ToDemote);
       continue;
     }
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
index 0519227aa09e4..0cae10157bd2e 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
@@ -5,24 +5,18 @@ define <16 x i64> @test(<16 x i8> %x) {
 ; CHECK-LABEL: define <16 x i64> @test(
 ; CHECK-SAME: <16 x i8> [[X:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[TMP0:%.*]] = zext <16 x i8> [[X]] to <16 x i64>
-; CHECK-NEXT:    [[TMP12:%.*]] = sub <16 x i64> zeroinitializer, [[TMP0]]
-; CHECK-NEXT:    [[TMP2:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP13:%.*]] = sub <16 x i64> zeroinitializer, [[TMP2]]
-; CHECK-NEXT:    [[TMP14:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP12]], <16 x i64> [[TMP13]])
-; CHECK-NEXT:    [[TMP5:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP15:%.*]] = sub <16 x i64> zeroinitializer, [[TMP5]]
-; CHECK-NEXT:    [[TMP16:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP14]], <16 x i64> [[TMP15]])
-; CHECK-NEXT:    [[TMP8:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT:    [[TMP17:%.*]] = sub <16 x i64> zeroinitializer, [[TMP8]]
-; CHECK-NEXT:    [[TMP48:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP16]], <16 x i64> [[TMP17]])
-; CHECK-NEXT:    [[TMP11:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
-; CHECK-NEXT:    [[TMP19:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP48]], <16 x i64> [[TMP11]])
-; CHECK-NEXT:    [[TMP20:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP0:%.*]] = shufflevector <16 x i8> [[X]], <16 x i8> poison, <64 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
+; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <64 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, <64 x i8> [[TMP0]], <64 x i32> <i32 64, i32 65, i32 66, i32 67, i32 68, i32 69, i32 70, i32 71, i32 72, i32 73, i32 74, i32 75, i32 76, i32 77, i32 78, i32 79, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63>
+; CHECK-NEXT:    [[TMP2:%.*]] = zext <64 x i8> [[TMP1]] to <64 x i16>
+; CHECK-NEXT:    [[TMP3:%.*]] = sub <64 x i16> zeroinitializer, [[TMP2]]
+; CHECK-NEXT:    [[TMP4:%.*]] = sext <64 x i16> [[TMP3]] to <64 x i64>
+; CHECK-NEXT:    [[TMP5:%.*]] = call <64 x i64> @llvm.smin.v64i64(<64 x i64> [[TMP4]], <64 x i64> zeroinitializer)
+; CHECK-NEXT:    [[TMP19:%.*]] = shufflevector <64 x i64> [[TMP5]], <64 x i64> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; CHECK-NEXT:    [[TMP20:%.*]] = shufflevector <64 x i64> [[TMP5]], <64 x i64> poison, <16 x i32> <i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
 ; CHECK-NEXT:    [[TMP21:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP19]], <16 x i64> [[TMP20]])
-; CHECK-NEXT:    [[TMP22:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP22:%.*]] = shufflevector <64 x i64> [[TMP5]], <64 x i64> poison, <16 x i32> <i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47>
 ; CHECK-NEXT:    [[TMP23:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP21]], <16 x i64> [[TMP22]])
-; CHECK-NEXT:    [[TMP24:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT:    [[TMP24:%.*]] = shufflevector <64 x i64> [[TMP5]], <64 x i64> poison, <16 x i32> <i32 48, i32 49, i32 50, i32 51, i32 52, i32 53, i32 54, i32 55, i32 56, i32 57, i32 58, i32 59, i32 60, i32 61, i32 62, i32 63>
 ; CHECK-NEXT:    [[TMP18:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP23]], <16 x i64> [[TMP24]])
 ; CHECK-NEXT:    ret <16 x i64> [[TMP18]]
 ;
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/subvector-minbitwidth-unsigned-value.ll b/llvm/test/Transforms/SLPVectorizer/X86/subvector-minbitwidth-unsigned-value.ll
index a8f8ed8c4c397..7f3efc0f191a4 100644
--- a/llvm/test/Transforms/SLPVectorizer/X86/subvector-minbitwidth-unsigned-value.ll
+++ b/llvm/test/Transforms/SLPVectorizer/X86/subvector-minbitwidth-unsigned-value.ll
@@ -5,12 +5,19 @@ define i1 @test(i64 %v1, ptr %v2, i32 %v3, i1 %v4) {
 ; CHECK-LABEL: define i1 @test(
 ; CHECK-SAME: i64 [[V1:%.*]], ptr [[V2:%.*]], i32 [[V3:%.*]], i1 [[V4:%.*]]) {
 ; CHECK-NEXT:  [[NEWFUNCROOT:.*:]]
-; CHECK-NEXT:    [[TMP0:%.*]] = insertelement <2 x i64> poison, i64 [[V1]], i64 0
-; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x i64> [[TMP0]], <2 x i64> poison, <2 x i32> zeroinitializer
-; CHECK-NEXT:    [[TMP2:%.*]] = lshr <2 x i64> [[TMP1]], <i64 32, i64 40>
-; CHECK-NEXT:    [[TMP4:%.*]] = trunc <2 x i64> [[TMP2]] to <2 x i32>
-; CHECK-NEXT:    [[TMP5:%.*]] = and <2 x i32> [[TMP4]], <i32 1, i32 255>
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq <2 x i32> [[TMP5]], zeroinitializer
+; CHECK-NEXT:    [[TMP0:%.*]] = lshr i64 [[V1]], 40
+; CHECK-NEXT:    [[TT3:%.*]] = lshr i64 [[V1]], 32
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[TMP0]] to i32
+; CHECK-NEXT:    [[TMP2:%.*]] = trunc i64 [[TT3]] to i32
+; CHECK-NEXT:    [[TT2:%.*]] = and i32 [[TMP1]], 255
+; CHECK-NEXT:    [[TT1:%.*]] = and i32 [[TMP2]], 1
+; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TT1]] to i8
+; CHECK-NEXT:    [[TMP4:%.*]] = insertelement <2 x i8> poison, i8 [[TMP3]], i64 0
+; CHECK-NEXT:    [[TMP7:%.*]] = trunc i32 [[TT2]] to i8
+; CHECK-NEXT:    [[TMP9:%.*]] = insertelement <2 x i8> [[TMP4]], i8 [[TMP7]], i64 1
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq <2 x i8> [[TMP9]], zeroinitializer
+; CHECK-NEXT:    [[TMP33:%.*]] = insertelement <2 x i32> poison, i32 [[TT1]], i64 0
+; CHECK-NEXT:    [[TMP5:%.*]] = insertelement <2 x i32> [[TMP33]], i32 [[TT2]], i64 1
 ; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <4 x i32> poison, i32 [[V3]], i64 0
 ; CHECK-NEXT:    [[TMP30:%.*]] = shufflevector <4 x i32> [[TMP8]], <4 x i32> poison, <4 x i32> <i32 poison, i32 poison, i32 0, i32 0>
 ; CHECK-NEXT:    [[TMP31:%.*]] = shufflevector <2 x i32> [[TMP5]], <2 x i32> poison, <4 x i32> <i32 0, i32 1, i32 poison, i32 poison>

>From 0eca55f738d253b431ba58f31e1bb616a828ea6a Mon Sep 17 00:00:00 2001
From: bababuck <buchner.ryan at gmail.com>
Date: Mon, 21 Sep 2026 13:35:51 -0700
Subject: [PATCH 3/5] Add comment to getTreeSizeExcludingGathers()

---
 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index defec1834c2b2..b0ced08e63592 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -719,6 +719,11 @@ class slpvectorizer::BoUpSLP {
 
   unsigned getTreeSize() const { return VectorizableTree.size(); }
 
+  /// Returns the size of the current tree, stopping at gathers
+  /// For instance, even though we continue adding tree entries after
+  /// a split node, this function stops counting at the split node
+  /// since it is a gather. This matches the behavior of MinBWs
+  /// computation.
   unsigned getTreeSizeExcludingGathers() const {
     unsigned Cnt = 0;
     SmallDenseSet<unsigned> GatherTreeNodes;

>From 556a30880b543d2dc29af7dd1b71ceb039046c16 Mon Sep 17 00:00:00 2001
From: bababuck <buchner.ryan at gmail.com>
Date: Mon, 21 Sep 2026 13:37:04 -0700
Subject: [PATCH 4/5] [SLP] Use SmallBitVector rather than SmallDenseSet

---
 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index b0ced08e63592..2a7926af875b5 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -726,18 +726,18 @@ class slpvectorizer::BoUpSLP {
   /// computation.
   unsigned getTreeSizeExcludingGathers() const {
     unsigned Cnt = 0;
-    SmallDenseSet<unsigned> GatherTreeNodes;
+    SmallBitVector GatherTreeNodes(VectorizableTree.size(), false);
     for (unsigned NodeIdx : seq<unsigned>(VectorizableTree.size())) {
       auto &TE = VectorizableTree[NodeIdx];
       if (DeletedNodes.contains(TE.get()))
         continue;
       auto IsGather = [&](TreeEntry *TE) {
         return TE->isGather() || TransformedToGatherNodes.contains(TE) ||
-               GatherTreeNodes.contains(TE->Idx);
+               GatherTreeNodes.test(TE->Idx);
       };
       if (IsGather(TE.get()) || (!TE->UserTreeIndex.UserTE && NodeIdx != 0) ||
           (TE->UserTreeIndex.UserTE && (IsGather(TE->UserTreeIndex.UserTE)))) {
-        GatherTreeNodes.insert(NodeIdx);
+        GatherTreeNodes.set(NodeIdx);
         continue;
       }
       ++Cnt;

>From 0af1639eb046dabfd277b4ba304748ec12ba9ffc Mon Sep 17 00:00:00 2001
From: bababuck <buchner.ryan at gmail.com>
Date: Mon, 21 Sep 2026 15:44:06 -0700
Subject: [PATCH 5/5] Use insert_or_assign

---
 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 2a7926af875b5..8cb8b1faff690 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -28623,7 +28623,7 @@ void BoUpSLP::computeMinimumValueSizes() {
                 ->getBitWidth()) {
       if (UserIgnoreList)
         for (Value *V : TreeRoot)
-          AnalyzedMinBWVals[V] = getTreeSizeExcludingGathers();
+          AnalyzedMinBWVals.insert_or_assign(V, getTreeSizeExcludingGathers());
       NodesToKeepBWs.insert_range(ToDemote);
       continue;
     }



More information about the llvm-commits mailing list