[llvm] [LV] Use getSmallBestKnownTC in IV-overflow-check (PR #195226)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 25 13:47:36 PDT 2026


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/195226

>From 38f6ad7e239ead5bda49f0f305af81637559065f Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 16 Jun 2026 21:55:04 +0100
Subject: [PATCH 1/8] [LV] Add tests from Florian

Co-authored-by: Florian Hahn <flo at fhahn.com>
---
 .../indvar-overflow-check-scalable-tc.ll      | 109 ++++++++++++++++++
 .../indvar-overflow-check-scalable-tc.ll      |  52 +++++++++
 2 files changed, 161 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
new file mode 100644
index 0000000000000..98dd50a395dc9
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -0,0 +1,109 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -passes=loop-vectorize -force-tail-folding-style=data -tail-folding-policy=prefer-fold-tail -force-vector-width=4 -force-vector-interleave=1 -scalable-vectorization=off -S %s | FileCheck %s
+
+target triple = "aarch64-unknown-linux-gnu"
+
+define void @foo(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VS:%.*]] = tail call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    [[N:%.*]] = mul nuw nsw i32 [[VS]], 6
+; CHECK-NEXT:    br label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[N_RND_UP:%.*]] = add i32 [[N]], 3
+; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], 4
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[ACTIVE_LANE_MASK:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32 [[INDEX]], i32 [[N]])
+; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP0]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
+; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    br label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vs = tail call i32 @llvm.vscale.i32()
+  %n = mul nuw nsw i32 %vs, 6
+  br label %for.body
+
+for.body:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %for.body ]
+  %gep.a = getelementptr inbounds nuw float, ptr %a, i32 %iv
+  %la = load float, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds nuw float, ptr %b, i32 %iv
+  %lb = load float, ptr %gep.b, align 4
+  %mul = fmul float %la, %lb
+  store float %mul, ptr %gep.b, align 4
+  %iv.next = add nuw nsw i32 %iv, 1
+  %ec = icmp eq i32 %iv.next, %n
+  br i1 %ec, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+; TC * MaxVScale overflows
+define void @bar(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @bar(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VS:%.*]] = tail call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    [[N:%.*]] = mul nuw nsw i32 [[VS]], 268435457
+; CHECK-NEXT:    br label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[N_RND_UP:%.*]] = add i32 [[N]], 3
+; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], 4
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[ACTIVE_LANE_MASK:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32 [[INDEX]], i32 [[N]])
+; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP0]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
+; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
+; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    br label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vs = tail call i32 @llvm.vscale.i32()
+  %n = mul nuw nsw i32 %vs, 268435457
+  br label %for.body
+
+for.body:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %for.body ]
+  %gep.a = getelementptr inbounds nuw float, ptr %a, i32 %iv
+  %la = load float, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds nuw float, ptr %b, i32 %iv
+  %lb = load float, ptr %gep.b, align 4
+  %mul = fmul float %la, %lb
+  store float %mul, ptr %gep.b, align 4
+  %iv.next = add nuw nsw i32 %iv, 1
+  %ec = icmp eq i32 %iv.next, %n
+  br i1 %ec, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+declare i32 @llvm.vscale.i32()
+
+attributes #0 = { vscale_range(1,16) "target-features"="+sve" }
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
new file mode 100644
index 0000000000000..d636cf0ebcc28
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -passes=loop-vectorize -mtriple=riscv64 -S %s | FileCheck %s
+
+target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
+
+define void @foo(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VS:%.*]] = tail call i64 @llvm.vscale.i64()
+; CHECK-NEXT:    [[N:%.*]] = mul nuw nsw i64 [[VS]], 100
+; CHECK-NEXT:    br label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[CURRENT_ITERATION_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[AVL:%.*]] = phi i64 [ [[N]], %[[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT:    [[VP_OP_LOAD:%.*]] = call <vscale x 4 x float> @llvm.vp.load.nxv4f32.p0(ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP0]])
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT:    call void @llvm.vp.store.nxv4f32.p0(<vscale x 4 x float> [[VP_OP_LOAD]], ptr align 4 [[TMP2]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP0]])
+; CHECK-NEXT:    [[TMP3:%.*]] = zext i32 [[TMP0]] to i64
+; CHECK-NEXT:    [[CURRENT_ITERATION_NEXT]] = add i64 [[TMP3]], [[INDEX]]
+; CHECK-NEXT:    [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP3]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
+; CHECK-NEXT:    br i1 [[TMP4]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    br label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vs = tail call i64 @llvm.vscale.i64()
+  %n = mul nuw nsw i64 %vs, 100
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+  %gep.a = getelementptr inbounds nuw float, ptr %a, i64 %iv
+  %la = load float, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds nuw float, ptr %b, i64 %iv
+  store float %la, ptr %gep.b, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
+attributes #0 = { "target-features"="+v,+zvl512b" }

>From 79bb4f500e88d9edab849ed66c393ec511506921 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Fri, 1 May 2026 07:12:22 +0100
Subject: [PATCH 2/8] [LV] Use getSmallBestKnownTC in IV-overflow-check

---
 .../Transforms/Vectorize/LoopVectorize.cpp    | 25 +++++++++++++------
 .../indvar-overflow-check-scalable-tc.ll      |  2 +-
 .../indvar-overflow-check-scalable-tc.ll      |  2 +-
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index b8f547b189b6c..3daa2dcbc8e74 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -473,16 +473,15 @@ static unsigned getMaxTCFromNonZeroRange(PredicatedScalarEvolution &PSE,
 ///   4) Returns the maximum trip count from the SCEV range excluding zero,
 ///      if \p CanUseConstantMax and \p CanExcludeZeroTrips.
 ///   5) Returns std::nullopt if all of the above failed.
-static std::optional<ElementCount>
-getSmallBestKnownTC(PredicatedScalarEvolution &PSE, Loop *L,
-                    bool CanUseConstantMax = true,
-                    bool CanExcludeZeroTrips = false) {
+static std::optional<ElementCount> getSmallBestKnownTC(
+    PredicatedScalarEvolution &PSE, Loop *L, bool CanUseConstantMax = true,
+    bool CanExcludeZeroTrips = false, bool ComputeUpperBoundOnly = false) {
   // Check if exact trip count is known.
   if (auto ExpectedTC = getSmallConstantTripCount(PSE.getSE(), L))
     return ExpectedTC;
 
   // Check if there is an expected trip count available from profile data.
-  if (LoopVectorizeWithBlockFrequency)
+  if (LoopVectorizeWithBlockFrequency && !ComputeUpperBoundOnly)
     if (auto EstimatedTC = getLoopEstimatedTripCount(L))
       return ElementCount::getFixed(*EstimatedTC);
 
@@ -1942,17 +1941,27 @@ static bool isIndvarOverflowCheckKnownFalse(
   // We know the runtime overflow check is known false iff the (max) trip-count
   // is known and (max) trip-count + (VF * UF) does not overflow in the type of
   // the vector loop induction variable.
-  if (unsigned TC = Cost->PSE.getSmallConstantMaxTripCount()) {
-    uint64_t MaxVF = VF.getKnownMinValue();
+  if (std::optional<ElementCount> TC = getSmallBestKnownTC(
+          Cost->PSE, Cost->TheLoop,
+          /*CanUseConstantMax=*/true, /*CanExcludeZeroTrips=*/false,
+          /*ComputeUpperBoundOnly=*/true)) {
+    unsigned MaxVF = VF.getKnownMinValue();
+    unsigned MaxTC = TC->getKnownMinValue();
     if (VF.isScalable()) {
       std::optional<unsigned> MaxVScale =
           getMaxVScale(*Cost->TheFunction, Cost->TTI);
       if (!MaxVScale)
         return false;
       MaxVF *= *MaxVScale;
+      if (TC->isScalable()) {
+        bool Overflow;
+        MaxTC = SaturatingMultiply(MaxTC, *MaxVScale, &Overflow);
+        if (Overflow)
+          return false;
+      }
     }
 
-    return (MaxUIntTripCount - TC).ugt(MaxVF * MaxUF);
+    return (MaxUIntTripCount - MaxTC).ugt(MaxVF * MaxUF);
   }
 
   return false;
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index 98dd50a395dc9..526d43e293ac7 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -75,7 +75,7 @@ define void @bar(ptr noalias %a, ptr noalias %b) #0 {
 ; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
 ; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
 ; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
-; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
index d636cf0ebcc28..6a577d959c68b 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
@@ -21,7 +21,7 @@ define void @foo(ptr noalias %a, ptr noalias %b) #0 {
 ; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[INDEX]]
 ; CHECK-NEXT:    call void @llvm.vp.store.nxv4f32.p0(<vscale x 4 x float> [[VP_OP_LOAD]], ptr align 4 [[TMP2]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP0]])
 ; CHECK-NEXT:    [[TMP3:%.*]] = zext i32 [[TMP0]] to i64
-; CHECK-NEXT:    [[CURRENT_ITERATION_NEXT]] = add i64 [[TMP3]], [[INDEX]]
+; CHECK-NEXT:    [[CURRENT_ITERATION_NEXT]] = add nuw i64 [[TMP3]], [[INDEX]]
 ; CHECK-NEXT:    [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP3]]
 ; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
 ; CHECK-NEXT:    br i1 [[TMP4]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]

>From 6ff11185e9f7a15d8bb735031ce0872d36b1ba8b Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 16 Jun 2026 22:02:05 +0100
Subject: [PATCH 3/8] [LV] Check VF.isScalable || TC->isScalable

---
 llvm/lib/Transforms/Vectorize/LoopVectorize.cpp                 | 2 +-
 .../LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 3daa2dcbc8e74..a0b00053c9f55 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1947,7 +1947,7 @@ static bool isIndvarOverflowCheckKnownFalse(
           /*ComputeUpperBoundOnly=*/true)) {
     unsigned MaxVF = VF.getKnownMinValue();
     unsigned MaxTC = TC->getKnownMinValue();
-    if (VF.isScalable()) {
+    if (VF.isScalable() || TC->isScalable()) {
       std::optional<unsigned> MaxVScale =
           getMaxVScale(*Cost->TheFunction, Cost->TTI);
       if (!MaxVScale)
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index 526d43e293ac7..98dd50a395dc9 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -75,7 +75,7 @@ define void @bar(ptr noalias %a, ptr noalias %b) #0 {
 ; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
 ; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
 ; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
-; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:

>From c00cf6316278a4464673003d0cdec2cc71215a97 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 22 Jun 2026 09:14:34 +0100
Subject: [PATCH 4/8] [LV/test] Improve tests

---
 .../indvar-overflow-check-scalable-tc.ll      | 29 +++++----
 .../indvar-overflow-check-scalable-tc.ll      | 61 +++++++++++++++++--
 2 files changed, 71 insertions(+), 19 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index 98dd50a395dc9..b25ebd29a95e4 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -3,8 +3,10 @@
 
 target triple = "aarch64-unknown-linux-gnu"
 
-define void @foo(ptr noalias %a, ptr noalias %b) #0 {
-; CHECK-LABEL: define void @foo(
+; VF is not scalable, but TC is. We can prove that TC > VF,
+; and hence, IV increment cannot unsigned-wrap.
+define void @iv_no_overflow(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @iv_no_overflow(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0:[0-9]+]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[VS:%.*]] = tail call i32 @llvm.vscale.i32()
@@ -35,10 +37,10 @@ define void @foo(ptr noalias %a, ptr noalias %b) #0 {
 entry:
   %vs = tail call i32 @llvm.vscale.i32()
   %n = mul nuw nsw i32 %vs, 6
-  br label %for.body
+  br label %loop
 
-for.body:
-  %iv = phi i32 [ 0, %entry ], [ %iv.next, %for.body ]
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
   %gep.a = getelementptr inbounds nuw float, ptr %a, i32 %iv
   %la = load float, ptr %gep.a, align 4
   %gep.b = getelementptr inbounds nuw float, ptr %b, i32 %iv
@@ -47,15 +49,16 @@ for.body:
   store float %mul, ptr %gep.b, align 4
   %iv.next = add nuw nsw i32 %iv, 1
   %ec = icmp eq i32 %iv.next, %n
-  br i1 %ec, label %exit, label %for.body
+  br i1 %ec, label %exit, label %loop
 
 exit:
   ret void
 }
 
-; TC * MaxVScale overflows
-define void @bar(ptr noalias %a, ptr noalias %b) #0 {
-; CHECK-LABEL: define void @bar(
+; VF is not scalable, but TC is. TC * MaxVScale overflows,
+; and hence IV increment does not have nuw.
+define void @tc_maxvscale_overflow(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @tc_maxvscale_overflow(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[VS:%.*]] = tail call i32 @llvm.vscale.i32()
@@ -86,10 +89,10 @@ define void @bar(ptr noalias %a, ptr noalias %b) #0 {
 entry:
   %vs = tail call i32 @llvm.vscale.i32()
   %n = mul nuw nsw i32 %vs, 268435457
-  br label %for.body
+  br label %loop
 
-for.body:
-  %iv = phi i32 [ 0, %entry ], [ %iv.next, %for.body ]
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
   %gep.a = getelementptr inbounds nuw float, ptr %a, i32 %iv
   %la = load float, ptr %gep.a, align 4
   %gep.b = getelementptr inbounds nuw float, ptr %b, i32 %iv
@@ -98,7 +101,7 @@ for.body:
   store float %mul, ptr %gep.b, align 4
   %iv.next = add nuw nsw i32 %iv, 1
   %ec = icmp eq i32 %iv.next, %n
-  br i1 %ec, label %exit, label %for.body
+  br i1 %ec, label %exit, label %loop
 
 exit:
   ret void
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
index 6a577d959c68b..669046831c4a5 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
@@ -3,8 +3,9 @@
 
 target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
 
-define void @foo(ptr noalias %a, ptr noalias %b) #0 {
-; CHECK-LABEL: define void @foo(
+; VF is scalable, as is TC. We can prove that TC > VF.
+define void @iv_no_overflow(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @iv_no_overflow(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0:[0-9]+]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    [[VS:%.*]] = tail call i64 @llvm.vscale.i64()
@@ -33,17 +34,65 @@ define void @foo(ptr noalias %a, ptr noalias %b) #0 {
 entry:
   %vs = tail call i64 @llvm.vscale.i64()
   %n = mul nuw nsw i64 %vs, 100
-  br label %for.body
+  br label %loop
 
-for.body:
-  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
   %gep.a = getelementptr inbounds nuw float, ptr %a, i64 %iv
   %la = load float, ptr %gep.a, align 4
   %gep.b = getelementptr inbounds nuw float, ptr %b, i64 %iv
   store float %la, ptr %gep.b, align 4
   %iv.next = add nuw nsw i64 %iv, 1
   %ec = icmp eq i64 %iv.next, %n
-  br i1 %ec, label %exit, label %for.body
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+; VF is scalable, as is TC. TC * MaxVScale overflows,
+; make sure that the IV increment does not have nuw.
+define void @tc_maxvscale_overflow(ptr noalias %a, ptr noalias %b) #0 {
+; CHECK-LABEL: define void @tc_maxvscale_overflow(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VS:%.*]] = tail call i64 @llvm.vscale.i64()
+; CHECK-NEXT:    [[N:%.*]] = mul nuw nsw i64 [[VS]], 268435457
+; CHECK-NEXT:    br label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[CURRENT_ITERATION_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[AVL:%.*]] = phi i64 [ [[N]], %[[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[INDEX]]
+; CHECK-NEXT:    [[VP_OP_LOAD:%.*]] = call <vscale x 4 x float> @llvm.vp.load.nxv4f32.p0(ptr align 4 [[TMP2]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP0]])
+; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[INDEX]]
+; CHECK-NEXT:    call void @llvm.vp.store.nxv4f32.p0(<vscale x 4 x float> [[VP_OP_LOAD]], ptr align 4 [[TMP3]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP0]])
+; CHECK-NEXT:    [[TMP5:%.*]] = zext i32 [[TMP0]] to i64
+; CHECK-NEXT:    [[CURRENT_ITERATION_NEXT]] = add i64 [[TMP5]], [[INDEX]]
+; CHECK-NEXT:    [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP5]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
+; CHECK-NEXT:    br i1 [[TMP4]], label %[[SCALAR_PH:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK:       [[SCALAR_PH]]:
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vs = tail call i64 @llvm.vscale.i64()
+  %n = mul nuw nsw i64 %vs, 268435457
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep.a = getelementptr inbounds nuw float, ptr %a, i64 %iv
+  %la = load float, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds nuw float, ptr %b, i64 %iv
+  store float %la, ptr %gep.b, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
 
 exit:
   ret void

>From 6b24fbda956fa2a540f518b4e310d98ccc1f1d03 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 22 Jun 2026 15:23:18 +0100
Subject: [PATCH 5/8] [LV] Fixed missed comment

---
 llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index a0b00053c9f55..453b1b4afed61 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -469,7 +469,8 @@ static unsigned getMaxTCFromNonZeroRange(PredicatedScalarEvolution &PSE,
 /// following procedure:
 ///   1) Returns exact trip count if it is known.
 ///   2) Returns expected trip count according to profile data if any.
-///   3) Returns upper bound estimate if known, and if \p CanUseConstantMax.
+///   3) Returns upper bound estimate if known, if \p CanUseConstantMax, and
+///      if \p ComputeUpperBoundOnly is false.
 ///   4) Returns the maximum trip count from the SCEV range excluding zero,
 ///      if \p CanUseConstantMax and \p CanExcludeZeroTrips.
 ///   5) Returns std::nullopt if all of the above failed.

>From a621a2d937f1ea7d8bb604cc4f0f7c7a1695025a Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 24 Jun 2026 16:23:52 +0100
Subject: [PATCH 6/8] [LV/test] Minor test improvements

---
 .../AArch64/indvar-overflow-check-scalable-tc.ll       | 10 +++-------
 .../RISCV/indvar-overflow-check-scalable-tc.ll         |  6 +-----
 2 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index b25ebd29a95e4..e6bab4ee5fe3e 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -1,11 +1,9 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
-; RUN: opt -passes=loop-vectorize -force-tail-folding-style=data -tail-folding-policy=prefer-fold-tail -force-vector-width=4 -force-vector-interleave=1 -scalable-vectorization=off -S %s | FileCheck %s
-
-target triple = "aarch64-unknown-linux-gnu"
+; RUN: opt -passes=loop-vectorize -mtriple=aarch64 -mattr=+sve -force-tail-folding-style=data -tail-folding-policy=prefer-fold-tail -force-vector-width=4 -force-vector-interleave=1 -scalable-vectorization=off -S %s | FileCheck %s
 
 ; VF is not scalable, but TC is. We can prove that TC > VF,
 ; and hence, IV increment cannot unsigned-wrap.
-define void @iv_no_overflow(ptr noalias %a, ptr noalias %b) #0 {
+define void @iv_no_overflow(ptr noalias %a, ptr noalias %b) vscale_range(1, 16) {
 ; CHECK-LABEL: define void @iv_no_overflow(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0:[0-9]+]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
@@ -57,7 +55,7 @@ exit:
 
 ; VF is not scalable, but TC is. TC * MaxVScale overflows,
 ; and hence IV increment does not have nuw.
-define void @tc_maxvscale_overflow(ptr noalias %a, ptr noalias %b) #0 {
+define void @tc_maxvscale_overflow(ptr noalias %a, ptr noalias %b) vscale_range(1, 16) {
 ; CHECK-LABEL: define void @tc_maxvscale_overflow(
 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
@@ -108,5 +106,3 @@ exit:
 }
 
 declare i32 @llvm.vscale.i32()
-
-attributes #0 = { vscale_range(1,16) "target-features"="+sve" }
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
index 669046831c4a5..01767ba3357bf 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/indvar-overflow-check-scalable-tc.ll
@@ -1,7 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
-; RUN: opt -passes=loop-vectorize -mtriple=riscv64 -S %s | FileCheck %s
-
-target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
+; RUN: opt -passes=loop-vectorize -mtriple=riscv64 -mattr=+v -S %s | FileCheck %s
 
 ; VF is scalable, as is TC. We can prove that TC > VF.
 define void @iv_no_overflow(ptr noalias %a, ptr noalias %b) #0 {
@@ -97,5 +95,3 @@ loop:
 exit:
   ret void
 }
-
-attributes #0 = { "target-features"="+v,+zvl512b" }

>From 175ed4a488af7761d038140bd61754ef4c0df9ed Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 24 Jun 2026 17:39:23 +0100
Subject: [PATCH 7/8] [LV/test] Add edge-case test

---
 .../indvar-overflow-check-scalable-tc.ll      | 52 ++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index e6bab4ee5fe3e..bfd1d0d186d63 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -105,4 +105,54 @@ exit:
   ret void
 }
 
-declare i32 @llvm.vscale.i32()
+; VF is not scalable, but TC is. This test exercises an
+; edge case, where we can prove that TC > VF.
+define void @iv_no_overflow_edge(ptr noalias %a, ptr noalias %b) vscale_range(2, 8) {
+; CHECK-LABEL: define void @iv_no_overflow_edge(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]]) #[[ATTR1:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VS:%.*]] = tail call i32 @llvm.vscale.i32()
+; CHECK-NEXT:    [[N:%.*]] = mul nuw nsw i32 [[VS]], 536870909
+; CHECK-NEXT:    br label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[N_RND_UP:%.*]] = add i32 [[N]], 3
+; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], 4
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[ACTIVE_LANE_MASK:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32 [[INDEX]], i32 [[N]])
+; CHECK-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP0]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i32 [[INDEX]]
+; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
+; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
+; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
+; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    br label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vs = tail call i32 @llvm.vscale.i32()
+  %n = mul nuw nsw i32 %vs, 536870909
+  br label %loop
+
+loop:
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep.a = getelementptr inbounds nuw float, ptr %a, i32 %iv
+  %la = load float, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds nuw float, ptr %b, i32 %iv
+  %lb = load float, ptr %gep.b, align 4
+  %mul = fmul float %la, %lb
+  store float %mul, ptr %gep.b, align 4
+  %iv.next = add nuw nsw i32 %iv, 1
+  %ec = icmp eq i32 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  ret void
+}

>From 9890417fe74e632e7664c97605aceef015530a9a Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 24 Jun 2026 17:40:47 +0100
Subject: [PATCH 8/8] [LV] Guard for non-scalable VF

---
 llvm/lib/Transforms/Vectorize/LoopVectorize.cpp                | 3 ++-
 .../LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 453b1b4afed61..18e6890d13e13 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1953,7 +1953,8 @@ static bool isIndvarOverflowCheckKnownFalse(
           getMaxVScale(*Cost->TheFunction, Cost->TTI);
       if (!MaxVScale)
         return false;
-      MaxVF *= *MaxVScale;
+      if (VF.isScalable())
+        MaxVF *= *MaxVScale;
       if (TC->isScalable()) {
         bool Overflow;
         MaxTC = SaturatingMultiply(MaxTC, *MaxVScale, &Overflow);
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
index bfd1d0d186d63..e5cff50a02a49 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/indvar-overflow-check-scalable-tc.ll
@@ -128,7 +128,7 @@ define void @iv_no_overflow_edge(ptr noalias %a, ptr noalias %b) vscale_range(2,
 ; CHECK-NEXT:    [[WIDE_MASKED_LOAD1:%.*]] = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]], <4 x float> poison)
 ; CHECK-NEXT:    [[TMP2:%.*]] = fmul <4 x float> [[WIDE_MASKED_LOAD]], [[WIDE_MASKED_LOAD1]]
 ; CHECK-NEXT:    call void @llvm.masked.store.v4f32.p0(<4 x float> [[TMP2]], ptr align 4 [[TMP1]], <4 x i1> [[ACTIVE_LANE_MASK]])
-; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[TMP3]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
 ; CHECK:       [[MIDDLE_BLOCK]]:



More information about the llvm-commits mailing list