global optimizations
DESCRIPTION
Global optimizations. Local analysis framework. Define an analysis of a basic block as a quadruple ( D , V , F , I ) where D is a direction (forwards or backwards) V is a set of values the program can have at any point - PowerPoint PPT PresentationTRANSCRIPT
Global optimizations
2
Local analysis framework
• Define an analysis of a basic block as a quadruple (D, V, F, I) where– D is a direction (forwards or backwards)– V is a set of values the program can have at any
point– F is a family of transfer functions defining the
meaning of any expression as a function f : V V– I is the initial information at the top (or bottom)
of a basic block
3
Available Expressions
• Direction: Forward• Values: Sets of expressions assigned to variables• Transfer functions: Given a set of variable
assignments V and statement a = b + c:– Remove from V any expression containing a as a
subexpression– Add to V the expression a = b + c– Formally: Vout = (Vin \ {e | e contains a}) {a = b + c}
• Initial value: Empty set of expressions
4
Available expressions analysis
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;
sIN[s] OUT[s]
{a=b, c=b, d=b, e=a+b, f=a+b}
{a=b, c=b, d=b, e=a+b}
{a=b, c=b, d=a+b, e=a+b}
{a=b, c=b, d=a+b}
{a=b, c=b}
{a=b}
entry: {} Initial value
5
Optimizing from available expressions
• Common sub-expression elimination– If {… t = y op z … } x = y op z– Can transform statement into x = t
• Copy propagation– If {… y = t … } x = y op z– Can transform statement into x = t op z
• Note: same for x=y and x=op y
6
Common sub-expression elimination
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b; {a=b, c=b, d=b, e=a+b, f=a+b}
{a=b, c=b, d=b, e=a+b}
{a=b, c=b, d=a+b, e=a+b}
{a=b, c=b, d=a+b}
{a=b, c=b}
{a=b}
entry: {}
e = d;
f = e;
7
Liveness Analysis• Direction: Backward• Values: Sets of variables• Transfer functions: Given a set of variable assignments V
and statement a = b + c:– Remove a from V (any previous value of a is now dead)– Add b and c to V (any previous value of b or c is now live)– Formally: Vin = (Vout \ {a}) {b,c}
• Initial value: Depends on semantics of language– E.g., function arguments and return values (pushes)– Result of local analysis of other blocks as part of a global analysis
Liveness analysis
8
sIN[s] OUT[s]a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
{ b, d } exit:
{ b, d, e }
{ a, b, e }
{ a, b, d }
{ a, b }
{ a, b }
{ b }
Initial value
9
Optimizing from liveness analysis
• Dead code elimination– If x = y op z {v1,…,vk}– And x {v1,…,vk}– We can eliminate x = y op z
• Note: same for x=y and x=op y
Dead code elimination
10
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
{ b, d } exit:
{ a, b}
{ b }
Zero value Analysis• Direction: Forward• Values: Sets of valuations (values assigned to variables)• Transfer functions:
– Given a set of variable assignments V and statement a = b op c: update valuation of a at V according to values of b, c and operator op.
– Formally: Vout = (Vin \ {a = value’}) {a = value}
• Initial value: Depends on semantics of language– E.g., function arguments and return values (pushes)– Result of local analysis of other blocks as part of a global
analysis
12
Zero value Analysis
a = b;
c = a + b;
d = a / c;
c = b - b;
a = d - b;
f = d / c;
sIN[s] OUT[s]
{b=, a=?, c=0, d=, f=?}
{b=, a=?, c=0, d=}
{b=, a=, c=0, d=}
{b=, a=, c=, d=}
{b=, a=, c=}
{b=, a=}
entry: {b=} Initial value
13
Optimizing from zero value analysis
• Safe division– If x = y / z {v1,…,vk}– And z {v1,…,z=0,…,vk} or {v1,…,z=?,…,vk}
– We can update x = y safe div z
14
Safe division
a = b;
c = a + b;
d = a / c;
c = b - b;
a = d - b;
f = d / c; {b=, a=?, c=0, d=, f=?}
{b=, a=?, c=0, d=}
{b=, a=, c=0, d=}
{b=, a=, c=, d=}
{b=, a=, c=}
{b=, a=}
entry: {b=}
f = d safe div c;
Global Optimizations
15
16
Global dead code elimination
• Local dead code elimination needed to know what variables were live on exit from a basic block
• This information can only be computed as part of a global analysis
• How do we modify our liveness analysis to handle a CFG?
17
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
18
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{a, c, d}
?
Which variables may be live on some execution path?
19
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{b, c, d}
{c, d}Need to combine currently-computed value with new value
Need to combine currently-computed value with new value
20
CFGs without loops
Exit
x = a + b;y = c + d;
y = a + b;x = c + d;a = b + c;
b = c + d;e = c + d;Entry
{x, y}
{x, y}
{a, b, c, d}
{a, b, c, d} {a, b, c, d}
{a, b, c, d}{b, c, d}
{a, b, c, d}
{a, c, d}
21
CFGs without loops
Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
22
CFGs without loops
Exit
x = a + b;y = c + d;
a = b + c;
b = c + d;Entry
23
CFGs with loops
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;IfZ ...
Entry
{a}
?
24
CFGs with loops - initialization
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{}
25
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{}
{a}
26
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{a, b, c}
{a}
27
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{}
{}
{a, b, c}
{a}
{a, b, c}
28
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{}
{a, b, c}
{a}
{a, b, c}
29
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{}
{a, b, c}
{a}
{a, b, c}
{b, c}
30
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
31
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
32
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a}
{a, b, c}
{b, c}
{a, b, c}
33
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
34
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
35
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
36
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{b, c}
{a, b, c}
37
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
38
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{a, c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
39
CFGs with loops - iteration
Exit
a = a + b;d = b + c;
c = a + b;a = b + c;d = a + c;
b = c + d;c = c + d;Entry
{a}
{a, b}{b, c}
{a, c, d}
{a, b, c}
{a, c, d}
{a, b, c}
{a, b, c}
{a, b, c}
40
Join semilattices• A join semilattice is a ordering defined on a set of elements• Any two elements have some join that is the smallest
element larger than both elements• There is a unique bottom element, which is smaller than all
other elements• Intuitively:
– The join of two elements represents combining information from two elements by an overapproximation
• The bottom element represents “no information yet” or “the least conservative possible answer”
41
Formal definitions
• A join semilattice is a pair (V, ), where• V is a domain of elements• is a join operator that is– commutative: x y = y x– associative: (x y) z = x (y z)– idempotent: x x = x
• If x y = z, we say that z is the joinor (least upper bound) of x and y
• Every join semilattice has a bottom element denoted such that x = x for all x
42
A general framework
• A global analysis is a tuple (D, V, , F, I), where– D is a direction (forward or backward)
• The order to visit statements within a basic block, not the order in which to visit the basic blocks
– V is a set of values– is a join operator over those values– F is a set of transfer functions f : V V– I is an initial value
• The only difference from local analysis is the introduction of the join operator
43
A semilattice for zero value analysis• One possible semilattice for this analysis is
shown here (for each variable):
Undefined
0𝟎− 𝟎+¿¿
?
44
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
x=Undefinedy=Undefinedz=Undefinedw=Undefined
45
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
x = 6;Undefined
entryUndefined
46
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;Undefined
entryUndefined
47
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
48
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
y = x;Undefined
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
49
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
x= y = x;Undefined
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
50
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
51
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
w = x;Undefined
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
y= y=Undefined gives what?
52
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
x= ,y= w = x;Undefined
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
53
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
54
Global constant propagation
exit x = -5;Undefined
z = y / x;Undefined
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
55
Global constant propagation
exit x = -5;Undefined
x=y=w= z = y / x;Undefined
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
56
Global constant propagation
exit x = -5;Undefined
x=y=w=z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
57
Global constant propagation
exit x = -5;Undefined
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
58
Global constant propagation
exitx=y=w=z= x = -5;Undefined
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
59
Global constant propagation
exitx=y=w=z=x = -5;x=, y=w=z=
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
60
Global constant propagation
exitx=y=w=z= x = -5;x=, y=w=z=
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
z = y;Undefined
Undefinedx = 6;x =
entryUndefined
61
Global constant propagation
exitx=y=w=z= x = -5;x=, y=w=z=
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
x = z = y;Undefined
Undefinedx = 6;x =
entryUndefined
62
Global constant propagation
exitx=y=w=z= x = -5;x=, y=w=z=
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
63
Global constant propagation
exitx=y=w=z= x = -5;x=, y=w=z=
x=y=w= z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
x= x= gives what?
64
Global constant propagation
exitx=y=w=z=x = -5;x=, y=w=z=
y=w= ,x=?z = y / x;x=y=w=z=
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
65
Global constant propagation
exitx=y=w=z=x = -5;x=, y=w=z=
y=w=,x=?z = y / x;y=w=,x=z=?
x=,y=w = x;x=y=w=
x=y = x;x=,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
66
Global constant propagation
exity=w=,x=z=? x = -5;x=, y=w=z=
y=w=,x=?z = y / x;y=w=,x=z=?
x=,y=w = x;x=y=w=
x=y = x;x=,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
67
Global constant propagation
exity=w=,x=z=? x = -5;y=w=,x=z=?
y=w=,x=?z = y / x;y=w=,x=z=?
x= ,y= w = x;x=y=w=
x= y = x;x= ,y=
x = z = y;x =
Undefinedx = 6;x =
entryUndefined
Global analysisreached fixpoint