Problemintro
Available expressions
Also called available expressions analysis.
For each program point, find the expressions computed on every path to it with no later change to their operands.
Available expressions is the forward must problem of dataflow analysis. An expression such as x * 2 is available at a program point if every path in the control flow graph from the entry to evaluates it, and no operand of it is written between the last evaluation and . At such a point the value of the expression is already in some variable, so computing it again is redundant. The problem asks for the set of available expressions at the start and the end of every basic block. It drives global common subexpression elimination. It is a gen-kill problem, so the worklist dataflow algorithm computes its meet-over-all-paths solution exactly, but only if it starts from the right values.
In plain words
Suppose the program computes x * 2 and later computes x * 2 again. If x did not change between the two, the second computation can reuse the first result. But this is only safe if the first computation happened on every way to the second one.
The compiler walks forward through the program. Each block adds the expressions it computes and removes the expressions whose inputs it changes. Where paths join, an expression survives only if it survives on all of them. At the start of the program nothing has been computed yet. Inside loops the compiler starts with the hopeful guess "everything is available" and removes expressions until the guesses agree.
The answer can miss a reuse that is really safe, but it never claims an unsafe one.
Definition
Input. A control flow graph with blocks, edges and one entry block, in three-address code. Let be the set of expressions a op b that occur in the program, compared by their text, and its size.
Output. For every block , the set of expressions available at its start, and at its end.
The framework.
| property | value |
|---|---|
| direction | forward |
| may or must | must: available only if available along every path |
| domain | the subsets of |
| order | (a smaller set is a weaker claim) |
| meet | |
| and | , |
| boundary value | at the start of the entry block: nothing has been computed yet |
| start value elsewhere | () |
| transfer functions | , monotone and distributive |
| height |
The boundary value is here, not , and the start value elsewhere is , not . Both choices matter, as the examples show. In the meet convention of the dataflow pages, the answer is the greatest fixed point, which for means the largest sets that satisfy the equations.
Lattice and height. is the set of subsets of ordered by . A strict chain removes one expression per step, so .
Transfer functions. An instruction x = a op b evaluates a op b and then writes x. Let be the set of expressions in that have x as an operand. The instruction has and ; so s = s + a generates nothing, because it changes s right after it computes s + a. A constant or copy into x has and . Composing the instructions of in order gives , the expressions that computes and does not kill afterward, and , the expressions with an operand that writes.
Equations.
Meet over all paths. For a path from the entry to the start of , is the set of expressions that evaluates with no later write to an operand. MOP is the intersection over all such paths: the expressions available along every CFG path.
The fixed point computed. The worklist dataflow algorithm sets the entry to and every other block to , then shrinks the sets. It stops at the maximal fixed-point solution, the largest sets that satisfy the equations, and since the functions are distributive, this equals MOP when every block is reachable from the entry.
Starting the other blocks at instead also ends at a solution of the equations, but at a smaller one. In this loop x * 2 is available at head and in done, so c = x * 2 can reuse a:
entry:
i = 0
a = x * 2
jump head
head:
t = i < n
br t body done
body:
i = i + 1
jump head
done:
c = x * 2
ret c
With the start value at body, keeps x * 2, since nothing in the loop writes x. With the start value , the back edge from body brings to head, the intersection is empty, and nothing ever puts x * 2 back. Both are solutions; only the first is the greatest one.
Soundness. If an expression is in , then every CFG path from the entry to evaluates it after the last write to its operands. Every run follows a CFG path, so at the start of the value is already computed on that run.
Precision. CFG paths that no run takes make the sets smaller than the truth. Here every run that reaches r went through p, since both branches test the same c, so x * 2 is always already computed at r. The CFG path through q and then r does not compute it, so the analysis does not list it, and the redundant computation stays.
entry:
br c p q
p:
a = x * 2
jump m
q:
jump m
m:
br c r s
r:
b = x * 2
ret b
s:
ret c
Cost. With bit vectors of bits, one set operation costs word operations. The worklist algorithm applies at most transfer functions and intersections, which is time in the worst case. Round-robin iteration in reverse postorder needs at most passes for loop-connectedness depth (Kam-Ullman iteration bound), so .
Variants
- Commutative forms. Treat
a + bandb + aas the same expression by putting the operands in a fixed order first. This finds more redundancy at no extra cost. - Available copies. The same must analysis over copies
x = y, killed by a write toxory, tells copy propagation where a read ofxcan ready. - Available loads and checks. Memory loads and array bound checks can be tracked the same way, with a write to memory or to the index as the kill. Aliasing makes the kill sets larger.
- Value-based availability. Textual equality misses
b = a; c = b * 2afterd = a * 2. Global value numbering works with values instead of text and finds such cases.
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 | OUT | ||
|---|---|---|---|---|
| entry | none | i < n, i & 1, i + 1, s + a |
none | none |
| head | i < n |
none | none | i < n |
| body | x * 2, i & 1 |
s + a |
i < n |
i < n, x * 2, i & 1 |
| odd | none | s + a |
i < n, x * 2, i & 1 |
i < n, x * 2, i & 1 |
| next | none | i < n, i & 1, i + 1 |
i < n, x * 2, i & 1 |
x * 2 |
| done | none | none | i < n |
i < n |
bodykillss + abecause it writesa.oddcomputess + aand then writess, sos + ais never available.x * 2is available at the end ofnext, but not athead: the path fromentrytoheadhas not computed it. The intersection atheadremoves it.- If
nexthad a second instructionb = x * 2, it would be redundant, becausex * 2is in . CSE would replace it withb = a.
The worklist trace. In reverse postorder (entry, head, done, body, odd, next) the first pass is the last one. entry sends to head, which drops from to ; then each block shrinks its successors once, and next sends to head, where the intersection with changes nothing. The algorithm applies 6 transfer functions and 7 intersections, and each IN set changes at most once.
Non-example: the wrong boundary. Starting the entry at instead of claims that every expression is available before the program has computed anything. In the running example it would put x * 2 into , and CSE could then reuse a value that was never computed.
Known algorithms
| algorithm | time | notes |
|---|---|---|
| Worklist dataflow algorithm | worst case | start at except at the entry |
| Round-robin in reverse postorder | Kam-Ullman iteration bound | |
| One graph search per expression on the complement | see Reductions | |
| Global value numbering | depends on the method | finds equal values, not only equal text |
| 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 entry block computes expressions over parameters that nothing writes, all of them are available in all blocks. So listing the sets needs time in the worst case.
Reductions
- To a may problem by complement. Let , the expressions that are not available. By De Morgan's law, satisfies a may problem with union: its transfer function is , its boundary is at the entry, and its start value is . The greatest solution of the original is the complement of the least sets for .
- To graph reachability. Through the complement, if and only if some path reaches either from the start of the entry or from a block that kills without generating it again, and passes through no block that generates . One search per expression finds these blocks; see graph reachability.
- Used by partial redundancy elimination. Lazy code motion combines available expressions with very busy expressions and two more bit-vector problems to decide where to insert and delete computations.
Applications
- Global common subexpression elimination. If
a op bis available at an instructiony = a op b, keep the earlier result in a temporary and replace the instruction with a copy. See common subexpression elimination. - Partial redundancy elimination. Partial redundancy elimination also removes computations that are available on some paths only, by inserting the computation on the other paths.
- Copy propagation and redundant loads. The available copies and available loads variants find where a copy or a load can be reused.
- Bounds-check removal. A check that is available at a second check of the same index and length makes the second check redundant.
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.2.6 (Available Expressions, with the e_gen and e_kill sets, the equations, and the reason the start value is the set of all expressions)
- 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)
- Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Section 2.1.1 (Available Expressions Analysis, where the answer is the largest solution)
- 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 (common subexpression elimination as an instance of the iterative algorithm on a meet semilattice)
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 9DTDJA Edit this page History Report an error Markdown