Presentation is loading. Please wait.

Presentation is loading. Please wait.

Global optimizations.

Similar presentations


Presentation on theme: "Global optimizations."— Presentation transcript:

1 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
IN[s] OUT[s] entry: {} Initial value a = b; {a=b} c = b; {a=b, c=b} d = a + b; {a=b, c=b, d=a+b} e = a + b; {a=b, c=b, d=a+b, e=a+b} d = b; {a=b, c=b, d=b, e=a+b} f = a + b; {a=b, c=b, d=b, e=a+b, f=a+b}

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
entry: {} a = b; {a=b} c = b; {a=b, c=b} d = a + b; {a=b, c=b, d=a+b} e = d; e = a + b; {a=b, c=b, d=a+b, e=a+b} d = b; {a=b, c=b, d=b, e=a+b} f = e; f = a + b; {a=b, c=b, d=b, e=a+b, f=a+b}

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

8 Liveness analysis s IN[s] OUT[s] { b } a = b; { a, b } c = a; { a, b }
d = a + b; { a, b, d } e = d; { a, b, e } d = a; { b, d, e } f = e; Initial value { b, d } exit:

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

10 Dead code elimination { b } a = b; c = a; d = a + b; e = d; { a, b}
f = e; { b, d } exit:

11 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 s IN[s] OUT[s] entry: {b= 𝟎 + } a = b;
Initial value a = b; {b= 𝟎 + , a= 𝟎 + } c = a + b; {b= 𝟎 + , a= 𝟎 + , c= 𝟎 + } d = a / c; {b= 𝟎 + , a= 𝟎 + , c= 𝟎 + , d= 𝟎 + } c = b - b; {b= 𝟎 + , a= 𝟎 + , c=0, d= 𝟎 + } a = d - b; {b= 𝟎 + , a=?, c=0, d= 𝟎 + } f = d / c; {b= 𝟎 + , a=?, c=0, d= 𝟎 + , f=?}

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 entry: {b= 𝟎 + } a = b; {b= 𝟎 + , a= 𝟎 + } c = a + b;
d = a / c; {b= 𝟎 + , a= 𝟎 + , c= 𝟎 + , d= 𝟎 + } c = b - b; {b= 𝟎 + , a= 𝟎 + , c=0, d= 𝟎 + } a = d - b; {b= 𝟎 + , a=?, c=0, d= 𝟎 + } f = d safe div c; f = d / c; {b= 𝟎 + , a=?, c=0, d= 𝟎 + , f=?}

15 Global Optimizations

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 b = c + d; e = c + d; Entry x = c + d; a = b + c;
y = a + b; x = a + b; y = c + d; Exit

18 CFGs without loops ? {a, c, d} b = c + d; e = c + d; Entry
Which variables may be live on some execution path? Entry {a, b, c, d} ? x = c + d; a = b + c; {b, c, d} {a, b, c, d} y = a + b; {a, b, c, d} {a, b, c, d} x = a + b; y = c + d; {a, b, c, d} {x, y} {x, y} Exit

19 CFGs without loops b = c + d; e = c + d; {c, d} Entry {b, c, d}
Need to combine currently-computed value with new value Entry {b, c, d} x = c + d; a = b + c; {b, c, d} {a, b, c, d} y = a + b; {a, b, c, d} {a, b, c, d} x = a + b; y = c + d; {a, b, c, d} {x, y} {x, y} Exit

20 CFGs without loops {a, c, d} b = c + d; e = c + d; Entry {a, b, c, d}
x = c + d; a = b + c; {a, b, c, d} y = a + b; {a, b, c, d} {a, b, c, d} x = a + b; y = c + d; {a, b, c, d} {x, y} {x, y} Exit

21 CFGs without loops b = c + d; Entry a = b + c; x = a + b; y = c + d;
Exit

22 CFGs without loops b = c + d; Entry a = b + c; x = a + b; y = c + d;
Exit

