Problemcore
Very busy expressions
Also called anticipable expressions, anticipated expressions.
For each program point, find the expressions that every path from it computes before any of their operands change.
Very busy expressions is the backward must problem of dataflow analysis. An expression such as i + 1 is very busy (or anticipated) at a program point if every path in the control flow graph from to a return evaluates it before any of its operands is written. At such a point the expression can be computed early: every path that goes on to a return would compute the same value anyway. The problem asks for the set of very busy expressions at the start and the end of every basic block. It is the mirror image of available expressions (backward instead of forward) and has the same meet as available expressions and the same direction as live variable analysis. It is the first pass of lazy code motion. It is a gen-kill problem, so the worklist dataflow algorithm on the reverse CFG computes its meet-over-all-paths solution.
In plain words
Stand at some point in a program and look ahead. If every way forward computes i + 1 before it changes i, then the value of i + 1 is already decided where you stand. You could compute it here, once, and let the later places reuse it.
The compiler finds these expressions by reading the program backward from the returns. A block adds the expressions it computes and drops the ones whose inputs it changes before computing them. Where the program branches, an expression stays only if every branch computes it. Loops make the compiler repeat the backward walk until nothing changes.
Moving a computation to such a point never adds work to a run that returns, which is why the answer is safe to use.
Definition
Input. A control flow graph with blocks and edges, in three-address code, with the set of expressions a op b that occur in it.
Output. For every block , the set of expressions very busy at its start, and at its end.
The framework.
| property | value |
|---|---|
| direction | backward: the analysis runs on the reverse CFG |
| may or must | must: busy only if every path evaluates the expression |
| domain | the subsets of |
| order | |
| meet | |
| and | , |
| boundary value | at the end of each block that ends in ret: after the return nothing is evaluated |
| start value elsewhere | () |
| transfer functions | , monotone and distributive |
| height |
As for available expressions, the boundary is and the start value elsewhere is . The answer is the greatest fixed point, here the largest sets.
Lattice and height. The subsets of ordered by ; a strict chain removes one expression per step, so .
Transfer functions. Read each block from its end to its start. An instruction x = a op b first evaluates a op b and then writes x. Going backward, the write comes first: it removes , the expressions with x as an operand, and then the evaluation adds a op b. So the step is , and s = s + a makes s + a very busy before it. A constant or copy into x only removes . Composing the steps from the last instruction to the first gives , the expressions that evaluates before writing any of their operands, and , the expressions with an operand that writes.
Equations. With the empty intersection equal to :
Meet over all paths. For a path from the end of to the end of a ret block, is the set of expressions that evaluates before writing an operand. MOP is the intersection over all such paths.
The fixed point computed. The worklist dataflow algorithm runs on the reverse CFG with the ret blocks as start nodes, gives them , starts every other set at , and shrinks the sets. It stops at the maximal fixed-point solution, which equals MOP when every block can reach a ret block, because the functions are distributive.
Soundness. If and a run leaves and later returns, the part of the run up to the return follows a CFG path from the end of to a ret block, and on that path is evaluated before its operands change. So computing at the end of gives the value that the run would compute later, and adds no evaluation to the run. A run that never returns is not covered; see Variants.
Precision. CFG paths that no run takes make the sets smaller than the truth. Here every run computes x * 2 after entry: through p if c is true, and through s if c is false. But the CFG path through q and then r computes nothing, so x * 2 is not very busy at the end of entry, and it cannot be hoisted there.
entry:
br c p q
p:
a = x * 2
jump m
q:
jump m
m:
br c r s
r:
ret a
s:
b = x * 2
ret b
Cost. With bit vectors of bits, the worklist algorithm applies at most transfer functions and intersections of words each, so time in the worst case. Round-robin iteration in reverse postorder of the reverse CFG takes , where is the loop-connectedness depth of the reverse CFG (Kam-Ullman iteration bound).
Variants
- Paths that never return. A block from which no
retblock can be reached, such as an infinite loop, has no path to a return, so the claim "every such path evaluates " holds for every and MOP is . The fixed point is the greatest solution of the loop's own equations, which can be smaller than : forL: x = y * 2; jump Lwith , the set atLis . The value flows to the blocks before it. An optimizer that hoists an expression into such a region can add an evaluation that the run would never do; if the expression can trap, such as a division, that changes the behavior. A safe optimizer either adds an exit edge from each such region, or treats the end of each such region as a boundary with . - Anticipated expressions in lazy code motion. The dragon book calls these sets anticipated expressions. Lazy code motion then uses them with available expressions to find the earliest safe point for each computation.
- Partially anticipated expressions. Replacing with gives the may version: some path evaluates the expression. Some forms of partial redundancy elimination use it as well.
Examples
The running example of dataflow analysis has five expressions: i < n, x * 2, i & 1, s + a and i + 1.
entry:
i = 0
s = 0
jump head
head:
t = i < n
br t body done
body:
a = x * 2
u = i & 1
br u odd next
odd:
s = s + a
jump next
next:
i = i + 1
jump head
done:
ret s
| block | IN (busy at the start) | OUT (busy at the end) | ||
|---|---|---|---|---|
| entry | none | i < n, i & 1, i + 1, s + a |
none | i < n |
| head | i < n |
none | i < n |
none |
| body | x * 2, i & 1 |
s + a |
x * 2, i & 1, i + 1 |
i + 1 |
| odd | s + a |
s + a |
s + a, i + 1 |
i + 1 |
| next | i + 1 |
i < n, i & 1, i + 1 |
i + 1 |
i < n |
| done | none | none | none | none |
i + 1is very busy at the end ofbody: bothoddandnextcompute it, andodddoes not writei. Soi + 1could be computed once at the end ofbody. This makes the code smaller (code hoisting) but not faster, since each path still computes it once.i < nis very busy at the end ofentryand ofnext, becauseheadcomputes it first thing.- Nothing is very busy at the end of
head: the path todoneevaluates nothing before it returns. oddhass + ain both and , but the evaluation comes before the write, sos + ais busy at its start.
The worklist on the reverse CFG, in the order done, head, next, odd, body, entry, needs one pass: 6 transfer functions and 7 intersections, with each set changing at most once.
Known algorithms
| algorithm | time | notes |
|---|---|---|
| Worklist dataflow algorithm on the reverse CFG | worst case | start at except after the ret blocks |
| Round-robin in reverse postorder of the reverse CFG | Kam-Ullman iteration bound | |
| One backward search per expression on the complement | see Reductions |
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| IFDS framework | O(E D^3) worst · average | O(N D^2 + C D^2) extra | deterministiciterative | 1 language |
| Worklist dataflow algorithm | O((n + m)(h + 1)) worst · average | Θ(n) extra | deterministiciterative | 1 language |
Bounds
The output can have entries: if the last block before the only return computes expressions over parameters that nothing writes, and every path goes through it, every expression is very busy in all blocks. So listing the sets needs time in the worst case.
Reductions
- To a may problem by complement. The sets of expressions that are not very busy satisfy a backward may problem with union, the transfer function , and the boundary after each
retblock. - To graph reachability on the reverse CFG. Through the complement, if and only if some path from the end of reaches a return, or a block that writes an operand of before it evaluates , through blocks that do not evaluate . One backward search per expression finds these blocks. This is graph reachability on the transpose graph.
- Mirror of available expressions. On the reverse CFG with the order of the instructions in each block reversed, very busy expressions become available expressions, with one change: an instruction
x = a op bkeepsa op beven whenxis an operand, since the evaluation comes first in the original order.
Applications
- Code hoisting. An expression that is very busy at the end of a block can be computed there once instead of in each successor. This saves space, not time.
- Lazy code motion and partial redundancy elimination. Anticipation tells lazy code motion where a computation may be inserted without adding work on any path. Together with available expressions it gives the earliest safe placement, and two more passes push it as late as possible.
- Safe loop-invariant code motion. Hoisting an invariant expression to the loop preheader is safe from new traps if the expression is very busy at the preheader, that is, every path from there evaluates it. See loop-invariant code motion.
References
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Pearson Addison-Wesley, 2007, Section 9.5.4 (Anticipation of Expressions, the backward must problem used as the first pass of lazy code motion)
- Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Section 2.1.3 (Very Busy Expressions Analysis, a backward analysis where the answer is the largest solution)
- Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 9 (Data-Flow Analysis), Section 9.2.4 (Other Data-Flow Problems)
- Gary A. Kildall, A unified approach to global program optimization, Proceedings of the 1st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL 1973), 194-206, the whole paper (the iterative algorithm on a meet semilattice, which solves this problem on the reverse graph)
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 | W2 basic |
|
Tag JMEDV5 Edit this page History Report an error Markdown