40-414 compiler design global optimizations

74
1 40-414 Compiler Design Lecture 12 Global Optimizations

Upload: others

Post on 27-Jan-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 40-414 Compiler Design Global Optimizations

1

40-414 Compiler Design

Lecture 12

Global Optimizations

Page 2: 40-414 Compiler Design Global Optimizations

2

Organization of a Code Optimizer (Revisited)

Control-flow analysis

Data-flow analysis

Transformations

Front-end Code generator

Code optimizer

Page 3: 40-414 Compiler Design Global Optimizations

3

Local Optimization

Recall the simple basic-block optimizations– Constant propagation

– Dead code elimination

X := 3

Y := Z * W

Q := X + Y

X := 3

Y := Z * W

Q := 3 + Y

Y := Z * W

Q := 3 + Y

Prof. Aiken

Page 4: 40-414 Compiler Design Global Optimizations

4

Global Optimization

These optimizations can be extended to an entire control-flow graph

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

Prof. Aiken

Page 5: 40-414 Compiler Design Global Optimizations

5

Global Optimization

These optimizations can be extended to an entire control-flow graph

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

Prof. Aiken

Page 6: 40-414 Compiler Design Global Optimizations

6

Global Optimization

These optimizations can be extended to an entire control-flow graph

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * 3

Prof. Aiken

Page 7: 40-414 Compiler Design Global Optimizations

7

Correctness

• How do we know it is OK to globally propagate constants?

• There are situations where it is incorrect:

X := 3

B > 0

Y := Z + W

X := 4

Y := 0

A := 2 * X

Prof. Aiken

We cannot propagate constant 3 or 4 to X

Page 8: 40-414 Compiler Design Global Optimizations

8

Correctness (Cont.)

To replace a use of x by a constant k we must know that:

On every path to the use of x, the last assignment to x is x := k **

Prof. Aiken

Page 9: 40-414 Compiler Design Global Optimizations

9

Example 1 Revisited

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

Prof. Aiken

Page 10: 40-414 Compiler Design Global Optimizations

10

Example 2 Revisited

X := 3

B > 0

Y := Z + W

X := 4

Y := 0

A := 2 * X

Prof. Aiken

Page 11: 40-414 Compiler Design Global Optimizations

11

Discussion

• The correctness condition is not trivial to check

• “All paths” includes paths around loops and through branches of conditionals

• Checking the condition requires global dataflow analysis– An analysis of the entire control-flow graph

Prof. Aiken

Page 12: 40-414 Compiler Design Global Optimizations

Global optimization tasks share several traits:– The optimization depends on knowing a property X

at a particular point in program execution

– Proving X at any point requires knowledge of the entire program

– It is OK to be conservative. If the optimization requires X to be true, then want to know either• X is definitely true

• Don’t know if X is true (we don’t do the optimization)

– It is always safe to say “don’t know”Prof. Aiken 12

Global Analysis

Page 13: 40-414 Compiler Design Global Optimizations

Prof. Aiken 13

Global Analysis (Cont.)

• Global dataflow analysis is a standard technique for solving problems with these characteristics

• Global constant propagation is one example of an optimization that requires global dataflow analysis

Page 14: 40-414 Compiler Design Global Optimizations

14

Global Constant Propagation

• Global constant propagation can be performed at any point where ** holds

On every path to the use of x, the last assignment to x is x := k (**)

• Consider the case of computing ** for a single variable X at all program points

Prof. Aiken [slightly modified]

Page 15: 40-414 Compiler Design Global Optimizations

15

Global Constant Propagation (Cont.)

• To make the problem precise, we associate one of the following values with X at every program point

value interpretation

This statement never executes

c X = constant c

X is not a constant

Prof. Aiken

Page 16: 40-414 Compiler Design Global Optimizations

16

Example

X =

X = 3

X = 3

X = 3X = 4

X =

X := 3

B > 0

Y := Z + W

X := 4

Y := 0

A := 2 * X

X = 3

