Algorithm Search⌘K

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 t→ht \to h, an edge whose target hh dominates its source tt. The loop is hh together with every block that can reach tt without passing through hh. Then hh, 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 tt that stops at hh finds the loop in time proportional to its size.

Definition

Let G=(N,E,r)G = (N, E, r) be a control flow graph in which every node is reachable from the entry rr. A node dd dominates vv if every path from rr to vv contains dd (dominators problem).

  • An edge (t,h)(t, h) is a back edge if hh dominates tt. A self-loop (h,h)(h, h) is a back edge.
  • The natural loop of the back edge (t,h)(t, h) is
L(t,h)={h}∪{ v∈N:there is a path from v to t that does not contain h }.L(t, h) = \{h\} \cup \{\, v \in N : \text{there is a path from } v \text{ to } t \text{ that does not contain } h \,\}.
  • hh is the header of the loop and tt is a latch. The body is L(t,h)L(t, h). An edge (u,w)(u, w) with u∈Lu \in L and w∉Lw \notin L is an exit edge.
  • When several back edges share the header hh, compilers usually treat the union of their natural loops as one loop with header hh. 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 L(t,h)L(t, h), 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 hh.

Proof. Every node is pushed at most once, because it is added to looploop when it is pushed and never removed, and hh is in looploop 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 L(t,h)L(t, h). For each pushed node vv we show that a path from vv to tt avoids hh. For v=tv = t the path of length 0 does, since t≠ht \ne h when tt is pushed. A node pp is pushed in line 6 as a predecessor of a popped node vv; vv was pushed, so v≠hv \ne h and vv reaches tt without hh, and p≠hp \ne h because hh is in looploop. So the path p,v,…,tp, v, \ldots, t avoids hh.

Every node of L(t,h)L(t, h) is in the result. hh is added in line 1. Let v≠hv \ne h have a path v=v0,v1,…,vk=tv = v_0, v_1, \ldots, v_k = t that avoids hh; we use induction on kk. If k=0k = 0, then v=t≠hv = t \ne h is added in line 2. Otherwise v1v_1 has a shorter path to tt that avoids hh, so v1v_1 is added, and since v1≠hv_1 \ne h it was pushed, so it is popped later, and line 6 adds its predecessor vv if it is not already there. □\square

The search never passes hh, so it stays inside the loop even if the header has many predecessors outside.

Properties

Claim 2. Let L=L(t,h)L = L(t, h).

  1. hh dominates every node of LL.
  2. Every edge from a node outside LL to a node of LL ends at hh. So hh is the only entry.
  3. LL is strongly connected: every node of LL reaches every other one by a path inside LL.

Proof. (1) Let v∈Lv \in L, v≠hv \ne h, with a path from vv to tt that avoids hh. If a path from rr to vv avoided hh, then together they would give a path from rr to tt that avoids hh, but hh dominates tt.

(2) Let (u,w)(u, w) be an edge with w∈Lw \in L and w≠hw \ne h. Then ww has a path to tt that avoids hh. If u≠hu \ne h, then uu, ww, and that path form a path from uu to tt that avoids hh, so u∈Lu \in L. If u=hu = h, then u∈Lu \in L too. So an edge from outside ends at hh.

(3) From v∈Lv \in L, a path to tt that avoids hh stays in LL, since each node on it reaches tt without hh; then the edge (t,h)(t, h) leads to hh. From hh to v∈Lv \in L: take a path from rr to vv; by (1) it contains hh; take the part after the last hh. Each node ww on that part reaches vv without hh, and vv reaches tt without hh (or v=hv = h, and the part is just hh), so w∈Lw \in L. □\square

Claim 3 (nesting). Two natural loops with different headers are either disjoint or one is a proper subset of the other.

Proof. Let L1=L(t1,h1)L_1 = L(t_1, h_1) and L2=L(t2,h2)L_2 = L(t_2, h_2) with h1≠h2h_1 \ne h_2 share a node vv. By Claim 2, h1h_1 and h2h_2 both dominate vv. The dominators of a node lie on one path of the dominator tree, so one of them dominates the other; say h2h_2 strictly dominates h1h_1. Then h2∉L1h_2 \notin L_1, since every node of L1L_1 is dominated by h1h_1, and h1h_1 does not dominate h2h_2. In particular v≠h2v \ne h_2, so vv has a path to t2t_2 that avoids h2h_2. By the proof of Claim 2 (3), there is a path from h1h_1 to vv inside L1L_1, so it avoids h2h_2. Joined, they show that h1h_1 reaches t2t_2 without h2h_2, so h1∈L2h_1 \in L_2. Now every u∈L1u \in L_1 reaches t1t_1 inside L1L_1, then h1h_1 by the back edge, then t2t_2 as just shown, all without h2h_2. So u∈L2u \in L_2, and L1⊆L2L_1 \subseteq L_2. The inclusion is proper because h2∈L2∖L1h_2 \in L_2 \setminus L_1. □\square

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 B1→B2B1 \to B2, B2→B3B2 \to B3, B2→B6B2 \to B6, B3→B4B3 \to B4, B3→B5B3 \to B5, B4→B5B4 \to B5, B5→B2B5 \to B2. B2 dominates B5, so B5→B2B5 \to B2 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 {B2,B3,B4,B5}\{B2, B3, B4, B5\} with header B2 and the exit edge B2→B6B2 \to B6.

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 B0→B1B0 \to B1, B1→B2B1 \to B2, B1→B6B1 \to B6, B2→B3B2 \to B3, B3→B4B3 \to B4, B3→B5B3 \to B5, B4→B3B4 \to B3, B5→B1B5 \to B1. Computed by the definition, the back edges are B4→B3B4 \to B3 and B5→B1B5 \to B1. Their natural loops are {B3,B4}\{B3, B4\} and {B1,B2,B3,B4,B5}\{B1, B2, B3, B4, B5\}, 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 jj over all 0≤j<i<n0 \le j < i < n; the toy interpreter returns 4 for n=4n = 4 and 10 for n=5n = 5.

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 ii is a multiple of 3, and B3 jumps back after adding ii. Both B2→B1B2 \to B1 and B3→B1B3 \to B1 are back edges. Their natural loops are {B1,B2}\{B1, B2\} and {B1,B2,B3}\{B1, B2, B3\}, which share the header, so Claim 3 does not apply; a compiler merges them into one loop {B1,B2,B3}\{B1, B2, B3\}. On n=4n = 4 the run returns 1+2+4=71 + 2 + 4 = 7.

Non-examples

  • A cycle with two entries. In the graph r→ar \to a, r→br \to b, a→ca \to c, b→db \to d, c→dc \to d, d→cd \to c, the cycle c,dc, d is entered at cc from aa and at dd from bb. Neither cc nor dd dominates the other, so neither c→dc \to d nor d→cd \to c 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 aa first has d→cd \to c as a retreating edge, an edge to a DFS ancestor. It is not a back edge, because cc does not dominate dd.
  • 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 {c,d}\{c, d\} above holds none.

Pitfalls

  • Searching forward from the header. The blocks that hh reaches are not the loop: they include the code after the loop. The search must go backward from the latch and stop at hh.
  • Not stopping at the header. If the search continues through hh, 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 hh overlap without nesting. Merge them, or a loop pass may see the same header twice.

References

  1. 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)
  2. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 7 (Control-Flow Analysis), back edges, natural loops and their nesting
  3. 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
  4. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/DSAB3F/text.yaml