Register Allocation - Stanford...

50
Wei Li 1 Stanford University CS243 Winter 2006 Register Allocation Register Allocation

Transcript of Register Allocation - Stanford...

Page 1: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

Wei Li 1Stanford University

CS243 Winter 2006

Register Allocation Register Allocation

Page 2: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

2CS243 Winter 2006Stanford University

Register AllocationRegister Allocation

IntroductionIntroductionProblem FormulationProblem FormulationAlgorithmAlgorithm

Page 3: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

3CS243 Winter 2006Stanford University

Register Allocation GoalRegister Allocation Goal

Allocation of variables (pseudoAllocation of variables (pseudo--registers) registers) in a procedure to hardware registersin a procedure to hardware registersDirectly reduces running time by Directly reduces running time by converting memory access to register converting memory access to register accessaccess

WhatWhat’’s memory latency in CPU cycles?s memory latency in CPU cycles?

Page 4: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

4CS243 Winter 2006Stanford University

ExampleExample

a = b + cHow long will the loop take, if a, b, and c are all in memory?

Page 5: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

5CS243 Winter 2006Stanford University

ExampleExample

Ld r1, bLd r2, c

Add r0, r1, r2

St a, r0

a = b + c

Page 6: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

6CS243 Winter 2006Stanford University

ExampleExample

Ld r1, bLd r2, c

Add r0, r1, r2

St a, r0

How long will the loop take?

Page 7: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

7CS243 Winter 2006Stanford University

Revisit SSA ExampleRevisit SSA ExampleMapping aMapping aii to a is like register allocationto a is like register allocation

a1= b1

a1+5

b2=

a1+5

a1= b1

b1+5

b2=

b1+5

a1= b1

b1+5

b2=

a1+5

Page 8: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

8CS243 Winter 2006Stanford University

HighHigh--level Stepslevel Steps

Find an assignment for all pseudoFind an assignment for all pseudo--registers or aliasregisters or alias--free variables.free variables.Assign a hardware register for each Assign a hardware register for each variable.variable.If there are not enough registers in the If there are not enough registers in the machine, choose registers to spill to machine, choose registers to spill to memory memory

Page 9: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

9CS243 Winter 2006Stanford University

Register AllocationRegister Allocation

IntroductionIntroductionProblem FormulationProblem FormulationAlgorithmAlgorithm

Page 10: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

10CS243 Winter 2006Stanford University

Problem FormulationProblem Formulation

Two pseudoTwo pseudo--registers registers interfereinterfere if at some if at some point in the program they can not both point in the program they can not both occupy the same register.occupy the same register.Interfere Graph:Interfere Graph:

nodes = pseudonodes = pseudo--registersregistersThere is an edge between two nodes if their There is an edge between two nodes if their corresponding pseudocorresponding pseudo--registers interfereregisters interfere

Page 11: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

11CS243 Winter 2006Stanford University

PseudoPseudo--registers registers

a = 1b = 2c = a + bd = a + 3e = a + b

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

Page 12: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

12CS243 Winter 2006Stanford University

Interference Graph Interference Graph

t1 t2 t3

t4 t5

Page 13: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

13CS243 Winter 2006Stanford University

Graph ColoringGraph Coloring

A graph is nA graph is n--colorable, if every node is the colorable, if every node is the graph can be colored with one of the n graph can be colored with one of the n colors such that two adjacent nodes do not colors such that two adjacent nodes do not have the same color.have the same color.To determine if a graph is nTo determine if a graph is n--colorable is colorable is NPNP--complete, for n>2complete, for n>2

Too expensiveToo expensiveHeuristicsHeuristics

Page 14: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

14CS243 Winter 2006Stanford University

Example Example

How many colors are needed?How many colors are needed?

Page 15: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

15CS243 Winter 2006Stanford University

Graph Coloring and Register Graph Coloring and Register AllocationAllocation

Assigning n registers (without spilling) = Assigning n registers (without spilling) = Coloring with n colorsColoring with n colors

Assign a node to a register (color) such that Assign a node to a register (color) such that no two adjacent nodes are assigned same no two adjacent nodes are assigned same registers (colors)registers (colors)

Is spilling necessary? = Is the graph nIs spilling necessary? = Is the graph n--colorable?colorable?

Page 16: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

16CS243 Winter 2006Stanford University

Register AllocationRegister Allocation

IntroductionIntroductionProblem FormulationProblem FormulationAlgorithmAlgorithm

Page 17: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

17CS243 Winter 2006Stanford University

AlgorithmAlgorithm

Step 1: Build an interference graphStep 1: Build an interference graphRefining the notion of a nodeRefining the notion of a nodeFinding the edgesFinding the edges

Step 2: ColoringStep 2: ColoringUse heuristics to find an nUse heuristics to find an n--coloringcoloring

Successful Successful →→ colorable and we have an colorable and we have an assignmentassignmentFailure Failure →→ graph not colorable, or graph is graph not colorable, or graph is colorable, but it is too expensive to colorcolorable, but it is too expensive to color

