Register Allocation Akim Demaille tienne Renault Roland Levillain - - PowerPoint PPT Presentation

register allocation
SMART_READER_LITE
LIVE PREVIEW

Register Allocation Akim Demaille tienne Renault Roland Levillain - - PowerPoint PPT Presentation

Register Allocation Akim Demaille tienne Renault Roland Levillain first . last @lrde.epita.fr EPITA cole Pour lInformatique et les Techniques Avances May 19, 2018 Register Allocation Interference Graph 1 Coloring by


slide-1
SLIDE 1

Register Allocation

Akim Demaille Étienne Renault Roland Levillain first.last@lrde.epita.fr

EPITA — École Pour l’Informatique et les Techniques Avancées

May 19, 2018

slide-2
SLIDE 2

Register Allocation

1

Interference Graph

2

Coloring by Simplification

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 2 / 98

slide-3
SLIDE 3

Interference Graph

1

Interference Graph

2

Coloring by Simplification

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 3 / 98

slide-4
SLIDE 4

Interference Graph

a := 0 b := a + 1 c := c + b a := b * 2 a < N return c

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 4 / 98

slide-5
SLIDE 5

Interference Graph

a := 0 b := a + 1 c := c + b a := b * 2 a < N return c

a c b

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 4 / 98

slide-6
SLIDE 6

Interference Graph

a := 0 b := a + 1 c := c + b a := b * 2 a < N return c

a c b a c b

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 4 / 98

slide-7
SLIDE 7

Register Allocation

a := 0 L1: b := a + 1 c := c + b a := b * 2 if a < N goto L1 return c

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 5 / 98

slide-8
SLIDE 8

Register Allocation

a := 0 L1: b := a + 1 c := c + b a := b * 2 if a < N goto L1 return c

a c b

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 5 / 98

slide-9
SLIDE 9

Register Allocation

a := 0 L1: b := a + 1 c := c + b a := b * 2 if a < N goto L1 return c

a c b

r1 := 0 L1: r1 := r1 + 1 r2 := r2 + r1 r1 := r1 * 2 if r1 < N goto L1 return r2

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 5 / 98

slide-10
SLIDE 10

Coloring by Simplification

1

Interference Graph

2

Coloring by Simplification Spilling Coalescing Precolored Nodes Implementation

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 6 / 98

slide-11
SLIDE 11

Interference Graph [Appel, 1998]

Four registers: r1, r2, r3, r4.

live in: k j g := [j + 12] h := k - 1 f := g * h e := [j + 8] m := [j + 16] b := [f] c := e + 8 d := c k := m + 4 j := b live out: d k j

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 7 / 98

slide-12
SLIDE 12

Interference Graph: Simplify 0

g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 8 / 98

slide-13
SLIDE 13

Interference Graph: Simplify 1

h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 9 / 98

slide-14
SLIDE 14

Interference Graph: Simplify 2

k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 10 / 98

slide-15
SLIDE 15

Interference Graph: Simplify 3

d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 11 / 98

slide-16
SLIDE 16

Interference Graph: Simplify 4

j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 12 / 98

slide-17
SLIDE 17

Interference Graph: Simplify 5

e j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 13 / 98

slide-18
SLIDE 18

Interference Graph: Simplify 6

f e j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 14 / 98

slide-19
SLIDE 19

Interference Graph: Simplify 7

b f e j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 15 / 98

slide-20
SLIDE 20

Interference Graph: Simplify 8

c b f e j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 16 / 98

slide-21
SLIDE 21

Interference Graph: Simplify 9

m c b f e j d k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 17 / 98

slide-22
SLIDE 22

Interference Graph: Color 9

m c b f e j d k h g

m:1 c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 18 / 98

slide-23
SLIDE 23

Interference Graph: Color 8

c b f e j d k h g

m:1 c:3 d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 19 / 98

slide-24
SLIDE 24

Interference Graph: Color 7

b f e j d k h g

m:1 c:3 d b:2 e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 20 / 98

slide-25
SLIDE 25

