Your SlideShare is downloading. ×
0
Register AllocationGuido Wachsmuth       Delft                              Course IN4303, 2012/13       University of    ...
Overviewtoday’s lectureinterference graphs  •   construction during liveness analysis                                     ...
Overviewtoday’s lectureinterference graphs  •   construction during liveness analysisgraph colouring  •   assign registers...
Overviewtoday’s lectureinterference graphs  •   construction during liveness analysisgraph colouring  •   assign registers...
Overviewtoday’s lectureinterference graphs  •   construction during liveness analysisgraph colouring  •   assign registers...
Iinterference graphs                      Register Allocation   3
Recap: Liveness analysisterminology              a ← 0              b ← a + 1              c ← c + b              a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0              b ← a + 1              c ← c + b              a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + b              a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * ...
Recap: Liveness analysisterminologydefinition    a ← 0                           live-inusage         b ← a + 1           ...
Recap: Liveness analysisterminologydefinition    a ← 0                           live-inusage         b ← a + 1           ...
Recap: Liveness analysisterminologydefinition    a ← 0        live-out                           live-inusage         b ← ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Recap: Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b       ...
Interference graphsexample   a ← 0   b ← a + 1   c ← c + b   a ← 2 * b   if a < N   return c                      Register...
Interference graphsexample   a ← 0   b ← a + 1                      a              b   c ← c + b   a ← 2 * b              ...
Interference graphsexample   a ← 0   b ← a + 1                      a              b   c ← c + b   a ← 2 * b              ...
Interference graphsexample   a ← 0   b ← a + 1                      a              b   c ← c + b   a ← 2 * b              ...
IIgraph colouring                  Register Allocation   7
Graph colouringexample   a ← 0   b ← a + 1                  a                b   c ← c + b   a ← 2 * b                    ...
Graph colouringexample   a ← 0   b ← a + 1                  a                b   c ← c + b   a ← 2 * b                    ...
Graph colouringexample   a ← 0   b ← a + 1                  a                b   c ← c + b   a ← 2 * b                    ...
Graph colouringexample   a ← 0   b ← a + 1                  a                b   c ← c + b   a ← 2 * b                    ...
Graph colouringexample r1 ← 0 r1 ← r1 + 1                  a                b r2 ← r2 + r1 r1 ← 2 * r1                    ...
Graph colouringstepssimplify        remove node of insignificant degree (fewer than k edges)spill        remove node with ...
Graph colouringexample with 2 colours             a           b                 c                             Register All...
Graph colouringexample with 2 colours             a           b                 c                                    a    ...
Graph colouringexample with 2 colours             a           b                 c                                    c    ...
Graph colouringexample with 2 colours             a           b                 c                                    c    ...
Graph colouringexample with 2 colours             a           b                 c                                    a    ...
Graph colouringexample with 2 colours             a           b                 c                             Register All...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m h     ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m h     ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m       ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m       ...
Graph colouringexample with 4 colours                    f                    e                                           ...
Graph colouringexample with 4 colours                    f                    e                                 j         ...
Graph colouringexample with 4 colours                    f                                                      e         ...
Graph colouringexample with 4 colours                                                      f                    f         ...
Graph colouringexample with 4 colours                                b                                                    ...
Graph colouring                                       cexample with 4 colours                                b            ...
Graph colouring                                       cexample with 4 colours                                b            ...
Graph colouringexample with 4 colours                                b                                                    ...
Graph colouringexample with 4 colours                                                      f                    f         ...
Graph colouringexample with 4 colours                    f                                                      e         ...
Graph colouringexample with 4 colours                    f                    e                                 j         ...
Graph colouringexample with 4 colours                    f                    e                                           ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m       ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m       ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m h     ...
Graph colouringexample with 4 colours                    f                    e j     k                 b         m h     ...
Optimistic colouringstepssimplify        remove node of insignificant degree (fewer than k edges)spill        remove node ...
Optimistic colouringexample with 2 colours                 d             a           b                 c                  ...
Optimistic colouringexample with 2 colours                 d             a           b                 c                  ...
Optimistic colouringexample with 2 colours                 d             a           b                 c                  ...
Optimistic colouringexample with 2 colours                 d             a           b                                    ...
Optimistic colouringexample with 2 colours                 d             a           b                                    ...
Optimistic colouringexample with 2 colours                 d             a           b                                    ...
Optimistic colouringexample with 2 colours                 d             a           b                                    ...
Optimistic colouringexample with 2 colours                 d             a           b                 c                  ...
Optimistic colouringexample with 2 colours                 d             a           b                 c                  ...
Spillingstepssimplify        remove node of insignificant degree (less than k edges)spill        remove node of significan...
Spillingexample with 2 colours             a           b                 c                             Register Allocation...
Spillingexample with 2 colours             a           b                 c                             potential spill    ...
Spillingexample with 2 colours             a           b                 c                                                ...
Spillingexample with 2 colours             a           b                 c                                                ...
Spillingexample with 2 colours             a           b                 c                             potential spill    ...
Spillingexample with 2 colours             a           b                 c                             actual spill       ...
Spillingexample           a ← 1           b ← a + 1           c ← b + 1           a ← c + a           return b            ...
Spillingexample  a1 ← 1       a ← 1  M[42] ← a1               b ← a + 1               c ← b + 1               a ← c + a   ...
Spillingexample  a1 ← 1       a ← 1  M[42] ← a1               b ← a + 1   a2 ← M[42]                           b ← a2 + 1 ...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample  a1 ← 1        a ← 1  M[42] ← a1                b ← a + 1   a2 ← M[42]                            b ← a2 +...
Spillingexample           a ← 1           b ← a + 1           c ← b + 1           a ← c + a           return b            ...
Spillingexample               a ← 1               b ← a + 1  b2 ← M[42]   c ← b + 1  c ← b2 + 1               a ← c + a   ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
Spillingexample               a ← 1               b ← a + 1   b1 ← a + 1                           M[42] ← b1  b2 ← M[42] ...
coffee break               Register Allocation 40
IIIcoalescing             Register Allocation 41
Recap: graph colouringexample               f               ej     k                  b         mh     g                  ...
Recap: graph colouringexample               f               ej     k                  b         mh     g                  ...
Recap: graph colouringexample               f               ej     k                  b         mh     g                  ...
Coalescingbetter solution                  f                  e j     k              b         m h     g              d   ...
Coalescingbetter solution                  f                  e j     k              b         m h     g              d   ...
Coalescingbetter solution                  f                  e j     k              b         m h     g              d   ...
Coalescingcoalescing nodes                   f                   ej/b    k                      m h     g               c/...
Coalescingconservative strategiesBriggs  •      a/b has fewer than k neighbours of significant degree  •      nodes of ins...
Graph colouringstepssimplify        remove non-move-related node of insignificant degreecoalescefreeze        turn move-re...
Coalescingexample             f             ej     k          b         mh     g          d         c                     ...
Coalescingexample             f             ej     k          b         mh     g          d         c                  g  ...
Coalescingexample             f             ej     k          b         m                                              hh ...
Coalescingexample             f             ej     k          b         m                  k                              ...
Coalescingexample             f             ej     k          b          m                  k                             ...
Coalescingexample             f             ej/b   k                 m                  k                                 ...
Coalescingexample             f             e                                      c/dj/b   k                 m           ...
Coalescingexample             f             e                        j/b                                      c/dj/b   k  ...
Coalescingexample             f                                           f             e                        j/b      ...
Coalescingexample                                           m             f                                           f   ...
Coalescingexample                                           m             f                                           f   ...
Coalescingexample             f                                           f             e                        j/b      ...
Coalescingexample             f             e                        j/b                                      c/dj/b   k  ...
Coalescingexample             f             e                                      c/dj/b   k                 m           ...
Coalescingexample             f             ej/b   k                 m                  k                                 ...
Coalescingexample             f             ej/b   k                 m                                           h h    g ...
Coalescingexample             f             ej/b   k                 m h    g          c/d                       g        ...
Coalescingcoalescing nodes                   f                   ej/b    k                      m h     g               c/...
IVpre-coloured nodes                     Register Allocation 65
Recap: Calling ConventionsCDECLcaller                                                push   21                            ...
Recap: Calling ConventionsSTDCALLcaller                                                push 21                            ...
Recap: Calling ConventionsFASTCALLcaller                                                  mov ECX 21                      ...
Pre-coloured nodesrepresenting registersnodes  •     register = pre-coloured node  •     no simplify, no spill  •     coal...
Vsummary          Register Allocation 70
Summarylessons learnedHow can we assign registers to local variables and temporaries?  •   perform liveness analysis  •   ...
Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002Lal George, An...
Outlookcoming nextimperative and object-oriented languages  •   Lecture 11: Register Allocation today  •   Lecture 12: Gar...
copyrights             Register Allocation 74
Register Allocation 75
Picturesattribution & copyrightsSlide 1:   Colors #2 by Carmelo Speltino, some rights reservedSlide 40:   Tchibo by Domini...
Upcoming SlideShare
Loading in...5
×

