This is an archive of the discontinued LLVM Phabricator instance.

[mem2reg] Use range loops (NFCI)
ClosedPublic

Authored by modocache on Feb 19 2018, 10:00 AM.

Details

Summary

Several for loops in PromoteMemoryToRegister.cpp leave their increment
expression empty, instead incrementing the iterator within the for loop
body. I believe this is because these loops were previously implemented
as while loops; see https://reviews.llvm.org/rL188327.

Incrementing the iterator within the body of the for loop instead of
in its increment expression makes it seem like the iterator will be
modified or conditionally incremented within the loop, but that is not
the case in these loops.

Instead, use range loops.

Test Plan: check-llvm

Diff Detail

Repository
rL LLVM

Event Timeline

modocache created this revision.Feb 19 2018, 10:00 AM

Looks like all of these could be written as for-range loops for (User *U : Foo->users()) now.

yes, please use Range loops here. Otherwise the idea is fine.

Thanks for the review! I've updated the diff to use range loops instead.

modocache retitled this revision from Bump iterator in for loop increment expr (NFCI) to [mem2reg] Use range loops (NFCI).Feb 19 2018, 10:38 AM
modocache edited the summary of this revision. (Show Details)
davide accepted this revision.Feb 19 2018, 10:40 AM

lgtm, thanks.

This revision is now accepted and ready to land.Feb 19 2018, 10:40 AM
This revision was automatically updated to reflect the committed changes.

Great, thank you both for the reviews!

bjope added a subscriber: bjope.Feb 20 2018, 12:17 AM
bjope added inline comments.
llvm/trunk/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
329

I think the old code was more safe since it does not depend as much on how the iterators are implemented. The instruction "I" is actually deleted inside the loop. So stepping the iterator after deleting "I" could be dangerous if stepping of the iterator involves dereferencing of the object that the iterator refers. Or am I missing something?
I would at least assume that the old code was written the way it was just because the datastructure that we iterate over can change during iteration. So it should be more safe to step the iterator to the "next" element first, and then remove the "current" element.

modocache added inline comments.Feb 20 2018, 5:12 AM
llvm/trunk/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
329

Aha, that's a very good point, thanks! I was looking at the references to U but didn't see that I was being erased. I guess the same can be said for the loop below as well. So of the three loops in this diff, only the first loop appears to be convertible to a range loop.