Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: BoUpSLP::vectorizeTree
Issue title: LLVM ERROR: Broken module found, compilation aborted!
Labels: llvm:SLPVectorizer
crash-on-valid
Reported by: TatyanaDoubts

To reproduce run opt with  
[Test.ll.tgz](https://github.com/user-attachments/files/16513795/Test.ll.tgz)
using `-passes slp-vectorizer -slp-threshold=-99999` 

Reproducer https://godbolt.org/z/f4szGsMYd

```
The unwind destination does not have an exception handling instruction!
  %invoke = invoke i64 null(ptr addrspace(1) null, i32 0, i64 0, i32 0)
          to label %bb65 unwind label %bb4
The unwind destination does not have an exception handling instruction!
  %invoke3 = invoke i64 null(ptr addrspace(1) null, i32 0, i64 0, i32 0)
          to label %bb65 unwind label %bb4
LandingPadInst not the first non-PHI instruction in the block.
  %landingpad = landingpad { ptr, i32 }
          cleanup
LLVM ERROR: Broken module found, compilation aborted!
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 -slp-threshold=-99999 <source>
1.	Running pass "verify" on module "<source>"
 #0 0x0000000004f1d2d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4f1d2d8)
 #1 0x0000000004f1aa4c SignalHandler(int) Signals.cpp:0:0
 #2 0x0000788c1ca42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000788c1ca969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x0000788c1ca42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x0000788c1ca287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00000000007b40fd llvm::json::operator==(llvm::json::Value const&, llvm::json::Value const&) (.cold) JSON.cpp:0:0
 #7 0x0000000004e571e8 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4e571e8)
 #8 0x0000000004d53550 llvm::VerifierPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4d53550)
 #9 0x00000000008d8a7e 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+0x8d8a7e)
#10 0x0000000004d17270 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4d17270)
#11 0x00000000008e37f2 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+0x8e37f2)
#12 0x00000000008d671c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8d671c)
#13 0x0000788c1ca29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#14 0x0000788c1ca29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#15 0x00000000008cdb7e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8cdb7e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Fix patch:
commit 2601d6f189cf1d4e4e77a7cba83e5d32c87bf079
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Tue Aug 6 12:27:36 2024 -0700

    [SLP]Fix PR102187: do not insert extractelement before landingpad instruction.
    
    Landingpad instruction must be the very first instruction after the phi
    nodes, so need to inser extractelement/shuffles after this instruction.
    
    Fixes https://github.com/llvm/llvm-project/issues/102187

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 9589d306581e..5c8873853a73 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -14049,12 +14049,19 @@ Value *BoUpSLP::vectorizeTree(
              "ExternallyUsedValues map or remain as scalar in vectorized "
              "instructions");
       if (auto *VecI = dyn_cast<Instruction>(Vec)) {
-        if (auto *PHI = dyn_cast<PHINode>(VecI))
-          Builder.SetInsertPoint(PHI->getParent(),
-                                 PHI->getParent()->getFirstNonPHIIt());
-        else
+        if (auto *PHI = dyn_cast<PHINode>(VecI)) {
+          if (PHI->getParent()->isLandingPad())
+            Builder.SetInsertPoint(
+                PHI->getParent(),
+                std::next(
+                    PHI->getParent()->getLandingPadInst()->getIterator()));
+          else
+            Builder.SetInsertPoint(PHI->getParent(),
+                                   PHI->getParent()->getFirstNonPHIIt());
+        } else {
           Builder.SetInsertPoint(VecI->getParent(),
                                  std::next(VecI->getIterator()));
+        }
       } else {
         Builder.SetInsertPoint(&F->getEntryBlock(), F->getEntryBlock().begin());
       }
@@ -14080,11 +14087,18 @@ Value *BoUpSLP::vectorizeTree(
             auto VecIt = VectorCasts.find(Key);
             if (VecIt == VectorCasts.end()) {
               IRBuilderBase::InsertPointGuard Guard(Builder);
-              if (auto *IVec = dyn_cast<PHINode>(Vec))
-                Builder.SetInsertPoint(
-                    IVec->getParent()->getFirstNonPHIOrDbgOrLifetime());
-              else if (auto *IVec = dyn_cast<Instruction>(Vec))
+              if (auto *IVec = dyn_cast<PHINode>(Vec)) {
+                if (IVec->getParent()->isLandingPad())
+                  Builder.SetInsertPoint(IVec->getParent(),
+                                         std::next(IVec->getParent()
+                                                       ->getLandingPadInst()
+                                                       ->getIterator()));
+                else
+                  Builder.SetInsertPoint(
+                      IVec->getParent()->getFirstNonPHIOrDbgOrLifetime());
+              } else if (auto *IVec = dyn_cast<Instruction>(Vec)) {
                 Builder.SetInsertPoint(IVec->getNextNonDebugInstruction());
+              }
               Vec = Builder.CreateIntCast(
                   Vec,
                   getWidenedType(


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/X86/extractelement-phi-in-landingpad.ll
commands: opt -S --passes=slp-vectorizer -slp-threshold=-99999 -mtriple=x86_64-unknown-linux-gnu < %s
tests: test_name: test
test_body: define void @test() personality ptr null {
bb:
  invoke void null()
          to label %bb65 unwind label %bb4

bb2:                                              ; No predecessors!
  invoke void null()
          to label %bb65 unwind label %bb4

bb4:                                              ; preds = %bb2, %bb
  %phi5 = phi i32 [ 0, %bb ], [ 0, %bb2 ]
  %phi6 = phi i32 [ 0, %bb ], [ 0, %bb2 ]
  %landingpad = landingpad { ptr, i32 }
          cleanup
  call void null(i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5, i32 %phi5)
  ret void

bb65:                                             ; preds = %bb2, %bb
  ret void
}