Open Quuxplusone opened 6 years ago
Attached 0001-Fix-When-multiple-loops-are-left-a-loop-can-be-enter.patch
(6097 bytes, text/plain): Test case + fix
Patch seems reasonable to me.
Is it correct that at most one loop can be entered at a time? Otherwise we have a few more cases to handle.
(In reply to Michael Kruse from comment #1)
> Patch seems reasonable to me.
>
> Is it correct that at most one loop can be entered at a time? Otherwise we
> have a few more cases to handle.
Since we only allow/deal with natural loops we can expect that yes.
Let's assume we could enter two natural loops at the same time. Since the two
loops have to be nested they form two SCCs in the CFG (one for the inner loop
and one for the whole loop nest). Note that the nesting implies that the outer
loop is reachable from the outside. Assuming now we have an edge that targets
the inner one but the source is not in the SCC of the whole loop nest
(otherwise the source would be, at least, in the outer loop, thus at most one
loop would be entered). This edge, creates a second entry block for the outer
loop (SCC) as the source is not in the outer loop (SCC) but the target is and
the outer loop (SCC) already had an entry block. This assums again the source
was reachable. Two entries for a single SCC should not form a natural loop
which contradicts our initial assumption.
Does that sound right?
I did not follow your argument. It shows that we cannot 'skip' the outer loop's header (with the additional assumption that the assumed edge's target is not the outer loop's header). This can also be shown given that a natural loop can only be entered by its header. What if the two loops share the same header?
@grosser: I think the patch is sound. How to proceed?
(In reply to Michael Kruse from comment #3)
> I did not follow your argument. It shows that we cannot 'skip' the outer
> loop's header (with the additional assumption that the assumed edge's target
> is not the outer loop's header). This can also be shown given that a natural
> loop can only be entered by its header.
I don't recall the details anymore, sorry.
> What if the two loops share the same header?
Same header <=> same natural loop.
> Same header <=> same natural loop.
Ok, I got confused by a comment in LoopInfo.cpp:
// Note that the
// loops identified may actually be several natural loops that share the same
// header node... not just a single natural loop.
(In reply to Michael Kruse from comment #5)
> > Same header <=> same natural loop.
>
> Ok, I got confused by a comment in LoopInfo.cpp:
>
> // Note that the
> // loops identified may actually be several natural loops that share the same
> // header node... not just a single natural loop.
Maybe it is a terminology problem on my side.
Regardless of the names, all that is needed, and all that LoopInfo provides, is
a strongly connected component which includes a block that dominates the
strongly connected component. This block is the loop header, all others are
contained in the loop. Each such strongly connected component is recognized as
a loop by LoopInfo, any other SCC is not (since it would be irreducible). Note
that neither LoopInfo nor me does require a single back edge or exiting edge.
0001-Fix-When-multiple-loops-are-left-a-loop-can-be-enter.patch
(6097 bytes, text/plain)