Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: BoUpSLP::vectorizeTree
Issue title: [SLPVectorizer] Assertion `(I->use_empty() || all_of(I->uses(), [&](Use &U) { return isDeleted( cast<Instruction>(U.getUser())); })) && "trying to erase instruction with users."' failed.
Labels: llvm:SLPVectorizer
crash-on-valid
Reported by: dtcxzyw

Reduced reproducer: https://godbolt.org/z/azEc6zjod
```
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 i1 @foo() {
entry:
  %tobool.not.not509.i.1311.i.i = icmp ne i32 0, 0
  %cmp80.1319.i.i189 = icmp sgt i32 0, 0
  %tobool.not.not509.i.1.1.i.i = icmp ne i32 0, 0
  %cmp80.1.1.i.i190 = icmp sgt i32 0, 0
  %tobool.not.not509.i.2329.i.i = icmp ne i32 0, 0
  %storemerge.2333.i.i = select i1 %tobool.not.not509.i.2329.i.i, i32 0, i32 0
  %cmp80.2337.i.i192 = icmp sgt i32 0, 0
  %tobool.not.not509.i.1.2.i.i = icmp ne i32 %storemerge.2333.i.i, 0
  %cmp80.1.2.i.i193 = icmp sgt i32 0, 0
  %cmp80.1.2.i.i = select i1 %tobool.not.not509.i.1.2.i.i, i1 %cmp80.1.2.i.i193, i1 false
  %0 = select i1 %cmp80.1.2.i.i, i1 %tobool.not.not509.i.2329.i.i, i1 false
  %1 = select i1 %0, i1 %cmp80.2337.i.i192, i1 false
  %2 = select i1 %1, i1 false, i1 false
  %3 = select i1 %2, i1 %tobool.not.not509.i.1.1.i.i, i1 false
  %4 = select i1 %3, i1 %cmp80.1.1.i.i190, i1 false
  %5 = select i1 %4, i1 %tobool.not.not509.i.1311.i.i, i1 false
  %6 = select i1 %5, i1 %cmp80.1319.i.i189, i1 false
  ret i1 %6
}
```
```
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:2748: void llvm::slpvectorizer::BoUpSLP::removeInstructionsAndOperands(llvm::ArrayRef<U*>) [with T = llvm::Instruction]: Assertion `(I->use_empty() || all_of(I->uses(), [&](Use &U) { return isDeleted( cast<Instruction>(U.getUser())); })) && "trying to erase instruction with users."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
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 "foo"
 #0 0x00000000050d8608 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x50d8608)
 #1 0x00000000050d5fbc SignalHandler(int) Signals.cpp:0:0
 #2 0x000071afd7642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000071afd76969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x000071afd7642476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x000071afd76287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x000071afd762871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x000071afd7639e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x000000000414e3af void llvm::slpvectorizer::BoUpSLP::removeInstructionsAndOperands<llvm::Instruction>(llvm::ArrayRef<llvm::Instruction*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x414e3af)
 #9 0x00000000041aa335 llvm::slpvectorizer::BoUpSLP::vectorizeTree(llvm::SmallDenseSet<llvm::Value*, 4u, llvm::DenseMapInfo<llvm::Value*, void>> const&, llvm::Instruction*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41aa335)
#10 0x00000000041bcd90 (anonymous namespace)::HorizontalReduction::tryToReduce(llvm::slpvectorizer::BoUpSLP&, llvm::DataLayout const&, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo const&) SLPVectorizer.cpp:0:0
#11 0x00000000041bfb71 llvm::SLPVectorizerPass::vectorizeHorReduction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&, llvm::SmallVectorImpl<llvm::WeakTrackingVH>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41bfb71)
#12 0x00000000041c431b llvm::SLPVectorizerPass::vectorizeRootInstruction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (.constprop.0) SLPVectorizer.cpp:0:0
#13 0x00000000041c8ae8 llvm::SLPVectorizerPass::vectorizeChainsInBlock(llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41c8ae8)
#14 0x00000000041cf231 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
#15 0x00000000041cfedb llvm::SLPVectorizerPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41cfedb)
#16 0x0000000002f64f0e 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+0x2f64f0e)
#17 0x0000000004ed9788 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed9788)
#18 0x0000000000e2773e 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+0xe2773e)
#19 0x0000000004ed81ae llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed81ae)
#20 0x0000000000e26e8e 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+0xe26e8e)
#21 0x0000000004ed7be0 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed7be0)
#22 0x000000000090a07a 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+0x90a07a)
#23 0x00000000008fce7c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8fce7c)
#24 0x000071afd7629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#25 0x000071afd7629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#26 0x00000000008f493e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8f493e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```
llvm version: 33363521ca24f912cc25530f6cecbca53acce8a3


Fix patch:
commit d2e7ee77d33e8b3be3b1d4e9bc5bc4c60b62b554
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Thu Oct 24 08:15:21 2024 -0700

    [SLP]Do not check for clustered loads only
    
    Since SLP support "clusterization" of the non-load instructions, the
    restriction for reduced values for loads only should be removed to avoid
    compiler crash.
    
    Fixes #113516

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index f76e913a57d5..012d85353f08 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -16118,7 +16118,7 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
     for (Instruction *I : RemovedInsts) {
       const TreeEntry *IE = getTreeEntry(I);
       if (IE->Idx != 0 &&
-          !(VectorizableTree.front()->isGather() && isa<LoadInst>(I) &&
+          !(VectorizableTree.front()->isGather() &&
             !IE->UserTreeIndices.empty() &&
             any_of(IE->UserTreeIndices,
                    [&](const EdgeInfo &EI) {


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/X86/non-load-reduced-as-part-of-bv.ll
commands: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu < %s
tests: test_name: foo
test_body: define i1 @foo() {
entry:
  %tobool.not.not509.i.1311.i.i = icmp ne i32 0, 0
  %cmp80.1319.i.i189 = icmp sgt i32 0, 0
  %tobool.not.not509.i.1.1.i.i = icmp ne i32 0, 0
  %cmp80.1.1.i.i190 = icmp sgt i32 0, 0
  %tobool.not.not509.i.2329.i.i = icmp ne i32 0, 0
  %storemerge.2333.i.i = select i1 %tobool.not.not509.i.2329.i.i, i32 0, i32 0
  %cmp80.2337.i.i192 = icmp sgt i32 0, 0
  %tobool.not.not509.i.1.2.i.i = icmp ne i32 %storemerge.2333.i.i, 0
  %cmp80.1.2.i.i193 = icmp sgt i32 0, 0
  %cmp80.1.2.i.i = select i1 %tobool.not.not509.i.1.2.i.i, i1 %cmp80.1.2.i.i193, i1 false
  %0 = select i1 %cmp80.1.2.i.i, i1 %tobool.not.not509.i.2329.i.i, i1 false
  %1 = select i1 %0, i1 %cmp80.2337.i.i192, i1 false
  %2 = select i1 %1, i1 false, i1 false
  %3 = select i1 %2, i1 %tobool.not.not509.i.1.1.i.i, i1 false
  %4 = select i1 %3, i1 %cmp80.1.1.i.i190, i1 false
  %5 = select i1 %4, i1 %tobool.not.not509.i.1311.i.i, i1 false
  %6 = select i1 %5, i1 %cmp80.1319.i.i189, i1 false
  ret i1 %6
}