Académique Documents
Professionnel Documents
Culture Documents
• Liveness analysis
Lecture 15
X := 3 X := 3 Y := Z * W Y := Z + W Y := 0
Y := Z * W Y := Z * W Q := 3 + Y
Q := X + Y Q := 3 + Y A := 2 * X
Y := Z + W Y := 0 Y := Z + W Y := 0
A := 2 * X A := 2 * 3
1
Correctness Correctness (Cont.)
Y := Z + W Y := 0
X := 4
A := 2 * X
X := 3 X := 3
B>0 B>0
Y := Z + W Y := 0 Y := Z + W Y := 0
X := 4
A := 2 * X
A := 2 * X
• The correctness condition is not trivial to Global optimization tasks share several traits:
check – The optimization depends on knowing a property X
at a particular point in program execution
– Proving X at any point requires knowledge of the
• “All paths” includes paths around loops and entire program
through branches of conditionals
– It is OK to be conservative. If the optimization
requires X to be true, then want to know either
• Checking the condition requires global analysis • X is definitely true
• Don’t know if X is true
– An analysis of the entire control-flow graph
2
Global Analysis (Cont.) Global Constant Propagation
3
Explanation Transfer Functions
• The idea is to “push” or “transfer” information • Define a transfer function that transfers
from one statement to the next information one statement to another
• For each statement s, we compute information • In the following rules, let statement s have
about the value of x immediately before and immediate predecessor statements p1,…,pn
after s
C(x,s,in) = value of x before s
C(x,s,out) = value of x after s
Rule 1 Rule 2
X=* X=*
s s
if C(pi, x, out) = * for any i, then C(s, x, in) = * C(pi, x, out) = c & C(pj, x, out) = d & d <> c then
C(s, x, in) = *
Rule 3 Rule 4
X=c X=#
s s
4
The Other Half Rule 5
Rule 6 Rule 7
X=? X=?
x := c x := f(…)
X=c X=*
Rule 8 An Algorithm
C(y := …, x, out) = C(y := …, x, in) if x <> y 3. Repeat until all points satisfy 1-8:
Pick s not satisfying 1-8 and update using the
appropriate rule
5
The Value # Discussion
Example Example
X=* X=*
X := 3 X := 3
X=3 X=3
B>0 B>0
X=3 X=3 X=3 X=3
Y := Z + W Y := 0 Y := Z + W Y := 0
X=3 X=3
X=3 X=# X=3 X=#
A := 2 * X A := 2 * X
X=# X=3
A<B A<B
6
Example Orderings
#
Prof. Aiken CS 143 Lecture 15 37 Prof. Aiken CS 143 Lecture 15 38
• * is the greatest value, # is the least • Simply saying “repeat until nothing changes”
– All constants are in between and incomparable doesn’t guarantee that eventually nothing
changes
• Let lub be the least-upper bound in this
ordering • The use of lub explains why the algorithm
terminates
– Values start as # and only increase
• Rules 1-4 can be written using lub:
– # can change to a constant, and a constant to *
C(s, x, in) = lub { C(p, x, out) | p is a predecessor of s }
– Thus, C(s, x, _) can change at most twice
Thus the algorithm is linear in program size Once constants have been globally propagated,
we would like to eliminate dead code
Number of steps = X := 3
A := 2 * X
7
Live and Dead Liveness
p
X = true X = true
…:= f(x)
X=?
X=? X=? X = true X=?
L(p, x, out) = { L(s, x, in) | s a successor of p } L(s, x, in) = true if s refers to x on the rhs
8
Liveness Rule 3 Liveness Rule 4
X = false X=a
x := e s
X=? X=a
L(x := e, x, in) = false if e does not refer to x L(s, x, in) = L(s, x, out) if s does not refer to x
Algorithm Termination
1. Let all L(…) = false initially • A value can change from false to true, but not
the other way around
2. Repeat until all statements s satisfy rules 1-4
Pick s where one of 1-4 does not hold and update • Each value can change only once, so
using the appropriate rule termination is guaranteed
We’ve seen two kinds of analysis: • There are many other global flow analyses
Liveness is a backwards analysis: information is • Most also follow the methodology of local
pushed from outputs back towards inputs rules relating information between adjacent
program points