Definitioncore
Reducible flow graph
Also called reducible control flow graph.
A flow graph whose retreating edges are all back edges to dominators, so every loop has one entry, as in structured programs.
A reducible flow graph is a control flow graph whose loops all have a single entry. Formally, call an edge a back edge if dominates ; the graph is reducible if removing its back edges leaves no cycle. Then every cycle is part of a natural loop, the loops nest, and a depth-first search classifies the edges the same way whatever order it uses. Programs written with if, while, for, break and continue give reducible graphs; a goto into the middle of a loop can give an irreducible one. Many compiler algorithms are simpler or faster on reducible graphs, and some only work on them.
Definition
Let be a flow graph in which every node is reachable from . An edge is a back edge if dominates (dominators problem); let be the set of back edges. is reducible if the graph is acyclic. Otherwise it is irreducible.
For a depth-first search from with postorder numbers , an edge is retreating if . These are the edges to an ancestor in the DFS forest, self-loops included; every other edge, tree, forward or cross, goes to a smaller .
Equivalent forms
Claim. The following are equivalent.
- is reducible: without its back edges is acyclic.
- Every cycle of contains a back edge.
- Every simple cycle of has a node that dominates all nodes of the cycle.
- For every depth-first search from , the retreating edges are exactly the back edges.
- For some depth-first search from , every retreating edge is a back edge.
Proof. (1) ⇔ (2): a cycle avoids exactly when it is a cycle of .
(2) ⇒ (3): a simple cycle contains a back edge . For a node of the cycle, the part of the cycle from to does not contain , which appears once, right after . So is in the natural loop of , and dominates .
(3) ⇒ (2): if on a simple cycle dominates all its nodes, then the edge of the cycle that enters is a back edge. Every cycle contains a simple cycle, so it contains a back edge.
Back edges are retreating, for every DFS. If dominates , then lies on the tree path from to , whose nodes are the DFS ancestors of , so goes to an ancestor.
(1) ⇒ (4): let be retreating, so is a DFS ancestor of . A tree edge is never a back edge, because the tree path from to does not contain its descendant . If were not a back edge, the tree path from to and the edge would be a cycle without back edges. So retreating edges are back edges, and with the previous paragraph the two sets are equal.
(4) ⇒ (5) is clear. (5) ⇒ (1): the back edges include all retreating edges of that search, so every edge of goes to a smaller , and no cycle can do that.
So on a reducible graph the edge classes do not depend on the order of the search: the back edges, the retreating edges and the acyclic graph are the same for every DFS. On an irreducible graph the set of retreating edges can change with the order.
Other characterizations
- T1 and T2. Hecht and Ullman (1972) defined reducibility by two transformations: T1 deletes a self-loop, and T2 merges a node with a single predecessor into that predecessor. is reducible if and only if T1 and T2 reduce it to a single node. The proof is on the page of the T1-T2 reduction.
- Forbidden subgraph. Hecht and Ullman (1974) showed that is irreducible if and only if it contains a subgraph: nodes , , , where and may be , with paths , , , and that share no nodes except their ends. It is a cycle through and that can be entered at both. This page uses the result and does not prove it.
- Intervals. Allen and Cocke's interval analysis partitions the graph into single-entry regions and collapses each region to a node. is reducible exactly when repeating this ends in one node.
- Testing. The T1-T2 rules give a simple quadratic test. Tarjan (1974) tests reducibility in almost linear time with a depth-first search and disjoint set union.
What reducibility gives
- Loops. Every cycle lies in a natural loop, natural loops are disjoint or nested, and the loop nesting forest is unique.
- Fast iteration. The depth of a flow graph, for a DFS, is the largest number of retreating edges on a cycle-free path. Round-robin iteration in reverse postorder of the common bit-vector data-flow problems takes at most depth plus 2 passes (Kam-Ullman bound). On a reducible graph, a simple path that starts at has no retreating edge at all: such an edge is a back edge, so comes before on the path, and the edge would repeat . This is why the Cooper-Harvey-Kennedy dominator algorithm needs at most 2 passes.
- Elimination methods. Interval and T1-T2 elimination methods solve data-flow problems by collapsing the graph, which needs it to collapse to one node.
- Structured code. Decompilers that rebuild source code with loops and no
gotohave an easier job on reducible graphs, since each loop has one header.
Examples and non-examples
Reducible
The odd-sum program of the control flow graph page has one retreating edge, , and B2 dominates B5, so the graph is reducible. Removing leaves the acyclic graph , , . The nested and two-latch examples of the natural loop page are reducible too: each loop is entered only at its header.
Every graph without cycles is reducible, since is empty and nothing needs to be removed.
The smallest irreducible graph
r:
br p A B
A:
x = x + 1
br q B C
B:
x = x * 2
br q A C
C:
ret x
The cycle can be entered at and at from . Neither dominates the other, so neither nor is a back edge, is empty, and the cycle remains: the graph is irreducible. It is the subgraph with , , . A DFS that visits first makes retreating; one that visits first makes retreating. T1 and T2 get stuck: there is no self-loop, has no predecessor, and , and have two each.
Irreducible code in C
switch (n % 4) {
case 0: do { *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--count > 0);
}
This is Duff's device. The switch jumps into the middle of the do loop, so the loop has four entries and the CFG is irreducible. A goto into a loop body does the same, and so does some generated code.
Non-examples
- "Every loop has a header" is not a test by itself: an irreducible graph can contain natural loops as well. Reducibility asks that every cycle has one entry.
- Reducibility is not about the source language alone. A compiler transformation such as jump threading can turn a reducible graph into an irreducible one, so analyses should not assume it without checking.
Pitfalls
- Testing with one DFS the wrong way. Finding retreating edges with one DFS and checking that each one's target dominates its source is a correct test (form 5). Checking only that the retreating edges form loops, without the dominance test, is not.
- Assuming reducibility. Code that collapses natural loops or uses interval analysis must detect irreducible regions and handle them, by node splitting or by a loop nesting forest that allows several entries. Node splitting copies code and can make the graph much larger.
- Unreachable nodes. Dominance is defined only for reachable nodes. Delete unreachable nodes before the test.
Related definitions
- Natural loop: the loop of a back edge, the building block of loops in a reducible graph.
- T1-T2 reduction: the transformations that define reducibility and test it.
- Loop nesting forest: the loop structure, unique on reducible graphs and defined also for irreducible ones.
- Depth-first orders and DFS forest: retreating edges and the order of iterative analyses.
- Cooper-Harvey-Kennedy dominance: dominators in at most two passes on a reducible graph.
- Elimination data-flow methods: analyses that collapse a reducible graph.
References
- Matthew S. Hecht and Jeffrey D. Ullman, Flow graph reducibility, SIAM Journal on Computing 1(2) (1972), 188-202, the transformations T1 and T2, the definition of reducibility as reduction to one node, and the uniqueness of the limit graph
- Matthew S. Hecht and Jeffrey D. Ullman, Characterizations of reducible flow graphs, Journal of the ACM 21(3), 1974, the equivalent forms of reducibility, among them the forbidden (*) subgraph and the uniqueness of the acyclic graph of forward edges
- Robert Endre Tarjan, Testing flow graph reducibility, Journal of Computer and System Sciences 9(3), 1974, pages 355-365, the almost-linear test for reducibility with depth-first search and disjoint set union
- 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.3 (edges in a depth-first spanning tree), Section 9.6.4 (back edges and reducibility), Section 9.6.5 (depth of a flow graph) and Section 9.7 (region-based analysis)
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 2BH0BE Edit this page History Report an error Markdown