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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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.

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

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

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

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

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

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

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)

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)

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)

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)

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)

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.

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)

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)

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)

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

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

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

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

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?

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’

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’

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’

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

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

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

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