Compiling Imperative and Object-Oriented Languages - Register Allocation

2,179

Published on

Presentation slides for lecture 9 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,179
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Transcript of "Compiling Imperative and Object-Oriented Languages - Register Allocation"

    1. 1. Register AllocationGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
    2. 2. Overviewtoday’s lectureinterference graphs • construction during liveness analysis Register Allocation 2
    3. 3. Overviewtoday’s lectureinterference graphs • construction during liveness analysisgraph colouring • assign registers to local variables and compiler temporaries • store local variables and temporaries in memory Register Allocation 2
    4. 4. Overviewtoday’s lectureinterference graphs • construction during liveness analysisgraph colouring • assign registers to local variables and compiler temporaries • store local variables and temporaries in memorycoalescing • handle move instructions Register Allocation 2
    5. 5. Overviewtoday’s lectureinterference graphs • construction during liveness analysisgraph colouring • assign registers to local variables and compiler temporaries • store local variables and temporaries in memorycoalescing • handle move instructionspre-coloured nodes Register Allocation 2
    6. 6. Iinterference graphs Register Allocation 3
    7. 7. Recap: Liveness analysisterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
    8. 8. Recap: Liveness analysisterminologydefinition a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
    9. 9. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
    10. 10. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b if a < N return c Register Allocation 4
    11. 11. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * busage if a < N return c Register Allocation 4
    12. 12. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b live-inusage if a < N return c Register Allocation 4
    13. 13. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N return c Register Allocation 4
    14. 14. Recap: Liveness analysisterminologydefinition a ← 0 live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N return c Register Allocation 4
    15. 15. Recap: Liveness analysisterminologydefinition a ← 0 live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N live-out return c Register Allocation 4
    16. 16. Recap: Liveness analysisterminologydefinition a ← 0 live-out live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N live-out return c Register Allocation 4
    17. 17. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    18. 18. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    19. 19. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    20. 20. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    21. 21. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    22. 22. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    23. 23. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    24. 24. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    25. 25. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    26. 26. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    27. 27. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
    28. 28. Interference graphsexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 6
    29. 29. Interference graphsexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 6
    30. 30. Interference graphsexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 6
    31. 31. Interference graphsexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 6
    32. 32. IIgraph colouring Register Allocation 7
    33. 33. Graph colouringexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 8
    34. 34. Graph colouringexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 8
    35. 35. Graph colouringexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 8
    36. 36. Graph colouringexample a ← 0 b ← a + 1 a b c ← c + b a ← 2 * b c if a < N return c Register Allocation 8
    37. 37. Graph colouringexample r1 ← 0 r1 ← r1 + 1 a b r2 ← r2 + r1 r1 ← 2 * r1 c if r1 < N return r2 Register Allocation 9
    38. 38. Graph colouringstepssimplify remove node of insignificant degree (fewer than k edges)spill remove node with k or more edgesselect add node, select colour Register Allocation 10
    39. 39. Graph colouringexample with 2 colours a b c Register Allocation 11
    40. 40. Graph colouringexample with 2 colours a b c a Register Allocation 11
    41. 41. Graph colouringexample with 2 colours a b c c a Register Allocation 11
    42. 42. Graph colouringexample with 2 colours a b c c a Register Allocation 11
    43. 43. Graph colouringexample with 2 colours a b c a Register Allocation 11
    44. 44. Graph colouringexample with 2 colours a b c Register Allocation 11
    45. 45. Graph colouringexample with 4 colours f e j k b m h g d c Register Allocation 12
    46. 46. Graph colouringexample with 4 colours f e j k b m h g d c g Register Allocation 13
    47. 47. Graph colouringexample with 4 colours f e j k b m h h g d c g Register Allocation 14
    48. 48. Graph colouringexample with 4 colours f e j k b m k h h g d c g Register Allocation 15
    49. 49. Graph colouringexample with 4 colours f e d j k b m k h h g d c g Register Allocation 16
    50. 50. Graph colouringexample with 4 colours f e j d j k b m k h h g d c g Register Allocation 17
    51. 51. Graph colouringexample with 4 colours f e e j d j k b m k h h g d c g Register Allocation 18
    52. 52. Graph colouringexample with 4 colours f f e e j d j k b m k h h g d c g Register Allocation 19
    53. 53. Graph colouringexample with 4 colours b f f e e j d j k b m k h h g d c g Register Allocation 20
    54. 54. Graph colouring cexample with 4 colours b f f e e j d j k b m k h h g d c g Register Allocation 21
    55. 55. Graph colouring cexample with 4 colours b f f e e j d j k b m k h h g d c g Register Allocation 22
    56. 56. Graph colouringexample with 4 colours b f f e e j d j k b m k h h g d c g Register Allocation 23
    57. 57. Graph colouringexample with 4 colours f f e e j d j k b m k h h g d c g Register Allocation 24
    58. 58. Graph colouringexample with 4 colours f e e j d j k b m k h h g d c g Register Allocation 25
    59. 59. Graph colouringexample with 4 colours f e j d j k b m k h h g d c g Register Allocation 26
    60. 60. Graph colouringexample with 4 colours f e d j k b m k h h g d c g Register Allocation 27
    61. 61. Graph colouringexample with 4 colours f e j k b m k h h g d c g Register Allocation 28
    62. 62. Graph colouringexample with 4 colours f e j k b m h h g d c g Register Allocation 29
    63. 63. Graph colouringexample with 4 colours f e j k b m h g d c g Register Allocation 30
    64. 64. Graph colouringexample with 4 colours f e j k b m h g d c Register Allocation 31
    65. 65. Optimistic colouringstepssimplify remove node of insignificant degree (fewer than k edges)spill remove node of significant degree (k or more edges)select add node, select colour Register Allocation 32
    66. 66. Optimistic colouringexample with 2 colours d a b c Register Allocation 33
    67. 67. Optimistic colouringexample with 2 colours d a b c a Register Allocation 33
    68. 68. Optimistic colouringexample with 2 colours d a b c potential spill a Register Allocation 33
    69. 69. Optimistic colouringexample with 2 colours d a b c c potential spill a Register Allocation 33
    70. 70. Optimistic colouringexample with 2 colours d a b b c c potential spill a Register Allocation 33
    71. 71. Optimistic colouringexample with 2 colours d a b b c c potential spill a Register Allocation 33
    72. 72. Optimistic colouringexample with 2 colours d a b c c potential spill a Register Allocation 33
    73. 73. Optimistic colouringexample with 2 colours d a b c potential spill a Register Allocation 33
    74. 74. Optimistic colouringexample with 2 colours d a b c Register Allocation 33
    75. 75. Spillingstepssimplify remove node of insignificant degree (less than k edges)spill remove node of significant degree (k or more edges)select add node, select colouractual spillstart over Register Allocation 34
    76. 76. Spillingexample with 2 colours a b c Register Allocation 35
    77. 77. Spillingexample with 2 colours a b c potential spill a Register Allocation 35
    78. 78. Spillingexample with 2 colours a b c c potential spill a Register Allocation 35
    79. 79. Spillingexample with 2 colours a b c c potential spill a Register Allocation 35
    80. 80. Spillingexample with 2 colours a b c potential spill a Register Allocation 35
    81. 81. Spillingexample with 2 colours a b c actual spill a Register Allocation 35
    82. 82. Spillingexample a ← 1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 36
    83. 83. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 36
    84. 84. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a ← c + a return b Register Allocation 36
    85. 85. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 36
    86. 86. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 37
    87. 87. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 37
    88. 88. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 37
    89. 89. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 37
    90. 90. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 a2 ← M[42] b ← a2 + 1 c ← b + 1 a3 ← M[42] a4 ← c + a3 a ← c + a M[42] ← a4 return b Register Allocation 37
    91. 91. Spillingexample a ← 1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 38
    92. 92. Spillingexample a ← 1 b ← a + 1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b Register Allocation 38
    93. 93. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b Register Allocation 38
    94. 94. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 38
    95. 95. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 39
    96. 96. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 39
    97. 97. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 39
    98. 98. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 39
    99. 99. Spillingexample a ← 1 b ← a + 1 b1 ← a + 1 M[42] ← b1 b2 ← M[42] c ← b + 1 c ← b2 + 1 a ← c + a return b b3 ← M[42] return b3 Register Allocation 39
    100. 100. coffee break Register Allocation 40
    101. 101. IIIcoalescing Register Allocation 41
    102. 102. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
    103. 103. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
    104. 104. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
    105. 105. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
    106. 106. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
    107. 107. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
    108. 108. Coalescingcoalescing nodes f ej/b k m h g c/d Register Allocation 44
    109. 109. Coalescingconservative strategiesBriggs • a/b has fewer than k neighbours of significant degree • nodes of insignificant degree and a/b can be simplified • remaining graph is colourableGeorge • all neighbours of a of significant degree interfere also with b • neighbours of a of insignificant degree can be simplified • subgraph of original graph is colourable Register Allocation 45
    110. 110. Graph colouringstepssimplify remove non-move-related node of insignificant degreecoalescefreeze turn move-related node of insignificant degree into non-move-relatedspillselectstart over Register Allocation 46
    111. 111. Coalescingexample f ej k b mh g d c Register Allocation 47
    112. 112. Coalescingexample f ej k b mh g d c g Register Allocation 48
    113. 113. Coalescingexample f ej k b m hh g d c g Register Allocation 49
    114. 114. Coalescingexample f ej k b m k hh g d c g Register Allocation 50
    115. 115. Coalescingexample f ej k b m k hh g c/d g Register Allocation 51
    116. 116. Coalescingexample f ej/b k m k h h g c/d g Register Allocation 52
    117. 117. Coalescingexample f e c/dj/b k m k h h g c/d g Register Allocation 53
    118. 118. Coalescingexample f e j/b c/dj/b k m k h h g c/d g Register Allocation 54
    119. 119. Coalescingexample f f e j/b c/dj/b k m k h h g c/d g Register Allocation 55
    120. 120. Coalescingexample m f f e j/b c/dj/b k m k h h g c/d g Register Allocation 56
    121. 121. Coalescingexample m f f e j/b c/dj/b k m k h h g c/d g Register Allocation 57
    122. 122. Coalescingexample f f e j/b c/dj/b k m k h h g c/d g Register Allocation 58
    123. 123. Coalescingexample f e j/b c/dj/b k m k h h g c/d g Register Allocation 59
    124. 124. Coalescingexample f e c/dj/b k m k h h g c/d g Register Allocation 60
    125. 125. Coalescingexample f ej/b k m k h h g c/d g Register Allocation 61
    126. 126. Coalescingexample f ej/b k m h h g c/d g Register Allocation 62
    127. 127. Coalescingexample f ej/b k m h g c/d g Register Allocation 63
    128. 128. Coalescingcoalescing nodes f ej/b k m h g c/d Register Allocation 64
    129. 129. IVpre-coloured nodes Register Allocation 65
    130. 130. Recap: Calling ConventionsCDECLcaller push 21 push 42 • push parameters right-to-left on the stack call add _f ESP 8 • clean-up stack after callcallee • save old BP push EBP • initialise new BP mov mov EBP EAX ESP [EBP + 8] • save registers mov EDX [EBP + 12] • return result in AX add pop EAX EBP EDX • restore registers ret • restore BP Register Allocation 66
    131. 131. Recap: Calling ConventionsSTDCALLcaller push 21 push 42 • push parameters right-to-left on the stack call _f@8callee • save old BP • initialise new BP push EBP • save registers mov mov EBP EAX ESP [EBP + 8] • return result in AX mov EDX [EBP + 12] • restore registers add pop EAX EBP EDX • restore BP ret 8 Register Allocation 67
    132. 132. Recap: Calling ConventionsFASTCALLcaller mov ECX 21 mov EDX 42 • passes parameters in registers call @f@8 • pushes additional parameters right-to-left on the stack • cleans up the stackcallee push EBP • save old BP, initialise new BP mov mov EBP ESP EAX ECX • save registers add EAX EDX • return result in AX pop ret EBP • restore registers • restore BP Register Allocation 68
    133. 133. Pre-coloured nodesrepresenting registersnodes • register = pre-coloured node • no simplify, no spill • coalesce possibleedges • all registers interfere with each other • explicit usage of registers • call and return instructions influence liveness Register Allocation 69
    134. 134. Vsummary Register Allocation 70
    135. 135. Summarylessons learnedHow can we assign registers to local variables and temporaries? • perform liveness analysis • build interference graph • colour interference graphWhat to do if the graph is not colourable? • keep local variables in memoryHow to handle move instructions efficiently? • coalesce nodes safely Register Allocation 71
    136. 136. Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002Lal George, Andrew W. Appel: Iterative Register Coalescing.POPL 1996Lal George, Andrew W. Appel: Iterative Register Coalescing.TOPLAS 18(3), 1996 Register Allocation 72
    137. 137. Outlookcoming nextimperative and object-oriented languages • Lecture 11: Register Allocation today • Lecture 12: Garbage Collection Nov 13compiler components & their generators • Lecture 13: Scanning Nov 20 • Lecture 14: LL Parsing Dec 04 • Lecture 15: LR Parsing Dec 11exam preparation • Question & Answer Jan 08 Register Allocation 73
    138. 138. copyrights Register Allocation 74
    139. 139. Register Allocation 75
    140. 140. Picturesattribution & copyrightsSlide 1: Colors #2 by Carmelo Speltino, some rights reservedSlide 40: Tchibo by Dominica Williamson, some rights reservedSlide 76: Romantic Pigeon Date by Harald Hoyer, some rights reserved Register Allocation 76
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×