Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp: simplifyRecipe
Issue title: [LoopVectorize] Instruction does not dominate all uses!
Labels: vectorizers
crash-on-valid
generated by fuzzer
Reported by: dtcxzyw

Reproducer: https://godbolt.org/z/P1ea8d1bT
```
; bin/opt -passes=loop-vectorize test.ll -S
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"
target triple = "x86_64-unknown-linux-gnu"

define i16 @main() {
entry:
  br label %safe_lshift_func_uint16_t_u_s.exit.i.i

safe_lshift_func_uint16_t_u_s.exit.i.i:           ; preds = %safe_lshift_func_uint16_t_u_s.exit.i.i, %entry
  %l_1488.0377.i.i = phi i16 [ 0, %entry ], [ %l_1488.2.lcssa.2.i.i, %safe_lshift_func_uint16_t_u_s.exit.i.i ]
  %storemerge376.i.i = phi i16 [ 1, %entry ], [ %inc275.i.i, %safe_lshift_func_uint16_t_u_s.exit.i.i ]
  %0 = load i32, ptr null, align 4
  %tobool254.not.2.i.i = icmp eq i32 %0, 0
  %l_1488.2.lcssa.2.i.i = select i1 %tobool254.not.2.i.i, i16 1, i16 0
  %inc275.i.i = add i16 %storemerge376.i.i, 1
  %exitcond.not.i.i = icmp eq i16 %inc275.i.i, 0
  br i1 %exitcond.not.i.i, label %func_210.exit, label %safe_lshift_func_uint16_t_u_s.exit.i.i

func_210.exit:                                    ; preds = %safe_lshift_func_uint16_t_u_s.exit.i.i
  ret i16 %l_1488.0377.i.i
}
```
```
Instruction does not dominate all uses!
  %4 = extractelement <4 x i16> %2, i32 0
  %scalar.recur.init = phi i16 [ %4, %middle.block ], [ 0, %entry ]
Instruction does not dominate all uses!
  %2 = select <4 x i1> %1, <4 x i16> splat (i16 1), <4 x i16> zeroinitializer
  %4 = extractelement <4 x i16> %2, i32 0
PHI nodes not grouped at top of basic block!
  %bc.resume.val = phi i16 [ -7, %middle.block ], [ 1, %entry ]
label %scalar.ph
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp:10182: bool llvm::LoopVectorizePass::processLoop(llvm::Loop*): Assertion `!verifyFunction(*F, &dbgs())' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
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 "main"
 #0 0x000000000589ea68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x589ea68)
 #1 0x000000000589b914 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007e16e6e42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007e16e6e969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007e16e6e42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007e16e6e287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007e16e6e2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007e16e6e39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x00000000036379e9 llvm::LoopVectorizePass::processLoop(llvm::Loop*) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x36379e9)
 #9 0x0000000003639970 llvm::LoopVectorizePass::runImpl(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3639970)
#10 0x000000000363a055 llvm::LoopVectorizePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x363a055)
#11 0x0000000002f3481e 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+0x2f3481e)
#12 0x0000000005687c01 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5687c01)
#13 0x0000000000eeb6ee 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+0xeeb6ee)
#14 0x000000000568616a llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x568616a)
#15 0x000000000096f08e 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+0x96f08e)
#16 0x0000000005685b21 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5685b21)
#17 0x0000000000979388 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+0x979388)
#18 0x000000000096d409 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x96d409)
#19 0x00007e16e6e29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#20 0x00007e16e6e29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#21 0x0000000000964555 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x964555)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```
cc @fhahn 



Fix patch:
commit ef7e03a2d1571e3c651fa9ccd205dbacf1a103c8
Author: Florian Hahn <flo@fhahn.com>
Date:   Sat Sep 13 18:13:22 2025 +0100

    [VPlan] Limit ExtractLastElem fold to recipes guaranteed single-scalar.
    
    vputils::isSingleScalar(A) may return true to recipes that produce only
    a single scalar value, but they could still end up as vector
    instruction, because the recipe could not be converted to a
    single-scalar VPInstruction/VPReplicateRecipe.
    
    For now, only apply the fold for recipes guaranteed to produce a single
    value, i.e. single-scalar VPInstructions and VPReplicateRecipes.
    
    Fixes https://github.com/llvm/llvm-project/issues/158319.

diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index fcd85ba9ab7f..a193c438e7ea 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1272,9 +1272,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
 
   if (match(Def,
             m_VPInstruction<VPInstruction::ExtractLastElement>(m_VPValue(A))) &&
-      vputils::isSingleScalar(A) && all_of(A->users(), [Def, A](VPUser *U) {
-        return U->usesScalars(A) || Def == U;
-      })) {
+      ((isa<VPInstruction>(A) && vputils::isSingleScalar(A)) ||
+       (isa<VPReplicateRecipe>(A) &&
+        cast<VPReplicateRecipe>(A)->isSingleScalar())) &&
+      all_of(A->users(),
+             [Def, A](VPUser *U) { return U->usesScalars(A) || Def == U; })) {
     return Def->replaceAllUsesWith(A);
   }
 }


Regression tests:
file: llvm/test/Transforms/LoopVectorize/first-order-recurrence-with-uniform-ops.ll
commands: opt -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=2 -S %s
opt -passes=loop-vectorize -force-vector-width=1 -force-vector-interleave=2 -S %s
opt -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S %s
tests: test_name: <module>
test_body: 
; Test case for https://github.com/llvm/llvm-project/issues/95520.
define i32 @recurence_uniform_load(ptr %src, ptr noalias %dst, i64 %n) {
; UNROLL-NO-IC-LABEL: define i32 @recurence_uniform_load(
; UNROLL-NO-IC-SAME: ptr [[SRC:%.*]], ptr noalias [[DST:%.*]], i64 [[N:%.*]]) {
; UNROLL-NO-IC-NEXT:  [[ENTRY:.*]]:
; UNROLL-NO-IC-NEXT:    [[TMP0:%.*]] = add i64 [[N]], 1
; UNROLL-NO-IC-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 8
; UNROLL-NO-IC-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; UNROLL-NO-IC:       [[VECTOR_PH]]:
; UNROLL-NO-IC-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], 8
; UNROLL-NO-IC-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
; UNROLL-NO-IC-NEXT:    br label %[[VECTOR_BODY:.*]]
; UNROLL-NO-IC:       [[VECTOR_BODY]]:
; UNROLL-NO-IC-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; UNROLL-NO-IC-NEXT:    [[TMP1:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-IC-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8
; UNROLL-NO-IC-NEXT:    [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; UNROLL-NO-IC-NEXT:    br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; UNROLL-NO-IC:       [[MIDDLE_BLOCK]]:
; UNROLL-NO-IC-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
; UNROLL-NO-IC-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
; UNROLL-NO-IC:       [[SCALAR_PH]]:
; UNROLL-NO-IC-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-IC-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP1]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-IC-NEXT:    br label %[[LOOP:.*]]
; UNROLL-NO-IC:       [[LOOP]]:
; UNROLL-NO-IC-NEXT:    [[PHI:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[ADD:%.*]], %[[LOOP]] ]
; UNROLL-NO-IC-NEXT:    [[RECUR:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[LOAD:%.*]], %[[LOOP]] ]
; UNROLL-NO-IC-NEXT:    [[ADD]] = add i64 [[PHI]], 1
; UNROLL-NO-IC-NEXT:    [[LOAD]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-IC-NEXT:    [[ICMP:%.*]] = icmp ult i64 [[PHI]], [[N]]
; UNROLL-NO-IC-NEXT:    br i1 [[ICMP]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
; UNROLL-NO-IC:       [[EXIT]]:
; UNROLL-NO-IC-NEXT:    [[RECUR_LCSSA:%.*]] = phi i32 [ [[RECUR]], %[[LOOP]] ], [ [[TMP1]], %[[MIDDLE_BLOCK]] ]
; UNROLL-NO-IC-NEXT:    ret i32 [[RECUR_LCSSA]]
;
; UNROLL-NO-VF-LABEL: define i32 @recurence_uniform_load(
; UNROLL-NO-VF-SAME: ptr [[SRC:%.*]], ptr noalias [[DST:%.*]], i64 [[N:%.*]]) {
; UNROLL-NO-VF-NEXT:  [[ENTRY:.*]]:
; UNROLL-NO-VF-NEXT:    [[TMP1:%.*]] = add i64 [[N]], 1
; UNROLL-NO-VF-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP1]], 2
; UNROLL-NO-VF-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; UNROLL-NO-VF:       [[VECTOR_PH]]:
; UNROLL-NO-VF-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[TMP1]], 2
; UNROLL-NO-VF-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP1]], [[N_MOD_VF]]
; UNROLL-NO-VF-NEXT:    br label %[[VECTOR_BODY:.*]]
; UNROLL-NO-VF:       [[VECTOR_BODY]]:
; UNROLL-NO-VF-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; UNROLL-NO-VF-NEXT:    [[TMP0:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-VF-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
; UNROLL-NO-VF-NEXT:    [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; UNROLL-NO-VF-NEXT:    br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; UNROLL-NO-VF:       [[MIDDLE_BLOCK]]:
; UNROLL-NO-VF-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP1]], [[N_VEC]]
; UNROLL-NO-VF-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
; UNROLL-NO-VF:       [[SCALAR_PH]]:
; UNROLL-NO-VF-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-VF-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP0]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-VF-NEXT:    br label %[[LOOP:.*]]
; UNROLL-NO-VF:       [[LOOP]]:
; UNROLL-NO-VF-NEXT:    [[PHI:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[ADD:%.*]], %[[LOOP]] ]
; UNROLL-NO-VF-NEXT:    [[RECUR:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[LOAD:%.*]], %[[LOOP]] ]
; UNROLL-NO-VF-NEXT:    [[ADD]] = add i64 [[PHI]], 1
; UNROLL-NO-VF-NEXT:    [[LOAD]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-VF-NEXT:    [[ICMP:%.*]] = icmp ult i64 [[PHI]], [[N]]
; UNROLL-NO-VF-NEXT:    br i1 [[ICMP]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
; UNROLL-NO-VF:       [[EXIT]]:
; UNROLL-NO-VF-NEXT:    [[RECUR_LCSSA:%.*]] = phi i32 [ [[RECUR]], %[[LOOP]] ], [ [[TMP0]], %[[MIDDLE_BLOCK]] ]
; UNROLL-NO-VF-NEXT:    ret i32 [[RECUR_LCSSA]]
;
; SINK-AFTER-LABEL: define i32 @recurence_uniform_load(
; SINK-AFTER-SAME: ptr [[SRC:%.*]], ptr noalias [[DST:%.*]], i64 [[N:%.*]]) {
; SINK-AFTER-NEXT:  [[ENTRY:.*]]:
; SINK-AFTER-NEXT:    [[TMP0:%.*]] = add i64 [[N]], 1
; SINK-AFTER-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 4
; SINK-AFTER-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; SINK-AFTER:       [[VECTOR_PH]]:
; SINK-AFTER-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], 4
; SINK-AFTER-NEXT:    [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
; SINK-AFTER-NEXT:    br label %[[VECTOR_BODY:.*]]
; SINK-AFTER:       [[VECTOR_BODY]]:
; SINK-AFTER-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; SINK-AFTER-NEXT:    [[TMP1:%.*]] = load i32, ptr [[SRC]], align 4
; SINK-AFTER-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; SINK-AFTER-NEXT:    [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; SINK-AFTER-NEXT:    br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; SINK-AFTER:       [[MIDDLE_BLOCK]]:
; SINK-AFTER-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
; SINK-AFTER-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
; SINK-AFTER:       [[SCALAR_PH]]:
; SINK-AFTER-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; SINK-AFTER-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[TMP1]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; SINK-AFTER-NEXT:    br label %[[LOOP:.*]]
; SINK-AFTER:       [[LOOP]]:
; SINK-AFTER-NEXT:    [[PHI:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[ADD:%.*]], %[[LOOP]] ]
; SINK-AFTER-NEXT:    [[RECUR:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[LOAD:%.*]], %[[LOOP]] ]
; SINK-AFTER-NEXT:    [[ADD]] = add i64 [[PHI]], 1
; SINK-AFTER-NEXT:    [[LOAD]] = load i32, ptr [[SRC]], align 4
; SINK-AFTER-NEXT:    [[ICMP:%.*]] = icmp ult i64 [[PHI]], [[N]]
; SINK-AFTER-NEXT:    br i1 [[ICMP]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
; SINK-AFTER:       [[EXIT]]:
; SINK-AFTER-NEXT:    [[RECUR_LCSSA:%.*]] = phi i32 [ [[RECUR]], %[[LOOP]] ], [ [[TMP1]], %[[MIDDLE_BLOCK]] ]
; SINK-AFTER-NEXT:    ret i32 [[RECUR_LCSSA]]
;
entry:
  br label %loop

loop:
  %phi = phi i64 [ 0, %entry ], [ %add, %loop ]
  %recur = phi i32 [ 0, %entry ], [ %load, %loop ]
  %add = add i64 %phi, 1
  %load = load i32, ptr %src, align 4
  %icmp = icmp ult i64 %phi, %n
  br i1 %icmp, label %loop, label %exit

exit:
  ret i32 %recur
}

; Test for https://github.com/llvm/llvm-project/issues/158319. The recurrence
; phi can be removed.
define i16 @for_phi_removed(ptr  %src) {
; UNROLL-NO-IC-LABEL: define i16 @for_phi_removed(
; UNROLL-NO-IC-SAME: ptr [[SRC:%.*]]) {
; UNROLL-NO-IC-NEXT:  [[ENTRY:.*]]:
; UNROLL-NO-IC-NEXT:    br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; UNROLL-NO-IC:       [[VECTOR_PH]]:
; UNROLL-NO-IC-NEXT:    br label %[[VECTOR_BODY:.*]]
; UNROLL-NO-IC:       [[VECTOR_BODY]]:
; UNROLL-NO-IC-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; UNROLL-NO-IC-NEXT:    [[TMP0:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-IC-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TMP0]], i64 0
; UNROLL-NO-IC-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
; UNROLL-NO-IC-NEXT:    [[TMP1:%.*]] = icmp eq <4 x i32> [[BROADCAST_SPLAT]], zeroinitializer
; UNROLL-NO-IC-NEXT:    [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x i16> splat (i16 1), <4 x i16> zeroinitializer
; UNROLL-NO-IC-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 8
; UNROLL-NO-IC-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], 104
; UNROLL-NO-IC-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
; UNROLL-NO-IC:       [[MIDDLE_BLOCK]]:
; UNROLL-NO-IC-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <4 x i16> [[TMP2]], i32 3
; UNROLL-NO-IC-NEXT:    br label %[[SCALAR_PH]]
; UNROLL-NO-IC:       [[SCALAR_PH]]:
; UNROLL-NO-IC-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i16 [ 104, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-IC-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i16 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-IC-NEXT:    br label %[[LOOP:.*]]
; UNROLL-NO-IC:       [[LOOP]]:
; UNROLL-NO-IC-NEXT:    [[IV:%.*]] = phi i16 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; UNROLL-NO-IC-NEXT:    [[P:%.*]] = phi i16 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[SEL:%.*]], %[[LOOP]] ]
; UNROLL-NO-IC-NEXT:    [[L:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-IC-NEXT:    [[C:%.*]] = icmp eq i32 [[L]], 0
; UNROLL-NO-IC-NEXT:    [[SEL]] = select i1 [[C]], i16 1, i16 0
; UNROLL-NO-IC-NEXT:    [[IV_NEXT]] = add i16 [[IV]], 1
; UNROLL-NO-IC-NEXT:    [[EC:%.*]] = icmp eq i16 [[IV_NEXT]], 111
; UNROLL-NO-IC-NEXT:    br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
; UNROLL-NO-IC:       [[EXIT]]:
; UNROLL-NO-IC-NEXT:    [[P_LCSSA:%.*]] = phi i16 [ [[P]], %[[LOOP]] ]
; UNROLL-NO-IC-NEXT:    ret i16 [[P_LCSSA]]
;
; UNROLL-NO-VF-LABEL: define i16 @for_phi_removed(
; UNROLL-NO-VF-SAME: ptr [[SRC:%.*]]) {
; UNROLL-NO-VF-NEXT:  [[ENTRY:.*]]:
; UNROLL-NO-VF-NEXT:    br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; UNROLL-NO-VF:       [[VECTOR_PH]]:
; UNROLL-NO-VF-NEXT:    br label %[[VECTOR_BODY:.*]]
; UNROLL-NO-VF:       [[VECTOR_BODY]]:
; UNROLL-NO-VF-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; UNROLL-NO-VF-NEXT:    [[TMP0:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-VF-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[TMP0]], 0
; UNROLL-NO-VF-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i16 1, i16 0
; UNROLL-NO-VF-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 2
; UNROLL-NO-VF-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], 110
; UNROLL-NO-VF-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
; UNROLL-NO-VF:       [[MIDDLE_BLOCK]]:
; UNROLL-NO-VF-NEXT:    br label %[[SCALAR_PH]]
; UNROLL-NO-VF:       [[SCALAR_PH]]:
; UNROLL-NO-VF-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i16 [ 110, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-VF-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i16 [ [[TMP2]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; UNROLL-NO-VF-NEXT:    br label %[[LOOP:.*]]
; UNROLL-NO-VF:       [[LOOP]]:
; UNROLL-NO-VF-NEXT:    [[IV:%.*]] = phi i16 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; UNROLL-NO-VF-NEXT:    [[P:%.*]] = phi i16 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[SEL:%.*]], %[[LOOP]] ]
; UNROLL-NO-VF-NEXT:    [[L:%.*]] = load i32, ptr [[SRC]], align 4
; UNROLL-NO-VF-NEXT:    [[C:%.*]] = icmp eq i32 [[L]], 0
; UNROLL-NO-VF-NEXT:    [[SEL]] = select i1 [[C]], i16 1, i16 0
; UNROLL-NO-VF-NEXT:    [[IV_NEXT]] = add i16 [[IV]], 1
; UNROLL-NO-VF-NEXT:    [[EC:%.*]] = icmp eq i16 [[IV_NEXT]], 111
; UNROLL-NO-VF-NEXT:    br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
; UNROLL-NO-VF:       [[EXIT]]:
; UNROLL-NO-VF-NEXT:    [[P_LCSSA:%.*]] = phi i16 [ [[P]], %[[LOOP]] ]
; UNROLL-NO-VF-NEXT:    ret i16 [[P_LCSSA]]
;
; SINK-AFTER-LABEL: define i16 @for_phi_removed(
; SINK-AFTER-SAME: ptr [[SRC:%.*]]) {
; SINK-AFTER-NEXT:  [[ENTRY:.*]]:
; SINK-AFTER-NEXT:    br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; SINK-AFTER:       [[VECTOR_PH]]:
; SINK-AFTER-NEXT:    br label %[[VECTOR_BODY:.*]]
; SINK-AFTER:       [[VECTOR_BODY]]:
; SINK-AFTER-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; SINK-AFTER-NEXT:    [[TMP0:%.*]] = load i32, ptr [[SRC]], align 4
; SINK-AFTER-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TMP0]], i64 0
; SINK-AFTER-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
; SINK-AFTER-NEXT:    [[TMP1:%.*]] = icmp eq <4 x i32> [[BROADCAST_SPLAT]], zeroinitializer
; SINK-AFTER-NEXT:    [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x i16> splat (i16 1), <4 x i16> zeroinitializer
; SINK-AFTER-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
; SINK-AFTER-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], 108
; SINK-AFTER-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
; SINK-AFTER:       [[MIDDLE_BLOCK]]:
; SINK-AFTER-NEXT:    [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <4 x i16> [[TMP2]], i32 3
; SINK-AFTER-NEXT:    br label %[[SCALAR_PH]]
; SINK-AFTER:       [[SCALAR_PH]]:
; SINK-AFTER-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i16 [ 108, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; SINK-AFTER-NEXT:    [[SCALAR_RECUR_INIT:%.*]] = phi i16 [ [[VECTOR_RECUR_EXTRACT]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
; SINK-AFTER-NEXT:    br label %[[LOOP:.*]]
; SINK-AFTER:       [[LOOP]]:
; SINK-AFTER-NEXT:    [[IV:%.*]] = phi i16 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; SINK-AFTER-NEXT:    [[P:%.*]] = phi i16 [ [[SCALAR_RECUR_INIT]], %[[SCALAR_PH]] ], [ [[SEL:%.*]], %[[LOOP]] ]
; SINK-AFTER-NEXT:    [[L:%.*]] = load i32, ptr [[SRC]], align 4
; SINK-AFTER-NEXT:    [[C:%.*]] = icmp eq i32 [[L]], 0
; SINK-AFTER-NEXT:    [[SEL]] = select i1 [[C]], i16 1, i16 0
; SINK-AFTER-NEXT:    [[IV_NEXT]] = add i16 [[IV]], 1
; SINK-AFTER-NEXT:    [[EC:%.*]] = icmp eq i16 [[IV_NEXT]], 111
; SINK-AFTER-NEXT:    br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
; SINK-AFTER:       [[EXIT]]:
; SINK-AFTER-NEXT:    [[P_LCSSA:%.*]] = phi i16 [ [[P]], %[[LOOP]] ]
; SINK-AFTER-NEXT:    ret i16 [[P_LCSSA]]
;
entry:
  br label %loop

loop:
  %iv = phi i16 [ 0, %entry ], [ %iv.next, %loop ]
  %p = phi i16 [ 0, %entry ] , [ %sel, %loop ]
  %l = load i32, ptr %src, align 4
  %c = icmp eq i32 %l, 0
  %sel = select i1 %c, i16 1, i16 0
  %iv.next = add i16 %iv, 1
  %ec = icmp eq i16 %iv.next, 111
  br i1 %ec, label %exit, label %loop

exit:
  ret i16 %p
}