173
Fall 2016-2017 Compiler Principles Lecture 9: Register Allocation Roman Manevich Ben-Gurion University of the Negev

Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Fall 2016-2017 Compiler PrinciplesLecture 9: Register Allocation

Roman ManevichBen-Gurion University of the Negev

Page 2: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Syllabus

FrontEnd

Scanning

Top-downParsing (LL)

Bottom-upParsing (LR)

IntermediateRepresentation

Operational Semantics

Lowering

Optimizations

DataflowAnalysis

LoopOptimizations

Code Generation

RegisterAllocation

2

mid-term exam

Page 3: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Previously• Dataflow framework

– Loop optimizations

– Strong liveness analysis dead code elimination

3

Page 4: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

agenda• An simple assembly language

• The need for register allocation

• Register allocation algorithms

– Graph coloring

– Linear scan (tentative)

4

Page 5: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Assembly language:ASM

5

Page 6: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Syntax

V n | r

R V Op V

Op - | + | * | / | = | | << | >> | …

C l: skip

| l: r := R | l: r := M[V] | l: M[V] := V| l: Goto l’| l: IfZ x Goto l’ | l: IfNZ x Goto l’| l: Push V | l: Pop r | Call foo

ASM C+

6

n Num Numeralsl Num Labelsr Reg Registers

Page 7: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

7

Register allocation motivation

Page 8: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Registers

• Most machines have a set of registers, dedicated memory locations that

– can be accessed quickly,

– can have computations performed on them, and

– exist in small quantity

• Using registers intelligently is a critical stepin any compiler

– A good register allocator can generate code orders of magnitude better than a bad register allocator

8

Page 9: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Register allocation

• In IL, there are an unlimited number of variables

• On a physical machine there are a small number of registers:

– x86 has four general-purpose registers and a number of specialized registers

– MIPS has twenty-four general-purpose registers and eight special-purpose registers

• Register allocation is the process of assigning variables to registers and managing data transfer in and out of registers

9

Page 10: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Challenges in register allocation

• Registers are scarce– Often substantially more IR variables than registers– Need to find a way to reuse registers whenever possible

• Registers are complicated– x86: Each register made of several smaller registers;

can't use a register and its constituent registers at the same time

– x86: Certain instructions must store their results in specific registers; can't store values there if you want to use those instructions

– MIPS: Some registers reserved for the assembler or operating system

– Most architectures: Some registers must be preserved across function calls

10

Page 11: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

11

naive register allocation

Page 12: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simple approach

• Problem: program execution very inefficient –moving data back and forth between memory and registers

12

x := y + z

mov 16(%ebp), %eaxmov 20(%ebp), %ebxadd %ebx, %eaxmov %eax, 24(%ebp)

• Straightforward solution:• Allocate each variable in activation record• At each instruction:

• bring values needed into registers (rematerialization),• perform operation,• then store result to memory

Page 13: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

13

Reusing registers

Page 14: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Find a register allocation

14

b := a + 2

c := b * b

b := c + 1

Ret b * a

eax

ebx

registerregistervariable

?a

?b

?c

Page 15: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Is this a valid allocation?

eax

ebx

register

15

b := a + 2

c := b * b

b := c + 1

Ret b * a

registervariable

eaxa

ebxb

eaxc

ebx := eax + 2

eax := ebx * ebx

ebx := eax + 1

Ret ebx * eax

Overwrites previous value of ‘a’ also stored in eax

Page 16: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Is this a valid allocation?

eax

ebx

register

16

b := a + 2

c := b * b

b := c + 1

Ret b * a

registervariable

ebxa

eaxb

eaxc

eax := ebx + 2

eax := eax * eax

eax := eax + 1

Ret eax * ebx

Value of ‘c’ stored in eaxis not needed anymore so reuse it for ‘b’

Page 17: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

17

Register interference graph

Page 18: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Main idea

• For every command c, we have live[c]– Set of temporaries live out of c

• Two variables interfere if they appear in the same live[c] of any command c– Cannot be allocated to the same register

• Conversely, if two variables do not interfere with each other, they can be assigned the same register– We say they have disjoint live ranges

• How to assign registers to variables?

18

Page 19: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Interference graph

