Algorithm Search⌘K

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 pp if every path in the control flow graph from the entry to pp evaluates it, and no operand of it is written between the last evaluation and pp. 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 nn blocks, mm edges and one entry block, in three-address code. Let EE be the set of expressions a op b that occur in the program, compared by their text, and ∣E∣|E| its size.

Output. For every block BB, the set IN[B]⊆E\mathrm{IN}[B] \subseteq E of expressions available at its start, and OUT[B]\mathrm{OUT}[B] at its end.

The framework.

property value
direction forward
may or must must: available only if available along every path
domain LL the subsets of EE
order ⊑\sqsubseteq ⊆\subseteq (a smaller set is a weaker claim)
meet ⊓\sqcap ∩\cap
⊤\top and ⊥\bot ⊤=E\top = E, ⊥=∅\bot = \emptyset
boundary value ι\iota ∅\emptyset at the start of the entry block: nothing has been computed yet
start value elsewhere EE (⊤\top)
transfer functions fB(X)=e_genB∪(X−e_killB)f_B(X) = e\_gen_B \cup (X - e\_kill_B), monotone and distributive
height hh ∣E∣\lvert E \rvert

The boundary value ∅\emptyset is ⊥\bot here, not ⊤\top, and the start value elsewhere is ⊤=E\top = E, not ∅\emptyset. Both choices matter, as the examples show. In the meet convention of the dataflow pages, the answer is the greatest fixed point, which for ⊆\subseteq means the largest sets that satisfy the equations.

Lattice and height. LL is the set of subsets of EE ordered by ⊆\subseteq. A strict chain removes one expression per step, so h=∣E∣h = |E|.

Transfer functions. An instruction x = a op b evaluates a op b and then writes x. Let killxkill_x be the set of expressions in EE that have x as an operand. The instruction has kill=killxkill = kill_x and gen={a op b}−killxgen = \{a \text{ op } b\} - kill_x; so s = s + a generates nothing, because it changes s right after it computes s + a. A constant or copy into x has gen=∅gen = \emptyset and kill=killxkill = kill_x. Composing the instructions of BB in order gives e_genBe\_gen_B, the expressions that BB computes and does not kill afterward, and e_killBe\_kill_B, the expressions with an operand that BB writes.

Equations.

IN[entry]=∅∩⋂P∈pred(entry)OUT[P]=∅,IN[B]=⋂P∈pred(B)OUT[P],OUT[B]=e_genB∪(IN[B]−e_killB).\mathrm{IN}[\mathrm{entry}] = \emptyset \cap \bigcap_{P \in \mathrm{pred}(\mathrm{entry})} \mathrm{OUT}[P] = \emptyset, \qquad \mathrm{IN}[B] = \bigcap_{P \in \mathrm{pred}(B)} \mathrm{OUT}[P], \qquad \mathrm{OUT}[B] = e\_gen_B \cup (\mathrm{IN}[B] - e\_kill_B).

Meet over all paths. For a path π\pi from the entry to the start of BB, fπ(∅)f_\pi(\emptyset) is the set of expressions that π\pi 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 ∅\emptyset and every other block to EE, 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 ∅\emptyset 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 EE at body, IN[head]={x∗2}∩OUT[body]\mathrm{IN}[\mathrm{head}] = \{x * 2\} \cap \mathrm{OUT}[\mathrm{body}] keeps x * 2, since nothing in the loop writes x. With the start value ∅\emptyset, the back edge from body brings ∅\emptyset 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 IN[B]\mathrm{IN}[B], then every CFG path from the entry to BB evaluates it after the last write to its operands. Every run follows a CFG path, so at the start of BB 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 ∣E∣|E| bits, one set operation costs O(∣E∣/w)O(|E| / w) word operations. The worklist algorithm applies at most n(∣E∣+1)n(|E| + 1) transfer functions and m(∣E∣+1)m(|E| + 1) intersections, which is O((n+m)∣E∣2/w)O((n + m) |E|^2 / w) time in the worst case. Round-robin iteration in reverse postorder needs at most d+2d + 2 passes for loop-connectedness depth dd (Kam-Ullman iteration bound), so O((d+2)(n+m)∣E∣/w)O((d + 2)(n + m) |E| / w).

Variants

  • Commutative forms. Treat a + b and b + a as 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 to x or y, tells copy propagation where a read of x can read y.
  • 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 * 2 after d = 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 e_gene\_gen e_kille\_kill 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
  • body kills s + a because it writes a. odd computes s + a and then writes s, so s + a is never available.
  • x * 2 is available at the end of next, but not at head: the path from entry to head has not computed it. The intersection at head removes it.
  • If next had a second instruction b = x * 2, it would be redundant, because x * 2 is in IN[next]\mathrm{IN}[\mathrm{next}]. CSE would replace it with b = a.

The worklist trace. In reverse postorder (entry, head, done, body, odd, next) the first pass is the last one. entry sends ∅\emptyset to head, which drops from EE to ∅\emptyset; then each block shrinks its successors once, and next sends {x∗2}\{x * 2\} to head, where the intersection with ∅\emptyset 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 ⊤=E\top = E instead of ∅\emptyset claims that every expression is available before the program has computed anything. In the running example it would put x * 2 into IN[head]\mathrm{IN}[\mathrm{head}], and CSE could then reuse a value that was never computed.

Known algorithms

algorithm time notes
Worklist dataflow algorithm O((n+m)∣E∣2/w)O((n + m) \lvert E \rvert^2 / w) worst case start at EE except at the entry
Round-robin in reverse postorder O((d+2)(n+m)∣E∣/w)O((d + 2)(n + m) \lvert E \rvert / w) Kam-Ullman iteration bound
One graph search per expression on the complement O(∣E∣(n+m))O(\lvert E \rvert (n + m)) see Reductions
Global value numbering depends on the method finds equal values, not only equal text
AlgorithmTimeSpacePropertiesCode
IFDS frameworkO(E D^3) worst · averageO(N D^2 + C D^2) extradeterministiciterative1 language
Worklist dataflow algorithmO((n + m)(h + 1)) worst · averageΘ(n) extradeterministiciterative1 language

Bounds

The output can have Θ(n∣E∣)\Theta(n \lvert E \rvert) entries: if the entry block computes nn expressions over parameters that nothing writes, all of them are available in all nn blocks. So listing the sets needs Ω(n∣E∣)\Omega(n \lvert E \rvert) time in the worst case.

Reductions

  • To a may problem by complement. Let N[B]=E−IN[B]N[B] = E - \mathrm{IN}[B], the expressions that are not available. By De Morgan's law, NN satisfies a may problem with union: its transfer function is X↦(e_killB−e_genB)∪(X−e_genB)X \mapsto (e\_kill_B - e\_gen_B) \cup (X - e\_gen_B), its boundary is EE at the entry, and its start value is ∅\emptyset. The greatest solution of the original is the complement of the least sets for NN.
  • To graph reachability. Through the complement, e∉IN[B]e \notin \mathrm{IN}[B] if and only if some path reaches BB either from the start of the entry or from a block that kills ee without generating it again, and passes through no block that generates ee. 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 b is available at an instruction y = 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

  1. 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)
  2. 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)
  3. 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)
  4. 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.

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)
WritingW2 basic
  • a contract.yaml