Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp: preparePlanForEpilogueVectorLoop
Issue title: Assertion `EPResumeVal && "must have a resume value for the canonical IV"' failed.
Labels: vectorizers
crash
Reported by: TatyanaDoubts

To reproduce run opt with the test below using -passes=loop-vectorize:
```
; 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 i32 @wombat() gc "statepoint-example" {
bb:
  br label %bb2

bb1:                                              ; preds = %bb2
  %phi = phi i8 [ %trunc, %bb2 ]
  ret i32 0

bb2:                                              ; preds = %bb2, %bb
  %phi3 = phi i32 [ 0, %bb ], [ %add5, %bb2 ]
  %phi4 = phi i32 [ 0, %bb ], [ %add, %bb2 ]
  %add = add i32 %phi4, -16777216
  %trunc = trunc i32 %phi4 to i8
  %add5 = add i32 %phi3, 1
  %icmp = icmp ugt i32 %phi3, 1050
  br i1 %icmp, label %bb1, label %bb2
}
```
Reproducer: https://godbolt.org/z/xz3sEWhTY
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 "wombat"
 #0 0x00000000056729e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x56729e8)
 #1 0x000000000566f894 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007685c0442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007685c04969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007685c0442476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007685c04287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007685c042871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007685c0439e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x00000000034821cd preparePlanForEpilogueVectorLoop(llvm::VPlan&, llvm::Loop*, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const&, llvm::EpilogueLoopVectorizationInfo const&) LoopVectorize.cpp:0:0
 #9 0x00000000034a4c56 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x34a4c56)
#10 0x00000000034a6690 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x34a6690)
#11 0x00000000034a6cf7 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x34a6cf7)
#12 0x0000000002db62be 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+0x2db62be)
#13 0x0000000005464431 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5464431)
#14 0x0000000000ebde5e 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+0xebde5e)
#15 0x0000000005464904 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5464904)
#16 0x00000000009637fe 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+0x9637fe)
#17 0x00000000054624d1 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x54624d1)
#18 0x000000000096daca 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+0x96daca)
#19 0x0000000000961b7b optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x961b7b)
#20 0x00007685c0429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#21 0x00007685c0429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#22 0x0000000000958c35 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x958c35)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```


Fix patch:
commit eee9755881bf39bc15e6e05697abbbfabb51cb78
Author: Florian Hahn <flo@fhahn.com>
Date:   Fri Aug 1 20:54:38 2025 +0100

    [LV] Refine check to find epilogue IV resume value.
    
    Make sure to check that the vector trip count is containedin the list of
    incoming values to serve as tie-breaker with phis with all-zero incoming
    values.
    
    Fixes https://github.com/llvm/llvm-project/issues/151686.

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 20528089c000..b4ea70e0e5cc 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9788,6 +9788,10 @@ preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L,
                 match(
                     P.getIncomingValueForBlock(EPI.MainLoopIterationCountCheck),
                     m_SpecificInt(0)) &&
+                any_of(P.incoming_values(),
+                       [&EPI](Value *Inc) {
+                         return Inc == EPI.VectorTripCount;
+                       }) &&
                 all_of(P.incoming_values(), [&EPI](Value *Inc) {
                   return Inc == EPI.VectorTripCount ||
                          match(Inc, m_SpecificInt(0));


Regression tests:
file: llvm/test/Transforms/LoopVectorize/X86/epilog-vectorization-inductions.ll
commands: opt -passes=loop-vectorize -mcpu=znver2 -S %s
tests: test_name: multiple_inductions_start_at_0
test_body: target triple = "x86_64-unknown-linux-gnu"

define i8 @multiple_inductions_start_at_0() {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
  %iv.2 = phi i32 [ 0, %entry ], [ %add, %loop ]
  %add = add i32 %iv.2, -16777216
  %trunc = trunc i32 %iv.2 to i8
  %iv.next = add i32 %iv, 1
  %ec = icmp ugt i32 %iv, 1050
  br i1 %ec, label %exit, label %loop

exit:                                             ; preds = %loop
  %res = phi i8 [ %trunc, %loop ]
  ret i8 %res
}