Skip to content

Commit b1bfe22

Browse files
committed
[VPlan] Remove unneeded getNumUsers calls in replaceAllUsesWith (NFC).
As suggested post-commit for a002271, replace unnecessary getNumUsers calls by boolean variable to indicate if users changed. Note that this also requires an early exit to detect the case where a value is replaced by itself.
1 parent 2103de0 commit b1bfe22

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,36 +1135,43 @@ void VPlanIngredient::print(raw_ostream &O) const {
11351135
template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
11361136

11371137
void VPValue::replaceAllUsesWith(VPValue *New) {
1138+
if (this == New)
1139+
return;
11381140
for (unsigned J = 0; J < getNumUsers();) {
11391141
VPUser *User = Users[J];
1140-
unsigned NumUsers = getNumUsers();
1142+
bool RemovedUser = false;
11411143
for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I)
1142-
if (User->getOperand(I) == this)
1144+
if (User->getOperand(I) == this) {
11431145
User->setOperand(I, New);
1146+
RemovedUser = true;
1147+
}
11441148
// If a user got removed after updating the current user, the next user to
11451149
// update will be moved to the current position, so we only need to
11461150
// increment the index if the number of users did not change.
1147-
if (NumUsers == getNumUsers())
1151+
if (!RemovedUser)
11481152
J++;
11491153
}
11501154
}
11511155

11521156
void VPValue::replaceUsesWithIf(
11531157
VPValue *New,
11541158
llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace) {
1159+
if (this == New)
1160+
return;
11551161
for (unsigned J = 0; J < getNumUsers();) {
11561162
VPUser *User = Users[J];
1157-
unsigned NumUsers = getNumUsers();
1163+
bool RemovedUser = false;
11581164
for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
11591165
if (User->getOperand(I) != this || !ShouldReplace(*User, I))
11601166
continue;
11611167

1168+
RemovedUser = true;
11621169
User->setOperand(I, New);
11631170
}
11641171
// If a user got removed after updating the current user, the next user to
11651172
// update will be moved to the current position, so we only need to
11661173
// increment the index if the number of users did not change.
1167-
if (NumUsers == getNumUsers())
1174+
if (!RemovedUser)
11681175
J++;
11691176
}
11701177
}

0 commit comments

Comments
 (0)