fall 2016-2017 compiler principles lecture 9: register ...comp171/wiki.files/09... · register...
TRANSCRIPT
![Page 1: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/1.jpg)
Fall 2016-2017 Compiler PrinciplesLecture 9: Register Allocation
Roman ManevichBen-Gurion University of the Negev
![Page 2: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/2.jpg)
Syllabus
FrontEnd
Scanning
Top-downParsing (LL)
Bottom-upParsing (LR)
IntermediateRepresentation
Operational Semantics
Lowering
Optimizations
DataflowAnalysis
LoopOptimizations
Code Generation
RegisterAllocation
2
mid-term exam
![Page 3: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/3.jpg)
Previously• Dataflow framework
– Loop optimizations
– Strong liveness analysis dead code elimination
3
![Page 4: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/4.jpg)
agenda• An simple assembly language
• The need for register allocation
• Register allocation algorithms
– Graph coloring
– Linear scan (tentative)
4
![Page 5: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/5.jpg)
Assembly language:ASM
5
![Page 6: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/6.jpg)
Syntax
V n | r
R V Op V
Op - | + | * | / | = | | << | >> | …
C l: skip
| l: r := R | l: r := M[V] | l: M[V] := V| l: Goto l’| l: IfZ x Goto l’ | l: IfNZ x Goto l’| l: Push V | l: Pop r | Call foo
ASM C+
6
n Num Numeralsl Num Labelsr Reg Registers
![Page 7: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/7.jpg)
7
Register allocation motivation
![Page 8: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/8.jpg)
Registers
• Most machines have a set of registers, dedicated memory locations that
– can be accessed quickly,
– can have computations performed on them, and
– exist in small quantity
• Using registers intelligently is a critical stepin any compiler
– A good register allocator can generate code orders of magnitude better than a bad register allocator
8
![Page 9: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/9.jpg)
Register allocation
• In IL, there are an unlimited number of variables
• On a physical machine there are a small number of registers:
– x86 has four general-purpose registers and a number of specialized registers
– MIPS has twenty-four general-purpose registers and eight special-purpose registers
• Register allocation is the process of assigning variables to registers and managing data transfer in and out of registers
9
![Page 10: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/10.jpg)
Challenges in register allocation
• Registers are scarce– Often substantially more IR variables than registers– Need to find a way to reuse registers whenever possible
• Registers are complicated– x86: Each register made of several smaller registers;
can't use a register and its constituent registers at the same time
– x86: Certain instructions must store their results in specific registers; can't store values there if you want to use those instructions
– MIPS: Some registers reserved for the assembler or operating system
– Most architectures: Some registers must be preserved across function calls
10
![Page 11: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/11.jpg)
11
naive register allocation
![Page 12: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/12.jpg)
Simple approach
• Problem: program execution very inefficient –moving data back and forth between memory and registers
12
x := y + z
mov 16(%ebp), %eaxmov 20(%ebp), %ebxadd %ebx, %eaxmov %eax, 24(%ebp)
• Straightforward solution:• Allocate each variable in activation record• At each instruction:
• bring values needed into registers (rematerialization),• perform operation,• then store result to memory
![Page 13: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/13.jpg)
13
Reusing registers
![Page 14: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/14.jpg)
Find a register allocation
14
b := a + 2
c := b * b
b := c + 1
Ret b * a
eax
ebx
registerregistervariable
?a
?b
?c
![Page 15: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/15.jpg)
Is this a valid allocation?
eax
ebx
register
15
b := a + 2
c := b * b
b := c + 1
Ret b * a
registervariable
eaxa
ebxb
eaxc
ebx := eax + 2
eax := ebx * ebx
ebx := eax + 1
Ret ebx * eax
Overwrites previous value of ‘a’ also stored in eax
![Page 16: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/16.jpg)
Is this a valid allocation?
eax
ebx
register
16
b := a + 2
c := b * b
b := c + 1
Ret b * a
registervariable
ebxa
eaxb
eaxc
eax := ebx + 2
eax := eax * eax
eax := eax + 1
Ret eax * ebx
Value of ‘c’ stored in eaxis not needed anymore so reuse it for ‘b’
![Page 17: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/17.jpg)
17
Register interference graph
![Page 18: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/18.jpg)
Main idea
• For every command c, we have live[c]– Set of temporaries live out of c
• Two variables interfere if they appear in the same live[c] of any command c– Cannot be allocated to the same register
• Conversely, if two variables do not interfere with each other, they can be assigned the same register– We say they have disjoint live ranges
• How to assign registers to variables?
18
![Page 19: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/19.jpg)
Interference graph
• Nodes of the graph = variables
• Edges connect variables that interfere with one another
• Nodes will be assigned a color corresponding to the register assigned to the variable
• Two colors can’t be next to one another in the graph
19
![Page 20: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/20.jpg)
Liveness analysis
b := a + 2
c := b * b
b := c + 1
Ret b * a
20
![Page 21: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/21.jpg)
Liveness analysis
b := a + 2
c := b * b
b := c + 1{b, a}
Ret b * a
21
![Page 22: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/22.jpg)
Liveness analysis
b := a + 2
c := b * b{a, c}
b := c + 1{b, a}
Ret b * a
22
![Page 23: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/23.jpg)
Liveness analysis
b := a + 2{b, a}
c := b * b{a, c}
b := c + 1{b, a}
Ret b * a
23
![Page 24: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/24.jpg)
Liveness analysis
{a}b := a + 2
{b, a}c := b * b
{a, c}b := c + 1
{b, a}Ret b * a
24
![Page 25: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/25.jpg)
Interference graph
a
cb
eax
ebx
color register
25
{a}b := a + 2
{b, a}c := b * b
{a, c}b := c + 1
{b, a}Ret b * a
![Page 26: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/26.jpg)
Colored graph
a
cb
eax
ebx
color register
26
{a}b := a + 2
{b, a}c := b * b
{a, c}b := c + 1
{b, a}Ret b * a
![Page 27: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/27.jpg)
27
![Page 28: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/28.jpg)
28
![Page 29: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/29.jpg)
29
Graph coloring
![Page 30: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/30.jpg)
Graph coloring
• Register allocation is equivalent to graph-coloring, which is NP-hard if there are at least three registers
• No good polynomial-time algorithms(or even good approximations!) are knownfor this problem
• We have to be content with a heuristic that is good enough for RIGs that arise in practice
30
![Page 31: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/31.jpg)
Coloring by simplification [Kempe 1879]
• How to find a K-coloring of a graph
• Observation:
– Suppose we are trying to K-color a graph and find a node with fewer than K edges
– If we delete this node from the graph and color what remains, we can find a color for this node if we add it back in
– Reason: fewer than K neighbors some color must be left over
31
![Page 32: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/32.jpg)
Coloring by simplification [Kempe 1879]
• How to find a K-coloring of a graph• Phase 1: Simplify
– Repeatedly simplify graph – When a variable (i.e., graph node) is
removed, push it on a stack
• Phase 2: Select– Unwind stack and reconstruct the graph as
follows:• Pop variable from the stack• Add it back to the graph• Color the node for that variable with a color
that it doesn’t interfere with
32
simplify
select
![Page 33: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/33.jpg)
33
Coloring example
![Page 34: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/34.jpg)
Coloring for K=2
b
ed
a
c
stack:
eax
ebx
color register
34
![Page 35: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/35.jpg)
Coloring for K=2
b
ed
a
stack:
c
c
eax
ebx
color register
35
![Page 36: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/36.jpg)
Coloring for K=2
b
ed
a
stack:
ec
c
eax
ebx
color register
36
![Page 37: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/37.jpg)
Coloring for K=2
b
ed
a
stack:
aec
c
eax
ebx
color register
37
![Page 38: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/38.jpg)
Coloring for K=2
b
ed
a
stack:baec
c
eax
ebx
color register
38
![Page 39: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/39.jpg)
Coloring for K=2
b
ed
a
stack:dbaec
c
eax
ebx
color register
39
![Page 40: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/40.jpg)
Coloring for K=2
b
ed
eax
ebx
color register
a
stack:
baec
c
40
![Page 41: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/41.jpg)
Coloring for K=2
b
e
a
stack:
aec
c
eax
ebx
color register
d
41
![Page 42: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/42.jpg)
Coloring for K=2
e
a
stack:
ec
c
eax
ebx
color register
b
d
42
![Page 43: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/43.jpg)
Coloring for K=2
e
stack:
c
c
eax
ebx
color register
a
b
d
43
![Page 44: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/44.jpg)
Coloring for K=2
stack:
c
eax
ebx
color register
e
a
b
d
44
![Page 45: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/45.jpg)
45
spilling
![Page 46: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/46.jpg)
Failure of heuristic
• If the graph cannot be colored, it will eventually be simplified to graph in which every node has at least K neighbors
• Sometimes, the graph is still K-colorable!
• Finding a K-coloring in all situations is anNP-complete problem
– We will have to approximate to make register allocators fast enough
46
![Page 47: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/47.jpg)
Example where Kempe’s approach fails
c
eax
ebx
color register
e
a
b
d
47
stack:
![Page 48: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/48.jpg)
Example where Kempe’s approach fails
c
eax
ebx
color register
e
a
b
d
48
stack:
d
![Page 49: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/49.jpg)
But RIG is actually 2-colorable
c
eax
ebx
color register
e
a
b
d
49
![Page 50: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/50.jpg)
Example of non 2-colorable graph
c
eax
ebx
color register
e
a
b
d
Some graphs can’t be colored in K colors:
50
A 3-clique
![Page 51: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/51.jpg)
Chaitin’s algorithm [CC’82]
• Choose and remove an arbitrary node, marking it “troublesome”
– Use heuristics to choose which one:
– When adding node back in, it may be possible to find a valid color
– Otherwise, we have to spill that node
51
?
![Page 52: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/52.jpg)
Chaitin’s algorithm [CC’82]
• Choose and remove an arbitrary node, marking it “troublesome”
– Use heuristics to choose which one:
– When adding node back in, it may be possible to find a valid color
– Otherwise, we have to spill that node(lowest priority)
52
spill priority = (uo + 10 ui) / deguo = #use+defs outside of loopui = #use+defs inside of loop
![Page 53: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/53.jpg)
Spilling
• Phase 3: Spill– once all nodes have K or more neighbors, pick a
node for spilling• There are many heuristics that can be used to pick a
node
• Try to pick node not used much, not in inner loop
• Assign its storage to activation record
– Remove it from graph and mark it as potential spill (technically, push on a spill stack)
• We can now repeat phases 1-2 without this node
53
![Page 54: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/54.jpg)
54
Potential spill example
![Page 55: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/55.jpg)
Potential spill example
• Color the following graph
55
c
a
be
AX
BX
color register
CX
d
f
(color)stack:
potentialspill:
![Page 56: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/56.jpg)
Potential spill example
• Pick a for potential spill
56
c
a
be
AX
BX
color register
CX
d
f
(color)stack:
potentialspill:
![Page 57: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/57.jpg)
Potential spill example
• Simplify d
57
c
a
be
AX
BX
color register
CX
d
f
(color)stack:
potentialspill:a
![Page 58: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/58.jpg)
Potential spill example
• Simplify e
58
c
a
be
AX
BX
color register
CX
d
f
(color)stack:d
potentialspill:a
![Page 59: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/59.jpg)
Potential spill example
• Simplify b
59
c
a
be
AX
BX
color register
CX
d
f
(color)stack:ed
potentialspill:a
![Page 60: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/60.jpg)
Potential spill example
• Simplify c
60
c
a
be
AX
BX
color register
CX
d
f
(color)stack:bed
potentialspill:a
![Page 61: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/61.jpg)
Potential spill example
• Simplify f
61
c
a
be
AX
BX
color register
CX
d
f
(color)stack:cbed
potentialspill:a
![Page 62: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/62.jpg)
Potential spill example
• Select color for f
62
c
a
be
AX
BX
color register
CX
d
f
(color)stack:fcbed
potentialspill:a
![Page 63: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/63.jpg)
Potential spill example
• Select color for c
63
c
a
be
AX
BX
color register
CX
d
f
(color)stack:cbed
potentialspill:a
![Page 64: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/64.jpg)
Potential spill example
• Select color for b
64
c
a
be
AX
BX
color register
CX
d
f
(color)stack:bed
potentialspill:a
![Page 65: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/65.jpg)
Potential spill example
• Select color for e
65
c
a
be
AX
BX
color register
CX
d
f
(color)stack:ed
potentialspill:a
![Page 66: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/66.jpg)
Potential spill example
• Select color for d
66
c
a
be
AX
BX
color register
CX
d
f
(color)stack:d
potentialspill:a
![Page 67: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/67.jpg)
Potential spill example
• Select color for a
67
c
a
be
AX
BX
color register
CX
d
f
(color)stack:
potentialspill:a
![Page 68: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/68.jpg)
Potential spill example
• Done – no actual spill
68
c
a
be
AX
BX
color register
CX
d
f
(color)stack:
potentialspill:
![Page 69: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/69.jpg)
Chaitin’s algorithm [CC’82]
• Phase 1: Simplify• Phase 2: Select
– Unwind stack and reconstruct the graph as follows:• Pop (non-spill) variable from the stack• Add it back to the graph• Color the node for that variable with a color that it doesn’t interfere with its
neighbors
– Unwind spill stack• Pop spill variable• If there is an available color add back to graph• Otherwise mark variable as actual spill
• Phase 3: Spill– If all nodes have K or more neighbors, pick a “heavy” node for spilling
and add to potential spill stack
• Phase 4: Rewrite– Rewrite code for actual spill variables– Recompute liveness information– Repeat phases 1-3
69
![Page 70: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/70.jpg)
Chaitin’s algorithm [CC’82]
70
Simplify
Mark potential spills
Select colorsand detectactual spills
Rewrite codeto implementactual spills
Livenessanalysis
any actualspill done
any potentialspill done
Don’t spill same variable twice
found lightnode
![Page 71: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/71.jpg)
71
actual spill example
![Page 72: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/72.jpg)
Actual spill example
• Apply Chaitin’s algorithm forthe following program
– The set of registers is AX, BX, CX
– Upon any non-deterministic choice,choose by lexicographic order
72
c := e;
a := a + e;
d := a + c;
d := a + b;
d := d + b;
// live = {d}
![Page 73: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/73.jpg)
Step 1: compute liveness
• Let’s compute liveness information
73
{a, b, e}
c := e;
{a, b, c, e}
a := a + e;
{a, b, c}
d := a + c;
{a, b}
d := a + b;
{b, d}
d := d + b;
// live = {d}
![Page 74: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/74.jpg)
Step 2: draw RIG
• Simplify d
74
c
e
a
b
d
(color)stack:
potentialspill:
AX
BX
color register
CX
![Page 75: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/75.jpg)
Potentially spill b
• #use+def(a)=4, #use+def(b)=2,#use+def(c)=2, #use+def(d)=4,#use+def(e)=2
• Priorities: p(a)=4/4, p(b)=2/3, p(c)=2/3, p(e)=2/3• (potentially) spill b
75
c
e
a
b
d
(color)stack:d
potentialspill:
AX
BX
color register
CX
c := e;
a := a + e;
d := a + c;
d := a + b;
d := d + b;
// live = {d}
![Page 76: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/76.jpg)
Simplify
• Simplify a
76
c
e
a
b
d
(color)stack:d
potentialspill:b
AX
BX
color register
CX
![Page 77: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/77.jpg)
Simplify
• Simplify c
77
c
e
a
b
d
(color)stack:ad
potentialspill:b
AX
BX
color register
CX
![Page 78: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/78.jpg)
Simplify
• Simplify e
78
c
e
a
b
d
(color)stack:cad
potentialspill:b
AX
BX
color register
CX
![Page 79: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/79.jpg)
Attempt to color nodes on stack
• Select color for e
79
c
e
a
b
d
(color)stack:ecad
potentialspill:b
AX
BX
color register
CX
![Page 80: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/80.jpg)
Attempt to color nodes on stack
• Select color for c
80
c
e
a
b
d
(color)stack:cad
potentialspill:b
AX
BX
color register
CX
![Page 81: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/81.jpg)
Attempt to color nodes on stack
• Select color for a
81
c
e
a
b
d
(color)stack:ad
potentialspill:b
AX
BX
color register
CX
![Page 82: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/82.jpg)
Attempt to color nodes on stack
• Select color for d
82
c
e
a
b
d
(color)stack:
potentialspill:b
AX
BX
color register
CX
![Page 83: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/83.jpg)
b cannot be colored
• Cannot color b – actual spill
83
c
e
a
b
d
(color)stack:
potentialspill:b
AX
BX
color register
CX
![Page 84: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/84.jpg)
Actual spill: rich assembly
• Suppose assembly allows commands of the form x:=y+M[offset]
• Rewrite program for spilled variable b
– Choose location on frame: b_loc
84
c := e;
a := a + e;
d := a + c;
d := a + M[b_loc];
d := d + M[b_loc];
// live = {d}
c := e;
a := a + e;
d := a + c;
d := a + b;
d := d + b;
// live = {d}
![Page 85: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/85.jpg)
Actual spill: basic assembly
• Rewrite program for spilled variable b
– Choose location on frame: b_loc
– Use temporaries for reading from frame: b1, b2
– Now attempt to color all variables, including temporaries• If unable don’t spill temporaries, choose other variables to spill,
otherwise can go into infinite spill-color loop
85
c := e;
a := a + e;
d := a + c;
b1 := M[b_loc];
d := a + b1;
b2 := M[b_loc];
d := d + b2;
// live = {d}
c := e;
a := a + e;
d := a + c;
d := a + b;
d := d + b;
// live = {d}
![Page 86: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/86.jpg)
Rewriting rules
• Assume we want to spill variable x
• Case 1: a command that reads x:c = l: z := a + x=> xl := M[x_loc]; z := a + xl
• Case 2: a command that writes to x:c = l: x := a + b=> xl := a + b; M[x_loc] := xl
86
![Page 87: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/87.jpg)
Compute liveness for new program
87
{a, e}
c := e;
{a, c, e}
a := a + e;
{a, c}
d := a + c;
{a}
b1 := M[b_loc];
{a, b1}
d := a + b1;
{d}
b2 := M[b_loc];
{d, b2}
d := d + b2;
// live = {d}
![Page 88: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/88.jpg)
Attempt to color
• Simplify b1, b2, d, a, c, e
• Select colors
88
c
e
a
b2
d
AX
BX
color register
CX
b1
![Page 89: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/89.jpg)
Attempt to color
• Simplify b1, b2, d, a, c, e
• Select colors
89
c
e
a
b2
d
AX
BX
color register
CX
b1
![Page 90: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/90.jpg)
Emit code based on colors
90
c
e
a
b2
d
AX
BX
color register
CX
b1
BX := AX;
CX := CX + AX;
AX := CX + BX;
BX := M[b_loc];
AX := CX + BX;
BX := M[b_loc];
AX := AX + BX;
c := e;
a := a + e;
d := a + c;
b1 := M[b_loc];
d := a + b1;
b2 := M[b_loc];
d := d + b2;
![Page 91: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/91.jpg)
Pathological cases
• In general, spilling a variable may not help
– E.g., if there is only one use
• On next iteration choose a different variable to spill
91
![Page 92: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/92.jpg)
92
Register constraints
![Page 93: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/93.jpg)
Handling precolored nodes
• Some variables are pre-assigned to registers– e.g.: mul on x86/pentium
• Uses eax; defines eax, edx
– e.g.: call on x86/pentium• Defines (trashes) caller-save registers eax, ecx, edx
• To properly allocate registers, treat these register uses as special temporary variables and enter into interference graph as precolored nodes
93
![Page 94: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/94.jpg)
Handling precolored nodes
• Simplify. Never remove a pre-colored node – it already has a color, i.e., it is a given register
• Select. Once simplified graph has all colored nodes, add other nodes back in and color them using precolored nodes as starting point
94
![Page 95: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/95.jpg)
95
Optimizing moves
![Page 96: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/96.jpg)
Optimizing move instructions
• Code generation produces a lot of extra MOVE instructions
t5 := t9
• If we can assign t5 and t9 to same register, we can get rid of the MOVE– Effectively, copy propagation at the register
allocation level
• Idea: if t5 and t9 are not connected in inference graph, coalesce them into a single variable; the move will be redundant
96
![Page 97: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/97.jpg)
RIG with MOVE edges
• Add a second type of edges – MOVE edges
97
b
ed
a
c
eax
ebx
color register
![Page 98: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/98.jpg)
Coalescing
• Problem: coalescing nodes can make a graphun-colorable
– Conservative coalescing heuristic
• What should we do?
98
a b a/b
coalesceMOVE
![Page 99: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/99.jpg)
Conservative coalescing
• Coalesce MOVE edge (a,b) if it does not affect colorability
• Definition: A node is heavy if its degree is ≥Kand light otherwise
99
![Page 100: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/100.jpg)
Briggs criterion
• Coalesce only if the merged node ab has <Kheavy neighbors
• Reason:
– Simplify will first remove all light neighbors
– ab will then be adjacent to <K neighbors
– Simplify can remove ab
100
![Page 101: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/101.jpg)
George criterion
• Coalesce only if all heavy neighbors of a are also neighbors of b
• Reason:
– Simplify can remove all light neighbors of a
– Remaining heavy neighbors of a are neighbors of bso if b is colorable then so is a
– The light neighbors of a are light => colorable
101
![Page 102: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/102.jpg)
Coalescing criterion example
c
eax
ebx
color register
e
a
b
102
Can we coalesce (a,e) ?
• By the Briggs criterion?– #(heavy-neighbors(a,e) < K
• By the George criterion?– All heavy neighbors of a are also neighbors of e
![Page 103: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/103.jpg)
Coalescing criterion example
c
eax
ebx
color register
e
a
b
103
Can we coalesce (a,e) ?
• By the Briggs criterion? NO– #(heavy-neighbors(a,e) < K
• By the George criterion? YES– All heavy neighbors of a are also neighbors of e
![Page 104: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/104.jpg)
Simplify, coalesce and freeze
• Phase 1: Simplify– Step 1 (simplify): simplify as much as possible without removing nodes that are the source or
destination of a move(MOVE-related nodes)
– Step 2 (coalesce): coalesce a MOVE-related edge provided low-degree node results– Step 3 (freeze): if neither steps 1 or 2 apply,
freeze a MOVE instruction: low-degree nodes involved are marked not MOVE-related (remove MOVE edge) and try step 1 again
– Step 4 (spill): if all nodes are heavy select a candidate for spilling using a priority function and move to potential spill stack
• Phase 2: Select– Unwind stack and reconstruct the graph as follows:
• Pop variable from the stack• Add it back to the graph• Color the node node with a color that it doesn’t interfere with
– Unwind potential spill stack and attempt to color node – if unable mark corresponding variable as actual spill
• Phase 4: Rewrite– Allocate position in frame for spilled variable v– On each usage of v load to vi from frame
104
![Page 105: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/105.jpg)
Simplify, coalesce and freeze
105
Simplify: recursivelyremove non-MOVE nodeswith <K neighbors, pushingthem onto stack
Coalesce: conservatively mergeunconstrained MOVE-relatednodes via Briggs/George Criterion
Freeze: give up coalescing on somelow-degree MOVE-related node
any coalescedone
any freezedone
any simplifydone
![Page 106: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/106.jpg)
Overall algorithm
106
Simplify, freezeand coalesce
Mark potential spills
Select colorsand detectactual spills
Rewrite codeto implementactual spills
Livenessanalysis
any actualspill done
any potentialspill done
![Page 107: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/107.jpg)
107
Coalesce example
![Page 108: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/108.jpg)
Coalesce example
• Apply Chaitin’s algorithm forthe following program
– The set of registers is AX, BX, CX
– Use the Briggs criterionfor conservative coalescing
– Upon any non-deterministic choice,choose by lexicographic order
108
a := e;
d := a + c;
d := d + b;
// live = {d}
![Page 109: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/109.jpg)
Step 1: compute liveness
109
{b, c, e}
a := e;
{a, b, c}
d := a + c;
{b, d}
d := d + b;
// live = {d}
![Page 110: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/110.jpg)
Attempt to color
• Simplify d
110
c
e
a
b
d
(color)stack:
potentialspill:
AX
BX
color register
CX
![Page 111: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/111.jpg)
Coalesce step
• Cannot simplify a or e because they are MOVE-related
• Coalesce (a,e)
111
c
e
a
b
d
(color)stack:d
potentialspill:
AX
BX
color register
CX
![Page 112: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/112.jpg)
Simplify
• Simplify ae
112
c
ae
b
d
(color)stack:d
potentialspill:
AX
BX
color register
CX
![Page 113: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/113.jpg)
Simplify
• Simplify b
113
c
ae
b
d
(color)stack:aed
potentialspill:
AX
BX
color register
CX
![Page 114: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/114.jpg)
Simplify
• Simplify c
114
c
ae
b
d
(color)stack:baed
potentialspill:
AX
BX
color register
CX
![Page 115: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/115.jpg)
Select
• Color c
115
c
ae
b
d
(color)stack:cbaed
potentialspill:
AX
BX
color register
CX
![Page 116: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/116.jpg)
Select
• Color b
116
c
ae
b
d
(color)stack:baed
potentialspill:
AX
BX
color register
CX
![Page 117: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/117.jpg)
Select
• Color ae
117
c
ae
b
d
(color)stack:aed
potentialspill:
AX
BX
color register
CX
![Page 118: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/118.jpg)
Select
• Color d
118
c
ae
b
d
(color)stack:d
potentialspill:
AX
BX
color register
CX
![Page 119: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/119.jpg)
Select
119
c
ae
b
d
(color)stack:
potentialspill:
AX
BX
color register
CX
![Page 120: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/120.jpg)
Emit code
120
CX := CX;
AX := CX + AX;
AX := AX + BX;
c
AX
BX
color register
ae
b
d
CX
a := e;
d := a + c;
d := d + b;
![Page 121: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/121.jpg)
121
Freeze example
![Page 122: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/122.jpg)
Step 1: compute liveness
122
{b, e}
c := e + b;
{a, c}
e := a;
{a, c}
b := a + c;
{b}
d := b;
{b, d}
d := b + d;
// live = {d}
![Page 123: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/123.jpg)
Attempt to color
• Simplify d
123
c
e
a
b
d
(color)stack:
potentialspill:
AX
BX
color register
![Page 124: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/124.jpg)
Attempt to color
• Can we coalesce a and e according to Briggs?
• Let’s try
124
c
e
a
b
(color)stack:d
potentialspill:
AX
BX
color register
![Page 125: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/125.jpg)
Unsuccessful attempt to color
• No, node becomes heavy
• Undo coalesce and try something else
125
c
ae
b
(color)stack:d
potentialspill:
AX
BX
color register
![Page 126: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/126.jpg)
Freeze a
126
c
e
a
b
(color)stack:d
potentialspill:
AX
BX
color register
![Page 127: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/127.jpg)
Simplify a
127
c
e
a
b
(color)stack:d
potentialspill:
AX
BX
color register
![Page 128: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/128.jpg)
Simplify c
128
c
e
b
(color)stack:ad
potentialspill:
AX
BX
color register
![Page 129: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/129.jpg)
Simplify b
129
e
b
(color)stack:cad
potentialspill:
AX
BX
color register
![Page 130: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/130.jpg)
Simplify e
130
e
(color)stack:bcad
potentialspill:
AX
BX
color register
![Page 131: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/131.jpg)
Select color for e
131
(color)stack:ebcad
potentialspill:
AX
BX
color register
![Page 132: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/132.jpg)
Select color for b
132
(color)stack:bcad
potentialspill:
AX
BX
color register
e
![Page 133: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/133.jpg)
Select color for c
133
(color)stack:cad
potentialspill:
AX
BX
color register
e
b
![Page 134: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/134.jpg)
Select color for a
134
(color)stack:ad
potentialspill:
AX
BX
color register
e
b c
![Page 135: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/135.jpg)
Select color for d
135
(color)stack:d
potentialspill:
AX
BX
color register
e
b c
a
![Page 136: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/136.jpg)
Select color for d
136
(color)stack:
potentialspill:
AX
BX
color register
e
b c
a
d
![Page 137: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/137.jpg)
137
Linear scan register allocation
![Page 138: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/138.jpg)
Live ranges and live intervals
• The live range for a variable is the set of program points at which the variable is alive
• The live interval for a variable is the smallest sub-range of the IR code containing all of a variable’s live ranges
– A property of the IR code, not the CFG
– Less precise than live ranges, but simpler to work with
138
![Page 139: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/139.jpg)
d
Live ranges and live intervals
139
e := d + a
f := b + c
f := f + b
IfZ e Goto _L0
d := e + f
d := e – f
Goto _L1
_L0:
d := e-f
_L1:
g := d
Ret g
a b c e f g
![Page 140: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/140.jpg)
Register allocation with live ranges
• Given the live intervals for all thevariables in the program, we canallocate registers using a simplegreedy algorithm
• Idea: Track which registers are freeat each point
• When a live interval begins, givethat variable a free register
• When a live interval ends, theregister is once again free
• We can't always fit everything intoa register (spill)
140
da b c e f g
![Page 141: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/141.jpg)
Example
141
R0 R1 R2 R3
da b c e f g Free registers
![Page 142: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/142.jpg)
Example
142
R0 R1 R2 R3
da b c e f g Free registers
![Page 143: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/143.jpg)
Example
143
R0 R1 R2 R3
da b c e f g Free registers
![Page 144: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/144.jpg)
Example
144
R0 R1 R2 R3
da b c e f g Free registers
![Page 145: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/145.jpg)
Example
145
R0 R1 R2 R3
da b c e f g Free registers
![Page 146: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/146.jpg)
Example
146
R0 R1 R2 R3
da b c e f g Free registers
![Page 147: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/147.jpg)
Example
147
R0 R1 R2 R3
da b c e f g Free registers
![Page 148: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/148.jpg)
Example
148
R0 R1 R2 R3
da b c e f g Free registers
![Page 149: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/149.jpg)
Example
149
R0 R1 R2 R3
da b c e f g Free registers
![Page 150: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/150.jpg)
Example
150
da b c e f g Free registers
R0 R1 R2 R3
![Page 151: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/151.jpg)
Example
151
da b c e f g Free registers
R0 R1 R2 R3
![Page 152: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/152.jpg)
Example
152
da b c e f g Free registers
R0 R1 R2 R3
![Page 153: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/153.jpg)
Another example
153
R0 R1 R2
da b c e f g Free registers
![Page 154: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/154.jpg)
Another example
154
R0 R1 R2
da b c e f g Free registers
![Page 155: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/155.jpg)
Another example
155
R0 R1 R2
da b c e f g Free registers
![Page 156: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/156.jpg)
Another example
156
R0 R1 R2
da b c e f g Free registers
![Page 157: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/157.jpg)
Another example
157
R0 R1 R2
da b c e f g Free registers
Which register should we use?
![Page 158: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/158.jpg)
Another example
158
R0 R1 R2
da b c e f g Free registers
Which variable should we spill?
One with longer interval
![Page 159: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/159.jpg)
Another example
159
R0 R1 R2
da b c e f g Free registers
![Page 160: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/160.jpg)
Another example
160
R0 R1 R2
da b c e f g Free registers
![Page 161: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/161.jpg)
Another example
161
R0 R1 R2
da b c e f g Free registers
![Page 162: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/162.jpg)
Another example
162
R0 R1 R2
da b c e f g Free registers
![Page 163: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/163.jpg)
Another example
163
R0 R1 R2
da b c e f g Free registers
![Page 164: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/164.jpg)
Another example
164
R0 R1 R2
da b c e f g Free registers
![Page 165: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/165.jpg)
Another example
165
R0 R1 R2
da b c e f g Free registers
![Page 166: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/166.jpg)
Another example
166
R0 R1 R2
da b c e f g Free registers
![Page 167: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/167.jpg)
Another example
167
R0 R1 R2
da b c e f g Free registers
![Page 168: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/168.jpg)
Linear scan register allocation
• A relatively new algorithm (Polleto and Sarkar)
• Advantages:– Very efficient (after computing live intervals, runs in
linear time)
– Produces good code in many instances
– Allocation step works in one pass; can generate code during iteration
– Often used in JIT compilers like Java HotSpot
• Disadvantage– Imprecise due to use of live intervals rather than live
ranges
168
![Page 169: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/169.jpg)
Comparison to RA by coloring
169
![Page 170: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/170.jpg)
RA recap
• Graph-coloring
• Linear scan
170
![Page 171: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/171.jpg)
Advertisement 1
• Program Analysis and Verification course next semester
– How to verify programs do what they are supposed to do
– How to verify they don’t loop forever
– Automatically, without ever running the program
– Possibly teach code synthesis
171
![Page 172: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/172.jpg)
Advertisement 2
• Are you looking to do some cool research?I am looking for enthusiastic students for M.Sc.
• Come talk to me
172
![Page 173: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine](https://reader033.vdocuments.us/reader033/viewer/2022042922/5f6c0c442241d62139793dc0/html5/thumbnails/173.jpg)
Good luck with the exam and the final project!
173