Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp: addUsersInExitBlock
Issue title: Assertion `(VF.isScalar() || Def->isLiveIn() || hasVectorValue(Def, Part) || (hasScalarValue(Def, VPIteration(Part, 0)) && Data.PerPartScalars[Def][Part].size() == 1)) && "Trying to access a single scalar per part but has multiple scalars " "per part."' failed.
Labels: vectorizers
crash
Reported by: TatyanaDoubts

To reproduce run opt opt with -passes loop-vectorize with the following test:
```
; 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(i64 %arg) #0 gc "statepoint-example" {
bb:
  br label %bb1

bb1:                                              ; preds = %bb3, %bb
  %phi = phi i64 [ 0, %bb ], [ %add, %bb3 ]
  br i1 false, label %bb2, label %bb3

bb2:                                              ; preds = %bb1
  %or = or disjoint i64 %phi, 0
  %getelementptr = getelementptr i32, ptr addrspace(1) null, i64 %or
  store i32 0, ptr addrspace(1) %getelementptr, align 4
  br label %bb3

bb3:                                              ; preds = %bb2, %bb1
  %add = add i64 %phi, 1
  %icmp = icmp ult i64 %phi, %arg
  br i1 %icmp, label %bb1, label %bb4

bb4:                                              ; preds = %bb3
  %phi5 = phi i64 [ %phi, %bb3 ]
  ret void
}

attributes #0 = { "target-features"="+prfchw,-cldemote,+avx,+aes,+sahf,+pclmul,-xop,+crc32,+xsaves,-avx512fp16,-usermsr,-sm4,-egpr,+sse4.1,-avx512ifma,+xsave,+sse4.2,-tsxldtrk,-sm3,-ptwrite,-widekl,+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,-gfni,-avxvnniint16,-amx-fp16,-ndd,+xsaveopt,+rdrnd,+avx512f,-amx-bf16,-avx512bf16,-avx512vnni,-push2pop2,+cx8,+avx512bw,+sse3,+pku,+fsgsbase,-clzero,-mwaitx,-lwp,+lzcnt,-sha,-movdir64b,-ppx,-wbnoinvd,-enqcmd,-avxneconvert,-tbm,-pconfig,-amx-complex,+ssse3,+cx16,+bmi2,+fma,+popcnt,-avxifma,+f16c,-avx512bitalg,-rdpru,+clwb,+mmx,+sse2,+rdseed,-avx512vbmi2,-prefetchi,-rdpid,-fma4,-avx512vbmi,-shstk,-vaes,-waitpkg,-sgx,+fxsr,+avx512dq,-sse4a,-avx512f" }
```
Reproducer:
https://godbolt.org/z/Ghz3147nj

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 0x0000000004e9b948 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4e9b948)
 #1 0x0000000004e990bc SignalHandler(int) Signals.cpp:0:0
 #2 0x000070edf8442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000070edf84969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x000070edf8442476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x000070edf84287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x000070edf842871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x000070edf8439e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x00000000040395f7 llvm::VPTransformState::get(llvm::VPValue*, unsigned int, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x40395f7)
 #9 0x0000000004056ece llvm::VPInstruction::generatePerPart(llvm::VPTransformState&, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4056ece)
#10 0x00000000040595ee llvm::VPInstruction::execute(llvm::VPTransformState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x40595ee)
#11 0x0000000004030a1f llvm::VPBasicBlock::executeRecipes(llvm::VPTransformState*, llvm::BasicBlock*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4030a1f)
#12 0x00000000040320e8 llvm::VPBasicBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x40320e8)
#13 0x0000000004030612 llvm::VPRegionBlock::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4030612)
#14 0x0000000004039946 llvm::VPlan::execute(llvm::VPTransformState*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4039946)
#15 0x0000000003f1d475 llvm::LoopVectorizationPlanner::executePlan(llvm::ElementCount, unsigned int, llvm::VPlan&, llvm::InnerLoopVectorizer&, llvm::DominatorTree*, bool, llvm::DenseMap<llvm::SCEV const*, llvm::Value*, llvm::DenseMapInfo<llvm::SCEV const*, void>, llvm::detail::DenseMapPair<llvm::SCEV const*, llvm::Value*>> const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3f1d475)
#16 0x0000000003f332ea llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3f332ea)
#17 0x0000000003f35a29 llvm::LoopVectorizePass::runImpl(llvm::Function&, llvm::ScalarEvolution&, llvm::LoopInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::BlockFrequencyInfo*, llvm::TargetLibraryInfo*, llvm::DemandedBits&, llvm::AssumptionCache&, llvm::LoopAccessInfoManager&, llvm::OptimizationRemarkEmitter&, llvm::ProfileSummaryInfo*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3f35a29)
#18 0x0000000003f3614b llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3f3614b)
#19 0x0000000002e212be 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+0x2e212be)
#20 0x0000000004c9f2d8 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4c9f2d8)
#21 0x0000000000dc9bee 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+0xdc9bee)
#22 0x0000000004c9dd46 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4c9dd46)
#23 0x0000000000dca1fe 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+0xdca1fe)
#24 0x0000000004c9bf10 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4c9bf10)
#25 0x00000000008db1d2 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+0x8db1d2)
#26 0x00000000008ce0fc optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8ce0fc)
#27 0x000070edf8429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#28 0x000070edf8429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#29 0x00000000008c556e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8c556e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```

