Top Banner
Register Usage • Keep as many values in registers as possible • Register assignment • Register allocation • Popular techniques – Local vs. global – Graph coloring – Bin packing
23

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

Dec 14, 2015

Download

Documents

Aubrey Burdon
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Register Usage

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

Page 2: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Local Register Assignment

• Given – Control-flow graph of basic blocks– List of 3-addr statements per BB– Set of “live” scalar values per stmt– Sets of scalar values used, defined per stmt

• Design a local register assignment/allocation algorithm

Page 3: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Graph Coloring

• Assign a color to each node in graph• Two nodes connected by an edge must have

different colors• Classic problem in graph theory• NP complete– But good heuristics exist for register allocation

Page 4: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

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 Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Graph Coloring Register Assign• Each value is allocated a (symbolic) register• “Variables” interfere iff live ranges overlap• Two interfering values cannot share register• How can we tell if two values interfere?

s1 s2

s3 s4

Page 6: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Interference Graph

• Values and interference– Nodes are the values– Edge between two nodes iff they interfere

s1 s2

s3 s4

Page 7: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Graph Coloring Example

Page 8: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Graph Coloring Example

• 3 Colors

Page 9: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Heuristics for Register Coloring

• Coloring a graph with N colors• For each node, m – If degree(m) < N • Node can always be colored, because• After coloring adjacent nodes, at least one color left for

current node

– If degree(m) >= N• Still may be colorable with N colors

Page 10: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Heuristics for Register Coloring

• Remove nodes that have degree < N– Push the removed nodes onto a stack

• When all the nodes have degree >= N – Find a node to spill (no color for that node)– Remove that node

• When graph empty, start to color– Pop a node from stack back– Color node different from adjacent (colored) nodes

Page 11: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 12: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 13: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 14: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 15: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 16: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 17: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 18: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1

s3 s4

s0

N = 3

s4s3

s2

Page 19: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1

s3 s4

s0

N = 3

s4

s2

Page 20: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1

s3 s4

s0

N = 3

s2

Page 21: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Another Coloring Example

s1

s3 s4

s0

N = 3

s2

Page 22: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

Which value to pick?

• One with interference degree >= N• One with minimal spill cost (cost of placing

value in memory rather than in register)• What is spill cost? – Cost of extra load and store instructions

Page 23: Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.

One Way to Compute Spill Cost

• Goal: give priority to values used in loops• So assume loops execute 10 times• Spill cost = defCost + useCost• defCost = sum over all definitions of cost of a

store times 10nestingDepthOfLoop

• useCost = sum over all uses of cost of a load times 10nestingDepthOfLoop

• Choose the value with the lowest spill cost