This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Change recursive calls to llvm::SimplifyCFG to instead use an outer while loop to revisit.
ClosedPublic

Authored by craig.topper on Oct 1 2018, 5:40 PM.

Details

Summary

The llvm::SimplifyCFG function creates a SimplifyCFGOpt object and calls run on it. There were numerous places reached from this run function that called back out llvm::SimplifyCFG which would create another SimplifyCFGOpt object. This is an inefficient use of stack space at minimum. We are also not passing along the LoopHeaders pointer passed into the outer llvm::SimplifyCFG call. So if its not null we lose it on the first recursion and get nullptr from there on.

This patch adds an outer loop around the main BasicBlock simplifying code and adds a flag to the SimplifyCFGOpt class that can be set by to request another iteration. I don't think we can iterate based just on the change flag alone since some of the simplifications delete a basic block entirely leaving nothing to iterate on.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.Oct 1 2018, 5:40 PM
reames added a comment.Oct 2 2018, 6:29 AM

Instead of doing the simplification recursively, wouldn't it be better to just iterate externally? That is, what if we split run into an outer function which called an inner helper routine as long as that inner routine was making progress? Or is there a concern that SimplifyCFG does not stabilize to a fixed point?

I definitely agree that this recursion is not the ideal way to implement this. I haven't looked closely to see if we always do this recursion or if we sometimes just return without recursing. Any issue with this patch in the short term?

reames added a comment.Oct 3 2018, 2:04 AM

I definitely agree that this recursion is not the ideal way to implement this. I haven't looked closely to see if we always do this recursion or if we sometimes just return without recursing. Any issue with this patch in the short term?

I'm not actively objecting, but I'm not sure I'd bother to pursue this intermediate state as opposed to directly addressing the root issue. If you really want to pursue this approach, I'm willing to review and signoff.

craig.topper retitled this revision from [SimplifyCFG] Change recursive calls to llvm::SimplifyCFG to instead call the "run" function on the SimplifyCFGOpt object to [SimplifyCFG] Change recursive calls to llvm::SimplifyCFG to instead use an outer while loop to revisit..
craig.topper edited the summary of this revision. (Show Details)
reames accepted this revision.Oct 4 2018, 10:19 AM

LGTM

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