Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp: LoopVectorizationLegality::canVectorizeLoopCFG
LoopVectorizationLegality::canVectorizeLoopNestCFG
Issue title: crashes with assertion failure when running opt -passes=loop-vectorize
Labels: vectorizers
crash
Reported by: k-arrows

Run opt with -passes=loop-vectorize

Reproducer:
https://godbolt.org/z/fMaPMYhE5

Backtrace:
```console
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp:381: bool canonicalHeaderAndLatch(llvm::VPBlockBase*, const llvm::VPDominatorTree&): Assertion `cast<VPInstruction>(Term)->getOpcode() == VPInstruction::BranchOnCond && "terminator must be a BranchOnCond"' 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=loop-vectorize <source>
1.	Running pass "function(loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>)" on module "<source>"
2.	Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "non_branch_terminator"
 #0 0x000000000579c848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x579c848)
 #1 0x00000000057996f4 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x0000720f57a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000720f57a969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x0000720f57a42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x0000720f57a287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x0000720f57a2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x0000720f57a39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x00000000036a7074 canonicalHeaderAndLatch(llvm::VPBlockBase*, llvm::VPDominatorTree const&) VPlanConstruction.cpp:0:0
 #9 0x00000000036ac8d1 llvm::VPlanTransforms::buildVPlan0(llvm::Loop*, llvm::LoopInfo&, llvm::Type*, llvm::DebugLoc, llvm::PredicatedScalarEvolution&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x36ac8d1)
#10 0x000000000352862e llvm::LoopVectorizationPlanner::buildVPlansWithVPRecipes(llvm::ElementCount, llvm::ElementCount) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x352862e)
#11 0x00000000035290c4 llvm::LoopVectorizationPlanner::plan(llvm::ElementCount, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x35290c4)
#12 0x000000000352ce82 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x352ce82)
#13 0x00000000035302d0 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x35302d0)
#14 0x000000000353097d llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x353097d)
#15 0x0000000002e2700e llvm::detail::PassModel<llvm::Function, llvm::LoopVectorizePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2e2700e)
#16 0x00000000055886f1 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x55886f1)
#17 0x0000000000edfbae 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+0xedfbae)
#18 0x0000000005586c5a llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5586c5a)
#19 0x000000000096cd6e 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+0x96cd6e)
#20 0x0000000005586611 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5586611)
#21 0x0000000000977068 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, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x977068)
#22 0x000000000096b0d7 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x96b0d7)
#23 0x0000720f57a29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#24 0x0000720f57a29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#25 0x00000000009621f5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x9621f5)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Fix patch:
commit 5e3ac2a6f22aa4d1d055c2d430913a960b9bb60b
Author: Florian Hahn <flo@fhahn.com>
Date:   Sun Oct 12 21:20:35 2025 +0100

    [LV] Bail out on loops with switch as latch terminator.
    
    Currently we cannot vectorize loops with latch blocks terminated by a
    switch. In the future this could be handled by materializing appropriate
    compares.
    
    Fixes https://github.com/llvm/llvm-project/issues/156894.

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 7d376c370bb1..fdfff1613209 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1642,6 +1642,19 @@ bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp,
       return false;
   }
 
+  // The latch must be terminated by a BranchInst.
+  BasicBlock *Latch = Lp->getLoopLatch();
+  if (Latch && !isa<BranchInst>(Latch->getTerminator())) {
+    reportVectorizationFailure(
+        "The loop latch terminator is not a BranchInst",
+        "loop control flow is not understood by vectorizer", "CFGNotUnderstood",
+        ORE, TheLoop);
+    if (DoExtraAnalysis)
+      Result = false;
+    else
+      return false;
+  }
+
   return Result;
 }
 


Regression tests:
file: llvm/test/Transforms/LoopVectorize/loop-form.ll
commands: opt -S -passes=loop-vectorize -force-vector-width=2 < %s
opt -S -passes=loop-vectorize -force-vector-width=2  -prefer-predicate-over-epilogue=predicate-dont-vectorize < %s
tests: test_name: switch_in_latch
test_body: target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

define void @switch_in_latch(ptr %a) {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
  %gep = getelementptr i32, ptr %a, i32 %iv
  store i32 1, ptr %gep, align 4
  %iv.next = add i32 %iv, 1
  switch i32 %iv.next, label %loop [
    i32 100, label %exit
  ]

exit:                                             ; preds = %loop
  ret void
}