Skip to content

Commit a002271

Browse files
committed
[VPlan] Add VPValue::replaceUsesWithIf (NFCI).
Add replaceUsesWithIf helper and use it in a few places.
1 parent 742d8eb commit a002271

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,26 @@ void VPValue::replaceAllUsesWith(VPValue *New) {
11201120
}
11211121
}
11221122

1123+
void VPValue::replaceUsesWithIf(
1124+
VPValue *New,
1125+
llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace) {
1126+
for (unsigned J = 0; J < getNumUsers();) {
1127+
VPUser *User = Users[J];
1128+
unsigned NumUsers = getNumUsers();
1129+
for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
1130+
if (User->getOperand(I) != this || !ShouldReplace(*User, I))
1131+
continue;
1132+
1133+
User->setOperand(I, New);
1134+
}
1135+
// If a user got removed after updating the current user, the next user to
1136+
// update will be moved to the current position, so we only need to
1137+
// increment the index if the number of users did not change.
1138+
if (NumUsers == getNumUsers())
1139+
J++;
1140+
}
1141+
}
1142+
11231143
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
11241144
void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {
11251145
if (const Value *UV = getUnderlyingValue()) {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,10 @@ static bool sinkScalarOperands(VPlan &Plan) {
162162
// TODO: add ".cloned" suffix to name of Clone's VPValue.
163163

164164
Clone->insertBefore(SinkCandidate);
165-
for (auto *U : to_vector(SinkCandidate->getVPSingleValue()->users())) {
166-
auto *UI = cast<VPRecipeBase>(U);
167-
if (UI->getParent() == SinkTo)
168-
continue;
169-
170-
for (unsigned Idx = 0; Idx != UI->getNumOperands(); Idx++) {
171-
if (UI->getOperand(Idx) != SinkCandidate->getVPSingleValue())
172-
continue;
173-
UI->setOperand(Idx, Clone);
174-
}
175-
}
165+
SinkCandidate->getVPSingleValue()->replaceUsesWithIf(
166+
Clone, [SinkTo](VPUser &U, unsigned) {
167+
return cast<VPRecipeBase>(&U)->getParent() != SinkTo;
168+
});
176169
}
177170
SinkCandidate->moveBefore(*SinkTo, SinkTo->getFirstNonPhi());
178171
for (VPValue *Op : SinkCandidate->operands())
@@ -277,16 +270,10 @@ static bool mergeReplicateRegionsIntoSuccessors(VPlan &Plan) {
277270
VPValue *PredInst1 =
278271
cast<VPPredInstPHIRecipe>(&Phi1ToMove)->getOperand(0);
279272
VPValue *Phi1ToMoveV = Phi1ToMove.getVPSingleValue();
280-
for (VPUser *U : to_vector(Phi1ToMoveV->users())) {
281-
auto *UI = dyn_cast<VPRecipeBase>(U);
282-
if (!UI || UI->getParent() != Then2)
283-
continue;
284-
for (unsigned I = 0, E = U->getNumOperands(); I != E; ++I) {
285-
if (Phi1ToMoveV != U->getOperand(I))
286-
continue;
287-
U->setOperand(I, PredInst1);
288-
}
289-
}
273+
Phi1ToMoveV->replaceUsesWithIf(PredInst1, [Then2](VPUser &U, unsigned) {
274+
auto *UI = dyn_cast<VPRecipeBase>(&U);
275+
return UI && UI->getParent() == Then2;
276+
});
290277

291278
Phi1ToMove.moveBefore(*Merge2, Merge2->begin());
292279
}
@@ -542,16 +529,10 @@ void VPlanTransforms::optimizeInductions(VPlan &Plan, ScalarEvolution &SE) {
542529

543530
// Update scalar users of IV to use Step instead. Use SetVector to ensure
544531
// the list of users doesn't contain duplicates.
545-
SetVector<VPUser *> Users(WideIV->user_begin(), WideIV->user_end());
546-
for (VPUser *U : Users) {
547-
if (HasOnlyVectorVFs && !U->usesScalars(WideIV))
548-
continue;
549-
for (unsigned I = 0, E = U->getNumOperands(); I != E; I++) {
550-
if (U->getOperand(I) != WideIV)
551-
continue;
552-
U->setOperand(I, Steps);
553-
}
554-
}
532+
WideIV->replaceUsesWithIf(
533+
Steps, [HasOnlyVectorVFs, WideIV](VPUser &U, unsigned) {
534+
return !HasOnlyVectorVFs || U.usesScalars(WideIV);
535+
});
555536
}
556537
}
557538

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ class VPValue {
163163

164164
void replaceAllUsesWith(VPValue *New);
165165

166+
/// Go through the uses list for this VPValue and make each use point to \p
167+
/// New if the callback ShouldReplace returns true for the given use specified
168+
/// by a pair of (VPUser, the use index).
169+
void replaceUsesWithIf(
170+
VPValue *New,
171+
llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace);
172+
166173
/// Returns the recipe defining this VPValue or nullptr if it is not defined
167174
/// by a recipe, i.e. is a live-in.
168175
VPRecipeBase *getDefiningRecipe();

0 commit comments

Comments
 (0)