Sub topic 16
The data flow analysis framework
This chapter assumes you can split three-address code into basic blocks and draw a control flow graph (CFG). It also assumes you are comfortable with union, intersection and difference on sets. Later chapters apply the framework to reaching definitions, live variables and available expressions. Here we build the shared structure they all use.
What a data flow analysis computes
A data flow analysis answers a question about every program point, such as "which definitions might reach here?" or "which variables might be read later?" The compiler cannot run the program on every input. So it computes a safe answer that holds on all paths through the CFG.
The framework attaches two sets to each basic block B:
- IN[B] is the set of facts true just before the first instruction of B.
- OUT[B] is the set of facts true just after the last instruction of B.
A fact is an item in the analysis domain. For reaching definitions it is a definition. For live variables it is a variable. For available expressions it is an expression.
The four ingredients
Every analysis in this tutorial is defined by four choices.
- Direction. Forward analyses push information along edges, from predecessors to successors. Backward analyses push it against the edges.
- Transfer function. This describes what one block does to the facts. It is written with two sets per block, gen and kill.
- Meet operator. This describes how facts combine where paths join. It is union or intersection.
- Boundary value. This is the value at the entry block (forward) or exit block (backward). It is usually the empty set.
There is also an initial value for every other block. It is chosen to match the meet operator, as explained below.
Gen and kill
- gen[B] holds the facts that block B creates and that are still true at the end of B.
- kill[B] holds the facts that block B invalidates.
The transfer function for a forward analysis is:
OUT[B] = gen[B] U (IN[B] - kill[B])Facts that B does not kill pass through. Facts that B generates are added. A backward analysis has the same shape with the roles of IN and OUT swapped:
IN[B] = use[B] U (OUT[B] - def[B])Here use plays the role of gen and def plays the role of kill.
Direction and meet at a glance
| Analysis | Direction | Meet | Boundary | Initial value for other blocks |
|---|---|---|---|---|
| Reaching definitions | Forward | Union | OUT[entry] = empty | Empty |
| Live variables | Backward | Union | IN[exit] = empty | Empty |
| Available expressions | Forward | Intersection | OUT[entry] = empty | All expressions |
The meet operator follows from the question being asked.
- A may analysis asks whether a fact holds on at least one path. It uses union. Reaching definitions and live variables are may analyses.
- A must analysis asks whether a fact holds on every path. It uses intersection. Available expressions is a must analysis.
The initial value matters for must analyses. Intersection starts from the full set, because starting from empty would erase every fact at the first join and the answer could never grow back. Union starts from empty, because facts are added until nothing changes.
Safe answers
The compiler needs a conservative answer. The word conservative has a different meaning for each analysis type.
- For a may analysis, a larger set is safe. Reporting that a definition might reach a point can only prevent an optimization. Missing a real one could permit a wrong one.
- For a must analysis, a smaller set is safe. Claiming an expression is available when it is not would allow an invalid reuse.
The solution computed by iteration is the most precise safe answer the framework can give. It is called the maximum fixed point for must analyses. For the may analyses here it is the least fixed point, because iteration begins with empty sets and grows them.
Where paths meet
The meet operator is applied when a block has several predecessors (forward) or several successors (backward).
Forward: IN[B] = meet over P in pred(B) of OUT[P]
Backward: OUT[B] = meet over S in succ(B) of IN[S]The diagram shows a forward analysis at a join block.
A small worked example
Consider a CFG with blocks B1, B2 and B3. B1 flows to B2 and B3, and both B2 and B3 flow to B4. Let the analysis be forward with union as the meet, and let facts be definitions d1 to d3.
- gen[B1] = {d1}, kill[B1] = {d3}
- gen[B2] = {d2}, kill[B2] = {d1}
- gen[B3] = {d3}, kill[B3] = empty
- gen[B4] = empty, kill[B4] = empty
Start with OUT[entry] as empty, so IN[B1] is empty.
- OUT[B1] = {d1} U (empty - {d3}) = {d1}
- IN[B2] = {d1}, so OUT[B2] = {d2} U ({d1} - {d1}) = {d2}
- IN[B3] = {d1}, so OUT[B3] = {d3} U ({d1} - empty) = {d1, d3}
- IN[B4] = {d2} U {d1, d3} = {d1, d2, d3}, so OUT[B4] = {d1, d2, d3}
Because this CFG has no loops, one pass in topological order is enough. With loops, a block's IN depends on a later block's OUT, so the compiler must repeat the computation. That repetition is the subject of the iterative solver chapter.
Computing gen and kill for a block
Gen and kill describe the whole block, so you build them by scanning its instructions in order.
For a forward analysis, process instructions from first to last. For each instruction, remove from gen anything it kills, then add what it generates, and add what it kills to kill. For a backward analysis, scan from last to first.
def block_gen_kill(instrs, generates, kills):
gen, kill = set(), set()
for ins in instrs:
gen -= kills(ins)
gen |= generates(ins)
kill |= kills(ins)
return gen, kill
# Facts are definitions named by instruction label.
# Two definitions of x in one block: d1 is killed by d2.
block = [("d1", "x"), ("d2", "x"), ("d3", "y")]
all_defs = [("d1", "x"), ("d2", "x"), ("d3", "y"), ("d4", "x")]
def generates(ins):
return {ins[0]}
def kills(ins):
return {d for d, v in all_defs if v == ins[1] and d != ins[0]}
gen, kill = block_gen_kill(block, generates, kills)
print(sorted(gen)) # ['d2', 'd3']
print(sorted(kill)) # ['d1', 'd2', 'd4']The block defines x twice, so only d2 survives in gen. The definition d4 of x in some other block is killed. Note that kill contains d2 as well. This is harmless, because the transfer function adds gen after removing kill.
Common errors
- Mixing up direction. Live variables is backward. Computing IN from predecessors gives a wrong answer.
- Wrong initial value for a must analysis. Starting available expressions at empty yields an empty answer everywhere inside loops.
- Applying the transfer function before the meet. Compute the meet first, then transfer.
- Forgetting that gen is the final state. A definition overwritten later in the same block is not in gen.
- Using kill for what a block merely reads. Reads matter only for use in live variables.
Exercise
A CFG has blocks B1, B2 and B3, with edges B1 to B2, B1 to B3 and B2 to B3. For a forward available-expressions analysis, gen[B1] = {e1}, gen[B2] = {e2}, kill[B2] = {e1} and all other gen and kill sets are empty. Compute IN[B3], with OUT[entry] empty and IN[B1] empty.
Show answer
OUT[B1] = {e1}. IN[B2] = {e1}, so OUT[B2] = {e2} U ({e1} - {e1}) = {e2}. IN[B3] is the intersection of OUT[B1] and OUT[B2], which is {e1} intersect {e2} = empty. So no expression is available on entry to B3.
Exercise
Why does a must analysis such as available expressions initialize OUT of non-entry blocks to the set of all expressions, while reaching definitions initializes them to empty?
Show answer
Intersection can only shrink a set. If a must analysis started at empty, the first join would produce empty and facts could never return, so the result would be too small inside loops. Starting from the full set lets intersection remove only facts that fail on some path. Union only grows a set, so a may analysis starts at empty and adds facts until nothing changes.