Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: BoUpSLP::vectorizeTree
Issue title: Instruction does not dominate all uses! LLVM ERROR: Broken module found, compilation aborted! (yet another)
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() #0 gc "statepoint-example" {
bb:
  br label %bb1

bb1:                                              ; preds = %bb4, %bb
  %phi = phi i32 [ 0, %bb ], [ %add6, %bb4 ]
  %phi2 = phi i32 [ 0, %bb ], [ %add, %bb4 ]
  %or = or i32 %phi2, 0
  %or3 = or i32 %phi, 0
  br i1 false, label %bb7, label %bb4

bb4:                                              ; preds = %bb1
  %add = add i32 0, 0
  %add5 = add i32 0, 0
  %add6 = add i32 %phi, 0
  br i1 false, label %bb7, label %bb1

bb7:                                              ; preds = %bb4, %bb1
  %phi8 = phi i32 [ %or, %bb1 ], [ %add5, %bb4 ]
  %phi9 = phi i32 [ %or3, %bb1 ], [ %add6, %bb4 ]
  ret void
}

attributes #0 = { "target-features"="+prfchw,-cldemote,+avx,+aes,+sahf,+pclmul,-xop,+crc32,-amx-fp8,+xsaves,-avx512fp16,-usermsr,-sm4,-egpr,+sse4.1,-avx512ifma,+xsave,+sse4.2,-tsxldtrk,-sm3,-ptwrite,-widekl,-movrs,+invpcid,+64bit,+xsavec,-avx10.1-512,-avx512vpopcntdq,+cmov,-avx512vp2intersect,+avx512cd,+movbe,-avxvnniint8,-ccmp,-amx-int8,-kl,-avx10.1-256,+evex512,-avxvnni,+rtm,+adx,+avx2,-hreset,-movdiri,-serialize,-sha512,-vpclmulqdq,+avx512vl,-uintr,-cf,+clflushopt,-raoint,-cmpccxadd,+bmi,-amx-tile,+sse,-avx10.2-256,-gfni,-avxvnniint16,-amx-fp16,-zu,-ndd,+xsaveopt,+rdrnd,+avx512f,-amx-bf16,-avx512bf16,-avx512vnni,-push2pop2,+cx8,+avx512bw,+sse3,+pku,-nf,-amx-tf32,-amx-avx512,+fsgsbase,-clzero,-mwaitx,-lwp,+lzcnt,-sha,-movdir64b,-ppx,-wbnoinvd,-enqcmd,-amx-transpose,-avx10.2-512,-avxneconvert,-tbm,-pconfig,-amx-complex,+ssse3,+cx16,+bmi2,+fma,+popcnt,-avxifma,+f16c,-avx512bitalg,-rdpru,+clwb,+mmx,+sse2,+rdseed,-avx512vbmi2,-prefetchi,-amx-movrs,-rdpid,-fma4,-avx512vbmi,-shstk,-vaes,-waitpkg,-sgx,+fxsr,+avx512dq,-sse4a,-avx512f" }
```
Reproducer: https://godbolt.org/z/Kqddzds1c

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 "verify" on module "<source>"
 #0 0x00000000051b4a48 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x51b4a48)
 #1 0x00000000051b243c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007c6fb5442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007c6fb54969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007c6fb5442476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007c6fb54287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00000000007e2469 llvm::json::operator==(llvm::json::Value const&, llvm::json::Value const&) (.cold) JSON.cpp:0:0
 #7 0x00000000050f32d8 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x50f32d8)
 #8 0x0000000004ff2498 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ff2498)
 #9 0x00000000009057ae llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x9057ae)
#10 0x0000000004fb1d10 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fb1d10)
#11 0x0000000000910d7a 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+0x910d7a)
#12 0x00000000009033f9 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x9033f9)
#13 0x00007c6fb5429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#14 0x00007c6fb5429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#15 0x00000000008facde _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8facde)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Fix patch:
commit 376dad72abb4960cd93e37e3e98de3950b5c3dfe
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Fri Dec 6 13:39:05 2024 -0800

    [SLP]Move resulting vector before inert point, if the late generated buildvector fully matched
    
    If the perfect diamond match was detected for the postponed buildvectors
    and the vector for the previous node comes after the current node, need
    to move the vector register before the current inserting point to
    prevent compiler crash.
    
    Fixes #119002

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 51841a842ce0..50590e51ae0e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -16215,6 +16215,11 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
     }
     Builder.SetCurrentDebugLocation(UserI->getDebugLoc());
     Value *Vec = vectorizeTree(TE, /*PostponedPHIs=*/false);
+    if (auto *VecI = dyn_cast<Instruction>(Vec);
+        VecI && VecI->getParent() == Builder.GetInsertBlock() &&
+        Builder.GetInsertPoint()->comesBefore(VecI))
+      VecI->moveBeforePreserving(*Builder.GetInsertBlock(),
+                                 Builder.GetInsertPoint());
     if (Vec->getType() != PrevVec->getType()) {
       assert(Vec->getType()->isIntOrIntVectorTy() &&
              PrevVec->getType()->isIntOrIntVectorTy() &&


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/X86/perfect-matched-reused-bv.ll
commands: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f < %s
tests: test_name: test
test_body: define void @test() {
bb:
  br label %bb1

bb1:                                              ; preds = %bb4, %bb
  %phi = phi i32 [ 0, %bb ], [ %add6, %bb4 ]
  %phi2 = phi i32 [ 0, %bb ], [ %add, %bb4 ]
  %or = or i32 %phi2, 0
  %or3 = or i32 %phi, 0
  br i1 false, label %bb7, label %bb4

bb4:                                              ; preds = %bb1
  %add = add i32 0, 0
  %add5 = add i32 0, 0
  %add6 = add i32 %phi, 0
  br i1 false, label %bb7, label %bb1

bb7:                                              ; preds = %bb4, %bb1
  %phi8 = phi i32 [ %or, %bb1 ], [ %add5, %bb4 ]
  %phi9 = phi i32 [ %or3, %bb1 ], [ %add6, %bb4 ]
  ret void
}