• Nodes of the graph = variables

• Edges connect variables that interfere with one another

• Nodes will be assigned a color corresponding to the register assigned to the variable

• Two colors can’t be next to one another in the graph

19

Page 20: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Liveness analysis

b := a + 2

c := b * b

b := c + 1

Ret b * a

20

Page 21: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Liveness analysis

b := a + 2

c := b * b

b := c + 1{b, a}

Ret b * a

21

Page 22: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Liveness analysis

b := a + 2

c := b * b{a, c}

b := c + 1{b, a}

Ret b * a

22

Page 23: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Liveness analysis

b := a + 2{b, a}

c := b * b{a, c}

b := c + 1{b, a}

Ret b * a

23

Page 24: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Liveness analysis

{a}b := a + 2

{b, a}c := b * b

{a, c}b := c + 1

{b, a}Ret b * a

24

Page 25: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Interference graph

a

cb

eax

ebx

color register

25

{a}b := a + 2

{b, a}c := b * b

{a, c}b := c + 1

{b, a}Ret b * a

Page 26: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Colored graph

a

cb

eax

ebx

color register

26

{a}b := a + 2

{b, a}c := b * b

{a, c}b := c + 1

{b, a}Ret b * a

Page 27: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

27

Page 28: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

28

Page 29: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

29

Graph coloring

Page 30: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Graph coloring

• Register allocation is equivalent to graph-coloring, which is NP-hard if there are at least three registers

• No good polynomial-time algorithms(or even good approximations!) are knownfor this problem

• We have to be content with a heuristic that is good enough for RIGs that arise in practice

30

Page 31: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring by simplification [Kempe 1879]

• How to find a K-coloring of a graph

• Observation:

– Suppose we are trying to K-color a graph and find a node with fewer than K edges

– If we delete this node from the graph and color what remains, we can find a color for this node if we add it back in

– Reason: fewer than K neighbors some color must be left over

31

Page 32: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring by simplification [Kempe 1879]

• How to find a K-coloring of a graph• Phase 1: Simplify

– Repeatedly simplify graph – When a variable (i.e., graph node) is

removed, push it on a stack

• Phase 2: Select– Unwind stack and reconstruct the graph as

follows:• Pop variable from the stack• Add it back to the graph• Color the node for that variable with a color

that it doesn’t interfere with

32

simplify

select

Page 33: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

33

Coloring example

Page 34: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

c

stack:

eax

ebx

color register

34

Page 35: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

stack:

c

c

eax

ebx

color register

35

Page 36: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

stack:

ec

c

eax

ebx

color register

36

Page 37: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

stack:

aec

c

eax

ebx

color register

37

Page 38: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

stack:baec

c

eax

ebx

color register

38

Page 39: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

a

stack:dbaec

c

eax

ebx

color register

39

Page 40: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

ed

eax

ebx

color register

a

stack:

baec

c

40

Page 41: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

b

e

a

stack:

aec

c

eax

ebx

color register

d

41

Page 42: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

e

a

stack:

ec

c

eax

ebx

color register

b

d

42

Page 43: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

e

stack:

c

c

eax

ebx

color register

a

b

d

43

Page 44: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coloring for K=2

stack:

c

eax

ebx

color register

e

a

b

d

44

Page 45: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

45

spilling

Page 46: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Failure of heuristic

• If the graph cannot be colored, it will eventually be simplified to graph in which every node has at least K neighbors

• Sometimes, the graph is still K-colorable!

• Finding a K-coloring in all situations is anNP-complete problem

– We will have to approximate to make register allocators fast enough

46

Page 47: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example where Kempe’s approach fails

c

eax

ebx

color register

e

a

b

d

47

stack:

Page 48: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example where Kempe’s approach fails

c

eax

ebx

color register

e

a

b

d

48

stack:

d

Page 49: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

But RIG is actually 2-colorable

c

eax

ebx

color register

e

a

b

d

49

Page 50: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example of non 2-colorable graph

c

eax

ebx

color register

e

a

b

d

Some graphs can’t be colored in K colors:

50

A 3-clique

Page 51: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Chaitin’s algorithm [CC’82]

• Choose and remove an arbitrary node, marking it “troublesome”

