Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPUT 680 - Compiler Design and Optimization1 Borrowed from J. N. Amaral, slightly modified LIVE-IN: k j.

Similar presentations


Presentation on theme: "CMPUT 680 - Compiler Design and Optimization1 Borrowed from J. N. Amaral, slightly modified LIVE-IN: k j."— Presentation transcript:

1 CMPUT 680 - Compiler Design and Optimization1 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 kj b c d k j

2 CMPUT 680 - Compiler Design and Optimization2 Example: Simplify (K=4) bmkj gh d c e f (Appel, pp. 237) (h,no-spill) stack

3 CMPUT 680 - Compiler Design and Optimization3 Example: Simplify (K=4) bmkj g d c e f (Appel, pp. 237) (g, no-spill) (h, no-spill) stack

4 CMPUT 680 - Compiler Design and Optimization4 Example: Simplify (K=4) bmkj d c e f (Appel, pp. 237) (k, no-spill) (g, no-spill) (h, no-spill) stack

5 CMPUT 680 - Compiler Design and Optimization5 Example: Simplify (K=4) bmj d c e f (Appel, pp. 237) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

6 CMPUT 680 - Compiler Design and Optimization6 Example: Simplify (K=4) bmj d c e (Appel, pp. 237) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

7 CMPUT 680 - Compiler Design and Optimization7 Example: Simplify (K=4) bmj d c (Appel, pp. 237) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

8 CMPUT 680 - Compiler Design and Optimization8 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.

9 CMPUT 680 - Compiler Design and Optimization9 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

10 CMPUT 680 - Compiler Design and Optimization10 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

11 CMPUT 680 - Compiler Design and Optimization11 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

12 CMPUT 680 - Compiler Design and Optimization12 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

13 CMPUT 680 - Compiler Design and Optimization13 Example: Select (K=4) bmkj 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

14 CMPUT 680 - Compiler Design and Optimization14 Example: Select (K=4) bmkj 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

15 CMPUT 680 - Compiler Design and Optimization15 Example: Select (K=4) bmkj 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

16 CMPUT 680 - Compiler Design and Optimization16 Example: Select (K=4) bmkj 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

17 CMPUT 680 - Compiler Design and Optimization17 Example: Select (K=4) bmkj 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

18 CMPUT 680 - Compiler Design and Optimization18 Example: Select (K=4) bmkj 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

19 CMPUT 680 - Compiler Design and Optimization19 Example: Select (K=4) bmkj 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

20 CMPUT 680 - Compiler Design and Optimization20 Example: Select (K=4) bmkj 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

21 CMPUT 680 - Compiler Design and Optimization21 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 kj b c d j k

22 CMPUT 680 - Compiler Design and Optimization22 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 kj d j k

23 CMPUT 680 - Compiler Design and Optimization23 Could we do the allocation in the previous example with 3 registers?

24 CMPUT 680 - Compiler Design and Optimization24 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237) (h,no-spill) stack

25 CMPUT 680 - Compiler Design and Optimization25 Example: Simplify (K=3) bmkj g d c e f (Appel, pp. 237) (g, no-spill) (h, no-spill) stack

26 CMPUT 680 - Compiler Design and Optimization26 Example: Freeze (K=3) bmkj 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.

27 CMPUT 680 - Compiler Design and Optimization27 Example: Simplify (K=3) bmkj d c e f (Appel, pp. 237) (c, no-spill) (g, no-spill) (h, no-spill) stack

28 CMPUT 680 - Compiler Design and Optimization28 Example: Potential Spill (K=3) bmkj d e f (Appel, pp. 237) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack Neither coalescing nor freezing help us. At this point we should use some profitability analysis to choose a node as may-spill.

29 CMPUT 680 - Compiler Design and Optimization29 Example: Simplify (K=3) bmkj d f (Appel, pp. 237) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

30 CMPUT 680 - Compiler Design and Optimization30 Example: Simplify (K=3) bmkj d (Appel, pp. 237) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

31 CMPUT 680 - Compiler Design and Optimization31 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

32 CMPUT 680 - Compiler Design and Optimization32 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

33 CMPUT 680 - Compiler Design and Optimization33 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

34 CMPUT 680 - Compiler Design and Optimization34 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

35 CMPUT 680 - Compiler Design and Optimization35 Example: Select (K=3) bmkj 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)

36 CMPUT 680 - Compiler Design and Optimization36 Example: Select (K=3) bmkj 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)

37 CMPUT 680 - Compiler Design and Optimization37 Example: Select (K=3) bmkj 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)

38 CMPUT 680 - Compiler Design and Optimization38 Example: Select (K=3) bmkj 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)

39 CMPUT 680 - Compiler Design and Optimization39 Example: Select (K=3) bmkj 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)

40 CMPUT 680 - Compiler Design and Optimization40 Example: Select (K=3) bmkj 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 could have paid off.

41 CMPUT 680 - Compiler Design and Optimization41 Example: Select (K=3) bmkj 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)

42 CMPUT 680 - Compiler Design and Optimization42 Example: Select (K=3) bmkj 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)

43 CMPUT 680 - Compiler Design and Optimization43 Example: Select (K=3) bmkj 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)

44 CMPUT 680 - Compiler Design and Optimization44 So, is it possible for K=3? bmkj gh d c e f (Appel, pp. 237)

45 CMPUT 680 - Compiler Design and Optimization45 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

46 CMPUT 680 - Compiler Design and Optimization46 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

47 CMPUT 680 - Compiler Design and Optimization47 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

48 CMPUT 680 - Compiler Design and Optimization48 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237) Impossible! But only 3 variables are live at any time…there may be a way?

49 CMPUT 680 - Compiler Design and Optimization49 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 kj b c d k’ j’

50 CMPUT 680 - Compiler Design and Optimization50 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 kj d k’ j’

51 CMPUT 680 - Compiler Design and Optimization51 Example as basic block: A 3-coloring of the graph bmkj gh d c e f (Appel, pp. 237) The two assignments of k (resp. j) can be placed in two different registers. k’ j’

52 CMPUT 680 - Compiler Design and Optimization52 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 kj b c d e k j

53 CMPUT 680 - Compiler Design and Optimization53 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 kj j d e k

54 CMPUT 680 - Compiler Design and Optimization54 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 kj j e k d

55 CMPUT 680 - Compiler Design and Optimization55 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 kj j d e f’ := f j’ f’ k


Download ppt "CMPUT 680 - Compiler Design and Optimization1 Borrowed from J. N. Amaral, slightly modified LIVE-IN: k j."

Similar presentations


Ads by Google