SSA-Form Register Allocation Foundations Sebastian Hack Compiler - - PowerPoint PPT Presentation

ssa form register allocation
SMART_READER_LITE
LIVE PREVIEW

SSA-Form Register Allocation Foundations Sebastian Hack Compiler - - PowerPoint PPT Presentation

SSA-Form Register Allocation Foundations Sebastian Hack Compiler Construction Course Winter Term 2009/2010 saarland university computer science Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance -functions 3


slide-1
SLIDE 1

SSA-Form Register Allocation

Foundations Sebastian Hack Compiler Construction Course Winter Term 2009/2010

computer science

saarland

university

slide-2
SLIDE 2

Overview

1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ-functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ-functions 5 Intuition

2

slide-3
SLIDE 3

Overview

1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ-functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ-functions 5 Intuition

3

slide-4
SLIDE 4

Complete Graphs and Cycles

Complete Graph K 5 Cycle C 5

4

slide-5
SLIDE 5

Induced Subgraphs

Graph with a C 4 subgraph Graph with a C 4 induced subgraph

5

slide-6
SLIDE 6

Induced Subgraphs

Graph with a C 4 subgraph Graph with a C 4 induced subgraph

Note

Induced complete graphs are called cliques

5

slide-7
SLIDE 7

Clique number and Chromatic number

Definition

ω(G) Size of the largest clique in G χ(G) Number of colors in a minimum coloring of G

6

slide-8
SLIDE 8

Clique number and Chromatic number

Definition

ω(G) Size of the largest clique in G χ(G) Number of colors in a minimum coloring of G

Corollary

ω(G) ≤ χ(G) holds for each graph G

6

slide-9
SLIDE 9

Clique number and Chromatic number

Definition

ω(G) Size of the largest clique in G χ(G) Number of colors in a minimum coloring of G

Corollary

ω(G) ≤ χ(G) holds for each graph G ω(G) 3 2 2 3 χ(G) 3 2 3 3

6

slide-10
SLIDE 10

Perfect Graphs

Definition

G is perfect ⇐ ⇒ χ(H) = ω(H) for each induced subgraph H of G

7

slide-11
SLIDE 11

Perfect Graphs

Definition

G is perfect ⇐ ⇒ χ(H) = ω(H) for each induced subgraph H of G perfect?

7

slide-12
SLIDE 12

Perfect Graphs

Definition

G is perfect ⇐ ⇒ χ(H) = ω(H) for each induced subgraph H of G perfect?

  • 7
slide-13
SLIDE 13

Chordal Graphs

Definition

G is chordal ⇐ ⇒ G contains no induced cycles longer than 3

8

slide-14
SLIDE 14

Chordal Graphs

Definition

G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal?

8

slide-15
SLIDE 15

Chordal Graphs

Definition

G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal?

  • Theorem

Chordal graphs are perfect

8

slide-16
SLIDE 16

Chordal Graphs

Definition

G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal?

  • Theorem

Chordal graphs are perfect

Theorem

Chordal graphs can be colored optimally in O(|V | · ω(G))

8

slide-17
SLIDE 17

Overview

1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ-functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ-functions 5 Intuition

9

slide-18
SLIDE 18

Dominance

Definition

Every use of a variable is dominated by its definition start v ← · · · · · · ← v

10

slide-19
SLIDE 19

Dominance

Definition

Every use of a variable is dominated by its definition start v ← · · · · · · ← v You cannot reach the use without passing by the definition Else, you could use uninitialized variables Dominance induces a tree on the control flow graph Sometimes called strict SSA

10

slide-20
SLIDE 20

What do φ-functions mean?

z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3) z1 ← x1 z2 ← x2 z3 ← x3 z1 ← y1 z2 ← y2 z3 ← y3

Frequent misconception

Put a sequence of copies in the predecessors

11

slide-21
SLIDE 21

What do φ-functions mean?

z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3) z1 ← x1 z2 ← x2 z3 ← x3 z1 ← y1 z2 ← y2 z3 ← y3

Frequent misconception

Put a sequence of copies in the predecessors

11

slide-22
SLIDE 22

What do φ-functions mean?

Lost Copies

B z1 ← φ(·, y1) z2 ← φ(·, y2) z3 ← φ(·, y3) A C z1 ← y1 z2 ← y2 z3 ← y3 B A C

Cannot simply push copies in predecessor Copies are also executed if we jump from B to C Need to remove critical edges (edge from B to A)

12

slide-23
SLIDE 23

What do φ-functions mean?

Lost Copies