Interference Graph: Color 6

f e j d k h g

m:1 c:3 d b:2 e j k f:2 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 21 / 98

slide-26
SLIDE 26

Interference Graph: Color 5

e j d k h g

m:1 c:3 d b:2 e:4 j k f:2 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 22 / 98

slide-27
SLIDE 27

Interference Graph: Color 4

j d k h g

m:1 c:3 d b:2 e:4 j:3 k f:2 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 23 / 98

slide-28
SLIDE 28

Interference Graph: Color 3

d k h g

m:1 c:3 d:4 b:2 e:4 j:3 k f:2 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 24 / 98

slide-29
SLIDE 29

Interference Graph: Color 2

k h g

m:1 c:3 d:4 b:2 e:4 j:3 k:1 f:2 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 25 / 98

slide-30
SLIDE 30

Interference Graph: Color 1

h g

m:1 c:3 d:4 b:2 e:4 j:3 k:1 f:2 h:2 g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 26 / 98

slide-31
SLIDE 31

Interference Graph: Color 0

g

m:1 c:3 d:4 b:2 e:4 j:3 k:1 f:2 h:2 g:4

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 27 / 98

slide-32
SLIDE 32

Result

live in: k j g := [j + 12] h := k - 1 f := g * h e := [j + 8] m := [j + 16] b := [f] c := e + 8 d := c k := m + 4 j := b live out: d k j live in: r1 r3 r4 := [r3 + 12] r2 := r1 - 1 r2 := r4 * r2 r4 := [r3 + 8] r1 := [r3 + 16] r2 := [r2] r3 := r4 + 8 r4 := r3 r1 := r1 + 4 r3 := r2 live out: r4 r1 r3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 28 / 98

slide-33
SLIDE 33

Simple Register Allocation

Build Simplify Select

build the conflict graph from the program simplify the nodes with insignificant degree select (or color) while rebuilding the graph.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 29 / 98

slide-34
SLIDE 34

Simple Register Allocation

Build Simplify Select

build the conflict graph from the program simplify the nodes with insignificant degree select (or color) while rebuilding the graph.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 29 / 98

slide-35
SLIDE 35

Simple Register Allocation

Build Simplify Select

build the conflict graph from the program simplify the nodes with insignificant degree select (or color) while rebuilding the graph.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 29 / 98

slide-36
SLIDE 36

Simple Register Allocation

Build Simplify Select

build the conflict graph from the program simplify the nodes with insignificant degree select (or color) while rebuilding the graph. Based on: A.B. Kempe. On the Geographical problem of the four colors, Am.

  • J. Math 2, 193–200, 1879.

[Appel, 1998, Matz, 2003]

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 29 / 98

slide-37
SLIDE 37

Yes, but What Color? [Matz, 2003]

Usually, first-fit (registers are ordered). Trying caller save first helps. Biased Coloring. [Briggs, 1992] Use a color already unavailable to our neighbors.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 30 / 98

slide-38
SLIDE 38

Yes, but What Color? [Matz, 2003]

Usually, first-fit (registers are ordered). Trying caller save first helps. Biased Coloring. [Briggs, 1992] Use a color already unavailable to our neighbors.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 30 / 98

slide-39
SLIDE 39

Yes, but What Color? [Matz, 2003]

Usually, first-fit (registers are ordered). Trying caller save first helps. Biased Coloring. [Briggs, 1992] Use a color already unavailable to our neighbors.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 30 / 98

slide-40
SLIDE 40

Spilling

1

Interference Graph

2

Coloring by Simplification Spilling Coalescing Precolored Nodes Implementation

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 31 / 98

slide-41
SLIDE 41

Spilling

A map can always be colored with 4 colors. . .

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 32 / 98

slide-42
SLIDE 42

Spilling

A map can always be colored with 4 colors. . . But for graph coloring, there is no reason for: this simple heuristics to always find a solution, a solution to always exist. . .

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 32 / 98

slide-43
SLIDE 43

Spilling