– Use heuristics to choose which one:

– When adding node back in, it may be possible to find a valid color

– Otherwise, we have to spill that node

51

?

Page 52: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Chaitin’s algorithm [CC’82]

• Choose and remove an arbitrary node, marking it “troublesome”

– Use heuristics to choose which one:

– When adding node back in, it may be possible to find a valid color

– Otherwise, we have to spill that node(lowest priority)

52

spill priority = (uo + 10 ui) / deguo = #use+defs outside of loopui = #use+defs inside of loop

Page 53: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Spilling

• Phase 3: Spill– once all nodes have K or more neighbors, pick a

node for spilling• There are many heuristics that can be used to pick a

node

• Try to pick node not used much, not in inner loop

• Assign its storage to activation record

– Remove it from graph and mark it as potential spill (technically, push on a spill stack)

• We can now repeat phases 1-2 without this node

53

Page 54: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

54

Potential spill example

Page 55: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Color the following graph

55

c

a

be

AX

BX

color register

CX

d

f

(color)stack:

potentialspill:

Page 56: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Pick a for potential spill

56

c

a

be

AX

BX

color register

CX

d

f

(color)stack:

potentialspill:

Page 57: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Simplify d

57

c

a

be

AX

BX

color register

CX

d

f

(color)stack:

potentialspill:a

Page 58: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Simplify e

58

c

a

be

AX

BX

color register

CX

d

f

(color)stack:d

potentialspill:a

Page 59: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Simplify b

59

c

a

be

AX

BX

color register

CX

d

f

(color)stack:ed

potentialspill:a

Page 60: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Simplify c

60

c

a

be

AX

BX

color register

CX

d

f

(color)stack:bed

potentialspill:a

Page 61: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Simplify f

61

c

a

be

AX

BX

color register

CX

d

f

(color)stack:cbed

potentialspill:a

Page 62: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for f

62

c

a

be

AX

BX

color register

CX

d

f

(color)stack:fcbed

potentialspill:a

Page 63: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for c

63

c

a

be

AX

BX

color register

CX

d

f

(color)stack:cbed

potentialspill:a

Page 64: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for b

64

c

a

be

AX

BX

color register

CX

d

f

(color)stack:bed

potentialspill:a

Page 65: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for e

65

c

a

be

AX

BX

color register

CX

d

f

(color)stack:ed

potentialspill:a

Page 66: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for d

66

c

a

be

AX

BX

color register

CX

d

f

(color)stack:d

potentialspill:a

Page 67: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Select color for a

67

c

a

be

AX

BX

color register

CX

d

f

(color)stack:

potentialspill:a

Page 68: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potential spill example

• Done – no actual spill

68

c

a

be

AX

BX

color register

CX

d

f

(color)stack:

potentialspill:

Page 69: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Chaitin’s algorithm [CC’82]

• Phase 1: Simplify• Phase 2: Select

– Unwind stack and reconstruct the graph as follows:• Pop (non-spill) variable from the stack• Add it back to the graph• Color the node for that variable with a color that it doesn’t interfere with its

neighbors

– Unwind spill stack• Pop spill variable• If there is an available color add back to graph• Otherwise mark variable as actual spill

• Phase 3: Spill– If all nodes have K or more neighbors, pick a “heavy” node for spilling

and add to potential spill stack

• Phase 4: Rewrite– Rewrite code for actual spill variables– Recompute liveness information– Repeat phases 1-3

69

Page 70: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Chaitin’s algorithm [CC’82]

70

Simplify

Mark potential spills

Select colorsand detectactual spills

Rewrite codeto implementactual spills

Livenessanalysis

any actualspill done

any potentialspill done

Don’t spill same variable twice

found lightnode

Page 71: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

71

actual spill example

Page 72: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Actual spill example

• Apply Chaitin’s algorithm forthe following program

– The set of registers is AX, BX, CX

– Upon any non-deterministic choice,choose by lexicographic order

72

c := e;

a := a + e;

d := a + c;

d := a + b;

d := d + b;

// live = {d}

Page 73: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Step 1: compute liveness

• Let’s compute liveness information

73

{a, b, e}

c := e;

{a, b, c, e}

a := a + e;

