Sub topic 12
The Data Flow Analysis Framework
Prerequisites and purpose
Assume you know basic C-style programming, control-flow graphs, sets, and three-address code. A data flow analysis is a compiler calculation that tracks facts about program values as control moves through a control-flow graph (CFG). A CFG contains basic blocks as nodes and possible transfers of control as directed edges.
The compiler uses these facts to answer questions such as:
- Which assignments may reach a statement?
- Which variables will be used later?
- Which expressions already have known values?
Each analysis associates a set with every block. The set before a block is IN[B]; the set after it is OUT[B].
Transfer equations
A block transforms incoming facts into outgoing facts. The transformation usually has two parts:
gen[B]: facts created by blockBkill[B]: facts invalidated by blockB
For many forward analyses:
OUT[B] = gen[B] ∪ (IN[B] - kill[B])The meet operator combines information from multiple control-flow paths. Use union when the analysis asks whether something is possible on at least one path. Use intersection when it asks whether something is guaranteed on every path.
| Analysis | Direction | Meet operator | Meaning |
|---|---|---|---|
| Reaching definitions | Forward | Union | A definition may reach a point |
| Live variables | Backward | Union | A variable may be used later |
| Available expressions | Forward | Intersection | An expression is available on every path |
A forward analysis combines predecessor facts. A backward analysis combines successor facts. Live-variable analysis, for example, uses:
OUT[B] = union of IN[S] for every successor S
IN[B] = use[B] ∪ (OUT[B] - def[B])Here, use[B] contains variables read before being assigned in B, and def[B] contains variables assigned in B.
CFG view
At a join point, the analysis combines facts from all incoming edges. The choice of union or intersection determines whether the result describes a possible fact or a fact guaranteed across paths.
Applying the framework
To analyze a CFG:
- List
use,def,gen, andkillfor each block. - Choose forward or backward direction.
- Choose union or intersection as the meet operator.
- Set boundary values at entry or exit.
- Repeatedly apply the equations until no
INorOUTset changes.
The final stable result is called a fixed point. It is the information used by later optimizations.
Exercise
Which meet operator is normally used for a must-analysis such as available expressions?
Show answer
Intersection, because an expression must be available along every incoming path.