A map can always be colored with 4 colors. . . But for graph coloring, there is no reason for: this simple heuristics to always find a solution, a solution to always exist. . .

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 32 / 98

slide-44
SLIDE 44

Spilling

Not enough registers

t1 := t1 + t2

So use the stack

[sp + 4] := [sp + 4] + [sp + 8]

But use temporaries to do so!

t12 := [sp + 4] t13 := [sp + 8] t12 := t12 + t13 [sp + 4] := t12

Why should it solve the problem?

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 33 / 98

slide-45
SLIDE 45

Spilling

Not enough registers

t1 := t1 + t2

So use the stack

[sp + 4] := [sp + 4] + [sp + 8]

But use temporaries to do so!

t12 := [sp + 4] t13 := [sp + 8] t12 := t12 + t13 [sp + 4] := t12

Why should it solve the problem?

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 33 / 98

slide-46
SLIDE 46

Spilling

Not enough registers

t1 := t1 + t2

So use the stack

[sp + 4] := [sp + 4] + [sp + 8]

But use temporaries to do so!

t12 := [sp + 4] t13 := [sp + 8] t12 := t12 + t13 [sp + 4] := t12

Why should it solve the problem?

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 33 / 98

slide-47
SLIDE 47

Spilling

Not enough registers

t1 := t1 + t2

So use the stack

[sp + 4] := [sp + 4] + [sp + 8]

But use temporaries to do so!

t12 := [sp + 4] t13 := [sp + 8] t12 := t12 + t13 [sp + 4] := t12

Why should it solve the problem?

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 33 / 98

slide-48
SLIDE 48

Register Allocation with Spills

Build Simplify Potential spill Select Actual spill

Rebuild the graph if there were any actual spills

spill when one cannot simplify, the (uses of the) temporary must be rewritten using the stack. rebuild but then, the conflict graph is to be rewritten [Appel, 1998, Matz, 2003]

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 34 / 98

slide-49
SLIDE 49

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-50
SLIDE 50

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-51
SLIDE 51

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-52
SLIDE 52

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-53
SLIDE 53

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-54
SLIDE 54

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-55
SLIDE 55

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-56
SLIDE 56

Yes, But Who Should be Spilled?

The simplification order does not matter The spilling order matters Spilling decreases the degree of the neighbors . . . hence it enables additional simplifications . . . so “first spilled, last served” . . . therefore: spill cheap temporaries

few def/uses pay attention to loops

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 35 / 98

slide-57
SLIDE 57

Optimistic Coloring

We miss many opportunities to avoid the stack

a b c d

Handle spills as if they were simplified (potential spills) then try to color them There might not be actual spills

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 36 / 98

slide-58
SLIDE 58

Optimistic Coloring

We miss many opportunities to avoid the stack

a b c d

Handle spills as if they were simplified (potential spills) then try to color them There might not be actual spills

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 36 / 98

slide-59
SLIDE 59

Optimistic Coloring

We miss many opportunities to avoid the stack

a b c d

Handle spills as if they were simplified (potential spills) then try to color them There might not be actual spills

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 36 / 98

slide-60
SLIDE 60

Optimistic Coloring

We miss many opportunities to avoid the stack

a b c d

Handle spills as if they were simplified (potential spills) then try to color them There might not be actual spills

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 36 / 98

slide-61
SLIDE 61

Coalescing

1

Interference Graph

2

Coloring by Simplification Spilling Coalescing Precolored Nodes Implementation

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 37 / 98

slide-62
SLIDE 62

Coalescing

Some low-level form of copy propagation While building traces we tried to remove jumps While allocating registers, we try to remove moves live-in: t2

t1 := ... t2 := t1 + t2 t3 := t2 t4 := t1 + t3 t2 := t3 + t4 t1 := t2 - t4

live-out: t1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 38 / 98

slide-63
SLIDE 63

Coalescing

Some low-level form of copy propagation While building traces we tried to remove jumps While allocating registers, we try to remove moves live-in: t2

t1 := ... t2 := t1 + t2 t3 := t2 t4 := t1 + t3 t2 := t3 + t4 t1 := t2 - t4