B z1 ← φ(·, y1) z2 ← φ(·, y2) z3 ← φ(·, y3) A C z1 ← y1 z2 ← y2 z3 ← y3 B A C

Cannot simply push copies in predecessor Copies are also executed if we jump from B to C Need to remove critical edges (edge from B to A)

12

slide-24
SLIDE 24

What do φ-functions mean?

φ-swap

z1 ← φ(·, z2) z2 ← φ(·, z1) z1 ← z2 z2 ← z1

z1 overwritten before used

13

slide-25
SLIDE 25

What do φ-functions mean?

φ-swap

z1 ← φ(·, z2) z2 ← φ(·, z1) z1 ← z2 z2 ← z1

z1 overwritten before used

13

slide-26
SLIDE 26

What do φ-functions mean?

z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

(z1, z2, z3) ← (x1, x2, x3) (z1, z2, z3) ← (y1, y2, y3)

The Reality

φ-functions correspond to parallel copies on the incoming edges

14

slide-27
SLIDE 27

φ-functions and uses

z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

Does not fulfill dominance property φs do not use their operands in the φ-block Uses happen in the predecessors

15

slide-28
SLIDE 28

φ-functions and uses

z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

Does not fulfill dominance property φs do not use their operands in the φ-block Uses happen in the predecessors

φr(x1, x2, x3) φr(y1, y2, y3) (z1, z2, z3) ← φw

Split φ-functions in two parts: Split critical edges Read part (φr) in the predecessors Write part (φw) in the block Correct modelling of liveness

15

slide-29
SLIDE 29

Overview

1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ-functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ-functions 5 Intuition

16

slide-30
SLIDE 30

Non-SSA Interference Graphs

An inconvenient property Program a ← 1 b ← a + a c ← a + 1 e ← b + 1 ← c d ← 1 e ← a + 1 ← d Interference Graph a b c e d

The number of live variables at each instruction (register pressure) is 2 However, we need 3 registers for a correct register allocation In theory, this gap can be arbitrarily large (Mycielski Graphs)

17

slide-31
SLIDE 31

Graph-Coloring Register Allocation

[Chaitin ’80, Briggs ’92, Appel & George ’96, Park & Moon ’04]

Build IG Coalesce Color Spill

coloring heuristic failed program changed

Every undirected graph can occur as an interference graph = ⇒ Finding a k-coloring is NP-complete Color using heuristic = ⇒ Iteration necessary Might introduce spills although IG is k-colorable Rebuilding the IG each iteration is costly

18

slide-32
SLIDE 32

Graph-Coloring Register Allocation

[Chaitin ’80, Briggs ’92, Appel & George ’96, Park & Moon ’04]

Build IG Coalesce Color Spill

coloring heuristic failed program changed

Spill-code insertion is crucial for the program’s performance Hence, it should be very sensitive to the structure of the program

◮ Place load and stores carefully ◮ Avoid spilling in loops!

Here, it is merely a fail-safe for coloring

18

slide-33
SLIDE 33

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order

19

slide-34
SLIDE 34

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order d,

19

slide-35
SLIDE 35

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order d, e,

19

slide-36
SLIDE 36

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order d, e, c,

19

slide-37
SLIDE 37

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order d, e, c, a,

19

slide-38
SLIDE 38

Coloring

Subsequently remove the nodes from the graph d e c a b elimination order d, e, c, a, b

19

slide-39
SLIDE 39

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order d, e, c, a, b

19

slide-40
SLIDE 40

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order d, e, c, a,

19

slide-41
SLIDE 41

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order d, e, c,

19

slide-42
SLIDE 42

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order d, e,

19

slide-43
SLIDE 43

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order d,

19

slide-44
SLIDE 44

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order

19

slide-45
SLIDE 45

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order

  • But. . .

this graph is 3-colorable. We obviously picked a wrong order.

19

slide-46
SLIDE 46

Coloring

Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e c a b elimination order

  • But. . .

this graph is 3-colorable. We obviously picked a wrong order.

19

slide-47
SLIDE 47

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order

20

slide-48
SLIDE 48

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a,

20

slide-49
SLIDE 49

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c,

20

slide-50
SLIDE 50

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c, d,

20

slide-51
SLIDE 51

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c, d, e,

20

slide-52
SLIDE 52

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c, d, e, b

20

slide-53
SLIDE 53

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c, d, e,

20

slide-54
SLIDE 54

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c, d,

20

slide-55
SLIDE 55

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a, c,

20

slide-56
SLIDE 56

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order a,

20