23 CFGs with loops b = c + d; c = c + d; IfZ ... Entry
a = b + c; d = a + c; c = a + b; a = a + b; d = b + c; ? Exit {a}

24 CFGs with loops - initialization
{} b = c + d; c = c + d; Entry a = b + c; d = a + c; {} {} c = a + b; a = a + b; d = b + c; {} Exit {a}

25 CFGs with loops - iteration
{} b = c + d; c = c + d; Entry {} a = b + c; d = a + c; {} c = a + b; a = a + b; d = b + c; {} {a} Exit {a}

26 CFGs with loops - iteration
{} b = c + d; c = c + d; Entry {} a = b + c; d = a + c; {} c = a + b; a = a + b; d = b + c; {a, b, c} {a} Exit {a}

27 CFGs with loops - iteration
{} b = c + d; c = c + d; Entry {} a = b + c; d = a + c; {} c = a + b; {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

28 CFGs with loops - iteration
{} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {} c = a + b; {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

29 CFGs with loops - iteration
{} b = c + d; c = c + d; Entry {b, c} {b, c} a = b + c; d = a + c; {} c = a + b; {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

30 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

31 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

32 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a} Exit {a}

33 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

34 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

35 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

36 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

37 CFGs with loops - iteration
{c, d} b = c + d; c = c + d; Entry {a, b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

38 CFGs with loops - iteration
{a, c, d} b = c + d; c = c + d; Entry {a, b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

39 CFGs with loops - iteration
{a, c, d} b = c + d; c = c + d; Entry {a, b, c} a = b + c; d = a + c; {b, c} {a, b} c = a + b; {a, b, c} {a, b, c} a = a + b; d = b + c; {a, b, c} {a, c, d} Exit {a}

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 join or (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

44 Global constant propagation
entry Undefined x = 6; Undefined y = x; Undefined z = y; Undefined x=Undefined y=Undefined z=Undefined w=Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

45 Global constant propagation
x = 6; Undefined entry Undefined y = x; Undefined z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

46 Global constant propagation
Undefined x = 6; Undefined entry Undefined y = x; Undefined z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

47 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined y = x; Undefined z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

48 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined y = x; Undefined z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

49 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; Undefined z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

50 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

51 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined w = x; Undefined y= 𝟎 +  y=Undefined gives what? z = y / x; Undefined x = -5; Undefined exit

52 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; Undefined z = y / x; Undefined x = -5; Undefined exit

53 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + z = y / x; Undefined x = -5; Undefined exit

54 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + z = y / x; Undefined x = -5; Undefined exit

55 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; Undefined x = -5; Undefined exit

56 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x = -5; Undefined exit

57 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x = -5; Undefined exit

58 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; Undefined exit

59 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

60 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

61 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; Undefined x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

62 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

63 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + x= 𝟎 +  x= 𝟎 − gives what? x=y=w= 𝟎 + z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

64 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + y=w= 𝟎 + ,x=? z = y / x; x=y=w=z= 𝟎 + x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

65 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + y=w= 𝟎 + ,x=? z = y / x; y=w= 𝟎 + ,x=z=? x=y=w=z= 𝟎 + x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

66 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + y=w= 𝟎 + ,x=? z = y / x; y=w= 𝟎 + ,x=z=? y=w= 𝟎 + ,x=z=? x = -5; x= 𝟎 − , y=w=z= 𝟎 + exit

67 Global constant propagation
Undefined x = 6; x = 𝟎 + entry Undefined x= 𝟎 + y = x; x= 𝟎 + ,y= 𝟎 + x = 𝟎 + z = y; x = 𝟎 + x= 𝟎 + ,y= 𝟎 + w = x; x=y=w= 𝟎 + Global analysis reached fixpoint y=w= 𝟎 + ,x=? z = y / x; y=w= 𝟎 + ,x=z=? y=w= 𝟎 + ,x=z=? x = -5; y=w= 𝟎 + ,x=z=? exit


Download ppt "Global optimizations."

Similar presentations


Ads by Google