Problemintro
Reaching definitions
Also called reaching definitions analysis.
For each program point, find the definitions of variables that can reach it along some path without being overwritten.
Reaching definitions is the forward may problem of dataflow analysis. A definition is an instruction that writes a variable, and a definition of reaches a program point if some path in the control flow graph goes from just after to with no other definition of on it. The problem asks, for every basic block, for the set of definitions that reach its start. The answer links each read of a variable to the writes that may have produced its value; these links are the use-def half of def-use chains. Reaching definitions is a gen-kill problem: it is distributive, so the worklist dataflow algorithm computes the meet-over-all-paths solution exactly.
In plain words
Every line that stores a value into a variable is a definition. Pick a line that reads a variable. Which of the stores could have put the value there? Follow the program backward from the read: a store counts if you can get from it to the read without passing another store into the same variable.
The compiler answers this for every block at once. It walks forward through the program. Each block adds its own stores and removes the older stores that they hide. Where paths join, a store survives if it survives on at least one of them. A loop carries stores from the bottom of the loop back to its top, so the walk repeats until nothing new arrives.
The answer may include a store that no real run delivers, but it never misses one that a run does deliver.
Definition
Input. A control flow graph with blocks, edges and one entry block, whose instructions are in three-address code. Let be the set of definitions, the instructions that write a variable, and let be its size.
Output. For every block , the set of definitions that reach the start of , and the set that reach its end.
The framework.
| property | value |
|---|---|
| direction | forward |
| may or must | may: a definition reaches if it reaches along some path |
| domain | the subsets of |
| order | (a larger set is a weaker claim) |
| meet | |
| and | , |
| boundary value | : no definition of the procedure has run yet |
| start value elsewhere | () |
| transfer functions | , monotone and distributive |
| height |
This page and the other dataflow pages use the meet convention: is the start value and the answer is the greatest fixed point. In a may problem the order is , so "greatest" means the smallest sets. Here happens to equal ; the variant with pseudo definitions below has .
Lattice and height. is the set of all subsets of , and a strict chain adds one definition per step. So .
Transfer functions. For one instruction that defines , and is the set of the other definitions of . A block is the composition of its instructions in order; for two steps the composition is and . So is the set of the last definition of each variable that writes, and is the set of all other definitions of the variables that writes. A block that writes twice keeps only the second write in .
Equations.
The entry block has , together with the outputs of any predecessors that it has.
Meet over all paths. For a path that ends just before , is the set of definitions that are the last write of their variable along . The meet over all such paths is the union, which is the set of definitions that reach the start of along some path. This is the exact meaning of "reaches" on the CFG.
The fixed point computed. The worklist dataflow algorithm starts every set at , puts at the entry, and grows the sets until the equations hold. The result is the maximal fixed-point solution: the smallest sets that satisfy the equations.
Soundness. If a run reaches the start of and the last write of on that run was , then . The run follows a CFG path, and on that path is the last write of , so . Since and functions are distributive, MFP = MOP when every block is reachable from the entry.
Precision. The answer is exact for CFG paths but not for runs, since some CFG paths are never taken. Two branches on the same unchanged condition c give an example:
entry:
br c left right
left:
x = 1
jump mid
right:
x = 2
jump mid
mid:
br c use other
use:
y = x
ret y
other:
ret c
Both definitions of x reach use, because the CFG has the path through right and then use. No run takes that path: if c is false at the first branch, it is false at the second one too. So the analysis cannot tell that y is always 1.
Cost. With bit vectors of bits, one union or difference costs word operations, where is the word size. The worklist algorithm applies at most transfer functions and does at most unions, so it runs in time. Processed in reverse postorder, round-robin iteration needs at most passes, where is the loop-connectedness depth (the Kam-Ullman iteration bound), for a total of . Real programs have small .
Variants
- Pseudo definitions at the entry. Add one pseudo definition for each variable and set . Then reaches a read of exactly when some path reads before writing it. A parameter is fine; any other variable is a possible use before initialization. Here the boundary is not , and starting the entry at would hide every such warning. Nielson, Nielson and Hankin use this form.
- Instruction level. The same equations with one node per instruction give the answer at every instruction. The usual practice is to solve at block level and then scan each block once.
- Ambiguous definitions. A store through a pointer or a call may write . Such a write generates a definition of but does not kill the others, because it might not write .
- Per variable. For a use of , only the definitions of matter. Splitting by variable gives smaller vectors and the same answer.
- Interprocedural. Across calls, the definitions in the callee and the definitions that reach the call are matched by the interprocedural dataflow analysis and IFDS framework methods.
Examples
The running example of dataflow analysis has seven definitions:
| name | block | instruction |
|---|---|---|
| entry | i = 0 |
|
| entry | s = 0 |
|
| head | t = i < n |
|
| body | a = x * 2 |
|
| body | u = i & 1 |
|
| odd | s = s + a |
|
| next | i = 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
Gen and kill sets, and the solution:
| block | IN | OUT | ||
|---|---|---|---|---|
| entry | none | |||
| head | none | all seven | all seven | |
| body | none | all seven | all seven | |
| odd | all seven | all but | ||
| next | all seven | all but | ||
| done | none | none | all seven | all seven |
What the answer says:
- In
head, the read ofisees and . Two definitions with different values reach it, soiis not a constant there. - In
odd, the read ofssees (first odd value) and (an earlier odd value). The read ofasees only . - In
body,a = x * 2reads only the parameterx, which no definition in the procedure writes. Sox * 2is invariant in the loop and loop-invariant code motion can hoist it.
The worklist in reverse postorder (entry, head, done, body, odd, next) processes 11 blocks: six in the first pass, then head, body, done, odd and next again after comes back along the edge from next to head. It does 13 unions and changes an IN set 11 times. The largest number of changes of one IN set is 3, at next, well below .
Known algorithms
| algorithm | time | notes |
|---|---|---|
| Worklist dataflow algorithm | worst case | Kildall 1973; the usual choice |
| Round-robin in reverse postorder | the Kam-Ullman iteration bound for rapid frameworks | |
| One graph search per definition | see the reduction below | |
| Elimination methods | see that page | solve by graph reduction instead of iteration; best on reducible graphs |
| SSA form | after SSA construction, each use has exactly one definition | phi-functions merge the definitions that reach a join |
| 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 alone can have entries. In a chain of blocks where block writes its own variable , every definition reaches every later block, so the answer has pairs. Any algorithm that lists the sets therefore needs time in the worst case. A search per definition matches this up to the factor . The bit-vector algorithms win in practice because one word operation handles definitions.
Reductions
- To graph reachability. Fix a definition in block with . Then if and only if the CFG has a path with in which no inner block kills . A depth-first search from the successors of that enters a killing block but does not leave it finds these blocks. One search per definition solves the problem, so every bit of a gen-kill problem is a graph reachability question.
- To Datalog. The two rules
reach(D, B) :- gen(P, D), edge(P, B).andreach(D, B) :- reach(D, P), not kill(P, D), edge(P, B).compute ; see Datalog-based program analysis. - From uninitialized variables. Possible uses before initialization reduce to reaching definitions with pseudo definitions at the entry, as in Variants.
Applications
- Def-use and use-def chains. For each read of in block , the definitions of in (or the last write before the read in ) are its use-def chain. Inverting the links gives def-use chains.
- Constant and copy propagation. If the only definition that reaches a read of
xisx = 4, the read can use 4. If it is the copyx = yandyis not written between, copy propagation can ready. - Loop-invariant code motion. An operand is invariant in a loop when all its reaching definitions are outside the loop, or it is a constant.
- Warnings. With pseudo definitions, a read that a pseudo definition reaches is a possible use of an uninitialized variable.
- Slicing and debugging. Program slicing follows use-def chains backward to find the instructions that can affect a value.
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.4 (Reaching Definitions, with the gen and kill sets, the equations and the iterative algorithm) and Section 9.6.7 (Speed of Convergence of Iterative Data-Flow Algorithms)
- Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Section 2.1.2 (Reaching Definitions Analysis, stated with a join and with pseudo definitions for the start)
- 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 that solves this and the other bit-vector problems)
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 7FJJMB Edit this page History Report an error Markdown