X = 3

X =

Prof. Aiken

Page 17: 40-414 Compiler Design Global Optimizations

Prof. Aiken 17

Using the Information

• Given global constant information, it is easy to perform the optimization– Simply inspect the x = ? associated with a

statement using x

– If x is constant at that point replace that use of xby the constant

• But how do we compute the properties x = ?

Page 18: 40-414 Compiler Design Global Optimizations

18

The Idea

The analysis of a complicated program can be expressed as a combination of simple rules relating the change in information between

adjacent statements

Prof. Aiken

Page 19: 40-414 Compiler Design Global Optimizations

19

Explanation

• The idea is to “push” or “transfer”information from one statement to the next

• For each statement s, we compute information about the value of x immediately before and after s

C(s,x,in) = value of x before s is executed

C(s,x,out) = value of x after s is executed

Prof. Aiken

Stands for ‘constant’ information

Page 20: 40-414 Compiler Design Global Optimizations

20

Transfer Functions

• Define a transfer function that transfers information one statement to another

• In the following rules, let statement s have immediate predecessor statements p1,…,pn

Prof. Aiken

Page 21: 40-414 Compiler Design Global Optimizations

21

Rule 1

if i (C(pi, x, out) = )

then C(s, x, in) =

s

X =

X =

X = ?X = ?X = ?

Prof. Aiken [slightly modified]

p1

p2p3 p4

Page 22: 40-414 Compiler Design Global Optimizations

22

Rule 2

if i,j (C(pi, x, out) = c & C(pj, x, out) = d & d <> c)then C(s, x, in) =

s

X = d

X =

X = ?X = ?X = c

p1

p2p3 p4

Prof. Aiken [slightly modified]

Page 23: 40-414 Compiler Design Global Optimizations

23

Rule 3

if i (C(pi, x, out) = c or )

then C(s, x, in) = c

s

X = c

X = c

X = X = X = c

p1

p2p3 p4

Prof. Aiken [slightly modified]

Page 24: 40-414 Compiler Design Global Optimizations

24

Rule 4

if i (C(pi, x, out) = )

then C(s, x, in) =

s

X =

X =

X = X = X =

p1

p2p3 p4

Prof. Aiken [slightly modified]

Page 25: 40-414 Compiler Design Global Optimizations

25

The Other Half

• Rules 1-4 relate the out of one statement to the in of the next statement

• Now we need rules relating the in of a statement to the out of the same statement

Prof. Aiken

Page 26: 40-414 Compiler Design Global Optimizations

26

Rule 5

if C(s, x, in) =

then C(s, x, out) =

s

X =

X =

Prof. Aiken [slightly modified]

Page 27: 40-414 Compiler Design Global Optimizations

27

Rule 6

if c is a constant

then C(x := c, x, out) = c

x := c

X = ?

X = c

Rule 6 has a lower priority than Rule 5

R6 is checked only when R5 cannot be applied

Prof. Aiken [slightly modified]

Page 28: 40-414 Compiler Design Global Optimizations

28

Rule 7

C(x := f(…), x, out) =

x := f(…)

X = ?

X =

[f(…) is anything except a constant]

Rule 7 has a lower priority than Rule 5

R7 is checked only when R5 cannot be applied

Prof. Aiken [slightly modified]

Page 29: 40-414 Compiler Design Global Optimizations

29

Rule 8

if x <> y

then C(y := …, x, out) = C(y := …, x, in)

y := . . .

X = a

X = a

Prof. Aiken [slightly modified]

Page 30: 40-414 Compiler Design Global Optimizations

30

An Algorithm

1. For every entry s to the program, set C(s, x, in) =

2. Set C(s, x, in) = C(s, x, out) = everywhere else

3. Repeat until all points satisfy 1-8:Pick s not satisfying 1-8 and update using the

appropriate rule

Prof. Aiken

Page 31: 40-414 Compiler Design Global Optimizations

31

Constant Propagation

Prof. Aiken

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

