cs4713 1
Dataflow analysis
Discovering Global Live Ranges
- f Variables
Dataflow analysis Discovering Global Live Ranges of Variables - - PowerPoint PPT Presentation
Dataflow analysis Discovering Global Live Ranges of Variables cs4713 1 Optimization and analysis Requirement for optimizations Correctness (safety) must preserve the meaning of the input computation Profitability must
cs4713 1
cs4713 2
Requirement for optimizations
Correctness (safety)
must preserve the meaning of the input computation
Profitability
must improve code quality
Program analysis
Statically examines input computation to ensure safety and profitability of optimizations
Compile-time reasoning of runtime program behavior
Undecidable in general due to external program input, complex control flow,
and pointer/array references
Conservative approximation of program runtime behavior:
may miss opportunities of applying optimization, but ensure all
Data-flow analysis
Reason about flow of values on control-flow graphs
Example: available expression analysis for global redundancy elimination
Can be used for program optimization or program understanding
cs4713 3
Graphical representation of runtime control-flow paths
Nodes of graph: basic blocks (straight-line computations) Edges of graph: flows of control
Useful for collecting information about computation
Detect loops, remove redundant computations, register
allocation, instruction scheduling…
Alternative CFG: Each node contains a single statement
…… i = 0 while (i < 50) { t1 = b * 2; a = a + t1; i = i + 1; } …. if I < 50 …… t1 := b * 2; a := a + t1; i = i + 1; i =0;
cs4713 4
A data-flow analysis problem
A variable v is live at CFG point p iff there is a path from
p to a use of v along which v is not redefined
At any CFG point p, what variables are alive?
Live variable analysis can be used in
Global register allocation
Dead variables no longer need to be in registers
Useless-store elimination
Dead variable don’t need to be stored back to memory
Uninitialized variable detection
No variable should be alive at program entry point
cs4713 5
For each basic block n, let
UEVar(n)=variables used before any definition in n VarKill(n)=variables defined (modified) in n (killed by n)
S1: m := y * z S2: y := y -z S3: o := y * z M for each basic block n:S1;S2;S3;…;Sk VarKill := ∅ UEVar(n) := ∅ for i = 1 to k suppose Si is “x := y op z” if y ∉ VarKill UEVar(n) = UEVar(n) ∪ {y} if z ∉ VarKill UEVar(n) = UEVar(n) ∪ {z} VarKill = VarKill ∪ {x}
cs4713 6
For each basic block n,
let
UEVar(n)
vars used before defined
VarKill(n)
vars defined (killed by n)
Goal: evaluate vars alive on exit from n
LiveOut(n)= ∪ m∈succ(n)
(UEVar(m) ∪ (LiveOut(m)-VarKill(m)) m:=a+b n:=a+b p:=c+d r:=c+d q:=a+b r:=c+d e:=b+18 s:=a+b u:=e+f e:=a+17 t:=c+d u:=e+f v:=a+b w:=c+d X:=e+f y:=a+b z:=c+d A B C D E F G
cs4713 7
For each basic block n, let
UEVar(n)=variables used before any definition in n
VarKill(n)=variables defined (modified) in n (killed by n)
Goal: evaluate names of variables alive on exit from n
LiveOut(n)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m)) m∈succ(n)
for each basic block bi compute UEVar(bi) and VarKill(bi) LiveOut(bi) := ∅ for (changed := true; changed; ) changed = false for each basic block bi
LiveOut(bi)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m)) if (LiveOut(bi) != old) changed := true m∈succ(bi)
cs4713 8
Iterative evaluation of result
sets until a fixed point is reached
Does the algorithm always
terminate?
If the result sets are
bounded and grow monotonically, then yes; Otherwise, no.
Fixed-point solution is
independent of evaluation
What answer does the
algorithm compute?
Unique fixed-point solution The meet-over-all-paths
solution
How long does it take the
algorithm to terminate?
Depends on traversing order
for each basic block bi compute Gen(bi) and Kill(bi) Result(bi) := ∅ for (changed := true; changed; ) changed = false for each basic block bi
Result(bi)= ∩ or ∪ [m∈pred(bi) or succ(bi)] (Gen(m) ∪ (Result(m)-Kill(m)) if (Result(bi) != old) changed := true
cs4713 9
Facilitate fast convergence to
the fixed point
Postorder traversal
Visits as many of a nodes
successors as possible before visiting the node
Used in backward data-flow
analysis
Reverse postorder traversal
Visits as many of a node’s
predecessors as possible before visiting the node
Used in forward data-flow
analysis 4 2 3 1 1 3 2 4 postorder Reverse postorder
cs4713 10
Sources of imprecision
Unreachable control flow edges, array and pointer references,
precedure calls
Other data-flow programs
Reaching definition analysis
A definition point d of variable v reaches CFG point p iff there is a
path from d to p along which v is not redefined
At any CFG point p, what definition points can reach p?
Very busy expression analysis
An expression e is very busy at a CFG point p if it is evaluated on
every path leaving p, and evaluating e at p yields the same result.
At any CFG point p, what expressions are very busy?
Constant propagation analysis
A variable-value pair (v,c) is valid at a CFG point p if on every
path from procedure entry to p, variable v has value c
At any CFG point p, what variables have constants?