Skip to content

Commit f5fe7ab

Browse files
committed
[VPlan] Account for removed users in replaceAllUsesWith.
Make sure we do not iterate using an invalid iterator. Another small fix/step towards traversing the def-use chains in VPlan.
1 parent aaae13d commit f5fe7ab

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,18 @@ void VPWidenCanonicalIVRecipe::print(raw_ostream &O, const Twine &Indent,
884884
template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
885885

886886
void VPValue::replaceAllUsesWith(VPValue *New) {
887-
for (VPUser *User : users())
887+
for (unsigned J = 0; J < getNumUsers();) {
888+
VPUser *User = Users[J];
889+
unsigned NumUsers = getNumUsers();
888890
for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I)
889891
if (User->getOperand(I) == this)
890892
User->setOperand(I, New);
893+
// If a user got removed after updating the current user, the next user to
894+
// update will be moved to the current position, so we only need to
895+
// increment the index if the number of users did not change.
896+
if (NumUsers == getNumUsers())
897+
J++;
898+
}
891899
}
892900

893901
void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ TEST(VPInstructionTest, replaceAllUsesWith) {
167167
EXPECT_EQ(0u, VPV2->getNumUsers());
168168
EXPECT_EQ(0u, VPV3->getNumUsers());
169169

170+
VPInstruction *I2 = new VPInstruction(0, {VPV1, VPV2});
171+
EXPECT_EQ(3u, VPV1->getNumUsers());
172+
VPV1->replaceAllUsesWith(VPV3);
173+
EXPECT_EQ(3u, VPV3->getNumUsers());
174+
170175
delete I1;
176+
delete I2;
171177
delete VPV1;
172178
delete VPV2;
173179
delete VPV3;

0 commit comments

Comments
 (0)