Your SlideShare is downloading. ×
  • Like
Compiling Imperative and Object-Oriented Languages - Register Allocation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Compiling Imperative and Object-Oriented Languages - Register Allocation

  • 833 views
Published

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

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

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
833
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
24
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

  • 1. Register AllocationGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
  • 2. Overviewtoday’s lectureinterference graphs • construction during liveness analysis Register Allocation 2
  • 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. 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. 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. Iinterference graphs Register Allocation 3
  • 7. Recap: Liveness analysisterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
  • 8. Recap: Liveness analysisterminologydefinition a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
  • 9. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 4
  • 10. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b if a < N return c Register Allocation 4
  • 11. Recap: Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * busage if a < N return c Register Allocation 4
  • 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. 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. 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. 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. 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. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 18. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 19. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 20. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 21. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 22. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 23. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 24. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 25. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 26. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 27. Recap: Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 5
  • 28. Interference graphsexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Register Allocation 6
  • 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. 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. 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. IIgraph colouring Register Allocation 7
  • 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. 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. 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. 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. 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. 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. Graph colouringexample with 2 colours a b c Register Allocation 11
  • 40. Graph colouringexample with 2 colours a b c a Register Allocation 11
  • 41. Graph colouringexample with 2 colours a b c c a Register Allocation 11
  • 42. Graph colouringexample with 2 colours a b c c a Register Allocation 11
  • 43. Graph colouringexample with 2 colours a b c a Register Allocation 11
  • 44. Graph colouringexample with 2 colours a b c Register Allocation 11
  • 45. Graph colouringexample with 4 colours f e j k b m h g d c Register Allocation 12
  • 46. Graph colouringexample with 4 colours f e j k b m h g d c g Register Allocation 13
  • 47. Graph colouringexample with 4 colours f e j k b m h h g d c g Register Allocation 14
  • 48. Graph colouringexample with 4 colours f e j k b m k h h g d c g Register Allocation 15
  • 49. Graph colouringexample with 4 colours f e d j k b m k h h g d c g Register Allocation 16
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. Graph colouringexample with 4 colours f e j d j k b m k h h g d c g Register Allocation 26
  • 60. Graph colouringexample with 4 colours f e d j k b m k h h g d c g Register Allocation 27
  • 61. Graph colouringexample with 4 colours f e j k b m k h h g d c g Register Allocation 28
  • 62. Graph colouringexample with 4 colours f e j k b m h h g d c g Register Allocation 29
  • 63. Graph colouringexample with 4 colours f e j k b m h g d c g Register Allocation 30
  • 64. Graph colouringexample with 4 colours f e j k b m h g d c Register Allocation 31
  • 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. Optimistic colouringexample with 2 colours d a b c Register Allocation 33
  • 67. Optimistic colouringexample with 2 colours d a b c a Register Allocation 33
  • 68. Optimistic colouringexample with 2 colours d a b c potential spill a Register Allocation 33
  • 69. Optimistic colouringexample with 2 colours d a b c c potential spill a Register Allocation 33
  • 70. Optimistic colouringexample with 2 colours d a b b c c potential spill a Register Allocation 33
  • 71. Optimistic colouringexample with 2 colours d a b b c c potential spill a Register Allocation 33
  • 72. Optimistic colouringexample with 2 colours d a b c c potential spill a Register Allocation 33
  • 73. Optimistic colouringexample with 2 colours d a b c potential spill a Register Allocation 33
  • 74. Optimistic colouringexample with 2 colours d a b c Register Allocation 33
  • 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. Spillingexample with 2 colours a b c Register Allocation 35
  • 77. Spillingexample with 2 colours a b c potential spill a Register Allocation 35
  • 78. Spillingexample with 2 colours a b c c potential spill a Register Allocation 35
  • 79. Spillingexample with 2 colours a b c c potential spill a Register Allocation 35
  • 80. Spillingexample with 2 colours a b c potential spill a Register Allocation 35
  • 81. Spillingexample with 2 colours a b c actual spill a Register Allocation 35
  • 82. Spillingexample a ← 1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 36
  • 83. Spillingexample a1 ← 1 a ← 1 M[42] ← a1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 36
  • 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. 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. 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. 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. 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. 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. 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. Spillingexample a ← 1 b ← a + 1 c ← b + 1 a ← c + a return b Register Allocation 38
  • 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. 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. 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. 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. 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. 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. 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. 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. coffee break Register Allocation 40
  • 101. IIIcoalescing Register Allocation 41
  • 102. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
  • 103. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
  • 104. Recap: graph colouringexample f ej k b mh g d c Register Allocation 42
  • 105. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
  • 106. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
  • 107. Coalescingbetter solution f e j k b m h g d c Register Allocation 43
  • 108. Coalescingcoalescing nodes f ej/b k m h g c/d Register Allocation 44
  • 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. 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. Coalescingexample f ej k b mh g d c Register Allocation 47
  • 112. Coalescingexample f ej k b mh g d c g Register Allocation 48
  • 113. Coalescingexample f ej k b m hh g d c g Register Allocation 49
  • 114. Coalescingexample f ej k b m k hh g d c g Register Allocation 50
  • 115. Coalescingexample f ej k b m k hh g c/d g Register Allocation 51
  • 116. Coalescingexample f ej/b k m k h h g c/d g Register Allocation 52
  • 117. Coalescingexample f e c/dj/b k m k h h g c/d g Register Allocation 53
  • 118. Coalescingexample f e j/b c/dj/b k m k h h g c/d g Register Allocation 54
  • 119. Coalescingexample f f e j/b c/dj/b k m k h h g c/d g Register Allocation 55
  • 120. Coalescingexample m f f e j/b c/dj/b k m k h h g c/d g Register Allocation 56
  • 121. Coalescingexample m f f e j/b c/dj/b k m k h h g c/d g Register Allocation 57
  • 122. Coalescingexample f f e j/b c/dj/b k m k h h g c/d g Register Allocation 58
  • 123. Coalescingexample f e j/b c/dj/b k m k h h g c/d g Register Allocation 59
  • 124. Coalescingexample f e c/dj/b k m k h h g c/d g Register Allocation 60
  • 125. Coalescingexample f ej/b k m k h h g c/d g Register Allocation 61
  • 126. Coalescingexample f ej/b k m h h g c/d g Register Allocation 62
  • 127. Coalescingexample f ej/b k m h g c/d g Register Allocation 63
  • 128. Coalescingcoalescing nodes f ej/b k m h g c/d Register Allocation 64
  • 129. IVpre-coloured nodes Register Allocation 65
  • 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. 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. 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. 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. Vsummary Register Allocation 70
  • 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. 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. 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. copyrights Register Allocation 74
  • 139. Register Allocation 75
  • 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