[llvm] 2d8bcb5 - [VPlan] Lift isUsedByLoadStoreAddr into vputils, operate on VPValue(NFC) (#196415)
via llvm-commits
llvm-commits at lists.llvm.org
Sat May 9 12:16:05 PDT 2026
Author: Florian Hahn
Date: 2026-05-09T21:16:01+02:00
New Revision: 2d8bcb5fab244bc263e2450857788ef1a5c2b9c1
URL: https://github.com/llvm/llvm-project/commit/2d8bcb5fab244bc263e2450857788ef1a5c2b9c1
DIFF: https://github.com/llvm/llvm-project/commit/2d8bcb5fab244bc263e2450857788ef1a5c2b9c1.diff
LOG: [VPlan] Lift isUsedByLoadStoreAddr into vputils, operate on VPValue(NFC) (#196415)
Extract the helper previously scoped to VPReplicateRecipe::computeCost
and make it available from VPlanUtils so other transforms can query
whether a VPValue is used as part of another load or store's address.
Also relax the input type from VPUser * to VPValue *: the worklist now
tracks VPValues directly, and traversal is gated on the user being a
VPSingleDefRecipe before walking its own users. This is NFC for the
existing caller.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
llvm/lib/Transforms/Vectorize/VPlanUtils.h
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index d04b5edcfc212..11a91dcd46867 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -3347,56 +3347,6 @@ static const SCEV *getAddressAccessSCEV(const VPValue *Ptr,
return vputils::isAddressSCEVForCost(Addr, *PSE.getSE(), L) ? Addr : nullptr;
}
-/// Returns true if \p V is used as part of the address of another load or
-/// store.
-static bool isUsedByLoadStoreAddress(const VPUser *V) {
- SmallPtrSet<const VPUser *, 4> Seen;
- SmallVector<const VPUser *> WorkList = {V};
-
- while (!WorkList.empty()) {
- auto *Cur = dyn_cast<VPSingleDefRecipe>(WorkList.pop_back_val());
- if (!Cur || !Seen.insert(Cur).second)
- continue;
-
- auto *Blend = dyn_cast<VPBlendRecipe>(Cur);
- // Skip blends that use V only through a compare by checking if any incoming
- // value was already visited.
- if (Blend && none_of(seq<unsigned>(0, Blend->getNumIncomingValues()),
- [&](unsigned I) {
- return Seen.contains(
- Blend->getIncomingValue(I)->getDefiningRecipe());
- }))
- continue;
-
- for (VPUser *U : Cur->users()) {
- if (auto *InterleaveR = dyn_cast<VPInterleaveBase>(U))
- if (InterleaveR->getAddr() == Cur)
- return true;
- if (auto *RepR = dyn_cast<VPReplicateRecipe>(U)) {
- if (RepR->getOpcode() == Instruction::Load &&
- RepR->getOperand(0) == Cur)
- return true;
- if (RepR->getOpcode() == Instruction::Store &&
- RepR->getOperand(1) == Cur)
- return true;
- }
- if (auto *MemR = dyn_cast<VPWidenMemoryRecipe>(U)) {
- if (MemR->getAddr() == Cur && MemR->isConsecutive())
- return true;
- }
- }
-
- // The legacy cost model only supports scalarization loads/stores with phi
- // addresses, if the phi is directly used as load/store address. Don't
- // traverse further for Blends.
- if (Blend)
- continue;
-
- append_range(WorkList, Cur->users());
- }
- return false;
-}
-
/// Return true if \p R is a predicated load/store with a loop-invariant address
/// only masked by the header mask.
static bool isPredicatedUniformMemOpAfterTailFolding(const VPReplicateRecipe &R,
@@ -3539,7 +3489,7 @@ InstructionCost VPReplicateRecipe::computeCost(ElementCount VF,
TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(UI->getOperand(0));
bool PreferVectorizedAddressing = Ctx.TTI.prefersVectorizedAddressing();
bool UsedByLoadStoreAddress =
- !PreferVectorizedAddressing && isUsedByLoadStoreAddress(this);
+ !PreferVectorizedAddressing && vputils::isUsedByLoadStoreAddress(this);
InstructionCost ScalarMemOpCost = Ctx.TTI.getMemoryOpCost(
UI->getOpcode(), ValTy, Alignment, AS, Ctx.CostKind, OpInfo,
UsedByLoadStoreAddress ? UI : nullptr);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
index b776d5c75a849..5b80fa15a5535 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.cpp
@@ -781,3 +781,54 @@ VPInstruction *vputils::findComputeReductionResult(VPReductionPHIRecipe *PhiR) {
return vputils::findUserOf<VPInstruction::ComputeReductionResult>(
cast<VPSingleDefRecipe>(SelR));
}
+
+bool vputils::isUsedByLoadStoreAddress(const VPValue *V) {
+ SmallPtrSet<const VPValue *, 4> Seen;
+ SmallVector<const VPValue *> WorkList = {V};
+
+ while (!WorkList.empty()) {
+ const VPValue *Cur = WorkList.pop_back_val();
+ if (!Seen.insert(Cur).second)
+ continue;
+
+ auto *Blend = dyn_cast<VPBlendRecipe>(Cur);
+ // Skip blends that use V only through a compare by checking if any incoming
+ // value was already visited.
+ if (Blend && none_of(seq<unsigned>(0, Blend->getNumIncomingValues()),
+ [&](unsigned I) {
+ return Seen.contains(Blend->getIncomingValue(I));
+ }))
+ continue;
+
+ for (VPUser *U : Cur->users()) {
+ if (auto *InterleaveR = dyn_cast<VPInterleaveBase>(U))
+ if (InterleaveR->getAddr() == Cur)
+ return true;
+ if (auto *RepR = dyn_cast<VPReplicateRecipe>(U)) {
+ if (RepR->getOpcode() == Instruction::Load &&
+ RepR->getOperand(0) == Cur)
+ return true;
+ if (RepR->getOpcode() == Instruction::Store &&
+ RepR->getOperand(1) == Cur)
+ return true;
+ }
+ if (auto *MemR = dyn_cast<VPWidenMemoryRecipe>(U)) {
+ if (MemR->getAddr() == Cur && MemR->isConsecutive())
+ return true;
+ }
+ }
+
+ // The legacy cost model only supports scalarization loads/stores with phi
+ // addresses, if the phi is directly used as load/store address. Don't
+ // traverse further for Blends.
+ if (Blend)
+ continue;
+
+ // Only traverse further through users that also define a value (and can
+ // thus have their own users walked).
+ for (VPUser *U : Cur->users())
+ if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
+ WorkList.push_back(SDR);
+ }
+ return false;
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index ac3a1005c8f24..2a4b8566d8475 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -159,6 +159,10 @@ VPInstruction *findCanonicalIVIncrement(VPlan &Plan);
/// mirroring Value::stripPointerCasts.
GEPNoWrapFlags getGEPFlagsForPtr(VPValue *Ptr);
+/// Returns true if \p V is used as part of the address of another load or
+/// store.
+bool isUsedByLoadStoreAddress(const VPValue *V);
+
/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
/// inserted for predicated reductions or tail folding.
VPInstruction *findComputeReductionResult(VPReductionPHIRecipe *PhiR);
More information about the llvm-commits
mailing list