67
Global optimizations

Global optimizations

  • Upload
    aaralyn

  • View
    88

  • Download
    0

Embed Size (px)

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 Presentation

Citation preview

Page 1: Global optimizations

Global optimizations

Page 2: 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

Page 3: Global optimizations

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

Page 4: Global optimizations

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

Page 5: Global optimizations

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

Page 6: Global optimizations

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;

Page 7: Global optimizations

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

Page 8: Global optimizations

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

Page 9: Global optimizations

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

Page 10: Global optimizations

Dead code elimination

10

a = b;

c = a;

d = a + b;

e = d;

d = a;

f = e;

{ b, d } exit:

{ a, b}

{ b }

Page 11: Global optimizations

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

Page 12: Global optimizations

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

Page 13: Global optimizations

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

Page 14: Global optimizations

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;

Page 15: Global optimizations

Global Optimizations

15

Page 16: 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?

Page 17: Global optimizations

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

Page 18: Global optimizations

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?

Page 19: Global optimizations

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

Page 20: Global optimizations

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}

Page 21: Global optimizations

21

CFGs without loops

Exit

x = a + b;y = c + d;

a = b + c;

b = c + d;Entry

Page 22: Global optimizations

22

CFGs without loops

Exit

x = a + b;y = c + d;

a = b + c;

b = c + d;Entry

Page 23: Global optimizations

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}

?

Page 24: Global optimizations

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}

{}{}

{}

{}

Page 25: Global optimizations

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}

Page 26: Global optimizations

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}

Page 27: Global optimizations

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}

Page 28: Global optimizations

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}

Page 29: Global optimizations

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}

Page 30: Global optimizations

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}

Page 31: Global optimizations

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}

Page 32: Global optimizations

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}

Page 33: Global optimizations

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}

Page 34: Global optimizations

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}

Page 35: Global optimizations

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}

Page 36: Global optimizations

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}

Page 37: Global optimizations

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}

Page 38: Global optimizations

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}

Page 39: Global optimizations

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}

Page 40: Global optimizations

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”

Page 41: Global optimizations

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

Page 42: Global optimizations

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

Page 43: Global optimizations

43

A semilattice for zero value analysis• One possible semilattice for this analysis is

shown here (for each variable):

Undefined

0𝟎− 𝟎+¿¿

?

Page 44: Global optimizations

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

Page 45: Global optimizations

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

Page 46: Global optimizations

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

Page 47: Global optimizations

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

Page 48: Global optimizations

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

Page 49: Global optimizations

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

Page 50: Global optimizations

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

Page 51: Global optimizations

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?

Page 52: Global optimizations

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

Page 53: Global optimizations

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

Page 54: Global optimizations

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

Page 55: Global optimizations

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

Page 56: Global optimizations

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

Page 57: Global optimizations

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

Page 58: Global optimizations

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

Page 59: Global optimizations

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

Page 60: Global optimizations

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

Page 61: Global optimizations

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

Page 62: Global optimizations

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

Page 63: Global optimizations

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?

Page 64: Global optimizations

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

Page 65: Global optimizations

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

Page 66: Global optimizations

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

Page 67: Global optimizations

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