[llvm] [LV] Check inner-loop latch operands for uniformity in outer-loop path (PR #227415)

Julian Nagele via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 04:02:40 PDT 2026


https://github.com/juliannagele updated https://github.com/llvm/llvm-project/pull/227415

>From 54fe2fcc0f5aeb6773c1425a5cea56bf6e8246be Mon Sep 17 00:00:00 2001
From: Julian Nagele <j_nagele at apple.com>
Date: Tue, 29 Sep 2026 18:37:32 +0100
Subject: [PATCH 1/4] [LV] Add tests for uniform inner loops in outer-loop path
 (NFC)

Inner loops with a non-canonical or pointer IV, which are currently
rejected as divergent, and inner loops with a canonical IV but no
computable trip count, which are accepted. Also add a uniform inner
loop that is not recognized, with a TODO.
---
 .../outer_loop_uniform_inner_loop.ll          | 463 ++++++++++++++++++
 1 file changed, 463 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll

diff --git a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
new file mode 100644
index 0000000000000..16103b3f0364a
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
@@ -0,0 +1,463 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --filter-out-after "^scalar.ph" --version 6
+; RUN: opt -S -passes=loop-vectorize -enable-vplan-native-path < %s | FileCheck %s
+
+; Outer-loop vectorization of nests whose inner loop is uniform: every lane
+; computes the same inner latch condition. The inner IV need not be canonical,
+; and the inner trip count need not be computable.
+
+; for (i = 0; i < N; i++)
+;   for (j = lo; j < M; j++)
+;     A[i*M+j] *= 2.0f;
+define void @inner_start_invariant(ptr %A, i64 %N, i64 %M, i64 %lo) {
+; CHECK-LABEL: define void @inner_start_invariant(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i64 [[LO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[I_MUL_M:%.*]] = mul nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
+; CHECK:       [[INNER_BODY]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[LO]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL_M]], [[J]]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
+; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
+; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
+; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i64 [[J_NEXT]], [[M]]
+; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %i.mul.M = mul nsw i64 %i, %M
+  br label %inner.body
+
+inner.body:
+  %j = phi i64 [ %lo, %outer.header ], [ %j.next, %inner.body ]
+  %idx = add nsw i64 %i.mul.M, %j
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  %A.val = load float, ptr %A.ptr, align 4
+  %mul = fmul float %A.val, 2.000000e+00
+  store float %mul, ptr %A.ptr, align 4
+  %j.next = add nsw i64 %j, 1
+  %j.cmp = icmp eq i64 %j.next, %M
+  br i1 %j.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; for (i = 0; i < N; i++)
+;   for (j = 0; j < M; j += 2)
+;     A[i*M+j] *= 2.0f;
+define void @inner_step_2(ptr %A, i64 %N, i64 %M) {
+; CHECK-LABEL: define void @inner_step_2(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[I_MUL_M:%.*]] = mul nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
+; CHECK:       [[INNER_BODY]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ 0, %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL_M]], [[J]]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
+; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
+; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 2
+; CHECK-NEXT:    [[J_CMP:%.*]] = icmp sge i64 [[J_NEXT]], [[M]]
+; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %i.mul.M = mul nsw i64 %i, %M
+  br label %inner.body
+
+inner.body:
+  %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.body ]
+  %idx = add nsw i64 %i.mul.M, %j
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  %A.val = load float, ptr %A.ptr, align 4
+  %mul = fmul float %A.val, 2.000000e+00
+  store float %mul, ptr %A.ptr, align 4
+  %j.next = add nsw i64 %j, 2
+  %j.cmp = icmp sge i64 %j.next, %M
+  br i1 %j.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The latch compares a truncated IV update, as left by IndVarSimplify's linear
+; function test replacement.
+; for (i = 0; i < N; i++)
+;   for (j = lo; j != bound; j++)   // exit test on (int)(j + 1)
+;     A[i*64+j] *= 2.0f;
+define void @inner_latch_cmp_on_trunc(ptr %A, i64 %N, i32 %bound, i64 %lo) {
+; CHECK-LABEL: define void @inner_latch_cmp_on_trunc(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i32 [[BOUND:%.*]], i64 [[LO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[I_MUL:%.*]] = mul nsw i64 [[I]], 64
+; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
+; CHECK:       [[INNER_BODY]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[LO]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL]], [[J]]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
+; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
+; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
+; CHECK-NEXT:    [[LFTR_WIDEIV:%.*]] = trunc i64 [[J_NEXT]] to i32
+; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i32 [[BOUND]], [[LFTR_WIDEIV]]
+; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %i.mul = mul nsw i64 %i, 64
+  br label %inner.body
+
+inner.body:
+  %j = phi i64 [ %lo, %outer.header ], [ %j.next, %inner.body ]
+  %idx = add nsw i64 %i.mul, %j
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  %A.val = load float, ptr %A.ptr, align 4
+  %mul = fmul float %A.val, 2.000000e+00
+  store float %mul, ptr %A.ptr, align 4
+  %j.next = add nsw i64 %j, 1
+  %lftr.wideiv = trunc i64 %j.next to i32
+  %j.cmp = icmp eq i32 %bound, %lftr.wideiv
+  br i1 %j.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The inner latch continues while j.next == X. SCEV does not model this exit.
+define void @inner_latch_continue_while_eq(ptr %A, i64 %N, i64 %M, i32 %X) {
+; CHECK-LABEL: define void @inner_latch_continue_while_eq(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i32 [[X:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[X]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH5:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH5]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP5:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[J4]] to <4 x i64>
+; CHECK-NEXT:    [[TMP3:%.*]] = add nsw <4 x i64> [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP3]]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP4:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP4]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP5]] = add <4 x i32> [[J4]], splat (i32 1)
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ne <4 x i32> [[TMP5]], [[BROADCAST_SPLAT2]]
+; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK:       [[OUTER_LATCH5]]:
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %i.mul.M = mul nsw i64 %i, %M
+  br label %inner.body
+
+inner.body:
+  %j = phi i32 [ 0, %outer.header ], [ %j.next, %inner.body ]
+  %j.ext = zext i32 %j to i64
+  %idx = add nsw i64 %i.mul.M, %j.ext
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  %A.val = load float, ptr %A.ptr, align 4
+  %mul = fmul float %A.val, 2.000000e+00
+  store float %mul, ptr %A.ptr, align 4
+  %j.next = add i32 %j, 1
+  %j.cont = icmp eq i32 %j.next, %X
+  br i1 %j.cont, label %inner.body, label %outer.latch
+
+outer.latch:
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The inner latch continues while j.next <=u X. The loop does not terminate
+; if X is UINT_MAX, so SCEV cannot compute its trip count.
+define void @inner_latch_continue_while_ule(ptr %A, i64 %N, i64 %M, i32 %X) {
+; CHECK-LABEL: define void @inner_latch_continue_while_ule(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i32 [[X:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i32> poison, i32 [[X]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT1]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH5:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH5]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP5:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[J4]] to <4 x i64>
+; CHECK-NEXT:    [[TMP3:%.*]] = add nsw <4 x i64> [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP3]]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP4:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP4]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP5]] = add <4 x i32> [[J4]], splat (i32 1)
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ugt <4 x i32> [[TMP5]], [[BROADCAST_SPLAT2]]
+; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK:       [[OUTER_LATCH5]]:
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP7:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %i.mul.M = mul nsw i64 %i, %M
+  br label %inner.body
+
+inner.body:
+  %j = phi i32 [ 0, %outer.header ], [ %j.next, %inner.body ]
+  %j.ext = zext i32 %j to i64
+  %idx = add nsw i64 %i.mul.M, %j.ext
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  %A.val = load float, ptr %A.ptr, align 4
+  %mul = fmul float %A.val, 2.000000e+00
+  store float %mul, ptr %A.ptr, align 4
+  %j.next = add i32 %j, 1
+  %j.cont = icmp ule i32 %j.next, %X
+  br i1 %j.cont, label %inner.body, label %outer.latch
+
+outer.latch:
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The inner loop is controlled by a pointer IV. Its start and end are the same
+; in every lane.
+; for (i = 0; i < N; i++) {
+;   float s = 0;
+;   for (const float *p = begin; p != end; ++p)
+;     s += *p * C[i];
+;   A[i] = s;
+; }
+define void @inner_latch_ptr_iv(ptr noalias %A, ptr noalias %C, ptr %begin, ptr %end, i64 %N) {
+; CHECK-LABEL: define void @inner_latch_ptr_iv(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[C:%.*]], ptr [[BEGIN:%.*]], ptr [[END:%.*]], i64 [[N:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[C_PTR:%.*]] = getelementptr inbounds float, ptr [[C]], i64 [[I]]
+; CHECK-NEXT:    [[C:%.*]] = load float, ptr [[C_PTR]], align 4
+; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
+; CHECK:       [[INNER_BODY]]:
+; CHECK-NEXT:    [[P:%.*]] = phi ptr [ [[BEGIN]], %[[OUTER_HEADER]] ], [ [[P_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[S:%.*]] = phi float [ 0.000000e+00, %[[OUTER_HEADER]] ], [ [[S_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[V:%.*]] = load float, ptr [[P]], align 4
+; CHECK-NEXT:    [[M:%.*]] = fmul float [[V]], [[C]]
+; CHECK-NEXT:    [[S_NEXT]] = fadd float [[S]], [[M]]
+; CHECK-NEXT:    [[P_NEXT]] = getelementptr inbounds float, ptr [[P]], i64 1
+; CHECK-NEXT:    [[P_CMP:%.*]] = icmp eq ptr [[P_NEXT]], [[END]]
+; CHECK-NEXT:    br i1 [[P_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi float [ [[S_NEXT]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[I]]
+; CHECK-NEXT:    store float [[S_LCSSA]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %C.ptr = getelementptr inbounds float, ptr %C, i64 %i
+  %c = load float, ptr %C.ptr, align 4
+  br label %inner.body
+
+inner.body:
+  %p = phi ptr [ %begin, %outer.header ], [ %p.next, %inner.body ]
+  %s = phi float [ 0.000000e+00, %outer.header ], [ %s.next, %inner.body ]
+  %v = load float, ptr %p, align 4
+  %m = fmul float %v, %c
+  %s.next = fadd float %s, %m
+  %p.next = getelementptr inbounds float, ptr %p, i64 1
+  %p.cmp = icmp eq ptr %p.next, %end
+  br i1 %p.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %s.lcssa = phi float [ %s.next, %inner.body ]
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %i
+  store float %s.lcssa, ptr %A.ptr, align 4
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; TODO: The inner loop is uniform, because every lane runs M iterations, but it
+; is not recognized: the inner IV start and bound both depend on the outer IV,
+; so the compared values differ per lane.
+; for (i = 0; i < N; i++) {
+;   float s = 0;
+;   for (j = i; j < i + M; j++)
+;     s += B[j];
+;   A[i] = s;
+; }
+define void @inner_start_and_bound_shift_with_outer(ptr noalias %A, ptr noalias %B, i64 %N, i64 %M) {
+; CHECK-LABEL: define void @inner_start_and_bound_shift_with_outer(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
+; CHECK:       [[OUTER_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[END:%.*]] = add nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
+; CHECK:       [[INNER_BODY]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[I]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[S:%.*]] = phi float [ 0.000000e+00, %[[OUTER_HEADER]] ], [ [[S_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[B_PTR:%.*]] = getelementptr inbounds float, ptr [[B]], i64 [[J]]
+; CHECK-NEXT:    [[B_VAL:%.*]] = load float, ptr [[B_PTR]], align 4
+; CHECK-NEXT:    [[S_NEXT]] = fadd float [[S]], [[B_VAL]]
+; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
+; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i64 [[J_NEXT]], [[END]]
+; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi float [ [[S_NEXT]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[I]]
+; CHECK-NEXT:    store float [[S_LCSSA]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
+; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
+  %end = add nsw i64 %i, %M
+  br label %inner.body
+
+inner.body:
+  %j = phi i64 [ %i, %outer.header ], [ %j.next, %inner.body ]
+  %s = phi float [ 0.000000e+00, %outer.header ], [ %s.next, %inner.body ]
+  %B.ptr = getelementptr inbounds float, ptr %B, i64 %j
+  %B.val = load float, ptr %B.ptr, align 4
+  %s.next = fadd float %s, %B.val
+  %j.next = add nsw i64 %j, 1
+  %j.cmp = icmp eq i64 %j.next, %end
+  br i1 %j.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %s.lcssa = phi float [ %s.next, %inner.body ]
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %i
+  store float %s.lcssa, ptr %A.ptr, align 4
+  %i.next = add nuw nsw i64 %i, 1
+  %i.cmp = icmp eq i64 %i.next, %N
+  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+!0 = distinct !{!0, !1, !2}
+!1 = !{!"llvm.loop.vectorize.width", i32 4}
+!2 = !{!"llvm.loop.vectorize.enable"}

>From 5da76a56c615b988f0840dae32a272addaaf9cae Mon Sep 17 00:00:00 2001
From: Julian Nagele <j_nagele at apple.com>
Date: Tue, 29 Sep 2026 18:39:26 +0100
Subject: [PATCH 2/4] [LV] Check inner-loop latch operands for uniformity in
 outer-loop path

An inner loop is uniform if every lane computes the same latch
condition. Check that both operands of the latch compare are
loop-uniform in the outer loop, instead of requiring a canonical IV
compared against an outer-loop invariant. This accepts every loop the
old check accepted.

Inner loops whose compared values differ per lane are still rejected,
even if every lane runs the same number of iterations.
---
 .../Vectorize/LoopVectorizationLegality.cpp   |  50 +++--
 .../outer_loop_uniform_inner_loop.ll          | 181 +++++++++++-------
 2 files changed, 139 insertions(+), 92 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 0c080d9434ea8..71db1003def04 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -326,13 +326,17 @@ void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) {
 
 // Return true if the inner loop \p Lp is uniform with regard to the outer loop
 // \p OuterLp (i.e., if the outer loop is vectorized, all the vector lanes
-// executing the inner loop will execute the same iterations). This check is
-// very constrained for now but it will be relaxed in the future. \p Lp is
+// executing the inner loop will execute the same iterations). \p Lp is
 // considered uniform if it meets all the following conditions:
-//   1) it has a canonical IV (starting from 0 and with stride 1),
-//   2) its latch terminator is a conditional branch and,
-//   3) its latch condition is a compare instruction whose operands are the
-//      canonical IV and an OuterLp invariant.
+//   1) its latch terminator is a conditional branch and,
+//   2) its latch condition is a compare instruction whose operands are both
+//      loop-uniform in \p OuterLp, i.e. for a given iteration of \p Lp their
+//      value is the same in every iteration of \p OuterLp (see
+//      ScalarEvolution::isLoopUniform). Every lane then computes the same latch
+//      condition, and as nested loops only exit via their latch (checked by the
+//      caller), all lanes leave \p Lp together.
+// A loop whose compared values differ between lanes is rejected, even if its
+// trip count is the same in every lane.
 // This check doesn't take into account the uniformity of other conditions not
 // related to the loop latch because they don't affect the loop uniformity.
 //
@@ -347,7 +351,7 @@ void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) {
 // before introducing the aforementioned infrastructure. However, if this is not
 // the case, we should move the \p OuterLp independent checks to a separate
 // function that is only executed once for each \p Lp.
-static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
+static bool isUniformLoop(Loop *Lp, Loop *OuterLp, ScalarEvolution &SE) {
   assert(Lp->getLoopLatch() && "Expected loop with a single latch.");
 
   // If Lp is the outer loop, it's uniform by definition.
@@ -356,13 +360,6 @@ static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
   assert(OuterLp->contains(Lp) && "OuterLp must contain Lp.");
 
   // 1.
-  PHINode *IV = Lp->getCanonicalInductionVariable();
-  if (!IV) {
-    LLVM_DEBUG(dbgs() << "LV: Canonical IV not found.\n");
-    return false;
-  }
-
-  // 2.
   BasicBlock *Latch = Lp->getLoopLatch();
   auto *LatchBr = dyn_cast<CondBrInst>(Latch->getTerminator());
   if (!LatchBr) {
@@ -370,7 +367,7 @@ static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
     return false;
   }
 
-  // 3.
+  // 2.
   auto *LatchCmp = dyn_cast<CmpInst>(LatchBr->getCondition());
   if (!LatchCmp) {
     LLVM_DEBUG(
@@ -378,13 +375,12 @@ static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
     return false;
   }
 
-  Value *CondOp0 = LatchCmp->getOperand(0);
-  Value *CondOp1 = LatchCmp->getOperand(1);
-  Value *IVUpdate = IV->getIncomingValueForBlock(Latch);
-  if (!(CondOp0 == IVUpdate && OuterLp->isLoopInvariant(CondOp1)) &&
-      !(CondOp1 == IVUpdate && OuterLp->isLoopInvariant(CondOp0))) {
-    LLVM_DEBUG(dbgs() << "LV: Loop latch condition is not uniform.\n");
-    return false;
+  for (Value *Op : LatchCmp->operand_values()) {
+    if (!SE.isSCEVable(Op->getType()) ||
+        !SE.isLoopUniform(SE.getSCEV(Op), OuterLp)) {
+      LLVM_DEBUG(dbgs() << "LV: Loop latch condition is not uniform.\n");
+      return false;
+    }
   }
 
   return true;
@@ -392,13 +388,13 @@ static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
 
 // Return true if \p Lp and all its nested loops are uniform with regard to \p
 // OuterLp.
-static bool isUniformLoopNest(Loop *Lp, Loop *OuterLp) {
-  if (!isUniformLoop(Lp, OuterLp))
+static bool isUniformLoopNest(Loop *Lp, Loop *OuterLp, ScalarEvolution &SE) {
+  if (!isUniformLoop(Lp, OuterLp, SE))
     return false;
 
   // Check if nested loops are uniform.
   for (Loop *SubLp : *Lp)
-    if (!isUniformLoopNest(SubLp, OuterLp))
+    if (!isUniformLoopNest(SubLp, OuterLp, SE))
       return false;
 
   return true;
@@ -678,8 +674,8 @@ bool LoopVectorizationLegality::canVectorizeOuterLoop() {
 
   // Check whether inner loops are uniform. At this point, we only support
   // simple outer loops scenarios with uniform nested loops.
-  if (!isUniformLoopNest(TheLoop /*loop nest*/,
-                         TheLoop /*context outer loop*/)) {
+  if (!isUniformLoopNest(TheLoop /*loop nest*/, TheLoop /*context outer loop*/,
+                         *PSE.getSE())) {
     reportVectorizationFailure(
         "Outer loop contains divergent loops",
         "loop control flow is not understood by vectorizer", "CFGNotUnderstood",
diff --git a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
index 16103b3f0364a..662f711ad3e57 100644
--- a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
+++ b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
@@ -11,28 +11,42 @@
 define void @inner_start_invariant(ptr %A, i64 %N, i64 %M, i64 %lo) {
 ; CHECK-LABEL: define void @inner_start_invariant(
 ; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i64 [[LO:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[LO]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT1]], <4 x i64> poison, <4 x i32> zeroinitializer
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[I_MUL_M:%.*]] = mul nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[LO]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL_M]], [[J]]
-; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
-; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
-; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
-; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i64 [[J_NEXT]], [[M]]
+; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J4]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J4]], splat (i64 1)
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq <4 x i64> [[TMP4]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
 ; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
 ; CHECK:       [[OUTER_LATCH]]:
-; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
-; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0:![0-9]+]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
@@ -68,28 +82,40 @@ exit:
 define void @inner_step_2(ptr %A, i64 %N, i64 %M) {
 ; CHECK-LABEL: define void @inner_step_2(
 ; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[I_MUL_M:%.*]] = mul nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J:%.*]] = phi i64 [ 0, %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL_M]], [[J]]
-; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
-; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
-; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 2
-; CHECK-NEXT:    [[J_CMP:%.*]] = icmp sge i64 [[J_NEXT]], [[M]]
+; CHECK-NEXT:    [[J2:%.*]] = phi <4 x i64> [ zeroinitializer, %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J2]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J2]], splat (i64 2)
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp sge <4 x i64> [[TMP4]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
 ; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
 ; CHECK:       [[OUTER_LATCH]]:
-; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
-; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP4:![0-9]+]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
@@ -127,29 +153,43 @@ exit:
 define void @inner_latch_cmp_on_trunc(ptr %A, i64 %N, i32 %bound, i64 %lo) {
 ; CHECK-LABEL: define void @inner_latch_cmp_on_trunc(
 ; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i32 [[BOUND:%.*]], i64 [[LO:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[BOUND]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[LO]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT1]], <4 x i64> poison, <4 x i32> zeroinitializer
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[I_MUL:%.*]] = mul nsw i64 [[I]], 64
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = shl nsw <4 x i64> [[VEC_IND]], splat (i64 6)
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[LO]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[IDX:%.*]] = add nsw i64 [[I_MUL]], [[J]]
-; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[IDX]]
-; CHECK-NEXT:    [[A_VAL:%.*]] = load float, ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[A_VAL]], 2.000000e+00
-; CHECK-NEXT:    store float [[MUL]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
-; CHECK-NEXT:    [[LFTR_WIDEIV:%.*]] = trunc i64 [[J_NEXT]] to i32
-; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i32 [[BOUND]], [[LFTR_WIDEIV]]
+; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J4]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J4]], splat (i64 1)
+; CHECK-NEXT:    [[TMP5:%.*]] = trunc <4 x i64> [[TMP4]] to <4 x i32>
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq <4 x i32> [[BROADCAST_SPLAT]], [[TMP5]]
+; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
 ; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
 ; CHECK:       [[OUTER_LATCH]]:
-; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
-; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP6:![0-9]+]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
@@ -216,7 +256,7 @@ define void @inner_latch_continue_while_eq(ptr %A, i64 %N, i64 %M, i32 %X) {
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
 ; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
@@ -288,7 +328,7 @@ define void @inner_latch_continue_while_ule(ptr %A, i64 %N, i64 %M, i32 %X) {
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
 ; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP7:![0-9]+]]
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
@@ -334,31 +374,42 @@ exit:
 define void @inner_latch_ptr_iv(ptr noalias %A, ptr noalias %C, ptr %begin, ptr %end, i64 %N) {
 ; CHECK-LABEL: define void @inner_latch_ptr_iv(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[C:%.*]], ptr [[BEGIN:%.*]], ptr [[END:%.*]], i64 [[N:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x ptr> poison, ptr [[END]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT]], <4 x ptr> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x ptr> poison, ptr [[BEGIN]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT1]], <4 x ptr> poison, <4 x i32> zeroinitializer
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
 ; CHECK-NEXT:    [[C_PTR:%.*]] = getelementptr inbounds float, ptr [[C]], i64 [[I]]
-; CHECK-NEXT:    [[C:%.*]] = load float, ptr [[C_PTR]], align 4
+; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[C_PTR]], align 4
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[P:%.*]] = phi ptr [ [[BEGIN]], %[[OUTER_HEADER]] ], [ [[P_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[S:%.*]] = phi float [ 0.000000e+00, %[[OUTER_HEADER]] ], [ [[S_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[V:%.*]] = load float, ptr [[P]], align 4
-; CHECK-NEXT:    [[M:%.*]] = fmul float [[V]], [[C]]
-; CHECK-NEXT:    [[S_NEXT]] = fadd float [[S]], [[M]]
-; CHECK-NEXT:    [[P_NEXT]] = getelementptr inbounds float, ptr [[P]], i64 1
-; CHECK-NEXT:    [[P_CMP:%.*]] = icmp eq ptr [[P_NEXT]], [[END]]
+; CHECK-NEXT:    [[P4:%.*]] = phi <4 x ptr> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[WIDE_GEP:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[S5:%.*]] = phi <4 x float> [ zeroinitializer, %[[OUTER_HEADER]] ], [ [[TMP3:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[P4]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], [[WIDE_LOAD]]
+; CHECK-NEXT:    [[TMP3]] = fadd <4 x float> [[S5]], [[TMP2]]
+; CHECK-NEXT:    [[WIDE_GEP]] = getelementptr inbounds float, <4 x ptr> [[P4]], i64 1
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq <4 x ptr> [[WIDE_GEP]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[P_CMP:%.*]] = extractelement <4 x i1> [[TMP4]], i64 0
 ; CHECK-NEXT:    br i1 [[P_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
 ; CHECK:       [[OUTER_LATCH]]:
-; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi float [ [[S_NEXT]], %[[INNER_BODY]] ]
 ; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[I]]
-; CHECK-NEXT:    store float [[S_LCSSA]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
-; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK-NEXT:    store <4 x float> [[TMP3]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[I]], 4
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP12:![0-9]+]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    ret void
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
@@ -424,7 +475,7 @@ define void @inner_start_and_bound_shift_with_outer(ptr noalias %A, ptr noalias
 ; CHECK-NEXT:    store float [[S_LCSSA]], ptr [[A_PTR]], align 4
 ; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
 ; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0]]
+; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP14:![0-9]+]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;

>From 688d1bf16c8a0e3eca975bc7c940832fee56514a Mon Sep 17 00:00:00 2001
From: Julian Nagele <j_nagele at apple.com>
Date: Wed, 30 Sep 2026 15:13:50 +0100
Subject: [PATCH 3/4] fixup! [LV] Check inner-loop latch operands for
 uniformity in outer-loop path

---
 .../outer_loop_uniform_inner_loop.ll          | 634 +++++++++++-------
 1 file changed, 407 insertions(+), 227 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
index 662f711ad3e57..1e838bff993fc 100644
--- a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
+++ b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
@@ -1,16 +1,16 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --filter-out-after "^scalar.ph" --version 6
-; RUN: opt -S -passes=loop-vectorize -enable-vplan-native-path < %s | FileCheck %s
+; RUN: opt -passes=loop-vectorize -enable-vplan-native-path -S %s | FileCheck %s
 
 ; Outer-loop vectorization of nests whose inner loop is uniform: every lane
 ; computes the same inner latch condition. The inner IV need not be canonical,
 ; and the inner trip count need not be computable.
 
 ; for (i = 0; i < N; i++)
-;   for (j = lo; j < M; j++)
-;     A[i*M+j] *= 2.0f;
-define void @inner_start_invariant(ptr %A, i64 %N, i64 %M, i64 %lo) {
+;   for (j = start; j < M; j++)
+;     A[i*M+j] = 0;
+define void @inner_start_invariant(ptr %A, i64 %N, i64 %M, i64 %start) {
 ; CHECK-LABEL: define void @inner_start_invariant(
-; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i64 [[LO:%.*]]) {
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]], i64 [[START:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
 ; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
@@ -19,58 +19,54 @@ define void @inner_start_invariant(ptr %A, i64 %N, i64 %M, i64 %lo) {
 ; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
 ; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[LO]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[START]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT1]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
-; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH5:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH5]] ]
 ; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
-; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
-; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J4]]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[VECTOR_BODY]] ], [ [[TMP3:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[INNER_IV4]]
 ; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
-; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
-; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J4]], splat (i64 1)
-; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq <4 x i64> [[TMP4]], [[BROADCAST_SPLAT]]
-; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
-; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
-; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP3]] = add nsw <4 x i64> [[INNER_IV4]], splat (i64 1)
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq <4 x i64> [[TMP3]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <4 x i1> [[TMP4]], i64 0
+; CHECK-NEXT:    br i1 [[TMP5]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK:       [[OUTER_LATCH5]]:
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
-; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP0:![0-9]+]]
-; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
 ; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %i.mul.M = mul nsw i64 %i, %M
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
   br label %inner.body
 
 inner.body:
-  %j = phi i64 [ %lo, %outer.header ], [ %j.next, %inner.body ]
-  %idx = add nsw i64 %i.mul.M, %j
+  %inner.iv = phi i64 [ %start, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %idx = add nsw i64 %outer.offset, %inner.iv
   %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
-  %A.val = load float, ptr %A.ptr, align 4
-  %mul = fmul float %A.val, 2.000000e+00
-  store float %mul, ptr %A.ptr, align 4
-  %j.next = add nsw i64 %j, 1
-  %j.cmp = icmp eq i64 %j.next, %M
-  br i1 %j.cmp, label %outer.latch, label %inner.body
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add nsw i64 %inner.iv, 1
+  %inner.cmp = icmp eq i64 %inner.iv.next, %M
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
 
 outer.latch:
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
@@ -78,7 +74,7 @@ exit:
 
 ; for (i = 0; i < N; i++)
 ;   for (j = 0; j < M; j += 2)
-;     A[i*M+j] *= 2.0f;
+;     A[i*M+j] = 0;
 define void @inner_step_2(ptr %A, i64 %N, i64 %M) {
 ; CHECK-LABEL: define void @inner_step_2(
 ; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
@@ -90,69 +86,64 @@ define void @inner_step_2(ptr %A, i64 %N, i64 %M) {
 ; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
 ; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
-; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH3:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH3]] ]
 ; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
-; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
-; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J2:%.*]] = phi <4 x i64> [ zeroinitializer, %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J2]]
+; CHECK-NEXT:    br label %[[INNER_BODY1:.*]]
+; CHECK:       [[INNER_BODY1]]:
+; CHECK-NEXT:    [[INNER_IV2:%.*]] = phi <4 x i64> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP3:%.*]], %[[INNER_BODY1]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[INNER_IV2]]
 ; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
-; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
-; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J2]], splat (i64 2)
-; CHECK-NEXT:    [[TMP5:%.*]] = icmp sge <4 x i64> [[TMP4]], [[BROADCAST_SPLAT]]
-; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
-; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
-; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP3]] = add nsw <4 x i64> [[INNER_IV2]], splat (i64 2)
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp sge <4 x i64> [[TMP3]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <4 x i1> [[TMP4]], i64 0
+; CHECK-NEXT:    br i1 [[TMP5]], label %[[OUTER_LATCH3]], label %[[INNER_BODY1]]
+; CHECK:       [[OUTER_LATCH3]]:
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
-; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP4:![0-9]+]]
-; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
 ; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %i.mul.M = mul nsw i64 %i, %M
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
   br label %inner.body
 
 inner.body:
-  %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.body ]
-  %idx = add nsw i64 %i.mul.M, %j
+  %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %idx = add nsw i64 %outer.offset, %inner.iv
   %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
-  %A.val = load float, ptr %A.ptr, align 4
-  %mul = fmul float %A.val, 2.000000e+00
-  store float %mul, ptr %A.ptr, align 4
-  %j.next = add nsw i64 %j, 2
-  %j.cmp = icmp sge i64 %j.next, %M
-  br i1 %j.cmp, label %outer.latch, label %inner.body
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add nsw i64 %inner.iv, 2
+  %inner.cmp = icmp sge i64 %inner.iv.next, %M
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
 
 outer.latch:
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
 }
 