live-out: t1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 38 / 98

slide-64
SLIDE 64

Coalescing

Some low-level form of copy propagation While building traces we tried to remove jumps While allocating registers, we try to remove moves live-in: t2

t1 := ... t2 := t1 + t2 t3 := t2 t4 := t1 + t3 t2 := t3 + t4 t1 := t2 - t4

live-out: t1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 38 / 98

slide-65
SLIDE 65

Coalescing Improves the Coloralibility

t1 t2 t3 t4

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 39 / 98

slide-66
SLIDE 66

Coalescing Improves the Coloralibility

t1 t2 t3 t4

t1 t2&t3 t4

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 39 / 98

slide-67
SLIDE 67

Coalescing Improves the Coloralibility

t1 t2 t3 t4

t1 t2&t3 t4

t1 and t4 have one neighbor less!

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 39 / 98

slide-68
SLIDE 68

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-69
SLIDE 69

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-70
SLIDE 70

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-71
SLIDE 71

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-72
SLIDE 72

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-73
SLIDE 73

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-74
SLIDE 74

Yes, But Coalesce Who?

Conservative Coalescing: don’t make it harder. Coalesce a and b if Briggs ab has fewer than k neighbors of significant degree. George every neighbor of a is

  • f insignificant degree

already interfering with b

George’s criterion is well suited for real registers

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 40 / 98

slide-75
SLIDE 75

Interference Graph [Appel, 1998]

Four registers: r1, r2, r3, r4.

live in: k j g := [j + 12] h := k - 1 f := g * h e := [j + 8] m := [j + 16] b := [f] c := e + 8 d := c k := m + 4 j := b live out: d k j

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 41 / 98

slide-76
SLIDE 76

Interference Graph: Simplify 0

g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 42 / 98

slide-77
SLIDE 77

Interference Graph: Simplify 1

h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 43 / 98

slide-78
SLIDE 78

Interference Graph: Simplify 2

k h g

m c d b e j k f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 44 / 98

slide-79
SLIDE 79

Interference Graph: Simplify 3

c&d k h g

m c&d b j k e f h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 45 / 98

slide-80
SLIDE 80

Interference Graph: Simplify 4

j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 46 / 98

slide-81
SLIDE 81

Interference Graph: Simplify 5

c&d j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 47 / 98

slide-82
SLIDE 82

Interference Graph: Simplify 6

j&b c&d j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 48 / 98

slide-83
SLIDE 83

Interference Graph: Simplify 7

f j&b c&d j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 49 / 98

slide-84
SLIDE 84

Interference Graph: Simplify 8

m f j&b c&d j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 50 / 98

slide-85
SLIDE 85

Interference Graph: Simplify 9

e m f j&b c&d j&b c&d k h g

m c&d j&b k f e h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 51 / 98

slide-86
SLIDE 86

Interference Graph: Simplify 9

e m f j&b c&d j&b c&d k h g

m c&d j&b k f e:1 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 52 / 98

slide-87
SLIDE 87

Interference Graph: Simplify 8

m f j&b c&d j&b c&d k h g

m:2 c&d j&b k f e:1 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 53 / 98

slide-88
SLIDE 88

Interference Graph: Simplify 7

f j&b c&d j&b c&d k h g

m:2 c&d j&b k f:3 e:1 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 54 / 98

slide-89
SLIDE 89

Interference Graph: Simplify 6

j&b c&d j&b c&d k h g

m:2 c&d j&b:4 k f:3 e:1 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 55 / 98

slide-90
SLIDE 90

Interference Graph: Simplify 5

c&d j&b c&d k h g

m:2 c&d:1 j&b:4 k f:3 e:1 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 56 / 98

slide-91
SLIDE 91

Interference Graph: Simplify 4

j&b c&d k h g

m:2 c&d:1 b:4 j:4 k e:1 f:3 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 57 / 98

slide-92
SLIDE 92

Interference Graph: Simplify 3

c&d k h g

