Definition
Reaching definitions is a data-flow analysis §3.15, a method that propagates facts along every possible path through a program, that finds which assignments may have produced the value a variable holds at each point. A definition is an instruction that writes a variable. A definition of variable reaches a point if execution can go from to along some path that does not write again. The analysis keeps a definition if it reaches along at least one path, so it is a may-analysis.
The idea
Programmers ask where a value came from every time they debug. Compilers ask the same question. If the only definition that reaches a use of x is x = 5, the compiler can replace the use with the constant 5. If a definition reaches no use at all, and computing its value has no other effect, the compiler can delete it. A checker asks it too: if a read of x can be reached along some path on which no definition of x occurs, that read can see an unwritten variable.
The idea is to label every definition and let the labels travel forward along the paths of the program, like tags on parcels moving through a sorting network. Each definition puts its label into the flow. A later write to the same variable removes that variable’s older labels, because the old value is gone. Where paths meet, the labels from all incoming paths are kept, because any of those paths may have run. The labels present at a point are the definitions that reach it. The picture stops at memory: a write through a pointer may or may not overwrite a given variable, and such a write cannot remove the variable’s older labels.
How it works
Reaching definitions is computed per basic block §3.14, a run of instructions with no jump into or out of its middle. Let be the set of definitions that reach the start of block , and the set that reaches its end.
Label the definitions .
Compute two sets per block . The generated set holds the definitions in that are still the last write to their variable at the end of . The killed set holds every definition, anywhere in the program, of a variable that writes, except the definitions in ; the values those definitions wrote cannot survive .
Set up the equations. The definitions reaching the start of are those leaving any predecessor, a block with an edge to : is the union of over all predecessors . The definitions leaving are those generated in , together with those that entered and were not killed: , where is the set of elements of that are not in .
Iterate. Start every at the empty set, , and recompute the blocks until no set changes.
Read off the answer for a use. The definitions of that reach a read of in block are the definitions of in , unless itself writes before the read; in that case only the last such write in reaches the read.
To find reads of unwritten variables, add at the entry one pseudo-definition per variable, meaning “not yet written”. If the pseudo-definition of reaches a read of , some path reads before writing it.
Writes through pointers need care. A write *p = 3 is a definition of whatever variable p points to. If an alias analysis §3.18, which works out what each pointer may point to, shows that p points to exactly one variable a, the write is a certain definition of a and kills the older definitions of a. If p may point to a or to b, the write is a possible definition of each. It is added to the reaching sets as a definition of both, and it kills nothing, because on some runs a keeps its old value. A call to a function that receives the address of a variable is treated the same way.
Worked example
The program adds the whole numbers from 0 to . It has four blocks.
B1, the entry: :
i = 0; :s = 0.B2:
if i >= n goto B4; otherwise continue to B3.B3: :
s = s + i; :i = i + 1;goto B2.B4:
return s.
The edges are , , and .
The generated and killed sets: B1 writes i and s, so and , the other definitions of s and i. B3 also writes s and i, so and . B2 and B4 write nothing, so both of their sets are empty and for them.
Round 1, with every starting empty:
.
, and .
, and .
.
Round 2: , which has changed. Then , , unchanged, and . Round 3 changes nothing, so the analysis has reached its fixed point §3.22.
Meaning: at return s in B4, two definitions of s reach, (s = 0) and (s = s + i). Both are real. With the loop body never runs, and the function returns the 0 written by . With the last write is , and the function returns . Because two definitions reach, a compiler may not replace s in B4 with the constant 0. Inside the loop, the read of i in is reached by on the first trip through the loop and by on later trips.
Stated precisely
A definition of variable reaches a program point when the control-flow graph §3.13 (CFG) contains a path from just after to that contains no other definition of . For each block , with its set of predecessor blocks, with , or the set of pseudo-definitions when reads of unwritten variables are to be found. Starting from empty sets, the iteration reaches the least solution of these equations, a fixed point.
The transfer function distributes over union: applying it to gives the same set as applying it to and to separately and taking the union. As a result, the least solution equals exactly the set of definitions that reach along some path of the CFG. The analysis loses nothing at merges; its only imprecision comes from paths that the graph contains but no run takes.
The guarantee, that every definition that reaches a point in some real run is in the computed set, rests on three assumptions.
The CFG contains every transfer a run can make.
Every write that can change , including writes through pointers and writes by called functions, appears as a definition of .
A definition kills another only when it certainly overwrites the same location.
Limits and common mistakes
Reaching is a may-fact. Two definitions reaching a use does not mean that both occur in one run, and a definition that reaches only along a path no run takes is reported anyway.
Reaching definitions identifies origins, not values or types. It says, for example, that the value of a register came from one of two definitions. Whether that value is a valid pointer, within bounds, or still alive needs other analyses applied to each origin.
Killing on an uncertain write is unsound. If *p = 3 may write a or b, treating it as a kill of the older definitions of a loses the runs in which p pointed to b and a kept its old value. In the same way, a write to arr[i] with an unknown index i is a possible definition of every element of arr and kills none.
Writes can come from outside the instructions being analyzed. A called function, another thread or an interrupt handler §6.12 can write a shared variable without any definition appearing in the analyzed function. Such writes must be modeled, for example as possible definitions at each call and, for shared variables, at every point.
Static single assignment (SSA) §3.38 form, a program form used by most modern optimizing compilers, renames variables so that each name has exactly one definition, and inserts a merge function, called a -function, where paths meet. In SSA form the reaching definition of a use is given by the name it uses, so this analysis is built into the program text. Memory still needs separate treatment.
Reaching definitions is easily confused with two neighbors. Live-variable analysis is backward and asks whether a value will be read later. Available expressions is a must-analysis and asks whether an expression has certainly been computed already.
Questions answered
Can two branches assign different pointer types to one register?
Yes: at the machine level two branches can write different kinds of value into the same register, and after the branches meet, both definitions reach every later use. A register holds only bits. Nothing in the hardware records whether those bits are a pointer to the stack, a pointer to some other memory region, or a plain number. One branch might load the address of a buffer into r1 while the other sets r1 to 7. Reaching definitions then reports that a later use of r1 can see either value.
A checker that must prove every memory access safe therefore has to make the use safe for each reaching definition. It has three options. It can keep the two paths apart and check the use once for each path, which is called path sensitivity §3.11; the Linux eBPF verifier §3.3, which checks programs for the eBPF §2.1 (extended Berkeley Packet Filter) instruction set before the kernel runs them, works this way. It can merge the two kinds into a combined kind that permits only the operations valid for both, for example no memory access through a value that may be the number 7. Or it can reject the program. It must not pick one reaching definition and ignore the other. A statically typed source language usually rules the situation out, because each variable has one declared type, but a register has no declared type.
Related entries: Def-use chain (§3.17), Alias analysis (§3.18), Static single assignment (SSA) (§3.38), Data-flow analysis (§3.15).