Page 32: 40-414 Compiler Design Global Optimizations

32

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X := 3 by rule 6

Rule 6: if c is a constant then C(x := c, x, out) = c

Prof. Aiken [slightly modified]

Page 33: 40-414 Compiler Design Global Optimizations

33

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X :=

X := 3

by rule 8X := 3

Rule 8: if x <> y then C(y := …, x, out) = C(y := …, x, in)

Prof. Aiken [slightly modified]

Page 34: 40-414 Compiler Design Global Optimizations

34

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X := 3

X :=

X :=

X := 3

X :=

X :=

X :=

X :=

X := 3

by rule 3

X := 3

Rule 3: if i (C(pi, x, out) = c or ) then C(s, x, in) = c

Prof. Aiken [slightly modified]

by rule 3

Page 35: 40-414 Compiler Design Global Optimizations

35

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X := 3

X := 3

X :=

X := 3

X := 3

X :=

X :=

X :=

X := 3

by rule 8

X := 3

by rule 8

Rule 8: if x <> y then C(y := …, x, out) = C(y := …, x, in)

Prof. Aiken [slightly modified]

Page 36: 40-414 Compiler Design Global Optimizations

36

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X := 3

X := 3

X := 4

X := 3

X := 3

X :=

X :=

X :=

X := 3

X := 3

by rule 6

Prof. Aiken [slightly modified]

Rule 6: if c is a constant then C(x := c, x, out) = c

Page 37: 40-414 Compiler Design Global Optimizations

37

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X := 3

X := 3

X := 4

X := 3

X := 3

X :=

X :=

X :=

X := 3

by rule 2

X := 3

Rule 2: if i,j (C(pi, x, out) = c & C(pj, x, out) = d & d <> c)

then C(s, x, in) = Prof. Aiken [slightly modified]

Page 38: 40-414 Compiler Design Global Optimizations

38

Constant Propagation

X := 3

B > 0

Y := Z + W

X := 4Y := 0

A := 2 * X

X := 3

X := 3

X := 4

X := 3

X := 3

X :=

X :=

X :=

X := 3

X := 3

by rule 8

Prof. Aiken [slightly modified]

Rule 8: if x <> y then C(y := …, x, out) = C(y := …, x, in)

Page 39: 40-414 Compiler Design Global Optimizations

39

The Value

• To understand why we need , look at a loop

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

Prof. Aiken

X = ?

X = ?

X = ?

X = ?X = ?

Page 40: 40-414 Compiler Design Global Optimizations

40

Discussion

• Consider the statement Y := 0

• To compute whether X is constant at this point, we need to know whether X is constant at the two predecessors– X := 3

– A := 2 * X

• But info for A := 2 * X depends on its predecessors, including Y := 0!

Prof. Aiken

Page 41: 40-414 Compiler Design Global Optimizations

41

The Value (Cont.)

• Because of cycles, all points must have values at all times

• Intuitively, assigning some initial value allows the analysis to break cycles

• The initial value means “So far as we know, control never reaches this point”

Prof. Aiken

Page 42: 40-414 Compiler Design Global Optimizations

42

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X =

X =

Prof. Aiken

X =

X =

X =

Page 43: 40-414 Compiler Design Global Optimizations

43

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X =

X =

Prof. Aiken

X = 3

X =

X =

Page 44: 40-414 Compiler Design Global Optimizations

44

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X =

X = 3

Prof. Aiken

X = 3

X =

X =

Page 45: 40-414 Compiler Design Global Optimizations

45

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X =

X = 3

Prof. Aiken

X = 3

X = 3

X =

Page 46: 40-414 Compiler Design Global Optimizations

46

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X = 3

X = 3

Prof. Aiken

X = 3

X = 3

X =

Page 47: 40-414 Compiler Design Global Optimizations

47

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X =

X = 3

X = 3

Prof. Aiken

X = 3

X = 3

X = 3

Page 48: 40-414 Compiler Design Global Optimizations

48

Example

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

A < B