{a, b, c}

d := a + c;

{a, b}

d := a + b;

{b, d}

d := d + b;

// live = {d}

Page 74: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Step 2: draw RIG

• Simplify d

74

c

e

a

b

d

(color)stack:

potentialspill:

AX

BX

color register

CX

Page 75: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Potentially spill b

• #use+def(a)=4, #use+def(b)=2,#use+def(c)=2, #use+def(d)=4,#use+def(e)=2

• Priorities: p(a)=4/4, p(b)=2/3, p(c)=2/3, p(e)=2/3• (potentially) spill b

75

c

e

a

b

d

(color)stack:d

potentialspill:

AX

BX

color register

CX

c := e;

a := a + e;

d := a + c;

d := a + b;

d := d + b;

// live = {d}

Page 76: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify a

76

c

e

a

b

d

(color)stack:d

potentialspill:b

AX

BX

color register

CX

Page 77: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify c

77

c

e

a

b

d

(color)stack:ad

potentialspill:b

AX

BX

color register

CX

Page 78: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify e

78

c

e

a

b

d

(color)stack:cad

potentialspill:b

AX

BX

color register

CX

Page 79: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color nodes on stack

• Select color for e

79

c

e

a

b

d

(color)stack:ecad

potentialspill:b

AX

BX

color register

CX

Page 80: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color nodes on stack

• Select color for c

80

c

e

a

b

d

(color)stack:cad

potentialspill:b

AX

BX

color register

CX

Page 81: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color nodes on stack

• Select color for a

81

c

e

a

b

d

(color)stack:ad

potentialspill:b

AX

BX

color register

CX

Page 82: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color nodes on stack

• Select color for d

82

c

e

a

b

d

(color)stack:

potentialspill:b

AX

BX

color register

CX

Page 83: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

b cannot be colored

• Cannot color b – actual spill

83

c

e

a

b

d

(color)stack:

potentialspill:b

AX

BX

color register

CX

Page 84: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Actual spill: rich assembly

• Suppose assembly allows commands of the form x:=y+M[offset]

• Rewrite program for spilled variable b

– Choose location on frame: b_loc

84

c := e;

a := a + e;

d := a + c;

d := a + M[b_loc];

d := d + M[b_loc];

// live = {d}

c := e;

a := a + e;

d := a + c;

d := a + b;

d := d + b;

// live = {d}

Page 85: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Actual spill: basic assembly

• Rewrite program for spilled variable b

– Choose location on frame: b_loc

– Use temporaries for reading from frame: b1, b2

– Now attempt to color all variables, including temporaries• If unable don’t spill temporaries, choose other variables to spill,

otherwise can go into infinite spill-color loop

85

c := e;

a := a + e;

d := a + c;

b1 := M[b_loc];

d := a + b1;

b2 := M[b_loc];

d := d + b2;

// live = {d}

c := e;

a := a + e;

d := a + c;

d := a + b;

d := d + b;

// live = {d}

Page 86: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Rewriting rules

• Assume we want to spill variable x

• Case 1: a command that reads x:c = l: z := a + x=> xl := M[x_loc]; z := a + xl

• Case 2: a command that writes to x:c = l: x := a + b=> xl := a + b; M[x_loc] := xl

86

Page 87: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Compute liveness for new program

87

{a, e}

c := e;

{a, c, e}

a := a + e;

{a, c}

d := a + c;

{a}

b1 := M[b_loc];

{a, b1}

d := a + b1;

{d}

b2 := M[b_loc];

{d, b2}

d := d + b2;

// live = {d}

Page 88: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color

• Simplify b1, b2, d, a, c, e

• Select colors

88

c

e

a

b2

d

AX

BX

color register

CX

b1

Page 89: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color

• Simplify b1, b2, d, a, c, e

• Select colors

89

c

e

a

b2

d

AX

BX

color register

CX

b1

Page 90: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Emit code based on colors

90

c

e

a

b2

d

AX

BX

color register

CX

b1

BX := AX;

CX := CX + AX;

AX := CX + BX;

BX := M[b_loc];

AX := CX + BX;

BX := M[b_loc];

AX := AX + BX;

c := e;

a := a + e;

d := a + c;

