Unit content
Control-flow graphs and dataflow analysis
A control-flow graph (CFG) represents possible transfers of control inside a function. Nodes are basic blocks or program points; directed edges indicate which point may execute next.
For
if cond:
x = 1
else:
x = 2
print(x)
the two assignment branches split from the condition and join again before print.
A dataflow analysis propagates abstract facts along this graph until the facts become stable. For example, a reaching-definitions analysis can ask which assignments might supply the value of a variable at each program point.
Each node has a transfer function describing how executing that block changes the analysis fact. At control-flow joins, information from predecessor paths is combined with a join operation. Because loops create cycles, facts are usually updated repeatedly until a fixed point is reached.
Different analyses change the information domain and direction of propagation. Liveness commonly flows backward; available expressions or constant information commonly flow forward.
Dataflow analysis separates a reusable algorithmic framework—graph propagation to a fixed point—from the particular program property being computed. Compilers use it for optimization, diagnostics and program analysis without executing every concrete runtime path.