slide-57
SLIDE 57

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order

20

slide-58
SLIDE 58

Coloring

PEOs

Perfect Elimination Order (PEO)

All not yet eliminated neighbors of a node are mutually connected d e c a b elimination order

From Graph Theory [Berge ’60, Fulkerson/Gross ’65, Gavril ’72]

A PEO allows for an optimal coloring in k × |V | The number of colors is bound by the size of the largest clique

20

slide-59
SLIDE 59

Coloring

PEOs

Graphs with holes larger than 3 have no PEO, e.g. G has a PEO ⇐ ⇒ G is chordal

21

slide-60
SLIDE 60

Coloring

PEOs

Graphs with holes larger than 3 have no PEO, e.g. G has a PEO ⇐ ⇒ G is chordal

Core Theorem of SSA Register Allocation

The dominance relation in SSA programs induces a PEO in the IG Thus, SSA IGs are chordal

21

slide-61
SLIDE 61

Overview

1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ-functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ-functions 5 Intuition

22

slide-62
SLIDE 62

Liveness and Dominance

Each instruction ℓ where a variable v is live, is dominated by v start v ← · · · ℓ : · · · · · · ← v

23

slide-63
SLIDE 63

Liveness and Dominance

Each instruction ℓ where a variable v is live, is dominated by v start v ← · · · ℓ : · · · · · · ← v

Why?

Assume ℓ is not dominated by v Then there’s a path from start to some usage of v not containing the definition of v This cannot be since each value must have been defined before it is used

23

slide-64
SLIDE 64

Liveness and Dominance

Each instruction ℓ where a variable v is live, is dominated by v start v ← · · · ℓ : · · · · · · ← v

Why?

Assume ℓ is not dominated by v Then there’s a path from start to some usage of v not containing the definition of v This cannot be since each value must have been defined before it is used

23

slide-65
SLIDE 65

Interference and Dominance

Assume v, w interfere, i.e. they are live at some instruction ℓ Then, v ℓ and w ℓ Since dominance is a tree, either v w or w v {, } v w

24

slide-66
SLIDE 66

Interference and Dominance

Assume v, w interfere, i.e. they are live at some instruction ℓ Then, v ℓ and w ℓ Since dominance is a tree, either v w or w v {, } v w

Consequences

Each edge in the IG is directed by dominance The interference graph is an “excerpt” of the dominance relation

24

slide-67
SLIDE 67

Interference and Dominance

Assume

  • v

w Then, v is live at w

d

  • m

i n a n c e s u b t r e e

  • f

v

v ← · · · w ← · · ·

25

slide-68
SLIDE 68

Interference and Dominance

Assume

  • v

w Then, v is live at w

d

  • m

i n a n c e s u b t r e e

  • f

v

v ← · · · w ← · · · both live ← v ← w

Why?

If v and w interfere then there is a place where both are live w dominates all places where w is live Hence, v is live inside w’s dominance tree Thus, v is live at w

25

slide-69
SLIDE 69

Interference and Dominance

Consider three nodes u, v, w in the IG:

  • ???

v w u

26

slide-70
SLIDE 70

Interference and Dominance

Consider three nodes u, v, w in the IG:

  • ???

v w u u is live at w v is live at w

26

slide-71
SLIDE 71

Interference and Dominance

Consider three nodes u, v, w in the IG:

  • r

v w u u is live at w v is live at w Thus, they interfere

26

slide-72
SLIDE 72

Interference and Dominance

Consider three nodes u, v, w in the IG:

  • r

v w u u is live at w v is live at w Thus, they interfere

Conclusion

All variables that . . . interfere with w dominate w . . . are mutually connected in the IG

26

slide-73
SLIDE 73

Dominance and PEOs

Before a value v is added to a PEO, add all values whose definitions are dominated by v A post order walk of the dominance tree defines a PEO A pre order walk of the dominance tree yields a coloring sequence IGs of SSA-form programs can be colored optimally in O(ω(G) · |V |) Without constructing the interference graph itself

27

slide-74
SLIDE 74

Spilling

Theorem

For each clique in the IG there is a program point where all nodes in the clique are live.

28

slide-75
SLIDE 75

Spilling

Theorem

For each clique in the IG there is a program point where all nodes in the clique are live.

  • a

b c d Dominance induces a total order inside the clique ⇒ There is a “smallest” value d All others are live at the definition of d

28

slide-76
SLIDE 76

Spilling

Consequences

