Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: BoUpSLP::buildTree_rec
Issue title: crash in slp vectorizer after "[SLP]Fix a check for the whole register use"
Labels: llvm:SLPVectorizer
crash-on-valid
Reported by: macurtis-amd

After applying [bfd8cc0a3e82](https://github.com/llvm/llvm-project/commit/bfd8cc0a3e82), I am seeing a crash in SLPVectorize.cpp.

Reproducer:
```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define <2 x float> @foo() {
entry:
  br label %do.body

do.body:                                          ; preds = %do.body, %entry
  %val.sroa.66.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.66.1, %do.body ]
  %val.sroa.60.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.60.2, %do.body ]
  %val.sroa.54.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.54.2, %do.body ]
  %val.sroa.48.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.48.2, %do.body ]
  %val.sroa.42.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.42.2, %do.body ]
  %val.sroa.36.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.36.2, %do.body ]
  %val.sroa.30.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.30.2, %do.body ]
  %val.sroa.24.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.24.2, %do.body ]
  %val.sroa.18.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.18.2, %do.body ]
  %val.sroa.12.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.12.2, %do.body ]
  %val.sroa.6.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.6.2, %do.body ]
  %val.sroa.0.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.0.1, %do.body ]
  %cmp119 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.6.1 = select i1 %cmp119, float 0.000000e+00, float 0.000000e+00
  %val.sroa.0.1 = select i1 %cmp119, float 0.000000e+00, float 0.000000e+00
  %cmp119.1 = fcmp uge float %val.sroa.6.1, 0.000000e+00
  %val.sroa.6.2 = select i1 %cmp119.1, float 0.000000e+00, float 0.000000e+00
  %cmp119.2 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.12.2 = select i1 %cmp119.2, float 0.000000e+00, float 0.000000e+00
  %cmp119.3 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.18.2 = select i1 %cmp119.3, float 0.000000e+00, float 0.000000e+00
  %cmp119.4 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.24.2 = select i1 %cmp119.4, float 0.000000e+00, float 0.000000e+00
  %cmp119.5 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.30.2 = select i1 %cmp119.5, float 0.000000e+00, float 0.000000e+00
  %cmp119.6 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.36.2 = select i1 %cmp119.6, float 0.000000e+00, float 0.000000e+00
  %cmp119.7 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.42.2 = select i1 %cmp119.7, float 0.000000e+00, float 0.000000e+00
  %cmp119.8 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.48.2 = select i1 %cmp119.8, float 0.000000e+00, float 0.000000e+00
  %cmp119.9 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.54.2 = select i1 %cmp119.9, float 0.000000e+00, float 0.000000e+00
  %cmp119.10 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.66.1 = select i1 %cmp119.10, float 0.000000e+00, float 0.000000e+00
  %val.sroa.60.2 = select i1 %cmp119.10, float 0.000000e+00, float 0.000000e+00
  br label %do.body
}
```
Crash is occurring on [SLPVectorizer.cpp:16251](https://github.com/llvm/llvm-project/blob/ad1ca5f4a2bc09f99fd82e5444f5da37c2985e97/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp#L16251).
At the time `VF:12` and `Scalars.size():16`.


Comments: author: macurtis-amd
body: @alexey-bataev Thanks!

Fix patch:
commit 48a4b14cb65d56ec3bbb473887c31d251092c83c
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Wed Apr 2 05:57:16 2025 -0700

    [SLP]Fix whole vector registers calculations for compares
    
    Need to check that the calculated number of the elements is not larger
    than the original number of scalars to prevent a compiler crash.
    
    Fixes #134013

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index f9284b3a5f21..838e952c024c 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -8873,6 +8873,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
           // Find the number of elements, which forms full vectors.
           unsigned PWSz = getFullVectorNumberOfElements(
               *TTI, UniqueValues.front()->getType(), UniqueValues.size());
+          PWSz = std::min<unsigned>(PWSz, VL.size());
           if (PWSz == VL.size()) {
             ReuseShuffleIndices.clear();
           } else {


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/X86/whole-registers-compare.ll
commands: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu -slp-threshold=-99999 < %s
tests: test_name: <module>
test_body: 
define void @test() {
;
entry:
  br label %do.body

do.body:
  %val.sroa.66.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.66.1, %do.body ]
  %val.sroa.60.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.60.2, %do.body ]
  %val.sroa.54.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.54.2, %do.body ]
  %val.sroa.48.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.48.2, %do.body ]
  %val.sroa.42.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.42.2, %do.body ]
  %val.sroa.36.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.36.2, %do.body ]
  %val.sroa.30.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.30.2, %do.body ]
  %val.sroa.24.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.24.2, %do.body ]
  %val.sroa.18.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.18.2, %do.body ]
  %val.sroa.12.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.12.2, %do.body ]
  %val.sroa.6.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.6.2, %do.body ]
  %val.sroa.0.0 = phi float [ 0.000000e+00, %entry ], [ %val.sroa.0.1, %do.body ]
  %cmp119 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.6.1 = select i1 %cmp119, float 0.000000e+00, float 0.000000e+00
  %val.sroa.0.1 = select i1 %cmp119, float 0.000000e+00, float 0.000000e+00
  %cmp119.1 = fcmp uge float %val.sroa.6.1, 0.000000e+00
  %val.sroa.6.2 = select i1 %cmp119.1, float 0.000000e+00, float 0.000000e+00
  %cmp119.2 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.12.2 = select i1 %cmp119.2, float 0.000000e+00, float 0.000000e+00
  %cmp119.3 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.18.2 = select i1 %cmp119.3, float 0.000000e+00, float 0.000000e+00
  %cmp119.4 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.24.2 = select i1 %cmp119.4, float 0.000000e+00, float 0.000000e+00
  %cmp119.5 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.30.2 = select i1 %cmp119.5, float 0.000000e+00, float 0.000000e+00
  %cmp119.6 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.36.2 = select i1 %cmp119.6, float 0.000000e+00, float 0.000000e+00
  %cmp119.7 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.42.2 = select i1 %cmp119.7, float 0.000000e+00, float 0.000000e+00
  %cmp119.8 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.48.2 = select i1 %cmp119.8, float 0.000000e+00, float 0.000000e+00
  %cmp119.9 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.54.2 = select i1 %cmp119.9, float 0.000000e+00, float 0.000000e+00
  %cmp119.10 = fcmp uge float 0.000000e+00, 0.000000e+00
  %val.sroa.66.1 = select i1 %cmp119.10, float 0.000000e+00, float 0.000000e+00
  %val.sroa.60.2 = select i1 %cmp119.10, float 0.000000e+00, float 0.000000e+00
  br label %do.body
}