-; The latch compares a truncated IV update, as left by IndVarSimplify's linear
-; function test replacement.
+; The latch compares a truncated IV update.
 ; for (i = 0; i < N; i++)
-;   for (j = lo; j != bound; j++)   // exit test on (int)(j + 1)
-;     A[i*64+j] *= 2.0f;
-define void @inner_latch_cmp_on_trunc(ptr %A, i64 %N, i32 %bound, i64 %lo) {
+;   for (long j = start; (int)j != bound; j++)
+;     A[i*64+j] = 0;
+define void @inner_latch_cmp_on_trunc(ptr %A, i64 %N, i32 %bound, i64 %start) {
 ; CHECK-LABEL: define void @inner_latch_cmp_on_trunc(
-; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i32 [[BOUND:%.*]], i64 [[LO:%.*]]) {
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i32 [[BOUND:%.*]], i64 [[START:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
 ; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
@@ -161,60 +152,212 @@ define void @inner_latch_cmp_on_trunc(ptr %A, i64 %N, i32 %bound, i64 %lo) {
 ; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
 ; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[BOUND]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[LO]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[START]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT1]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
-; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH]] ]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH5:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH5]] ]
 ; CHECK-NEXT:    [[TMP1:%.*]] = shl nsw <4 x i64> [[VEC_IND]], splat (i64 6)
-; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
-; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[TMP4:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[J4]]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x i64> [ [[BROADCAST_SPLAT2]], %[[VECTOR_BODY]] ], [ [[TMP3:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[INNER_IV4]]
 ; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP3:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
-; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP3]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
-; CHECK-NEXT:    [[TMP4]] = add nsw <4 x i64> [[J4]], splat (i64 1)
-; CHECK-NEXT:    [[TMP5:%.*]] = trunc <4 x i64> [[TMP4]] to <4 x i32>
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq <4 x i32> [[BROADCAST_SPLAT]], [[TMP5]]
-; CHECK-NEXT:    [[J_CMP:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
-; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
-; CHECK:       [[OUTER_LATCH]]:
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP3]] = add nsw <4 x i64> [[INNER_IV4]], splat (i64 1)
+; CHECK-NEXT:    [[TMP4:%.*]] = trunc <4 x i64> [[TMP3]] to <4 x i32>
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq <4 x i32> [[BROADCAST_SPLAT]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK:       [[OUTER_LATCH5]]:
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
-; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP8]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP6:![0-9]+]]
-; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, 64
+  br label %inner.body
+
+inner.body:
+  %inner.iv = phi i64 [ %start, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %idx = add nsw i64 %outer.offset, %inner.iv
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add nsw i64 %inner.iv, 1
+  %inner.iv.next.trunc = trunc i64 %inner.iv.next to i32
+  %inner.cmp = icmp eq i32 %bound, %inner.iv.next.trunc
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The inner bound is computed inside the outer loop, but its SCEV is invariant
+; in the outer loop.
+; for (i = 0; i < N; i++) {
+;   long bound = M + 1;
+;   for (j = 0; j < bound; j++)
+;     A[i*M+j] = 0;
+; }
+define void @inner_bound_defined_in_outer_loop(ptr %A, i64 %N, i64 %M) {
+; CHECK-LABEL: define void @inner_bound_defined_in_outer_loop(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw <4 x i64> [[BROADCAST_SPLAT]], splat (i64 1)
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH3:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    br label %[[INNER_BODY1:.*]]
+; CHECK:       [[INNER_BODY1]]:
+; CHECK-NEXT:    [[INNER_IV2:%.*]] = phi <4 x i64> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP4:%.*]], %[[INNER_BODY1]] ]
+; CHECK-NEXT:    [[TMP3:%.*]] = add nsw <4 x i64> [[TMP2]], [[INNER_IV2]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP3]]
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add nuw nsw <4 x i64> [[INNER_IV2]], splat (i64 1)
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq <4 x i64> [[TMP4]], [[TMP1]]
+; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[OUTER_LATCH3]], label %[[INNER_BODY1]]
+; CHECK:       [[OUTER_LATCH3]]:
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
 ; CHECK:       [[SCALAR_PH]]:
 ;
 entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %i.mul = mul nsw i64 %i, 64
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
+  %bound = add nsw i64 %M, 1
   br label %inner.body
 
 inner.body:
