Date post: | 17-Jan-2018 |
Category: |
Documents |
Upload: | hortense-mosley |
View: | 222 times |
Download: | 0 times |
Register UsageRegister Usage Keep as many values in registers as Keep as many values in registers as
possiblepossible Register assignmentRegister assignment Register allocationRegister allocation Popular techniquesPopular techniques
Local vs. globalLocal vs. global Graph coloringGraph coloring Bin packingBin packing
Local Register AssignmentLocal Register Assignment Given Given
Control-flow graph of basic blocksControl-flow graph of basic blocks List of 3-addr statements per BBList of 3-addr statements per BB Set of “live” scalar values per stmtSet of “live” scalar values per stmt Sets of scalar values used, defined per Sets of scalar values used, defined per
stmtstmt
Design a local register Design a local register assignment/allocation algorithmassignment/allocation algorithm
Graph ColoringGraph Coloring Assign a color to each node in graphAssign a color to each node in graph Two nodes connected by an edge Two nodes connected by an edge
must have different colorsmust have different colors Classic problem in graph theoryClassic problem in graph theory NP completeNP complete
But good heuristics exist for register But good heuristics exist for register allocationallocation
Live Ranges Live Ranges def y
def xuse y
def xdef y
use xdef x
use x
use xuse y
Graph Coloring Register Graph Coloring Register AssignAssign
Each value is allocated a (symbolic) Each value is allocated a (symbolic) registerregister
““Variables” Variables” interfereinterfere iff live ranges iff live ranges overlapoverlap
Two interfering values cannot share Two interfering values cannot share registerregister
How can we tell if two values interfere?How can we tell if two values interfere?s1 s2
s3 s4
Interference GraphInterference Graph Values and interferenceValues and interference
Nodes are the valuesNodes are the values Edge between two nodes iff they Edge between two nodes iff they
interfereinterfere
s1 s2
s3 s4
Graph Coloring ExampleGraph Coloring Example
Graph Coloring ExampleGraph Coloring Example
• 3 Colors
Heuristics for Register Heuristics for Register ColoringColoring
Coloring a graph with N colorsColoring a graph with N colors For each node, m For each node, m
If degree(m) < N If degree(m) < N Node can always be colored, becauseNode can always be colored, because After coloring adjacent nodes, at least one After coloring adjacent nodes, at least one
color left for current nodecolor left for current node If degree(m) >= NIf degree(m) >= N
Still may be colorable with N colorsStill may be colorable with N colors
Heuristics for Register Heuristics for Register ColoringColoring
Remove nodes that have degree < NRemove nodes that have degree < N Push the removed nodes onto a stackPush the removed nodes onto a stack
When all the nodes have degree >= N When all the nodes have degree >= N Find a node to spill (no color for that node)Find a node to spill (no color for that node) Remove that nodeRemove that node
When graph empty, start to colorWhen graph empty, start to color Pop a node from stack backPop a node from stack back Color node different from adjacent (colored) Color node different from adjacent (colored)
nodesnodes
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3s2
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3s2
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3s2
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4s3
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
s4
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
Another Coloring ExampleAnother Coloring Example
s1 s2
s3 s4
s0
N = 3
Which value to pick?Which value to pick? One with interference degree >= NOne with interference degree >= N One with minimal One with minimal spill cost spill cost (cost of (cost of
placing value in memory rather than placing value in memory rather than in register)in register)
What is spill cost? What is spill cost? Cost of extra load and store instructionsCost of extra load and store instructions
One Way to Compute Spill One Way to Compute Spill CostCost
Goal: give priority to values used in loopsGoal: give priority to values used in loops So assume loops execute 10 timesSo assume loops execute 10 times Spill cost = defCost + useCostSpill cost = defCost + useCost defCost = sum over all definitions of cost defCost = sum over all definitions of cost
of a store times 10of a store times 10nestingDepthOfLoopnestingDepthOfLoop
useCost = sum over all uses of cost of a useCost = sum over all uses of cost of a load times 10load times 10nestingDepthOfLoopnestingDepthOfLoop
Choose the value with the lowest spill costChoose the value with the lowest spill cost