[llvm] [SLP] Avoid seeding related affine loop address computations (PR #226220)
Tim Besard via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 03:35:31 PDT 2026
https://github.com/maleadt updated https://github.com/llvm/llvm-project/pull/226220
>From abee20817304579986bb7a69d863669a82a624a6 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Thu, 24 Sep 2026 12:55:55 +0200
Subject: [PATCH 1/5] [SLP] Avoid seeding related affine loop address
computations
When the loop vectorizer scalarizes strided accesses, each lane computes
its own address from the induction variable, a loop-invariant stride
and a loop-invariant offset, e.g.
base + ((off + stride * (iv + k)) << 2). These addresses are affine
recurrences that loop strength reduction turns into pointer increments.
With the loop-aware cost model (#150450), SLP now vectorizes the index
arithmetic instead: the multiplies, adds and shifts are done on
<4 x i64> in the loop, and every lane is extracted again to feed the
scalar loads. The cost model compares this against the scalar index
arithmetic, which LSR removes, so the vector form looks profitable
while the loop gets longer. For strided_gather_offset in the new test,
the loop grows from 13 to 30 instructions on Haswell, and from 13 to 23
on skylake-avx512, which has a vector i64 multiply.
vectorizeGEPIndices already drops pairs of getelementptrs with a
constant difference, because one address can be computed from the
other. Generalize this to loop-invariant differences: skip a bundle
when each lane is a short single-use chain of index arithmetic in a
loop, ending as the index of a getelementptr that is only used by
scalar loads and stores, and the addresses are affine recurrences of
that loop that differ by loop-invariant amounts. Check this in
tryToVectorizeList, because the once-used seeds vectorize the same
arithmetic when only the getelementptr seeds are skipped. Loads that
are vectorized as a gather together with their addresses are not
affected.
Assisted-by: Claude Code, Codex
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 71 ++++++++
.../SLPVectorizer/AArch64/getelementptr.ll | 27 +--
.../X86/strength-reducible-address.ll | 155 ++++++++----------
3 files changed, 149 insertions(+), 104 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 9d7fbd9f0ae044..e98b5d62719a72 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -30521,6 +30521,71 @@ void SLPVectorizerPass::collectSeedInstructions(BasicBlock *BB) {
}
}
+/// Returns true if \p Ptr is only used by scalar loads and stores, directly or
+/// through getelementptrs with constant offsets.
+static bool onlyFeedsScalarAccesses(const Value *Ptr, unsigned Depth = 0) {
+ constexpr unsigned MaxDepth = 2;
+ return !Ptr->use_empty() && all_of(Ptr->users(), [&](const User *U) {
+ if (const auto *Load = dyn_cast<LoadInst>(U))
+ return !Load->getType()->isVectorTy();
+ if (const auto *Store = dyn_cast<StoreInst>(U))
+ return Store->getPointerOperand() == Ptr &&
+ !Store->getValueOperand()->getType()->isVectorTy();
+ if (const auto *GEP = dyn_cast<GetElementPtrInst>(U))
+ return Depth < MaxDepth && GEP->getPointerOperand() == Ptr &&
+ GEP->hasAllConstantIndices() &&
+ onlyFeedsScalarAccesses(GEP, Depth + 1);
+ return false;
+ });
+}
+
+/// Leave related affine address recurrences feeding scalar accesses for loop
+/// strength reduction. Vectorizing their indices can retain expensive
+/// arithmetic and require an extract for each lane instead of a scalar pointer
+/// increment.
+static bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL,
+ ScalarEvolution &SE, LoopInfo &LI) {
+ constexpr unsigned MaxIndexChainLength = 3;
+ const Loop *L = nullptr;
+ SmallVector<GetElementPtrInst *> GEPs;
+ for (Value *V : VL) {
+ Value *Cur = V;
+ GetElementPtrInst *GEP = nullptr;
+ for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
+ if (!isa<BinaryOperator, CastInst>(Cur) || !Cur->hasOneUse())
+ return false;
+ User *U = Cur->user_back();
+ if ((GEP = dyn_cast<GetElementPtrInst>(U)))
+ break;
+ Cur = U;
+ }
+ if (!GEP || GEP->getPointerOperand() == Cur ||
+ !onlyFeedsScalarAccesses(GEP))
+ return false;
+ // LSR only removes the arithmetic computed in the loop itself.
+ const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
+ if (!GEPLoop || (L && L != GEPLoop) ||
+ LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
+ return false;
+ L = GEPLoop;
+ GEPs.push_back(GEP);
+ }
+ const SCEV *FirstAddr = nullptr;
+ for (GetElementPtrInst *GEP : GEPs) {
+ const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
+ if (!Addr || Addr->getLoop() != L || !Addr->isAffine())
+ return false;
+ if (!FirstAddr) {
+ FirstAddr = Addr;
+ continue;
+ }
+ const SCEV *Diff = SE.getMinusSCEV(Addr, FirstAddr);
+ if (isa<SCEVCouldNotCompute>(Diff) || !SE.isLoopInvariant(Diff, L))
+ return false;
+ }
+ return true;
+}
+
bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
bool MaxVFOnly,
bool StandaloneSeeds) {
@@ -30662,6 +30727,12 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
continue;
}
+ if (isStrengthReducibleIndexBundle(Ops, *SE, *LI)) {
+ LLVM_DEBUG(dbgs() << "SLP: Not vectorizing strength-reducible address "
+ "computations.\n");
+ continue;
+ }
+
LLVM_DEBUG(dbgs() << "SLP: Analyzing " << ActualVF << " operations "
<< "\n");
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/getelementptr.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/getelementptr.ll
index d7f5ad7861034d..ee7b4ab5a411d0 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/getelementptr.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/getelementptr.ll
@@ -8,6 +8,8 @@
; getelementptrs when they are known to have a constant difference. Such pairs
; are likely not good candidates for vectorization since one can be computed
; from the other. We use an unprofitable threshold to force vectorization.
+; The same holds for addresses that are affine recurrences of the loop with
+; loop-invariant differences, as in getelementptr_4x32.
;
; int getelementptr(int *g, int n, int w, int x, int y, int z) {
; int sum = 0;
@@ -30,25 +32,12 @@
; YAML-NEXT: - String: ' and with tree size '
; YAML-NEXT: - TreeSize: '1'
-; YAML: --- !Passed
-; YAML-NEXT: Pass: slp-vectorizer
-; YAML-NEXT: Name: VectorizedList
-; YAML-NEXT: Function: getelementptr_4x32
-; YAML-NEXT: Args:
-; YAML-NEXT: - String: 'SLP vectorized with cost '
-; YAML-NEXT: - Cost: '10'
-; YAML-NEXT: - String: ' and with tree size '
-; YAML-NEXT: - TreeSize: '3'
-
define i32 @getelementptr_4x32(ptr nocapture readonly %g, i32 %n, i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @getelementptr_4x32(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP31:%.*]] = icmp sgt i32 [[N:%.*]], 0
; CHECK-NEXT: br i1 [[CMP31]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]]
; CHECK: for.body.preheader:
-; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i32> <i32 0, i32 poison>, i32 [[X:%.*]], i64 1
-; CHECK-NEXT: [[TMP4:%.*]] = insertelement <2 x i32> poison, i32 [[Y:%.*]], i64 0
-; CHECK-NEXT: [[TMP5:%.*]] = insertelement <2 x i32> [[TMP4]], i32 [[Z:%.*]], i64 1
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.cond.cleanup.loopexit:
; CHECK-NEXT: br label [[FOR_COND_CLEANUP]]
@@ -63,20 +52,16 @@ define i32 @getelementptr_4x32(ptr nocapture readonly %g, i32 %n, i32 %x, i32 %y
; CHECK-NEXT: [[SUM_32:%.*]] = phi i32 [ 0, [[FOR_BODY_PREHEADER]] ], [ [[OP_RDX:%.*]], [[FOR_BODY]] ]
; CHECK-NEXT: [[SLPRDX_ACC:%.*]] = phi <4 x i32> [ zeroinitializer, [[FOR_BODY_PREHEADER]] ], [ [[SLPRDX_ACC1]], [[FOR_BODY]] ]
; CHECK-NEXT: [[T4:%.*]] = shl nsw i32 [[SUM_32]], 1
-; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[T4]], i64 0
-; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP3:%.*]] = add nsw <2 x i32> [[TMP2]], [[TMP0]]
-; CHECK-NEXT: [[TMP12:%.*]] = extractelement <2 x i32> [[TMP3]], i64 0
+; CHECK-NEXT: [[TMP12:%.*]] = add nsw i32 [[T4]], 0
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[G:%.*]], i32 [[TMP12]]
; CHECK-NEXT: [[T6:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
-; CHECK-NEXT: [[TMP11:%.*]] = extractelement <2 x i32> [[TMP3]], i64 1
+; CHECK-NEXT: [[TMP11:%.*]] = add nsw i32 [[T4]], [[X:%.*]]
; CHECK-NEXT: [[ARRAYIDX5:%.*]] = getelementptr inbounds i32, ptr [[G]], i32 [[TMP11]]
; CHECK-NEXT: [[T8:%.*]] = load i32, ptr [[ARRAYIDX5]], align 4
-; CHECK-NEXT: [[TMP16:%.*]] = add nsw <2 x i32> [[TMP2]], [[TMP5]]
-; CHECK-NEXT: [[TMP13:%.*]] = extractelement <2 x i32> [[TMP16]], i64 0
+; CHECK-NEXT: [[TMP13:%.*]] = add nsw i32 [[T4]], [[Y:%.*]]
; CHECK-NEXT: [[ARRAYIDX10:%.*]] = getelementptr inbounds i32, ptr [[G]], i32 [[TMP13]]
; CHECK-NEXT: [[T10:%.*]] = load i32, ptr [[ARRAYIDX10]], align 4
-; CHECK-NEXT: [[TMP14:%.*]] = extractelement <2 x i32> [[TMP16]], i64 1
+; CHECK-NEXT: [[TMP14:%.*]] = add nsw i32 [[T4]], [[Z:%.*]]
; CHECK-NEXT: [[ARRAYIDX15:%.*]] = getelementptr inbounds i32, ptr [[G]], i32 [[TMP14]]
; CHECK-NEXT: [[T12:%.*]] = load i32, ptr [[ARRAYIDX15]], align 4
; CHECK-NEXT: [[TMP17:%.*]] = insertelement <4 x i32> poison, i32 [[T6]], i64 0
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/strength-reducible-address.ll b/llvm/test/Transforms/SLPVectorizer/X86/strength-reducible-address.ll
index 553b472868edbb..86e00db13015a5 100644
--- a/llvm/test/Transforms/SLPVectorizer/X86/strength-reducible-address.ll
+++ b/llvm/test/Transforms/SLPVectorizer/X86/strength-reducible-address.ll
@@ -12,32 +12,33 @@ define i32 @strided_gather(ptr %base, ptr %dims, i64 %n) {
; AVX2-LABEL: define i32 @strided_gather(
; AVX2-SAME: ptr [[BASE:%.*]], ptr [[DIMS:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
; AVX2-NEXT: [[ENTRY:.*]]:
-; AVX2-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; AVX2-NEXT: [[STRIDE:%.*]] = load i64, ptr [[DIMS]], align 8
+; AVX2-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; AVX2-NEXT: [[OFF:%.*]] = load i64, ptr [[OFF_PTR]], align 8
-; AVX2-NEXT: [[TMP0:%.*]] = insertelement <4 x i64> poison, i64 [[OFF]], i64 0
-; AVX2-NEXT: [[TMP1:%.*]] = shufflevector <4 x i64> [[TMP0]], <4 x i64> poison, <4 x i32> zeroinitializer
-; AVX2-NEXT: [[TMP2:%.*]] = insertelement <4 x i64> poison, i64 [[STRIDE]], i64 0
-; AVX2-NEXT: [[TMP3:%.*]] = shufflevector <4 x i64> [[TMP2]], <4 x i64> poison, <4 x i32> zeroinitializer
; AVX2-NEXT: br label %[[LOOP:.*]]
; AVX2: [[LOOP]]:
; AVX2-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; AVX2-NEXT: [[ACC:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ACC_NEXT:%.*]], %[[LOOP]] ]
-; AVX2-NEXT: [[TMP4:%.*]] = insertelement <4 x i64> poison, i64 [[IV]], i64 0
-; AVX2-NEXT: [[TMP5:%.*]] = shufflevector <4 x i64> [[TMP4]], <4 x i64> poison, <4 x i32> zeroinitializer
-; AVX2-NEXT: [[TMP6:%.*]] = add <4 x i64> [[TMP5]], <i64 1, i64 2, i64 3, i64 4>
+; AVX2-NEXT: [[I1:%.*]] = add i64 [[IV]], 1
+; AVX2-NEXT: [[I2:%.*]] = add i64 [[IV]], 2
+; AVX2-NEXT: [[I3:%.*]] = add i64 [[IV]], 3
; AVX2-NEXT: [[IV_NEXT]] = add i64 [[IV]], 4
-; AVX2-NEXT: [[TMP7:%.*]] = mul <4 x i64> [[TMP3]], [[TMP6]]
-; AVX2-NEXT: [[TMP8:%.*]] = add <4 x i64> [[TMP1]], [[TMP7]]
-; AVX2-NEXT: [[TMP9:%.*]] = shl <4 x i64> [[TMP8]], splat (i64 2)
-; AVX2-NEXT: [[TMP10:%.*]] = extractelement <4 x i64> [[TMP9]], i64 0
-; AVX2-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP10]]
-; AVX2-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[TMP9]], i64 1
-; AVX2-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP11]]
-; AVX2-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[TMP9]], i64 2
-; AVX2-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP12]]
-; AVX2-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[TMP9]], i64 3
-; AVX2-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP13]]
+; AVX2-NEXT: [[M0:%.*]] = mul i64 [[STRIDE]], [[I1]]
+; AVX2-NEXT: [[M1:%.*]] = mul i64 [[STRIDE]], [[I2]]
+; AVX2-NEXT: [[M2:%.*]] = mul i64 [[STRIDE]], [[I3]]
+; AVX2-NEXT: [[M3:%.*]] = mul i64 [[STRIDE]], [[IV_NEXT]]
+; AVX2-NEXT: [[A0:%.*]] = add i64 [[OFF]], [[M0]]
+; AVX2-NEXT: [[A1:%.*]] = add i64 [[OFF]], [[M1]]
+; AVX2-NEXT: [[A2:%.*]] = add i64 [[OFF]], [[M2]]
+; AVX2-NEXT: [[A3:%.*]] = add i64 [[OFF]], [[M3]]
+; AVX2-NEXT: [[S0:%.*]] = shl i64 [[A0]], 2
+; AVX2-NEXT: [[S1:%.*]] = shl i64 [[A1]], 2
+; AVX2-NEXT: [[S2:%.*]] = shl i64 [[A2]], 2
+; AVX2-NEXT: [[S3:%.*]] = shl i64 [[A3]], 2
+; AVX2-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S0]]
+; AVX2-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S1]]
+; AVX2-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S2]]
+; AVX2-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S3]]
; AVX2-NEXT: [[L0:%.*]] = load i32, ptr [[P0]], align 4
; AVX2-NEXT: [[L1:%.*]] = load i32, ptr [[P1]], align 4
; AVX2-NEXT: [[L2:%.*]] = load i32, ptr [[P2]], align 4
@@ -139,32 +140,33 @@ define i32 @strided_gather_offset(ptr %base, ptr %dims, i64 %n) {
; CHECK-LABEL: define i32 @strided_gather_offset(
; CHECK-SAME: ptr [[BASE:%.*]], ptr [[DIMS:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[ENTRY:.*]]:
-; CHECK-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; CHECK-NEXT: [[STRIDE:%.*]] = load i64, ptr [[DIMS]], align 8
+; CHECK-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; CHECK-NEXT: [[OFF:%.*]] = load i64, ptr [[OFF_PTR]], align 8
-; CHECK-NEXT: [[TMP0:%.*]] = insertelement <4 x i64> poison, i64 [[OFF]], i64 0
-; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i64> [[TMP0]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP2:%.*]] = insertelement <4 x i64> poison, i64 [[STRIDE]], i64 0
-; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <4 x i64> [[TMP2]], <4 x i64> poison, <4 x i32> zeroinitializer
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; CHECK-NEXT: [[ACC:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ACC_NEXT:%.*]], %[[LOOP]] ]
-; CHECK-NEXT: [[TMP4:%.*]] = insertelement <4 x i64> poison, i64 [[IV]], i64 0
-; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <4 x i64> [[TMP4]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP6:%.*]] = add <4 x i64> [[TMP5]], <i64 1, i64 2, i64 3, i64 4>
+; CHECK-NEXT: [[I1:%.*]] = add i64 [[IV]], 1
+; CHECK-NEXT: [[I2:%.*]] = add i64 [[IV]], 2
+; CHECK-NEXT: [[I3:%.*]] = add i64 [[IV]], 3
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 4
-; CHECK-NEXT: [[TMP7:%.*]] = mul <4 x i64> [[TMP3]], [[TMP6]]
-; CHECK-NEXT: [[TMP8:%.*]] = add <4 x i64> [[TMP1]], [[TMP7]]
-; CHECK-NEXT: [[TMP9:%.*]] = shl <4 x i64> [[TMP8]], splat (i64 2)
-; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i64> [[TMP9]], i64 0
-; CHECK-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP10]]
-; CHECK-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[TMP9]], i64 1
-; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP11]]
-; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[TMP9]], i64 2
-; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP12]]
-; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[TMP9]], i64 3
-; CHECK-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP13]]
+; CHECK-NEXT: [[M0:%.*]] = mul i64 [[STRIDE]], [[I1]]
+; CHECK-NEXT: [[M1:%.*]] = mul i64 [[STRIDE]], [[I2]]
+; CHECK-NEXT: [[M2:%.*]] = mul i64 [[STRIDE]], [[I3]]
+; CHECK-NEXT: [[M3:%.*]] = mul i64 [[STRIDE]], [[IV_NEXT]]
+; CHECK-NEXT: [[A0:%.*]] = add i64 [[OFF]], [[M0]]
+; CHECK-NEXT: [[A1:%.*]] = add i64 [[OFF]], [[M1]]
+; CHECK-NEXT: [[A2:%.*]] = add i64 [[OFF]], [[M2]]
+; CHECK-NEXT: [[A3:%.*]] = add i64 [[OFF]], [[M3]]
+; CHECK-NEXT: [[S0:%.*]] = shl i64 [[A0]], 2
+; CHECK-NEXT: [[S1:%.*]] = shl i64 [[A1]], 2
+; CHECK-NEXT: [[S2:%.*]] = shl i64 [[A2]], 2
+; CHECK-NEXT: [[S3:%.*]] = shl i64 [[A3]], 2
+; CHECK-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S0]]
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S1]]
+; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S2]]
+; CHECK-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S3]]
; CHECK-NEXT: [[Q0:%.*]] = getelementptr i8, ptr [[P0]], i64 -4
; CHECK-NEXT: [[L0:%.*]] = load i32, ptr [[Q0]], align 4
; CHECK-NEXT: [[Q1:%.*]] = getelementptr i8, ptr [[P1]], i64 -4
@@ -240,31 +242,32 @@ define void @strided_update(ptr %base, ptr %dims, i64 %n) {
; CHECK-LABEL: define void @strided_update(
; CHECK-SAME: ptr [[BASE:%.*]], ptr [[DIMS:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: [[ENTRY:.*]]:
-; CHECK-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; CHECK-NEXT: [[STRIDE:%.*]] = load i64, ptr [[DIMS]], align 8
+; CHECK-NEXT: [[OFF_PTR:%.*]] = getelementptr i8, ptr [[DIMS]], i64 8
; CHECK-NEXT: [[OFF:%.*]] = load i64, ptr [[OFF_PTR]], align 8
-; CHECK-NEXT: [[TMP0:%.*]] = insertelement <4 x i64> poison, i64 [[OFF]], i64 0
-; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i64> [[TMP0]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP2:%.*]] = insertelement <4 x i64> poison, i64 [[STRIDE]], i64 0
-; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <4 x i64> [[TMP2]], <4 x i64> poison, <4 x i32> zeroinitializer
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
-; CHECK-NEXT: [[TMP4:%.*]] = insertelement <4 x i64> poison, i64 [[IV]], i64 0
-; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <4 x i64> [[TMP4]], <4 x i64> poison, <4 x i32> zeroinitializer
-; CHECK-NEXT: [[TMP6:%.*]] = add <4 x i64> [[TMP5]], <i64 1, i64 2, i64 3, i64 4>
+; CHECK-NEXT: [[I1:%.*]] = add i64 [[IV]], 1
+; CHECK-NEXT: [[I2:%.*]] = add i64 [[IV]], 2
+; CHECK-NEXT: [[I3:%.*]] = add i64 [[IV]], 3
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 4
-; CHECK-NEXT: [[TMP7:%.*]] = mul <4 x i64> [[TMP3]], [[TMP6]]
-; CHECK-NEXT: [[TMP8:%.*]] = add <4 x i64> [[TMP1]], [[TMP7]]
-; CHECK-NEXT: [[TMP9:%.*]] = shl <4 x i64> [[TMP8]], splat (i64 2)
-; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i64> [[TMP9]], i64 0
-; CHECK-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP10]]
-; CHECK-NEXT: [[TMP11:%.*]] = extractelement <4 x i64> [[TMP9]], i64 1
-; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP11]]
-; CHECK-NEXT: [[TMP12:%.*]] = extractelement <4 x i64> [[TMP9]], i64 2
-; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP12]]
-; CHECK-NEXT: [[TMP13:%.*]] = extractelement <4 x i64> [[TMP9]], i64 3
-; CHECK-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[TMP13]]
+; CHECK-NEXT: [[M0:%.*]] = mul i64 [[STRIDE]], [[I1]]
+; CHECK-NEXT: [[M1:%.*]] = mul i64 [[STRIDE]], [[I2]]
+; CHECK-NEXT: [[M2:%.*]] = mul i64 [[STRIDE]], [[I3]]
+; CHECK-NEXT: [[M3:%.*]] = mul i64 [[STRIDE]], [[IV_NEXT]]
+; CHECK-NEXT: [[A0:%.*]] = add i64 [[OFF]], [[M0]]
+; CHECK-NEXT: [[A1:%.*]] = add i64 [[OFF]], [[M1]]
+; CHECK-NEXT: [[A2:%.*]] = add i64 [[OFF]], [[M2]]
+; CHECK-NEXT: [[A3:%.*]] = add i64 [[OFF]], [[M3]]
+; CHECK-NEXT: [[S0:%.*]] = shl i64 [[A0]], 2
+; CHECK-NEXT: [[S1:%.*]] = shl i64 [[A1]], 2
+; CHECK-NEXT: [[S2:%.*]] = shl i64 [[A2]], 2
+; CHECK-NEXT: [[S3:%.*]] = shl i64 [[A3]], 2
+; CHECK-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S0]]
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S1]]
+; CHECK-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S2]]
+; CHECK-NEXT: [[P3:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[S3]]
; CHECK-NEXT: [[L0:%.*]] = load i32, ptr [[P0]], align 4
; CHECK-NEXT: [[U0:%.*]] = add i32 [[L0]], 1
; CHECK-NEXT: store i32 [[U0]], ptr [[P0]], align 4
@@ -560,37 +563,23 @@ define i32 @preheader_offsets(ptr %base, ptr %offs, i64 %n) {
; AVX2-LABEL: define i32 @preheader_offsets(
; AVX2-SAME: ptr [[BASE:%.*]], ptr [[OFFS:%.*]], i64 [[N:%.*]]) #[[ATTR0]] {
; AVX2-NEXT: [[ENTRY:.*]]:
-; AVX2-NEXT: [[X1_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 8
-; AVX2-NEXT: [[X2_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 16
-; AVX2-NEXT: [[X3_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 24
-; AVX2-NEXT: [[X0:%.*]] = load i64, ptr [[OFFS]], align 8
-; AVX2-NEXT: [[X1:%.*]] = load i64, ptr [[X1_PTR]], align 8
-; AVX2-NEXT: [[X2:%.*]] = load i64, ptr [[X2_PTR]], align 8
-; AVX2-NEXT: [[X3:%.*]] = load i64, ptr [[X3_PTR]], align 8
; AVX2-NEXT: [[Y0_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 32
-; AVX2-NEXT: [[Y1_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 40
-; AVX2-NEXT: [[Y2_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 48
-; AVX2-NEXT: [[Y3_PTR:%.*]] = getelementptr i8, ptr [[OFFS]], i64 56
-; AVX2-NEXT: [[Y0:%.*]] = load i64, ptr [[Y0_PTR]], align 8
-; AVX2-NEXT: [[Y1:%.*]] = load i64, ptr [[Y1_PTR]], align 8
-; AVX2-NEXT: [[Y2:%.*]] = load i64, ptr [[Y2_PTR]], align 8
-; AVX2-NEXT: [[Y3:%.*]] = load i64, ptr [[Y3_PTR]], align 8
-; AVX2-NEXT: [[T0:%.*]] = add i64 [[X0]], [[Y0]]
-; AVX2-NEXT: [[T1:%.*]] = add i64 [[X1]], [[Y1]]
-; AVX2-NEXT: [[T2:%.*]] = add i64 [[X2]], [[Y2]]
-; AVX2-NEXT: [[T3:%.*]] = add i64 [[X3]], [[Y3]]
-; AVX2-NEXT: [[O0:%.*]] = shl i64 [[T0]], 2
-; AVX2-NEXT: [[O1:%.*]] = shl i64 [[T1]], 2
-; AVX2-NEXT: [[O2:%.*]] = shl i64 [[T2]], 2
-; AVX2-NEXT: [[O3:%.*]] = shl i64 [[T3]], 2
+; AVX2-NEXT: [[TMP0:%.*]] = load <4 x i64>, ptr [[OFFS]], align 8
+; AVX2-NEXT: [[TMP1:%.*]] = load <4 x i64>, ptr [[Y0_PTR]], align 8
+; AVX2-NEXT: [[TMP2:%.*]] = add <4 x i64> [[TMP0]], [[TMP1]]
+; AVX2-NEXT: [[TMP3:%.*]] = shl <4 x i64> [[TMP2]], splat (i64 2)
+; AVX2-NEXT: [[TMP4:%.*]] = extractelement <4 x i64> [[TMP3]], i64 0
+; AVX2-NEXT: [[TMP5:%.*]] = extractelement <4 x i64> [[TMP3]], i64 1
+; AVX2-NEXT: [[TMP6:%.*]] = extractelement <4 x i64> [[TMP3]], i64 2
+; AVX2-NEXT: [[TMP7:%.*]] = extractelement <4 x i64> [[TMP3]], i64 3
; AVX2-NEXT: br label %[[LOOP:.*]]
; AVX2: [[LOOP]]:
; AVX2-NEXT: [[IV:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
; AVX2-NEXT: [[ACC:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[ACC_NEXT:%.*]], %[[LOOP]] ]
-; AVX2-NEXT: [[A0:%.*]] = add i64 [[IV]], [[O0]]
-; AVX2-NEXT: [[A1:%.*]] = add i64 [[IV]], [[O1]]
-; AVX2-NEXT: [[A2:%.*]] = add i64 [[IV]], [[O2]]
-; AVX2-NEXT: [[A3:%.*]] = add i64 [[IV]], [[O3]]
+; AVX2-NEXT: [[A0:%.*]] = add i64 [[IV]], [[TMP4]]
+; AVX2-NEXT: [[A1:%.*]] = add i64 [[IV]], [[TMP5]]
+; AVX2-NEXT: [[A2:%.*]] = add i64 [[IV]], [[TMP6]]
+; AVX2-NEXT: [[A3:%.*]] = add i64 [[IV]], [[TMP7]]
; AVX2-NEXT: [[P0:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[A0]]
; AVX2-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[A1]]
; AVX2-NEXT: [[P2:%.*]] = getelementptr i8, ptr [[BASE]], i64 [[A2]]
>From 0d6d6549f15d6d2eacc04028e8da0af0e07dce1e Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Thu, 24 Sep 2026 20:40:14 +0200
Subject: [PATCH 2/5] Address review comments
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 86 ++++---------------
.../Vectorize/SLPVectorizer/SLPUtils.cpp | 86 +++++++++++++++++++
.../Vectorize/SLPVectorizer/SLPUtils.h | 17 ++++
3 files changed, 121 insertions(+), 68 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index e98b5d62719a72..922d6ff0bafdd3 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -29400,6 +29400,9 @@ bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
!R.isScalarFallbackBlock(BB);
})) {
R.clearReductionData();
+ // GEPs is collected per block and is also used to keep its index
+ // computations out of the standalone-seed attempt.
+ collectSeedInstructions(BB);
Changed |= vectorizeOnceUsedSeeds(BB, R);
}
}
@@ -30521,71 +30524,6 @@ void SLPVectorizerPass::collectSeedInstructions(BasicBlock *BB) {
}
}
-/// Returns true if \p Ptr is only used by scalar loads and stores, directly or
-/// through getelementptrs with constant offsets.
-static bool onlyFeedsScalarAccesses(const Value *Ptr, unsigned Depth = 0) {
- constexpr unsigned MaxDepth = 2;
- return !Ptr->use_empty() && all_of(Ptr->users(), [&](const User *U) {
- if (const auto *Load = dyn_cast<LoadInst>(U))
- return !Load->getType()->isVectorTy();
- if (const auto *Store = dyn_cast<StoreInst>(U))
- return Store->getPointerOperand() == Ptr &&
- !Store->getValueOperand()->getType()->isVectorTy();
- if (const auto *GEP = dyn_cast<GetElementPtrInst>(U))
- return Depth < MaxDepth && GEP->getPointerOperand() == Ptr &&
- GEP->hasAllConstantIndices() &&
- onlyFeedsScalarAccesses(GEP, Depth + 1);
- return false;
- });
-}
-
-/// Leave related affine address recurrences feeding scalar accesses for loop
-/// strength reduction. Vectorizing their indices can retain expensive
-/// arithmetic and require an extract for each lane instead of a scalar pointer
-/// increment.
-static bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL,
- ScalarEvolution &SE, LoopInfo &LI) {
- constexpr unsigned MaxIndexChainLength = 3;
- const Loop *L = nullptr;
- SmallVector<GetElementPtrInst *> GEPs;
- for (Value *V : VL) {
- Value *Cur = V;
- GetElementPtrInst *GEP = nullptr;
- for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
- if (!isa<BinaryOperator, CastInst>(Cur) || !Cur->hasOneUse())
- return false;
- User *U = Cur->user_back();
- if ((GEP = dyn_cast<GetElementPtrInst>(U)))
- break;
- Cur = U;
- }
- if (!GEP || GEP->getPointerOperand() == Cur ||
- !onlyFeedsScalarAccesses(GEP))
- return false;
- // LSR only removes the arithmetic computed in the loop itself.
- const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
- if (!GEPLoop || (L && L != GEPLoop) ||
- LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
- return false;
- L = GEPLoop;
- GEPs.push_back(GEP);
- }
- const SCEV *FirstAddr = nullptr;
- for (GetElementPtrInst *GEP : GEPs) {
- const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
- if (!Addr || Addr->getLoop() != L || !Addr->isAffine())
- return false;
- if (!FirstAddr) {
- FirstAddr = Addr;
- continue;
- }
- const SCEV *Diff = SE.getMinusSCEV(Addr, FirstAddr);
- if (isa<SCEVCouldNotCompute>(Diff) || !SE.isLoopInvariant(Diff, L))
- return false;
- }
- return true;
-}
-
bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
bool MaxVFOnly,
bool StandaloneSeeds) {
@@ -30727,9 +30665,15 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
continue;
}
- if (isStrengthReducibleIndexBundle(Ops, *SE, *LI)) {
- LLVM_DEBUG(dbgs() << "SLP: Not vectorizing strength-reducible address "
- "computations.\n");
+ auto IsCollectedGEP = [&](GetElementPtrInst *GEP) {
+ auto It = GEPs.find(GEP->getPointerOperand());
+ return It != GEPs.end() && It->second.size() >= 2 &&
+ is_contained(It->second, GEP);
+ };
+ if (StandaloneSeeds &&
+ isGEPCandidateIndexBundle(Ops, *LI, SLPReVec, IsCollectedGEP)) {
+ LLVM_DEBUG(dbgs() << "SLP: Leaving collected GEP index computations "
+ "to vectorizeGEPIndices.\n");
continue;
}
@@ -36573,6 +36517,12 @@ bool SLPVectorizerPass::vectorizeGEPIndices(BasicBlock *BB, BoUpSLP &R) {
Bundle[BundleIndex++] = GEPIdx;
}
+ if (isStrengthReducibleIndexBundle(Bundle, *SE, *LI, SLPReVec)) {
+ LLVM_DEBUG(dbgs() << "SLP: Not vectorizing strength-reducible address "
+ "computations.\n");
+ continue;
+ }
+
// Try and vectorize the indices. We are currently only interested in
// gather-like cases of the form:
//
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
index feac4d031c72cc..e4b8fff3a6730b 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
@@ -7,12 +7,16 @@
//===----------------------------------------------------------------------===//
#include "SLPUtils.h"
+#include "SLPTypeUtils.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/IR/Constants.h"
@@ -1089,6 +1093,88 @@ bool isOnceUsedSeed(const Instruction *I) {
I);
}
+/// Returns true if \p Ptr is only used by scalar loads and stores, directly or
+/// through getelementptrs with constant offsets.
+static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
+ unsigned Depth = 0) {
+ constexpr unsigned MaxDepth = 2;
+ if (Ptr->use_empty() || Ptr->hasNUsesOrMore(UsesLimit))
+ return false;
+ return all_of(Ptr->users(), [&](User *U) {
+ if (isa<LoadInst>(U))
+ return !getValueType(U, ReVec)->isVectorTy();
+ if (auto *Store = dyn_cast<StoreInst>(U))
+ return Store->getPointerOperand() == Ptr &&
+ !getValueType(Store, ReVec)->isVectorTy();
+ if (auto *GEP = dyn_cast<GetElementPtrInst>(U))
+ return Depth < MaxDepth && GEP->getPointerOperand() == Ptr &&
+ GEP->hasAllConstantIndices() &&
+ onlyFeedsScalarAccesses(GEP, ReVec, Depth + 1);
+ return false;
+ });
+}
+
+static bool collectScalarAccessGEPs(ArrayRef<Value *> VL, LoopInfo &LI,
+ bool ReVec,
+ SmallVectorImpl<GetElementPtrInst *> &GEPs,
+ const Loop *&L) {
+ constexpr unsigned MaxIndexChainLength = 3;
+ for (Value *V : VL) {
+ Value *Cur = V;
+ GetElementPtrInst *GEP = nullptr;
+ for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
+ if (!isa<BinaryOperator, CastInst>(Cur) || !Cur->hasOneUse())
+ return false;
+ User *U = Cur->user_back();
+ if ((GEP = dyn_cast<GetElementPtrInst>(U)))
+ break;
+ Cur = U;
+ }
+ if (!GEP || GEP->getPointerOperand() == Cur ||
+ !onlyFeedsScalarAccesses(GEP, ReVec))
+ return false;
+ // LSR only removes the arithmetic computed in the loop itself.
+ const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
+ if (!GEPLoop || (L && L != GEPLoop) ||
+ LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
+ return false;
+ L = GEPLoop;
+ GEPs.push_back(GEP);
+ }
+ return true;
+}
+
+bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
+ LoopInfo &LI, bool ReVec) {
+ const Loop *L = nullptr;
+ SmallVector<GetElementPtrInst *> GEPs;
+ if (!collectScalarAccessGEPs(VL, LI, ReVec, GEPs, L))
+ return false;
+ const SCEV *FirstAddr = nullptr;
+ for (GetElementPtrInst *GEP : GEPs) {
+ const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
+ if (!Addr || Addr->getLoop() != L || !Addr->isAffine())
+ return false;
+ if (!FirstAddr) {
+ FirstAddr = Addr;
+ continue;
+ }
+ const SCEV *Diff = SE.getMinusSCEV(Addr, FirstAddr);
+ if (isa<SCEVCouldNotCompute>(Diff) || !SE.isLoopInvariant(Diff, L))
+ return false;
+ }
+ return true;
+}
+
+bool isGEPCandidateIndexBundle(
+ ArrayRef<Value *> VL, LoopInfo &LI, bool ReVec,
+ function_ref<bool(GetElementPtrInst *)> IsCandidate) {
+ const Loop *L = nullptr;
+ SmallVector<GetElementPtrInst *> GEPs;
+ return collectScalarAccessGEPs(VL, LI, ReVec, GEPs, L) &&
+ all_of(GEPs, IsCandidate);
+}
+
Instruction *lookThroughCastRoundTrip(Value *V, bool MustBeElidable) {
auto *Wide = dyn_cast<FPExtInst>(V);
if (!Wide || !Wide->hasOneUse())
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
index 6d2888e868c6de..73342eb3e698b2 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
@@ -36,8 +36,11 @@ namespace llvm {
class AssumptionCache;
class Constant;
class DataLayout;
+class GetElementPtrInst;
class Instruction;
class IRBuilderBase;
+class LoopInfo;
+class ScalarEvolution;
class TargetLibraryInfo;
class Type;
class Value;
@@ -412,6 +415,20 @@ Intrinsic::ID getMaskedDivRemIntrinsic(unsigned Opcode);
/// dedicated attempt.
bool isOnceUsedSeed(const Instruction *I);
+/// Leave related affine address recurrences feeding scalar accesses for loop
+/// strength reduction. Vectorizing their indices can retain expensive
+/// arithmetic and require an extract for each lane instead of a scalar pointer
+/// increment.
+bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
+ LoopInfo &LI, bool ReVec);
+
+/// Returns true if each value in \p VL is an in-loop index computation ending
+/// at a getelementptr accepted by \p IsCandidate and used only by scalar
+/// accesses.
+bool isGEPCandidateIndexBundle(
+ ArrayRef<Value *> VL, LoopInfo &LI, bool ReVec,
+ function_ref<bool(GetElementPtrInst *)> IsCandidate);
+
/// If \p V is a single-use fpext of a single-use fptrunc forming a round-trip
/// back to the type of \p V, returns the fptrunc; the round-trip source is its
/// operand, always an instruction of the same type as \p V. If
>From 93c9296928790200295c2a6c59362434d3588575 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Fri, 25 Sep 2026 09:25:15 +0200
Subject: [PATCH 3/5] Address review comments (round 2)
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 19 +++----
.../Vectorize/SLPVectorizer/SLPUtils.cpp | 57 ++++++++++++-------
.../Vectorize/SLPVectorizer/SLPUtils.h | 8 +--
3 files changed, 46 insertions(+), 38 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 922d6ff0bafdd3..d3948e8979db7d 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -30665,18 +30665,6 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
continue;
}
- auto IsCollectedGEP = [&](GetElementPtrInst *GEP) {
- auto It = GEPs.find(GEP->getPointerOperand());
- return It != GEPs.end() && It->second.size() >= 2 &&
- is_contained(It->second, GEP);
- };
- if (StandaloneSeeds &&
- isGEPCandidateIndexBundle(Ops, *LI, SLPReVec, IsCollectedGEP)) {
- LLVM_DEBUG(dbgs() << "SLP: Leaving collected GEP index computations "
- "to vectorizeGEPIndices.\n");
- continue;
- }
-
LLVM_DEBUG(dbgs() << "SLP: Analyzing " << ActualVF << " operations "
<< "\n");
@@ -36401,6 +36389,13 @@ bool SLPVectorizerPass::vectorizeOnceUsedSeeds(BasicBlock *BB, BoUpSLP &R) {
isOnceUsedSeed(&I) && !isNonVectorizableInst(&I, TLI) &&
!R.hasResolvedUser(&I);
})) {
+ // Index chains of collected GEPs are handled by vectorizeGEPIndices.
+ if (isGEPCandidateIndexBundle({&I}, *SE, *LI, SLPReVec, [&](auto *GEP) {
+ auto It = GEPs.find(GEP->getPointerOperand());
+ return It != GEPs.end() && It->second.size() >= 2 &&
+ is_contained(It->second, GEP);
+ }))
+ continue;
// The poor-throughput ops are seeded on their own, with the different
// grouping.
if (VectorizePoorThroughput &&
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
index e4b8fff3a6730b..b4102bd621de01 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
@@ -1101,11 +1101,12 @@ static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
if (Ptr->use_empty() || Ptr->hasNUsesOrMore(UsesLimit))
return false;
return all_of(Ptr->users(), [&](User *U) {
- if (isa<LoadInst>(U))
- return !getValueType(U, ReVec)->isVectorTy();
- if (auto *Store = dyn_cast<StoreInst>(U))
- return Store->getPointerOperand() == Ptr &&
- !getValueType(Store, ReVec)->isVectorTy();
+ if (getValueType(U, ReVec)->isVectorTy())
+ return false;
+ Value *Op;
+ if (isa<LoadInst>(U) ||
+ (match(U, m_Store(m_Value(Op), m_Specific(Ptr))) && Op != Ptr))
+ return true;
if (auto *GEP = dyn_cast<GetElementPtrInst>(U))
return Depth < MaxDepth && GEP->getPointerOperand() == Ptr &&
GEP->hasAllConstantIndices() &&
@@ -1114,17 +1115,20 @@ static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
});
}
-static bool collectScalarAccessGEPs(ArrayRef<Value *> VL, LoopInfo &LI,
- bool ReVec,
- SmallVectorImpl<GetElementPtrInst *> &GEPs,
- const Loop *&L) {
+/// Collects in \p GEPs the getelementptrs that the in-loop index computations
+/// \p VL end at, if they are only used by scalar accesses. Returns the loop
+/// containing all of them, or nullptr otherwise.
+static const Loop *
+collectScalarAccessGEPs(ArrayRef<Value *> VL, const LoopInfo &LI, bool ReVec,
+ SmallVectorImpl<GetElementPtrInst *> &GEPs) {
constexpr unsigned MaxIndexChainLength = 3;
+ const Loop *L = nullptr;
for (Value *V : VL) {
Value *Cur = V;
GetElementPtrInst *GEP = nullptr;
for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
if (!isa<BinaryOperator, CastInst>(Cur) || !Cur->hasOneUse())
- return false;
+ return nullptr;
User *U = Cur->user_back();
if ((GEP = dyn_cast<GetElementPtrInst>(U)))
break;
@@ -1132,28 +1136,36 @@ static bool collectScalarAccessGEPs(ArrayRef<Value *> VL, LoopInfo &LI,
}
if (!GEP || GEP->getPointerOperand() == Cur ||
!onlyFeedsScalarAccesses(GEP, ReVec))
- return false;
+ return nullptr;
// LSR only removes the arithmetic computed in the loop itself.
const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
if (!GEPLoop || (L && L != GEPLoop) ||
LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
- return false;
+ return nullptr;
L = GEPLoop;
GEPs.push_back(GEP);
}
- return true;
+ return L;
+}
+
+/// Returns the address computed by \p GEP if it is an affine recurrence of
+/// \p L, or nullptr otherwise.
+static const SCEVAddRecExpr *
+getAffineAddress(GetElementPtrInst *GEP, const Loop *L, ScalarEvolution &SE) {
+ const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
+ return Addr && Addr->getLoop() == L && Addr->isAffine() ? Addr : nullptr;
}
bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
- LoopInfo &LI, bool ReVec) {
- const Loop *L = nullptr;
+ const LoopInfo &LI, bool ReVec) {
SmallVector<GetElementPtrInst *> GEPs;
- if (!collectScalarAccessGEPs(VL, LI, ReVec, GEPs, L))
+ const Loop *L = collectScalarAccessGEPs(VL, LI, ReVec, GEPs);
+ if (!L)
return false;
const SCEV *FirstAddr = nullptr;
for (GetElementPtrInst *GEP : GEPs) {
- const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
- if (!Addr || Addr->getLoop() != L || !Addr->isAffine())
+ const SCEV *Addr = getAffineAddress(GEP, L, SE);
+ if (!Addr)
return false;
if (!FirstAddr) {
FirstAddr = Addr;
@@ -1167,12 +1179,13 @@ bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
}
bool isGEPCandidateIndexBundle(
- ArrayRef<Value *> VL, LoopInfo &LI, bool ReVec,
+ ArrayRef<Value *> VL, ScalarEvolution &SE, const LoopInfo &LI, bool ReVec,
function_ref<bool(GetElementPtrInst *)> IsCandidate) {
- const Loop *L = nullptr;
SmallVector<GetElementPtrInst *> GEPs;
- return collectScalarAccessGEPs(VL, LI, ReVec, GEPs, L) &&
- all_of(GEPs, IsCandidate);
+ const Loop *L = collectScalarAccessGEPs(VL, LI, ReVec, GEPs);
+ return L && all_of(GEPs, [&](GetElementPtrInst *GEP) {
+ return IsCandidate(GEP) && getAffineAddress(GEP, L, SE);
+ });
}
Instruction *lookThroughCastRoundTrip(Value *V, bool MustBeElidable) {
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
index 73342eb3e698b2..07ba571634b82f 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
@@ -420,13 +420,13 @@ bool isOnceUsedSeed(const Instruction *I);
/// arithmetic and require an extract for each lane instead of a scalar pointer
/// increment.
bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
- LoopInfo &LI, bool ReVec);
+ const LoopInfo &LI, bool ReVec);
/// Returns true if each value in \p VL is an in-loop index computation ending
-/// at a getelementptr accepted by \p IsCandidate and used only by scalar
-/// accesses.
+/// at a getelementptr accepted by \p IsCandidate, used only by scalar accesses
+/// and computing an affine recurrence of that loop.
bool isGEPCandidateIndexBundle(
- ArrayRef<Value *> VL, LoopInfo &LI, bool ReVec,
+ ArrayRef<Value *> VL, ScalarEvolution &SE, const LoopInfo &LI, bool ReVec,
function_ref<bool(GetElementPtrInst *)> IsCandidate);
/// If \p V is a single-use fpext of a single-use fptrunc forming a round-trip
>From 28361ffd0157c0dc74ce0cb8336e9ce594a821b5 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Mon, 28 Sep 2026 09:38:22 +0200
Subject: [PATCH 4/5] Address review comments (round 3)
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 2 +-
.../Vectorize/SLPVectorizer/SLPUtils.cpp | 87 +++++++------------
.../Vectorize/SLPVectorizer/SLPUtils.h | 10 +--
3 files changed, 36 insertions(+), 63 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index d3948e8979db7d..c9b9b8b6119129 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -36390,7 +36390,7 @@ bool SLPVectorizerPass::vectorizeOnceUsedSeeds(BasicBlock *BB, BoUpSLP &R) {
!R.hasResolvedUser(&I);
})) {
// Index chains of collected GEPs are handled by vectorizeGEPIndices.
- if (isGEPCandidateIndexBundle({&I}, *SE, *LI, SLPReVec, [&](auto *GEP) {
+ if (!GEPs.empty() && isGEPCandidateIndex(&I, [&](auto *GEP) {
auto It = GEPs.find(GEP->getPointerOperand());
return It != GEPs.end() && It->second.size() >= 2 &&
is_contained(It->second, GEP);
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
index b4102bd621de01..0f048ae1139ca1 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
@@ -1094,10 +1094,9 @@ bool isOnceUsedSeed(const Instruction *I) {
}
/// Returns true if \p Ptr is only used by scalar loads and stores, directly or
-/// through getelementptrs with constant offsets.
+/// through at most \p Depth levels of getelementptrs with constant offsets.
static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
- unsigned Depth = 0) {
- constexpr unsigned MaxDepth = 2;
+ unsigned Depth = 2) {
if (Ptr->use_empty() || Ptr->hasNUsesOrMore(UsesLimit))
return false;
return all_of(Ptr->users(), [&](User *U) {
@@ -1108,64 +1107,44 @@ static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
(match(U, m_Store(m_Value(Op), m_Specific(Ptr))) && Op != Ptr))
return true;
if (auto *GEP = dyn_cast<GetElementPtrInst>(U))
- return Depth < MaxDepth && GEP->getPointerOperand() == Ptr &&
+ return Depth > 0 && GEP->getPointerOperand() == Ptr &&
GEP->hasAllConstantIndices() &&
- onlyFeedsScalarAccesses(GEP, ReVec, Depth + 1);
+ onlyFeedsScalarAccesses(GEP, ReVec, Depth - 1);
return false;
});
}
-/// Collects in \p GEPs the getelementptrs that the in-loop index computations
-/// \p VL end at, if they are only used by scalar accesses. Returns the loop
-/// containing all of them, or nullptr otherwise.
-static const Loop *
-collectScalarAccessGEPs(ArrayRef<Value *> VL, const LoopInfo &LI, bool ReVec,
- SmallVectorImpl<GetElementPtrInst *> &GEPs) {
+/// Returns the getelementptr whose index is computed by the short chain of
+/// single-use arithmetic starting at \p V, or nullptr otherwise.
+static GetElementPtrInst *getIndexChainGEP(Value *V) {
constexpr unsigned MaxIndexChainLength = 3;
- const Loop *L = nullptr;
- for (Value *V : VL) {
- Value *Cur = V;
- GetElementPtrInst *GEP = nullptr;
- for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
- if (!isa<BinaryOperator, CastInst>(Cur) || !Cur->hasOneUse())
- return nullptr;
- User *U = Cur->user_back();
- if ((GEP = dyn_cast<GetElementPtrInst>(U)))
- break;
- Cur = U;
- }
- if (!GEP || GEP->getPointerOperand() == Cur ||
- !onlyFeedsScalarAccesses(GEP, ReVec))
- return nullptr;
- // LSR only removes the arithmetic computed in the loop itself.
- const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
- if (!GEPLoop || (L && L != GEPLoop) ||
- LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
+ for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
+ if (!isa<BinaryOperator, CastInst>(V) || !V->hasOneUse())
return nullptr;
- L = GEPLoop;
- GEPs.push_back(GEP);
+ User *U = V->user_back();
+ if (auto *GEP = dyn_cast<GetElementPtrInst>(U))
+ return GEP->getPointerOperand() != V ? GEP : nullptr;
+ V = U;
}
- return L;
-}
-
-/// Returns the address computed by \p GEP if it is an affine recurrence of
-/// \p L, or nullptr otherwise.
-static const SCEVAddRecExpr *
-getAffineAddress(GetElementPtrInst *GEP, const Loop *L, ScalarEvolution &SE) {
- const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
- return Addr && Addr->getLoop() == L && Addr->isAffine() ? Addr : nullptr;
+ return nullptr;
}
bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
const LoopInfo &LI, bool ReVec) {
- SmallVector<GetElementPtrInst *> GEPs;
- const Loop *L = collectScalarAccessGEPs(VL, LI, ReVec, GEPs);
- if (!L)
- return false;
+ const Loop *L = nullptr;
const SCEV *FirstAddr = nullptr;
- for (GetElementPtrInst *GEP : GEPs) {
- const SCEV *Addr = getAffineAddress(GEP, L, SE);
- if (!Addr)
+ for (Value *V : VL) {
+ GetElementPtrInst *GEP = getIndexChainGEP(V);
+ if (!GEP || !onlyFeedsScalarAccesses(GEP, ReVec))
+ return false;
+ // LSR only removes the arithmetic computed in the loop itself.
+ const Loop *GEPLoop = LI.getLoopFor(GEP->getParent());
+ if (!GEPLoop || (L && L != GEPLoop) ||
+ LI.getLoopFor(cast<Instruction>(V)->getParent()) != GEPLoop)
+ return false;
+ L = GEPLoop;
+ const auto *Addr = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(GEP));
+ if (!Addr || Addr->getLoop() != L || !Addr->isAffine())
return false;
if (!FirstAddr) {
FirstAddr = Addr;
@@ -1178,14 +1157,10 @@ bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
return true;
}
-bool isGEPCandidateIndexBundle(
- ArrayRef<Value *> VL, ScalarEvolution &SE, const LoopInfo &LI, bool ReVec,
- function_ref<bool(GetElementPtrInst *)> IsCandidate) {
- SmallVector<GetElementPtrInst *> GEPs;
- const Loop *L = collectScalarAccessGEPs(VL, LI, ReVec, GEPs);
- return L && all_of(GEPs, [&](GetElementPtrInst *GEP) {
- return IsCandidate(GEP) && getAffineAddress(GEP, L, SE);
- });
+bool isGEPCandidateIndex(Instruction *I,
+ function_ref<bool(GetElementPtrInst *)> IsCandidate) {
+ GetElementPtrInst *GEP = getIndexChainGEP(I);
+ return GEP && IsCandidate(GEP);
}
Instruction *lookThroughCastRoundTrip(Value *V, bool MustBeElidable) {
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
index 07ba571634b82f..31f134d27677fb 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
@@ -422,12 +422,10 @@ bool isOnceUsedSeed(const Instruction *I);
bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
const LoopInfo &LI, bool ReVec);
-/// Returns true if each value in \p VL is an in-loop index computation ending
-/// at a getelementptr accepted by \p IsCandidate, used only by scalar accesses
-/// and computing an affine recurrence of that loop.
-bool isGEPCandidateIndexBundle(
- ArrayRef<Value *> VL, ScalarEvolution &SE, const LoopInfo &LI, bool ReVec,
- function_ref<bool(GetElementPtrInst *)> IsCandidate);
+/// Returns true if \p I starts a short chain of single-use arithmetic that
+/// computes the index of a getelementptr accepted by \p IsCandidate.
+bool isGEPCandidateIndex(Instruction *I,
+ function_ref<bool(GetElementPtrInst *)> IsCandidate);
/// If \p V is a single-use fpext of a single-use fptrunc forming a round-trip
/// back to the type of \p V, returns the fptrunc; the round-trip source is its
>From 97d7d9118b5e3afca24fa443a371326cc31de636 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Tue, 6 Oct 2026 12:11:05 +0200
Subject: [PATCH 5/5] Address review comments (round 4)
Save the GEP index candidates of the first pass instead of collecting
the seeds of every block again for the once-used seed attempt, and make
the index chain length a default argument.
---
.../llvm/Transforms/Vectorize/SLPVectorizer.h | 5 +++++
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 16 ++++++++--------
.../Vectorize/SLPVectorizer/SLPUtils.cpp | 11 +----------
.../Vectorize/SLPVectorizer/SLPUtils.h | 7 +++----
4 files changed, 17 insertions(+), 22 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Vectorize/SLPVectorizer.h b/llvm/include/llvm/Transforms/Vectorize/SLPVectorizer.h
index 3d8ab0d1b75736..d83eb6a8dfe13c 100644
--- a/llvm/include/llvm/Transforms/Vectorize/SLPVectorizer.h
+++ b/llvm/include/llvm/Transforms/Vectorize/SLPVectorizer.h
@@ -21,6 +21,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/PassManager.h"
@@ -210,6 +211,10 @@ struct SLPVectorizerPass : public OptionalPassInfoMixin<SLPVectorizerPass> {
/// The getelementptr instructions in a basic block organized by base pointer.
GEPListMap GEPs;
+
+ /// The getelementptr instructions of the function whose indices are
+ /// candidates for vectorizeGEPIndices.
+ SmallPtrSet<GetElementPtrInst *, 16> IndexedGEPs;
};
} // end namespace llvm
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index c9b9b8b6119129..ef82c62e801605 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -29315,6 +29315,7 @@ bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
Stores.clear();
GEPs.clear();
+ IndexedGEPs.clear();
bool Changed = false;
// If the target claims to have no vector registers don't attempt
@@ -29356,6 +29357,11 @@ bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
// Start new block - clear the list of reduction roots.
R.clearReductionData();
collectSeedInstructions(BB);
+ // Saved to keep their index computations out of the once-used seed attempt.
+ if (VectorizeOnceUsed)
+ for (const GEPList &List : make_second_range(GEPs))
+ if (List.size() >= 2)
+ IndexedGEPs.insert_range(List);
// Vectorize trees that end at stores.
if (!Stores.empty()) {
@@ -29400,9 +29406,6 @@ bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
!R.isScalarFallbackBlock(BB);
})) {
R.clearReductionData();
- // GEPs is collected per block and is also used to keep its index
- // computations out of the standalone-seed attempt.
- collectSeedInstructions(BB);
Changed |= vectorizeOnceUsedSeeds(BB, R);
}
}
@@ -36390,11 +36393,8 @@ bool SLPVectorizerPass::vectorizeOnceUsedSeeds(BasicBlock *BB, BoUpSLP &R) {
!R.hasResolvedUser(&I);
})) {
// Index chains of collected GEPs are handled by vectorizeGEPIndices.
- if (!GEPs.empty() && isGEPCandidateIndex(&I, [&](auto *GEP) {
- auto It = GEPs.find(GEP->getPointerOperand());
- return It != GEPs.end() && It->second.size() >= 2 &&
- is_contained(It->second, GEP);
- }))
+ if (GetElementPtrInst *GEP = getIndexChainGEP(&I);
+ GEP && GEP->getParent() == BB && IndexedGEPs.contains(GEP))
continue;
// The poor-throughput ops are seeded on their own, with the different
// grouping.
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
index 0f048ae1139ca1..b24313b64173fb 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.cpp
@@ -1114,10 +1114,7 @@ static bool onlyFeedsScalarAccesses(Value *Ptr, bool ReVec,
});
}
-/// Returns the getelementptr whose index is computed by the short chain of
-/// single-use arithmetic starting at \p V, or nullptr otherwise.
-static GetElementPtrInst *getIndexChainGEP(Value *V) {
- constexpr unsigned MaxIndexChainLength = 3;
+GetElementPtrInst *getIndexChainGEP(Value *V, unsigned MaxIndexChainLength) {
for ([[maybe_unused]] unsigned _ : seq<unsigned>(MaxIndexChainLength)) {
if (!isa<BinaryOperator, CastInst>(V) || !V->hasOneUse())
return nullptr;
@@ -1157,12 +1154,6 @@ bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
return true;
}
-bool isGEPCandidateIndex(Instruction *I,
- function_ref<bool(GetElementPtrInst *)> IsCandidate) {
- GetElementPtrInst *GEP = getIndexChainGEP(I);
- return GEP && IsCandidate(GEP);
-}
-
Instruction *lookThroughCastRoundTrip(Value *V, bool MustBeElidable) {
auto *Wide = dyn_cast<FPExtInst>(V);
if (!Wide || !Wide->hasOneUse())
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
index 31f134d27677fb..019113cc7bf49a 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPUtils.h
@@ -422,10 +422,9 @@ bool isOnceUsedSeed(const Instruction *I);
bool isStrengthReducibleIndexBundle(ArrayRef<Value *> VL, ScalarEvolution &SE,
const LoopInfo &LI, bool ReVec);
-/// Returns true if \p I starts a short chain of single-use arithmetic that
-/// computes the index of a getelementptr accepted by \p IsCandidate.
-bool isGEPCandidateIndex(Instruction *I,
- function_ref<bool(GetElementPtrInst *)> IsCandidate);
+/// Returns the getelementptr whose index is computed by the short chain of
+/// single-use arithmetic starting at \p V, or nullptr otherwise.
+GetElementPtrInst *getIndexChainGEP(Value *V, unsigned MaxIndexChainLength = 3);
/// If \p V is a single-use fpext of a single-use fptrunc forming a round-trip
/// back to the type of \p V, returns the fptrunc; the round-trip source is its
More information about the llvm-commits
mailing list