A Study of Spilling and Coalescing in Register Allocation as Two

Apr 30, 2009 - Problem. Graph-k-coloring is NP-complete (for k ≥ 3), and any interference graph can arise in programs. (Chaitin et al.'s proof). « register ...
4MB taille 1 téléchargements 224 vues
Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

A Study of Spilling and Coalescing in Register Allocation as Two Separate Phases Ph.D. Defense Florent Bouchez [email protected] Compsys LIP UMR CNRS — Inria — ENS Lyon — UCBL France

April 30th 2009

1 / 39 BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Compilation

foo.c

foo.bin

Our interest Register allocation, one of the latest steps.

2 / 39 ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

French folklore: Les Shadoks

3 / 39 MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Shadoks have a very small brain

4 / 39 BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The shadoks at the library

5 / 39 BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is register allocation? a ← 3425 n←0

Assign variables to memory locations ˆ Registers:

,

,

a 6= 1 ?

, ...

ˆ Memory: infinite

n ←n+1 a even ?

Rules of the game ˆ two interfering variables

à different registers

a ← a/2

a←3×a+1

ˆ not enough registers

à spill to memory print n BU ZO

6 / 39

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is register allocation?

Assign variables to memory locations ˆ Registers:

,

,

, ...

ˆ Memory: infinite

Rules of the game ˆ two interfering variables

à different registers

Plus constraints: ˆ register constraints ˆ pre-colored variables ˆ register pairing,

aliasing,. . .

ˆ not enough registers

à spill to memory 6 / 39 BU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin et al. model k j Live-ranges Live-in: k j g := mem[j+12] h := k-1 f := g+h e := mem[j+8] m := mem[j+16] b := mem[f] c := e+8 d := c k := m+4 j := b Live-out: d k j

g h f e m b c d k j 7 / 39 BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin et al. model Interference graph k j

k

g

Live-ranges g

h

h

j

f

e

e

f

m m

b

b c

c d

d k j

7 / 39 BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin et al. model Interference graph k j

k

g

Live-ranges g

h

h

j

f

e

e

f

m m

b

b c

c d

d k j

7 / 39 BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Coloring the interference graph: the greedy way Register allocation is modeled as coloring the interference graph of the program.

Problem Graph-k-coloring is NP-complete (for k ≥ 3), and any interference graph can arise in programs. (Chaitin et al.’s proof) à register allocation is NP-complete in this model.

8 / 39 ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Coloring the interference graph: the greedy way Register allocation is modeled as coloring the interference graph of the program.

Problem Graph-k-coloring is NP-complete (for k ≥ 3), and any interference graph can arise in programs. (Chaitin et al.’s proof) à register allocation is NP-complete in this model. A greedy coloring heuristic is used: Chaitin et al.’s greedy scheme. Greedy scheme

If coloring fails, usually spill.

8 / 39 ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Coloring the interference graph: the greedy way Register allocation is modeled as coloring the interference graph of the program.

Problem Graph-k-coloring is NP-complete (for k ≥ 3), and any interference graph can arise in programs. (Chaitin et al.’s proof) à register allocation is NP-complete in this model. A greedy coloring heuristic is used: Chaitin et al.’s greedy scheme. Greedy scheme

If coloring fails, usually spill. Disadvantages: ˆ The scheme might fail even when there is a solution. ˆ A variable is supposed to be in exactly one register. 8 / 39 ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Coloring the interference graph: the greedy way Register allocation is modeled as coloring the interference graph of the program.

Problem Graph-k-coloring is NP-complete (for k ≥ 3), and any interference graph can arise in programs. (Chaitin et al.’s proof) à register allocation is NP-complete in this model. A greedy coloring heuristic is used: Chaitin et al.’s greedy scheme. Greedy scheme

If coloring fails, usually spill. Disadvantages: ˆ The scheme might fail even when there is a solution. à need to spill more than necessary ˆ A variable is supposed to be in exactly one register. à restriction on the coloring ZO GA

8 / 39

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Outline 1

Introduction Les Shadoks Register allocation

2

Difficulty of register allocation Strict SSA graphs are chordal Questions with register allocation in two phases

3

The coalescing problem Introduction to coalescing Incremental coalescing

9 / 39 ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Outline 1

Introduction Les Shadoks Register allocation

2

Difficulty of register allocation Strict SSA graphs are chordal Questions with register allocation in two phases

3

The coalescing problem Introduction to coalescing Incremental coalescing

10 / 39 ZO ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

SSA interference graphs are chordal In 2002, Darte realized that the interference graph of programs under strict SSA are chordal. (The same discovery was made independently by Brisk and Hack in 2004-2005.)

à Starting point of this thesis

Theorem The interference graph of a program under strict SSA form is chordal.

11 / 39 ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

SSA interference graphs are chordal In 2002, Darte realized that the interference graph of programs under strict SSA are chordal. (The same discovery was made independently by Brisk and Hack in 2004-2005.)

à Starting point of this thesis

Theorem The interference graph of a program under strict SSA form is chordal.

11 / 39 ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord.

a

b

c

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a

c

b

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs Definition (Chordal graph) A graph is chordal iff every cycle of size ≥ 4 has a chord. a b e c d

Chordal graphs are perfect and easy to color. 12 / 39 MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Static Single Assignment SSA : exactly one textual definition per variable

13 / 39 MEU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Static Single Assignment SSA : exactly one textual definition per variable

Example (Straight code converted to SSA form) a ← ... . . . ... ← a . . . a ← ... . . . ... ← a

a1 ← . . . . . . . . . ← a1 . . . a2 ← . . . . . . . . . ← a2

13 / 39 MEU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Static Single Assignment SSA : exactly one textual definition per variable

Example (Conditional code converted to SSA form) if (. . . )

if (. . . ) a←1

a←2 ... ← a

a1 ← 1

a2 ← 2

a3 ← φ(a1 , a2 ) . . . ← a3 13 / 39 MEU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Static Single Assignment SSA : exactly one textual definition per variable strictness : SSA where the definition always dominates its uses

Example (strict SSA or SSA with dominance property) if (. . . )

if (. . . ) a1 ← 1 . . . ← a1

...

a1 ← 1

...

a2 ← φ(a1 , ⊥) . . . ← a2 13 / 39 MEU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a b e c d

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a

ˆ dominance property

b e c d

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a

ˆ dominance property

b e

ˆ a and b interfere ⇒ def (a) dominates def (b) (or the converse)

c d

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a

ˆ dominance property

b e

ˆ a and b interfere ⇒ def (a) dominates def (b) (or the converse) ˆ direct each edge with dominance

c d

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a

ˆ dominance property

b e

ˆ a and b interfere ⇒ def (a) dominates def (b) (or the converse) ˆ direct each edge with dominance ˆ def (d) is dominated by def (c) and def (e)

c d

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Proof that strict SSA interference graphs are chordal Theorem The interference graph of a program under strict SSA form is chordal.

Proof. a

ˆ dominance property

b e

ˆ a and b interfere ⇒ def (a) dominates def (b) (or the converse) ˆ direct each edge with dominance ˆ def (d) is dominated by def (c) and def (e)

c d

ˆ c and e are live at def (d)

14 / 39 MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Strict SSA programs are easy to color Chordal graphs are perfect graphs, hence easy to color. We proved more:

Theorem Chordal graphs are colorable using Chaitin et al. greedy scheme. They are greedy-k-colorable. General program: NP-complete

strict SSA program: greedy-k-colorable

15 / 39 MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Strict SSA programs are easy to color Chordal graphs are perfect graphs, hence easy to color. We proved more:

Theorem Chordal graphs are colorable using Chaitin et al. greedy scheme. They are greedy-k-colorable. General program: NP-complete

strict SSA program: greedy-k-colorable

Under strict SSA, Maxlive, the maximum number of simultaneously live variables, is the coloring indicator: Maxlive ≤ R

15 / 39 MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Register allocation in two phases Using Maxlive, it seems possible to use a very simple register allocation scheme: 1

spill variables until Maxlive ≤ R

2

transform program into strict SSA form

3

allocate variables using R registers

4

go out of colored SSA form

16 / 39 BU GA GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Register allocation in two phases Using Maxlive, it seems possible to use a very simple register allocation scheme: 1

spill variables until Maxlive ≤ R

2

transform program into strict SSA form

3

allocate variables using R registers

4

go out of colored SSA form

Questions SSA seems to transform an NP-complete problem into polynomial one. . . Where is the complexity now? What else is simplified?

16 / 39 BU GA GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

SSA = chordal (2002)

Coalescing (out-of-SSA)

Chaitin’s proof

spilling simplified by SSA? 17 / 39 BU GA BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

critical flow-edge splitting

SSA = chordal (2002)

revisit Chaitin’s Chaitin’s proof proof

swap problem

Coalescing (out-of-SSA)

# simult. defs

spilling simplified by SSA? 17 / 39 BU GA BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin’s proof revisited

Pro

of

it es spl edg e l l a iab tic var cri

g

tin

y xit

le mp

co NP-complete

Chaitin et al.

No

Yes

18 / 39 BU GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin’s proof revisited

Pro

of

it es spl edg e l l a iab tic var cri

g

tin

y xit

le mp

co NP-complete

Chaitin et al.

NP-complete

Thm. 3.1

No

Yes

18 / 39 BU GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin’s proof revisited

Pro

of

ng ion itti truct es l g p ed es ins cal abl wap i i t r i va S cr

Chaitin et al.

co NP-complete NP-complete

Thm. 3.1 Sec. 3.2.1 (SSA)

polynomial

*

No * on basic block borders only

y xit

le mp

Yes

18 / 39 BU GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin’s proof revisited

Pro

of

ng ion itti truct es l g p ed es ins cal abl wap i i t r i va S cr

Chaitin et al.

co NP-complete NP-complete

Thm. 3.1 Sec. 3.2.1 (SSA)

*

Thm. 3.2

*

No * on basic block borders only

y xit

le mp

polynomial NP-complete

Yes

18 / 39 BU GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chaitin’s proof revisited

Pro

of

Chaitin et al.

ng ion . itti truct es l g def p ty d s s . e e lt l in exi l l u a b p p c ia a im ti var Sw com cri 2s NP-complete NP-complete

Thm. 3.1 Sec. 3.2.1 (SSA)

*

Thm. 3.2

*

polynomial NP-complete polynomial

Thm. 3.4 (Col. prop.)

NP-complete

Thm. 3.3 No * on basic block borders only

Yes

18 / 39 BU GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

critical flow-edge splitting

SSA = chordal (2002)

revisit Chaitin’s Chaitin’s proof proof

swap problem

Coalescing (out-of-SSA)

# simult. defs

spilling simplified by SSA? 19 / 39 BU GA MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

π-motion

critical flow-edge splitting

SSA = chordal (2002)

revisit Chaitin’s Chaitin’s proof proof

swap problem

Coalescing (out-of-SSA)

# simult. defs

spilling simplified by SSA? 19 / 39 BU GA MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not.

20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not. Short answer: No

20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not. Short answer: No Longer answer: weighted Chordal graph = SSA case Interval graph = basic block

Ω0 ≤ C

Ω0 ≤ R

Ω0 ≤ Ω − 1 X3C

no yes

dyn. prog.

no

furthest use

yes

ILP

polynomial

NP-complete

dyn. prog. new result

20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not. Short answer: No Longer answer: weighted Chordal graph = SSA case Interval graph = basic block

Ω0 ≤ C

Ω0 ≤ R

Ω0 ≤ Ω − 1 X3C

no yes

dyn. prog.

no

furthest use

yes

ILP

polynomial

dyn. prog. new result

NP-complete

Previous studies by Belady, Yannakakis&Gavril, and Liberatore. 20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not. Short answer: No

The problem of “holes.” On RISC architectures, spilled variables leave small intervals behind.

a ← ... . . . ... ← a . . . ... ← a

a1 ← . . . store a1 . . . spill a

a2 ← load . . . ← a2 . . . a3 ← load . . . ← a3 20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Spilling under SSA is still a difficult problem In this thesis, we studied the complexity of spill everywhere under SSA to know whether it would simplify the problem or not. Short answer: No With “holes,” it becomes harder even on a basic block: weight Ω0 ≤ C Ω0 ≤ R Ω0 ≤ Ω − C no ? h=1 yes stable set no stable set h≥2 yes dyn. prog. no h not bounded yes dyn. prog. polynomial

NP-complete

Ω0 ≤ Ω − 1

set cover

new result 20 / 39 BU BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

π-motion

critical flow-edge splitting

SSA = chordal (2002)

revisit Chaitin’s Chaitin’s proof proof

swap problem

Coalescing (out-of-SSA)

# simult. defs

spilling spilling NOT simplified by by SSA? SSA 21 / 39 BU BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases

π-motion

critical flow-edge splitting

SSA = chordal (2002)

revisit Chaitin’s Chaitin’s proof proof

swap problem

edge splitting π-motion

Coalescing (out-of-SSA)

# simult. defs

spilling spilling NOT simplified by by SSA? SSA

complexity

heuristics

21 / 39 BU BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Outline 1

Introduction Les Shadoks Register allocation

2

Difficulty of register allocation Strict SSA graphs are chordal Questions with register allocation in two phases

3

The coalescing problem Introduction to coalescing Incremental coalescing

22 / 39 BU BU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is coalescing? Goal of coalescing Removing the register-to-register copies [move a ← b]

Numerous move due to: ˆ live-range splitting to

avoid spilling

23 / 39 BU BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is coalescing? Goal of coalescing Removing the register-to-register copies [move a ← b]

Numerous move due to: ˆ live-range splitting to

avoid spilling ˆ register constraints

a ← ... b ← ... c ← f (a, b)

a ← ... b ← ... move R0 ,a move R1 ,b call f move c,R0

23 / 39 BU BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is coalescing? Goal of coalescing Removing the register-to-register copies [move a ← b]

Numerous move due to:

if (. . . )

ˆ live-range splitting to

avoid spilling ˆ register constraints

a1 ← 1

a2 ← 2

ˆ SSA destruction

a3 ← φ(a1 , a2 ) · · · ← a3 23 / 39 BU BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is coalescing? Goal of coalescing Removing the register-to-register copies [move a ← b]

Numerous move due to:

if (. . . )

ˆ live-range splitting to

avoid spilling ˆ register constraints ˆ SSA destruction

a1 ← 1 move a3 ← a1

a2 ← 2 move a3 ← a2

a3 ← φ(a1 , a2 ) · · · ← a3 23 / 39 BU BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

What is coalescing? Goal of coalescing Removing the register-to-register copies [move a ← b]

Numerous move due to:

if (. . . )

ˆ live-range splitting to

avoid spilling ˆ register constraints ˆ SSA destruction

Not a new problem, but never studied not intermixed with spilling.

a1 ← 1 move a3 ← a1

a2 ← 2 move a3 ← a2

a3 ← φ(a1 , a2 ) · · · ← a3 23 / 39 BU BU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Traditional modeling of the coalescing problem Given an instruction [move a ← b] Fact I Giving the same color to both a and b saves the instruction. Fact II Merging nodes a and b forces them to have the same color.

24 / 39 BU ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Traditional modeling of the coalescing problem Given an instruction [move a ← b] Fact I Giving the same color to both a and b saves the instruction. Fact II Merging nodes a and b forces them to have the same color.

a

d

d

Merge a and b b

ab

c

c

24 / 39 BU ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Traditional modeling of the coalescing problem Given an instruction [move a ← b] Fact I Giving the same color to both a and b saves the instruction. Fact II Merging nodes a and b forces them to have the same color. Idea Express this as an “affinity” between a and b in the interference graph to drive the algorithm. a

d

d

Merge a and b b

ab

c

c

24 / 39 BU ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Traditional modeling of the coalescing problem Given an instruction [move a ← b] Fact I Giving the same color to both a and b saves the instruction. Fact II Merging nodes a and b forces them to have the same color. Idea Express this as an “affinity” between a and b in the interference graph to drive the algorithm. a

d

d

Merge a and b b

ab

c

c

We work on graphs instead of programs. 24 / 39 BU ZO GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

Incremental

Optimistic

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

Coalesce as many affinities as possible.

Incremental

Optimistic

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive s3

Conservative v

s1

v

s3 w

u

w

u

s2 edge removed

s1

s2

affinity not coalesced

multiway-cut

Incremental

Optimistic

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive s3

Conservative v

s1

v

s3 w

u

w

u

s2 edge removed

s1

s2

affinity not coalesced

Coalesce as many affinities as possible but remains k-colorable.

multiway-cut

Incremental

Optimistic

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative xhd,bi

s3 u s1

v

v

s3

w u s2 edge removed

d

w s1

s2

affinity not coalesced

e c

b a

yhc,di

e xhc,di

c

b xhb,ai

yhc,ei xhc,ei

yha,ci yhb,ai

multiway-cut

d

yhd,bi

a

xha,ci

graph k-coloring

Incremental

Optimistic

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative xhd,bi

s3 u s1

v

v

s3

w u s2 edge removed

d

w s1

s2

affinity not coalesced

e c

b a

yhc,di

e xhc,di

c

b xhb,ai

yhc,ei xhc,ei

yha,ci yhb,ai

multiway-cut

d

yhd,bi

a

xha,ci

graph k-coloring

Incremental

Optimistic Perform aggressive coalescing, then de-coalescing to get k-colorable.

25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

v2

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

d

xhd,bi

s3 u s1

v

v

s3

w u s2 edge removed

d

w s1

s2

affinity not coalesced

e

xhc,di

c

xhb,ai

a

e

cv

b

c

b

yhc,di

yhd,bi

xhc,ei

yha,ci yhb,ai

multiway-cut

yhc,ei

xha,ci

a

graph k-coloring

Incremental

Optimistic v2

v1 cv

c0v cv

v3

v3

3-vertex-cover 25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

v2

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

d

xhd,bi

s3 u s1

v

v

s3

w u s2 edge removed

d

w s1

s2

affinity not coalesced

e

xhc,di

c

xhb,ai

a

e

cv

b

c

b

yhc,di

yhd,bi

xhc,ei

yha,ci yhb,ai

multiway-cut

yhc,ei

xha,ci

a

graph k-coloring

Incremental

Optimistic v2

Coalesce one affinity while staying k-colorable.

v1 cv

c0v cv

v3

v3

3-vertex-cover 25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

v2

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

d

xhd,bi

v

s3

s3

w u s2 edge removed

u s1

v

d

w s2

s1

affinity not coalesced

e

xhc,di

c

xhb,ai

a

e

cv

b

c

b

yhc,di

yhd,bi

xhc,ei

yha,ci yhb,ai

multiway-cut

yhc,ei

xha,ci

a

graph k-coloring

Optimistic

Incremental yi,1

ai,1

v2

bi,1 yi,2

ai,2

yi,3

ai,3

v1

ci,1 F T

cv c0v

R yi,4

ai,4

bi,2

ci,2

3-SAT

cv

v3

v3

3-vertex-cover 25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

v2

The coalescing problem

Conclusion

Different coalescing problems Aggressive

Conservative

d

xhd,bi

v

s3

s3

w u s2 edge removed

u s1

v

d

w s2

s1

affinity not coalesced

e

xhc,di

c

xhb,ai

a

e

cv

b

c

b

yhc,di

yhd,bi

xhc,ei

yha,ci yhb,ai

multiway-cut

yhc,ei

xha,ci

a

graph k-coloring

Optimistic

Incremental yi,1

ai,1

v2

bi,1 yi,2

ai,2

yi,3

ai,3

v1

ci,1 F T

cv c0v

R yi,4

ai,4

bi,2

ci,2

3-SAT

NP-complete for general graphs. Polynomial for chordal graphs.

cv

v3

v3

3-vertex-cover 25 / 39 BU ZO BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

The problem of incremental coalescing The goal of incremental is to perform conservative coalescing by coalescing affinities one by one.

Problem (Incremental coalescing) Given a k-colorable graph G and two nodes x and y , is it possible to color G such that x and y have the same color?

Theorem The incremental coalescing problem is NP-complete.

26 / 39 BU ZO ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

ˆ 3 nodes for True, False, and X

F

X

27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F x

ˆ 3 nodes for True, False, and X

X

ˆ 2 nodes for each variable: v and v¯



27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F x

ˆ 3 nodes for True, False, and X

X



ˆ 2 nodes for each variable: v and v¯

y y¯ 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F x

ˆ 3 nodes for True, False, and X

X



ˆ 2 nodes for each variable: v and v¯

y y¯ z¯

z 27 / 39

BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F x

ˆ 3 nodes for True, False, and X

X



ˆ 2 nodes for each variable: v and v¯

y

w ¯ w

y¯ z¯

z 27 / 39

BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F

v¯ ˆ 3 nodes for True, False, and X ˆ 2 nodes for each variable: v and v¯

x X

v

x¯ y

w ¯ w

y¯ z¯

z 27 / 39

BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). First, equivalence between graph-3-coloring and 4-SAT. T

