register allocation - stanford university€¦ · register allocation in tac, there are an...
TRANSCRIPT
![Page 1: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/1.jpg)
Register Allocation
![Page 2: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/2.jpg)
Announcements
● Programming Project 4 due Saturday, August 18 at 11:30AM● OH all this week.● Ask questions via email!● Ask questions via Piazza!● No late submissions.
![Page 3: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/3.jpg)
Please evaluate this course on Axess.
Your feedback really makes a difference.
![Page 4: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/4.jpg)
Where We Are
Lexical Analysis
IR Generation
Syntax Analysis
Semantic Analysis
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 5: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/5.jpg)
Where We Are
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
Fan-TAC-stic!
Lexical Analysis
Syntax Analysis
IR Generation
Semantic Analysis
Fan-TAC-stic!
![Page 6: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/6.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Optimization
IR Generation
Code Generation
Optimization
SourceCode
MachineCode
![Page 7: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/7.jpg)
Code Generation at a Glance
● At this point, we have optimized IR code that needs to be converted into the target language (e.g. assembly, machine code).
● Goal of this stage:● Choose the appropriate machine instructions for each IR
instruction.● Divvy up finite machine resources (registers, caches,
etc.)● Implement low-level details of the runtime environment.
● Machine-specific optimizations are often done here, though some are treated as part of a final optimization phase.
![Page 8: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/8.jpg)
Overview
● Register Allocation (Today)● How to assign variables to finitely many
registers?● What to do when it can't be done?● How to do so efficienty?
● Garbage Collection (Monday)● How to detect reclaimable memory?● How to reclaim memory efficiently?
![Page 9: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/9.jpg)
Memory Tradeoffs
● There is an enormous tradeoff between speed and size in memory.
● SRAM is fast but very expensive:● Can keep up with processor speeds in the GHz.● As of 2007, cost is $10/MB● Good luck buying 1TB of the stuff!
● Hard disks are cheap but very slow:● As of 2012, you can buy a 2TB hard drive for about $100● As of 2012, good disk seek times are measured in ms
(about two to four million times slower than a processor cycle!)
![Page 10: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/10.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
![Page 11: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/11.jpg)
The Memory Hierarchy
● Idea: Try to get the best of all worlds by using multiple types of memory.
Registers
L1 Cache
L2 Cache
Main Memory
Hard Disk
Network
![Page 12: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/12.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
Registers
![Page 13: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/13.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 14: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/14.jpg)
The Challenges 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.
● (Interesting exception: Stanford's Sequoia programming language)
● Challenges in code generation:● Position objects in a way that takes maximum
advantage of the memory hierarchy.● Do so without hints from the programmer.
![Page 15: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/15.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 16: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/16.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 17: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/17.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 18: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/18.jpg)
Goals for Today
● Introduce register allocation for a MIPS-style machine:● Some number of indivisible, general-purpose
registers.
● Explore three algorithms for register allocation:● Naïve (“no”) register allocation.● Linear scan register allocation.● Graph-coloring register allocation.
![Page 19: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/19.jpg)
An Initial Register Allocator
● Idea: Store every value in main memory, loading values only when they're needed.
● To generate a code that performs a computation:● Generate load instructions to pull the values
from main memory into registers.● Generate code to perform the computation
on the registers.● Generate store instructions to store the
result back into main memory.
![Page 20: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/20.jpg)
Our Register Allocator In Action
![Page 21: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/21.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
![Page 22: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/22.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
![Page 23: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/23.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
![Page 24: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/24.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)
![Page 25: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/25.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)
![Page 26: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/26.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1
![Page 27: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/27.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
![Page 28: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/28.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
![Page 29: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/29.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)
![Page 30: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/30.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
![Page 31: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/31.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
![Page 32: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/32.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)
![Page 33: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/33.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)
![Page 34: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/34.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)add $t2, $t0, $t1
![Page 35: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/35.jpg)
Our Register Allocator In Action
a = b + c;d = a;c = a + d;
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)add $t2, $t0, $t1sw $t2, -16(fp)
Param N
...
Param 1
Stored fp
Stored ra
a
b
c
d
fp + 0
fp + 4N
...
fp + 4
fp - 4
fp - 8
fp - 12
fp - 16
fp - 20
![Page 36: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/36.jpg)
Analysis of our Allocator● Disadvantage: Gross inefficiency.
● Issues unnecessary loads and stores by the dozen.● Wastes space on values that could be stored purely in registers.● Easily an order of magnitude or two slower than necessary.● Unacceptable in any production compiler.
● Advantage: Simplicity.● Can translate each piece of IR directly to assembly as we go.● Never need to worry about running out of registers.● Never need to worry about function calls or special-purpose
registers.● Good if you just needed to get a prototype compiler up and
running.
![Page 37: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/37.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 38: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/38.jpg)
Register Consistency
a = b + c
d = a d = b
e = d
![Page 39: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/39.jpg)
Register Consistency
● At each program point, each variable must be in the same location.● Does not mean that each variable is always
stored in the same location!
● At each program point, each register holds at most one live variable.● Can assign several variables the same
register if no two of them ever will be read together.
![Page 40: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/40.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 41: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/41.jpg)
Live Ranges and Live Intervals
![Page 42: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/42.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 43: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/43.jpg)
Live Ranges and Live Intervalse = 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 44: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/44.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
d = e + f d = e – f
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 45: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/45.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
d = e + f d = e – f
{ 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 46: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/46.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
d = e + f{ d }
d = e – f
{ 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 47: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/47.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
{ e, f }d = e + f{ d }
d = e – f
{ 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 48: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/48.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
{ e, f }d = e + f{ d }
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 49: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/49.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
f = f + b
{ 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 50: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/50.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
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 51: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/51.jpg)
Live Ranges and Live Intervalse = d + a
f = b + c
{ 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 52: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/52.jpg)
Live Ranges and Live Intervalse = d + a
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 53: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/53.jpg)
Live Ranges and Live Intervalse = d + a
{ 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 54: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/54.jpg)
Live Ranges and Live Intervalse = 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 55: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/55.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 56: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/56.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 57: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/57.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 58: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/58.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 59: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/59.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 60: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/60.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 61: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/61.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 62: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/62.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 63: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/63.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 64: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/64.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 65: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/65.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 66: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/66.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
![Page 67: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/67.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2R0 R1 R2
Free RegistersR2R3
![Page 68: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/68.jpg)
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2R0 R1 R2
Free RegistersR2R3
![Page 69: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/69.jpg)
R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2R1 R2
Free RegistersR2R3
![Page 70: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/70.jpg)
R1R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2R2
Free RegistersR2R3
![Page 71: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/71.jpg)
R2R1R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2
Free RegistersR2R3
![Page 72: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/72.jpg)
R3R2R1R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R0 R1 R2
Free RegistersR2
![Page 73: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/73.jpg)
R0 R3R2R1R0
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1 R2
Free RegistersR2
![Page 74: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/74.jpg)
R0R0 R3R2R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1 R2
Free RegistersR2
![Page 75: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/75.jpg)
R2R0R0 R3R2R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
Free RegistersR2
![Page 76: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/76.jpg)
R2R2R0R0 R3R1
Register Allocation with Live Intervalsa bcbb bebd bgbf
R1
Free RegistersR2
![Page 77: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/77.jpg)
R2R0 R1 R2R2R0 R3R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 78: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/78.jpg)
R0 R2R0 R1 R2R2 R3R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 79: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/79.jpg)
R0 R2R0 R1 R2R2 R3R1
Register Allocation with Live Intervalsa bcbb bebd bgbf Free Registers
![Page 80: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/80.jpg)
Another Example
![Page 81: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/81.jpg)
Another Examplebbba bgbe bfc bd
![Page 82: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/82.jpg)
Another Example
R0 R1 R2
bbba bgbe bfc bdR0 R1 R2
Free Registers
![Page 83: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/83.jpg)
R0R0
Another Examplebbba bgbe bfc bd
R1 R2R1 R2
Free Registers
![Page 84: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/84.jpg)
R0R0
Another Examplebbba bgbe bfc bd
R1 R2R1 R2
Free Registers
![Page 85: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/85.jpg)
R1R0R0
Another Examplebbba bgbe bfc bd
R1 R2R2
Free Registers
![Page 86: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/86.jpg)
R2R1R0R0
Another Examplebbba bgbe bfc bd
R1 R2
Free Registers
![Page 87: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/87.jpg)
R2R1R0R0
Another Examplebbba bgbe bfc bd
R1 R2
Free Registers
What do we do now?
![Page 88: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/88.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 89: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/89.jpg)
R2R1R0R0
Another Examplebbba bgbe bfc bd
R1 R2
Free Registers
What do we do now?
![Page 90: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/90.jpg)
R0 R2R1R0
Another Examplebbba bgbe bfc bd
R1 R2
Free Registers
![Page 91: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/91.jpg)
R0R0 R2R1
Another Examplebbba bgbe bfc bd
R1 R2
Free Registers
![Page 92: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/92.jpg)
R2R0R0 R2R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 93: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/93.jpg)
R2R2R0R0 R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 94: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/94.jpg)
R0 R2R2R0 R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 95: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/95.jpg)
R0R0 R2R2R1
Another Examplebbba bgbe bfc bd
R1
Free Registers
![Page 96: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/96.jpg)
R1R0R0 R2R2R1
Another Examplebbba bgbe bfc bd Free Registers
![Page 97: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/97.jpg)
R1R1R0R0 R2R2
Another Examplebbba bgbe bfc bd Free Registers
![Page 98: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/98.jpg)
R1R1R0R0 R2R2
Another Examplebbba bgbe bfc bd Free Registers
![Page 99: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/99.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 100: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/100.jpg)
Correctness Proof Sketch
● No register holds two live variables at once:● Live intervals are conservative
approximations of live ranges.● No two variables with overlapping live
ranges placed in the same register.
● At each program point, every variable is in the same location:● All variables assigned a unique location.
![Page 101: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/101.jpg)
Second-Chance Bin Packing
● A more aggressive version of linear-scan.● Uses live ranges instead of live intervals.● If a variable must be spilled, don't spill all uses
of it.● A later live range might still fit into a register.
● Requires a final data-flow analysis to confirm variables are assigned consistent locations.
● See “Quality and Speed in Linear-scan Register Allocation” by Traub, Holloway, and Smith.
![Page 102: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/102.jpg)
Second-Chance Bin Packingbbba bgbe bfc bd
R0R0 R1 R2R1 R2
Free Registers
![Page 103: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/103.jpg)
Second-Chance Bin Packingbbba bgbe bfc bd
R0R0 R1 R2R1 R2
Free Registers
![Page 104: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/104.jpg)
R0
Second-Chance Bin Packingbbba bgbe bfc bd
R0 R1 R2R1 R2
Free Registers
![Page 105: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/105.jpg)
R1R0
Second-Chance Bin Packingbbba bgbe bfc bd
R0 R1 R2R2
Free Registers
![Page 106: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/106.jpg)
R2R1R0
Second-Chance Bin Packingbbba bgbe bfc bd
R0 R1 R2
Free Registers
![Page 107: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/107.jpg)
R2R1R0
Second-Chance Bin Packingbbba bgbe bfc bd
R0 R1 R2
Free Registers
![Page 108: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/108.jpg)
R0 R2R1R0
Second-Chance Bin Packingbbba bgbe bfc bd
R1 R2
Free Registers
![Page 109: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/109.jpg)
R0R0 R2R1
Second-Chance Bin Packingbbba bgbe bfc bd
R1 R2
Free Registers
![Page 110: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/110.jpg)
R2R1R1R0R0 R2
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 111: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/111.jpg)
R2R2R1R1R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 112: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/112.jpg)
R1 R2R2R1R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 113: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/113.jpg)
R1R1 R2R2R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 114: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/114.jpg)
R1R1 R2R2R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 115: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/115.jpg)
R1R1 R2R2R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 116: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/116.jpg)
R1R1 R2R2R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 117: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/117.jpg)
R1R1 R2R2R0R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 118: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/118.jpg)
R0 R1R1 R2R2R0
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 119: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/119.jpg)
R0R0 R1R1 R2R2
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 120: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/120.jpg)
R0R0 R1R1 R2R2
Second-Chance Bin Packingbbba bgbe bfc bd Free Registers
![Page 121: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/121.jpg)
An Entirely Different Approach
![Page 122: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/122.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 123: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/123.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 124: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/124.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 125: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/125.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 126: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/126.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 127: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/127.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 128: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/128.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 129: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/129.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 R2R0 R1 R2
RegistersR3
![Page 130: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/130.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 R2R0 R1 R2
RegistersR3
![Page 131: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/131.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 132: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/132.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 133: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/133.jpg)
The One Catch to The One Catch
![Page 134: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/134.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 135: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/135.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 136: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/136.jpg)
Battling NP-Hardness
![Page 137: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/137.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 138: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/138.jpg)
Chaitin's Algorithm
![Page 139: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/139.jpg)
Chaitin's Algorithm
a
b c
d e
g f
![Page 140: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/140.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
![Page 141: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/141.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
![Page 142: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/142.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
c
![Page 143: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/143.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
c
![Page 144: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/144.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
c
![Page 145: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/145.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cb
![Page 146: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/146.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cb
![Page 147: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/147.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cb
![Page 148: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/148.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbd
![Page 149: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/149.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbd
![Page 150: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/150.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
![Page 151: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/151.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
![Page 152: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/152.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
g
![Page 153: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/153.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
g
f
![Page 154: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/154.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
g
![Page 155: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/155.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
g
![Page 156: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/156.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
![Page 157: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/157.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
a
![Page 158: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/158.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
![Page 159: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/159.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbde
![Page 160: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/160.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbd
![Page 161: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/161.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cbd
![Page 162: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/162.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cb
![Page 163: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/163.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
cb
![Page 164: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/164.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
c
![Page 165: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/165.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
c
![Page 166: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/166.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
![Page 167: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/167.jpg)
Chaitin's Algorithm
a
b c
d e
g f
R0 R1 R2R0 R1 R2
RegistersR3
![Page 168: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/168.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 169: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/169.jpg)
Chaitin's Algorithm Reloaded
a
b c
d
e
f
g
R0 R1 R2R0 R1 R2
Registers
![Page 170: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/170.jpg)
Chaitin's Algorithm Reloaded
a
b c
d
e
f
R0 R1 R2R0 R1 R2
Registers
gg
![Page 171: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/171.jpg)
f
Chaitin's Algorithm Reloaded
a
b c
d
e
R0 R1 R2R0 R1 R2
Registers
gg f
![Page 172: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/172.jpg)
e
f
Chaitin's Algorithm Reloaded
a
b c
d
R0 R1 R2R0 R1 R2
Registers
gg f
e
![Page 173: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/173.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b c
R0 R1 R2R0 R1 R2
Registers
gg f
ed
![Page 174: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/174.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edc
![Page 175: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/175.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edcb
![Page 176: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/176.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edcba
![Page 177: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/177.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edcb
![Page 178: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/178.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edcb
![Page 179: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/179.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edc
![Page 180: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/180.jpg)
c
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
edc
![Page 181: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/181.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
ed
c
![Page 182: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/182.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
ed
c
![Page 183: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/183.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
e
c
![Page 184: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/184.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
e
c
![Page 185: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/185.jpg)
d
e
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
e
d
c
(spilled)
![Page 186: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/186.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
d
c
e
(spilled)
![Page 187: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/187.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg f
d
c
e
(spilled)
![Page 188: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/188.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg
d
c
e
(spilled)
![Page 189: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/189.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
gg
d
c
e
(spilled)
![Page 190: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/190.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
g
d
c
e
(spilled)
![Page 191: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/191.jpg)
d
f
Chaitin's Algorithm Reloaded
a
b
R0 R1 R2R0 R1 R2
Registers
g
d
c
e
(spilled)
![Page 192: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/192.jpg)
A Smarter Algorithm{ 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 }
R0 R1 R2R0 R1 R2
Registers
d
f
a
b
g
d
c
(spilled)
e
![Page 193: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/193.jpg)
A Smarter Algorithm{ 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 }
R0 R1 R2R0 R1 R2
Registers
d
f
a
b
g
d
c
(spilled)
e
![Page 194: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/194.jpg)
A Smarter Algorithm{ 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 }
R0 R1 R2R0 R1 R2
Registers
d
f
a
b
g
d
c
(spilled)
e
![Page 195: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/195.jpg)
A Smarter Algorithm{ 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 }
R0 R1 R2R0 R1 R2
Registers
d
e
a
b
g
d
c
d'
f
dd (spilled)
![Page 196: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/196.jpg)
Another Example
![Page 197: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/197.jpg)
Another Example
a
b c
d
ef
![Page 198: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/198.jpg)
Another Example
a
b c
d
ef
R0 R1 R2R0 R1 R2
Registers
![Page 199: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/199.jpg)
a
Another Example
b c
d
ef
R0 R1 R2R0 R1 R2
Registers
a
![Page 200: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/200.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
ac
![Page 201: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/201.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
![Page 202: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/202.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 203: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/203.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
![Page 204: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/204.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
d
![Page 205: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/205.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
![Page 206: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/206.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
![Page 207: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/207.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 208: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/208.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 209: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/209.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
![Page 210: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/210.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
![Page 211: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/211.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
ac
b
![Page 212: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/212.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
ac
b
![Page 213: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/213.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
a
b
![Page 214: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/214.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
a
b
![Page 215: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/215.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
b
![Page 216: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/216.jpg)
c
a
Another Example
d
ef
R0 R1 R2R0 R1 R2
Registers
b
![Page 217: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/217.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
d
![Page 218: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/218.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
![Page 219: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/219.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
f
![Page 220: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/220.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 221: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/221.jpg)
c
a
Another Example
b
d
ef
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 222: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/222.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 223: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/223.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
acb
e
![Page 224: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/224.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
ac
e
![Page 225: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/225.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
ac
e
![Page 226: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/226.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
ae
![Page 227: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/227.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
ae
(spilled)
![Page 228: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/228.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
e
(spilled)
![Page 229: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/229.jpg)
c
a
Another Example
b
d
f
R0 R1 R2R0 R1 R2
Registers
e
(spilled)
(spilled)
![Page 230: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/230.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 231: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/231.jpg)
Correctness Proof Sketch
● No two variables live at some point are assigned the same register.● Forced by graph coloring.
● At any program point each variable is always in one location.● Automatic if we assign each variable one
register.● Requires a few tricks if we separate by use
case.
![Page 232: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/232.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 233: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/233.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.
![Page 234: Register Allocation - Stanford University€¦ · Register Allocation In TAC, there are an unlimited number of variables. On a physical machine there are a small number of registers:](https://reader030.vdocument.in/reader030/viewer/2022041001/5ea14e55f2b04f54621272fa/html5/thumbnails/234.jpg)
Next Time
● Garbage Collection● Reference Counting● Mark-and-Sweep● Stop-and-Copy● Incremental Collectors