Definitioncore
Natural loop
Also called back edge loop.
For a back edge n to h where h dominates n, the set of nodes that reach n without passing through h, with h as the single header.
A natural loop is the loop that a compiler sees in a control flow graph. It comes from a back edge , an edge whose target dominates its source . The loop is together with every block that can reach without passing through . Then , the header, is the only way into the loop, and it dominates every block of the loop. Single entry is what loop optimizations need: there is one place before the loop to put hoisted code, the preheader, and every run that enters the loop starts at the same block. A backward search from that stops at finds the loop in time proportional to its size.
Definition
Let be a control flow graph in which every node is reachable from the entry . A node dominates if every path from to contains (dominators problem).
- An edge is a back edge if dominates . A self-loop is a back edge.
- The natural loop of the back edge is
- is the header of the loop and is a latch. The body is . An edge with and is an exit edge.
- When several back edges share the header , compilers usually treat the union of their natural loops as one loop with header . It has the same properties as below.
A back edge in this sense is the target of the arrow being a dominator. A retreating edge of a depth-first search, an edge to an ancestor in the DFS tree, need not be a back edge; the graphs in which every retreating edge is a back edge are the reducible flow graphs.
Building the loop
NATURAL-LOOP(G, t, h) (t, h) is a back edge
1 loop <- {h}; stack <- empty
2 if t != h then add t to loop; push t
3 while stack is not empty do
4 pop v
5 for each predecessor p of v do
6 if p is not in loop then add p to loop; push p
7 return loop
Claim 1. NATURAL-LOOP returns , and it runs in time proportional to the number of nodes of the loop plus the number of edges that end in a node of the loop other than .
Proof. Every node is pushed at most once, because it is added to when it is pushed and never removed, and is in from line 1, so it is never pushed. Each popped node scans its predecessor list once, which gives the time bound.
Every node in the result is in . For each pushed node we show that a path from to avoids . For the path of length 0 does, since when is pushed. A node is pushed in line 6 as a predecessor of a popped node ; was pushed, so and reaches without , and because is in . So the path avoids .
Every node of is in the result. is added in line 1. Let have a path that avoids ; we use induction on . If , then is added in line 2. Otherwise has a shorter path to that avoids , so is added, and since it was pushed, so it is popped later, and line 6 adds its predecessor if it is not already there.
The search never passes , so it stays inside the loop even if the header has many predecessors outside.
Properties
Claim 2. Let .
- dominates every node of .
- Every edge from a node outside to a node of ends at . So is the only entry.
- is strongly connected: every node of reaches every other one by a path inside .
Proof. (1) Let , , with a path from to that avoids . If a path from to avoided , then together they would give a path from to that avoids , but dominates .
(2) Let be an edge with and . Then has a path to that avoids . If , then , , and that path form a path from to that avoids , so . If , then too. So an edge from outside ends at .
(3) From , a path to that avoids stays in , since each node on it reaches without ; then the edge leads to . From to : take a path from to ; by (1) it contains ; take the part after the last . Each node on that part reaches without , and reaches without (or , and the part is just ), so .
Claim 3 (nesting). Two natural loops with different headers are either disjoint or one is a proper subset of the other.
Proof. Let and with share a node . By Claim 2, and both dominate . The dominators of a node lie on one path of the dominator tree, so one of them dominates the other; say strictly dominates . Then , since every node of is dominated by , and does not dominate . In particular , so has a path to that avoids . By the proof of Claim 2 (3), there is a path from to inside , so it avoids . Joined, they show that reaches without , so . Now every reaches inside , then by the back edge, then as just shown, all without . So , and . The inclusion is proper because .
After the loops with the same header are merged, Claim 3 makes the natural loops a forest under inclusion, the loop nesting forest of a reducible graph. The innermost loop that contains a block gives its loop depth.
Examples and non-examples
A loop with a branch inside
The odd-sum program of the control flow graph page has the edges , , , , , , . B2 dominates B5, so is a back edge. The backward search from B5 adds B3 and B4, then from B3 it finds B2, which is already in the loop, and stops. The natural loop is with header B2 and the exit edge .
Nested loops
B0:
i = 0
s = 0
jump B1
B1:
t = i < n
br t B2 B6
B2:
j = 0
jump B3
B3:
u = j < i
br u B4 B5
B4:
s = s + j
j = j + 1
jump B3
B5:
i = i + 1
jump B1
B6:
ret s
The edges are , , , , , , , . Computed by the definition, the back edges are and . Their natural loops are and , and the first lies inside the second, as Claim 3 says. B4 has loop depth 2, B2 and B5 have depth 1, and B0 and B6 have depth 0. The program adds over all ; the toy interpreter returns 4 for and 10 for .
Two latches, one header
B0:
i = 0
s = 0
jump B1
B1:
t = i < n
br t B2 B5
B2:
i = i + 1
r = i % 3
br r B3 B1
B3:
s = s + i
jump B1
B5:
ret s
This is a loop with a continue: B2 jumps back to B1 when is a multiple of 3, and B3 jumps back after adding . Both and are back edges. Their natural loops are and , which share the header, so Claim 3 does not apply; a compiler merges them into one loop . On the run returns .
Non-examples
- A cycle with two entries. In the graph , , , , , , the cycle is entered at from and at from . Neither nor dominates the other, so neither nor is a back edge, and the cycle is not a natural loop. Such graphs are not reducible; a loop nesting forest still describes the cycle, with two entries.
- A retreating edge that is not a back edge. In the same graph, a DFS that visits first has as a retreating edge, an edge to a DFS ancestor. It is not a back edge, because does not dominate .
- A strongly connected component. The strongly connected components of a CFG are the maximal sets of blocks that all reach each other. One component can hold several natural loops, nested or with a shared header, and the component above holds none.
Pitfalls
- Searching forward from the header. The blocks that reaches are not the loop: they include the code after the loop. The search must go backward from the latch and stop at .
- Not stopping at the header. If the search continues through , it runs into the code before the loop and, through the entry, into the whole procedure.
- Unreachable blocks. An unreachable block that jumps into the loop would be added by the backward search. Delete unreachable blocks first, as the definition assumes.
- Treating every retreating edge as a loop. On an irreducible graph some retreating edges have no natural loop; an optimization that needs a single header must check dominance, or first make the graph reducible by node splitting.
- Loops with the same header. The natural loops of two back edges to overlap without nesting. Merge them, or a loop pass may see the same header twice.
Related definitions
- Dominators problem, dominator tree and Cooper-Harvey-Kennedy dominance: the dominance needed to find back edges.
- Reducible flow graph: every cycle has a back edge, so the natural loops cover all loops.
- Loop nesting forest: the loops arranged by inclusion, also for irreducible graphs.
- Depth-first orders: retreating edges, the candidates for back edges.
- Loop-invariant code motion and induction variable analysis: optimizations that work on natural loops and use the preheader.
References
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Addison-Wesley, 2007, Section 9.6.4 (back edges and reducibility) and Section 9.6.6 (natural loops, with the algorithm that builds the natural loop of a back edge by a backward search from its tail)
- Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 7 (Control-Flow Analysis), back edges, natural loops and their nesting
- Andrew W. Appel with Jens Palsberg, Modern Compiler Implementation in Java, second edition, Cambridge University Press, 2002, Chapter 18 (Loop Optimizations), the section on loops, with dominators, back edges, natural loops, nested loops and the loop-nest tree
- Frances E. Allen, Control flow analysis, Proceedings of a Symposium on Compiler Optimization, SIGPLAN Notices 5(7), 1970, dominance and intervals, the single-entry regions of a flow graph that hold its loops
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag DSAB3F Edit this page History Report an error Markdown