b1 := M[b_loc];

d := a + b1;

b2 := M[b_loc];

d := d + b2;

Page 91: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Pathological cases

• In general, spilling a variable may not help

– E.g., if there is only one use

• On next iteration choose a different variable to spill

91

Page 92: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

92

Register constraints

Page 93: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Handling precolored nodes

• Some variables are pre-assigned to registers– e.g.: mul on x86/pentium

• Uses eax; defines eax, edx

– e.g.: call on x86/pentium• Defines (trashes) caller-save registers eax, ecx, edx

• To properly allocate registers, treat these register uses as special temporary variables and enter into interference graph as precolored nodes

93

Page 94: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Handling precolored nodes

• Simplify. Never remove a pre-colored node – it already has a color, i.e., it is a given register

• Select. Once simplified graph has all colored nodes, add other nodes back in and color them using precolored nodes as starting point

94

Page 95: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

95

Optimizing moves

Page 96: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Optimizing move instructions

• Code generation produces a lot of extra MOVE instructions

t5 := t9

• If we can assign t5 and t9 to same register, we can get rid of the MOVE– Effectively, copy propagation at the register

allocation level

• Idea: if t5 and t9 are not connected in inference graph, coalesce them into a single variable; the move will be redundant

96

Page 97: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

RIG with MOVE edges

• Add a second type of edges – MOVE edges

97

b

ed

a

c

eax

ebx

color register

Page 98: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coalescing

• Problem: coalescing nodes can make a graphun-colorable

– Conservative coalescing heuristic

• What should we do?

98

a b a/b

coalesceMOVE

Page 99: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Conservative coalescing

• Coalesce MOVE edge (a,b) if it does not affect colorability

• Definition: A node is heavy if its degree is ≥Kand light otherwise

99

Page 100: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Briggs criterion

• Coalesce only if the merged node ab has <Kheavy neighbors

• Reason:

– Simplify will first remove all light neighbors

– ab will then be adjacent to <K neighbors

– Simplify can remove ab

100

Page 101: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

George criterion

• Coalesce only if all heavy neighbors of a are also neighbors of b

• Reason:

– Simplify can remove all light neighbors of a

– Remaining heavy neighbors of a are neighbors of bso if b is colorable then so is a

– The light neighbors of a are light => colorable

101

Page 102: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coalescing criterion example

c

eax

ebx

color register

e

a

b

102

Can we coalesce (a,e) ?

• By the Briggs criterion?– #(heavy-neighbors(a,e) < K

• By the George criterion?– All heavy neighbors of a are also neighbors of e

Page 103: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coalescing criterion example

c

eax

ebx

color register

e

a

b

103

Can we coalesce (a,e) ?

• By the Briggs criterion? NO– #(heavy-neighbors(a,e) < K

• By the George criterion? YES– All heavy neighbors of a are also neighbors of e

Page 104: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify, coalesce and freeze

• Phase 1: Simplify– Step 1 (simplify): simplify as much as possible without removing nodes that are the source or

destination of a move(MOVE-related nodes)

– Step 2 (coalesce): coalesce a MOVE-related edge provided low-degree node results– Step 3 (freeze): if neither steps 1 or 2 apply,

freeze a MOVE instruction: low-degree nodes involved are marked not MOVE-related (remove MOVE edge) and try step 1 again

– Step 4 (spill): if all nodes are heavy select a candidate for spilling using a priority function and move to potential spill stack

• Phase 2: Select– Unwind stack and reconstruct the graph as follows:

• Pop variable from the stack• Add it back to the graph• Color the node node with a color that it doesn’t interfere with

– Unwind potential spill stack and attempt to color node – if unable mark corresponding variable as actual spill

• Phase 4: Rewrite– Allocate position in frame for spilled variable v– On each usage of v load to vi from frame

104

Page 105: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify, coalesce and freeze

105

Simplify: recursivelyremove non-MOVE nodeswith <K neighbors, pushingthem onto stack

Coalesce: conservatively mergeunconstrained MOVE-relatednodes via Briggs/George Criterion

Freeze: give up coalescing on somelow-degree MOVE-related node

any coalescedone

any freezedone

any simplifydone

Page 106: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Overall algorithm

