This is an archive of the discontinued LLVM Phabricator instance.

[LoopSimplify] Rebuild LCSSA for the inner loop after separating nested loops.
ClosedPublic

Authored by mzolotukhin on Aug 8 2016, 6:12 PM.

Details

Summary

This hopefully fixes PR28825. The problem now was that a value from the
original loop was used in a subloop, which became a sibling after separation.
While a subloop doesn't need an lcssa phi node, a sibling does, and that's
where we broke LCSSA. The most natural way to fix this now is to simply call
formLCSSA on the original loop: it'll do what we've been doing before plus
it'll cover situations described above.

I think we don't need to run formLCSSARecursively here, and we have an assert
to verify this (I've tried testing it on LLVM testsuite + SPECs). I'd be happy
to be corrected here though.

Diff Detail

Repository
rL LLVM

Event Timeline

mzolotukhin updated this revision to Diff 67269.Aug 8 2016, 6:12 PM
mzolotukhin retitled this revision from to [LoopSimplify] Rebuild LCSSA for the inner loop after separating nested loops..
mzolotukhin updated this object.
mzolotukhin added reviewers: chandlerc, sanjoy, silvas.
mzolotukhin added a subscriber: llvm-commits.
chandlerc edited edge metadata.Aug 8 2016, 6:25 PM

The only way I can see this needing the recursive side of LCSSA is the following.

Imagine you had L1 nested inside L2 nested inside L3 nested inside L4. And this takes the L1 nested loop and makes it a sibling of L3. Now you have L2 nested inside L3 nested inside L4, and L1 nested inside L4. But my reading a code indicates we don't ever do this transform. But if you see a way we could do something similar, and end up with L in the code pointing at L3 for example, we might miss that L2 suddenly needs an LCSSA phi node in addition to L3 needing one.

test/Transforms/LoopSimplify/pr28272.ll
114 ↗(On Diff #67269)

It would be really nice to add CHECKs to ensure the particular loop nest structure (after LoopSimplify) is in fact formed... Is that reasonable to do?

silvas edited edge metadata.Aug 8 2016, 6:40 PM

FTR, I'm pretty clueless about all this loop stuff, so don't wait on me! But do let me know if there's any testing you'd like me to do.

Imagine you had l1 nested inside L2 nested inside L3 nested inside L4. And this takes the l1 nested loop and makes it a sibling of L3. Now you have L2 nested inside L3 nested inside L4, and l1 nested inside L4. But my reading a code indicates we don't ever do this transform. But if you see a way we could do something similar, and end up with L in the code pointing at L3 for example, we might miss that L2 suddenly needs an LCSSA phi node in addition to L3 needing one.

In your example the ordering of the loops has changed - from my understanding of the code we don't do this (it'll be more like loop interchange, right?).

My understanding was that recursive invocation is needed to form LCSSA inside enclosing loop (e.g. if one subloop uses values from another), but since we're not changing internals of the loop, it should be preserved. A simple non-recursive run of formLCSSA on the new loop should fix LCSSA for values used outside it, including values defined in inner loops since their blocks are also blocks of the new loop, and that's the only (as far as I see now) place where LCSSA can be broken. Does this logic sound correct?

This revision was automatically updated to reflect the committed changes.