[llvm] [LAA][LV]Allow recognition of strided pointers with constant stride (PR #171151)
Nashe Mncube via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 11 03:24:23 PST 2026
https://github.com/nasherm updated https://github.com/llvm/llvm-project/pull/171151
>From 5571b9c348a89c58ee6afb19f49859f59e56014c Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Fri, 5 Dec 2025 13:51:10 +0000
Subject: [PATCH 01/10] [IndVarSimplify] Add rewriting ptr-add phis with offset
addressing
This patch adds support to IndVarSimplify to rewrite IVs used in loops for
strided loads or stores. These IVs are rewritten to make use of offset
addressing thereby elimnating PHI nodes and simplifying the IR.
The motivation for this is that with these PHI nodes eliminated vectorization
becomes more feasible.
Change-Id: Ib3385ee5de03c46b4de035b550fbf56403e9e376
---
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 111 ++++++++++++++++++
.../rewrite-ptr-addr-with-offset-addr.ll | 64 ++++++++++
2 files changed, 175 insertions(+)
create mode 100644 llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index f377c192371ea..435931ce5ea26 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -164,6 +164,9 @@ class IndVarSimplify {
bool sinkUnusedInvariants(Loop *L);
+ bool rewritePtrIncrementWithOffsettAddressing(
+ Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts);
+
public:
IndVarSimplify(LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
const DataLayout &DL, TargetLibraryInfo *TLI,
@@ -2039,6 +2042,111 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
return Changed;
}
+bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
+ Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
+ SmallVector<PHINode *, 8> LoopPhis(
+ llvm::make_pointer_range(L->getHeader()->phis()));
+ bool Changed = false;
+
+ auto IsPtrToOffsetAddressingCandidate = [&](PHINode *PHI) -> bool {
+ if (PHI->user_empty())
+ return false;
+
+ if (PHI->hasConstantValue())
+ return false;
+
+ // TODO: is this necessary.
+ if (L->getCanonicalInductionVariable() == PHI)
+ return false;
+
+ // We are only concerned with simple PHI nodes
+ // with two incoming values.
+ if (PHI->getNumIncomingValues() != 2)
+ return false;
+
+ return true;
+ };
+
+ auto NonInvariantPHIOp = [&](Value *Op0, Value *Op1) -> Value * {
+ // One of the two incoming values must be loop invariant
+ // but not both
+ if (!(L->isLoopInvariant(Op0) != L->isLoopInvariant(Op1)))
+ return nullptr;
+
+ return (L->isLoopInvariant(Op0)) ? Op1 : Op0;
+ };
+
+ auto IsConstantIncrement = [&](GetElementPtrInst *GEP) -> bool {
+ if (GEP->getNumIndices() != 1)
+ return false;
+
+ if (GEP->hasAllConstantIndices())
+ return true;
+
+ for (Value *V : GEP->indices())
+ if (!L->isLoopInvariant(V))
+ return false;
+
+ return true;
+ };
+
+ auto AdjustWidth = [&](Value *V, IRBuilder<> &IR,
+ unsigned TargetWidth, bool IsSigned) -> Value * {
+ if (V->getType()->getIntegerBitWidth() == TargetWidth)
+ return V;
+ if (IsSigned)
+ return IR.CreateSExt(V, IntegerType::get(V->getContext(), TargetWidth));
+ return IR.CreateZExt(V, IntegerType::get(V->getContext(), TargetWidth));
+ };
+
+ IRBuilder<> Builder(L->getHeader()->getFirstNonPHI());
+ for (auto *PHI : LoopPhis) {
+ if (!IsPtrToOffsetAddressingCandidate(PHI))
+ continue;
+
+ Value *CanonicalIV = L->getCanonicalInductionVariable();
+ if (!CanonicalIV)
+ continue;
+ if (!L->getLatchCmpInst())
+ return false;
+
+ Value *Op0 = PHI->getIncomingValue(0);
+ Value *Op1 = PHI->getIncomingValue(1);
+ Value *LoopDependentIncomingVal = NonInvariantPHIOp(Op0, Op1);
+ if (!LoopDependentIncomingVal)
+ continue;
+
+ if (auto *LoopStrideGEPInst =
+ dyn_cast<GetElementPtrInst>(LoopDependentIncomingVal)) {
+ Value *InvariantIncomingVal =
+ (Op0 == LoopDependentIncomingVal) ? Op1 : Op0;
+ if (!dyn_cast<GetElementPtrInst>(InvariantIncomingVal))
+ continue;
+
+ if (!IsConstantIncrement(LoopStrideGEPInst))
+ continue;
+
+ // Replace PHI with offset addressing GEP
+ Value *Stride = LoopStrideGEPInst->getOperand(1);
+ bool IsSigned = L->getLatchCmpInst()->isSigned();
+ unsigned MaxWidth = std::max(CanonicalIV->getType()->getIntegerBitWidth(),
+ Stride->getType()->getIntegerBitWidth());
+
+ CanonicalIV = AdjustWidth(CanonicalIV, Builder, MaxWidth, IsSigned);
+ Stride = AdjustWidth(Stride, Builder, MaxWidth, IsSigned);
+
+ Value *Mul = Builder.CreateMul(CanonicalIV, Stride);
+ Value *NewGEP =
+ Builder.CreateInBoundsGEP(LoopStrideGEPInst->getResultElementType(),
+ InvariantIncomingVal, ArrayRef(Mul));
+ PHI->replaceAllUsesWith(NewGEP);
+ DeadInsts.emplace_back(PHI);
+ Changed |= true;
+ }
+ }
+ return Changed;
+}
+
//===----------------------------------------------------------------------===//
// IndVarSimplify driver. Manage several subpasses of IV simplification.
//===----------------------------------------------------------------------===//
@@ -2116,6 +2224,9 @@ bool IndVarSimplify::run(Loop *L) {
SE->forgetLoop(L);
}
+ // Try to rewrite ptr increments with ptr offset addressing
+ Changed |= rewritePtrIncrementWithOffsettAddressing(L, DeadInsts);
+
// If we have a trip count expression, rewrite the loop's exit condition
// using it.
if (!DisableLFTR) {
diff --git a/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll b/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
new file mode 100644
index 0000000000000..c08696f51b076
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
@@ -0,0 +1,64 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=indvars -S -o - | FileCheck %s
+
+
+define void @mat_transpose(float* %pIn, float* %pOut, i32 %nRows, i32 %nCols) {
+; CHECK-LABEL: define void @mat_transpose(
+; CHECK-SAME: ptr [[PIN:%.*]], ptr [[POUT:%.*]], i32 [[NROWS:%.*]], i32 [[NCOLS:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[LOOP_ROW:.*]]
+; CHECK: [[LOOP_ROW]]:
+; CHECK-NEXT: [[ROW_IV:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ROW_INC:%.*]], %[[LOOP_ROW_END:.*]] ]
+; CHECK-NEXT: [[CMP_ROW:%.*]] = icmp ult i32 [[ROW_IV]], [[NROWS]]
+; CHECK-NEXT: br i1 [[CMP_ROW]], label %[[LOOP_COL_PRE:.*]], label %[[EXIT:.*]]
+; CHECK: [[LOOP_COL_PRE]]:
+; CHECK-NEXT: [[PX_BASE:%.*]] = getelementptr inbounds float, ptr [[POUT]], i32 [[ROW_IV]]
+; CHECK-NEXT: br label %[[LOOP_COL:.*]]
+; CHECK: [[LOOP_COL]]:
+; CHECK-NEXT: [[COL_IV:%.*]] = phi i32 [ 0, %[[LOOP_COL_PRE]] ], [ [[COL_INC:%.*]], %[[LOOP_COL]] ]
+; CHECK-NEXT: [[PIN_PTR:%.*]] = phi ptr [ [[PIN]], %[[LOOP_COL_PRE]] ], [ [[PIN_NEXT:%.*]], %[[LOOP_COL]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = mul i32 [[COL_IV]], [[NROWS]]
+; CHECK-NEXT: [[PX_PTR:%.*]] = getelementptr inbounds float, ptr [[PX_BASE]], i32 [[TMP0]]
+; CHECK-NEXT: [[PIN_LOAD:%.*]] = load float, ptr [[PIN_PTR]], align 4
+; CHECK-NEXT: [[PIN_NEXT]] = getelementptr float, ptr [[PIN_PTR]], i32 1
+; CHECK-NEXT: store float [[PIN_LOAD]], ptr [[PX_PTR]], align 4
+; CHECK-NEXT: [[COL_INC]] = add nuw i32 [[COL_IV]], 1
+; CHECK-NEXT: [[CMP_COL:%.*]] = icmp ult i32 [[COL_INC]], [[NCOLS]]
+; CHECK-NEXT: br i1 [[CMP_COL]], label %[[LOOP_COL]], label %[[LOOP_ROW_END]]
+; CHECK: [[LOOP_ROW_END]]:
+; CHECK-NEXT: [[ROW_INC]] = add nuw i32 [[ROW_IV]], 1
+; CHECK-NEXT: br label %[[LOOP_ROW]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop_row
+
+loop_row:
+ %row.iv = phi i32 [ 0, %entry ], [ %row.inc, %loop_row_end ]
+ %cmp.row = icmp ult i32 %row.iv, %nRows
+ br i1 %cmp.row, label %loop_col_pre, label %exit
+
+loop_col_pre:
+ %px.base = getelementptr inbounds float, float* %pOut, i32 %row.iv
+ br label %loop_col
+
+loop_col:
+ %col.iv = phi i32 [ 0, %loop_col_pre ], [ %col.inc, %loop_col ]
+ %pIn.ptr = phi float* [ %pIn, %loop_col_pre ], [ %pIn.next, %loop_col ]
+ %px.ptr = phi float* [ %px.base, %loop_col_pre ], [ %px.next, %loop_col ]
+ %pIn.load = load float, float* %pIn.ptr
+ %pIn.next = getelementptr float, float* %pIn.ptr, i32 1
+ store float %pIn.load, float* %px.ptr
+ %px.next = getelementptr float, float* %px.ptr, i32 %nRows
+ %col.inc = add nuw i32 %col.iv, 1
+ %cmp.col = icmp ult i32 %col.inc, %nCols
+ br i1 %cmp.col, label %loop_col, label %loop_row_end
+
+loop_row_end:
+ %row.inc = add nuw i32 %row.iv, 1
+ br label %loop_row
+
+exit:
+ ret void
+}
>From 3877b5051396129b3407675b923a21d86d2d6221 Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Tue, 9 Dec 2025 10:34:25 +0000
Subject: [PATCH 02/10] Compiler warning due to use of deprecated function
Change-Id: Id8287887c3c77c24619ebf07b5f76a532217aff6
---
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index 435931ce5ea26..b76c784aba879 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -2099,7 +2099,7 @@ bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
return IR.CreateZExt(V, IntegerType::get(V->getContext(), TargetWidth));
};
- IRBuilder<> Builder(L->getHeader()->getFirstNonPHI());
+ IRBuilder<> Builder(&*L->getHeader()->getFirstInsertionPt());
for (auto *PHI : LoopPhis) {
if (!IsPtrToOffsetAddressingCandidate(PHI))
continue;
>From b6e55f35cc94ad06d1b5205dec7e39deb9f12c2d Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Tue, 9 Dec 2025 10:39:06 +0000
Subject: [PATCH 03/10] Formatting
Change-Id: I4d161c236398dca05be84d43e33ada913309b992
---
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index b76c784aba879..fb891b786c8fe 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -2090,8 +2090,8 @@ bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
return true;
};
- auto AdjustWidth = [&](Value *V, IRBuilder<> &IR,
- unsigned TargetWidth, bool IsSigned) -> Value * {
+ auto AdjustWidth = [&](Value *V, IRBuilder<> &IR, unsigned TargetWidth,
+ bool IsSigned) -> Value * {
if (V->getType()->getIntegerBitWidth() == TargetWidth)
return V;
if (IsSigned)
>From 56e6bb23026f93ece3cde0b6f1daa21cf377a2ef Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Tue, 9 Dec 2025 11:15:01 +0000
Subject: [PATCH 04/10] Copilot review
Change-Id: Ida6175d40f7bc0f95b28e37a30538aa3cbd64a1e
---
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index fb891b786c8fe..a1fa76f39814c 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -164,7 +164,7 @@ class IndVarSimplify {
bool sinkUnusedInvariants(Loop *L);
- bool rewritePtrIncrementWithOffsettAddressing(
+ bool rewritePtrIncrementWithOffsetAddressing(
Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts);
public:
@@ -2042,12 +2042,15 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
return Changed;
}
-bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
+bool IndVarSimplify::rewritePtrIncrementWithOffsetAddressing(
Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
SmallVector<PHINode *, 8> LoopPhis(
llvm::make_pointer_range(L->getHeader()->phis()));
bool Changed = false;
+ if (!L->getLatchCmpInst())
+ return false;
+
auto IsPtrToOffsetAddressingCandidate = [&](PHINode *PHI) -> bool {
if (PHI->user_empty())
return false;
@@ -2055,7 +2058,6 @@ bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
if (PHI->hasConstantValue())
return false;
- // TODO: is this necessary.
if (L->getCanonicalInductionVariable() == PHI)
return false;
@@ -2107,8 +2109,6 @@ bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
Value *CanonicalIV = L->getCanonicalInductionVariable();
if (!CanonicalIV)
continue;
- if (!L->getLatchCmpInst())
- return false;
Value *Op0 = PHI->getIncomingValue(0);
Value *Op1 = PHI->getIncomingValue(1);
@@ -2141,7 +2141,7 @@ bool IndVarSimplify::rewritePtrIncrementWithOffsettAddressing(
InvariantIncomingVal, ArrayRef(Mul));
PHI->replaceAllUsesWith(NewGEP);
DeadInsts.emplace_back(PHI);
- Changed |= true;
+ Changed = true;
}
}
return Changed;
@@ -2225,7 +2225,7 @@ bool IndVarSimplify::run(Loop *L) {
}
// Try to rewrite ptr increments with ptr offset addressing
- Changed |= rewritePtrIncrementWithOffsettAddressing(L, DeadInsts);
+ Changed |= rewritePtrIncrementWithOffsetAddressing(L, DeadInsts);
// If we have a trip count expression, rewrite the loop's exit condition
// using it.
>From 25e0efc660e5e28be7c107adb796ec6d4bc33852 Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Mon, 12 Jan 2026 13:17:05 +0000
Subject: [PATCH 05/10] [LAA][LV]Allow recognition of strided pointers with
constant stride
This patch fixes an issue found during LoopAccessAnalysis with respect
to recognising strided pointers that make use of runtime constants.
It was found that for a loop of the following form
```
for (unsigned row = 0; row < nRows; row++) {
float *px = pOut + row;
for (unsigned col = 0; col < nCols; col++) {
*px = *pIn++;
px += nRows;
}
```
Vectorization would expectedly fail due to the use of a runtime strided IV px.
When passing the `lv-strided-pointer-ivs=true` option, vectorization would be
attempted but would be seen as non-profitable. However, when transforming the loop
to the following form
```
for (unsigned row = 0; row < nRows; row++) {
for (unsigned col = 0; col < nCols; col++) {
pOut[row + col * nRows] = *pIn++;
}
```
With `lv-strided-pointer-ivs=true` this vectorizes successfully. As these
loops are equivalent, investigation sought to find out why vectorization was failing.
The cause was found to be that when obtaining the stride for the IVs LAA doesn't
recognise strides that may make use of loop-invariant runtime constants
unless they are in a specific SCEV add-rec form such as in loop two. This patch
allows for the first type of add-rec to also be recognised when the option
`lv-strided-pointer-ivs=true` is passed to the compiler.
Change-Id: Ib01a6b802f516516ac0215569a8d283af915c297
---
.../llvm/Analysis/LoopAccessAnalysis.h | 3 +-
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 46 ++++++-
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 111 ---------------
.../Vectorize/LoopVectorizationLegality.cpp | 7 +-
.../rewrite-ptr-addr-with-offset-addr.ll | 64 ---------
.../LoopVectorize/RISCV/strided-accesses.ll | 48 +++----
.../LoopVectorize/pointer-induction.ll | 130 +++++++++++++++---
.../LoopVectorize/scev-predicate-reasoning.ll | 21 +--
8 files changed, 176 insertions(+), 254 deletions(-)
delete mode 100644 llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
index c85ef3e131068..902f76496eede 100644
--- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
+++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
@@ -896,7 +896,8 @@ getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap =
DenseMap<Value *, const SCEV *>(),
- bool Assume = false, bool ShouldCheckWrap = true);
+ bool Assume = false, bool ShouldCheckWrap = true,
+ bool AllowStridedPtrs = false);
/// Returns the distance between the pointers \p PtrA and \p PtrB iff they are
/// compatible and it is possible to calculate the distance between them. This
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 2fc724970747c..a8e2ee647269a 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -968,7 +968,8 @@ class AccessAnalysis {
/// isNoWrap.
static std::optional<int64_t>
getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
- Value *Ptr, PredicatedScalarEvolution &PSE) {
+ Value *Ptr, PredicatedScalarEvolution &PSE,
+ bool AllowStridedPtrs = false) {
if (isa<ScalableVectorType>(AccessTy)) {
LLVM_DEBUG(dbgs() << "LAA: Bad stride - Scalable object: " << *AccessTy
<< "\n");
@@ -987,12 +988,43 @@ getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
return std::nullopt;
}
- // Check the step is constant.
- const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
-
// Calculate the pointer stride and check if it is constant.
+ // If we allow strided pointers we also check the add recurrence
+ // expression is making use of loop-invariant
+ // runtime constant for example
+ //
+ // Step = { stride * runtime_constant }
+ //
+ // we should treat this as a constant add recurrence.
+ // To achieve this we check the arguments of
+ // the step are loop invariant and compute the step
+ // as equivalent to just stride.
+ //
+ // We only check this for mul expressions as these
+ // are most common and expected for strided
+ // pointer recurrence expressions.
+ const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
const APInt *APStepVal;
- if (!match(Step, m_scev_APInt(APStepVal))) {
+ auto IsInvariantConstAddRecStep = [&](const SCEV *Step) -> bool {
+ const SCEV *Multiplier;
+
+ // Check the step is constant.
+ if (match(Step, m_scev_APInt(APStepVal)))
+ return true;
+
+ if (AllowStridedPtrs) {
+ for (const auto *Op : Step->operands()) {
+ if (!PSE.getSE()->isLoopInvariant(Op, Lp))
+ return false;
+ }
+ return match(Step,
+ m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)));
+ }
+
+ return false;
+ };
+
+ if (!IsInvariantConstAddRecStep(Step)) {
LLVM_DEBUG({
dbgs() << "LAA: Bad stride - Not a constant strided ";
if (Ptr)
@@ -1625,7 +1657,7 @@ std::optional<int64_t>
llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap,
- bool Assume, bool ShouldCheckWrap) {
+ bool Assume, bool ShouldCheckWrap, bool AllowStridedPtrs) {
const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
return 0;
@@ -1643,7 +1675,7 @@ llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
}
std::optional<int64_t> Stride =
- getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE);
+ getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE, AllowStridedPtrs);
if (!ShouldCheckWrap || !Stride)
return Stride;
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index a1fa76f39814c..f377c192371ea 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -164,9 +164,6 @@ class IndVarSimplify {
bool sinkUnusedInvariants(Loop *L);
- bool rewritePtrIncrementWithOffsetAddressing(
- Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts);
-
public:
IndVarSimplify(LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
const DataLayout &DL, TargetLibraryInfo *TLI,
@@ -2042,111 +2039,6 @@ bool IndVarSimplify::predicateLoopExits(Loop *L, SCEVExpander &Rewriter) {
return Changed;
}
-bool IndVarSimplify::rewritePtrIncrementWithOffsetAddressing(
- Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
- SmallVector<PHINode *, 8> LoopPhis(
- llvm::make_pointer_range(L->getHeader()->phis()));
- bool Changed = false;
-
- if (!L->getLatchCmpInst())
- return false;
-
- auto IsPtrToOffsetAddressingCandidate = [&](PHINode *PHI) -> bool {
- if (PHI->user_empty())
- return false;
-
- if (PHI->hasConstantValue())
- return false;
-
- if (L->getCanonicalInductionVariable() == PHI)
- return false;
-
- // We are only concerned with simple PHI nodes
- // with two incoming values.
- if (PHI->getNumIncomingValues() != 2)
- return false;
-
- return true;
- };
-
- auto NonInvariantPHIOp = [&](Value *Op0, Value *Op1) -> Value * {
- // One of the two incoming values must be loop invariant
- // but not both
- if (!(L->isLoopInvariant(Op0) != L->isLoopInvariant(Op1)))
- return nullptr;
-
- return (L->isLoopInvariant(Op0)) ? Op1 : Op0;
- };
-
- auto IsConstantIncrement = [&](GetElementPtrInst *GEP) -> bool {
- if (GEP->getNumIndices() != 1)
- return false;
-
- if (GEP->hasAllConstantIndices())
- return true;
-
- for (Value *V : GEP->indices())
- if (!L->isLoopInvariant(V))
- return false;
-
- return true;
- };
-
- auto AdjustWidth = [&](Value *V, IRBuilder<> &IR, unsigned TargetWidth,
- bool IsSigned) -> Value * {
- if (V->getType()->getIntegerBitWidth() == TargetWidth)
- return V;
- if (IsSigned)
- return IR.CreateSExt(V, IntegerType::get(V->getContext(), TargetWidth));
- return IR.CreateZExt(V, IntegerType::get(V->getContext(), TargetWidth));
- };
-
- IRBuilder<> Builder(&*L->getHeader()->getFirstInsertionPt());
- for (auto *PHI : LoopPhis) {
- if (!IsPtrToOffsetAddressingCandidate(PHI))
- continue;
-
- Value *CanonicalIV = L->getCanonicalInductionVariable();
- if (!CanonicalIV)
- continue;
-
- Value *Op0 = PHI->getIncomingValue(0);
- Value *Op1 = PHI->getIncomingValue(1);
- Value *LoopDependentIncomingVal = NonInvariantPHIOp(Op0, Op1);
- if (!LoopDependentIncomingVal)
- continue;
-
- if (auto *LoopStrideGEPInst =
- dyn_cast<GetElementPtrInst>(LoopDependentIncomingVal)) {
- Value *InvariantIncomingVal =
- (Op0 == LoopDependentIncomingVal) ? Op1 : Op0;
- if (!dyn_cast<GetElementPtrInst>(InvariantIncomingVal))
- continue;
-
- if (!IsConstantIncrement(LoopStrideGEPInst))
- continue;
-
- // Replace PHI with offset addressing GEP
- Value *Stride = LoopStrideGEPInst->getOperand(1);
- bool IsSigned = L->getLatchCmpInst()->isSigned();
- unsigned MaxWidth = std::max(CanonicalIV->getType()->getIntegerBitWidth(),
- Stride->getType()->getIntegerBitWidth());
-
- CanonicalIV = AdjustWidth(CanonicalIV, Builder, MaxWidth, IsSigned);
- Stride = AdjustWidth(Stride, Builder, MaxWidth, IsSigned);
-
- Value *Mul = Builder.CreateMul(CanonicalIV, Stride);
- Value *NewGEP =
- Builder.CreateInBoundsGEP(LoopStrideGEPInst->getResultElementType(),
- InvariantIncomingVal, ArrayRef(Mul));
- PHI->replaceAllUsesWith(NewGEP);
- DeadInsts.emplace_back(PHI);
- Changed = true;
- }
- }
- return Changed;
-}
-
//===----------------------------------------------------------------------===//
// IndVarSimplify driver. Manage several subpasses of IV simplification.
//===----------------------------------------------------------------------===//
@@ -2224,9 +2116,6 @@ bool IndVarSimplify::run(Loop *L) {
SE->forgetLoop(L);
}
- // Try to rewrite ptr increments with ptr offset addressing
- Changed |= rewritePtrIncrementWithOffsetAddressing(L, DeadInsts);
-
// If we have a trip count expression, rewrite the loop's exit condition
// using it.
if (!DisableLFTR) {
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 1b000be130e49..1690ecc3674f4 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -481,9 +481,10 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
- int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
- AllowRuntimeSCEVChecks, false)
- .value_or(0);
+ int Stride =
+ getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
+ AllowRuntimeSCEVChecks, false, AllowStridedPointerIVs)
+ .value_or(0);
if (Stride == 1 || Stride == -1)
return Stride;
return 0;
diff --git a/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll b/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
deleted file mode 100644
index c08696f51b076..0000000000000
--- a/llvm/test/Transforms/IndVarSimplify/rewrite-ptr-addr-with-offset-addr.ll
+++ /dev/null
@@ -1,64 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
-; RUN: opt < %s -passes=indvars -S -o - | FileCheck %s
-
-
-define void @mat_transpose(float* %pIn, float* %pOut, i32 %nRows, i32 %nCols) {
-; CHECK-LABEL: define void @mat_transpose(
-; CHECK-SAME: ptr [[PIN:%.*]], ptr [[POUT:%.*]], i32 [[NROWS:%.*]], i32 [[NCOLS:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*]]:
-; CHECK-NEXT: br label %[[LOOP_ROW:.*]]
-; CHECK: [[LOOP_ROW]]:
-; CHECK-NEXT: [[ROW_IV:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ROW_INC:%.*]], %[[LOOP_ROW_END:.*]] ]
-; CHECK-NEXT: [[CMP_ROW:%.*]] = icmp ult i32 [[ROW_IV]], [[NROWS]]
-; CHECK-NEXT: br i1 [[CMP_ROW]], label %[[LOOP_COL_PRE:.*]], label %[[EXIT:.*]]
-; CHECK: [[LOOP_COL_PRE]]:
-; CHECK-NEXT: [[PX_BASE:%.*]] = getelementptr inbounds float, ptr [[POUT]], i32 [[ROW_IV]]
-; CHECK-NEXT: br label %[[LOOP_COL:.*]]
-; CHECK: [[LOOP_COL]]:
-; CHECK-NEXT: [[COL_IV:%.*]] = phi i32 [ 0, %[[LOOP_COL_PRE]] ], [ [[COL_INC:%.*]], %[[LOOP_COL]] ]
-; CHECK-NEXT: [[PIN_PTR:%.*]] = phi ptr [ [[PIN]], %[[LOOP_COL_PRE]] ], [ [[PIN_NEXT:%.*]], %[[LOOP_COL]] ]
-; CHECK-NEXT: [[TMP0:%.*]] = mul i32 [[COL_IV]], [[NROWS]]
-; CHECK-NEXT: [[PX_PTR:%.*]] = getelementptr inbounds float, ptr [[PX_BASE]], i32 [[TMP0]]
-; CHECK-NEXT: [[PIN_LOAD:%.*]] = load float, ptr [[PIN_PTR]], align 4
-; CHECK-NEXT: [[PIN_NEXT]] = getelementptr float, ptr [[PIN_PTR]], i32 1
-; CHECK-NEXT: store float [[PIN_LOAD]], ptr [[PX_PTR]], align 4
-; CHECK-NEXT: [[COL_INC]] = add nuw i32 [[COL_IV]], 1
-; CHECK-NEXT: [[CMP_COL:%.*]] = icmp ult i32 [[COL_INC]], [[NCOLS]]
-; CHECK-NEXT: br i1 [[CMP_COL]], label %[[LOOP_COL]], label %[[LOOP_ROW_END]]
-; CHECK: [[LOOP_ROW_END]]:
-; CHECK-NEXT: [[ROW_INC]] = add nuw i32 [[ROW_IV]], 1
-; CHECK-NEXT: br label %[[LOOP_ROW]]
-; CHECK: [[EXIT]]:
-; CHECK-NEXT: ret void
-;
-entry:
- br label %loop_row
-
-loop_row:
- %row.iv = phi i32 [ 0, %entry ], [ %row.inc, %loop_row_end ]
- %cmp.row = icmp ult i32 %row.iv, %nRows
- br i1 %cmp.row, label %loop_col_pre, label %exit
-
-loop_col_pre:
- %px.base = getelementptr inbounds float, float* %pOut, i32 %row.iv
- br label %loop_col
-
-loop_col:
- %col.iv = phi i32 [ 0, %loop_col_pre ], [ %col.inc, %loop_col ]
- %pIn.ptr = phi float* [ %pIn, %loop_col_pre ], [ %pIn.next, %loop_col ]
- %px.ptr = phi float* [ %px.base, %loop_col_pre ], [ %px.next, %loop_col ]
- %pIn.load = load float, float* %pIn.ptr
- %pIn.next = getelementptr float, float* %pIn.ptr, i32 1
- store float %pIn.load, float* %px.ptr
- %px.next = getelementptr float, float* %px.ptr, i32 %nRows
- %col.inc = add nuw i32 %col.iv, 1
- %cmp.col = icmp ult i32 %col.inc, %nCols
- br i1 %cmp.col, label %loop_col, label %loop_row_end
-
-loop_row_end:
- %row.inc = add nuw i32 %row.iv, 1
- br label %loop_row
-
-exit:
- ret void
-}
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 0da00bf0dbd0f..2bef1e50317de 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -764,25 +764,20 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
; STRIDED-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
; STRIDED: vector.ph:
-; STRIDED-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[STRIDE]], i64 0
-; STRIDED-NEXT: [[BROADCAST_SPLAT1:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; STRIDED-NEXT: [[TMP12:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
; STRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED: vector.body:
-; STRIDED-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[TMP12]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[TMP43:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; STRIDED-NEXT: [[TMP44:%.*]] = zext i32 [[TMP43]] to i64
-; STRIDED-NEXT: [[BROADCAST_SPLATINSERT9:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP44]], i64 0
-; STRIDED-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT9]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; STRIDED-NEXT: [[TMP18:%.*]] = mul nuw nsw <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT1]]
-; STRIDED-NEXT: [[TMP19:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP18]]
-; STRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP19]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META5:![0-9]+]]
+; STRIDED-NEXT: [[TMP42:%.*]] = mul nuw nsw i64 [[EVL_BASED_IV]], [[STRIDE]]
+; STRIDED-NEXT: [[TMP45:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP42]]
+; STRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP45]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META5:![0-9]+]]
; STRIDED-NEXT: [[TMP20:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
-; STRIDED-NEXT: [[TMP21:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP18]]
-; STRIDED-NEXT: call void @llvm.vp.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP20]], <vscale x 4 x ptr> align 4 [[TMP21]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
+; STRIDED-NEXT: [[TMP46:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP42]]
+; STRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP20]], ptr align 4 [[TMP46]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
+; STRIDED-NEXT: [[TMP44:%.*]] = zext i32 [[TMP43]] to i64
+; STRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP44]], [[EVL_BASED_IV]]
; STRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP44]]
-; STRIDED-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
; STRIDED-NEXT: [[TMP41:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; STRIDED-NEXT: br i1 [[TMP41]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
; STRIDED: middle.block:
@@ -861,33 +856,24 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2: vector.ph:
; STRIDED-UF2-NEXT: [[TMP28:%.*]] = call i64 @llvm.vscale.i64()
; STRIDED-UF2-NEXT: [[TMP29:%.*]] = shl nuw i64 [[TMP28]], 2
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP29]], i64 0
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
; STRIDED-UF2-NEXT: [[TMP30:%.*]] = shl nuw i64 [[TMP29]], 1
; STRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP30]]
; STRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[N_MOD_VF]]
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT10:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[STRIDE]], i64 0
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT11:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT10]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; STRIDED-UF2-NEXT: [[TMP31:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED-UF2: vector.body:
; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[TMP31]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = add <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
-; STRIDED-UF2-NEXT: [[TMP33:%.*]] = mul nuw nsw <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT11]]
-; STRIDED-UF2-NEXT: [[TMP34:%.*]] = mul nuw nsw <vscale x 4 x i64> [[STEP_ADD]], [[BROADCAST_SPLAT11]]
-; STRIDED-UF2-NEXT: [[TMP35:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP33]]
-; STRIDED-UF2-NEXT: [[TMP36:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP34]]
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP35]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison), !alias.scope [[META8:![0-9]+]]
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER12:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP36]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison), !alias.scope [[META8]]
+; STRIDED-UF2-NEXT: [[TMP31:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
+; STRIDED-UF2-NEXT: [[TMP32:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP31]]
+; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[TMP32]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = load <vscale x 4 x i32>, ptr [[TMP32]], align 4, !alias.scope [[META8:![0-9]+]]
+; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER12:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8]]
; STRIDED-UF2-NEXT: [[TMP37:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
; STRIDED-UF2-NEXT: [[TMP38:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER12]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP39:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP33]]
-; STRIDED-UF2-NEXT: [[TMP40:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP34]]
-; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP37]], <vscale x 4 x ptr> align 4 [[TMP39]], <vscale x 4 x i1> splat (i1 true)), !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
-; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP38]], <vscale x 4 x ptr> align 4 [[TMP40]], <vscale x 4 x i1> splat (i1 true)), !alias.scope [[META11]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: [[TMP36:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP31]]
+; STRIDED-UF2-NEXT: [[TMP39:%.*]] = getelementptr i32, ptr [[TMP36]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP37]], ptr [[TMP36]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP38]], ptr [[TMP39]], align 4, !alias.scope [[META11]], !noalias [[META8]]
; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP30]]
-; STRIDED-UF2-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i64> [[STEP_ADD]], [[BROADCAST_SPLAT]]
; STRIDED-UF2-NEXT: [[TMP41:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; STRIDED-UF2-NEXT: br i1 [[TMP41]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP13:![0-9]+]]
; STRIDED-UF2: middle.block:
diff --git a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
index 995c2016339a4..44514cf758450 100644
--- a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
+++ b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
@@ -536,28 +536,10 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED: vector.body:
; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], [[TMP1]]
-; STRIDED-NEXT: [[TMP8:%.*]] = mul i64 0, [[TMP1]]
-; STRIDED-NEXT: [[TMP9:%.*]] = add i64 [[OFFSET_IDX]], [[TMP8]]
-; STRIDED-NEXT: [[TMP10:%.*]] = mul i64 1, [[TMP1]]
-; STRIDED-NEXT: [[TMP11:%.*]] = add i64 [[OFFSET_IDX]], [[TMP10]]
-; STRIDED-NEXT: [[TMP12:%.*]] = mul i64 2, [[TMP1]]
-; STRIDED-NEXT: [[TMP13:%.*]] = add i64 [[OFFSET_IDX]], [[TMP12]]
-; STRIDED-NEXT: [[TMP14:%.*]] = mul i64 3, [[TMP1]]
-; STRIDED-NEXT: [[TMP15:%.*]] = add i64 [[OFFSET_IDX]], [[TMP14]]
-; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[TMP9]]
-; STRIDED-NEXT: [[NEXT_GEP1:%.*]] = getelementptr i8, ptr null, i64 [[TMP11]]
-; STRIDED-NEXT: [[NEXT_GEP2:%.*]] = getelementptr i8, ptr null, i64 [[TMP13]]
-; STRIDED-NEXT: [[NEXT_GEP3:%.*]] = getelementptr i8, ptr null, i64 [[TMP15]]
+; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[OFFSET_IDX]]
; STRIDED-NEXT: [[TMP21:%.*]] = getelementptr i64, ptr [[A:%.*]], i64 [[INDEX]]
; STRIDED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i64>, ptr [[TMP21]], align 8
-; STRIDED-NEXT: [[TMP23:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 0
-; STRIDED-NEXT: [[TMP24:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 1
-; STRIDED-NEXT: [[TMP16:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 2
-; STRIDED-NEXT: [[TMP25:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 3
-; STRIDED-NEXT: store i64 [[TMP23]], ptr [[NEXT_GEP]], align 8
-; STRIDED-NEXT: store i64 [[TMP24]], ptr [[NEXT_GEP1]], align 8
-; STRIDED-NEXT: store i64 [[TMP16]], ptr [[NEXT_GEP2]], align 8
-; STRIDED-NEXT: store i64 [[TMP25]], ptr [[NEXT_GEP3]], align 8
+; STRIDED-NEXT: store <4 x i64> [[WIDE_LOAD]], ptr [[NEXT_GEP]], align 8
; STRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; STRIDED-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; STRIDED-NEXT: br i1 [[TMP18]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]
@@ -700,3 +682,111 @@ exit:
%result = add i64 %cast.ptr, %0
ret i64 %result
}
+
+; Test a pointer induction with a loop-invariant runtime stride.
+define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32 %stride) {
+; DEFAULT-LABEL: @strided_ptr_iv_runtime_stride(
+; DEFAULT-NEXT: entry:
+; DEFAULT-NEXT: br label [[LOOP:%.*]]
+; DEFAULT: loop:
+; DEFAULT-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
+; DEFAULT-NEXT: [[IN_PTR:%.*]] = phi ptr [ [[PIN:%.*]], [[ENTRY]] ], [ [[IN_NEXT:%.*]], [[LOOP]] ]
+; DEFAULT-NEXT: [[OUT_PTR:%.*]] = phi ptr [ [[POUT:%.*]], [[ENTRY]] ], [ [[OUT_NEXT:%.*]], [[LOOP]] ]
+; DEFAULT-NEXT: [[VAL:%.*]] = load float, ptr [[IN_PTR]], align 4
+; DEFAULT-NEXT: store float [[VAL]], ptr [[OUT_PTR]], align 4
+; DEFAULT-NEXT: [[IN_NEXT]] = getelementptr inbounds float, ptr [[IN_PTR]], i32 1
+; DEFAULT-NEXT: [[OUT_NEXT]] = getelementptr inbounds float, ptr [[OUT_PTR]], i32 [[STRIDE:%.*]]
+; DEFAULT-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
+; DEFAULT-NEXT: [[CMP:%.*]] = icmp ult i32 [[IV_NEXT]], [[NCOLS:%.*]]
+; DEFAULT-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT:%.*]]
+; DEFAULT: exit:
+; DEFAULT-NEXT: ret void
+;
+; STRIDED-LABEL: @strided_ptr_iv_runtime_stride(
+; STRIDED-NEXT: entry:
+; STRIDED-NEXT: [[TMP0:%.*]] = zext i32 [[NCOLS:%.*]] to i64
+; STRIDED-NEXT: [[UMAX4:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP0]], i64 1)
+; STRIDED-NEXT: [[TMP1:%.*]] = sext i32 [[STRIDE:%.*]] to i64
+; STRIDED-NEXT: [[TMP2:%.*]] = shl nsw i64 [[TMP1]], 2
+; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[UMAX4]], 4
+; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
+; STRIDED: vector.memcheck:
+; STRIDED-NEXT: [[TMP3:%.*]] = sext i32 [[STRIDE]] to i64
+; STRIDED-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[NCOLS]], i32 1)
+; STRIDED-NEXT: [[TMP4:%.*]] = add i32 [[UMAX]], -1
+; STRIDED-NEXT: [[TMP5:%.*]] = zext i32 [[TMP4]] to i64
+; STRIDED-NEXT: [[TMP6:%.*]] = mul i64 [[TMP3]], [[TMP5]]
+; STRIDED-NEXT: [[TMP7:%.*]] = shl i64 [[TMP6]], 2
+; STRIDED-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[POUT:%.*]], i64 [[TMP7]]
+; STRIDED-NEXT: [[TMP8:%.*]] = icmp ult ptr [[POUT]], [[SCEVGEP]]
+; STRIDED-NEXT: [[UMIN:%.*]] = select i1 [[TMP8]], ptr [[POUT]], ptr [[SCEVGEP]]
+; STRIDED-NEXT: [[TMP9:%.*]] = icmp ugt ptr [[POUT]], [[SCEVGEP]]
+; STRIDED-NEXT: [[UMAX1:%.*]] = select i1 [[TMP9]], ptr [[POUT]], ptr [[SCEVGEP]]
+; STRIDED-NEXT: [[SCEVGEP2:%.*]] = getelementptr i8, ptr [[UMAX1]], i64 4
+; STRIDED-NEXT: [[TMP10:%.*]] = shl nuw nsw i64 [[TMP5]], 2
+; STRIDED-NEXT: [[TMP11:%.*]] = add nuw nsw i64 [[TMP10]], 4
+; STRIDED-NEXT: [[SCEVGEP3:%.*]] = getelementptr i8, ptr [[PIN:%.*]], i64 [[TMP11]]
+; STRIDED-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[UMIN]], [[SCEVGEP3]]
+; STRIDED-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[PIN]], [[SCEVGEP2]]
+; STRIDED-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
+; STRIDED-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
+; STRIDED: vector.ph:
+; STRIDED-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[UMAX4]], 4
+; STRIDED-NEXT: [[N_VEC:%.*]] = sub i64 [[UMAX4]], [[N_MOD_VF]]
+; STRIDED-NEXT: [[TMP12:%.*]] = trunc i64 [[N_VEC]] to i32
+; STRIDED-NEXT: [[TMP13:%.*]] = mul i64 [[N_VEC]], 4
+; STRIDED-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[PIN]], i64 [[TMP13]]
+; STRIDED-NEXT: [[TMP15:%.*]] = mul i64 [[N_VEC]], [[TMP2]]
+; STRIDED-NEXT: [[TMP16:%.*]] = getelementptr i8, ptr [[POUT]], i64 [[TMP15]]
+; STRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
+; STRIDED: vector.body:
+; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], 4
+; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr [[PIN]], i64 [[OFFSET_IDX]]
+; STRIDED-NEXT: [[OFFSET_IDX5:%.*]] = mul i64 [[INDEX]], [[TMP2]]
+; STRIDED-NEXT: [[NEXT_GEP6:%.*]] = getelementptr i8, ptr [[POUT]], i64 [[OFFSET_IDX5]]
+; STRIDED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[NEXT_GEP]], align 4, !alias.scope [[META16:![0-9]+]]
+; STRIDED-NEXT: store <4 x float> [[WIDE_LOAD]], ptr [[NEXT_GEP6]], align 4, !alias.scope [[META19:![0-9]+]], !noalias [[META16]]
+; STRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; STRIDED-NEXT: [[TMP17:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; STRIDED-NEXT: br i1 [[TMP17]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP21:![0-9]+]]
+; STRIDED: middle.block:
+; STRIDED-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[UMAX4]], [[N_VEC]]
+; STRIDED-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
+; STRIDED: scalar.ph:
+; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[TMP12]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ], [ 0, [[VECTOR_MEMCHECK]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL7:%.*]] = phi ptr [ [[TMP14]], [[MIDDLE_BLOCK]] ], [ [[PIN]], [[ENTRY]] ], [ [[PIN]], [[VECTOR_MEMCHECK]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL8:%.*]] = phi ptr [ [[TMP16]], [[MIDDLE_BLOCK]] ], [ [[POUT]], [[ENTRY]] ], [ [[POUT]], [[VECTOR_MEMCHECK]] ]
+; STRIDED-NEXT: br label [[LOOP:%.*]]
+; STRIDED: loop:
+; STRIDED-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
+; STRIDED-NEXT: [[IN_PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL7]], [[SCALAR_PH]] ], [ [[IN_NEXT:%.*]], [[LOOP]] ]
+; STRIDED-NEXT: [[OUT_PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL8]], [[SCALAR_PH]] ], [ [[OUT_NEXT:%.*]], [[LOOP]] ]
+; STRIDED-NEXT: [[VAL:%.*]] = load float, ptr [[IN_PTR]], align 4
+; STRIDED-NEXT: store float [[VAL]], ptr [[OUT_PTR]], align 4
+; STRIDED-NEXT: [[IN_NEXT]] = getelementptr inbounds float, ptr [[IN_PTR]], i32 1
+; STRIDED-NEXT: [[OUT_NEXT]] = getelementptr inbounds float, ptr [[OUT_PTR]], i32 [[STRIDE]]
+; STRIDED-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
+; STRIDED-NEXT: [[CMP:%.*]] = icmp ult i32 [[IV_NEXT]], [[NCOLS]]
+; STRIDED-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT]], !llvm.loop [[LOOP22:![0-9]+]]
+; STRIDED: exit:
+; STRIDED-NEXT: ret void
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
+ %in.ptr = phi ptr [ %pIn, %entry ], [ %in.next, %loop ]
+ %out.ptr = phi ptr [ %pOut, %entry ], [ %out.next, %loop ]
+ %val = load float, ptr %in.ptr, align 4
+ store float %val, ptr %out.ptr, align 4
+ %in.next = getelementptr inbounds float, ptr %in.ptr, i32 1
+ %out.next = getelementptr inbounds float, ptr %out.ptr, i32 %stride
+ %iv.next = add nuw i32 %iv, 1
+ %cmp = icmp ult i32 %iv.next, %nCols
+ br i1 %cmp, label %loop, label %exit
+
+exit:
+ ret void
+}
diff --git a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
index e40e8bb10a3f8..2749a819614c7 100644
--- a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
+++ b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
@@ -20,28 +20,15 @@ define void @step_direction_unknown(i32 %arg, ptr %dst) {
; CHECK-NEXT: [[TMP6:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
; CHECK-NEXT: br i1 [[TMP6]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; CHECK: [[VECTOR_PH]]:
-; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[ADD]], i64 0
-; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <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:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[TMP8:%.*]] = mul <4 x i32> [[BROADCAST_SPLAT]], [[VEC_IND]]
-; CHECK-NEXT: [[TMP9:%.*]] = zext <4 x i32> [[TMP8]] to <4 x i64>
-; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i64> [[TMP9]], i32 0
-; CHECK-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[TMP9]], i32 1
-; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[TMP9]], i32 2
-; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[TMP9]], i32 3
-; CHECK-NEXT: [[TMP14:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP10]]
-; CHECK-NEXT: [[TMP15:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP11]]
-; CHECK-NEXT: [[TMP16:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP12]]
+; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[INDEX]] to i32
+; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[ADD]], [[TMP7]]
+; CHECK-NEXT: [[TMP13:%.*]] = zext i32 [[TMP8]] to i64
; CHECK-NEXT: [[TMP17:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP13]]
-; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP14]], align 8
-; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP15]], align 8
-; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP16]], align 8
-; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP17]], align 8
+; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP17]], align 8
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
-; CHECK-NEXT: [[VEC_IND_NEXT]] = add <4 x i32> [[VEC_IND]], splat (i32 4)
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
; CHECK-NEXT: br i1 [[TMP18]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: [[MIDDLE_BLOCK]]:
>From 38885f3d5ebe342ccba247532594aff6f9ab3868 Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Wed, 21 Jan 2026 15:39:51 +0000
Subject: [PATCH 06/10] Add const runtime strided pointer helpers
Change-Id: I554dba37cb16436851427d47298733784d08ff6c
---
.../llvm/Analysis/LoopAccessAnalysis.h | 14 +++-
.../Vectorize/LoopVectorizationLegality.h | 10 +++
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 65 ++++++++++++++-----
.../Vectorize/LoopVectorizationLegality.cpp | 23 +++++--
4 files changed, 90 insertions(+), 22 deletions(-)
diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
index 902f76496eede..d76790b72e2f8 100644
--- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
+++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
@@ -896,8 +896,18 @@ getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap =
DenseMap<Value *, const SCEV *>(),
- bool Assume = false, bool ShouldCheckWrap = true,
- bool AllowStridedPtrs = false);
+ bool Assume = false, bool ShouldCheckWrap = true);
+
+/// This method will check if the ptr is making use of a runtime constant to
+/// calculate the stride i.e. A[stride * multiplier] with multiplier defined at
+/// runtime. In the case that the stride is of the form stride * multiplier,
+/// this method will return a pair of the base stride and the multiplier SCEV.
+LLVM_ABI std::optional<int64_t>
+getPtrConstRuntimeStride(PredicatedScalarEvolution &PSE, Type *AccessTy,
+ Value *Ptr, const Loop *Lp, const DominatorTree &DT,
+ const DenseMap<Value *, const SCEV *> &StridesMap =
+ DenseMap<Value *, const SCEV *>(),
+ bool Assume = false);
/// Returns the distance between the pointers \p PtrA and \p PtrB iff they are
/// compatible and it is possible to calculate the distance between them. This
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index f82fc588639dd..9136782d97acb 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -378,6 +378,16 @@ class LoopVectorizationLegality {
/// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
+
+ // Check is this pointer is const runtime strided when vectorizing.
+ // This occurs for pointers that have an access pattern such as
+ //
+ // A[offset + stride * runtime_constant]
+ //
+ // This check allows us to vectorize these into a wide load/store when
+ // `lv-strided-pointer-ivs=true` is true.
+ bool isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
+
/// Returns true if \p V is invariant across all loop iterations according to
/// SCEV.
bool isInvariant(Value *V) const;
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index a8e2ee647269a..1be961bd928ed 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -964,8 +964,8 @@ class AccessAnalysis {
} // end anonymous namespace
-/// Try to compute a constant stride for \p AR. Used by getPtrStride and
-/// isNoWrap.
+/// Try to compute a constant stride for \p AR. Used by getPtrStride,
+/// getPtrConstRuntimeStride and isNoWrap.
static std::optional<int64_t>
getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
Value *Ptr, PredicatedScalarEvolution &PSE,
@@ -988,21 +988,18 @@ getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
return std::nullopt;
}
- // Calculate the pointer stride and check if it is constant.
- // If we allow strided pointers we also check the add recurrence
- // expression is making use of loop-invariant
- // runtime constant for example
+ // Calculate the pointer stride and check if it is constant. If we allow
+ // strided pointers we also check the add recurrence expression is making use
+ // of loop-invariant runtime constant for example
//
// Step = { stride * runtime_constant }
//
- // we should treat this as a constant add recurrence.
- // To achieve this we check the arguments of
- // the step are loop invariant and compute the step
- // as equivalent to just stride.
+ // we should treat this as a constant add recurrence. To achieve this we check
+ // the arguments of the step are loop invariant and compute the step as
+ // equivalent to just stride.
//
- // We only check this for mul expressions as these
- // are most common and expected for strided
- // pointer recurrence expressions.
+ // We only check this for mul expressions as these are most common and
+ // expected for strided pointer recurrence expressions.
const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
const APInt *APStepVal;
auto IsInvariantConstAddRecStep = [&](const SCEV *Step) -> bool {
@@ -1013,10 +1010,14 @@ getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
return true;
if (AllowStridedPtrs) {
+ if (PSE.getSE()->isKnownNegative(Step))
+ return false;
+
for (const auto *Op : Step->operands()) {
if (!PSE.getSE()->isLoopInvariant(Op, Lp))
return false;
}
+
return match(Step,
m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)));
}
@@ -1657,7 +1658,7 @@ std::optional<int64_t>
llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap,
- bool Assume, bool ShouldCheckWrap, bool AllowStridedPtrs) {
+ bool Assume, bool ShouldCheckWrap) {
const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
return 0;
@@ -1675,7 +1676,7 @@ llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
}
std::optional<int64_t> Stride =
- getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE, AllowStridedPtrs);
+ getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE);
if (!ShouldCheckWrap || !Stride)
return Stride;
@@ -1688,6 +1689,40 @@ llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
return std::nullopt;
}
+/// Check whether the access through \p Ptr has a constant runtime stride
+std::optional<int64_t> llvm::getPtrConstRuntimeStride(
+ PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp,
+ const DominatorTree &DT, const DenseMap<Value *, const SCEV *> &StridesMap,
+ bool Assume) {
+ const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
+ if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
+ return 0;
+
+ assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
+
+ const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
+ if (Assume && !AR)
+ AR = PSE.getAsAddRec(Ptr);
+
+ if (!AR) {
+ LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer " << *Ptr
+ << " SCEV: " << *PtrScev << "\n");
+ return std::nullopt;
+ }
+
+ std::optional<int64_t> Stride = getStrideFromAddRec(
+ AR, Lp, AccessTy, Ptr, PSE, /* AllowStridedPtrs =*/true);
+
+ if (!Stride) {
+ LLVM_DEBUG(
+ dbgs() << "LAA: Bad stride - Pointer isn't runtime const strided "
+ << *Ptr << " SCEV: " << *AR << "\n");
+ return std::nullopt;
+ }
+
+ return Stride;
+}
+
std::optional<int64_t> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
Type *ElemTyB, Value *PtrB,
const DataLayout &DL,
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 1690ecc3674f4..fdda4d313d364 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -481,15 +481,29 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
- int Stride =
- getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
- AllowRuntimeSCEVChecks, false, AllowStridedPointerIVs)
- .value_or(0);
+ int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
+ AllowRuntimeSCEVChecks, false)
+ .value_or(0);
if (Stride == 1 || Stride == -1)
return Stride;
return 0;
}
+bool LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
+ Value *Ptr) const {
+ if (!AllowStridedPointerIVs)
+ return false;
+
+ const auto &Strides =
+ LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
+
+ int Stride = getPtrConstRuntimeStride(PSE, AccessTy, Ptr, TheLoop, *DT,
+ Strides, AllowRuntimeSCEVChecks)
+ .value_or(0);
+
+ return Stride;
+}
+
bool LoopVectorizationLegality::isInvariant(Value *V) const {
return LAI->isInvariant(V);
}
@@ -1712,7 +1726,6 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
"RecurrencesInEarlyExitLoop", ORE, TheLoop);
return false;
}
-
SmallVector<BasicBlock *, 8> ExitingBlocks;
TheLoop->getExitingBlocks(ExitingBlocks);
>From c462b9f4b5b982953b73e553e2b1643066678669 Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Thu, 22 Jan 2026 15:12:11 +0000
Subject: [PATCH 07/10] Plugging use of runtime const stride helpers into
vectorizer
- makes use of new LAA methods
- adds changes to LV cost model decision code to allow for widening
or gather/scatter.
Change-Id: I108ad4a995d54f7f40b84ba8abdc362cc6681125
---
.../Vectorize/LoopVectorizationLegality.h | 6 +-
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 18 +-
.../Vectorize/LoopVectorizationLegality.cpp | 6 +-
.../Transforms/Vectorize/LoopVectorize.cpp | 59 ++++-
.../LoopVectorize/RISCV/strided-accesses.ll | 221 +++++++++++-------
.../LoopVectorize/pointer-induction.ll | 19 +-
.../LoopVectorize/scev-predicate-reasoning.ll | 11 +-
7 files changed, 238 insertions(+), 102 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 9136782d97acb..7116ad017c72a 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -379,14 +379,14 @@ class LoopVectorizationLegality {
int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
- // Check is this pointer is const runtime strided when vectorizing.
+ // Check if this pointer is const runtime strided when vectorizing.
// This occurs for pointers that have an access pattern such as
//
// A[offset + stride * runtime_constant]
//
// This check allows us to vectorize these into a wide load/store when
- // `lv-strided-pointer-ivs=true` is true.
- bool isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
+ // `lv-strided-pointer-ivs=true`
+ int isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
/// Returns true if \p V is invariant across all loop iterations according to
/// SCEV.
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 1be961bd928ed..afabb8b68d93f 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1693,7 +1693,11 @@ llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
std::optional<int64_t> llvm::getPtrConstRuntimeStride(
PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp,
const DominatorTree &DT, const DenseMap<Value *, const SCEV *> &StridesMap,
- bool Assume) {
+ bool AllowRuntimeSCEVChecks) {
+ // Require runtime checks to ensure correctness of runtime stride
+ if (!AllowRuntimeSCEVChecks)
+ return std::nullopt;
+
const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
return 0;
@@ -1701,7 +1705,7 @@ std::optional<int64_t> llvm::getPtrConstRuntimeStride(
assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
- if (Assume && !AR)
+ if (AllowRuntimeSCEVChecks && !AR)
AR = PSE.getAsAddRec(Ptr);
if (!AR) {
@@ -1720,6 +1724,16 @@ std::optional<int64_t> llvm::getPtrConstRuntimeStride(
return std::nullopt;
}
+ // Add runtime SCEV check for multiplier
+ const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
+ const APInt *APStepVal;
+ const SCEV *Multiplier;
+ if (match(Step, m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)))) {
+ const SCEV *One = PSE.getSE()->getOne(Multiplier->getType());
+ PSE.addPredicate(
+ *PSE.getSE()->getComparePredicate(CmpInst::ICMP_EQ, Multiplier, One));
+ }
+
return Stride;
}
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index fdda4d313d364..7cefa973933a4 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -489,10 +489,10 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
return 0;
}
-bool LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
- Value *Ptr) const {
+int LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
+ Value *Ptr) const {
if (!AllowStridedPointerIVs)
- return false;
+ return 0;
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index abac45b265d10..411c294729b52 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1261,6 +1261,10 @@ class LoopVectorizationCostModel {
/// access that can be widened.
bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF);
+ // Returns true if \p is a memory instruction with strided memory access that
+ // can be widened.
+ bool stridedMemoryAccessCanBeWidened(Instruction *I, ElementCount VF);
+
/// Returns true if \p I is a memory instruction in an interleaved-group
/// of memory accesses that can be vectorized with wide vector loads/stores
/// and shuffles.
@@ -1549,7 +1553,8 @@ class LoopVectorizationCostModel {
/// The cost computation for widening instruction \p I with consecutive
/// memory access.
- InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF);
+ InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF,
+ bool IsRuntimeConstStrided = false);
/// The cost calculation for Load/Store instruction \p I with uniform pointer -
/// Load: scalar load + broadcast.
@@ -3097,6 +3102,32 @@ bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
return true;
}
+bool LoopVectorizationCostModel::stridedMemoryAccessCanBeWidened(
+ Instruction *I, ElementCount VF) {
+ // Get and ensure we have a valid memory instruction.
+ assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
+
+ auto *Ptr = getLoadStorePointerOperand(I);
+ auto *ScalarTy = getLoadStoreType(I);
+
+ // In order to be widened, the pointer should be consecutive, first of all.
+ if (!Legal->isConstRuntimeStridedPtr(ScalarTy, Ptr))
+ return false;
+
+ // If the instruction is a store located in a predicated block, it will be
+ // scalarized.
+ if (isScalarWithPredication(I, VF))
+ return false;
+
+ // If the instruction's allocated size doesn't equal it's type size, it
+ // requires padding and will be scalarized.
+ auto &DL = I->getDataLayout();
+ if (hasIrregularType(ScalarTy, DL))
+ return false;
+
+ return true;
+}
+
void LoopVectorizationCostModel::collectLoopUniforms(ElementCount VF) {
// We should not collect Uniforms more than once per VF. Right now,
// this function is called from collectUniformsAndScalars(), which
@@ -5306,15 +5337,18 @@ LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
return Cost;
}
-InstructionCost
-LoopVectorizationCostModel::getConsecutiveMemOpCost(Instruction *I,
- ElementCount VF) {
+InstructionCost LoopVectorizationCostModel::getConsecutiveMemOpCost(
+ Instruction *I, ElementCount VF, bool IsRuntimeConstStrided) {
Type *ValTy = getLoadStoreType(I);
auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
Value *Ptr = getLoadStorePointerOperand(I);
unsigned AS = getLoadStoreAddressSpace(I);
int ConsecutiveStride = Legal->isConsecutivePtr(ValTy, Ptr);
+ ConsecutiveStride = (!ConsecutiveStride && IsRuntimeConstStrided)
+ ? Legal->isConstRuntimeStridedPtr(ValTy, Ptr)
+ : ConsecutiveStride;
+
assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
"Stride should be 1 or -1 for consecutive memory access");
const Align Alignment = getLoadStoreAlignment(I);
@@ -5767,6 +5801,23 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
setWideningDecision(&I, VF, Decision, Cost);
continue;
+ } else if (stridedMemoryAccessCanBeWidened(&I, VF)) {
+ int BaseStride = Legal->isConstRuntimeStridedPtr(
+ getLoadStoreType(&I), getLoadStorePointerOperand(&I));
+ assert((BaseStride >= 1) && "Expected base stride to be >= 1");
+
+ // For base strides greater than 1 we should default to
+ // gather/scatters
+ InstructionCost Cost;
+ if (BaseStride == 1) {
+ Cost = getConsecutiveMemOpCost(&I, VF, true);
+ setWideningDecision(&I, VF, CM_Widen, Cost);
+ } else {
+ Cost = isLegalGatherOrScatter(&I, VF) ? getGatherScatterCost(&I, VF)
+ : InstructionCost::getInvalid();
+ setWideningDecision(&I, VF, CM_GatherScatter, Cost);
+ }
+ continue;
}
// Choose between Interleaving, Gather/Scatter or Scalarization.
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 2bef1e50317de..9d641cc08a18c 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -237,68 +237,125 @@ define void @single_constant_stride_ptr_iv(ptr %p) {
; CHECK: exit:
; CHECK-NEXT: ret void
;
-; CHECK-UF2-LABEL: @single_constant_stride_ptr_iv(
-; CHECK-UF2-NEXT: entry:
-; CHECK-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
-; CHECK-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
-; CHECK-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
-; CHECK-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
-; CHECK-UF2: vector.ph:
-; CHECK-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
-; CHECK-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
-; CHECK-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
-; CHECK-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; CHECK-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
-; CHECK-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
-; CHECK-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
-; CHECK-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
-; CHECK-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
-; CHECK-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
-; CHECK-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
-; CHECK-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
-; CHECK-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
-; CHECK-UF2: vector.body:
-; CHECK-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; CHECK-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
-; CHECK-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
-; CHECK-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
-; CHECK-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
-; CHECK-UF2-NEXT: [[TMP12:%.*]] = extractelement <vscale x 4 x ptr> [[VECTOR_GEP]], i32 0
-; CHECK-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
-; CHECK-UF2-NEXT: [[WIDE_VEC:%.*]] = load <vscale x 8 x i32>, ptr [[TMP12]], align 4
-; CHECK-UF2-NEXT: [[STRIDED_VEC:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC]])
-; CHECK-UF2-NEXT: [[TMP13:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC]], 0
-; CHECK-UF2-NEXT: [[TMP14:%.*]] = extractelement <vscale x 4 x ptr> [[STEP_ADD]], i32 0
-; CHECK-UF2-NEXT: [[WIDE_VEC1:%.*]] = load <vscale x 8 x i32>, ptr [[TMP14]], align 4
-; CHECK-UF2-NEXT: [[STRIDED_VEC2:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC1]])
-; CHECK-UF2-NEXT: [[TMP15:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC2]], 0
-; CHECK-UF2-NEXT: [[TMP16:%.*]] = add <vscale x 4 x i32> [[TMP13]], splat (i32 1)
-; CHECK-UF2-NEXT: [[TMP17:%.*]] = add <vscale x 4 x i32> [[TMP15]], splat (i32 1)
-; CHECK-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP16]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
-; CHECK-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP17]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
-; CHECK-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
-; CHECK-UF2-NEXT: [[TMP18:%.*]] = shl i64 [[TMP4]], 3
-; CHECK-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP18]]
-; CHECK-UF2-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-UF2-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
-; CHECK-UF2: middle.block:
-; CHECK-UF2-NEXT: br label [[SCALAR_PH]]
-; CHECK-UF2: scalar.ph:
-; CHECK-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
-; CHECK-UF2-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
-; CHECK-UF2-NEXT: br label [[LOOP:%.*]]
-; CHECK-UF2: loop:
-; CHECK-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
-; CHECK-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
-; CHECK-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
-; CHECK-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
-; CHECK-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
-; CHECK-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
-; CHECK-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
-; CHECK-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
-; CHECK-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
-; CHECK-UF2: exit:
-; CHECK-UF2-NEXT: ret void
+; NOSTRIDED-UF2-LABEL: @single_constant_stride_ptr_iv(
+; NOSTRIDED-UF2-NEXT: entry:
+; NOSTRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; NOSTRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
+; NOSTRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
+; NOSTRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
+; NOSTRIDED-UF2: vector.ph:
+; NOSTRIDED-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; NOSTRIDED-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
+; NOSTRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
+; NOSTRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; NOSTRIDED-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
+; NOSTRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
+; NOSTRIDED-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
+; NOSTRIDED-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
+; NOSTRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
+; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
+; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
+; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
+; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
+; NOSTRIDED-UF2: vector.body:
+; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
+; NOSTRIDED-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
+; NOSTRIDED-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
+; NOSTRIDED-UF2-NEXT: [[TMP12:%.*]] = extractelement <vscale x 4 x ptr> [[VECTOR_GEP]], i32 0
+; NOSTRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
+; NOSTRIDED-UF2-NEXT: [[WIDE_VEC:%.*]] = load <vscale x 8 x i32>, ptr [[TMP12]], align 4
+; NOSTRIDED-UF2-NEXT: [[STRIDED_VEC:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC]])
+; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC]], 0
+; NOSTRIDED-UF2-NEXT: [[TMP14:%.*]] = extractelement <vscale x 4 x ptr> [[STEP_ADD]], i32 0
+; NOSTRIDED-UF2-NEXT: [[WIDE_VEC1:%.*]] = load <vscale x 8 x i32>, ptr [[TMP14]], align 4
+; NOSTRIDED-UF2-NEXT: [[STRIDED_VEC2:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC1]])
+; NOSTRIDED-UF2-NEXT: [[TMP15:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC2]], 0
+; NOSTRIDED-UF2-NEXT: [[TMP16:%.*]] = add <vscale x 4 x i32> [[TMP13]], splat (i32 1)
+; NOSTRIDED-UF2-NEXT: [[TMP17:%.*]] = add <vscale x 4 x i32> [[TMP15]], splat (i32 1)
+; NOSTRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP16]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
+; NOSTRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP17]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
+; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
+; NOSTRIDED-UF2-NEXT: [[TMP18:%.*]] = shl i64 [[TMP4]], 3
+; NOSTRIDED-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP18]]
+; NOSTRIDED-UF2-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; NOSTRIDED-UF2-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
+; NOSTRIDED-UF2: middle.block:
+; NOSTRIDED-UF2-NEXT: br label [[SCALAR_PH]]
+; NOSTRIDED-UF2: scalar.ph:
+; NOSTRIDED-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
+; NOSTRIDED-UF2-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
+; NOSTRIDED-UF2-NEXT: br label [[LOOP:%.*]]
+; NOSTRIDED-UF2: loop:
+; NOSTRIDED-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
+; NOSTRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
+; NOSTRIDED-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
+; NOSTRIDED-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
+; NOSTRIDED-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
+; NOSTRIDED-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
+; NOSTRIDED-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
+; NOSTRIDED-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
+; NOSTRIDED-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
+; NOSTRIDED-UF2: exit:
+; NOSTRIDED-UF2-NEXT: ret void
+;
+; STRIDED-UF2-LABEL: @single_constant_stride_ptr_iv(
+; STRIDED-UF2-NEXT: entry:
+; STRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; STRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
+; STRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
+; STRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
+; STRIDED-UF2: vector.ph:
+; STRIDED-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; STRIDED-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; STRIDED-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
+; STRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
+; STRIDED-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
+; STRIDED-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
+; STRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
+; STRIDED-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
+; STRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
+; STRIDED-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
+; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
+; STRIDED-UF2: vector.body:
+; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
+; STRIDED-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
+; STRIDED-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
+; STRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
+; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER1:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
+; STRIDED-UF2-NEXT: [[TMP12:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; STRIDED-UF2-NEXT: [[TMP13:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER1]], splat (i32 1)
+; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP12]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
+; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP13]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
+; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
+; STRIDED-UF2-NEXT: [[TMP14:%.*]] = shl i64 [[TMP4]], 3
+; STRIDED-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP14]]
+; STRIDED-UF2-NEXT: [[TMP15:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; STRIDED-UF2-NEXT: br i1 [[TMP15]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
+; STRIDED-UF2: middle.block:
+; STRIDED-UF2-NEXT: br label [[SCALAR_PH]]
+; STRIDED-UF2: scalar.ph:
+; STRIDED-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
+; STRIDED-UF2-NEXT: [[BC_RESUME_VAL2:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
+; STRIDED-UF2-NEXT: br label [[LOOP:%.*]]
+; STRIDED-UF2: loop:
+; STRIDED-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
+; STRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL2]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
+; STRIDED-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
+; STRIDED-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
+; STRIDED-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
+; STRIDED-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
+; STRIDED-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
+; STRIDED-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
+; STRIDED-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
+; STRIDED-UF2: exit:
+; STRIDED-UF2-NEXT: ret void
;
entry:
br label %loop
@@ -743,8 +800,10 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[TMP38:%.*]] = icmp ugt ptr [[TMP36]], [[P]]
; STRIDED-NEXT: [[TMP39:%.*]] = select i1 [[TMP34]], i1 [[TMP38]], i1 [[TMP37]]
; STRIDED-NEXT: [[TMP40:%.*]] = or i1 [[TMP39]], [[MUL_OVERFLOW3]]
+; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
; STRIDED-NEXT: [[TMP23:%.*]] = or i1 [[TMP13]], [[TMP40]]
-; STRIDED-NEXT: br i1 [[TMP23]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK1:%.*]]
+; STRIDED-NEXT: [[TMP21:%.*]] = or i1 [[TMP23]], [[IDENT_CHECK]]
+; STRIDED-NEXT: br i1 [[TMP21]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK1:%.*]]
; STRIDED: vector.memcheck:
; STRIDED-NEXT: [[TMP3:%.*]] = mul i64 [[STRIDE]], 4092
; STRIDED-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[P2]], i64 [[TMP3]]
@@ -772,9 +831,9 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[TMP42:%.*]] = mul nuw nsw i64 [[EVL_BASED_IV]], [[STRIDE]]
; STRIDED-NEXT: [[TMP45:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP42]]
; STRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP45]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META5:![0-9]+]]
-; STRIDED-NEXT: [[TMP20:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; STRIDED-NEXT: [[TMP47:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
; STRIDED-NEXT: [[TMP46:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP42]]
-; STRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP20]], ptr align 4 [[TMP46]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
+; STRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP47]], ptr align 4 [[TMP46]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
; STRIDED-NEXT: [[TMP44:%.*]] = zext i32 [[TMP43]] to i64
; STRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP44]], [[EVL_BASED_IV]]
; STRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP44]]
@@ -803,7 +862,7 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: entry:
; STRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
; STRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
-; STRIDED-UF2-NEXT: [[UMAX9:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP1]], i64 79)
+; STRIDED-UF2-NEXT: [[UMAX9:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP1]], i64 98)
; STRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 1024, [[UMAX9]]
; STRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_SCEVCHECK:%.*]]
; STRIDED-UF2: vector.scevcheck:
@@ -833,8 +892,10 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: [[TMP19:%.*]] = icmp ugt ptr [[TMP17]], [[P]]
; STRIDED-UF2-NEXT: [[TMP20:%.*]] = select i1 [[TMP13]], i1 [[TMP19]], i1 [[TMP18]]
; STRIDED-UF2-NEXT: [[TMP21:%.*]] = or i1 [[TMP20]], [[MUL_OVERFLOW3]]
+; STRIDED-UF2-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
; STRIDED-UF2-NEXT: [[TMP22:%.*]] = or i1 [[TMP12]], [[TMP21]]
-; STRIDED-UF2-NEXT: br i1 [[TMP22]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
+; STRIDED-UF2-NEXT: [[TMP32:%.*]] = or i1 [[TMP22]], [[IDENT_CHECK]]
+; STRIDED-UF2-NEXT: br i1 [[TMP32]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
; STRIDED-UF2: vector.memcheck:
; STRIDED-UF2-NEXT: [[TMP23:%.*]] = mul i64 [[STRIDE]], 4092
; STRIDED-UF2-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[P2]], i64 [[TMP23]]
@@ -862,20 +923,20 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED-UF2: vector.body:
; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[TMP31:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
-; STRIDED-UF2-NEXT: [[TMP32:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP31]]
-; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[TMP32]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = load <vscale x 4 x i32>, ptr [[TMP32]], align 4, !alias.scope [[META8:![0-9]+]]
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER12:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8]]
-; STRIDED-UF2-NEXT: [[TMP37:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP38:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER12]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP36:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP31]]
-; STRIDED-UF2-NEXT: [[TMP39:%.*]] = getelementptr i32, ptr [[TMP36]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP37]], ptr [[TMP36]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP38]], ptr [[TMP39]], align 4, !alias.scope [[META11]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: [[TMP34:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
+; STRIDED-UF2-NEXT: [[TMP35:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP34]]
+; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[TMP35]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP35]], align 4, !alias.scope [[META8:![0-9]+]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD10:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8]]
+; STRIDED-UF2-NEXT: [[TMP36:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
+; STRIDED-UF2-NEXT: [[TMP39:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD10]], splat (i32 1)
+; STRIDED-UF2-NEXT: [[TMP31:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP34]]
+; STRIDED-UF2-NEXT: [[TMP37:%.*]] = getelementptr i32, ptr [[TMP31]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP36]], ptr [[TMP31]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP39]], ptr [[TMP37]], align 4, !alias.scope [[META11]], !noalias [[META8]]
; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP30]]
-; STRIDED-UF2-NEXT: [[TMP41:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; STRIDED-UF2-NEXT: br i1 [[TMP41]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP13:![0-9]+]]
+; STRIDED-UF2-NEXT: [[TMP38:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; STRIDED-UF2-NEXT: br i1 [[TMP38]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP13:![0-9]+]]
; STRIDED-UF2: middle.block:
; STRIDED-UF2-NEXT: [[CMP_N:%.*]] = icmp eq i64 1024, [[N_VEC]]
; STRIDED-UF2-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
diff --git a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
index 44514cf758450..ddcb5ab622b7c 100644
--- a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
+++ b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
@@ -527,6 +527,9 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED-NEXT: [[TMP1:%.*]] = shl i64 [[STRIDE:%.*]], 3
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 4
; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
+; STRIDED: vector.scevcheck:
+; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
+; STRIDED-NEXT: br i1 [[IDENT_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH1:%.*]]
; STRIDED: vector.ph:
; STRIDED-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], 4
; STRIDED-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
@@ -534,7 +537,7 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED-NEXT: [[TMP3:%.*]] = getelementptr i8, ptr null, i64 [[TMP2]]
; STRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED: vector.body:
-; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], [[TMP1]]
; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[OFFSET_IDX]]
; STRIDED-NEXT: [[TMP21:%.*]] = getelementptr i64, ptr [[A:%.*]], i64 [[INDEX]]
@@ -550,8 +553,8 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED-NEXT: [[IND_ESCAPE:%.*]] = getelementptr i8, ptr [[TMP3]], i64 [[TMP19]]
; STRIDED-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
; STRIDED: scalar.ph:
-; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
-; STRIDED-NEXT: [[BC_RESUME_VAL4:%.*]] = phi ptr [ [[TMP3]], [[MIDDLE_BLOCK]] ], [ null, [[ENTRY]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ], [ 0, [[VECTOR_PH]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL4:%.*]] = phi ptr [ [[TMP3]], [[MIDDLE_BLOCK]] ], [ null, [[ENTRY]] ], [ null, [[VECTOR_PH]] ]
; STRIDED-NEXT: br label [[FOR_BODY:%.*]]
; STRIDED: for.body:
; STRIDED-NEXT: [[INDVAR:%.*]] = phi i64 [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
@@ -710,6 +713,10 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
; STRIDED-NEXT: [[TMP2:%.*]] = shl nsw i64 [[TMP1]], 2
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[UMAX4]], 4
; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
+; STRIDED: vector.scevcheck:
+; STRIDED-NEXT: [[TMP18:%.*]] = sext i32 [[STRIDE]] to i64
+; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP18]], 1
+; STRIDED-NEXT: br i1 [[IDENT_CHECK]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK1:%.*]]
; STRIDED: vector.memcheck:
; STRIDED-NEXT: [[TMP3:%.*]] = sext i32 [[STRIDE]] to i64
; STRIDED-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[NCOLS]], i32 1)
@@ -754,9 +761,9 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
; STRIDED-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[UMAX4]], [[N_VEC]]
; STRIDED-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
; STRIDED: scalar.ph:
-; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[TMP12]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ], [ 0, [[VECTOR_MEMCHECK]] ]
-; STRIDED-NEXT: [[BC_RESUME_VAL7:%.*]] = phi ptr [ [[TMP14]], [[MIDDLE_BLOCK]] ], [ [[PIN]], [[ENTRY]] ], [ [[PIN]], [[VECTOR_MEMCHECK]] ]
-; STRIDED-NEXT: [[BC_RESUME_VAL8:%.*]] = phi ptr [ [[TMP16]], [[MIDDLE_BLOCK]] ], [ [[POUT]], [[ENTRY]] ], [ [[POUT]], [[VECTOR_MEMCHECK]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[TMP12]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ], [ 0, [[VECTOR_MEMCHECK]] ], [ 0, [[VECTOR_MEMCHECK1]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL7:%.*]] = phi ptr [ [[TMP14]], [[MIDDLE_BLOCK]] ], [ [[PIN]], [[ENTRY]] ], [ [[PIN]], [[VECTOR_MEMCHECK]] ], [ [[PIN]], [[VECTOR_MEMCHECK1]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL8:%.*]] = phi ptr [ [[TMP16]], [[MIDDLE_BLOCK]] ], [ [[POUT]], [[ENTRY]] ], [ [[POUT]], [[VECTOR_MEMCHECK]] ], [ [[POUT]], [[VECTOR_MEMCHECK1]] ]
; STRIDED-NEXT: br label [[LOOP:%.*]]
; STRIDED: loop:
; STRIDED-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
diff --git a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
index 2749a819614c7..a3648fceb7d2c 100644
--- a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
+++ b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
@@ -18,16 +18,19 @@ define void @step_direction_unknown(i32 %arg, ptr %dst) {
; CHECK-NEXT: [[TMP4:%.*]] = icmp ugt i32 [[TMP3]], 0
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP1]], i1 [[TMP4]], i1 false
; CHECK-NEXT: [[TMP6:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
-; CHECK-NEXT: br i1 [[TMP6]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK-NEXT: [[TMP9:%.*]] = sext i32 [[ADD]] to i64
+; CHECK-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP9]], 1
+; CHECK-NEXT: [[TMP10:%.*]] = or i1 [[TMP6]], [[IDENT_CHECK]]
+; CHECK-NEXT: br i1 [[TMP10]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; CHECK: [[VECTOR_PH]]:
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[INDEX]] to i32
; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[ADD]], [[TMP7]]
-; CHECK-NEXT: [[TMP13:%.*]] = zext i32 [[TMP8]] to i64
-; CHECK-NEXT: [[TMP17:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP13]]
-; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP17]], align 8
+; CHECK-NEXT: [[TMP12:%.*]] = zext i32 [[TMP8]] to i64
+; CHECK-NEXT: [[TMP16:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP12]]
+; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP16]], align 8
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
; CHECK-NEXT: br i1 [[TMP18]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
>From e96564bb861fa179e27004143190e9fff786ad6a Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Fri, 23 Jan 2026 14:37:12 +0000
Subject: [PATCH 08/10] Review comments
- refactor runtime strided pointer checks to fit
into the setCostBasedWideningDecision->isConsecutivePtr
->getPtrStride call pipeline.
- refactoring comments, updating tests.
Change-Id: I5f16032c8841edc861c4779999efae086ff91469
---
.../llvm/Analysis/LoopAccessAnalysis.h | 20 +-
.../Vectorize/LoopVectorizationLegality.h | 17 +-
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 90 ++++-----
.../Vectorize/LoopVectorizationLegality.cpp | 22 +--
.../Transforms/Vectorize/LoopVectorize.cpp | 55 +-----
.../LoopVectorize/RISCV/strided-accesses.ll | 181 ++++++------------
6 files changed, 117 insertions(+), 268 deletions(-)
diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
index d76790b72e2f8..5365cdc093592 100644
--- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
+++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
@@ -888,6 +888,12 @@ replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
/// The \p Assume parameter indicates if we are allowed to make additional
/// run-time assumptions.
///
+/// If AllowRuntimeStridedIVs is true, this method will check if the ptr is
+/// making use of a runtime constant to calculate the stride i.e.
+/// A[stride * multiplier] with multiplier defined at runtime. In the case that
+/// the stride is of the form stride * multiplier, this method will return the
+/// base stride and also add a runtime SCEV for stride.
+///
/// Note that the analysis results are defined if-and-only-if the original
/// memory access was defined. If that access was dead, or UB, then the
/// result of this function is undefined.
@@ -896,18 +902,8 @@ getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap =
DenseMap<Value *, const SCEV *>(),
- bool Assume = false, bool ShouldCheckWrap = true);
-
-/// This method will check if the ptr is making use of a runtime constant to
-/// calculate the stride i.e. A[stride * multiplier] with multiplier defined at
-/// runtime. In the case that the stride is of the form stride * multiplier,
-/// this method will return a pair of the base stride and the multiplier SCEV.
-LLVM_ABI std::optional<int64_t>
-getPtrConstRuntimeStride(PredicatedScalarEvolution &PSE, Type *AccessTy,
- Value *Ptr, const Loop *Lp, const DominatorTree &DT,
- const DenseMap<Value *, const SCEV *> &StridesMap =
- DenseMap<Value *, const SCEV *>(),
- bool Assume = false);
+ bool Assume = false, bool ShouldCheckWrap = true,
+ bool AllowRuntimeStridedIVs = false);
/// Returns the distance between the pointers \p PtrA and \p PtrB iff they are
/// compatible and it is possible to calculate the distance between them. This
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 7116ad017c72a..a2d4f6bfc4ec6 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -374,19 +374,18 @@ class LoopVectorizationLegality {
/// 0 - Stride is unknown or non-consecutive.
/// 1 - Address is consecutive.
/// -1 - Address is consecutive, and decreasing.
- /// NOTE: This method must only be used before modifying the original scalar
- /// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
- int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
-
-
- // Check if this pointer is const runtime strided when vectorizing.
- // This occurs for pointers that have an access pattern such as
+ ///
+ /// When AllowStridedPointerIVs is true, check if this pointer is
+ /// const runtime strided when vectorizing. This occurs for pointers
+ /// that have an access pattern such as
//
// A[offset + stride * runtime_constant]
//
// This check allows us to vectorize these into a wide load/store when
- // `lv-strided-pointer-ivs=true`
- int isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
+ // `lv-strided-pointer-ivs=true. In this case the return value is 'stride'
+ /// NOTE: This method must only be used before modifying the original scalar
+ /// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
+ int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
/// Returns true if \p V is invariant across all loop iterations according to
/// SCEV.
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index afabb8b68d93f..82dc45d224d8a 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -969,7 +969,7 @@ class AccessAnalysis {
static std::optional<int64_t>
getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
Value *Ptr, PredicatedScalarEvolution &PSE,
- bool AllowStridedPtrs = false) {
+ bool AllowRuntimeStridedIVs = false) {
if (isa<ScalableVectorType>(AccessTy)) {
LLVM_DEBUG(dbgs() << "LAA: Bad stride - Scalable object: " << *AccessTy
<< "\n");
@@ -1009,7 +1009,7 @@ getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
if (match(Step, m_scev_APInt(APStepVal)))
return true;
- if (AllowStridedPtrs) {
+ if (AllowRuntimeStridedIVs) {
if (PSE.getSE()->isKnownNegative(Step))
return false;
@@ -1654,17 +1654,23 @@ void AccessAnalysis::processMemAccesses() {
}
/// Check whether the access through \p Ptr has a constant stride.
-std::optional<int64_t>
-llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
- const Loop *Lp, const DominatorTree &DT,
- const DenseMap<Value *, const SCEV *> &StridesMap,
- bool Assume, bool ShouldCheckWrap) {
+std::optional<int64_t> llvm::getPtrStride(
+ PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp,
+ const DominatorTree &DT, const DenseMap<Value *, const SCEV *> &StridesMap,
+ bool Assume, bool ShouldCheckWrap, bool AllowStridedPointerIVs) {
const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
return 0;
assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
+ // Require runtime checks to ensure correctness of runtime stride
+ if (AllowStridedPointerIVs && !Assume) {
+ LLVM_DEBUG(dbgs() << "Cannot check for runtime strided constants without \
+ allowing runtime SCEV checks");
+ return std::nullopt;
+ }
+
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
if (Assume && !AR)
AR = PSE.getAsAddRec(Ptr);
@@ -1676,65 +1682,33 @@ llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
}
std::optional<int64_t> Stride =
- getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE);
- if (!ShouldCheckWrap || !Stride)
- return Stride;
-
- if (isNoWrap(PSE, AR, Ptr, AccessTy, Lp, Assume, DT, Stride))
- return Stride;
-
- LLVM_DEBUG(
- dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
- << *Ptr << " SCEV: " << *AR << "\n");
- return std::nullopt;
-}
-
-/// Check whether the access through \p Ptr has a constant runtime stride
-std::optional<int64_t> llvm::getPtrConstRuntimeStride(
- PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp,
- const DominatorTree &DT, const DenseMap<Value *, const SCEV *> &StridesMap,
- bool AllowRuntimeSCEVChecks) {
- // Require runtime checks to ensure correctness of runtime stride
- if (!AllowRuntimeSCEVChecks)
- return std::nullopt;
-
- const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
- if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
- return 0;
+ getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE, AllowStridedPointerIVs);
- assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
-
- const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
- if (AllowRuntimeSCEVChecks && !AR)
- AR = PSE.getAsAddRec(Ptr);
+ if (AllowStridedPointerIVs && Stride) {
+ // Add runtime SCEV check for multiplier
+ const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
+ const APInt *APStepVal;
+ const SCEV *Multiplier;
+ if (match(Step,
+ m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)))) {
+ const SCEV *One = PSE.getSE()->getOne(Multiplier->getType());
+ PSE.addPredicate(
+ *PSE.getSE()->getComparePredicate(CmpInst::ICMP_EQ, Multiplier, One));
+ }
- if (!AR) {
- LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer " << *Ptr
- << " SCEV: " << *PtrScev << "\n");
- return std::nullopt;
- }
+ return Stride;
+ } else {
+ if (!ShouldCheckWrap || !Stride)
+ return Stride;
- std::optional<int64_t> Stride = getStrideFromAddRec(
- AR, Lp, AccessTy, Ptr, PSE, /* AllowStridedPtrs =*/true);
+ if (isNoWrap(PSE, AR, Ptr, AccessTy, Lp, Assume, DT, Stride))
+ return Stride;
- if (!Stride) {
LLVM_DEBUG(
- dbgs() << "LAA: Bad stride - Pointer isn't runtime const strided "
+ dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
<< *Ptr << " SCEV: " << *AR << "\n");
return std::nullopt;
}
-
- // Add runtime SCEV check for multiplier
- const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
- const APInt *APStepVal;
- const SCEV *Multiplier;
- if (match(Step, m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)))) {
- const SCEV *One = PSE.getSE()->getOne(Multiplier->getType());
- PSE.addPredicate(
- *PSE.getSE()->getComparePredicate(CmpInst::ICMP_EQ, Multiplier, One));
- }
-
- return Stride;
}
std::optional<int64_t> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 7cefa973933a4..c33833db97cfa 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -481,29 +481,15 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
- int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
- AllowRuntimeSCEVChecks, false)
- .value_or(0);
+ int Stride =
+ getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
+ AllowRuntimeSCEVChecks, false, AllowStridedPointerIVs)
+ .value_or(0);
if (Stride == 1 || Stride == -1)
return Stride;
return 0;
}
-int LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
- Value *Ptr) const {
- if (!AllowStridedPointerIVs)
- return 0;
-
- const auto &Strides =
- LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
-
- int Stride = getPtrConstRuntimeStride(PSE, AccessTy, Ptr, TheLoop, *DT,
- Strides, AllowRuntimeSCEVChecks)
- .value_or(0);
-
- return Stride;
-}
-
bool LoopVectorizationLegality::isInvariant(Value *V) const {
return LAI->isInvariant(V);
}
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 411c294729b52..cca446cb15f36 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1258,13 +1258,11 @@ class LoopVectorizationCostModel {
getDivRemSpeculationCost(Instruction *I, ElementCount VF);
/// Returns true if \p I is a memory instruction with consecutive memory
- /// access that can be widened.
+ /// access that can be widened. If AllowStridedPointerIVs is true, we
+ // check that strided pointers of the form {base * stride} can be widened.
+ // Will return true if stride is a loop invariant runtime constant.
bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF);
- // Returns true if \p is a memory instruction with strided memory access that
- // can be widened.
- bool stridedMemoryAccessCanBeWidened(Instruction *I, ElementCount VF);
-
/// Returns true if \p I is a memory instruction in an interleaved-group
/// of memory accesses that can be vectorized with wide vector loads/stores
/// and shuffles.
@@ -3102,32 +3100,6 @@ bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
return true;
}
-bool LoopVectorizationCostModel::stridedMemoryAccessCanBeWidened(
- Instruction *I, ElementCount VF) {
- // Get and ensure we have a valid memory instruction.
- assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
-
- auto *Ptr = getLoadStorePointerOperand(I);
- auto *ScalarTy = getLoadStoreType(I);
-
- // In order to be widened, the pointer should be consecutive, first of all.
- if (!Legal->isConstRuntimeStridedPtr(ScalarTy, Ptr))
- return false;
-
- // If the instruction is a store located in a predicated block, it will be
- // scalarized.
- if (isScalarWithPredication(I, VF))
- return false;
-
- // If the instruction's allocated size doesn't equal it's type size, it
- // requires padding and will be scalarized.
- auto &DL = I->getDataLayout();
- if (hasIrregularType(ScalarTy, DL))
- return false;
-
- return true;
-}
-
void LoopVectorizationCostModel::collectLoopUniforms(ElementCount VF) {
// We should not collect Uniforms more than once per VF. Right now,
// this function is called from collectUniformsAndScalars(), which
@@ -5345,10 +5317,6 @@ InstructionCost LoopVectorizationCostModel::getConsecutiveMemOpCost(
unsigned AS = getLoadStoreAddressSpace(I);
int ConsecutiveStride = Legal->isConsecutivePtr(ValTy, Ptr);
- ConsecutiveStride = (!ConsecutiveStride && IsRuntimeConstStrided)
- ? Legal->isConstRuntimeStridedPtr(ValTy, Ptr)
- : ConsecutiveStride;
-
assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
"Stride should be 1 or -1 for consecutive memory access");
const Align Alignment = getLoadStoreAlignment(I);
@@ -5801,23 +5769,6 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
setWideningDecision(&I, VF, Decision, Cost);
continue;
- } else if (stridedMemoryAccessCanBeWidened(&I, VF)) {
- int BaseStride = Legal->isConstRuntimeStridedPtr(
- getLoadStoreType(&I), getLoadStorePointerOperand(&I));
- assert((BaseStride >= 1) && "Expected base stride to be >= 1");
-
- // For base strides greater than 1 we should default to
- // gather/scatters
- InstructionCost Cost;
- if (BaseStride == 1) {
- Cost = getConsecutiveMemOpCost(&I, VF, true);
- setWideningDecision(&I, VF, CM_Widen, Cost);
- } else {
- Cost = isLegalGatherOrScatter(&I, VF) ? getGatherScatterCost(&I, VF)
- : InstructionCost::getInvalid();
- setWideningDecision(&I, VF, CM_GatherScatter, Cost);
- }
- continue;
}
// Choose between Interleaving, Gather/Scatter or Scalarization.
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 9d641cc08a18c..9b39166a3d70c 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -237,125 +237,68 @@ define void @single_constant_stride_ptr_iv(ptr %p) {
; CHECK: exit:
; CHECK-NEXT: ret void
;
-; NOSTRIDED-UF2-LABEL: @single_constant_stride_ptr_iv(
-; NOSTRIDED-UF2-NEXT: entry:
-; NOSTRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
-; NOSTRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
-; NOSTRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
-; NOSTRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
-; NOSTRIDED-UF2: vector.ph:
-; NOSTRIDED-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
-; NOSTRIDED-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
-; NOSTRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
-; NOSTRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; NOSTRIDED-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
-; NOSTRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
-; NOSTRIDED-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
-; NOSTRIDED-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
-; NOSTRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
-; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
-; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
-; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
-; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
-; NOSTRIDED-UF2: vector.body:
-; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
-; NOSTRIDED-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
-; NOSTRIDED-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
-; NOSTRIDED-UF2-NEXT: [[TMP12:%.*]] = extractelement <vscale x 4 x ptr> [[VECTOR_GEP]], i32 0
-; NOSTRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
-; NOSTRIDED-UF2-NEXT: [[WIDE_VEC:%.*]] = load <vscale x 8 x i32>, ptr [[TMP12]], align 4
-; NOSTRIDED-UF2-NEXT: [[STRIDED_VEC:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC]])
-; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC]], 0
-; NOSTRIDED-UF2-NEXT: [[TMP14:%.*]] = extractelement <vscale x 4 x ptr> [[STEP_ADD]], i32 0
-; NOSTRIDED-UF2-NEXT: [[WIDE_VEC1:%.*]] = load <vscale x 8 x i32>, ptr [[TMP14]], align 4
-; NOSTRIDED-UF2-NEXT: [[STRIDED_VEC2:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC1]])
-; NOSTRIDED-UF2-NEXT: [[TMP15:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC2]], 0
-; NOSTRIDED-UF2-NEXT: [[TMP16:%.*]] = add <vscale x 4 x i32> [[TMP13]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: [[TMP17:%.*]] = add <vscale x 4 x i32> [[TMP15]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP16]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
-; NOSTRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP17]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
-; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
-; NOSTRIDED-UF2-NEXT: [[TMP18:%.*]] = shl i64 [[TMP4]], 3
-; NOSTRIDED-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP18]]
-; NOSTRIDED-UF2-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; NOSTRIDED-UF2-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
-; NOSTRIDED-UF2: middle.block:
-; NOSTRIDED-UF2-NEXT: br label [[SCALAR_PH]]
-; NOSTRIDED-UF2: scalar.ph:
-; NOSTRIDED-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
-; NOSTRIDED-UF2-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
-; NOSTRIDED-UF2-NEXT: br label [[LOOP:%.*]]
-; NOSTRIDED-UF2: loop:
-; NOSTRIDED-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
-; NOSTRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
-; NOSTRIDED-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
-; NOSTRIDED-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
-; NOSTRIDED-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
-; NOSTRIDED-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
-; NOSTRIDED-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
-; NOSTRIDED-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
-; NOSTRIDED-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
-; NOSTRIDED-UF2: exit:
-; NOSTRIDED-UF2-NEXT: ret void
-;
-; STRIDED-UF2-LABEL: @single_constant_stride_ptr_iv(
-; STRIDED-UF2-NEXT: entry:
-; STRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
-; STRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
-; STRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
-; STRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
-; STRIDED-UF2: vector.ph:
-; STRIDED-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
-; STRIDED-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
-; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
-; STRIDED-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
-; STRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
-; STRIDED-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
-; STRIDED-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
-; STRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
-; STRIDED-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
-; STRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
-; STRIDED-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
-; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
-; STRIDED-UF2: vector.body:
-; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
-; STRIDED-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
-; STRIDED-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
-; STRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
-; STRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER1:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison)
-; STRIDED-UF2-NEXT: [[TMP12:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP13:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER1]], splat (i32 1)
-; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP12]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
-; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP13]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
-; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
-; STRIDED-UF2-NEXT: [[TMP14:%.*]] = shl i64 [[TMP4]], 3
-; STRIDED-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP14]]
-; STRIDED-UF2-NEXT: [[TMP15:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; STRIDED-UF2-NEXT: br i1 [[TMP15]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
-; STRIDED-UF2: middle.block:
-; STRIDED-UF2-NEXT: br label [[SCALAR_PH]]
-; STRIDED-UF2: scalar.ph:
-; STRIDED-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
-; STRIDED-UF2-NEXT: [[BC_RESUME_VAL2:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
-; STRIDED-UF2-NEXT: br label [[LOOP:%.*]]
-; STRIDED-UF2: loop:
-; STRIDED-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
-; STRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL2]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
-; STRIDED-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
-; STRIDED-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
-; STRIDED-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
-; STRIDED-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
-; STRIDED-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
-; STRIDED-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
-; STRIDED-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
-; STRIDED-UF2: exit:
-; STRIDED-UF2-NEXT: ret void
+; CHECK-UF2-LABEL: @single_constant_stride_ptr_iv(
+; CHECK-UF2-NEXT: entry:
+; CHECK-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
+; CHECK-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ule i64 1024, [[TMP1]]
+; CHECK-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
+; CHECK-UF2: vector.ph:
+; CHECK-UF2-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; CHECK-UF2-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
+; CHECK-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP3]], i64 0
+; CHECK-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; CHECK-UF2-NEXT: [[TMP4:%.*]] = shl nuw i64 [[TMP3]], 1
+; CHECK-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP4]]
+; CHECK-UF2-NEXT: [[TMP5:%.*]] = icmp eq i64 [[N_MOD_VF]], 0
+; CHECK-UF2-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i64 [[TMP4]], i64 [[N_MOD_VF]]
+; CHECK-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[TMP6]]
+; CHECK-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[N_VEC]], 8
+; CHECK-UF2-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP7]]
+; CHECK-UF2-NEXT: [[TMP9:%.*]] = shl <vscale x 4 x i64> [[BROADCAST_SPLAT]], splat (i64 3)
+; CHECK-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
+; CHECK-UF2: vector.body:
+; CHECK-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; CHECK-UF2-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[P]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
+; CHECK-UF2-NEXT: [[TMP10:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
+; CHECK-UF2-NEXT: [[TMP11:%.*]] = shl <vscale x 4 x i64> [[TMP10]], splat (i64 3)
+; CHECK-UF2-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 4 x i64> [[TMP11]]
+; CHECK-UF2-NEXT: [[TMP12:%.*]] = extractelement <vscale x 4 x ptr> [[VECTOR_GEP]], i32 0
+; CHECK-UF2-NEXT: [[STEP_ADD:%.*]] = getelementptr i8, <vscale x 4 x ptr> [[VECTOR_GEP]], <vscale x 4 x i64> [[TMP9]]
+; CHECK-UF2-NEXT: [[WIDE_VEC:%.*]] = load <vscale x 8 x i32>, ptr [[TMP12]], align 4
+; CHECK-UF2-NEXT: [[STRIDED_VEC:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC]])
+; CHECK-UF2-NEXT: [[TMP13:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC]], 0
+; CHECK-UF2-NEXT: [[TMP14:%.*]] = extractelement <vscale x 4 x ptr> [[STEP_ADD]], i32 0
+; CHECK-UF2-NEXT: [[WIDE_VEC1:%.*]] = load <vscale x 8 x i32>, ptr [[TMP14]], align 4
+; CHECK-UF2-NEXT: [[STRIDED_VEC2:%.*]] = call { <vscale x 4 x i32>, <vscale x 4 x i32> } @llvm.vector.deinterleave2.nxv8i32(<vscale x 8 x i32> [[WIDE_VEC1]])
+; CHECK-UF2-NEXT: [[TMP15:%.*]] = extractvalue { <vscale x 4 x i32>, <vscale x 4 x i32> } [[STRIDED_VEC2]], 0
+; CHECK-UF2-NEXT: [[TMP16:%.*]] = add <vscale x 4 x i32> [[TMP13]], splat (i32 1)
+; CHECK-UF2-NEXT: [[TMP17:%.*]] = add <vscale x 4 x i32> [[TMP15]], splat (i32 1)
+; CHECK-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP16]], <vscale x 4 x ptr> align 4 [[VECTOR_GEP]], <vscale x 4 x i1> splat (i1 true))
+; CHECK-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP17]], <vscale x 4 x ptr> align 4 [[STEP_ADD]], <vscale x 4 x i1> splat (i1 true))
+; CHECK-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
+; CHECK-UF2-NEXT: [[TMP18:%.*]] = shl i64 [[TMP4]], 3
+; CHECK-UF2-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP18]]
+; CHECK-UF2-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-UF2-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
+; CHECK-UF2: middle.block:
+; CHECK-UF2-NEXT: br label [[SCALAR_PH]]
+; CHECK-UF2: scalar.ph:
+; CHECK-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-UF2-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[TMP8]], [[MIDDLE_BLOCK]] ], [ [[P]], [[ENTRY]] ]
+; CHECK-UF2-NEXT: br label [[LOOP:%.*]]
+; CHECK-UF2: loop:
+; CHECK-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[NEXTI:%.*]], [[LOOP]] ]
+; CHECK-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], [[LOOP]] ]
+; CHECK-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
+; CHECK-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
+; CHECK-UF2-NEXT: store i32 [[Y0]], ptr [[PTR]], align 4
+; CHECK-UF2-NEXT: [[PTR_NEXT]] = getelementptr inbounds i8, ptr [[PTR]], i64 8
+; CHECK-UF2-NEXT: [[NEXTI]] = add i64 [[I]], 1
+; CHECK-UF2-NEXT: [[DONE:%.*]] = icmp eq i64 [[NEXTI]], 1024
+; CHECK-UF2-NEXT: br i1 [[DONE]], label [[EXIT:%.*]], label [[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
+; CHECK-UF2: exit:
+; CHECK-UF2-NEXT: ret void
;
entry:
br label %loop
>From f426888ab04ae64d37429340ec5c9ff889573a29 Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Wed, 4 Feb 2026 11:03:17 +0000
Subject: [PATCH 09/10] Review comments
- make use of gather scatter cost for cost widening of runtime strided ptrs
- re-introduced isConstRuntimeStridedPtr method
- update tests
Change-Id: Ie9dd939c871488cb65664e4196a2cfac4e0eaf53
---
.../Vectorize/LoopVectorizationLegality.h | 16 +-
.../Vectorize/LoopVectorizationLegality.cpp | 22 ++-
.../Transforms/Vectorize/LoopVectorize.cpp | 31 +++-
.../LoopVectorize/RISCV/strided-accesses.ll | 138 +++++++++---------
.../LoopVectorize/pointer-induction.ll | 108 +++++++-------
.../LoopVectorize/scev-predicate-reasoning.ll | 20 +--
6 files changed, 185 insertions(+), 150 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index a2d4f6bfc4ec6..3b2398dab012b 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -375,17 +375,19 @@ class LoopVectorizationLegality {
/// 1 - Address is consecutive.
/// -1 - Address is consecutive, and decreasing.
///
- /// When AllowStridedPointerIVs is true, check if this pointer is
- /// const runtime strided when vectorizing. This occurs for pointers
- /// that have an access pattern such as
+ /// NOTE: This method must only be used before modifying the original scalar
+ /// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
+ int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
+
+ // Check is this pointer is const runtime strided when vectorizing.
+ // This occurs for pointers that have an access pattern such as
//
// A[offset + stride * runtime_constant]
//
// This check allows us to vectorize these into a wide load/store when
- // `lv-strided-pointer-ivs=true. In this case the return value is 'stride'
- /// NOTE: This method must only be used before modifying the original scalar
- /// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
- int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
+ // `lv-strided-pointer-ivs=true` is true. Returns the base
+ // stride value
+ int isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
/// Returns true if \p V is invariant across all loop iterations according to
/// SCEV.
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index c33833db97cfa..7848a363d8c9e 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -481,13 +481,29 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
const auto &Strides =
LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
+ int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
+ AllowRuntimeSCEVChecks, false)
+ .value_or(0);
+
+ if (Stride == 1 || Stride == -1)
+ return Stride;
+ return 0;
+}
+
+int LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
+ Value *Ptr) const {
+ if (!AllowStridedPointerIVs)
+ return 0;
+
+ const auto &Strides =
+ LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
+
int Stride =
getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
AllowRuntimeSCEVChecks, false, AllowStridedPointerIVs)
.value_or(0);
- if (Stride == 1 || Stride == -1)
- return Stride;
- return 0;
+
+ return Stride;
}
bool LoopVectorizationLegality::isInvariant(Value *V) const {
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index cca446cb15f36..6533319bf52b1 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1261,7 +1261,8 @@ class LoopVectorizationCostModel {
/// access that can be widened. If AllowStridedPointerIVs is true, we
// check that strided pointers of the form {base * stride} can be widened.
// Will return true if stride is a loop invariant runtime constant.
- bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF);
+ bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF,
+ bool AllowStridedPointerIVs = false);
/// Returns true if \p I is a memory instruction in an interleaved-group
/// of memory accesses that can be vectorized with wide vector loads/stores
@@ -1551,8 +1552,7 @@ class LoopVectorizationCostModel {
/// The cost computation for widening instruction \p I with consecutive
/// memory access.
- InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF,
- bool IsRuntimeConstStrided = false);
+ InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF);
/// The cost calculation for Load/Store instruction \p I with uniform pointer -
/// Load: scalar load + broadcast.
@@ -3075,7 +3075,7 @@ bool LoopVectorizationCostModel::interleavedAccessCanBeWidened(
}
bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
- Instruction *I, ElementCount VF) {
+ Instruction *I, ElementCount VF, bool AllowStridedPointerIVs) {
// Get and ensure we have a valid memory instruction.
assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
@@ -3083,7 +3083,10 @@ bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
auto *ScalarTy = getLoadStoreType(I);
// In order to be widened, the pointer should be consecutive, first of all.
- if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
+ if (!AllowStridedPointerIVs) {
+ if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
+ return false;
+ } else if (!Legal->isConstRuntimeStridedPtr(ScalarTy, Ptr))
return false;
// If the instruction is a store located in a predicated block, it will be
@@ -5309,8 +5312,9 @@ LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
return Cost;
}
-InstructionCost LoopVectorizationCostModel::getConsecutiveMemOpCost(
- Instruction *I, ElementCount VF, bool IsRuntimeConstStrided) {
+InstructionCost
+LoopVectorizationCostModel::getConsecutiveMemOpCost(Instruction *I,
+ ElementCount VF) {
Type *ValTy = getLoadStoreType(I);
auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
Value *Ptr = getLoadStorePointerOperand(I);
@@ -5769,6 +5773,19 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
setWideningDecision(&I, VF, Decision, Cost);
continue;
+ } else if (memoryInstructionCanBeWidened(
+ &I, VF, /* AllowStridedPointerIVs =*/true)) {
+ int BaseStride = Legal->isConstRuntimeStridedPtr(
+ getLoadStoreType(&I), getLoadStorePointerOperand(&I));
+ assert((BaseStride >= 1) && "Expected base stride to be >= 1");
+
+ // For base strides greater than 1 we should default to
+ // gather/scatters
+ if (BaseStride == 1) {
+ InstructionCost Cost = getGatherScatterCost(&I, VF);
+ setWideningDecision(&I, VF, CM_Widen, Cost);
+ continue;
+ }
}
// Choose between Interleaving, Gather/Scatter or Scalarization.
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 9b39166a3d70c..06ce9ff2eeddd 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -329,15 +329,15 @@ define void @single_stride_int_scaled(ptr %p, i64 %stride) {
; NOSTRIDED: vector.ph:
; NOSTRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED: vector.body:
-; NOSTRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[TMP7:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; NOSTRIDED-NEXT: [[TMP8:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
-; NOSTRIDED-NEXT: [[TMP10:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
-; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP10]], ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[EVL_BASED_IV]]
+; NOSTRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP4:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP4]], ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
; NOSTRIDED-NEXT: [[TMP11:%.*]] = zext i32 [[TMP7]] to i64
-; NOSTRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[TMP11]], [[INDEX]]
+; NOSTRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP11]], [[EVL_BASED_IV]]
; NOSTRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP11]]
; NOSTRIDED-NEXT: [[TMP9:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; NOSTRIDED-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
@@ -376,14 +376,14 @@ define void @single_stride_int_scaled(ptr %p, i64 %stride) {
; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED-UF2: vector.body:
; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[TMP4]], i64 [[TMP5]]
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP6:%.*]] = getelementptr i32, ptr [[TMP7]], i64 [[TMP5]]
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD1]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP3]]
; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; NOSTRIDED-UF2-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP8:![0-9]+]]
@@ -450,15 +450,15 @@ define void @single_stride_int_iv(ptr %p, i64 %stride) {
; NOSTRIDED: vector.ph:
; NOSTRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED: vector.body:
-; NOSTRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[TMP7:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; NOSTRIDED-NEXT: [[TMP8:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
-; NOSTRIDED-NEXT: [[TMP10:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
-; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP10]], ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[EVL_BASED_IV]]
+; NOSTRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP4:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP4]], ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
; NOSTRIDED-NEXT: [[TMP11:%.*]] = zext i32 [[TMP7]] to i64
-; NOSTRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[TMP11]], [[INDEX]]
+; NOSTRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP11]], [[EVL_BASED_IV]]
; NOSTRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP11]]
; NOSTRIDED-NEXT: [[TMP9:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; NOSTRIDED-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP7:![0-9]+]]
@@ -498,14 +498,14 @@ define void @single_stride_int_iv(ptr %p, i64 %stride) {
; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED-UF2: vector.body:
; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[TMP4]], i64 [[TMP5]]
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP6:%.*]] = getelementptr i32, ptr [[TMP7]], i64 [[TMP5]]
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD1]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP3]]
; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; NOSTRIDED-UF2-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
@@ -620,16 +620,16 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; NOSTRIDED: vector.ph:
; NOSTRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED: vector.body:
-; NOSTRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[TMP16:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; NOSTRIDED-NEXT: [[TMP12:%.*]] = getelementptr i32, ptr [[P]], i64 [[INDEX]]
-; NOSTRIDED-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP12]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP16]])
-; NOSTRIDED-NEXT: [[TMP14:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
-; NOSTRIDED-NEXT: [[TMP15:%.*]] = getelementptr i32, ptr [[P2]], i64 [[INDEX]]
-; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP14]], ptr align 4 [[TMP15]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP16]])
+; NOSTRIDED-NEXT: [[TMP9:%.*]] = getelementptr i32, ptr [[P]], i64 [[EVL_BASED_IV]]
+; NOSTRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP9]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP16]])
+; NOSTRIDED-NEXT: [[TMP8:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; NOSTRIDED-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[P2]], i64 [[EVL_BASED_IV]]
+; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP8]], ptr align 4 [[TMP7]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP16]])
; NOSTRIDED-NEXT: [[TMP13:%.*]] = zext i32 [[TMP16]] to i64
-; NOSTRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[TMP13]], [[INDEX]]
+; NOSTRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP13]], [[EVL_BASED_IV]]
; NOSTRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP13]]
; NOSTRIDED-NEXT: [[TMP11:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; NOSTRIDED-NEXT: br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP9:![0-9]+]]
@@ -680,16 +680,16 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED-UF2: vector.body:
; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr i32, ptr [[P]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP11:%.*]] = getelementptr i32, ptr [[TMP8]], i64 [[TMP9]]
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP8]], align 4
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD4:%.*]] = load <vscale x 4 x i32>, ptr [[TMP11]], align 4
+; NOSTRIDED-UF2-NEXT: [[TMP11:%.*]] = getelementptr i32, ptr [[P]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP10:%.*]] = getelementptr i32, ptr [[TMP11]], i64 [[TMP9]]
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP11]], align 4
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD4:%.*]] = load <vscale x 4 x i32>, ptr [[TMP10]], align 4
; NOSTRIDED-UF2-NEXT: [[TMP12:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD4]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: [[TMP14:%.*]] = getelementptr i32, ptr [[P2]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP17:%.*]] = getelementptr i32, ptr [[TMP14]], i64 [[TMP9]]
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP12]], ptr [[TMP14]], align 4
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP13]], ptr [[TMP17]], align 4
+; NOSTRIDED-UF2-NEXT: [[TMP15:%.*]] = getelementptr i32, ptr [[P2]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP14:%.*]] = getelementptr i32, ptr [[TMP15]], i64 [[TMP9]]
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP12]], ptr [[TMP15]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP13]], ptr [[TMP14]], align 4
; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP7]]
; NOSTRIDED-UF2-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; NOSTRIDED-UF2-NEXT: br i1 [[TMP18]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]
@@ -744,8 +744,8 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[TMP39:%.*]] = select i1 [[TMP34]], i1 [[TMP38]], i1 [[TMP37]]
; STRIDED-NEXT: [[TMP40:%.*]] = or i1 [[TMP39]], [[MUL_OVERFLOW3]]
; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
-; STRIDED-NEXT: [[TMP23:%.*]] = or i1 [[TMP13]], [[TMP40]]
-; STRIDED-NEXT: [[TMP21:%.*]] = or i1 [[TMP23]], [[IDENT_CHECK]]
+; STRIDED-NEXT: [[TMP22:%.*]] = or i1 [[TMP13]], [[TMP40]]
+; STRIDED-NEXT: [[TMP21:%.*]] = or i1 [[TMP22]], [[IDENT_CHECK]]
; STRIDED-NEXT: br i1 [[TMP21]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK1:%.*]]
; STRIDED: vector.memcheck:
; STRIDED-NEXT: [[TMP3:%.*]] = mul i64 [[STRIDE]], 4092
@@ -836,9 +836,9 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: [[TMP20:%.*]] = select i1 [[TMP13]], i1 [[TMP19]], i1 [[TMP18]]
; STRIDED-UF2-NEXT: [[TMP21:%.*]] = or i1 [[TMP20]], [[MUL_OVERFLOW3]]
; STRIDED-UF2-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
-; STRIDED-UF2-NEXT: [[TMP22:%.*]] = or i1 [[TMP12]], [[TMP21]]
-; STRIDED-UF2-NEXT: [[TMP32:%.*]] = or i1 [[TMP22]], [[IDENT_CHECK]]
-; STRIDED-UF2-NEXT: br i1 [[TMP32]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
+; STRIDED-UF2-NEXT: [[TMP31:%.*]] = or i1 [[TMP12]], [[TMP21]]
+; STRIDED-UF2-NEXT: [[TMP35:%.*]] = or i1 [[TMP31]], [[IDENT_CHECK]]
+; STRIDED-UF2-NEXT: br i1 [[TMP35]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
; STRIDED-UF2: vector.memcheck:
; STRIDED-UF2-NEXT: [[TMP23:%.*]] = mul i64 [[STRIDE]], 4092
; STRIDED-UF2-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[P2]], i64 [[TMP23]]
@@ -866,17 +866,17 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED-UF2: vector.body:
; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[TMP34:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
-; STRIDED-UF2-NEXT: [[TMP35:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP34]]
-; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[TMP35]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP35]], align 4, !alias.scope [[META8:![0-9]+]]
-; STRIDED-UF2-NEXT: [[WIDE_LOAD10:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8]]
+; STRIDED-UF2-NEXT: [[TMP32:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
+; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP32]]
+; STRIDED-UF2-NEXT: [[TMP34:%.*]] = getelementptr i32, ptr [[TMP33]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8:![0-9]+]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD10:%.*]] = load <vscale x 4 x i32>, ptr [[TMP34]], align 4, !alias.scope [[META8]]
; STRIDED-UF2-NEXT: [[TMP36:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; STRIDED-UF2-NEXT: [[TMP39:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD10]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP31:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP34]]
-; STRIDED-UF2-NEXT: [[TMP37:%.*]] = getelementptr i32, ptr [[TMP31]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP36]], ptr [[TMP31]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP39]], ptr [[TMP37]], align 4, !alias.scope [[META11]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: [[TMP37:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP32]]
+; STRIDED-UF2-NEXT: [[TMP40:%.*]] = getelementptr i32, ptr [[TMP37]], i64 [[TMP29]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP36]], ptr [[TMP37]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP39]], ptr [[TMP40]], align 4, !alias.scope [[META11]], !noalias [[META8]]
; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP30]]
; STRIDED-UF2-NEXT: [[TMP38:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; STRIDED-UF2-NEXT: br i1 [[TMP38]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP13:![0-9]+]]
@@ -929,15 +929,15 @@ define void @double_stride_int_iv(ptr %p, ptr %p2, i64 %stride) {
; NOSTRIDED: vector.ph:
; NOSTRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED: vector.body:
-; NOSTRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; NOSTRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; NOSTRIDED-NEXT: [[TMP7:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; NOSTRIDED-NEXT: [[TMP8:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
-; NOSTRIDED-NEXT: [[TMP10:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
-; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP10]], ptr align 4 [[TMP8]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[EVL_BASED_IV]]
+; NOSTRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
+; NOSTRIDED-NEXT: [[TMP4:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; NOSTRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP4]], ptr align 4 [[TMP1]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP7]])
; NOSTRIDED-NEXT: [[TMP11:%.*]] = zext i32 [[TMP7]] to i64
-; NOSTRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[TMP11]], [[INDEX]]
+; NOSTRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP11]], [[EVL_BASED_IV]]
; NOSTRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP11]]
; NOSTRIDED-NEXT: [[TMP9:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; NOSTRIDED-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP11:![0-9]+]]
@@ -978,14 +978,14 @@ define void @double_stride_int_iv(ptr %p, ptr %p2, i64 %stride) {
; NOSTRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; NOSTRIDED-UF2: vector.body:
; NOSTRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; NOSTRIDED-UF2-NEXT: [[TMP4:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[TMP4]], i64 [[TMP5]]
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP6:%.*]] = getelementptr i32, ptr [[TMP7]], i64 [[TMP5]]
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: [[WIDE_LOAD1:%.*]] = load <vscale x 4 x i32>, ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD1]], splat (i32 1)
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP4]], align 4
-; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP8]], ptr [[TMP7]], align 4
+; NOSTRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP9]], ptr [[TMP6]], align 4
; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP3]]
; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; NOSTRIDED-UF2-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP14:![0-9]+]]
@@ -1320,10 +1320,10 @@ define void @constant_stride_reinterpret(ptr noalias %in, ptr noalias %out) {
; NOSTRIDED-UF2-NEXT: [[TMP8:%.*]] = getelementptr inbounds nuw i32, ptr [[IN]], <vscale x 2 x i64> [[STEP_ADD]]
; NOSTRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 2 x i64> @llvm.masked.gather.nxv2i64.nxv2p0(<vscale x 2 x ptr> align 8 [[TMP7]], <vscale x 2 x i1> splat (i1 true), <vscale x 2 x i64> poison)
; NOSTRIDED-UF2-NEXT: [[WIDE_MASKED_GATHER1:%.*]] = call <vscale x 2 x i64> @llvm.masked.gather.nxv2i64.nxv2p0(<vscale x 2 x ptr> align 8 [[TMP8]], <vscale x 2 x i1> splat (i1 true), <vscale x 2 x i64> poison)
-; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = getelementptr inbounds nuw i64, ptr [[OUT:%.*]], i64 [[INDEX]]
-; NOSTRIDED-UF2-NEXT: [[TMP12:%.*]] = getelementptr inbounds nuw i64, ptr [[TMP9]], i64 [[TMP3]]
-; NOSTRIDED-UF2-NEXT: store <vscale x 2 x i64> [[WIDE_MASKED_GATHER]], ptr [[TMP9]], align 8
-; NOSTRIDED-UF2-NEXT: store <vscale x 2 x i64> [[WIDE_MASKED_GATHER1]], ptr [[TMP12]], align 8
+; NOSTRIDED-UF2-NEXT: [[TMP10:%.*]] = getelementptr inbounds nuw i64, ptr [[OUT:%.*]], i64 [[INDEX]]
+; NOSTRIDED-UF2-NEXT: [[TMP9:%.*]] = getelementptr inbounds nuw i64, ptr [[TMP10]], i64 [[TMP3]]
+; NOSTRIDED-UF2-NEXT: store <vscale x 2 x i64> [[WIDE_MASKED_GATHER]], ptr [[TMP10]], align 8
+; NOSTRIDED-UF2-NEXT: store <vscale x 2 x i64> [[WIDE_MASKED_GATHER1]], ptr [[TMP9]], align 8
; NOSTRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
; NOSTRIDED-UF2-NEXT: [[VEC_IND_NEXT]] = add nuw nsw <vscale x 2 x i64> [[STEP_ADD]], [[BROADCAST_SPLAT]]
; NOSTRIDED-UF2-NEXT: [[TMP13:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
diff --git a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
index ddcb5ab622b7c..a73605bb9b614 100644
--- a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
+++ b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
@@ -20,65 +20,65 @@ define void @a(ptr readnone %b) {
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], 4
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[N_VEC]], -1
-; CHECK-NEXT: [[IND_END:%.*]] = getelementptr i8, ptr null, i64 [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr null, i64 [[TMP1]]
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
; CHECK: vector.body:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE10:%.*]] ]
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = sub i64 0, [[INDEX]]
-; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[OFFSET_IDX]], 0
-; CHECK-NEXT: [[TMP11:%.*]] = add i64 [[OFFSET_IDX]], -1
-; CHECK-NEXT: [[TMP14:%.*]] = add i64 [[OFFSET_IDX]], -2
-; CHECK-NEXT: [[TMP17:%.*]] = add i64 [[OFFSET_IDX]], -3
-; CHECK-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[TMP2]]
-; CHECK-NEXT: [[NEXT_GEP2:%.*]] = getelementptr i8, ptr null, i64 [[TMP11]]
-; CHECK-NEXT: [[NEXT_GEP3:%.*]] = getelementptr i8, ptr null, i64 [[TMP14]]
-; CHECK-NEXT: [[NEXT_GEP4:%.*]] = getelementptr i8, ptr null, i64 [[TMP17]]
-; CHECK-NEXT: [[TMP21:%.*]] = insertelement <4 x ptr> poison, ptr [[NEXT_GEP]], i32 0
-; CHECK-NEXT: [[TMP22:%.*]] = insertelement <4 x ptr> [[TMP21]], ptr [[NEXT_GEP2]], i32 1
-; CHECK-NEXT: [[TMP23:%.*]] = insertelement <4 x ptr> [[TMP22]], ptr [[NEXT_GEP3]], i32 2
-; CHECK-NEXT: [[TMP24:%.*]] = insertelement <4 x ptr> [[TMP23]], ptr [[NEXT_GEP4]], i32 3
-; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP]], i64 -1
-; CHECK-NEXT: [[TMP25:%.*]] = getelementptr inbounds i8, ptr [[TMP3]], i64 0
-; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i8, ptr [[TMP25]], i64 -3
-; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i8>, ptr [[TMP5]], align 1
+; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[OFFSET_IDX]], 0
+; CHECK-NEXT: [[TMP4:%.*]] = add i64 [[OFFSET_IDX]], -1
+; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[OFFSET_IDX]], -2
+; CHECK-NEXT: [[TMP6:%.*]] = add i64 [[OFFSET_IDX]], -3
+; CHECK-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[TMP3]]
+; CHECK-NEXT: [[NEXT_GEP2:%.*]] = getelementptr i8, ptr null, i64 [[TMP4]]
+; CHECK-NEXT: [[NEXT_GEP3:%.*]] = getelementptr i8, ptr null, i64 [[TMP5]]
+; CHECK-NEXT: [[NEXT_GEP4:%.*]] = getelementptr i8, ptr null, i64 [[TMP6]]
+; CHECK-NEXT: [[TMP7:%.*]] = insertelement <4 x ptr> poison, ptr [[NEXT_GEP]], i32 0
+; CHECK-NEXT: [[TMP8:%.*]] = insertelement <4 x ptr> [[TMP7]], ptr [[NEXT_GEP2]], i32 1
+; CHECK-NEXT: [[TMP9:%.*]] = insertelement <4 x ptr> [[TMP8]], ptr [[NEXT_GEP3]], i32 2
+; CHECK-NEXT: [[TMP10:%.*]] = insertelement <4 x ptr> [[TMP9]], ptr [[NEXT_GEP4]], i32 3
+; CHECK-NEXT: [[TMP11:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP]], i64 -1
+; CHECK-NEXT: [[TMP12:%.*]] = getelementptr inbounds i8, ptr [[TMP11]], i64 0
+; CHECK-NEXT: [[TMP13:%.*]] = getelementptr inbounds i8, ptr [[TMP12]], i64 -3
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i8>, ptr [[TMP13]], align 1
; CHECK-NEXT: [[REVERSE:%.*]] = shufflevector <4 x i8> [[WIDE_LOAD]], <4 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
-; CHECK-NEXT: [[TMP7:%.*]] = icmp ne <4 x i8> [[REVERSE]], zeroinitializer
-; CHECK-NEXT: [[TMP8:%.*]] = extractelement <4 x i1> [[TMP7]], i32 0
-; CHECK-NEXT: br i1 [[TMP8]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
+; CHECK-NEXT: [[TMP14:%.*]] = icmp ne <4 x i8> [[REVERSE]], zeroinitializer
+; CHECK-NEXT: [[TMP15:%.*]] = extractelement <4 x i1> [[TMP14]], i32 0
+; CHECK-NEXT: br i1 [[TMP15]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
; CHECK: pred.store.if:
-; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP]], i64 -1
-; CHECK-NEXT: store i8 95, ptr [[TMP9]], align 1
+; CHECK-NEXT: [[TMP16:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP]], i64 -1
+; CHECK-NEXT: store i8 95, ptr [[TMP16]], align 1
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE]]
; CHECK: pred.store.continue:
-; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i1> [[TMP7]], i32 1
-; CHECK-NEXT: br i1 [[TMP10]], label [[PRED_STORE_IF5:%.*]], label [[PRED_STORE_CONTINUE6:%.*]]
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <4 x i1> [[TMP14]], i32 1
+; CHECK-NEXT: br i1 [[TMP17]], label [[PRED_STORE_IF5:%.*]], label [[PRED_STORE_CONTINUE6:%.*]]
; CHECK: pred.store.if5:
-; CHECK-NEXT: [[TMP12:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP2]], i64 -1
-; CHECK-NEXT: store i8 95, ptr [[TMP12]], align 1
+; CHECK-NEXT: [[TMP18:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP2]], i64 -1
+; CHECK-NEXT: store i8 95, ptr [[TMP18]], align 1
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE6]]
; CHECK: pred.store.continue6:
-; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i1> [[TMP7]], i32 2
-; CHECK-NEXT: br i1 [[TMP13]], label [[PRED_STORE_IF7:%.*]], label [[PRED_STORE_CONTINUE8:%.*]]
+; CHECK-NEXT: [[TMP19:%.*]] = extractelement <4 x i1> [[TMP14]], i32 2
+; CHECK-NEXT: br i1 [[TMP19]], label [[PRED_STORE_IF7:%.*]], label [[PRED_STORE_CONTINUE8:%.*]]
; CHECK: pred.store.if7:
-; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP3]], i64 -1
-; CHECK-NEXT: store i8 95, ptr [[TMP15]], align 1
+; CHECK-NEXT: [[TMP20:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP3]], i64 -1
+; CHECK-NEXT: store i8 95, ptr [[TMP20]], align 1
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE8]]
; CHECK: pred.store.continue8:
-; CHECK-NEXT: [[TMP16:%.*]] = extractelement <4 x i1> [[TMP7]], i32 3
-; CHECK-NEXT: br i1 [[TMP16]], label [[PRED_STORE_IF9:%.*]], label [[PRED_STORE_CONTINUE10]]
+; CHECK-NEXT: [[TMP21:%.*]] = extractelement <4 x i1> [[TMP14]], i32 3
+; CHECK-NEXT: br i1 [[TMP21]], label [[PRED_STORE_IF9:%.*]], label [[PRED_STORE_CONTINUE10]]
; CHECK: pred.store.if9:
-; CHECK-NEXT: [[TMP18:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP4]], i64 -1
-; CHECK-NEXT: store i8 95, ptr [[TMP18]], align 1
+; CHECK-NEXT: [[TMP22:%.*]] = getelementptr inbounds i8, ptr [[NEXT_GEP4]], i64 -1
+; CHECK-NEXT: store i8 95, ptr [[TMP22]], align 1
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE10]]
; CHECK: pred.store.continue10:
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
-; CHECK-NEXT: [[TMP19:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT: br i1 [[TMP19]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK-NEXT: [[TMP23:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT: br i1 [[TMP23]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: middle.block:
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
; CHECK-NEXT: br i1 [[CMP_N]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[SCALAR_PH]]
; CHECK: scalar.ph:
-; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi ptr [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ null, [[FOR_BODY_PREHEADER]] ]
+; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi ptr [ [[TMP2]], [[MIDDLE_BLOCK]] ], [ null, [[FOR_BODY_PREHEADER]] ]
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.cond.cleanup.loopexit:
; CHECK-NEXT: br label [[FOR_COND_CLEANUP]]
@@ -87,8 +87,8 @@ define void @a(ptr readnone %b) {
; CHECK: for.body:
; CHECK-NEXT: [[C_05:%.*]] = phi ptr [ [[INCDEC_PTR:%.*]], [[IF_END:%.*]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds i8, ptr [[C_05]], i64 -1
-; CHECK-NEXT: [[TMP20:%.*]] = load i8, ptr [[INCDEC_PTR]], align 1
-; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP20]], 0
+; CHECK-NEXT: [[TMP24:%.*]] = load i8, ptr [[INCDEC_PTR]], align 1
+; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i8 [[TMP24]], 0
; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[IF_END]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: store i8 95, ptr [[INCDEC_PTR]], align 1
@@ -140,37 +140,37 @@ define void @pointer_induction_used_as_vector(ptr noalias %start.1, ptr noalias
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N]], 4
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
; CHECK-NEXT: [[TMP0:%.*]] = mul i64 [[N_VEC]], 8
-; CHECK-NEXT: [[IND_END:%.*]] = getelementptr i8, ptr [[START_1:%.*]], i64 [[TMP0]]
-; CHECK-NEXT: [[IND_END2:%.*]] = getelementptr i8, ptr [[START_2:%.*]], i64 [[N_VEC]]
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[START_1:%.*]], i64 [[TMP0]]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[START_2:%.*]], i64 [[N_VEC]]
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
; CHECK: vector.body:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; CHECK-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[START_2]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
; CHECK-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <4 x i64> <i64 0, i64 1, i64 2, i64 3>
-; CHECK-NEXT: [[TMP4:%.*]] = extractelement <4 x ptr> [[VECTOR_GEP]], i32 0
+; CHECK-NEXT: [[TMP3:%.*]] = extractelement <4 x ptr> [[VECTOR_GEP]], i32 0
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], 8
; CHECK-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr [[START_1]], i64 [[OFFSET_IDX]]
-; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, <4 x ptr> [[VECTOR_GEP]], i64 1
-; CHECK-NEXT: store <4 x ptr> [[TMP2]], ptr [[NEXT_GEP]], align 8
-; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i8>, ptr [[TMP4]], align 1
-; CHECK-NEXT: [[TMP6:%.*]] = add <4 x i8> [[WIDE_LOAD]], splat (i8 1)
-; CHECK-NEXT: store <4 x i8> [[TMP6]], ptr [[TMP4]], align 1
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i8, <4 x ptr> [[VECTOR_GEP]], i64 1
+; CHECK-NEXT: store <4 x ptr> [[TMP4]], ptr [[NEXT_GEP]], align 8
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i8>, ptr [[TMP3]], align 1
+; CHECK-NEXT: [[TMP5:%.*]] = add <4 x i8> [[WIDE_LOAD]], splat (i8 1)
+; CHECK-NEXT: store <4 x i8> [[TMP5]], ptr [[TMP3]], align 1
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; CHECK-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], 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 [[LOOP4:![0-9]+]]
+; 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]], label [[EXIT:%.*]], label [[SCALAR_PH]]
; CHECK: scalar.ph:
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
-; CHECK-NEXT: [[BC_RESUME_VAL1:%.*]] = phi ptr [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[START_1]], [[ENTRY]] ]
-; CHECK-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[IND_END2]], [[MIDDLE_BLOCK]] ], [ [[START_2]], [[ENTRY]] ]
+; CHECK-NEXT: [[BC_RESUME_VAL1:%.*]] = phi ptr [ [[TMP1]], [[MIDDLE_BLOCK]] ], [ [[START_1]], [[ENTRY]] ]
+; CHECK-NEXT: [[BC_RESUME_VAL2:%.*]] = phi ptr [ [[TMP2]], [[MIDDLE_BLOCK]] ], [ [[START_2]], [[ENTRY]] ]
; CHECK-NEXT: br label [[LOOP_BODY:%.*]]
; CHECK: loop.body:
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LOOP_BODY]] ]
; CHECK-NEXT: [[PTR_IV_1:%.*]] = phi ptr [ [[BC_RESUME_VAL1]], [[SCALAR_PH]] ], [ [[PTR_IV_1_NEXT:%.*]], [[LOOP_BODY]] ]
-; CHECK-NEXT: [[PTR_IV_2:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_IV_2_NEXT:%.*]], [[LOOP_BODY]] ]
+; CHECK-NEXT: [[PTR_IV_2:%.*]] = phi ptr [ [[BC_RESUME_VAL2]], [[SCALAR_PH]] ], [ [[PTR_IV_2_NEXT:%.*]], [[LOOP_BODY]] ]
; CHECK-NEXT: [[PTR_IV_1_NEXT]] = getelementptr inbounds ptr, ptr [[PTR_IV_1]], i64 1
; CHECK-NEXT: [[PTR_IV_2_NEXT]] = getelementptr inbounds i8, ptr [[PTR_IV_2]], i64 1
; CHECK-NEXT: store ptr [[PTR_IV_2_NEXT]], ptr [[PTR_IV_1]], align 8
@@ -309,8 +309,8 @@ define void @outside_lattice(ptr noalias %p, ptr noalias %q, i32 %n) {
; DEFAULT-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
; DEFAULT-NEXT: [[VECTOR_GEP:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <4 x i64> <i64 0, i64 4, i64 8, i64 12>
; DEFAULT-NEXT: [[OFFSET_IDX:%.*]] = trunc i64 [[INDEX]] to i32
-; DEFAULT-NEXT: [[TMP5:%.*]] = getelementptr inbounds ptr, ptr [[P]], i32 [[OFFSET_IDX]]
-; DEFAULT-NEXT: store <4 x ptr> [[VECTOR_GEP]], ptr [[TMP5]], align 8
+; DEFAULT-NEXT: [[TMP6:%.*]] = getelementptr inbounds ptr, ptr [[P]], i32 [[OFFSET_IDX]]
+; DEFAULT-NEXT: store <4 x ptr> [[VECTOR_GEP]], ptr [[TMP6]], align 8
; DEFAULT-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[Q:%.*]], i32 [[OFFSET_IDX]]
; DEFAULT-NEXT: store <4 x i32> [[VEC_IND]], ptr [[TMP7]], align 4
; DEFAULT-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
diff --git a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
index a3648fceb7d2c..f1d017d08d3f9 100644
--- a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
+++ b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
@@ -17,20 +17,20 @@ define void @step_direction_unknown(i32 %arg, ptr %dst) {
; CHECK-NEXT: [[TMP3:%.*]] = sub i32 0, [[MUL_RESULT]]
; CHECK-NEXT: [[TMP4:%.*]] = icmp ugt i32 [[TMP3]], 0
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP1]], i1 [[TMP4]], i1 false
-; CHECK-NEXT: [[TMP6:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
-; CHECK-NEXT: [[TMP9:%.*]] = sext i32 [[ADD]] to i64
-; CHECK-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP9]], 1
-; CHECK-NEXT: [[TMP10:%.*]] = or i1 [[TMP6]], [[IDENT_CHECK]]
-; CHECK-NEXT: br i1 [[TMP10]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK-NEXT: [[TMP8:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
+; CHECK-NEXT: [[TMP7:%.*]] = sext i32 [[ADD]] to i64
+; CHECK-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP7]], 1
+; CHECK-NEXT: [[TMP11:%.*]] = or i1 [[TMP8]], [[IDENT_CHECK]]
+; CHECK-NEXT: br i1 [[TMP11]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; CHECK: [[VECTOR_PH]]:
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[INDEX]] to i32
-; CHECK-NEXT: [[TMP8:%.*]] = mul i32 [[ADD]], [[TMP7]]
-; CHECK-NEXT: [[TMP12:%.*]] = zext i32 [[TMP8]] to i64
-; CHECK-NEXT: [[TMP16:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP12]]
-; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP16]], align 8
+; CHECK-NEXT: [[TMP9:%.*]] = trunc i64 [[INDEX]] to i32
+; CHECK-NEXT: [[TMP10:%.*]] = mul i32 [[ADD]], [[TMP9]]
+; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[TMP10]] to i64
+; CHECK-NEXT: [[TMP21:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP14]]
+; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP21]], align 8
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
; CHECK-NEXT: br i1 [[TMP18]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
>From 50c346c9a8186f148517052e10b0bcd1460052ee Mon Sep 17 00:00:00 2001
From: nasmnc01 <nashe.mncube at arm.com>
Date: Wed, 11 Feb 2026 11:03:25 +0000
Subject: [PATCH 10/10] Update getPtrStride to recognise const runtime strided
patterns
Update the getPtrStride method to allow recognition of runtime
const strided pointers.
Change-Id: I67b41c0f0948542b2f07d6d3fea1cee826a4d0cf
---
.../llvm/Analysis/LoopAccessAnalysis.h | 9 +-
.../Vectorize/LoopVectorizationLegality.h | 11 ---
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 96 ++++---------------
.../Vectorize/LoopVectorizationLegality.cpp | 18 +---
.../Transforms/Vectorize/LoopVectorize.cpp | 23 +----
.../LoopVectorize/RISCV/strided-accesses.ll | 58 ++++++-----
.../LoopVectorize/pointer-induction.ll | 68 ++++++-------
.../LoopVectorize/scev-predicate-reasoning.ll | 26 +++--
8 files changed, 112 insertions(+), 197 deletions(-)
diff --git a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
index 5365cdc093592..c85ef3e131068 100644
--- a/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
+++ b/llvm/include/llvm/Analysis/LoopAccessAnalysis.h
@@ -888,12 +888,6 @@ replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
/// The \p Assume parameter indicates if we are allowed to make additional
/// run-time assumptions.
///
-/// If AllowRuntimeStridedIVs is true, this method will check if the ptr is
-/// making use of a runtime constant to calculate the stride i.e.
-/// A[stride * multiplier] with multiplier defined at runtime. In the case that
-/// the stride is of the form stride * multiplier, this method will return the
-/// base stride and also add a runtime SCEV for stride.
-///
/// Note that the analysis results are defined if-and-only-if the original
/// memory access was defined. If that access was dead, or UB, then the
/// result of this function is undefined.
@@ -902,8 +896,7 @@ getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
const Loop *Lp, const DominatorTree &DT,
const DenseMap<Value *, const SCEV *> &StridesMap =
DenseMap<Value *, const SCEV *>(),
- bool Assume = false, bool ShouldCheckWrap = true,
- bool AllowRuntimeStridedIVs = false);
+ bool Assume = false, bool ShouldCheckWrap = true);
/// Returns the distance between the pointers \p PtrA and \p PtrB iff they are
/// compatible and it is possible to calculate the distance between them. This
diff --git a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index 3b2398dab012b..f82fc588639dd 100644
--- a/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -374,21 +374,10 @@ class LoopVectorizationLegality {
/// 0 - Stride is unknown or non-consecutive.
/// 1 - Address is consecutive.
/// -1 - Address is consecutive, and decreasing.
- ///
/// NOTE: This method must only be used before modifying the original scalar
/// loop. Do not use after invoking 'createVectorizedLoopSkeleton' (PR34965).
int isConsecutivePtr(Type *AccessTy, Value *Ptr) const;
- // Check is this pointer is const runtime strided when vectorizing.
- // This occurs for pointers that have an access pattern such as
- //
- // A[offset + stride * runtime_constant]
- //
- // This check allows us to vectorize these into a wide load/store when
- // `lv-strided-pointer-ivs=true` is true. Returns the base
- // stride value
- int isConstRuntimeStridedPtr(Type *AccessTy, Value *Ptr) const;
-
/// Returns true if \p V is invariant across all loop iterations according to
/// SCEV.
bool isInvariant(Value *V) const;
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 82dc45d224d8a..fe73a23ac4812 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -964,12 +964,11 @@ class AccessAnalysis {
} // end anonymous namespace
-/// Try to compute a constant stride for \p AR. Used by getPtrStride,
-/// getPtrConstRuntimeStride and isNoWrap.
+/// Try to compute a constant stride for \p AR. Used by getPtrStride and
+/// isNoWrap.
static std::optional<int64_t>
getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
- Value *Ptr, PredicatedScalarEvolution &PSE,
- bool AllowRuntimeStridedIVs = false) {
+ Value *Ptr, PredicatedScalarEvolution &PSE) {
if (isa<ScalableVectorType>(AccessTy)) {
LLVM_DEBUG(dbgs() << "LAA: Bad stride - Scalable object: " << *AccessTy
<< "\n");
@@ -988,44 +987,12 @@ getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy,
return std::nullopt;
}
- // Calculate the pointer stride and check if it is constant. If we allow
- // strided pointers we also check the add recurrence expression is making use
- // of loop-invariant runtime constant for example
- //
- // Step = { stride * runtime_constant }
- //
- // we should treat this as a constant add recurrence. To achieve this we check
- // the arguments of the step are loop invariant and compute the step as
- // equivalent to just stride.
- //
- // We only check this for mul expressions as these are most common and
- // expected for strided pointer recurrence expressions.
+ // Check the step is constant.
const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
- const APInt *APStepVal;
- auto IsInvariantConstAddRecStep = [&](const SCEV *Step) -> bool {
- const SCEV *Multiplier;
-
- // Check the step is constant.
- if (match(Step, m_scev_APInt(APStepVal)))
- return true;
-
- if (AllowRuntimeStridedIVs) {
- if (PSE.getSE()->isKnownNegative(Step))
- return false;
-
- for (const auto *Op : Step->operands()) {
- if (!PSE.getSE()->isLoopInvariant(Op, Lp))
- return false;
- }
-
- return match(Step,
- m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)));
- }
-
- return false;
- };
- if (!IsInvariantConstAddRecStep(Step)) {
+ // Calculate the pointer stride and check if is constant
+ const APInt *APStepVal;
+ if (!match(Step, m_scev_APInt(APStepVal))) {
LLVM_DEBUG({
dbgs() << "LAA: Bad stride - Not a constant strided ";
if (Ptr)
@@ -1654,23 +1621,17 @@ void AccessAnalysis::processMemAccesses() {
}
/// Check whether the access through \p Ptr has a constant stride.
-std::optional<int64_t> llvm::getPtrStride(
- PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp,
- const DominatorTree &DT, const DenseMap<Value *, const SCEV *> &StridesMap,
- bool Assume, bool ShouldCheckWrap, bool AllowStridedPointerIVs) {
+std::optional<int64_t>
+llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
+ const Loop *Lp, const DominatorTree &DT,
+ const DenseMap<Value *, const SCEV *> &StridesMap,
+ bool Assume, bool ShouldCheckWrap) {
const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
return 0;
assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
- // Require runtime checks to ensure correctness of runtime stride
- if (AllowStridedPointerIVs && !Assume) {
- LLVM_DEBUG(dbgs() << "Cannot check for runtime strided constants without \
- allowing runtime SCEV checks");
- return std::nullopt;
- }
-
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
if (Assume && !AR)
AR = PSE.getAsAddRec(Ptr);
@@ -1682,33 +1643,17 @@ std::optional<int64_t> llvm::getPtrStride(
}
std::optional<int64_t> Stride =
- getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE, AllowStridedPointerIVs);
-
- if (AllowStridedPointerIVs && Stride) {
- // Add runtime SCEV check for multiplier
- const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
- const APInt *APStepVal;
- const SCEV *Multiplier;
- if (match(Step,
- m_scev_c_Mul(m_scev_APInt(APStepVal), m_SCEV(Multiplier)))) {
- const SCEV *One = PSE.getSE()->getOne(Multiplier->getType());
- PSE.addPredicate(
- *PSE.getSE()->getComparePredicate(CmpInst::ICMP_EQ, Multiplier, One));
- }
-
+ getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE);
+ if (!ShouldCheckWrap || !Stride)
return Stride;
- } else {
- if (!ShouldCheckWrap || !Stride)
- return Stride;
- if (isNoWrap(PSE, AR, Ptr, AccessTy, Lp, Assume, DT, Stride))
- return Stride;
+ if (isNoWrap(PSE, AR, Ptr, AccessTy, Lp, Assume, DT, Stride))
+ return Stride;
- LLVM_DEBUG(
- dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
- << *Ptr << " SCEV: " << *AR << "\n");
- return std::nullopt;
- }
+ LLVM_DEBUG(
+ dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
+ << *Ptr << " SCEV: " << *AR << "\n");
+ return std::nullopt;
}
std::optional<int64_t> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
@@ -3034,6 +2979,7 @@ static const SCEV *getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *L
if (isa<SCEVUnknown>(V))
return V;
+ match(V, m_scev_Mul(m_SCEVConstant(), m_SCEV(V)));
if (auto *C = dyn_cast<SCEVIntegralCastExpr>(V))
if (isa<SCEVUnknown>(C->getOperand()))
return V;
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 7848a363d8c9e..1b000be130e49 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -484,28 +484,11 @@ int LoopVectorizationLegality::isConsecutivePtr(Type *AccessTy,
int Stride = getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
AllowRuntimeSCEVChecks, false)
.value_or(0);
-
if (Stride == 1 || Stride == -1)
return Stride;
return 0;
}
-int LoopVectorizationLegality::isConstRuntimeStridedPtr(Type *AccessTy,
- Value *Ptr) const {
- if (!AllowStridedPointerIVs)
- return 0;
-
- const auto &Strides =
- LAI ? LAI->getSymbolicStrides() : DenseMap<Value *, const SCEV *>();
-
- int Stride =
- getPtrStride(PSE, AccessTy, Ptr, TheLoop, *DT, Strides,
- AllowRuntimeSCEVChecks, false, AllowStridedPointerIVs)
- .value_or(0);
-
- return Stride;
-}
-
bool LoopVectorizationLegality::isInvariant(Value *V) const {
return LAI->isInvariant(V);
}
@@ -1728,6 +1711,7 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
"RecurrencesInEarlyExitLoop", ORE, TheLoop);
return false;
}
+
SmallVector<BasicBlock *, 8> ExitingBlocks;
TheLoop->getExitingBlocks(ExitingBlocks);
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 6533319bf52b1..2e926e7290e64 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -1261,8 +1261,7 @@ class LoopVectorizationCostModel {
/// access that can be widened. If AllowStridedPointerIVs is true, we
// check that strided pointers of the form {base * stride} can be widened.
// Will return true if stride is a loop invariant runtime constant.
- bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF,
- bool AllowStridedPointerIVs = false);
+ bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF);
/// Returns true if \p I is a memory instruction in an interleaved-group
/// of memory accesses that can be vectorized with wide vector loads/stores
@@ -3075,7 +3074,7 @@ bool LoopVectorizationCostModel::interleavedAccessCanBeWidened(
}
bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
- Instruction *I, ElementCount VF, bool AllowStridedPointerIVs) {
+ Instruction *I, ElementCount VF) {
// Get and ensure we have a valid memory instruction.
assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
@@ -3083,10 +3082,7 @@ bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
auto *ScalarTy = getLoadStoreType(I);
// In order to be widened, the pointer should be consecutive, first of all.
- if (!AllowStridedPointerIVs) {
- if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
- return false;
- } else if (!Legal->isConstRuntimeStridedPtr(ScalarTy, Ptr))
+ if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
return false;
// If the instruction is a store located in a predicated block, it will be
@@ -5773,19 +5769,6 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
setWideningDecision(&I, VF, Decision, Cost);
continue;
- } else if (memoryInstructionCanBeWidened(
- &I, VF, /* AllowStridedPointerIVs =*/true)) {
- int BaseStride = Legal->isConstRuntimeStridedPtr(
- getLoadStoreType(&I), getLoadStorePointerOperand(&I));
- assert((BaseStride >= 1) && "Expected base stride to be >= 1");
-
- // For base strides greater than 1 we should default to
- // gather/scatters
- if (BaseStride == 1) {
- InstructionCost Cost = getGatherScatterCost(&I, VF);
- setWideningDecision(&I, VF, CM_Widen, Cost);
- continue;
- }
}
// Choose between Interleaving, Gather/Scatter or Scalarization.
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
index 06ce9ff2eeddd..09440002e397b 100644
--- a/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll
@@ -743,9 +743,7 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[TMP38:%.*]] = icmp ugt ptr [[TMP36]], [[P]]
; STRIDED-NEXT: [[TMP39:%.*]] = select i1 [[TMP34]], i1 [[TMP38]], i1 [[TMP37]]
; STRIDED-NEXT: [[TMP40:%.*]] = or i1 [[TMP39]], [[MUL_OVERFLOW3]]
-; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
-; STRIDED-NEXT: [[TMP22:%.*]] = or i1 [[TMP13]], [[TMP40]]
-; STRIDED-NEXT: [[TMP21:%.*]] = or i1 [[TMP22]], [[IDENT_CHECK]]
+; STRIDED-NEXT: [[TMP21:%.*]] = or i1 [[TMP13]], [[TMP40]]
; STRIDED-NEXT: br i1 [[TMP21]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK1:%.*]]
; STRIDED: vector.memcheck:
; STRIDED-NEXT: [[TMP3:%.*]] = mul i64 [[STRIDE]], 4092
@@ -766,20 +764,25 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
; STRIDED-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
; STRIDED: vector.ph:
+; STRIDED-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[STRIDE]], i64 0
+; STRIDED-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; STRIDED-NEXT: [[TMP42:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
; STRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED: vector.body:
-; STRIDED-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[TMP42]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[AVL:%.*]] = phi i64 [ 1024, [[VECTOR_PH]] ], [ [[AVL_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[TMP43:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
-; STRIDED-NEXT: [[TMP42:%.*]] = mul nuw nsw i64 [[EVL_BASED_IV]], [[STRIDE]]
-; STRIDED-NEXT: [[TMP45:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP42]]
-; STRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP45]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META5:![0-9]+]]
-; STRIDED-NEXT: [[TMP47:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
-; STRIDED-NEXT: [[TMP46:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP42]]
-; STRIDED-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP47]], ptr align 4 [[TMP46]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
; STRIDED-NEXT: [[TMP44:%.*]] = zext i32 [[TMP43]] to i64
-; STRIDED-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP44]], [[EVL_BASED_IV]]
+; STRIDED-NEXT: [[BROADCAST_SPLATINSERT9:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP44]], i64 0
+; STRIDED-NEXT: [[BROADCAST_SPLAT10:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT9]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; STRIDED-NEXT: [[TMP45:%.*]] = mul nuw nsw <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; STRIDED-NEXT: [[TMP46:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP45]]
+; STRIDED-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 4 x i32> @llvm.vp.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP46]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META5:![0-9]+]]
+; STRIDED-NEXT: [[TMP47:%.*]] = add <vscale x 4 x i32> [[WIDE_MASKED_GATHER]], splat (i32 1)
+; STRIDED-NEXT: [[TMP48:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP45]]
+; STRIDED-NEXT: call void @llvm.vp.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP47]], <vscale x 4 x ptr> align 4 [[TMP48]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP43]]), !alias.scope [[META8:![0-9]+]], !noalias [[META5]]
; STRIDED-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP44]]
+; STRIDED-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT10]]
; STRIDED-NEXT: [[TMP41:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
; STRIDED-NEXT: br i1 [[TMP41]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
; STRIDED: middle.block:
@@ -805,7 +808,7 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: entry:
; STRIDED-UF2-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
; STRIDED-UF2-NEXT: [[TMP1:%.*]] = shl nuw i64 [[TMP0]], 3
-; STRIDED-UF2-NEXT: [[UMAX9:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP1]], i64 98)
+; STRIDED-UF2-NEXT: [[UMAX9:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP1]], i64 79)
; STRIDED-UF2-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 1024, [[UMAX9]]
; STRIDED-UF2-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_SCEVCHECK:%.*]]
; STRIDED-UF2: vector.scevcheck:
@@ -835,9 +838,7 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2-NEXT: [[TMP19:%.*]] = icmp ugt ptr [[TMP17]], [[P]]
; STRIDED-UF2-NEXT: [[TMP20:%.*]] = select i1 [[TMP13]], i1 [[TMP19]], i1 [[TMP18]]
; STRIDED-UF2-NEXT: [[TMP21:%.*]] = or i1 [[TMP20]], [[MUL_OVERFLOW3]]
-; STRIDED-UF2-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
-; STRIDED-UF2-NEXT: [[TMP31:%.*]] = or i1 [[TMP12]], [[TMP21]]
-; STRIDED-UF2-NEXT: [[TMP35:%.*]] = or i1 [[TMP31]], [[IDENT_CHECK]]
+; STRIDED-UF2-NEXT: [[TMP35:%.*]] = or i1 [[TMP12]], [[TMP21]]
; STRIDED-UF2-NEXT: br i1 [[TMP35]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK:%.*]]
; STRIDED-UF2: vector.memcheck:
; STRIDED-UF2-NEXT: [[TMP23:%.*]] = mul i64 [[STRIDE]], 4092
@@ -860,24 +861,33 @@ define void @double_stride_int_scaled(ptr %p, ptr %p2, i64 %stride) {
; STRIDED-UF2: vector.ph:
; STRIDED-UF2-NEXT: [[TMP28:%.*]] = call i64 @llvm.vscale.i64()
; STRIDED-UF2-NEXT: [[TMP29:%.*]] = shl nuw i64 [[TMP28]], 2
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[TMP29]], i64 0
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
; STRIDED-UF2-NEXT: [[TMP30:%.*]] = shl nuw i64 [[TMP29]], 1
; STRIDED-UF2-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP30]]
; STRIDED-UF2-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[N_MOD_VF]]
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLATINSERT10:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[STRIDE]], i64 0
+; STRIDED-UF2-NEXT: [[BROADCAST_SPLAT11:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT10]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
+; STRIDED-UF2-NEXT: [[TMP31:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
; STRIDED-UF2-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED-UF2: vector.body:
; STRIDED-UF2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
-; STRIDED-UF2-NEXT: [[TMP32:%.*]] = mul nuw nsw i64 [[INDEX]], [[STRIDE]]
-; STRIDED-UF2-NEXT: [[TMP33:%.*]] = getelementptr i32, ptr [[P]], i64 [[TMP32]]
-; STRIDED-UF2-NEXT: [[TMP34:%.*]] = getelementptr i32, ptr [[TMP33]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 4 x i32>, ptr [[TMP33]], align 4, !alias.scope [[META8:![0-9]+]]
-; STRIDED-UF2-NEXT: [[WIDE_LOAD10:%.*]] = load <vscale x 4 x i32>, ptr [[TMP34]], align 4, !alias.scope [[META8]]
+; STRIDED-UF2-NEXT: [[VEC_IND:%.*]] = phi <vscale x 4 x i64> [ [[TMP31]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
+; STRIDED-UF2-NEXT: [[STEP_ADD:%.*]] = add <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; STRIDED-UF2-NEXT: [[TMP32:%.*]] = mul nuw nsw <vscale x 4 x i64> [[VEC_IND]], [[BROADCAST_SPLAT11]]
+; STRIDED-UF2-NEXT: [[TMP33:%.*]] = mul nuw nsw <vscale x 4 x i64> [[STEP_ADD]], [[BROADCAST_SPLAT11]]
+; STRIDED-UF2-NEXT: [[TMP34:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP32]]
+; STRIDED-UF2-NEXT: [[TMP37:%.*]] = getelementptr i32, ptr [[P]], <vscale x 4 x i64> [[TMP33]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP34]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison), !alias.scope [[META8:![0-9]+]]
+; STRIDED-UF2-NEXT: [[WIDE_LOAD10:%.*]] = call <vscale x 4 x i32> @llvm.masked.gather.nxv4i32.nxv4p0(<vscale x 4 x ptr> align 4 [[TMP37]], <vscale x 4 x i1> splat (i1 true), <vscale x 4 x i32> poison), !alias.scope [[META8]]
; STRIDED-UF2-NEXT: [[TMP36:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD]], splat (i32 1)
; STRIDED-UF2-NEXT: [[TMP39:%.*]] = add <vscale x 4 x i32> [[WIDE_LOAD10]], splat (i32 1)
-; STRIDED-UF2-NEXT: [[TMP37:%.*]] = getelementptr i32, ptr [[P2]], i64 [[TMP32]]
-; STRIDED-UF2-NEXT: [[TMP40:%.*]] = getelementptr i32, ptr [[TMP37]], i64 [[TMP29]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP36]], ptr [[TMP37]], align 4, !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
-; STRIDED-UF2-NEXT: store <vscale x 4 x i32> [[TMP39]], ptr [[TMP40]], align 4, !alias.scope [[META11]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: [[TMP40:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP32]]
+; STRIDED-UF2-NEXT: [[TMP41:%.*]] = getelementptr i32, ptr [[P2]], <vscale x 4 x i64> [[TMP33]]
+; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP36]], <vscale x 4 x ptr> align 4 [[TMP40]], <vscale x 4 x i1> splat (i1 true)), !alias.scope [[META11:![0-9]+]], !noalias [[META8]]
+; STRIDED-UF2-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> [[TMP39]], <vscale x 4 x ptr> align 4 [[TMP41]], <vscale x 4 x i1> splat (i1 true)), !alias.scope [[META11]], !noalias [[META8]]
; STRIDED-UF2-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP30]]
+; STRIDED-UF2-NEXT: [[VEC_IND_NEXT]] = add <vscale x 4 x i64> [[STEP_ADD]], [[BROADCAST_SPLAT]]
; STRIDED-UF2-NEXT: [[TMP38:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; STRIDED-UF2-NEXT: br i1 [[TMP38]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP13:![0-9]+]]
; STRIDED-UF2: middle.block:
diff --git a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
index a73605bb9b614..115b22b57e556 100644
--- a/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
+++ b/llvm/test/Transforms/LoopVectorize/pointer-induction.ll
@@ -526,10 +526,7 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED-NEXT: [[TMP0:%.*]] = add i64 [[N:%.*]], 1
; STRIDED-NEXT: [[TMP1:%.*]] = shl i64 [[STRIDE:%.*]], 3
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 4
-; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
-; STRIDED: vector.scevcheck:
-; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[STRIDE]], 1
-; STRIDED-NEXT: br i1 [[IDENT_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH1:%.*]]
+; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH1:%.*]]
; STRIDED: vector.ph:
; STRIDED-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], 4
; STRIDED-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
@@ -539,10 +536,28 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED: vector.body:
; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH1]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], [[TMP1]]
-; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[OFFSET_IDX]]
+; STRIDED-NEXT: [[TMP4:%.*]] = mul i64 0, [[TMP1]]
+; STRIDED-NEXT: [[TMP5:%.*]] = add i64 [[OFFSET_IDX]], [[TMP4]]
+; STRIDED-NEXT: [[TMP6:%.*]] = mul i64 1, [[TMP1]]
+; STRIDED-NEXT: [[TMP7:%.*]] = add i64 [[OFFSET_IDX]], [[TMP6]]
+; STRIDED-NEXT: [[TMP8:%.*]] = mul i64 2, [[TMP1]]
+; STRIDED-NEXT: [[TMP9:%.*]] = add i64 [[OFFSET_IDX]], [[TMP8]]
+; STRIDED-NEXT: [[TMP10:%.*]] = mul i64 3, [[TMP1]]
+; STRIDED-NEXT: [[TMP11:%.*]] = add i64 [[OFFSET_IDX]], [[TMP10]]
+; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr null, i64 [[TMP5]]
+; STRIDED-NEXT: [[NEXT_GEP1:%.*]] = getelementptr i8, ptr null, i64 [[TMP7]]
+; STRIDED-NEXT: [[NEXT_GEP2:%.*]] = getelementptr i8, ptr null, i64 [[TMP9]]
+; STRIDED-NEXT: [[NEXT_GEP3:%.*]] = getelementptr i8, ptr null, i64 [[TMP11]]
; STRIDED-NEXT: [[TMP21:%.*]] = getelementptr i64, ptr [[A:%.*]], i64 [[INDEX]]
; STRIDED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i64>, ptr [[TMP21]], align 8
-; STRIDED-NEXT: store <4 x i64> [[WIDE_LOAD]], ptr [[NEXT_GEP]], align 8
+; STRIDED-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 0
+; STRIDED-NEXT: [[TMP14:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 1
+; STRIDED-NEXT: [[TMP15:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 2
+; STRIDED-NEXT: [[TMP16:%.*]] = extractelement <4 x i64> [[WIDE_LOAD]], i32 3
+; STRIDED-NEXT: store i64 [[TMP13]], ptr [[NEXT_GEP]], align 8
+; STRIDED-NEXT: store i64 [[TMP14]], ptr [[NEXT_GEP1]], align 8
+; STRIDED-NEXT: store i64 [[TMP15]], ptr [[NEXT_GEP2]], align 8
+; STRIDED-NEXT: store i64 [[TMP16]], ptr [[NEXT_GEP3]], align 8
; STRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; STRIDED-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
; STRIDED-NEXT: br i1 [[TMP18]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]
@@ -553,8 +568,8 @@ define i64 @ivopt_widen_ptr_indvar_2(ptr noalias %a, i64 %stride, i64 %n) {
; STRIDED-NEXT: [[IND_ESCAPE:%.*]] = getelementptr i8, ptr [[TMP3]], i64 [[TMP19]]
; STRIDED-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
; STRIDED: scalar.ph:
-; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ], [ 0, [[VECTOR_PH]] ]
-; STRIDED-NEXT: [[BC_RESUME_VAL4:%.*]] = phi ptr [ [[TMP3]], [[MIDDLE_BLOCK]] ], [ null, [[ENTRY]] ], [ null, [[VECTOR_PH]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
+; STRIDED-NEXT: [[BC_RESUME_VAL4:%.*]] = phi ptr [ [[TMP3]], [[MIDDLE_BLOCK]] ], [ null, [[ENTRY]] ]
; STRIDED-NEXT: br label [[FOR_BODY:%.*]]
; STRIDED: for.body:
; STRIDED-NEXT: [[INDVAR:%.*]] = phi i64 [ [[INDVAR_NEXT:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
@@ -707,6 +722,8 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
;
; STRIDED-LABEL: @strided_ptr_iv_runtime_stride(
; STRIDED-NEXT: entry:
+; STRIDED-NEXT: [[PIN2:%.*]] = ptrtoint ptr [[PIN:%.*]] to i64
+; STRIDED-NEXT: [[POUT1:%.*]] = ptrtoint ptr [[POUT:%.*]] to i64
; STRIDED-NEXT: [[TMP0:%.*]] = zext i32 [[NCOLS:%.*]] to i64
; STRIDED-NEXT: [[UMAX4:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP0]], i64 1)
; STRIDED-NEXT: [[TMP1:%.*]] = sext i32 [[STRIDE:%.*]] to i64
@@ -714,28 +731,11 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
; STRIDED-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[UMAX4]], 4
; STRIDED-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
; STRIDED: vector.scevcheck:
-; STRIDED-NEXT: [[TMP18:%.*]] = sext i32 [[STRIDE]] to i64
-; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP18]], 1
+; STRIDED-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i32 [[STRIDE]], 1
; STRIDED-NEXT: br i1 [[IDENT_CHECK]], label [[SCALAR_PH]], label [[VECTOR_MEMCHECK1:%.*]]
; STRIDED: vector.memcheck:
-; STRIDED-NEXT: [[TMP3:%.*]] = sext i32 [[STRIDE]] to i64
-; STRIDED-NEXT: [[UMAX:%.*]] = call i32 @llvm.umax.i32(i32 [[NCOLS]], i32 1)
-; STRIDED-NEXT: [[TMP4:%.*]] = add i32 [[UMAX]], -1
-; STRIDED-NEXT: [[TMP5:%.*]] = zext i32 [[TMP4]] to i64
-; STRIDED-NEXT: [[TMP6:%.*]] = mul i64 [[TMP3]], [[TMP5]]
-; STRIDED-NEXT: [[TMP7:%.*]] = shl i64 [[TMP6]], 2
-; STRIDED-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[POUT:%.*]], i64 [[TMP7]]
-; STRIDED-NEXT: [[TMP8:%.*]] = icmp ult ptr [[POUT]], [[SCEVGEP]]
-; STRIDED-NEXT: [[UMIN:%.*]] = select i1 [[TMP8]], ptr [[POUT]], ptr [[SCEVGEP]]
-; STRIDED-NEXT: [[TMP9:%.*]] = icmp ugt ptr [[POUT]], [[SCEVGEP]]
-; STRIDED-NEXT: [[UMAX1:%.*]] = select i1 [[TMP9]], ptr [[POUT]], ptr [[SCEVGEP]]
-; STRIDED-NEXT: [[SCEVGEP2:%.*]] = getelementptr i8, ptr [[UMAX1]], i64 4
-; STRIDED-NEXT: [[TMP10:%.*]] = shl nuw nsw i64 [[TMP5]], 2
-; STRIDED-NEXT: [[TMP11:%.*]] = add nuw nsw i64 [[TMP10]], 4
-; STRIDED-NEXT: [[SCEVGEP3:%.*]] = getelementptr i8, ptr [[PIN:%.*]], i64 [[TMP11]]
-; STRIDED-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[UMIN]], [[SCEVGEP3]]
-; STRIDED-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[PIN]], [[SCEVGEP2]]
-; STRIDED-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
+; STRIDED-NEXT: [[TMP3:%.*]] = sub i64 [[POUT1]], [[PIN2]]
+; STRIDED-NEXT: [[FOUND_CONFLICT:%.*]] = icmp ult i64 [[TMP3]], 16
; STRIDED-NEXT: br i1 [[FOUND_CONFLICT]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
; STRIDED: vector.ph:
; STRIDED-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[UMAX4]], 4
@@ -743,20 +743,20 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
; STRIDED-NEXT: [[TMP12:%.*]] = trunc i64 [[N_VEC]] to i32
; STRIDED-NEXT: [[TMP13:%.*]] = mul i64 [[N_VEC]], 4
; STRIDED-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[PIN]], i64 [[TMP13]]
-; STRIDED-NEXT: [[TMP15:%.*]] = mul i64 [[N_VEC]], [[TMP2]]
+; STRIDED-NEXT: [[TMP15:%.*]] = mul i64 [[N_VEC]], 4
; STRIDED-NEXT: [[TMP16:%.*]] = getelementptr i8, ptr [[POUT]], i64 [[TMP15]]
; STRIDED-NEXT: br label [[VECTOR_BODY:%.*]]
; STRIDED: vector.body:
; STRIDED-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; STRIDED-NEXT: [[OFFSET_IDX:%.*]] = mul i64 [[INDEX]], 4
; STRIDED-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr [[PIN]], i64 [[OFFSET_IDX]]
-; STRIDED-NEXT: [[OFFSET_IDX5:%.*]] = mul i64 [[INDEX]], [[TMP2]]
+; STRIDED-NEXT: [[OFFSET_IDX5:%.*]] = mul i64 [[INDEX]], 4
; STRIDED-NEXT: [[NEXT_GEP6:%.*]] = getelementptr i8, ptr [[POUT]], i64 [[OFFSET_IDX5]]
-; STRIDED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[NEXT_GEP]], align 4, !alias.scope [[META16:![0-9]+]]
-; STRIDED-NEXT: store <4 x float> [[WIDE_LOAD]], ptr [[NEXT_GEP6]], align 4, !alias.scope [[META19:![0-9]+]], !noalias [[META16]]
+; STRIDED-NEXT: [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[NEXT_GEP]], align 4
+; STRIDED-NEXT: store <4 x float> [[WIDE_LOAD]], ptr [[NEXT_GEP6]], align 4
; STRIDED-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
; STRIDED-NEXT: [[TMP17:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
-; STRIDED-NEXT: br i1 [[TMP17]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP21:![0-9]+]]
+; STRIDED-NEXT: br i1 [[TMP17]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP16:![0-9]+]]
; STRIDED: middle.block:
; STRIDED-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[UMAX4]], [[N_VEC]]
; STRIDED-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
@@ -775,7 +775,7 @@ define void @strided_ptr_iv_runtime_stride(ptr %pIn, ptr %pOut, i32 %nCols, i32
; STRIDED-NEXT: [[OUT_NEXT]] = getelementptr inbounds float, ptr [[OUT_PTR]], i32 [[STRIDE]]
; STRIDED-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
; STRIDED-NEXT: [[CMP:%.*]] = icmp ult i32 [[IV_NEXT]], [[NCOLS]]
-; STRIDED-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT]], !llvm.loop [[LOOP22:![0-9]+]]
+; STRIDED-NEXT: br i1 [[CMP]], label [[LOOP]], label [[EXIT]], !llvm.loop [[LOOP17:![0-9]+]]
; STRIDED: exit:
; STRIDED-NEXT: ret void
;
diff --git a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
index f1d017d08d3f9..e293e7c3261d2 100644
--- a/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
+++ b/llvm/test/Transforms/LoopVectorize/scev-predicate-reasoning.ll
@@ -17,21 +17,31 @@ define void @step_direction_unknown(i32 %arg, ptr %dst) {
; CHECK-NEXT: [[TMP3:%.*]] = sub i32 0, [[MUL_RESULT]]
; CHECK-NEXT: [[TMP4:%.*]] = icmp ugt i32 [[TMP3]], 0
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP1]], i1 [[TMP4]], i1 false
-; CHECK-NEXT: [[TMP8:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
-; CHECK-NEXT: [[TMP7:%.*]] = sext i32 [[ADD]] to i64
-; CHECK-NEXT: [[IDENT_CHECK:%.*]] = icmp ne i64 [[TMP7]], 1
-; CHECK-NEXT: [[TMP11:%.*]] = or i1 [[TMP8]], [[IDENT_CHECK]]
+; CHECK-NEXT: [[TMP11:%.*]] = or i1 [[TMP5]], [[MUL_OVERFLOW]]
; CHECK-NEXT: br i1 [[TMP11]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[ADD]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <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:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[TMP9:%.*]] = trunc i64 [[INDEX]] to i32
-; CHECK-NEXT: [[TMP10:%.*]] = mul i32 [[ADD]], [[TMP9]]
-; CHECK-NEXT: [[TMP14:%.*]] = zext i32 [[TMP10]] to i64
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP7:%.*]] = mul <4 x i32> [[BROADCAST_SPLAT]], [[VEC_IND]]
+; CHECK-NEXT: [[TMP8:%.*]] = zext <4 x i32> [[TMP7]] to <4 x i64>
+; CHECK-NEXT: [[TMP14:%.*]] = extractelement <4 x i64> [[TMP8]], i32 0
+; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i64> [[TMP8]], i32 1
+; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[TMP8]], i32 2
+; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[TMP8]], i32 3
; CHECK-NEXT: [[TMP21:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP14]]
-; CHECK-NEXT: store <4 x double> zeroinitializer, ptr [[TMP21]], align 8
+; CHECK-NEXT: [[TMP17:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP10]]
+; CHECK-NEXT: [[TMP15:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP13]]
+; CHECK-NEXT: [[TMP16:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP12]]
+; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP21]], align 8
+; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP17]], align 8
+; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP15]], align 8
+; CHECK-NEXT: store double 0.000000e+00, ptr [[TMP16]], align 8
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add <4 x i32> [[VEC_IND]], splat (i32 4)
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
; CHECK-NEXT: br i1 [[TMP18]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: [[MIDDLE_BLOCK]]:
More information about the llvm-commits
mailing list