X =

X = 3

X = 3

X = 3

X = 3

X = 3

X = 3

X = 3

Prof. Aiken

X = 3

X = 3

X = 3

Page 49: 40-414 Compiler Design Global Optimizations

49

Orderings

• We can simplify the presentation of the analysis by ordering the values

< c <

• Drawing a picture with “lower” values drawn lower, we get

-1 0 1

Prof. Aiken [slightly modified]

these are abstract values

e.g., stands for any possible run time value

constants are not comparable

e.g, 0 is not less than 1

Page 50: 40-414 Compiler Design Global Optimizations

50

Orderings (Cont.)

• is the greatest value, is the least– All constants are in between and incomparable

• Let lub be the least-upper bound in this ordering– Examples: lub(1, 2) = , lub( , ) = ,

lub(1, ) = 1, …

• Rules 1-4 are in fact computing lub:C(s, x, in) = lub { C(p, x, out) | p is a predecessor of s }

Prof. Aiken [slightly modified]

lub ( )

s

p1p2

p3 p4

x := x := x := x :=

Page 51: 40-414 Compiler Design Global Optimizations

51

Termination

• Simply saying “repeat until nothing changes”doesn’t guarantee that eventually nothing changes

• The use of lub explains why the algorithm terminates– Values start as and only increase

can change to a constant, and a constant to

– Thus, C(s, x, _) can change at most twice

Prof. Aiken

Page 52: 40-414 Compiler Design Global Optimizations

52

Termination (Cont.)

Thus the algorithm is linear in program size

Number of steps =

Number of C(….) value computed * 2 =

Number of program statements * 4

Prof. Aiken [slightly modified]

for each statement s, we haveone c(s, x, in) and one c(s, x, out),each can change at most twice

Page 53: 40-414 Compiler Design Global Optimizations

53

Liveness Analysis

Once constants have been globally propagated, we would like to eliminate dead code

After constant propagation, X := 3 is dead (assuming X not used elsewhere)

X := 3

B > 0

Y := Z + W Y := 0

A := 2 * X

Prof. Aiken

Page 54: 40-414 Compiler Design Global Optimizations

54

Live and Dead

• The first value of x is dead (never used)

• The second value of x is live (may be used in the future)

• Liveness is an important concept

X := 3

X := 4

Y := X

Prof. Aiken [slightly modified]

in this eg., xis guaranteed to be used

this value of xis never used

Page 55: 40-414 Compiler Design Global Optimizations

55

Liveness

A variable x is live at statement s if– There exists a statement s’ that uses x

– There is a path from s to s’

– That path has no intervening assignment to x

Prof. Aiken

X :=

:= X

s

s’

no x:= …

Page 56: 40-414 Compiler Design Global Optimizations

56

Global Dead Code Elimination

• A statement x := … is dead code if x is dead after the assignment

• Dead statements can be deleted from the program

• But we need liveness information first . . .

Prof. Aiken

Page 57: 40-414 Compiler Design Global Optimizations

57

Computing Liveness

• We can express liveness in terms of information transferred between adjacent statements, just as in copy propagation

• Liveness is simpler than constant propagation, since it is a boolean property (true or false)

Prof. Aiken

Page 58: 40-414 Compiler Design Global Optimizations

58

Liveness Rule 1

L(p, x, out) = { L(s, x, in) | s a successor of p }

p

X = true

X = true

X = ?X = ?X = ?

Prof. Aiken

Page 59: 40-414 Compiler Design Global Optimizations

59

Liveness Rule 2

L(s, x, in) = true if s refers to x on the rhs

…:= f(x)

X = true

X = ?

Prof. Aiken

Page 60: 40-414 Compiler Design Global Optimizations

60

Liveness Rule 3

L(x := e, x, in) = false if e does not refer to x

x := e

X = false

X = ?

Prof. Aiken

because x is being overwritten here

Page 61: 40-414 Compiler Design Global Optimizations

61

Liveness Rule 4