Page 18: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

18CS243 Winter 2006Stanford University

Step 1a: Nodes in Interference Step 1a: Nodes in Interference Graph Graph

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

Every pseudo-register is a node

Page 19: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

19CS243 Winter 2006Stanford University

Step 1b: Edges of Interference Step 1b: Edges of Interference GraphGraph

IntuitionIntuitionTwo live ranges (necessarily different Two live ranges (necessarily different variables) may interfere if they overlap at variables) may interfere if they overlap at some point in the programsome point in the program

Page 20: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

20CS243 Winter 2006Stanford University

Live RangesLive Ranges

Motivation: to create an interference graph Motivation: to create an interference graph that is easier to colorthat is easier to color

Eliminate interference in a variableEliminate interference in a variable’’s s ““deaddead””zoneszonesIncrease flexibility in allocation: can allocation Increase flexibility in allocation: can allocation same variable to different registerssame variable to different registers

A A live rangelive range consists of a definition and all consists of a definition and all the points in a program (e.g. end of an the points in a program (e.g. end of an instruction) in which that definition is live.instruction) in which that definition is live.

Page 21: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

21CS243 Winter 2006Stanford University

Merged Live RangesMerged Live Ranges

Two overlapping live ranges for same Two overlapping live ranges for same variable must be mergedvariable must be merged

a = b a = c

a = a + d

Page 22: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

22CS243 Winter 2006Stanford University

Merging Live RangesMerging Live RangesMerging definitions into equivalence Merging definitions into equivalence classesclasses

Start by putting each definition in a different Start by putting each definition in a different equivalence classequivalence classFor each point in a programFor each point in a program

If variable is live and there are multiple reaching If variable is live and there are multiple reaching definitions for the variabledefinitions for the variableMerge the equivalence classes of all such Merge the equivalence classes of all such definitions into one equivalence classdefinitions into one equivalence class

From now on, refer to merged live ranges From now on, refer to merged live ranges simply as live rangessimply as live ranges

Page 23: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

23CS243 Winter 2006Stanford University

Algorithm for EdgesAlgorithm for Edges

AlgorithmAlgorithmFor each instruction iFor each instruction i

Let x be live range of definition at instruction iLet x be live range of definition at instruction iFor each live range y present at end of instruction iFor each live range y present at end of instruction i

Insert en edge between x and yInsert en edge between x and y

Page 24: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

24CS243 Winter 2006Stanford University

ExampleExample

b =d = a (d2)

= b + d

c = d = a (d1)

= d + c

a = d (a2)

= a

liveness{a}{a,c}{c,d}{d}

reaching def{a1}{a1,c}{a1,c,d1}{a1,c,d1}

{d} {a}

{a1,b,c,d1,d2}{a2,b,c,d1,d2}

doesn’t use a,b,c or d

Page 25: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

25CS243 Winter 2006Stanford University

Interference Graph Interference Graph

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

t1-t5 are not live

Page 26: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

26CS243 Winter 2006Stanford University

Interference Graph Interference Graph

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

t1-t5 are not live

Page 27: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

27CS243 Winter 2006Stanford University

Interference Graph Interference Graph

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

t1-t5 are not live

Page 28: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

28CS243 Winter 2006Stanford University

Step 2: ColoringStep 2: Coloring

Reminder: coloring for n>2 is NPReminder: coloring for n>2 is NP--completecompleteObservationsObservations

A node with degree < n?A node with degree < n?A node with degree = n?A node with degree = n?A node with degree > n?A node with degree > n?

Page 29: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

29CS243 Winter 2006Stanford University

Coloring AlgorithmColoring Algorithm

AlgorithmAlgorithmIterate until stuck or doneIterate until stuck or done

Pick any node with degree < nPick any node with degree < nRemove the node and its edges from the graphRemove the node and its edges from the graph

If done (no nodes left)If done (no nodes left)Reverse process and add colorsReverse process and add colors

Note: degree of a node may drop in Note: degree of a node may drop in iterationiteration

Page 30: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

30CS243 Winter 2006Stanford University

Colorable by 3 Colors? Colorable by 3 Colors?

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

t1-t5 are not live

Page 31: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

31CS243 Winter 2006Stanford University

Colorable by 3 Colors? Colorable by 3 Colors?

Pick t5 and remove its edges

t1 t2 t3

t4

Page 32: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

32CS243 Winter 2006Stanford University

Colorable by 3 Colors? Colorable by 3 Colors?

Pick t4 and remove its edges

t1 t2 t3

Page 33: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

33CS243 Winter 2006Stanford University

Colorable by 3 Colors? Colorable by 3 Colors?

Pick t3 and remove its edges

t1 t2

Page 34: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

34CS243 Winter 2006Stanford University

Colorable by 3 Colors? Colorable by 3 Colors?

Pick t2 and remove its edges

t1

Page 35: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

35CS243 Winter 2006Stanford University