Comments: author: fhahn
body: Interesting, let me take a look

Fix patch:
commit fc9cd3272b50f4ee9f18c4ab82c278bbb014d99f
Author: Florian Hahn <flo@fhahn.com>
Date:   Sun Jul 14 20:49:03 2024 +0100

    [VPlan] Don't add live-outs for IV phis.
    
    Resume and exit values for inductions are currently still created
    outside of VPlan and independent of the induction recipes. Don't add
    live-outs for now, as the additional unneeded users can pessimize other
    anlysis.
    
    Fixes https://github.com/llvm/llvm-project/issues/98660.

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 7d37d67cde29..5520baef7152 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -8693,6 +8693,14 @@ static void addUsersInExitBlock(VPBasicBlock *HeaderVPBB, Loop *OrigLoop,
     Value *IncomingValue =
         ExitPhi.getIncomingValueForBlock(ExitingBB);
     VPValue *V = Builder.getVPValueOrAddLiveIn(IncomingValue, Plan);
+    // Exit values for inductions are computed and updated outside of VPlan and
+    // independent of induction recipes.
+    // TODO: Compute induction exit values in VPlan, use VPLiveOuts to update
+    // live-outs.
+    if ((isa<VPWidenIntOrFpInductionRecipe>(V) &&
+         !cast<VPWidenIntOrFpInductionRecipe>(V)->getTruncInst()) ||
+        isa<VPWidenPointerInductionRecipe>(V))
+      continue;
     Plan.addLiveOut(&ExitPhi, V);
   }
 }


Regression tests:
file: llvm/test/Transforms/LoopVectorize/AArch64/sve-live-out-pointer-induction.ll
commands: opt -passes=loop-vectorize -mtriple=aarch64-unknown -mattr=+sve -S %s
tests: test_name: test
test_body: define ptr @test(ptr %start.1, ptr %start.2, ptr %end) {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv.1 = phi ptr [ %start.1, %entry ], [ %iv.1.next, %loop ]
  %iv.2 = phi ptr [ %start.2, %entry ], [ %iv.2.next, %loop ]
  store i64 0, ptr %iv.2, align 4
  %iv.2.next = getelementptr inbounds ptr, ptr %iv.2, i64 1
  %iv.1.next = getelementptr inbounds ptr, ptr %iv.1, i64 1
  %cmp.i.i.not.i = icmp eq ptr %iv.2.next, %end
  br i1 %cmp.i.i.not.i, label %exit, label %loop

exit:                                             ; preds = %loop
  %res.lcssa = phi ptr [ %iv.1, %loop ]
  ret ptr %res.lcssa
}

file: llvm/test/Transforms/LoopVectorize/X86/ephemeral-recipes.ll
commands: opt -p loop-vectorize -S %s
tests: test_name: ephemeral_load_and_compare_iv_used_outside
test_body: target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx"

define i32 @ephemeral_load_and_compare_iv_used_outside(ptr %start, ptr %end) #0 {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv = phi ptr [ %start, %entry ], [ %iv.next, %loop ]
  %iv.next = getelementptr nusw i8, ptr %iv, i64 -8
  %l1 = load i32, ptr %iv, align 4
  %cmp = icmp ne i32 %l1, 0
  call void @llvm.assume(i1 %cmp)
  %cmp.not = icmp eq ptr %iv, %end
  br i1 %cmp.not, label %exit, label %loop

exit:                                             ; preds = %loop
  %final.load = load i32, ptr %iv, align 4
  ret i32 %final.load
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #1

attributes #0 = { "target-cpu"="skylake-avx512" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

