The dominator tree lets
us identify natural loops:
a back edge T->H whose head H dominates its
tail T defines a loop with the single entry H.
This works only for reducible control flow graphs. Optimized
machine code and decompiler output routinely contain
irreducible loops, which have more than one entry and thus no
dominating header, so the dominator-based method cannot see them.
This post builds a loop-nesting forest for an arbitrary CFG with the single-pass depth-first search of 韦韬、毛剑、邹维、陈宇(Tao Wei, Jian Mao, Wei Zou & Yu Chen) A New Algorithm for Identifying Loops in Decompilation, SAS 2007 (The 14th International Static Analysis Symposium).
Reducibility
A depth-first search from the entry classifies every non-tree edge
relative to the spanning tree. A retreating edge
u->v goes to an ancestor v of
u on the DFS path. A cycle is a closed path in the
CFG, a graph-theoretic object with no distinguished entry; a
loop is the control-flow structure built on top — a set of
nodes with a header, nested into a forest. (LLVM draws the same line:
LoopInfo represents natural loops, while the
GenericCycleInfo used below generalizes them to irreducible
control flow.) A CFG is reducible when, in every DFS, each
retreating edge is a back edge — its head v
dominates its tail u. Then every cycle lies in a natural
loop whose header dominates it, so control enters the loop only through
that header.
A CFG is irreducible when some cycle has two or more entries: nodes with a predecessor outside the cycle. No single node dominates the cycle, so there is no natural header. The smallest example is the irreducible core:
Node 0 branches to both 1 and 2, and 1 -> 2 -> 1
is a cycle entered at 1 (through 0->1) and at 2 (through
0->2). M.S. Hecht and J.D. Ullman proved that a CFG is
irreducible if and only if it contains this three-node pattern as a
subgraph, allowing each edge to be a path through other nodes.
Because no node dominates an irreducible cycle, its header is not intrinsic — we must pick one of the entries. The standard choice (Havlak, LLVM, and the algorithm below) is the node the DFS reaches first, i.e. the loop member with the smallest preorder number. So the loop-nesting forest of an irreducible CFG depends on the DFS order.
Loop-nesting forest
There is no agreed definition of the loop-nesting forest for
irreducible CFGs. Steensgaard, Sreedhar–Gao–Lee, Havlak, and Ramalingam
each give a different one; for the CFG in Wei et al.'s Fig. 3 they
report two, one, three, and one loops respectively. We adopt Havlak's,
the finest, because it gives each loop a single header and the fewest
gotos when re-structuring, and it is what LLVM's
GenericCycleInfo computes:
- The outermost loops are the maximal strongly connected regions (with at least one internal edge).
- A loop's header is its minimum-preorder node.
- Its inner loops are the loops of the subgraph induced on (loop nodes − header), found recursively.
The forest has a compact encoding. For each node record its
innermost loop header iloop_header: the header of
the smallest loop containing it, or none. A header's own
iloop_header is the header of its parent loop. Following
the iloop_header links from a node lists its enclosing
loops innermost-first — the "loop header list" of the paper. Which nodes
are headers, plus every node's innermost header, determines the whole
forest; that is what the program below prints.
Identifying loops in one DFS pass
Natural loops need a dominator tree first. Wei et al. observe that a single DFS with a little bookkeeping suffices for an arbitrary CFG — no dominator tree, no UNION-FIND, no second bottom-up pass.
Let p be the current DFS path, the recursion stack from
the entry to the node being visited (DFSP in the paper).
pos[b] is b's 1-based position on that path,
or 0 once b has been popped. Every node carries
iloop_header, its innermost loop header discovered so far.
When visiting b0, each successor b falls into
one of five cases:
bis unvisited — a tree edge. Recurse; the call returnsb's innermost header, which we merge intob0's chain.
bis on the current path (pos[b] > 0) — a back edge.bis a loop header; merge it intob0.
bis finished and in no loop — a forward or cross edge to a non-loop node; ignore it.
bis finished, inside a loop whose innermost headerhis still on the path —b0belongs to that loop too; mergeh.
bis finished, inside a loop whose innermost header is not on the path — the edgeb0->benters the loop below its header: a re-entry edge, and the loop is irreducible. Walk upb's header chain to the first header that is on the path and merge that.
Merging a header (tag_lhead) splices it into the node's
innermost-to-outermost chain, ordered by DFS position; this replaces the
UNION-FIND of the classical Havlak–Tarjan algorithm. The total cost is
O(N + k*E), where k is an unstructuredness
coefficient that measures the case-(E) climbs and the chain
splices. On real code k is tiny (empirically below 1.5), so
the algorithm is near-linear.
The input format matches the natural-loops post: n m on
the first line, then m edges u v, with node 0
the entry. The program prints each node's innermost loop header, then
the forest, then any re-entry edges.
1 |
|
Examples
The irreducible core:
1 | % ./wei |
The header is 1 because the DFS reaches 1 first. List
0 2 before 0 1 and the header becomes 2
instead: the loop is the same set of nodes, but its header — and
therefore the forest — depends on the DFS order, unlike a natural
loop.
1 | % ./wei |
A nested example: a reducible outer loop {1,2,3} (back
edge 3->1) containing an irreducible inner loop
{2,3}, entered at 2 (via 1->2) and at 3
(via 1->3):
1 | % ./wei |
Only the inner loop is irreducible; the outer loop has the single
entry 1. 3's header list is 2, 1: its
innermost loop is {2,3}, enclosed by
{1,2,3}.
Pipe any of these graphs through
awk 'BEGIN{print "digraph G{"} NR>1{print $1"->"$2} END{print "}"}'
to render them with graphviz.
Relation to natural loops
On a reducible CFG the first-visited node of a loop is exactly the node that dominates it, so this algorithm's header coincides with the natural-loop header and the two forests are identical. Running the program on the natural loops example — a reducible graph with a self-loop and an unreachable node — produces the same loops as that post's dominator-based program, and reports no re-entry edges.
The Havlak–Tarjan algorithm reaches the same forest by a different
route: a top-down DFS to find back edges, then a bottom-up UNION-FIND
pass propagating headers from loop tails. LLVM's
GenericCycleInfo
(llvm/include/llvm/ADT/GenericCycleImpl.h) computes the
same forest with its own two-pass scheme — a DFS that numbers blocks,
then a reverse-preorder scan that seeds each header from a back edge and
gathers the cycle body by walking predecessors backward (no UNION-FIND).
Wei et al.'s contribution is folding both passes into a single DFS, and
their re-entry bookkeeping (case (E)) is what marks the irreducible
loops along the way.