![Page 1: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/1.jpg)
Register Allocation
![Page 2: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/2.jpg)
Register Allocation
• How to assign variables to finitely many registers?
• What to do when it can’t be done?
• How to do so efficiently?
Monday, June 3, 13
![Page 3: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/3.jpg)
Memory Wall
• Disparity between CPU speed and memory access speed improvement
Monday, June 3, 13
![Page 4: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/4.jpg)
Monday, June 3, 13
![Page 5: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/5.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
L1 Cache
L2 Cache
Main Memory
Hard Disk
Network
256B - 8KB
16KB – 64KB
1MB - 4MB
4GB – 256GB
500GB+
HUGE
0.25 – 1ns
1ns – 5ns
5ns – 25ns
25ns – 100ns
3 – 10ms
10 – 2000ms
Registers
![Page 6: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/6.jpg)
One challenge of code generation
• Almost all programming languages expose a coarse view of the memory hierarchy:
• All variables live in “memory.”
• Disk and network explicitly handled separately.
• Challenges in code generation:
• Position objects in a way that takes maximum advantage of the memory hierarchy.
• Do so automatically
Monday, June 3, 13
![Page 7: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/7.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 step in any compiler.
● A good register allocator can generate code orders of magnitude better than a bad register allocator.
![Page 8: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/8.jpg)
Register Allocation
● In TAC, 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.
![Page 9: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/9.jpg)
performance – Affects almost every statement of the program
• Limited mem-to-mem ALU ops, may need two instructions – Probably is the optimization with the most impact!
Register Allocationg• Deciding which values to store in limited
b f i tnumber of registers
• Register allocation has a direct impact onRegister allocation has a direct impact on
– Eliminates expensive memory instructions – # of instructions goes down due to direct
manipulation of registersmanipulation of registers
Saman Amarasinghe & Armando Solar-Lezama 4 6.035 ©MIT�
![Page 10: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/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.
![Page 11: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/11.jpg)
Building a Better Allocator
● Goal: Try to hold as many variables in registers as possible.
● Reduces memory reads/writes.
● Reduces total memory usage.
● We will need to address these questions:
● Which registers do we put variables in?
● What do we do when we run out of registers?
![Page 12: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/12.jpg)
Live Ranges and Live Intervals
● Recall: A variable is live at a particular program point if its value may be read later before it is written.
● Can find this using global liveness analysis.
● The live range for a variable is the set of program points at which that variable is live.
● The live interval for a variable is the smallest subrange of the IR code containing all a variable's live ranges.
● A property of the IR code, not the CFG.
● Less precise than live ranges, but simpler to work with.
![Page 13: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/13.jpg)
Live Ranges and Live Intervals
![Page 14: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/14.jpg)
Live Ranges and Live Intervals
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 15: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/15.jpg)
Live Ranges and Live Intervals
e = d + a
f = b + c
f = f + b
d = e + f d = e – f
g = d
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 16: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/16.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 17: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/17.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 18: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/18.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bb
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 19: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/19.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 20: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/20.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb bd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 21: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/21.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb bd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 22: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/22.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb bebd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 23: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/23.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb bebd bf
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 24: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/24.jpg)
Live Ranges and Live Intervals{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a bcbb bebd bgbf
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
![Page 25: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/25.jpg)
Register Allocation with Live Intervals
● Given the live intervals for all the variables in the program, we can allocate registers using a simple greedy algorithm.
● Idea: Track which registers are free at each point.
● When a live interval begins, give that variable a free register.
● When a live interval ends, the register is once again free.
● We can't always fit everything into a register; we'll see what do to in a minute.
a bcbb ebd bgbf
![Page 26: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/26.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
![Page 27: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/27.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
R0
R1
R2
Free Registers
R2
R3
![Page 28: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/28.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
R0
R1
R2
Free Registers
R2
R3
![Page 29: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/29.jpg)
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
R1
R2
Free Registers
R2
R3
![Page 30: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/30.jpg)
R1
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
R2
Free Registers
R2
R3
![Page 31: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/31.jpg)
R2
R1
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
Free Registers
R2
R3
![Page 32: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/32.jpg)
R3
R2
R1
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0
R1
R2
Free Registers
R2
![Page 33: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/33.jpg)
R0
R3
R2
R1
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
R2
Free Registers
R2
![Page 34: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/34.jpg)
R0
R0
R3
R2
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
R2
Free Registers
R2
![Page 35: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/35.jpg)
R2
R0
R0
R3
R2
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
Free Registers
R2
![Page 36: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/36.jpg)
R2
R2
R0
R0
R3
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
Free Registers
R2
![Page 37: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/37.jpg)
R2
R0
R1
R2
R2
R0
R3
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 38: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/38.jpg)
R0
R2
R0
R1
R2
R2
R3
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 39: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/39.jpg)
R0
R2
R0
R1
R2
R2
R3
R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 40: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/40.jpg)
Another Example
![Page 41: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/41.jpg)
Another Examplebbba bgbe bfc bd
![Page 42: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/42.jpg)
Another Example
R0
R1
R2
bbba bgbe bfc bd
R0
R1
R2
Free Registers
![Page 43: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/43.jpg)
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
R1
R2
Free Registers
![Page 44: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/44.jpg)
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
R1
R2
Free Registers
![Page 45: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/45.jpg)
R1
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
R2
Free Registers
![Page 46: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/46.jpg)
R2
R1
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
Free Registers
![Page 47: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/47.jpg)
R2
R1
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
Free Registers
What do we do
now?
![Page 48: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/48.jpg)
Register Spilling
● If a register cannot be found for a variable v, we may need to spill a variable.
● When a variable is spilled, it is stored in memory rather than a register.
● When we need a register for the spilled variable:
● Evict some existing register to memory.
● Load the variable into the register.
● When done, write the register back to memory and reload the register with its original value.
● Spilling is slow, but sometimes necessary.
![Page 49: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/49.jpg)
R2
R1
R0
R0
Another Examplebbba bgbe bfc bd
R1
R2
Free Registers
What do we do
now?
![Page 50: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/50.jpg)
R0
R2
R1
R0
Another Examplebbba bgbe bfc bd
R1
R2
Free Registers
![Page 51: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/51.jpg)
R0
R0
R2
R1
Another Examplebbba bgbe bfc bd
R1
R2
Free Registers
![Page 52: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/52.jpg)
R2
R0
R0
R2
R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 53: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/53.jpg)
R2
R2
R0
R0
R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 54: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/54.jpg)
R0
R2
R2
R0
R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 55: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/55.jpg)
R0
R0
R2
R2
R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 56: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/56.jpg)
R1
R0
R0
R2
R2
R1
Another Examplebbba bgbe bfc bd Free Registers
![Page 57: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/57.jpg)
R1
R1
R0
R0
R2
R2
Another Examplebbba bgbe bfc bd Free Registers
![Page 58: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/58.jpg)
R1
R1
R0
R0
R2
R2
Another Examplebbba bgbe bfc bd Free Registers
![Page 59: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/59.jpg)
Linear Scan Register Allocation
● This algorithm is called linear scan register allocation and is a comparatively new algorithm.
● 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.
● Disadvantages:
● Imprecise due to use of live intervals rather than live ranges.
● Other techniques known to be superior in many cases.
![Page 60: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/60.jpg)
An Entirely Different Approach
![Page 61: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/61.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
![Page 62: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/62.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
What can we infer from
all these variables being
live at this point?
![Page 63: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/63.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
![Page 64: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/64.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
![Page 65: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/65.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
![Page 66: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/66.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
![Page 67: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/67.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
![Page 68: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/68.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 69: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/69.jpg)
An Entirely Different Approach{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f{ d }
{ e, f }d = e – f{ d }
{ d }g = d{ g }
a
b c
d
e
f
g
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 70: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/70.jpg)
The Register Interference Graph
● The register interference graph (RIG) of a control-flow graph is an undirected graph where
● Each node is a variable.
● There is an edge between two variables that are live at the same program point.
● Perform register allocation by assigning each variable a different register from all of its neighbors.
● There's just one catch...
![Page 71: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/71.jpg)
The One Catch
● This problem 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 known for this problem.
● We have to be content with a heuristic that is good enough for RIGs that arise in practice.
![Page 72: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/72.jpg)
The One Catch to The One Catch
![Page 73: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/73.jpg)
The One Catch to The One Catch
If you can figure out a way to assign registers to arbitrary RIGs, you've just
proven P = NP and will get a $1,000,000
check from the Clay Mathematics Institute.
![Page 74: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/74.jpg)
The One Catch to The One Catch
If you can figure out a way to assign registers to arbitrary RIGs, you've just
proven P = NP and will get a $1,000,000
check from the Clay Mathematics Institute.
![Page 75: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/75.jpg)
Battling NP-Hardness
![Page 76: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/76.jpg)
Chaitin's Algorithm
● Intuition:
● 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: With fewer than k neighbors, some color must be left over.
● Algorithm:
● Find a node with fewer than k outgoing edges.
● Remove it from the graph.
● Recursively color the rest of the graph.
● Add the node back in.
● Assign it a valid color.
![Page 77: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/77.jpg)
Chaitin's Algorithm
![Page 78: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/78.jpg)
Chaitin's Algorithm
a
b c
d e
g f
![Page 79: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/79.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 80: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/80.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 81: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/81.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
![Page 82: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/82.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
![Page 83: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/83.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
![Page 84: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/84.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
![Page 85: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/85.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
![Page 86: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/86.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
![Page 87: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/87.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
![Page 88: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/88.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
![Page 89: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/89.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
![Page 90: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/90.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
![Page 91: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/91.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
g
![Page 92: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/92.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
g
f
![Page 93: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/93.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
g
![Page 94: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/94.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
g
![Page 95: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/95.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
![Page 96: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/96.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
a
![Page 97: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/97.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
![Page 98: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/98.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
e
![Page 99: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/99.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
![Page 100: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/100.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
d
![Page 101: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/101.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
![Page 102: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/102.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
b
![Page 103: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/103.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
![Page 104: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/104.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
c
![Page 105: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/105.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 106: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/106.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0
R1
R2
R0
R1
R2
Registers
R3
![Page 107: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/107.jpg)
One Problem
● What if we can't find a node with fewer than k neighbors?
● 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.
![Page 108: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/108.jpg)
Chaitin's Algorithm Reloaded
a
b c
d
e
f
g
R0
R1
R2
R0
R1
R2
Registers
![Page 109: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/109.jpg)
Chaitin's Algorithm Reloaded
a
b c
d
e
f
R0
R1
R2
R0
R1
R2
Registers
g
g
![Page 110: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/110.jpg)
f
Chaitin's Algorithm Reloaded
a
b c
d
e
R0
R1
R2
R0
R1
R2
Registers
g
g f
![Page 111: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/111.jpg)
e
f
Chaitin's Algorithm Reloaded
a
b c
d
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
![Page 112: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/112.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b c
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
![Page 113: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/113.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
![Page 114: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/114.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
b
![Page 115: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/115.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
b
a
![Page 116: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/116.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
b
![Page 117: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/117.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
b
![Page 118: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/118.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
![Page 119: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/119.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
![Page 120: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/120.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
![Page 121: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/121.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
![Page 122: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/122.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
c
![Page 123: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/123.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
c
![Page 124: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/124.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
e
d
c
(spilled)
![Page 125: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/125.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
d
c
e
(spilled)
![Page 126: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/126.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g f
d
c
e
(spilled)
![Page 127: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/127.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g
d
c
e
(spilled)
![Page 128: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/128.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
g
d
c
e
(spilled)
![Page 129: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/129.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
d
c
e
(spilled)
![Page 130: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/130.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0
R1
R2
R0
R1
R2
Registers
g
d
c
e
(spilled)
![Page 131: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/131.jpg)
Another Example
![Page 132: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/132.jpg)
Another Example
a
b c
d
ef
![Page 133: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/133.jpg)
Another Example
a
b c
d
ef
R0
R1
R2
R0
R1
R2
Registers
![Page 134: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/134.jpg)
a
Another Example
b c
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
![Page 135: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/135.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
![Page 136: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/136.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
![Page 137: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/137.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 138: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/138.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
![Page 139: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/139.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
d
![Page 140: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/140.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
![Page 141: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/141.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
![Page 142: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/142.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 143: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/143.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 144: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/144.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
![Page 145: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/145.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
![Page 146: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/146.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
![Page 147: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/147.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
![Page 148: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/148.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
b
![Page 149: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/149.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
b
![Page 150: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/150.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
b
![Page 151: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/151.jpg)
c
a
Another Example
d
ef
R0
R1
R2
R0
R1
R2
Registers
b
![Page 152: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/152.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
d
![Page 153: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/153.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
![Page 154: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/154.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
f
![Page 155: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/155.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 156: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/156.jpg)
c
a
Another Example
b
d
ef
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 157: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/157.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 158: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/158.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
a
c
b
e
![Page 159: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/159.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
a
ce
![Page 160: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/160.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
a
ce
![Page 161: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/161.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
ae
![Page 162: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/162.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
ae
(spilled)
![Page 163: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/163.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
e
(spilled)
![Page 164: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/164.jpg)
c
a
Another Example
b
d
f
R0
R1
R2
R0
R1
R2
Registers
e
(spilled)
(spilled)
![Page 165: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/165.jpg)
Chaitin's Algorithm
● Advantages:
● For many control-flow graphs, finds an excellent assignment of variables to registers.
● When distinguishing variables by use, produces a precise RIG.
● Often used in production compilers like GCC.
● Disadvantages:
● Core approach based on the NP-hard graph coloring problem.
● Heuristic may produce pathologically worst-case assignments.
![Page 166: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/166.jpg)
Improvements to the Algorithm
● Choose what to spill intelligently.
● Use heuristics (least-commonly used, greatest improvement, etc.) to determine what to spill.
● Handle spilling intelligently.
● When spilling a variable, recompute the RIG based on the spill and use a new coloring to find a register.
![Page 167: Register Allocation - Electrical Engineering and Computer ... · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number](https://reader033.vdocuments.mx/reader033/viewer/2022042710/5f6c1031d006037bcd04eeea/html5/thumbnails/167.jpg)
Summary of Register Allocation
● Critical step in all optimizing compilers.
● The linear scan algorithm uses live
intervals to greedily assign variables to registers.
● Often used in JIT compilers due to efficiency.
● Chaitin's algorithm uses the register
interference graph (based on live ranges) and graph coloring to assign registers.
● The basis for the technique used in GCC.