file: llvm/test/Transforms/LoopVectorize/X86/iv-live-outs.ll
commands: opt -p loop-vectorize -mcpu=skylake-avx512 -S %s
tests: test_name: <module>
test_body: 
target triple = "x86_64-unknown-linux-gnu"

define i64 @test_pr98660(ptr %dst, i64 %N) {
;
entry:
  br label %loop.header

loop.header:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]
  %or = or disjoint i64 %iv, 1
  %gep = getelementptr i32, ptr %dst, i64 %or
  %l = load i32, ptr %gep
  %c = icmp eq i32 %l, 0
  br i1 %c, label %then, label %loop.latch

then:
  store i32 0, ptr %gep, align 4
  br label %loop.latch

loop.latch:
  %iv.next = add i64 %iv, 1
  %ec = icmp ult i64 %iv, %N
  br i1 %ec, label %loop.header, label %exit

exit:
  %ret = phi i64 [ %iv, %loop.latch ]
  ret i64 %ret
}
;.
;.
file: llvm/test/Transforms/LoopVectorize/iv_outside_user.ll
commands: opt -S -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=2 < %s
opt -S -passes=loop-vectorize -force-vector-interleave=2 -force-vector-width=1 < %s
tests: test_name: iv_2_dead_in_loop_only_used_outside
test_body: define i32 @iv_2_dead_in_loop_only_used_outside(ptr %ptr) {
entry:
  br label %loop

loop:                                             ; preds = %loop, %entry
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %iv.2 = phi i32 [ 0, %entry ], [ %iv.2.next, %loop ]
  %iv.next = add nuw i64 %iv, 1
  %iv.2.next = add nuw i32 %iv.2, 2
  %gep.ptr = getelementptr inbounds i64, ptr %ptr, i64 %iv
  store i64 %iv, ptr %gep.ptr, align 4
  %exitcond = icmp ugt i64 %iv, 1000
  br i1 %exitcond, label %exit, label %loop

exit:                                             ; preds = %loop
  %iv.2.lcssa = phi i32 [ %iv.2, %loop ]
  ret i32 %iv.2.lcssa
}

file: llvm/test/Transforms/LoopVectorize/no-fold-tail-by-masking-iv-external-uses.ll
commands: opt < %s -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S
tests: test_name: test
test_body: define i32 @test(ptr %arr, i64 %n) {
entry:
  %cmp1 = icmp ugt i64 %n, 1
  br i1 %cmp1, label %preheader, label %done

preheader:                                        ; preds = %entry
  br label %loop

loop:                                             ; preds = %loop, %preheader
  %conv = phi i64 [ %conv2, %loop ], [ 1, %preheader ]
  %i = phi i8 [ %inc, %loop ], [ 1, %preheader ]
  %sub = add nsw i64 %conv, -1
  %ptr = getelementptr inbounds i32, ptr %arr, i64 %sub
  store i32 65, ptr %ptr, align 4
  %inc = add i8 %i, 1
  %conv2 = zext i8 %inc to i64
  %cmp2 = icmp ult i64 %conv2, %n
  br i1 %cmp2, label %loop, label %load_val, !llvm.loop !0

load_val:                                         ; preds = %loop
  %final = phi i64 [ %conv, %loop ]
  %ptr2 = getelementptr inbounds i32, ptr %arr, i64 %final
  %val = load i32, ptr %ptr2, align 4
  br label %done

done:                                             ; preds = %load_val, %entry
  %value = phi i32 [ %val, %load_val ], [ 0, %entry ]
  ret i32 %value
}

!0 = distinct !{!0, !1, !2, !3}
!1 = !{!"llvm.loop.unroll.disable"}
!2 = !{!"llvm.loop.vectorize.predicate.enable", i1 true}
!3 = !{!"llvm.loop.vectorize.enable", i1 true}