-  %j = phi i64 [ %lo, %outer.header ], [ %j.next, %inner.body ]
-  %idx = add nsw i64 %i.mul, %j
+  %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %idx = add nsw i64 %outer.offset, %inner.iv
   %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
-  %A.val = load float, ptr %A.ptr, align 4
-  %mul = fmul float %A.val, 2.000000e+00
-  store float %mul, ptr %A.ptr, align 4
-  %j.next = add nsw i64 %j, 1
-  %lftr.wideiv = trunc i64 %j.next to i32
-  %j.cmp = icmp eq i32 %bound, %lftr.wideiv
-  br i1 %j.cmp, label %outer.latch, label %inner.body
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add nuw nsw i64 %inner.iv, 1
+  %inner.cmp = icmp eq i64 %inner.iv.next, %bound
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
 
 outer.latch:
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; Triangular 3-level nest. The innermost bound is an IV of the middle loop, so
+; it is the same in every lane.
+; for (i = 0; i < N; i++)
+;   for (j = 0; j < M; j++)
+;     for (k = 0; k <= j; k++)
+;       A[i*M+k] = 0;
+define void @nested_inner_bound_is_middle_iv(ptr %A, i64 %N, i64 %M) {
+; CHECK-LABEL: define void @nested_inner_bound_is_middle_iv(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], i64 [[M:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = and i64 [[N]], 3
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[TMP0]]
+; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[M]], i64 0
+; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH6:.*]] ]
+; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[OUTER_LATCH6]] ]
+; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    br label %[[MIDDLE_HEADER1:.*]]
+; CHECK:       [[MIDDLE_HEADER1]]:
+; CHECK-NEXT:    [[MIDDLE_IV2:%.*]] = phi <4 x i64> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP6:%.*]], %[[MIDDLE_LATCH5:.*]] ]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x i64> [ zeroinitializer, %[[MIDDLE_HEADER1]] ], [ [[TMP3:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nsw <4 x i64> [[TMP1]], [[INNER_IV4]]
+; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP2]]
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP3]] = add nuw nsw <4 x i64> [[INNER_IV4]], splat (i64 1)
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ugt <4 x i64> [[TMP3]], [[MIDDLE_IV2]]
+; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <4 x i1> [[TMP4]], i64 0
+; CHECK-NEXT:    br i1 [[TMP5]], label %[[MIDDLE_LATCH5]], label %[[INNER_BODY3]]
+; CHECK:       [[MIDDLE_LATCH5]]:
+; CHECK-NEXT:    [[TMP6]] = add nuw nsw <4 x i64> [[MIDDLE_IV2]], splat (i64 1)
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq <4 x i64> [[TMP6]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[TMP8:%.*]] = extractelement <4 x i1> [[TMP7]], i64 0
+; CHECK-NEXT:    br i1 [[TMP8]], label %[[OUTER_LATCH6]], label %[[MIDDLE_HEADER1]]
+; CHECK:       [[OUTER_LATCH6]]:
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
+; CHECK-NEXT:    [[TMP9:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP9]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
+  br label %middle.header
+
+middle.header:
+  %middle.iv = phi i64 [ 0, %outer.header ], [ %middle.iv.next, %middle.latch ]
+  br label %inner.body
+
+inner.body:
+  %inner.iv = phi i64 [ 0, %middle.header ], [ %inner.iv.next, %inner.body ]
+  %idx = add nsw i64 %outer.offset, %inner.iv
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add nuw nsw i64 %inner.iv, 1
+  %inner.cmp = icmp ugt i64 %inner.iv.next, %middle.iv
+  br i1 %inner.cmp, label %middle.latch, label %inner.body
+
+middle.latch:
+  %middle.iv.next = add nuw nsw i64 %middle.iv, 1
+  %middle.cmp = icmp eq i64 %middle.iv.next, %M
+  br i1 %middle.cmp, label %outer.latch, label %middle.header
+
+outer.latch:
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
@@ -241,22 +384,20 @@ define void @inner_latch_continue_while_eq(ptr %A, i64 %N, i64 %M, i32 %X) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
 ; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
 ; CHECK:       [[INNER_BODY3]]:
-; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP5:%.*]], %[[INNER_BODY3]] ]
-; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[J4]] to <4 x i64>
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP4:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[INNER_IV4]] to <4 x i64>
 ; CHECK-NEXT:    [[TMP3:%.*]] = add nsw <4 x i64> [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP3]]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP4:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
-; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP4]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
-; CHECK-NEXT:    [[TMP5]] = add <4 x i32> [[J4]], splat (i32 1)
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp ne <4 x i32> [[TMP5]], [[BROADCAST_SPLAT2]]
-; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
-; CHECK-NEXT:    br i1 [[TMP7]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add <4 x i32> [[INNER_IV4]], splat (i32 1)
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne <4 x i32> [[TMP4]], [[BROADCAST_SPLAT2]]
+; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
 ; CHECK:       [[OUTER_LATCH5]]:
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
-; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
@@ -266,26 +407,24 @@ entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %i.mul.M = mul nsw i64 %i, %M
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
   br label %inner.body
 
 inner.body:
-  %j = phi i32 [ 0, %outer.header ], [ %j.next, %inner.body ]
-  %j.ext = zext i32 %j to i64
-  %idx = add nsw i64 %i.mul.M, %j.ext
+  %inner.iv = phi i32 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %inner.iv.ext = zext i32 %inner.iv to i64
+  %idx = add nsw i64 %outer.offset, %inner.iv.ext
   %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
-  %A.val = load float, ptr %A.ptr, align 4
-  %mul = fmul float %A.val, 2.000000e+00
-  store float %mul, ptr %A.ptr, align 4
-  %j.next = add i32 %j, 1
-  %j.cont = icmp eq i32 %j.next, %X
-  br i1 %j.cont, label %inner.body, label %outer.latch
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add i32 %inner.iv, 1
+  %inner.cont = icmp eq i32 %inner.iv.next, %X
+  br i1 %inner.cont, label %inner.body, label %outer.latch
 
 outer.latch:
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
@@ -313,22 +452,20 @@ define void @inner_latch_continue_while_ule(ptr %A, i64 %N, i64 %M, i32 %X) {
 ; CHECK-NEXT:    [[TMP1:%.*]] = mul nsw <4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
 ; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
 ; CHECK:       [[INNER_BODY3]]:
-; CHECK-NEXT:    [[J4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP5:%.*]], %[[INNER_BODY3]] ]
-; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[J4]] to <4 x i64>
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x i32> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP4:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[TMP2:%.*]] = zext <4 x i32> [[INNER_IV4]] to <4 x i64>
 ; CHECK-NEXT:    [[TMP3:%.*]] = add nsw <4 x i64> [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    [[WIDE_GEP:%.*]] = getelementptr inbounds float, ptr [[A]], <4 x i64> [[TMP3]]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP4:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], splat (float 2.000000e+00)
-; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> [[TMP4]], <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
-; CHECK-NEXT:    [[TMP5]] = add <4 x i32> [[J4]], splat (i32 1)
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp ugt <4 x i32> [[TMP5]], [[BROADCAST_SPLAT2]]
-; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <4 x i1> [[TMP6]], i64 0
-; CHECK-NEXT:    br i1 [[TMP7]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
+; CHECK-NEXT:    call void @llvm.masked.scatter.v4f32.v4p0(<4 x float> zeroinitializer, <4 x ptr> align 4 [[WIDE_GEP]], <4 x i1> splat (i1 true))
+; CHECK-NEXT:    [[TMP4]] = add <4 x i32> [[INNER_IV4]], splat (i32 1)
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ugt <4 x i32> [[TMP4]], [[BROADCAST_SPLAT2]]
+; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i64 0
+; CHECK-NEXT:    br i1 [[TMP6]], label %[[OUTER_LATCH5]], label %[[INNER_BODY3]]
 ; CHECK:       [[OUTER_LATCH5]]:
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[VEC_IND]], splat (i64 4)
-; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP7]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP14:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:
 ; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
@@ -338,26 +475,24 @@ entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %i.mul.M = mul nsw i64 %i, %M
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %outer.offset = mul nsw i64 %outer.iv, %M
   br label %inner.body
 
 inner.body:
-  %j = phi i32 [ 0, %outer.header ], [ %j.next, %inner.body ]
-  %j.ext = zext i32 %j to i64
-  %idx = add nsw i64 %i.mul.M, %j.ext
+  %inner.iv = phi i32 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %inner.iv.ext = zext i32 %inner.iv to i64
+  %idx = add nsw i64 %outer.offset, %inner.iv.ext
   %A.ptr = getelementptr inbounds float, ptr %A, i64 %idx
-  %A.val = load float, ptr %A.ptr, align 4
-  %mul = fmul float %A.val, 2.000000e+00
-  store float %mul, ptr %A.ptr, align 4
-  %j.next = add i32 %j, 1
-  %j.cont = icmp ule i32 %j.next, %X
-  br i1 %j.cont, label %inner.body, label %outer.latch
+  store float 0.000000e+00, ptr %A.ptr, align 4
+  %inner.iv.next = add i32 %inner.iv, 1
+  %inner.cont = icmp ule i32 %inner.iv.next, %X
+  br i1 %inner.cont, label %inner.body, label %outer.latch
 
 outer.latch:
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
@@ -368,12 +503,12 @@ exit:
 ; for (i = 0; i < N; i++) {
 ;   float s = 0;
 ;   for (const float *p = begin; p != end; ++p)
-;     s += *p * C[i];
+;     s += *p;
 ;   A[i] = s;
 ; }
