SlideShare a Scribd company logo
1 of 39
Yin Wang
“See that bird? It's a Spencer's warbler.Well, in
Italian, it's a Chutto Lapittida. In Portuguese, it's
a Bom da Peida. In Chinese it's a Chung-Long-
tah, and in Japanese it's a KatanoTekeda.You
can know the name of the bird in all the
languages of the world, but when you're
finished, you'll know absolutely nothing
whatever about the bird.You'll only know about
humans in different places, and what they call
the bird. So let's look at the birds and see what
it's doing -- that's what counts!”
-- Richard Feynman, The Making of a Scientist
Let’s look at the register allocator and see what it’s doing …
 Why do we need
names in programs?
 Because we want to
refer to values.
 Why do processors
have registers?
 Because its circuits
need to refer to …
values.
 So, are registers
analogous to names?
 Yes.They are of the
same essence, holders
of values.
 Can we use registers to
stand for names?
 Yes. Because the
values of names can
also live in registers.
 Can ALL names live in
registers?
 Not necessarily.
Processors have very few
registers, because they
are expensive.
 Can I say that names
can live in registers?
 You may say so,
although it is their
values that are living.
 Can processors do
arithmetic on the
stack?
 No.They have to load
the operands into
registers first.
 Where do the rest of
the names live?
 The stack.
 What is the stack?  A large and cheap off-
processor space where
names can live.
The Life of Names
A name lives either in an
expensive register or a cheap
stack slot.
 What is register
allocation?
 The process of
deciding where and
when each name lives.
 Does it take a long time
to move value between
register and stack?
 Yes. It is a major cost
of time.
 What should be the
goal of register
allocation then?
 To minimize register-
stack traffic.
 What is a model?  It is a table which tells
you where each name
lives.
 Can you show me an
example model?
 There you are!
x : r1
y : r2
z : r3
u : fv0
v : fv1
 Does it keep track of
register and stack
separately?
 Yes, for maximum
accuracy.
u v
sp
y zx
r1 r2 r3 Stack
x : r1
y : r2
z : r3
u : fv0
v : fv1
model
physical machine
r4 r5 r6
 How do models relate
to each other?
 Models relate to each
other according to some
inference rules, similar
to that of Hoare Logic.
w := v * 5
w : r1
y : r2
v : r3
v : fv0
x : fv1
x : r1
y : r2
v : fv0“premodel”
“postmodel”
as Hoare-triple-style notation:
w := 1
w : r1
y : r2
y : r2“premodel”
“postmodel”
 When are names
created?
 When they are
assigned a value.
 Will names ever die?  Yes.When we no
longer refer to them.
 Where does x die?  After x is added with 1,
but before assigning to
y. I mean, here:
y := x + 1
x := 1
y := x + 1
z := y * 2
END
 How do we signify
where a variable dies?
 We mark their endings,
like:
y := x + 1, {x}
z := x + 1, {x}
z : r1
y : r2
v : fv0
x : r1
y : r2
v : fv0
x : fv1
“premodel”
“postmodel”
 What happens to a model
when a name dies?
 It is deleted from the
model (for both register
and stack parts).
But actually, the deletion
happens here!
z := x + 1, {x}
z : r1
y : r2
v : fv0
x : r1
y : r2
v : fv0
x : fv1
y : r2 v : fv0
premodel
midmodel
postmodel
z :=
x + 1, {x}
model transformer
for “z := x + 1”
 Why is there a
midmodel?
 Because the instruction
must takes at lest two
cycles to finish. x is already
dead in the second cycle.
+
x
1
z
clock signal
Time
x alivex dead
This is why z can
reuse the register
of x.
z := x + 1, {x}
 UIL (unified intermediate
language) after removing
complex operands
(remove-complex-opera*)
 Procedure definitions
 Sequencing
 Arithmetic
 Assignments
 Memory references
 Branching
 Calls (with trivial arguments)
x := 1
y := x + 1
z := y * 2
END
x := 1, {}
y := x + 1, {x}
z := y * 2, {y}
END, {z}
Backward scan
for marking
deaths of names
Forward model
transformation
and rewriting
r1 := 1
r1 := r1 + 1
r1 := r1 * 2
END
impose-calling-conventions
uncover-frame-conflict
pre-assign-frame
assign-new-frame
finalize-frame-locations
uncover-register-conflict
assign-registers
assign-frame
finalize-locations
mark-deaths
allocate
 Nine passes of nanopass compiler reduced to two.
 What is the model at
the beginning of a
procedure?
 It is pre-determined by