F

v¯ ˆ 3 nodes for True, False, and X ˆ 2 nodes for each variable: v and v¯ ˆ . . . and a widget to forbid every

variable of a clause to be false

x X

v

x¯ y

w ¯ w

y¯ z¯

z 27 / 39

BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w Need a widget that is 3-colorable only if not all 4 variables are false. 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w Need a widget that is 3-colorable only if not all 4 variables are false. 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w If all 4 variables are false, not 3-colorable. 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w If at least one variable is true 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w If at least one variable is true 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). x F

y T z¯

X

w If at least one variable is true, 3-colorable. 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). Now, transform 3-SAT instance into 4-SAT by adding x0 to every clause: (y ∨ z¯ ∨ w ) ∧ · · · ∧ (z ∨ y¯ ∨ u) becomes (x0 ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (x0 ∨ z ∨ y¯ ∨ u) Clearly, x0 =True satisfies the equation (i.e., the graph is 3-colorable).

27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). Now, transform 3-SAT instance into 4-SAT by adding x0 to every clause: (y ∨ z¯ ∨ w ) ∧ · · · ∧ (z ∨ y¯ ∨ u) becomes (x0 ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (x0 ∨ z ∨ y¯ ∨ u) Clearly, x0 =True satisfies the equation (i.e., the graph is 3-colorable). Now, ask x0 and False to be coalesced. . . 27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing is NP-complete in the general case Reduction from 3-SAT. (Similar to reduction of graph-3-coloring from 3-SAT).

Example on (x ∨ y ∨ z¯ ∨ w ) ∧ · · · ∧ (¯ x ∨ z ∨ y¯ ∨ u). To conclude:

3-SAT is true

⇐⇒

4-SAT is true with x0 = False

⇐⇒

graph is 3-colorable with x0 in red/False

⇐⇒

incremental coalescing of x0 with False is possible

27 / 39 BU ZO MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Incremental coalescing for chordal graphs Problem (Incremental coalescing for chordal graphs) Given a k-colorable chordal graph G and two nodes x and y . Is it possible to color G such that x and y have the same color?

This problem is polynomial!

Moreover, if the answer is yes, it is possible to modify G so that x and y are merged and G stays chordal. The same question with greedy-k-colorable graphs is still open. 28 / 39 BU MEU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Example on a chordal graph Let us consider a 3-colorable chordal graph.

Demo: graph in ubigraph

29 / 39 BU MEU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs as subtrees of a tree

30 / 39 BU MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs as subtrees of a tree

30 / 39 BU MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Chordal graphs as subtrees of a tree

30 / 39 BU MEU ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

y x

31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Finding a path on the interval graph Once the branches are pruned, an interval graph remains.

x

y 31 / 39 BU MEU MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

A simple algorithmic strategy for chordal incremental coalescing

Building the representation of a chordal graph as subtrees of a tree is painful. We have devised an algorithmic strategy that works directly on the graph, using the same ideas as in Chaitin et al.’s greedy coloring algorithm.

32 / 39 ZO GA GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Demonstration of coalescing Demonstration of conservative coalescing on graph #311 of the “Coalescing Challenge.” (Appel&George)

33 / 39

Demo: conservative coalescing

ZO GA BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Questions raised by register allocation in two phases not published

critical flow-edge splitting

π-motion

SSA = chordal (2002)

edge splitting π-motion

WDDD’06 revisit Chaitin’s Chaitin’s proof proof

Coalescing (out-of-SSA)

LCPC’06 (ext) swap problem

# simult. defs

LCTES’07 spilling spilling NOT simplified by by SSA? SSA

CGO’07 (best paper)

CASES’08

complexity

heuristics

34 / 39 ZO GA ZO

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Conclusion: practical work going on

This work was part of a collaboration with STMicroelectronics in Grenoble. Practical experiments were made during this thesis (coalescing). But a lot more are going on, including: ˆ including Hack’s spilling in LAO ˆ experimenting new coalescing strategies in LAO ˆ experimenting the permutation motion to avoid splitting edges

35 / 39 ZO GA MEU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Conclusion Why are those results important for register allocation? The idea of separating register allocation in two phases has been around for a while: ˆ spilling is the most important issue ˆ splitting helps (Briggs, Cooper) but copies are hard to remove ˆ greedy coloring is not a good indicator for spilling

Assuming that some copies are always better than a spill: ˆ Maxlive is an “oracle” for spilling ˆ spilling can be optimized first so that Maxlive ≤ R ˆ good coalescing strategies: do not worry about splitting anymore

36 / 39 ZO BU GA

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

Remaining problems to work on ˆ continuing the work on permutation motion ˆ improving the spilling as a first phase ˆ having a better “oracle” that handles registers constraints

(Pereira&Palsberg) ˆ register allocation in two phases for JIT ˆ Maxlive indicator can be used for better instruction scheduling

Bonus: what will be written left to the pumping shadoks in next slide?

37 / 39 ZO BU BU

Introduction

Difficulty of register allocation

The coalescing problem

Conclusion

That’s all for today

Answer: ZO BU ZO 38 / 39 ZO BU ZO

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j

Stack

2

d0 1

c0

c

4

4

b

3

d

e

3

4

f 2

g0

1

3

i g

2 Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j 2

Stack

d0 1

c0

j

c

4

4

b

3

d

e

3

4

f 2

g0

2

i g

2 Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j 2

Stack

d0 1

c0 g j

c

4

4

b

3

d

e

3

3

f 2

g0

1

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j 2

Stack

d0 1

c0 i g j

c

4

4

b

3

d

e

3

2

f 2

g0

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j 2

Stack

d0 1

f i g j

c0

c

4

4

b

3

d

e

2

f 1

g0

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j

Stack

2

d0 g0 f i g j

1

c0

c

4

4

b

3

d

e

1

g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j

Stack

2

d0 e g0 f i g j

1

c0

c

4

4

b

2

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

3

j

Stack

2

d0 d e g0 f i g j

1

c0

c

3

3

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a d0 d e g0 f i g j

3

j d0

1

c0

c

2

2

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a c d0 d e g0 f i g j

2

j d0

1

c0

c

1

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a b c d0 d e g0 f i g j

1

j d0

1

c0

c

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al.

Stack

Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

a b c d0 d e g0 f i g j

a

j d0

c0

c

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al.

Stack

Greedy-k-colorable-graphs: simplify nodes with < k neighbors. c0 a b c d0 d e g0 f i g j

a

j d0

c0

c

b

d

e g0

f

i g Return

Hi-degree node Low-degree node 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al.

Stack

Greedy-k-colorable-graphs: simplify nodes with < k neighbors. c0 a b c d0 d e g0 f i g j

a

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al.

Stack

Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

a b c d0 d e g0 f i g j

a

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a b c d0 d e g0 f i g j

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a c d0 d e g0 f i g j

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a d0 d e g0 f i g j

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a

j d0

d e g0 f i g j

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a

j d0

e g0 f i g j

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a

j d0

g0 f i g j

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

j

Stack

d0

f i g j

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

j

Stack

d0 c0

c

b

d

e g0

f

i g Return

i g j 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

j

Stack

d0 c0

c

b

d

e g0

f

i g Return

g j 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors. a

j

Stack

d0 c0

c

b

d

e g0

f

i g Return

j 39 / 39 ZO BU MEU

Greedy coloring scheme of Chaitin et al. Greedy-k-colorable-graphs: simplify nodes with < k neighbors.

Stack

a

j d0

c0

c

b

d

e g0

f

i g Return

39 / 39 ZO BU MEU