Register Usage Keep as many values in registers as possible Keep as many values in registers as...

25
Register Usage Register Usage Keep as many values in Keep as many values in registers as possible registers as possible Register assignment Register assignment Register allocation Register allocation Popular techniques Popular techniques Local vs. global Local vs. global Graph coloring Graph coloring Bin packing Bin packing

description

Graph Coloring Assign a color to each node in graph Assign a color to each node in graph Two nodes connected by an edge must have different colors Two nodes connected by an edge must have different colors Classic problem in graph theory Classic problem in graph theory NP complete NP complete But good heuristics exist for register allocation But good heuristics exist for register allocation

Transcript of Register Usage Keep as many values in registers as possible Keep as many values in registers as...

Page 1: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 2: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 3: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 4: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Live Ranges Live Ranges def y

def xuse y

def xdef y

use xdef x

use x

use xuse y

Page 5: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 6: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 7: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Graph Coloring ExampleGraph Coloring Example

Page 8: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Graph Coloring ExampleGraph Coloring Example

• 3 Colors

Page 9: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 10: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 11: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 12: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 13: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 14: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 15: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 16: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 17: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 18: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 19: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 20: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 21: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 22: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 23: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Another Coloring ExampleAnother Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 24: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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

Page 25: Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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