L(s, x, in) = L(s, x, out) if s does not refer to x

s

X = a

X = a

Prof. Aiken

Page 62: 40-414 Compiler Design Global Optimizations

62

Algorithm

1. Let all L(…) = false initially

2. Repeat until all statements s satisfy rules 1-4Pick s where one of 1-4 does not hold and update

using the appropriate rule

Prof. Aiken

Page 63: 40-414 Compiler Design Global Optimizations

63

Example

Prof. Aiken

If (x ==10)

x := x + 1

X := 0

false

falsefalse

false

false

false

where x is live?

Page 64: 40-414 Compiler Design Global Optimizations

64

Example

Prof. Aiken

If (x ==10)

x := x + 1

X := 0

false

false

false

false

true by rule 2

Rule 2: L(s, x, in) = true if s refers to x on the rhs

false

reading of x is before writing to it

where x is live?

Page 65: 40-414 Compiler Design Global Optimizations

65

Example

Prof. Aiken

If (x ==10)

x := x + 1

X := 0

false

false

false

false

true

by rule 2 true

where x is live?

Rule 2: L(s, x, in) = true if s refers to x on the rhs

Page 66: 40-414 Compiler Design Global Optimizations

66

Example

Prof. Aiken

If (x ==10)

x := x + 1

where x is live?

X := 0

false

false

false

true

true

trueby rule 2

Rule 2: L(s, x, in) = true if s refers to x on the rhs

Page 67: 40-414 Compiler Design Global Optimizations

67

Example

Prof. Aiken

If (x ==10)

x := x + 1

X := 0

false

false

true

true

true

true

Rule 1: L(p, x, out) = { L(s, x, in) | s a successor of p }

where x is live?

by rule 1

Page 68: 40-414 Compiler Design Global Optimizations

68

Example

Prof. Aiken

If (x ==10)

x := x + 1

X := 0

false

false

true

true

true

true

Rule 3: L(x := e, x, in) = false if e does not refer to x

where x is live?

by rule 3

Page 69: 40-414 Compiler Design Global Optimizations

69

Termination

• A value can change from false to true, but not the other way around

• Each value can change only once, so termination is guaranteed

• Once the analysis is computed, it is simple to eliminate dead code

Prof. Aiken

false < true

Page 70: 40-414 Compiler Design Global Optimizations

70

Forward vs. Backward Analysis

We've seen two kinds of analysis:

Constant propagation is a forwards analysis: information is pushed from inputs to outputs

Liveness is a backwards analysis: information is pushed from outputs back towards inputs

Prof. Aiken

Page 71: 40-414 Compiler Design Global Optimizations

71

Analysis

• There are many other global flow analyses

• Most can be classified as either forward or backward

• Most also follow the methodology of local rules relating information between adjacent program points

Prof. Aiken

Page 72: 40-414 Compiler Design Global Optimizations

72

Question?

After running the constant propagation algorithm to completion, choose the correct dataflow information for X, Y, and Z at the program point labeled at right.

Z := 5

C > 0

Y := 1

X := 4

Z := X + Y

X := 4

A := X * Y

B := A * Z

4

4 5

4 1 5

X Y Z

?

Prof. Aiken

Page 73: 40-414 Compiler Design Global Optimizations

73

Question?

Prof. Aiken

After running the constant propagation algorithm tocompletion, choose the correct dataflow informationfor X, Y, and Z at the program point labeled at right. Z := 5

A > 0

X := 4

Y := 1

B > 0

X := 4

Y := 1

X := Z + 3

C < 10

1

4 5

4 1 5

4

X Y Z

?

Z := X + 6

Page 74: 40-414 Compiler Design Global Optimizations

74

Question?

Prof. Aiken

After running the liveness analysis algorithm to completion, which of W, X, Y, and Z are live at the program point labeled at right? Assume all variables are dead on exit.

X > 0

Z := 5

X > 10

Z := W + 4

Y := Y + 1

W

?X := Z - 1

X

Y

Z