106

Simplify, freezeand coalesce

Mark potential spills

Select colorsand detectactual spills

Rewrite codeto implementactual spills

Livenessanalysis

any actualspill done

any potentialspill done

Page 107: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

107

Coalesce example

Page 108: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coalesce example

• Apply Chaitin’s algorithm forthe following program

– The set of registers is AX, BX, CX

– Use the Briggs criterionfor conservative coalescing

– Upon any non-deterministic choice,choose by lexicographic order

108

a := e;

d := a + c;

d := d + b;

// live = {d}

Page 109: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Step 1: compute liveness

109

{b, c, e}

a := e;

{a, b, c}

d := a + c;

{b, d}

d := d + b;

// live = {d}

Page 110: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color

• Simplify d

110

c

e

a

b

d

(color)stack:

potentialspill:

AX

BX

color register

CX

Page 111: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Coalesce step

• Cannot simplify a or e because they are MOVE-related

• Coalesce (a,e)

111

c

e

a

b

d

(color)stack:d

potentialspill:

AX

BX

color register

CX

Page 112: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify ae

112

c

ae

b

d

(color)stack:d

potentialspill:

AX

BX

color register

CX

Page 113: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify b

113

c

ae

b

d

(color)stack:aed

potentialspill:

AX

BX

color register

CX

Page 114: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify

• Simplify c

114

c

ae

b

d

(color)stack:baed

potentialspill:

AX

BX

color register

CX

Page 115: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select

• Color c

115

c

ae

b

d

(color)stack:cbaed

potentialspill:

AX

BX

color register

CX

Page 116: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select

• Color b

116

c

ae

b

d

(color)stack:baed

potentialspill:

AX

BX

color register

CX

Page 117: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select

• Color ae

117

c

ae

b

d

(color)stack:aed

potentialspill:

AX

BX

color register

CX

Page 118: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select

• Color d

118

c

ae

b

d

(color)stack:d

potentialspill:

AX

BX

color register

CX

Page 119: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select

119

c

ae

b

d

(color)stack:

potentialspill:

AX

BX

color register

CX

Page 120: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Emit code

120

CX := CX;

AX := CX + AX;

AX := AX + BX;

c

AX

BX

color register

ae

b

d

CX

a := e;

d := a + c;

d := d + b;

Page 121: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

121

Freeze example

Page 122: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Step 1: compute liveness

122

{b, e}

c := e + b;

{a, c}

e := a;

{a, c}

b := a + c;

{b}

d := b;

{b, d}

d := b + d;

// live = {d}

Page 123: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color

• Simplify d

123

c

e

a

b

d

(color)stack:

potentialspill:

AX

BX

color register

Page 124: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Attempt to color

• Can we coalesce a and e according to Briggs?

• Let’s try

124

c

e

a

b

(color)stack:d

potentialspill:

AX

BX

color register

Page 125: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Unsuccessful attempt to color

• No, node becomes heavy

• Undo coalesce and try something else

125

c

ae

b

(color)stack:d

potentialspill:

AX

BX

color register

Page 126: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Freeze a

126

c

e

a

b

(color)stack:d

potentialspill:

AX

BX

color register

Page 127: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify a

127

c

e

a

b

(color)stack:d

potentialspill:

AX

BX

color register

Page 128: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify c

128

c

e

b

(color)stack:ad

potentialspill:

AX

BX

color register

Page 129: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify b

129

e

b

(color)stack:cad

potentialspill:

AX

BX

color register

Page 130: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Simplify e

130

e

(color)stack:bcad

potentialspill:

AX

BX

color register

Page 131: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for e

131

(color)stack:ebcad

potentialspill:

AX

BX

color register

Page 132: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for b

132

(color)stack:bcad

potentialspill:

AX

BX

color register

e

Page 133: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for c

133

(color)stack:cad

potentialspill:

AX

BX

color register

e

b

Page 134: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for a

134

(color)stack:ad

potentialspill:

AX

BX

color register

e

b c

Page 135: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for d

135

(color)stack:d

potentialspill:

AX

BX

color register

e

b c

a

Page 136: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Select color for d

136

(color)stack:

potentialspill:

AX

BX

color register

e

b c