calling conventions.
f (x, y, z) =
u := x + 1
v := u * 2
z / v
$ret : r0
x : r1
y : r2
z : fv0
Suppose r1, r2 are parameter registers, r0 is
return address register. z has to be put into
stack.
 What is $ret in the
initial model?
 It is the return address
set by the caller.
 What is the essence of
it?
 It is the continuation “k” in a
CPSed functional program:
f x y z k =
k (z / ((x + 1) * 2))
 Save
 Load
 Shuffle
Regs
SAVE
Stack
LOAD
SHUFFLE
sp
 Transform the input model into new ones
 May emit instructions for loads and stores
x : r1
y : r2
z : fv0
SAVE({x, y})
x : fv1
y : fv2
z : fv0
x : r1
y : r2
u : r3
z : fv0
LOAD({x, z})
x : r1
y : r2
z : r3
z : fv0
u : fv1
fv1 <- x
fv2 <- y
fv1 <- u
r3 <- z
 What is the essence of
the inserted instructions
from SAVE and LOAD?
 They are actually doing
“live range splitting”
on-the-fly.
 What is the difference
between splitting and
spilling?
 Spilling puts the whole life
span of a name in stack.
Splitting only puts part of
its life span in stack.
 What if different registers are
assigned to the same
variables in different
branches
 We need to shuffle
them for consistency.
if x < 5
y := 1
z : = 2
z := 1
y : = 2
w := y+1
x : r1 x : r1
x : r1
y : r2
z : r3
x : r1
z : r2
y : r3
Need shuffle:
r2 <-> r3
 Shuffling may happen at
 Procedure calls
 Join points
 Shuffling is like permutation
 Break permutation into cycles (or paths):
 Generate code to move variables in cycles:
Mostly graph coloring (extensions) and linear scan (extensions).
 A variable either lives in a register throughout its life
time, or lives in a stack location throughout its life time
 A variable lives in the same register or stack location
throughout its life time
 But actually,
 A variable may live in a register, move to a stack location,
and then move back to a (possibly different) register.
 A variable may live in both a register AND a stack location.
 Graph Coloring doesn’t capture this kind of semantics
and may generate inefficient code.
 Goal: minimizing the number of allocated
registers. (Doesn’t aim at reducing memory
traffic!)
 Produces good code only if no spilling
happens. Otherwise no guarantee about
memory traffic
 Spilled variables may introduce unpredictably
many memory references
 Still need backtracking for “optimal”
allocation because of join points
 Maybe reducible to SAT (thus a graph), but a
much more complex graph (may contain
continuations of the allocator)
 Needs further investigation
 It never spills, but linear scan spills.
 Does “online live range splitting”
 existed partly in extended linear scan
 usually done in a separate pass and thus offline (as
in LLVM option “pre-alloc-split”)
 The model transformer semantics may be
used to simplify formal verification of register
allocation
 Formerly this is done by dynamic “validation”
and not static verification (as inCompCert)
 Handle instructions with memory operands
and other irregularities
 Limited form of backtracking
 Callee-save registers and link-time
optimization
 An industrial-strength compiler backend
eventually
Thanks to R. Kent Dybvig, Andy Keep, Oleg
Kiselyov for helpful comments and discussions.
The Little Register Allocator
The Little Register Allocator

More Related Content

What's hot

COSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in JuliaCOSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in Julia岳華 杜
 
Instruction Combine in LLVM
Instruction Combine in LLVMInstruction Combine in LLVM
Instruction Combine in LLVMWang Hsiangkai
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage CollectionEelco Visser
 
Clojure - an introduction (and some CFML)
Clojure - an introduction (and some CFML)Clojure - an introduction (and some CFML)
Clojure - an introduction (and some CFML)Kai Koenig
 
ARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupMoqod
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making LoopsDavid Evans
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2PVS-Studio
 
DevoxxFR 2016 - 3 degrees of MoM
DevoxxFR 2016 - 3 degrees of MoMDevoxxFR 2016 - 3 degrees of MoM
DevoxxFR 2016 - 3 degrees of MoMGuillaume Arnaud
 
Analyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectAnalyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectPVS-Studio
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 

What's hot (14)

COSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in JuliaCOSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in Julia
 
Instruction Combine in LLVM
Instruction Combine in LLVMInstruction Combine in LLVM
Instruction Combine in LLVM
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Clojure - an introduction (and some CFML)
Clojure - an introduction (and some CFML)Clojure - an introduction (and some CFML)
Clojure - an introduction (and some CFML)
 
Pda
PdaPda
Pda
 
ARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetup
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making Loops
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
DevoxxFR 2016 - 3 degrees of MoM
DevoxxFR 2016 - 3 degrees of MoMDevoxxFR 2016 - 3 degrees of MoM
DevoxxFR 2016 - 3 degrees of MoM
 
Analyzing the Dolphin-emu project
Analyzing the Dolphin-emu projectAnalyzing the Dolphin-emu project
Analyzing the Dolphin-emu project
 
Forloop
ForloopForloop
Forloop
 
Nsq meetup-messaging
Nsq meetup-messagingNsq meetup-messaging
Nsq meetup-messaging
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
F#3.0
F#3.0 F#3.0
F#3.0
 

Similar to The Little Register Allocator

Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospectivechenge2k
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptssuserebb9821
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxssuserebb9821
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talkJohn Stevenson
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environmentsJ'tong Atong
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako, Vasil Remeniuk
 
Clojure made really really simple
Clojure made really really simpleClojure made really really simple
Clojure made really really simpleJohn Stevenson
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHackito Ergo Sum
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for RSeth Falcon
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene BurmakoVasil Remeniuk
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»e-Legion
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrencyAlex Navis
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 

Similar to The Little Register Allocator (20)

Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 
Haskell retrospective
Haskell retrospectiveHaskell retrospective
Haskell retrospective
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.ppt
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptx
 
Clojure made simple - Lightning talk
Clojure made simple - Lightning talkClojure made simple - Lightning talk
Clojure made simple - Lightning talk
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Clojure made really really simple
Clojure made really really simpleClojure made really really simple
Clojure made really really simple
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
 
Pig
PigPig
Pig
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 
scala.reflect, Eugene Burmako
scala.reflect, Eugene Burmakoscala.reflect, Eugene Burmako
scala.reflect, Eugene Burmako
 
Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»Евгений Бурмако «scala.reflect»
Евгений Бурмако «scala.reflect»
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 

Recently uploaded

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 

Recently uploaded (20)

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

