Example
Consider the following program: b = 3 c = 5 a = f(b * c) The set of live variables between lines 2 and 3 is because both are used in the multiplication on line 3. But the set of live variables after line 1 is only , since variablec
is updated later, on line 2. The value of variable a
is not used in this code.
Note that the assignment to a
may be eliminated as a
is not used later, but there is insufficient information to justify removing all of line 3 as f
may have side effects (printing b * c
, perhaps).
Expression in terms of dataflow equations
Liveness analysis is a "backwards may" analysis. The analysis is done in a backwards order, and the dataflow confluence operator isSecond example
The in-state of b3 only contains ''b'' and ''d'', since ''c'' has been written. The out-state of b1 is the union of the in-states of b2 and b3. The definition of ''c'' in b2 can be removed, since ''c'' is not live immediately after the statement. Solving the data flow equations starts with initializing all in-states and out-states to the empty set. The work list is initialized by inserting the exit point (b3) in the work list (typical for backward flow). Its computed in-state differs from the previous one, so its predecessors b1 and b2 are inserted and the process continues. The progress is summarized in the table below. Note that b1 was entered in the list before b2, which forced processing b1 twice (b1 was re-entered as predecessor of b2). Inserting b2 before b1 would have allowed earlier completion. Initializing with the empty set is an optimistic initialization: all variables start out as dead. Note that the out-states cannot shrink from one iteration to the next, although the out-state can be smaller than the in-state. This can be seen from the fact that after the first iteration the out-state can only change by a change of the in-state. Since the in-state starts as the empty set, it can only grow in further iterations.References
{{Compiler optimizations Compiler optimizations Data-flow analysis Static program analysis