-define void @inner_latch_ptr_iv(ptr noalias %A, ptr noalias %C, ptr %begin, ptr %end, i64 %N) {
+define void @inner_latch_ptr_iv(ptr noalias %A, ptr %begin, ptr %end, i64 %N) {
 ; CHECK-LABEL: define void @inner_latch_ptr_iv(
-; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[C:%.*]], ptr [[BEGIN:%.*]], ptr [[END:%.*]], i64 [[N:%.*]]) {
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr [[BEGIN:%.*]], ptr [[END:%.*]], i64 [[N:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
 ; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
@@ -384,59 +519,104 @@ define void @inner_latch_ptr_iv(ptr noalias %A, ptr noalias %C, ptr %begin, ptr
 ; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT]], <4 x ptr> poison, <4 x i32> zeroinitializer
 ; CHECK-NEXT:    [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x ptr> poison, ptr [[BEGIN]], i64 0
 ; CHECK-NEXT:    [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT1]], <4 x ptr> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH6:.*]] ]
+; CHECK-NEXT:    br label %[[INNER_BODY3:.*]]
+; CHECK:       [[INNER_BODY3]]:
+; CHECK-NEXT:    [[INNER_IV4:%.*]] = phi <4 x ptr> [ [[BROADCAST_SPLAT2]], %[[VECTOR_BODY]] ], [ [[WIDE_GEP:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[S5:%.*]] = phi <4 x float> [ zeroinitializer, %[[VECTOR_BODY]] ], [ [[TMP1:%.*]], %[[INNER_BODY3]] ]
+; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[INNER_IV4]], <4 x i1> splat (i1 true), <4 x float> poison)
+; CHECK-NEXT:    [[TMP1]] = fadd <4 x float> [[S5]], [[WIDE_MASKED_GATHER]]
+; CHECK-NEXT:    [[WIDE_GEP]] = getelementptr inbounds float, <4 x ptr> [[INNER_IV4]], i64 1
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq <4 x ptr> [[WIDE_GEP]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <4 x i1> [[TMP2]], i64 0
+; CHECK-NEXT:    br i1 [[TMP3]], label %[[OUTER_LATCH6]], label %[[INNER_BODY3]]
+; CHECK:       [[OUTER_LATCH6]]:
+; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT:    store <4 x float> [[TMP1]], ptr [[TMP4]], align 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP5]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP16:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+;
+entry:
+  br label %outer.header
+
+outer.header:
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  br label %inner.body
+
+inner.body:
+  %inner.iv = phi ptr [ %begin, %outer.header ], [ %inner.iv.next, %inner.body ]
+  %s = phi float [ 0.000000e+00, %outer.header ], [ %s.next, %inner.body ]
+  %v = load float, ptr %inner.iv, align 4
+  %s.next = fadd float %s, %v
+  %inner.iv.next = getelementptr inbounds float, ptr %inner.iv, i64 1
+  %inner.cmp = icmp eq ptr %inner.iv.next, %end
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
+
+outer.latch:
+  %s.lcssa = phi float [ %s.next, %inner.body ]
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %outer.iv
+  store float %s.lcssa, ptr %A.ptr, align 4
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
+
+exit:
+  ret void
+}
+
+; The inner latch compares floating-point values, which are not SCEVable, so
+; the loop is rejected even though it is uniform.
+; for (i = 0; i < N; i++)
+;   for (float x = 0; x < X; x += 1)
+;     A[i] = x;
+define void @inner_latch_fp_cmp(ptr %A, i64 %N, float %X) {
+; CHECK-LABEL: define void @inner_latch_fp_cmp(
+; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]], float [[X:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[C_PTR:%.*]] = getelementptr inbounds float, ptr [[C]], i64 [[I]]
-; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[C_PTR]], align 4
+; CHECK-NEXT:    [[OUTER_IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[OUTER_IV_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[OUTER_IV]]
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[P4:%.*]] = phi <4 x ptr> [ [[BROADCAST_SPLAT2]], %[[OUTER_HEADER]] ], [ [[WIDE_GEP:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[S5:%.*]] = phi <4 x float> [ zeroinitializer, %[[OUTER_HEADER]] ], [ [[TMP3:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[WIDE_MASKED_GATHER:%.*]] = call <4 x float> @llvm.masked.gather.v4f32.v4p0(<4 x ptr> align 4 [[P4]], <4 x i1> splat (i1 true), <4 x float> poison)
-; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_GATHER]], [[WIDE_LOAD]]
-; CHECK-NEXT:    [[TMP3]] = fadd <4 x float> [[S5]], [[TMP2]]
-; CHECK-NEXT:    [[WIDE_GEP]] = getelementptr inbounds float, <4 x ptr> [[P4]], i64 1
-; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq <4 x ptr> [[WIDE_GEP]], [[BROADCAST_SPLAT]]
-; CHECK-NEXT:    [[P_CMP:%.*]] = extractelement <4 x i1> [[TMP4]], i64 0
-; CHECK-NEXT:    br i1 [[P_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK-NEXT:    [[INNER_IV:%.*]] = phi float [ 0.000000e+00, %[[OUTER_HEADER]] ], [ [[INNER_IV_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    store float [[INNER_IV]], ptr [[A_PTR]], align 4
+; CHECK-NEXT:    [[INNER_IV_NEXT]] = fadd float [[INNER_IV]], 1.000000e+00
+; CHECK-NEXT:    [[INNER_CONT:%.*]] = fcmp olt float [[INNER_IV_NEXT]], [[X]]
+; CHECK-NEXT:    br i1 [[INNER_CONT]], label %[[INNER_BODY]], label %[[OUTER_LATCH]]
 ; CHECK:       [[OUTER_LATCH]]:
-; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[I]]
-; CHECK-NEXT:    store <4 x float> [[TMP3]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[I]], 4
-; CHECK-NEXT:    [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[TMP7]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP12:![0-9]+]]
+; CHECK-NEXT:    [[OUTER_IV_NEXT]] = add nuw nsw i64 [[OUTER_IV]], 1
+; CHECK-NEXT:    [[OUTER_CMP:%.*]] = icmp eq i64 [[OUTER_IV_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[OUTER_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP18:![0-9]+]]
 ; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
-; CHECK-NEXT:    br i1 [[CMP_N]], [[EXIT1:label %.*]], label %[[SCALAR_PH]]
-; CHECK:       [[SCALAR_PH]]:
+; CHECK-NEXT:    ret void
 ;
 entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %C.ptr = getelementptr inbounds float, ptr %C, i64 %i
-  %c = load float, ptr %C.ptr, align 4
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %outer.iv
   br label %inner.body
 
 inner.body:
-  %p = phi ptr [ %begin, %outer.header ], [ %p.next, %inner.body ]
-  %s = phi float [ 0.000000e+00, %outer.header ], [ %s.next, %inner.body ]
-  %v = load float, ptr %p, align 4
-  %m = fmul float %v, %c
-  %s.next = fadd float %s, %m
-  %p.next = getelementptr inbounds float, ptr %p, i64 1
-  %p.cmp = icmp eq ptr %p.next, %end
-  br i1 %p.cmp, label %outer.latch, label %inner.body
+  %inner.iv = phi float [ 0.000000e+00, %outer.header ], [ %inner.iv.next, %inner.body ]
+  store float %inner.iv, ptr %A.ptr, align 4
+  %inner.iv.next = fadd float %inner.iv, 1.000000e+00
+  %inner.cont = fcmp olt float %inner.iv.next, %X
+  br i1 %inner.cont, label %inner.body, label %outer.latch
 
 outer.latch:
-  %s.lcssa = phi float [ %s.next, %inner.body ]
-  %A.ptr = getelementptr inbounds float, ptr %A, i64 %i
-  store float %s.lcssa, ptr %A.ptr, align 4
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void
@@ -457,25 +637,25 @@ define void @inner_start_and_bound_shift_with_outer(ptr noalias %A, ptr noalias
 ; CHECK-NEXT:  [[ENTRY:.*]]:
 ; CHECK-NEXT:    br label %[[OUTER_HEADER:.*]]
 ; CHECK:       [[OUTER_HEADER]]:
-; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
-; CHECK-NEXT:    [[END:%.*]] = add nsw i64 [[I]], [[M]]
+; CHECK-NEXT:    [[OUTER_IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[OUTER_IV_NEXT:%.*]], %[[OUTER_LATCH:.*]] ]
+; CHECK-NEXT:    [[END:%.*]] = add nsw i64 [[OUTER_IV]], [[M]]
 ; CHECK-NEXT:    br label %[[INNER_BODY:.*]]
 ; CHECK:       [[INNER_BODY]]:
-; CHECK-NEXT:    [[J:%.*]] = phi i64 [ [[I]], %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_BODY]] ]
+; CHECK-NEXT:    [[INNER_IV:%.*]] = phi i64 [ [[OUTER_IV]], %[[OUTER_HEADER]] ], [ [[INNER_IV_NEXT:%.*]], %[[INNER_BODY]] ]
 ; CHECK-NEXT:    [[S:%.*]] = phi float [ 0.000000e+00, %[[OUTER_HEADER]] ], [ [[S_NEXT:%.*]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[B_PTR:%.*]] = getelementptr inbounds float, ptr [[B]], i64 [[J]]
+; CHECK-NEXT:    [[B_PTR:%.*]] = getelementptr inbounds float, ptr [[B]], i64 [[INNER_IV]]
 ; CHECK-NEXT:    [[B_VAL:%.*]] = load float, ptr [[B_PTR]], align 4
 ; CHECK-NEXT:    [[S_NEXT]] = fadd float [[S]], [[B_VAL]]
-; CHECK-NEXT:    [[J_NEXT]] = add nsw i64 [[J]], 1
-; CHECK-NEXT:    [[J_CMP:%.*]] = icmp eq i64 [[J_NEXT]], [[END]]
-; CHECK-NEXT:    br i1 [[J_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
+; CHECK-NEXT:    [[INNER_IV_NEXT]] = add nsw i64 [[INNER_IV]], 1
+; CHECK-NEXT:    [[INNER_CMP:%.*]] = icmp eq i64 [[INNER_IV_NEXT]], [[END]]
+; CHECK-NEXT:    br i1 [[INNER_CMP]], label %[[OUTER_LATCH]], label %[[INNER_BODY]]
 ; CHECK:       [[OUTER_LATCH]]:
 ; CHECK-NEXT:    [[S_LCSSA:%.*]] = phi float [ [[S_NEXT]], %[[INNER_BODY]] ]
-; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[I]]
+; CHECK-NEXT:    [[A_PTR:%.*]] = getelementptr inbounds float, ptr [[A]], i64 [[OUTER_IV]]
 ; CHECK-NEXT:    store float [[S_LCSSA]], ptr [[A_PTR]], align 4
-; CHECK-NEXT:    [[I_NEXT]] = add nuw nsw i64 [[I]], 1
-; CHECK-NEXT:    [[I_CMP:%.*]] = icmp eq i64 [[I_NEXT]], [[N]]
-; CHECK-NEXT:    br i1 [[I_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP14:![0-9]+]]
+; CHECK-NEXT:    [[OUTER_IV_NEXT]] = add nuw nsw i64 [[OUTER_IV]], 1
+; CHECK-NEXT:    [[OUTER_CMP:%.*]] = icmp eq i64 [[OUTER_IV_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[OUTER_CMP]], label %[[EXIT:.*]], label %[[OUTER_HEADER]], !llvm.loop [[LOOP18]]
 ; CHECK:       [[EXIT]]:
 ; CHECK-NEXT:    ret void
 ;
@@ -483,27 +663,27 @@ entry:
   br label %outer.header
 
 outer.header:
-  %i = phi i64 [ 0, %entry ], [ %i.next, %outer.latch ]
-  %end = add nsw i64 %i, %M
+  %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
+  %end = add nsw i64 %outer.iv, %M
   br label %inner.body
 
 inner.body:
-  %j = phi i64 [ %i, %outer.header ], [ %j.next, %inner.body ]
+  %inner.iv = phi i64 [ %outer.iv, %outer.header ], [ %inner.iv.next, %inner.body ]
   %s = phi float [ 0.000000e+00, %outer.header ], [ %s.next, %inner.body ]
-  %B.ptr = getelementptr inbounds float, ptr %B, i64 %j
+  %B.ptr = getelementptr inbounds float, ptr %B, i64 %inner.iv
   %B.val = load float, ptr %B.ptr, align 4
   %s.next = fadd float %s, %B.val
-  %j.next = add nsw i64 %j, 1
-  %j.cmp = icmp eq i64 %j.next, %end
-  br i1 %j.cmp, label %outer.latch, label %inner.body
+  %inner.iv.next = add nsw i64 %inner.iv, 1
+  %inner.cmp = icmp eq i64 %inner.iv.next, %end
+  br i1 %inner.cmp, label %outer.latch, label %inner.body
 
 outer.latch:
   %s.lcssa = phi float [ %s.next, %inner.body ]
-  %A.ptr = getelementptr inbounds float, ptr %A, i64 %i
+  %A.ptr = getelementptr inbounds float, ptr %A, i64 %outer.iv
   store float %s.lcssa, ptr %A.ptr, align 4
-  %i.next = add nuw nsw i64 %i, 1
-  %i.cmp = icmp eq i64 %i.next, %N
-  br i1 %i.cmp, label %exit, label %outer.header, !llvm.loop !0
+  %outer.iv.next = add nuw nsw i64 %outer.iv, 1
+  %outer.cmp = icmp eq i64 %outer.iv.next, %N
+  br i1 %outer.cmp, label %exit, label %outer.header, !llvm.loop !0
 
 exit:
   ret void

>From e160802f0f1144413333f8e37e0c7135f5c8ea9f Mon Sep 17 00:00:00 2001
From: Julian Nagele <j_nagele at apple.com>
Date: Thu, 1 Oct 2026 11:18:46 +0100
Subject: [PATCH 4/4] fixup! [LV] Check inner-loop latch operands for
 uniformity in outer-loop path

---
 .../Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
index 1e838bff993fc..9ffbd9eea661c 100644
--- a/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
+++ b/llvm/test/Transforms/LoopVectorize/outer_loop_uniform_inner_loop.ll
@@ -571,8 +571,8 @@ exit:
   ret void
 }
 
-; The inner latch compares floating-point values, which are not SCEVable, so
-; the loop is rejected even though it is uniform.
+; The inner latch is an fcmp. Only icmp latch conditions are checked for
+; uniformity with SCEV, so the loop is rejected even though it is uniform.
 ; for (i = 0; i < N; i++)
 ;   for (float x = 0; x < X; x += 1)
 ;     A[i] = x;



More information about the llvm-commits mailing list