m:2 c:1 d:1 b:4 e:1 j:4 k f:3 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 58 / 98

slide-93
SLIDE 93

Interference Graph: Simplify 2

k h g

m:2 c:1 d:1 b:4 e:1 j:4 k:2 f:3 h g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 59 / 98

slide-94
SLIDE 94

Interference Graph: Simplify 1

h g

m:2 c:1 d:1 b:4 e:1 j:4 k:2 f:3 h:2 g

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 60 / 98

slide-95
SLIDE 95

Interference Graph: Simplify 0

g

m:2 c:1 d:1 b:4 e:1 j:4 k:2 f:3 h:2 g:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 61 / 98

slide-96
SLIDE 96

Interference Graph: Result

live in: k j g := [j + 12] h := k - 1 f := g * h e := [j + 8] m := [j + 16] b := [f] c := e + 8 d := c k := m + 4 j := b live out: d k j live in: r2 r4 r1 := [r4 + 12] r2 := r2 - 1 r3 := r1 * r2 r1 := [r4 + 8] r2 := [r4 + 16] r4 := [r3] r1 := r1 + 8 # r1 := r1 r2 := r2 + 4 # r4 := r4 live out: r1 r2 r4

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 62 / 98

slide-97
SLIDE 97

Precolored Nodes

1

Interference Graph

2

Coloring by Simplification Spilling Coalescing Precolored Nodes Implementation

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 63 / 98

slide-98
SLIDE 98

Hard Registers

Some nodes are precolored: the real registers

the stack pointer ($sp) the frame pointer ($fp) the argument registers ($a0, $a1, etc.) the return value ($v0, $v1) the return address ($ra) etc.

They all interfere with each other They cannot be simplified (infinite degree)

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 64 / 98

slide-99
SLIDE 99

Hard Registers

Some nodes are precolored: the real registers

the stack pointer ($sp) the frame pointer ($fp) the argument registers ($a0, $a1, etc.) the return value ($v0, $v1) the return address ($ra) etc.

They all interfere with each other They cannot be simplified (infinite degree)

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 64 / 98

slide-100
SLIDE 100

Hard Registers

Some nodes are precolored: the real registers

the stack pointer ($sp) the frame pointer ($fp) the argument registers ($a0, $a1, etc.) the return value ($v0, $v1) the return address ($ra) etc.

They all interfere with each other They cannot be simplified (infinite degree)

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 64 / 98

slide-101
SLIDE 101

Hard Registers

Some nodes are precolored: the real registers

the stack pointer ($sp) the frame pointer ($fp) the argument registers ($a0, $a1, etc.) the return value ($v0, $v1) the return address ($ra) etc.

They all interfere with each other They cannot be simplified (infinite degree)

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 64 / 98

slide-102
SLIDE 102

Hard Registers

Some nodes are precolored: the real registers

the stack pointer ($sp) the frame pointer ($fp) the argument registers ($a0, $a1, etc.) the return value ($v0, $v1) the return address ($ra) etc.

They all interfere with each other They cannot be simplified (infinite degree)

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 64 / 98

slide-103
SLIDE 103

Callee & Caller Save Registers

It just rocks! Caller Save Def’d by calls. Callee Save Def’d at entry, used at exit of functions. Register pressure will push temporaries live across calls into callee save.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 65 / 98

slide-104
SLIDE 104

Callee & Caller Save Registers

It just rocks! Caller Save Def’d by calls. Callee Save Def’d at entry, used at exit of functions. Register pressure will push temporaries live across calls into callee save.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 65 / 98

slide-105
SLIDE 105

Callee & Caller Save Registers

It just rocks! Caller Save Def’d by calls. Callee Save Def’d at entry, used at exit of functions. Register pressure will push temporaries live across calls into callee save.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 65 / 98

slide-106
SLIDE 106

Callee & Caller Save Registers

It just rocks! Caller Save Def’d by calls. Callee Save Def’d at entry, used at exit of functions. Register pressure will push temporaries live across calls into callee save.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 65 / 98

slide-107
SLIDE 107