The Little Register Allocator

  • 2. “See that bird? It's a Spencer's warbler.Well, in Italian, it's a Chutto Lapittida. In Portuguese, it's a Bom da Peida. In Chinese it's a Chung-Long- tah, and in Japanese it's a KatanoTekeda.You can know the name of the bird in all the languages of the world, but when you're finished, you'll know absolutely nothing whatever about the bird.You'll only know about humans in different places, and what they call the bird. So let's look at the birds and see what it's doing -- that's what counts!” -- Richard Feynman, The Making of a Scientist
  • 3. Let’s look at the register allocator and see what it’s doing …
  • 4.  Why do we need names in programs?  Because we want to refer to values.  Why do processors have registers?  Because its circuits need to refer to … values.  So, are registers analogous to names?  Yes.They are of the same essence, holders of values.
  • 5.  Can we use registers to stand for names?  Yes. Because the values of names can also live in registers.  Can ALL names live in registers?  Not necessarily. Processors have very few registers, because they are expensive.  Can I say that names can live in registers?  You may say so, although it is their values that are living.
  • 6.  Can processors do arithmetic on the stack?  No.They have to load the operands into registers first.  Where do the rest of the names live?  The stack.  What is the stack?  A large and cheap off- processor space where names can live.
  • 7. The Life of Names A name lives either in an expensive register or a cheap stack slot.
  • 8.  What is register allocation?  The process of deciding where and when each name lives.  Does it take a long time to move value between register and stack?  Yes. It is a major cost of time.  What should be the goal of register allocation then?  To minimize register- stack traffic.
  • 9.  What is a model?  It is a table which tells you where each name lives.  Can you show me an example model?  There you are! x : r1 y : r2 z : r3 u : fv0 v : fv1  Does it keep track of register and stack separately?  Yes, for maximum accuracy.
  • 10. u v sp y zx r1 r2 r3 Stack x : r1 y : r2 z : r3 u : fv0 v : fv1 model physical machine r4 r5 r6
  • 11.  How do models relate to each other?  Models relate to each other according to some inference rules, similar to that of Hoare Logic. w := v * 5 w : r1 y : r2 v : r3 v : fv0 x : fv1 x : r1 y : r2 v : fv0“premodel” “postmodel” as Hoare-triple-style notation:
  • 12. w := 1 w : r1 y : r2 y : r2“premodel” “postmodel”  When are names created?  When they are assigned a value.
  • 13.  Will names ever die?  Yes.When we no longer refer to them.  Where does x die?  After x is added with 1, but before assigning to y. I mean, here: y := x + 1 x := 1 y := x + 1 z := y * 2 END  How do we signify where a variable dies?  We mark their endings, like: y := x + 1, {x}
  • 14. z := x + 1, {x} z : r1 y : r2 v : fv0 x : r1 y : r2 v : fv0 x : fv1 “premodel” “postmodel”  What happens to a model when a name dies?  It is deleted from the model (for both register and stack parts). But actually, the deletion happens here!
  • 15. z := x + 1, {x} z : r1 y : r2 v : fv0 x : r1 y : r2 v : fv0 x : fv1 y : r2 v : fv0 premodel midmodel postmodel z := x + 1, {x} model transformer for “z := x + 1”
  • 16.  Why is there a midmodel?  Because the instruction must takes at lest two cycles to finish. x is already dead in the second cycle. + x 1 z clock signal Time x alivex dead This is why z can reuse the register of x. z := x + 1, {x}
  • 17.
  • 18.  UIL (unified intermediate language) after removing complex operands (remove-complex-opera*)  Procedure definitions  Sequencing  Arithmetic  Assignments  Memory references  Branching  Calls (with trivial arguments)
  • 19. x := 1 y := x + 1 z := y * 2 END x := 1, {} y := x + 1, {x} z := y * 2, {y} END, {z} Backward scan for marking deaths of names Forward model transformation and rewriting r1 := 1 r1 := r1 + 1 r1 := r1 * 2 END
  • 21.  What is the model at the beginning of a procedure?  It is pre-determined by calling conventions. f (x, y, z) = u := x + 1 v := u * 2 z / v $ret : r0 x : r1 y : r2 z : fv0 Suppose r1, r2 are parameter registers, r0 is return address register. z has to be put into stack.
  • 22.  What is $ret in the initial model?  It is the return address set by the caller.  What is the essence of it?  It is the continuation “k” in a CPSed functional program: f x y z k = k (z / ((x + 1) * 2))
  • 23.  Save  Load  Shuffle Regs SAVE Stack LOAD SHUFFLE sp  Transform the input model into new ones  May emit instructions for loads and stores
  • 24. x : r1 y : r2 z : fv0 SAVE({x, y}) x : fv1 y : fv2 z : fv0 x : r1 y : r2 u : r3 z : fv0 LOAD({x, z}) x : r1 y : r2 z : r3 z : fv0 u : fv1 fv1 <- x fv2 <- y fv1 <- u r3 <- z
  • 25.  What is the essence of the inserted instructions from SAVE and LOAD?  They are actually doing “live range splitting” on-the-fly.  What is the difference between splitting and spilling?  Spilling puts the whole life span of a name in stack. Splitting only puts part of its life span in stack.
  • 26.  What if different registers are assigned to the same variables in different branches  We need to shuffle them for consistency.
  • 27. if x < 5 y := 1 z : = 2 z := 1 y : = 2 w := y+1 x : r1 x : r1 x : r1 y : r2 z : r3 x : r1 z : r2 y : r3 Need shuffle: r2 <-> r3
  • 28.  Shuffling may happen at  Procedure calls  Join points  Shuffling is like permutation
  • 29.  Break permutation into cycles (or paths):  Generate code to move variables in cycles:
  • 30. Mostly graph coloring (extensions) and linear scan (extensions).
  • 31.  A variable either lives in a register throughout its life time, or lives in a stack location throughout its life time  A variable lives in the same register or stack location throughout its life time  But actually,  A variable may live in a register, move to a stack location, and then move back to a (possibly different) register.  A variable may live in both a register AND a stack location.  Graph Coloring doesn’t capture this kind of semantics and may generate inefficient code.
  • 32.  Goal: minimizing the number of allocated registers. (Doesn’t aim at reducing memory traffic!)  Produces good code only if no spilling happens. Otherwise no guarantee about memory traffic  Spilled variables may introduce unpredictably many memory references
  • 33.  Still need backtracking for “optimal” allocation because of join points  Maybe reducible to SAT (thus a graph), but a much more complex graph (may contain continuations of the allocator)  Needs further investigation
  • 34.  It never spills, but linear scan spills.  Does “online live range splitting”  existed partly in extended linear scan  usually done in a separate pass and thus offline (as in LLVM option “pre-alloc-split”)
  • 35.  The model transformer semantics may be used to simplify formal verification of register allocation  Formerly this is done by dynamic “validation” and not static verification (as inCompCert)
  • 36.  Handle instructions with memory operands and other irregularities  Limited form of backtracking  Callee-save registers and link-time optimization  An industrial-strength compiler backend eventually
  • 37. Thanks to R. Kent Dybvig, Andy Keep, Oleg Kiselyov for helpful comments and discussions.