Register Assignment Register Assignment

t1/r1

Reverse process and add color different Reverse process and add color different from all its neighborsfrom all its neighbors

Page 36: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

36CS243 Winter 2006Stanford University

Register Assignment Register Assignment

t1/r1 t2/r2

Color t2Color t2

Page 37: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

37CS243 Winter 2006Stanford University

Register Assignment Register Assignment

t1/r1 t2/r2 t3/r3

Color t3Color t3

Page 38: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

38CS243 Winter 2006Stanford University

Register Assignment Register Assignment

t1/r1 t2/r2 t3/r3

t4/r3

Color t4Color t4

Page 39: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

39CS243 Winter 2006Stanford University

Register Assignment Register Assignment

t1/r1 t2/r2 t3/r3

t4/r3 t5/r1

Color t5Color t5

Page 40: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

40CS243 Winter 2006Stanford University

After Register AllocationAfter Register Allocation

r1 = 1r2 = 2r3 = r1 + r2r3 = r1 + 3r1 = r1 + r2

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

Page 41: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

41CS243 Winter 2006Stanford University

When Coloring FailsWhen Coloring FailsUse heuristics to improve its chance of success Use heuristics to improve its chance of success and to spill codeand to spill codeAlgorithmAlgorithm

Iterate until doneIterate until doneIf there exists a node v with degree < nIf there exists a node v with degree < n

Place v on stack to register allocatePlace v on stack to register allocateElseElse

Pick a node v to spill using heuristics (e.g. least frequently Pick a node v to spill using heuristics (e.g. least frequently executed, with many neighbors etc)executed, with many neighbors etc)

Remove v and its edges from the graphRemove v and its edges from the graphIf done (no nodes left)If done (no nodes left)

Reverse process and add colorsReverse process and add colors

Page 42: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

42CS243 Winter 2006Stanford University

Colorable by 2 Colors? Colorable by 2 Colors?

t1 = 1t2 = 2t3 = t1 + t2t4 = t1 + 3t5 = t1 + t2

t1 t2 t3

t4 t5

t1-t5 are not live

Page 43: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

43CS243 Winter 2006Stanford University

Colorable by 2 Colors? Colorable by 2 Colors?

Pick t5 and remove it edges

t1 t2 t3

t4

Need to spill!

Page 44: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

44CS243 Winter 2006Stanford University

Is the Algorithm Optimal? Is the Algorithm Optimal?

t5

t1 t2

t3 t4

t1 t2

t3 t4

2-colorable? 3-colorable?

Page 45: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

45CS243 Winter 2006Stanford University

SummarySummaryProblems: Problems:

Given n registers in a machine, is spilling avoidable?Given n registers in a machine, is spilling avoidable?Find an assignment for all pseudoFind an assignment for all pseudo--registers.registers.

SolutionSolutionAbstraction: an interference graphAbstraction: an interference graph

Nodes: merged live rangesNodes: merged live rangesEdges: presence of live range at time of definitionEdges: presence of live range at time of definition

Register allocation and assignment problems = Register allocation and assignment problems = n=n=colorabilitycolorability of interference graph (NPof interference graph (NP--complete)complete)Heuristics to find an assignment for n colorsHeuristics to find an assignment for n colors

Successful: colorable, and finds assignmentSuccessful: colorable, and finds assignmentNot successful: Not successful: colorabilitycolorability unknown and no assignmentunknown and no assignment

Page 46: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

46CS243 Winter 2006Stanford University

backupbackup

Page 47: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

47CS243 Winter 2006Stanford University

Predication ExamplePredication Example

s = s + a

*p = s

a < b

cmp.lt p1,p2=a,b(p1) s = s + a(p2) s = s + b

*p = s

s = s + b

Page 48: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

48CS243 Winter 2006Stanford University

PredicationPredication

Identifying region of basic blocks based on Identifying region of basic blocks based on resource requirement and profitability resource requirement and profitability (branch (branch mispredictionmisprediction rate, rate, mispredictionmispredictioncost, and parallelism).cost, and parallelism).Result: a single predicated basic blockResult: a single predicated basic block

Page 49: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

49CS243 Winter 2006Stanford University

PredicatePredicate--aware Register aware Register AllocationAllocation

Use the same register for two separate computations in the presence of predication, even if there is live-range overlap without considering predicates

Page 50: Register Allocation - Stanford Universityinfolab.stanford.edu/~ullman/dragon/w06/lectures/cs243... · 2006-02-13 · Register Allocation Goal Allocation of variables (pseudo-registers)

50CS243 Winter 2006Stanford University

ExampleExample

(p1) r32 = 10(p2) r32 = 20 ;;(p1) st4 [r33]= r32(p2) r34 = r32 + 1 ;;

(p1) v1 = 10(p2) v2 = 20 ;;(p1) st4 [v10]= v1(p2) v11 = v2 + 1 ;; v1

v2 overlappedlive ranges

same register for v1 and v2