Conflicts

Minimize the conflicts (“pressure”) with hard regs. Source and sink.

# Routine: fact l0: # def $s0, $s1... move $x11, $s0 # def: $x11 use: $s0 move $x12, $s1 # def: $x12 use: $s1 ... l6: move $s0, $x11 # def: $s0 use: $x11 move $s1, $x12 # def: $s1 use: $x12 ... # use: $fp, $ra, $sp, # ... $v0, $zero

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 66 / 98

slide-108
SLIDE 108

Example [Appel, 1998]

int f (int a, int b) { int d = 0; int e = a; do { d += b;

  • -e;

} while (e > 0); return d; }

enter: c := r3 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d r3 := c return # liveout: r1, r3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 67 / 98

slide-109
SLIDE 109

Example

enter: c := r3 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d r3 := c return # liveout: r1, r3

r1 r2 a c d r3 b e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 68 / 98

slide-110
SLIDE 110

Interference Graph: Simplify 0

c

r1 r2 a c d r3 b e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 69 / 98

slide-111
SLIDE 111

Interference Graph: Simplify 1

a&e c

r1 r2 c d r3 b a&e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 70 / 98

slide-112
SLIDE 112

Interference Graph: Simplify 2

b&r2 a&e c

r1 c d b&r2 r3 a&e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 71 / 98

slide-113
SLIDE 113

Interference Graph: Simplify 3

a&e&r1 b&r2 a&e c

a&e&r1 b&r2 r3 c d

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 72 / 98

slide-114
SLIDE 114

Interference Graph: Simplify 4

d a&e&r1 b&r2 a&e c

a&e&r1 b&r2 r3 c d

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 73 / 98

slide-115
SLIDE 115

Interference Graph: Simplify 4

d a&e&r1 b&r2 a&e c

a&e&r1 b&r2 r3 c d:3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 74 / 98

slide-116
SLIDE 116

Interference Graph: Simplify 3

a&e&r1 b&r2 a&e c

r1:1 c d:3 b&r2 r3 a&e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 75 / 98

slide-117
SLIDE 117

Interference Graph: Simplify 2

b&r2 a&e c

r1:1 r2:2 c d:3 r3 b:2 a&e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 76 / 98

slide-118
SLIDE 118

Interference Graph: Simplify 1

a&e c

r1:1 r2:2 a:1 c d:3 r3 b:2 e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 77 / 98

slide-119
SLIDE 119

Interference Graph: Simplify 0

c

r1:1 r2:2 a:1 c:4 d:3 r3 b:2 e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 78 / 98

slide-120
SLIDE 120

Spilling

enter: c := r3 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d r3 := c return # liveout: r1, r3 enter: c1 := r3 [sp+8] := c1 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d c2 := [sp+8] r3 := c2 return # liveout: r1, r3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 79 / 98

slide-121
SLIDE 121

Example

enter: c1 := r3 [sp+8] := c1 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d c2 := [sp+8] r3 := c2 return # liveout: r1, r3

r1 r2 a d r3 b e c1 c2

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 80 / 98

slide-122
SLIDE 122

Interference Graph: Simplify 0

c1&r3

r1 r2 a d b c1&r3 c2 e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 81 / 98

slide-123
SLIDE 123

Interference Graph: Simplify 1

c1&r3&c2 c1&r3

r1 r2 a d b c1&r3&c2 e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 82 / 98

slide-124
SLIDE 124

Interference Graph: Simplify 2

a&e c1&r3&c2 c1&r3

r1 r2 d b c1&r3&c2 a&e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 83 / 98

slide-125
SLIDE 125

Interference Graph: Simplify 3

b&r2 a&e c1&r3&c2 c1&r3

r1 d b&r2 c1&r3&c2 a&e

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 84 / 98

slide-126
SLIDE 126

Interference Graph: Simplify 4

a&e&r1 b&r2 a&e c1&r3&c2 c1&r3

a&e&r1 b&r2 c1&r3&c2 d

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 85 / 98