The chromatic number of the IG is exactly determined by the number of live variables at the labels Lowering the number of values live at each label to k makes the IG k-colorable We know in advance where values must be spilled = ⇒ All labels where the pressure is larger than k Spilling can be done before coloring and coloring will always succeed afterwards

29

slide-77
SLIDE 77

Spilling

Consequences

The chromatic number of the IG is exactly determined by the number of live variables at the labels Lowering the number of values live at each label to k makes the IG k-colorable We know in advance where values must be spilled = ⇒ All labels where the pressure is larger than k Spilling can be done before coloring and coloring will always succeed afterwards

Conclusion

No iteration as in Chaitin/Briggs-allocators No interference graph necessary

29

slide-78
SLIDE 78

Getting out of SSA

We now have a k-coloring of the SSA interference graph Can we turn that program into a non-SSA program and maintain the coloring?

30

slide-79
SLIDE 79

Getting out of SSA

We now have a k-coloring of the SSA interference graph Can we turn that program into a non-SSA program and maintain the coloring?

Central question

What to do about φ-functions?

30

slide-80
SLIDE 80

Φ-Functions

Consider following example z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

31

slide-81
SLIDE 81

Φ-Functions

Consider following example z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

(z1, z2, z3) ← (x1, x2, x3) (z1, z2, z3) ← (y1, y2, y3)

Φ-functions are parallel copies on control flow edges

31

slide-82
SLIDE 82

Φ-Functions

Consider following example z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3)

(z1, z2, z3) ← (x1, x2, x3) (z1, z2, z3) ← (y1, y2, y3)

Φ-functions are parallel copies on control flow edges Considering assigned registers . . .

31

slide-83
SLIDE 83

Φ-Functions

Consider following example z1 ← φ(x1, y1) z2 ← φ(x2, y2) z3 ← φ(x3, y3) Φ-functions are parallel copies on control flow edges Considering assigned registers . . . . . . Φs represent register permutations

31

slide-84
SLIDE 84

Permutations

A permutation can be implemented with copies if one auxiliary register is available ← ← ← ← Permutations can be implemented by a series of transpositions (i.e. swaps) =

  • A transposition can be implemented by three xors

without a third register

32

slide-85
SLIDE 85

Intuition: Why do SSA IGs do not have cycles?

Why are SSA IGs chordal?

Program Live Ranges a a ← · · · b b ← · · · c c ← · · · d d ← a + b e e ← c + 1 Interference Graph a b c d e How can we create a 4-cycle {a, c, d, e}?

33

slide-86
SLIDE 86

Intuition: Why do SSA IGs do not have cycles?

Why are SSA IGs chordal?

Program Live Ranges a a ← · · · b b ← · · · c c ← · · · d d ← a + b e e ← c + 1 a a ← · · · Interference Graph a b c d e How can we create a 4-cycle {a, c, d, e}? Redefine a = ⇒ SSA violated!

33

slide-87
SLIDE 87

Intuition: φ-functions break cycles in the IG

Program and live ranges a ← · · · d ← · · · e ← a + · · · ← d b ← · · · c ← a + · · · e ← b ← c Interference Graph d a b c e

34

slide-88
SLIDE 88

Intuition: φ-functions break cycles in the IG

Program and live ranges a ← · · · d ← · · · e1 ← a + · · · ← d b ← · · · c ← a + · · · e2 ← b ← c e3 ← φ(e1, e2) Interference Graph d a b c e1 e3 e2

34

slide-89
SLIDE 89

Intuition: Why destroying SSA before RA is bad

Parallel copies Sequential copies

(a′, b′, c′, d′) ← (a, b, c, d) d′ ← d c′ ← c b′ ← b a′ ← a

35

slide-90
SLIDE 90

Intuition: Why destroying SSA before RA is bad

Parallel copies Sequential copies

(a′, b′, c′, d′) ← (a, b, c, d) d′ ← d c′ ← c b′ ← b a′ ← a a a′ b b′ c c′ d d′ a a′ b b′ c c′ d d′

35

slide-91
SLIDE 91

Intuition: Why destroying SSA before RA is bad

Parallel copies Sequential copies

(a′, b′, c′, d′) ← (a, b, c, d) d′ ← d c′ ← c b′ ← b a′ ← a a a′ b b′ c c′ d d′ a a′ b b′ c c′ d d′

35

slide-92
SLIDE 92

Summary

IGs of SSA-form programs are chordal The dominance relation induces a PEO No further spills after pressure is lowered Register assignment optimal in linear time Do not need to construct interference graph Allocator without iteration Spill Color Coalesce Φ-Impl.

36