[llvm] [SLP] Avoid seeding related affine loop address computations (PR #226220)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 11:02:42 PDT 2026


================
@@ -30041,6 +30041,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 ||
----------------
alexey-bataev wrote:

That was the question for Cur, but I see now that you're checking for it

https://github.com/llvm/llvm-project/pull/226220


More information about the llvm-commits mailing list