a

d

Page 137: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

137

Linear scan register allocation

Page 138: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Live ranges and live intervals

• The live range for a variable is the set of program points at which the variable is alive

• The live interval for a variable is the smallest sub-range of the IR code containing all of a variable’s live ranges

– A property of the IR code, not the CFG

– Less precise than live ranges, but simpler to work with

138

Page 139: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

d

Live ranges and live intervals

139

e := d + a

f := b + c

f := f + b

IfZ e Goto _L0

d := e + f

d := e – f

Goto _L1

_L0:

d := e-f

_L1:

g := d

Ret g

a b c e f g

Page 140: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Register allocation with live ranges

• Given the live intervals for all thevariables in the program, we canallocate registers using a simplegreedy algorithm

• Idea: Track which registers are freeat each point

• When a live interval begins, givethat variable a free register

• When a live interval ends, theregister is once again free

• We can't always fit everything intoa register (spill)

140

da b c e f g

Page 141: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

141

R0 R1 R2 R3

da b c e f g Free registers

Page 142: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

142

R0 R1 R2 R3

da b c e f g Free registers

Page 143: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

143

R0 R1 R2 R3

da b c e f g Free registers

Page 144: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

144

R0 R1 R2 R3

da b c e f g Free registers

Page 145: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

145

R0 R1 R2 R3

da b c e f g Free registers

Page 146: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

146

R0 R1 R2 R3

da b c e f g Free registers

Page 147: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

147

R0 R1 R2 R3

da b c e f g Free registers

Page 148: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

148

R0 R1 R2 R3

da b c e f g Free registers

Page 149: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

149

R0 R1 R2 R3

da b c e f g Free registers

Page 150: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

150

da b c e f g Free registers

R0 R1 R2 R3

Page 151: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

151

da b c e f g Free registers

R0 R1 R2 R3

Page 152: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Example

152

da b c e f g Free registers

R0 R1 R2 R3

Page 153: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

153

R0 R1 R2

da b c e f g Free registers

Page 154: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

154

R0 R1 R2

da b c e f g Free registers

Page 155: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

155

R0 R1 R2

da b c e f g Free registers

Page 156: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

156

R0 R1 R2

da b c e f g Free registers

Page 157: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

157

R0 R1 R2

da b c e f g Free registers

Which register should we use?

Page 158: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

158

R0 R1 R2

da b c e f g Free registers

Which variable should we spill?

One with longer interval

Page 159: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

159

R0 R1 R2

da b c e f g Free registers

Page 160: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

160

R0 R1 R2

da b c e f g Free registers

Page 161: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

161

R0 R1 R2

da b c e f g Free registers

Page 162: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

162

R0 R1 R2

da b c e f g Free registers

Page 163: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

163

R0 R1 R2

da b c e f g Free registers

Page 164: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

164

R0 R1 R2

da b c e f g Free registers

Page 165: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

165

R0 R1 R2

da b c e f g Free registers

Page 166: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

166

R0 R1 R2

da b c e f g Free registers

Page 167: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Another example

167

R0 R1 R2

da b c e f g Free registers

Page 168: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Linear scan register allocation

• A relatively new algorithm (Polleto and Sarkar)

• Advantages:– Very efficient (after computing live intervals, runs in

linear time)

– Produces good code in many instances

– Allocation step works in one pass; can generate code during iteration

– Often used in JIT compilers like Java HotSpot

• Disadvantage– Imprecise due to use of live intervals rather than live

ranges

168

Page 169: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Comparison to RA by coloring

169

Page 170: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

RA recap

• Graph-coloring

• Linear scan

170

Page 171: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Advertisement 1

• Program Analysis and Verification course next semester

– How to verify programs do what they are supposed to do

– How to verify they don’t loop forever

– Automatically, without ever running the program

– Possibly teach code synthesis

171

Page 172: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Advertisement 2

• Are you looking to do some cool research?I am looking for enthusiastic students for M.Sc.

• Come talk to me

172

Page 173: Fall 2016-2017 Compiler Principles Lecture 9: Register ...comp171/wiki.files/09... · Register allocation •In IL, there are an unlimited number of variables •On a physical machine

Good luck with the exam and the final project!

173