This is an archive of the discontinued LLVM Phabricator instance.

[LoopInfo] Clarify header-loop relationship
AbandonedPublic

Authored by baziotis on Sep 23 2020, 2:22 PM.

Details

Summary

I'm not sure if I have phrased it correctly or if I have understood completely analyze() but it seems to me that: Two loops can't have the same header (even if one is a sub-loop of another, which btw is not clear what it means in this context).

Example: https://godbolt.org/z/bqo8KP

Diff Detail

Event Timeline

baziotis created this revision.Sep 23 2020, 2:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 23 2020, 2:22 PM
baziotis requested review of this revision.Sep 23 2020, 2:22 PM

There are different definitions of loops. In the literature, such as a Dragon Book, a loop is identified by a backedge, but LLVM does it with its header. But only natural loops have a header: that is, not all cyclic control flow is represented by LoopInfo. Cyclic control flow that is is not form a natural loop is called irreducible control flow.

Anyway, using SCCs to stand for loops is bad as well since it ignores subloops. It should be a induced subgraph, not component. I think I raised this in the first review, but it was committed anyway. I'd be grateful if this could be cleaned up.

Would you be ok if I made another patch trying to fix all these things?

There are different definitions of loops. In the literature, such as a Dragon Book, a loop is identified by a backedge, but LLVM does it with its header. But only natural loops have a header: that is, not all cyclic control flow is represented by LoopInfo. Cyclic control flow that is is not form a natural loop is called irreducible control flow.

Anyway, using SCCs to stand for loops is bad as well since it ignores subloops. It should be a induced subgraph, not component. I think I raised this in the first review, but it was committed anyway. I'd be grateful if this could be cleaned up.

I agree in all that. Actually, this patch started when I was reading Muchnick's book. It has a great paragraph on reducibility and this is where my example came from. Muchnick defines
a natural loop by a backedge, but it depends on how we define that: It's when the head of an edge dominates its tail. With this definition (AFAIU), a natural loop is always reducible (only one way to enter),
but multiple natural loops can have the same header. And then the book says "if that happens, it's a matter of convention then and our convention is to consider all of them one big loop".

So, then I was curious what LLVM does: LLVM's definition of a natural loop makes it clear that in such a scenario, the loops should be considered as one big natural loop since the header dominates all the latches (and intermediate blocks).
That's also what seems to happen in the code and when I ran a couple of examples in godbolt (like the one above). But the doc at this part did not agree with this.

Would you be ok if I made another patch trying to fix all these things?

Yes of course. TBH though, although I thank you for this comment, I didn't understand if I was wrong / correct, the doc was wrong / correct or any combination of those :) It would help if you clarified, as I'm trying to understand more about loops.

With D88408 have been committed, can this be abandoned?

With D88408 have been committed, can this be abandoned?

Yep.

baziotis abandoned this revision.Oct 5 2020, 8:39 AM