file: llvm/test/Transforms/LoopVectorize/pr58811-scev-expansion.ll
commands: opt -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S %s
tests: test_name: test1_pr58811
test_body: define void @test1_pr58811() {
entry:
  br label %loop.1.preheader

loop.1.preheader:                                 ; preds = %unreachable.bb, %entry
  %iv.1.ph = phi i32 [ %sub93.2, %unreachable.bb ], [ 0, %entry ]
  br label %loop.1

loop.1:                                           ; preds = %loop.1, %loop.1.preheader
  %iv.1 = phi i32 [ %iv.1.next, %loop.1 ], [ %iv.1.ph, %loop.1.preheader ]
  %iv.2 = phi i32 [ %iv.2.next, %loop.1 ], [ 0, %loop.1.preheader ]
  %iv.2.next = add i32 %iv.2, 1
  %iv.1.next = add i32 %iv.2, %iv.1
  br i1 false, label %loop.1, label %loop.2.preheader

loop.2.preheader:                                 ; preds = %loop.1
  %iv.1.lcssa = phi i32 [ %iv.1, %loop.1 ]
  br label %loop.2

loop.2:                                           ; preds = %loop.2, %loop.2.preheader
  %iv.3 = phi i16 [ %iv.3.next, %loop.2 ], [ 0, %loop.2.preheader ]
  %iv.4 = phi i32 [ %iv.4.next, %loop.2 ], [ 0, %loop.2.preheader ]
  %iv.4.next = sub i32 %iv.4, %iv.1.lcssa
  %iv.3.next = add i16 %iv.3, 1
  %cmp88.1 = icmp ult i16 %iv.3, 198
  br i1 %cmp88.1, label %loop.2, label %loop.3.preheader

loop.3.preheader:                                 ; preds = %loop.2
  %iv.4.lcssa = phi i32 [ %iv.4, %loop.2 ]
  br label %loop.3

loop.3:                                           ; preds = %loop.3, %loop.3.preheader
  %iv.5 = phi i32 [ %sub93.2, %loop.3 ], [ 0, %loop.3.preheader ]
  %sub93.2 = sub i32 %iv.5, %iv.4.lcssa
  br label %loop.3

unreachable.bb:                                   ; No predecessors!
  br label %loop.1.preheader
}

test_name: test2_pr58811
test_body: define void @test2_pr58811() {
entry:
  br label %loop.1.header

loop.1.header:                                    ; preds = %loop.4, %entry
  %p.1 = phi i32 [ 0, %entry ], [ %sub93.2, %loop.4 ]
  br label %loop.2

loop.2:                                           ; preds = %loop.2, %loop.1.header
  %iv.2 = phi i32 [ %p.1, %loop.1.header ], [ %add101, %loop.2 ]
  %iv.3 = phi i32 [ 0, %loop.1.header ], [ %sub93, %loop.2 ]
  %sub93 = add i32 %iv.3, 1
  %add101 = add i32 %iv.3, %iv.2
  br i1 false, label %loop.2, label %loop.3

loop.3:                                           ; preds = %loop.3, %loop.2
  %iv.4 = phi i16 [ 0, %loop.2 ], [ %inc.1, %loop.3 ]
  %iv.5 = phi i32 [ 0, %loop.2 ], [ %sub93.1, %loop.3 ]
  %sub93.1 = sub i32 %iv.5, %iv.2
  %inc.1 = add i16 %iv.4, 1
  %cmp88.1 = icmp ult i16 %iv.4, 198
  br i1 %cmp88.1, label %loop.3, label %loop.4

loop.4:                                           ; preds = %loop.4, %loop.3
  %iv.6 = phi i32 [ 0, %loop.3 ], [ %sub93.2, %loop.4 ]
  %sub93.2 = sub i32 %iv.6, %iv.5
  br i1 false, label %loop.4, label %loop.1.header
}

test_name: test3_pr58811
test_body: define void @test3_pr58811() {
entry:
  br label %loop.1.header

loop.1.header:                                    ; preds = %loop.1.latch, %entry
  %p.1 = phi i32 [ 0, %entry ], [ %sub93.2, %loop.1.latch ]
  %rem85 = urem i32 1, %p.1
  br label %loop.2

loop.2:                                           ; preds = %loop.2, %loop.1.header
  %p.2 = phi i32 [ 1, %loop.1.header ], [ 0, %loop.2 ]
  %add101 = add i32 %rem85, %p.2
  br i1 false, label %loop.2, label %loop.3

loop.3:                                           ; preds = %loop.3, %loop.2
  %iv.3 = phi i16 [ 0, %loop.2 ], [ %inc.1, %loop.3 ]
  %iv.4 = phi i32 [ 0, %loop.2 ], [ %sub93.1, %loop.3 ]
  %sub93.1 = sub i32 %iv.4, %add101
  %inc.1 = add i16 %iv.3, 1
  %cmp88.1 = icmp ult i16 %iv.3, 198
  br i1 %cmp88.1, label %loop.3, label %loop.4

loop.4:                                           ; preds = %loop.4, %loop.3
  %iv.5 = phi i32 [ 0, %loop.3 ], [ %sub93.2, %loop.4 ]
  %sub93.2 = sub i32 %iv.5, %iv.4
  br label %loop.4

loop.1.latch:                                     ; No predecessors!
  br label %loop.1.header
}