slide-127
SLIDE 127

Interference Graph: Simplify 5

d a&e&r1 b&r2 a&e c1&r3&c2 c1&r3

a&e&r1 b&r2 c1&r3&c2 d

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 86 / 98

slide-128
SLIDE 128

Interference Graph: Simplify 5

d a&e&r1 b&r2 a&e c1&r3&c2 c1&r3

a&e&r1 b&r2 c1&r3&c2 d:3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 87 / 98

slide-129
SLIDE 129

Interference Graph: Simplify 4

a&e&r1 b&r2 a&e c1&r3&c2 c1&r3

r1:1 d:3 b&r2 c1&r3&c2 a&e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 88 / 98

slide-130
SLIDE 130

Interference Graph: Simplify 3

b&r2 a&e c1&r3&c2 c1&r3

r1:1 r2:2 d:3 b:2 c1&r3&c2 a&e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 89 / 98

slide-131
SLIDE 131

Interference Graph: Simplify 2

a&e c1&r3&c2 c1&r3

r1:1 r2:2 a:1 d:3 b:2 c1&r3&c2 e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 90 / 98

slide-132
SLIDE 132

Interference Graph: Simplify 1

c1&r3&c2 c1&r3

r1:1 r2:2 a:1 d:3 b:2 c1&r3:3 c2:3 e:1

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 91 / 98

slide-133
SLIDE 133

Interference Graph: Simplify 0

c1&r3

r1:1 r2:2 a:1 d:3 r3:3 b:2 e:1 c1:3 c2:3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 92 / 98

slide-134
SLIDE 134

Result

enter: c1 := r3 [sp+8] := c1 a := r1 b := r2 d := 0 e := a loop: d := d + b e := e - 1 if e > 0 goto loop r1 := d c2 := [sp+8] r3 := c2 return # liveout: r1, r3 enter: r3 := r3 [sp+8] := r3 r1 := r1 r2 := r2 r3 := 0 r1 := r1 loop: r3 := r3 + r2 r1 := r1 - 1 if r1 > 0 goto loop r1 := r3 r3 := [sp+8] r3 := r3 return # liveout: r1, r3 enter: [sp+8] := r3 r3 := 0 loop: r3 := r3 + r2 r1 := r1 - 1 if r1 > 0 goto loop r1 := r3 r3 := [sp+8] return # liveout: r1, r3

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 93 / 98

slide-135
SLIDE 135

Implementation

1

Interference Graph

2

Coloring by Simplification Spilling Coalescing Precolored Nodes Implementation

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 94 / 98

slide-136
SLIDE 136

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-137
SLIDE 137

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-138
SLIDE 138

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-139
SLIDE 139

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-140
SLIDE 140

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-141
SLIDE 141

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

Use both! For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-142
SLIDE 142

Implementation

Naive implementation is quadratic Lower with heavy use of worklists Queries on the conflict graph

Iterate over neighbors, hence adjacency list Existence of an edge between two nodes, hence bit matrix.

Use both! For more information, see [Appel, 1998].

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 95 / 98

slide-143
SLIDE 143

Alternatives to Graph Coloring

1

Interference Graph

2

Coloring by Simplification

3

Alternatives to Graph Coloring

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 96 / 98

slide-144
SLIDE 144

Register Allocation for Trees

Can be done during instruction selection with maximal munch

function SimpleAlloc (t) for each nontrivial tile u child of t SimpleAlloc (u) for each nontrivial tile u child of t n := n - 1 n := n + 1 assign rn to (the root of) t

[Appel, 1998]

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 97 / 98

slide-145
SLIDE 145

Bibliography I

Appel, A. W. (1998). Modern Compiler Implementation in C, Java, ML. Cambridge University Press. Briggs, P. (1992). Register Allocation via Graph Coloring. PhD thesis, Rice University, Houston, Texas. Matz, M. (2003). Design and Implementation of a Graph Coloring Register Allocator for gcc. pages 151–169.

  • A. Demaille, E. Renault, R. Levillain

Register Allocation 98 / 98