55
CMPUT 680 - Compiler Desi gn and Optimization 1 Borrowed from J. N. Amaral, slightly modified http://www.cs.ualberta.ca/~amaral/courses/680 LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c j := b k := m + 4 LIVE-OUT: d k j m e f h g k j b c d k j

Borrowed from J. N. Amaral, slightly modified cs.ualberta/~amaral/courses/680

  • Upload
    shania

  • View
    29

  • Download
    2

Embed Size (px)

DESCRIPTION

LIVE-IN: k j. g. g := mem[j+12]. h. h := k -1. f. f := g + h. e. e := mem[j+8]. m. m := mem[j+16]. b. b := mem[f]. c. c := e + 8. d. d := c. k := m + 4. j := b. LIVE-OUT: d k j. - PowerPoint PPT Presentation

Citation preview

Page 1: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

1

Borrowed from J. N. Amaral, slightly modified http://www.cs.ualberta.ca/~amaral/courses/680

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

b := mem[f]

c := e + 8

d := c

j := b

k := m + 4

LIVE-OUT: d k j

m

e

f

h

g

k j

b

c

d

kj

Page 2: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

2

Example:Simplify (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(h,no-spill)

stack

Page 3: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

3

Example:Simplify (K=4)

b mkj

g

d

c

e

f

(Appel, pp. 237)

(g, no-spill)(h, no-spill)

stack

Page 4: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

4

Example:Simplify (K=4)

b mkj

d

c

e

f

(Appel, pp. 237)

(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 5: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

5

Example:Simplify (K=4)

b mj

d

c

e

f

(Appel, pp. 237)

(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 6: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

6

Example:Simplify (K=4)

b mj

d

c

e

(Appel, pp. 237)

(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 7: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

7

Example:Simplify (K=4)

b mj

d

c

(Appel, pp. 237)

(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 8: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

8

Example:Coalesce (K=4)

bj

d

c

(Appel, pp. 237)

(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Why can’t we simplify?

Cannot simplify move-related nodes.

Page 9: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

9

Example:Coalesce (K=4)

bj

d

c

(Appel, pp. 237)

(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 10: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

10

Example:Simplify (K=4)

bj

c-d

(Appel, pp. 237)

(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 11: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

11

Example:Coalesce (K=4)

bj

(Appel, pp. 237)

(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 12: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

12

Example:Simplify (K=4) greedy-4-colorable

b-j

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

Page 13: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

13

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 14: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

14

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 15: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

15

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 16: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

16

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 17: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

17

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 18: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

18

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 19: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

19

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 20: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

20

Example:Select (K=4)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(b-j, no-spill)(c-d, no-spill)(m, no-spill)(e, no-spill)(f, no-spill)(k, no-spill)(g, no-spill)(h, no-spill)

stack

R1

R2

R3

R4

Page 21: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

21

Example:Allocation with 4 registers

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

b := mem[f]

c := e + 8

d := c

j := b

k := m + 4

LIVE-OUT: d k j

m

e

f

h

g

k j

b

c

d

j

k

Page 22: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

22

Example:Allocation with 4 registers

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

j := mem[f]

d := e + 8

k := m + 4

LIVE-OUT: d k j

m

e

f

h

g

k j

d

j

k

Page 23: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

23

Could we do the allocation inthe previous example with 3

registers?

Page 24: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

24

Example:Simplify (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

(h,no-spill)

stack

Page 25: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

25

Example:Simplify (K=3)

b mkj

g

d

c

e

f

(Appel, pp. 237)

(g, no-spill)(h, no-spill)

stack

Page 26: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

26

Example:Freeze (K=3)

b mkj

d

c

e

f

(Appel, pp. 237)

(g, no-spill)(h, no-spill)

stack

Coalescing may make things worse (not always).

George’s rule would coalesce the move d-c, Briggs’ rule would freeze.

Page 27: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

27

Example:Simplify (K=3)

b mkj

d

c

e

f

(Appel, pp. 237)

(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 28: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

28

Example:Potential Spill (K=3)

b mkj

d

e

f

(Appel, pp. 237)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Neither coalescing norfreezing help us.

At this point we shoulduse some profitabilityanalysis to choose anode as may-spill.

Page 29: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

29

Example:Simplify (K=3)

b mkj

d

f

(Appel, pp. 237)

(f, no-spill)(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 30: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

30

Example:Simplify (K=3)

b mkj

d

(Appel, pp. 237)

(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 31: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

31

Example:Coalesce (K=3)

bkj

d

(Appel, pp. 237)

(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 32: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

32

Example:Coalesce (K=3)

kj-b

d

(Appel, pp. 237)

(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 33: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

33

Example:Coalesce (K=3)

kj-b

(Appel, pp. 237)

(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 34: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

34

Example:Coalesce (K=3)

j-b

(Appel, pp. 237)

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

stack

Page 35: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

35

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 36: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

36

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 37: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

37

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 38: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

38

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 39: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

39

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 40: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

40

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

This is when our optimism couldhave paid off.

Page 41: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

41

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 42: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

42

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 43: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

43

Example:Select (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

stack

R1

R2

R3

(j-b, no-spill)(k, no-spill)(d, no-spill)(m, no-spill)(f, no-spill)

(e, may-spill)(c, no-spill)(g, no-spill)(h, no-spill)

Page 44: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

44

So, is it possible for K=3?

b mkj

gh

d

c

e

f

(Appel, pp. 237)

Page 45: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

45

Example:Simplify (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

Page 46: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

46

Example:Simplify (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

Page 47: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

47

Example:Simplify (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

Page 48: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

48

Example:Simplify (K=3)

b mkj

gh

d

c

e

f

(Appel, pp. 237)

Impossible!

But only 3 variables are live at any time…there may be a way?

Page 49: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

49

Example as basic block:3 Registers by renaming k & j

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

b := mem[f]

c := e + 8

d := c

j’ := b

k’ := m + 4

LIVE-OUT: d k’ j’

m

e

f

h

g

k j

b

c

dk’

j’

Page 50: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

50

Example as basic block:3 Registers by renaming k & j

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

j’ := mem[f]

d := e + 8

k’ := m + 4

LIVE-OUT: d k’ j’

m

e

f

h

g

k j

d

k’

j’

Page 51: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

51

Example as basic block:A 3-coloring of the graph

b mkj

gh

d

c

e

f

(Appel, pp. 237)

The two assignments of k (resp. j) can be placed in two different registers.

k’

j’

Page 52: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

52

Example as a loop: 3 Registers are enough!

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

b := mem[f]

c := e + 8

d := c

j := b

k := m + 4

LIVE-OUT: d k j

m

f

h

g

k j

b

c

d

e

kj

Page 53: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

53

Example as a loop: 3 Registers are enough!

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

b := mem[f]

d := e + 8

k := m + 4

LIVE-OUT: d k j

m

f

h

g

k j

j

d

e

k

Page 54: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

54

Example as a loop:3 Registers are enough!

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

e := mem[j+8]

m := mem[j+16]

j := mem[f]

k := m + 4

d := e + 8

LIVE-OUT: d k j

m

f

h

g

k j

j

e

kd

Page 55: Borrowed from  J.  N.  Amaral, slightly modified  cs.ualberta/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization

55

Example as a loop:3 Registers are enough!

LIVE-IN: k j

g := mem[j+12]

h := k -1

f := g + h

j’ := j

e := mem[j’+8]

m := mem[j’+16]

j := mem[f’]

k := m + 4

d := e + 8

LIVE-OUT: d k j

m

f

h

g

k j

j

d

e

f’ := fj’

f’

k