Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: BoUpSLP::gather
Issue title: Assertion `I->use_empty() && "trying to erase instruction with users."' failed.
Labels: llvm:SLPVectorizer
crash
Reported by: TatyanaDoubts

To reproduce run the test below with -passes=slp-vectorizer
```
; ModuleID = './reduced.ll'
source_filename = "./reduced.ll"
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-ni:1-p2:32:8:8:32-ni:2"
target triple = "x86_64-unknown-linux-gnu"

define void @wombat() gc "statepoint-example" {
bb:
  br label %bb1

bb1:                                              ; preds = %bb1, %bb
  %phi = phi i32 [ 0, %bb ], [ %or21, %bb1 ]
  %sitofp = sitofp i32 0 to float
  %fadd = fadd float %sitofp, %sitofp
  %fadd2 = fadd float %fadd, 0.000000e+00
  %fcmp = fcmp ogt float %fadd2, 0.000000e+00
  %select = select i1 %fcmp, i32 0, i32 0
  %select3 = select i1 false, i32 %select, i32 0
  %fadd4 = fadd float %sitofp, 0.000000e+00
  %fadd5 = fadd float %fadd4, 0.000000e+00
  %fcmp6 = fcmp ogt float %fadd5, 0.000000e+00
  %select7 = select i1 %fcmp6, i32 0, i32 0
  %select8 = select i1 false, i32 %select7, i32 0
  %or = or i32 %select3, %select8
  %sitofp9 = sitofp i32 0 to float
  %fmul = fmul float 0.000000e+00, 0.000000e+00
  %fadd10 = fadd float %sitofp9, %fmul
  %fadd11 = fadd float %fadd10, 0.000000e+00
  %fcmp12 = fcmp ogt float %fadd11, 0.000000e+00
  %select13 = select i1 %fcmp12, i32 0, i32 0
  %select14 = select i1 false, i32 %select13, i32 0
  %or15 = or i32 %select14, %or
  %fadd16 = fadd float %fmul, 0.000000e+00
  %fadd17 = fadd float %fadd16, 0.000000e+00
  %fcmp18 = fcmp ogt float %fadd17, 0.000000e+00
  %select19 = select i1 %fcmp18, i32 0, i32 0
  %select20 = select i1 false, i32 %select19, i32 0
  %or21 = or i32 %or15, %select20
  br label %bb1
}
```
Reproducer: https://godbolt.org/z/T8j1xEoKM
Stack dump:
```
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=slp-vectorizer <source>
1.	Running pass "function(slp-vectorizer)" on module "<source>"
2.	Running pass "slp-vectorizer" on function "wombat"
 #0 0x0000000005194b38 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5194b38)
 #1 0x000000000519252c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007eacdf842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007eacdf8969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007eacdf842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007eacdf8287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007eacdf82871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007eacdf839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x000000000419bb4f (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x419bb4f)
 #9 0x000000000425f0bf llvm::SLPVectorizerPass::runImpl(llvm::Function&, llvm::ScalarEvolution*, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo*, llvm::AAResults*, llvm::LoopInfo*, llvm::DominatorTree*, llvm::AssumptionCache*, llvm::DemandedBits*, llvm::OptimizationRemarkEmitter*) (.part.0) SLPVectorizer.cpp:0:0
#10 0x000000000425fa7b llvm::SLPVectorizerPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x425fa7b)
#11 0x0000000002fbe41e llvm::detail::PassModel<llvm::Function, llvm::SLPVectorizerPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2fbe41e)
#12 0x0000000004f949e8 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4f949e8)
#13 0x0000000000e4027e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe4027e)
#14 0x0000000004f9340e llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4f9340e)
#15 0x0000000000e3f92e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe3f92e)
#16 0x0000000004f92e40 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4f92e40)
#17 0x0000000000909fda llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x909fda)
#18 0x00000000008fc70a optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8fc70a)
#19 0x00007eacdf829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#20 0x00007eacdf829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#21 0x00000000008f3e9e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8f3e9e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```



Fix patch:
commit 79682c4d57620e623fb30271cc8003d0c9e14a01
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Tue Nov 19 11:03:11 2024 -0800

    [SLP]Check if the buildvector root is not a part of the graph before deletion
    
    If the buildvector root has no uses, it might be still needed as a part
    of the graph, so need to check that it is not a part of the graph before
    deletion.
    
    Fixes #116852

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index dc0dffd9fcbf..e70627b6afc1 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -13614,7 +13614,10 @@ Value *BoUpSLP::gather(
     } else {
       Vec = CreateShuffle(Root, Vec, Mask);
       if (auto *OI = dyn_cast<Instruction>(OriginalRoot);
-          OI && OI->hasNUses(0))
+          OI && OI->hasNUses(0) &&
+          none_of(VectorizableTree, [&](const std::unique_ptr<TreeEntry> &TE) {
+            return TE->VectorizedValue == OI;
+          }))
         eraseInstruction(OI);
     }
   }


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/X86/bv-root-part-of-graph.ll
commands: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu < %s
tests: test_name: <module>
test_body: 
define void @test() {
;
bb:
  br label %bb1

bb1:
  %phi = phi i32 [ 0, %bb ], [ %or21, %bb1 ]
  %sitofp = sitofp i32 0 to float
  %fadd = fadd float %sitofp, %sitofp
  %fadd2 = fadd float %fadd, 0.000000e+00
  %fcmp = fcmp ogt float %fadd2, 0.000000e+00
  %select = select i1 %fcmp, i32 0, i32 0
  %select3 = select i1 false, i32 %select, i32 0
  %fadd4 = fadd float %sitofp, 0.000000e+00
  %fadd5 = fadd float %fadd4, 0.000000e+00
  %fcmp6 = fcmp ogt float %fadd5, 0.000000e+00
  %select7 = select i1 %fcmp6, i32 0, i32 0
  %select8 = select i1 false, i32 %select7, i32 0
  %or = or i32 %select3, %select8
  %sitofp9 = sitofp i32 0 to float
  %fmul = fmul float 0.000000e+00, 0.000000e+00
  %fadd10 = fadd float %sitofp9, %fmul
  %fadd11 = fadd float %fadd10, 0.000000e+00
  %fcmp12 = fcmp ogt float %fadd11, 0.000000e+00
  %select13 = select i1 %fcmp12, i32 0, i32 0
  %select14 = select i1 false, i32 %select13, i32 0
  %or15 = or i32 %select14, %or
  %fadd16 = fadd float %fmul, 0.000000e+00
  %fadd17 = fadd float %fadd16, 0.000000e+00
  %fcmp18 = fcmp ogt float %fadd17, 0.000000e+00
  %select19 = select i1 %fcmp18, i32 0, i32 0
  %select20 = select i1 false, i32 %select19, i32 0
  %or21 = or i32 %or15, %select20
  br label %bb1
}