Towards an SSA based compiler back-end: some interesting...

64
Context Intermediate representation Liveness SSA destruction Conclusion Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions Benoit Boissinot ´ Equipe Compsys Laboratoire de l’Informatique du Parall´ elisme (LIP) ´ Ecole normale sup´ erieure de Lyon Membres du jury : Albert Cohen (rapporteur) INRIA Saclay Anton Ertl (examinateur) TU Wien David Monniaux (rapporteur) Verimag Fabrice Rastello (directeur de th` ese) ENS de Lyon Yves Robert (examinateur) ENS de Lyon 1 / 37

Transcript of Towards an SSA based compiler back-end: some interesting...

Page 1: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Towards an SSA based compiler back-end:some interesting properties of SSA and its

extensions

Benoit Boissinot

Equipe CompsysLaboratoire de l’Informatique du Parallelisme (LIP)

Ecole normale superieure de Lyon

Membres du jury : Albert Cohen (rapporteur) INRIA SaclayAnton Ertl (examinateur) TU WienDavid Monniaux (rapporteur) VerimagFabrice Rastello (directeur de these) ENS de LyonYves Robert (examinateur) ENS de Lyon

1 / 37

Page 2: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Compiling, compilers

2 / 37

Page 3: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Compiling, compilers

Two compilation phases

1 On a workstation: compile to architecture independantbytecode

2 On the target processor, compile bytecode to native code

Challenges

Reduce compilation time (speed of compilation),while keeping a high code quality (speed of execution).

3 / 37

Page 4: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Compiling, compilers

Two compilation phases

1 On a workstation: compile to architecture independantbytecode

2 On the target processor, compile bytecode to native code

Challenges

Reduce compilation time (speed of compilation),while keeping a high code quality (speed of execution).

3 / 37

Page 5: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Control flow graph

Control flow graph (CFG)

Program is represented as agraph

Node: sequence ofinstruction without branch

Edge: possible executionflow (branch instructions)

x← 1y← 2b← ?(x > y)branch b

r← x r← y

return r

4 / 37

Page 6: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Static Single Assignment

Static Single Assignment (SSA)

x ← . . .y ← . . .

x← . . . y← . . .

z← x + y

x0 ← . . .y0 ← . . .

x1 ← . . . y1 ← . . .

x2 ← φ(x1, x0)y2 ← φ(y0, y1)z ← x2 + y2

For each variable:

Only one textual definition (⇒ renaming)

No use of undefined variable (dominance property)

Add φ-function at merge points, acts as parallel switch

5 / 37

Page 7: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Static Single Assignment

Use of SSA

Goal

One static definition per variable ⇒ attach properties to variable(sparse representation)

Properties

Dominance property

Unique definition

⇒ simpler and more efficient algorithms

Drawbacks

Create more variables (memory)

Cost of maintainance

Need to get rid of φ-functions

6 / 37

Page 8: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Static Single Assignment

Use of SSA

Goal

One static definition per variable ⇒ attach properties to variable(sparse representation)

Properties

Dominance property

Unique definition

⇒ simpler and more efficient algorithms

Drawbacks

Create more variables (memory)

Cost of maintainance

Need to get rid of φ-functions

6 / 37

Page 9: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Static Single Assignment

Outline

SSA Live-rangeSSI

live-range

SSAdestruction

Value

Live-check

Interf-check

Sparse

SSIdebunking

7 / 37

Page 10: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Outline

SSA Live-rangeSSI

live-range

SSAdestruction

Value

Live-check

Interf-check

Sparse

SSIdebunking

8 / 37

Page 11: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Liveness

Definition (live-in)

Existence of a path to a use thatdoes not contain the definition.

Definition (live-range)

Set of program points where avariable is live-in.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

9 / 37

Page 12: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

What to compute?

Classical Approach: Liveness Sets

For every block boundary, the set of all live variables.

Expensive precomputation (space & time), fast query

Usually, not all computed information is needed

Adding, (re-)moving instructions ⇒ recompute information

Alternative approach: Liveness Checking

Answer on demand: Is a variable live at program point?

Faster precomputation, slower queries

Information depends only on CFG and def-use chains

Information invariant to adding, (re-) moving instructions

10 / 37

Page 13: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

What to compute?

Classical Approach: Liveness Sets

For every block boundary, the set of all live variables.

Expensive precomputation (space & time), fast query

Usually, not all computed information is needed

Adding, (re-)moving instructions ⇒ recompute information

Alternative approach: Liveness Checking

Answer on demand: Is a variable live at program point?

Faster precomputation, slower queries

Information depends only on CFG and def-use chains

Information invariant to adding, (re-) moving instructions

10 / 37

Page 14: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Data-flow

Traditional approach (not SSA specific)

Backward data-flow propagation:propagates the information about everyvariable inside the CFG.Wait for a fixed-point.⇒ number of iterations depends on thecyclic structure.

r=0

1

2

3

4

5

6

7

8

9

10

y = . . .

. . . = y

x = . . .

. . . = x

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

11 / 37

Page 15: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Data-flow

Traditional approach (not SSA specific)

Backward data-flow propagation:propagates the information about everyvariable inside the CFG.Wait for a fixed-point.⇒ number of iterations depends on thecyclic structure.

r=0

1

2

3

4

5

6

7

8

9

10

y = . . .

. . . = y

x = . . .

. . . = x

{}

{}

{}

{}

{}

{}

{}

{x}

{x}

{x}

{y}

11 / 37

Page 16: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Data-flow

Traditional approach (not SSA specific)

Backward data-flow propagation:propagates the information about everyvariable inside the CFG.Wait for a fixed-point.⇒ number of iterations depends on thecyclic structure.

r=0

1

2

3

4

5

6

7

8

9

10

y = . . .

. . . = y

x = . . .

. . . = x

{}

{}

{x}

{x}

{x}

{x, y}

{x, y}

{x, y}

{x}

{x}

{x}

11 / 37

Page 17: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Data-flow

Traditional approach (not SSA specific)

Backward data-flow propagation:propagates the information about everyvariable inside the CFG.Wait for a fixed-point.⇒ number of iterations depends on thecyclic structure.

r=0

1

2

3

4

5

6

7

8

9

10

y = . . .

. . . = y

x = . . .

. . . = x

{}

{}

{x}

{x}

{x}

{x, y}

{x, y}

{x, y}

{x}

{x}

{x, y}

11 / 37

Page 18: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

12 / 37

Page 19: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

12 / 37

Page 20: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

12 / 37

Page 21: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

6

12 / 37

Page 22: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

6

12 / 37

Page 23: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

6

7

12 / 37

Page 24: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (principle)

Context

Found in some modern compilertextbooks, requires or builds useand def sets.

Discover live-range: starting fromthe uses, mark the ancestors in theCFG, stop when a definition isfound.

Efficiency

Few uses per variables.

Under SSA: short live-ranges,use-def chains and def-use chainsavailable.

r=0

1

2

3

4

5

6

7

8

9

10

x = . . .

. . . = x

2

5

4

6

7

12 / 37

Page 25: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Different approaches

Path exploration (algorithm)

Live-sets

For each variable:

1 build the live-range

2 update the live-sets

Complexity: size of resulting sets (cost of insertions)

Live-check

Given a variable x and a program point p, build the live-range of x,test membership of p.

13 / 37

Page 26: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops

Loop

Decomposition of the CFG in strongly connected components

Headers: distinguished nodes (usually entry points)

Form a tree structure

14 / 37

Page 27: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

15 / 37

Page 28: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

1

2

3

4

5

6

7

8

9

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L1

1 2 3 4 5 6 7 8 9

15 / 37

Page 29: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

1

2

3

4

5

6

7

8

9

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L1

1 2 3 4 5 6 7 8 9

15 / 37

Page 30: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

4

5

6

7

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L4

4 5 6 7

15 / 37

Page 31: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

4

5

6

7

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L4

4 5 6 7

15 / 37

Page 32: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

5

6

7

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L5

5 6 7

15 / 37

Page 33: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loops (example)

r=0

1

2

3

4

5

6

7

8

9

10

5

6

7

Lr

L1

L4

L5

r 1 2 3 4 5 6 7 8 9 10

L5

5 6 7

15 / 37

Page 34: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Irreducible CFG

Strongly connectedcomponents with severalentry points

Unstructured original code(goto)

More complex algorithms

r=0

1

2

3

4

5

6

7

8

9

10

16 / 37

Page 35: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loop-based liveness (principle)

Build partial liveness

Liveness is easy to compute on directed acyclic graphs (DAG)⇒ partial liveness on the CFG without the loop-edges (reducedDAG)

Fix liveness using loops

For every SSA variable x and program point p such that x is live-inat p:

either p is found live-in with partial liveness,

or there exists a header of a loop containing p: h, such that his found live-in with partial liveness.

Reciprocally, if x is live-in of a loop header h, it is also live-in ofevery node contained in the loop.

17 / 37

Page 36: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loop-based liveness (principle)

Build partial liveness

Liveness is easy to compute on directed acyclic graphs (DAG)⇒ partial liveness on the CFG without the loop-edges (reducedDAG)

Fix liveness using loops

For every SSA variable x and program point p such that x is live-inat p:

either p is found live-in with partial liveness,

or there exists a header of a loop containing p: h, such that his found live-in with partial liveness.

Reciprocally, if x is live-in of a loop header h, it is also live-in ofevery node contained in the loop.

17 / 37

Page 37: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Two-pass data-flow (live-sets)

First pass (build partial liveness)

One pass of data-flow in reversetopological order of the reduced acyclicgraph.

Second pass (fix liveness)

Add the missing variables to thelive-sets with a top-down pass in theloop forest.

r=0

1

2

3

4

5

6

7

8

9

10

x =

= x

y =

= y

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

18 / 37

Page 38: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Two-pass data-flow (live-sets)

First pass (build partial liveness)

One pass of data-flow in reversetopological order of the reduced acyclicgraph.

Second pass (fix liveness)

Add the missing variables to thelive-sets with a top-down pass in theloop forest.

r=0

1

2

3

4

5

6

7

8

9

10

x =

= x

y =

= y

{}

{}

{}

{}

{}

{}

{}

{x}

{x}

{x}

{y}

18 / 37

Page 39: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Two-pass data-flow (live-sets)

First pass (build partial liveness)

One pass of data-flow in reversetopological order of the reduced acyclicgraph.

Second pass (fix liveness)

Add the missing variables to thelive-sets with a top-down pass in theloop forest.

r=0

1

2

3

4

5

6

7

8

9

10

x =

= x

y =

= y

{}

{}

{x}

{x}

{x}

1

2

3

4

5

6

7

8

9

{x, y}

{x}

{x}

{x}

{x}

{x}

18 / 37

Page 40: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Two-pass data-flow (live-sets)

First pass (build partial liveness)

One pass of data-flow in reversetopological order of the reduced acyclicgraph.

Second pass (fix liveness)

Add the missing variables to thelive-sets with a top-down pass in theloop forest.

r=0

1

2

3

4

5

6

7

8

9

10

x =

= x

y =

= y

{}

{}

{x}

{x}

{x}

{x, y}

{x}

{x}

4

5

6

7

{x, y}

{x, y}

{x, y}

18 / 37

Page 41: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Two-pass data-flow (live-sets)

First pass (build partial liveness)

One pass of data-flow in reversetopological order of the reduced acyclicgraph.

Second pass (fix liveness)

Add the missing variables to thelive-sets with a top-down pass in theloop forest.

r=0

1

2

3

4

5

6

7

8

9

10

x =

= x

y =

= y

{}

{}

{x}

{x}

{x}

{x, y}

{x}

{x}

{x, y}

{x, y}

{x, y}

5

6

7

18 / 37

Page 42: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loop-based liveness (live-check)

Query

Given a variable x, a program point p and a u a use of x:

1 Find h: header of largest loop containing p but not thedefinition of x.

2 Check existence of path from h to u in the reduced DAG.

Step 1 can be reduced to the least-common ancestor problem inthe loop-nesting tree.

19 / 37

Page 43: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Loop-based liveness (irreducible graphs)

Graph transformation preserving liveness

Given a graph that is not reducible, we can transform it, whilepreserving liveness and loop structure, so that it becomesreducible:

1 For every loop, choose an entry node as unique header

2 For every edge entering the loop, redirect them to the chosenheader

No need to actually modify the graph.

20 / 37

Page 44: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Summary

Algorithms Live-sets Live-check

Data-flow X –Path exploration X XLoop X X

Requirements

Path exploration:

Sets of use and definitions

Loop-based liveness:

Loop-nesting tree

SSA

Def-use chains for live-check

21 / 37

Page 45: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Summary

Algorithms Live-sets Live-check

Data-flow X –Path exploration X XLoop X X

Requirements

Path exploration:

Sets of use and definitions

Loop-based liveness:

Loop-nesting tree

SSA

Def-use chains for live-check

21 / 37

Page 46: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Loop-based liveness

Summary

Loop-based liveness

Use SSA properties to derive a new way to find liveness

Live-check

New way to use liveness information:

easy to pre-compute

no maintainance needed

overall speedup depending on the number of queries

22 / 37

Page 47: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Outline

SSA Live-rangeSSI

live-range

SSAdestruction

Value

Live-check

Interf-check

Sparse

SSIdebunking

23 / 37

Page 48: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Clean approach

Previous approaches

Cytron et al. (1991): copies in predecessor basic blocks.Incorrect because of a bad understanding of:

parallel nature of φ-functions;critical edges.

Briggs et al. (1998): both problems identified. Generalcorrectness unclear.

Sreedhar et al. (1999): correct but

handling of complex branching instructions unclear;interplay with coalescing unclear;“virtualization” hard to implement.

24 / 37

Page 49: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Clean approach

Going to CSSA (conventional SSA)

Conventional SSA (CSSA)

For any φ-functiona0 = φ(a1, . . . , an), the variablesa0, . . . , an can be safely replacedby a common ressource.

Correctness

Add copies with new localvariables around every φ.=⇒ CSSA

From SSA to CSSA

B1 Bi

B0

Bn

a0 = φ(a1, . . . , an)

25 / 37

Page 50: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Clean approach

Going to CSSA (conventional SSA)

Conventional SSA (CSSA)

For any φ-functiona0 = φ(a1, . . . , an), the variablesa0, . . . , an can be safely replacedby a common ressource.

Correctness

Add copies with new localvariables around every φ.=⇒ CSSA

From SSA to CSSA

B1 Bi

B0

Bn

a′0 = φ(a′1, . . . , a′n)

a0 = a′0

a′1 = a1 a′i = ai a′n = an

25 / 37

Page 51: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Clean approach

Code quality

Removing copies

Useless copies can be removed by standard aggressive coalescing.⇒ use an accurate notion of interference

26 / 37

Page 52: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Improving code quality

Traditional interference

Definition (ultimate interference)

Two variables interfere if they can be simultaneously live whilehaving different values.

Chaintin’s approximation

Two variables interfere if one is live at the definition of the other,and it is not a copy of the first.

x← . . .y← . . .

z← x z← y

x ← z

y ← z

. . . ← x

In both cases, x interferes with y.

27 / 37

Page 53: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Improving code quality

Traditional interference

Definition (ultimate interference)

Two variables interfere if they can be simultaneously live whilehaving different values.

Chaintin’s approximation

Two variables interfere if one is live at the definition of the other,and it is not a copy of the first.

x← . . .y← . . .

z← x z← y

x ← z

y ← z

. . . ← x

In both cases, x interferes with y. 27 / 37

Page 54: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Improving code quality

Exploiting SSA: value-based interference

Unique value V of a SSA variable

For a copy x← y, V (x) = V (y) (traversal of dominance tree).

Value-based interference

Two variables x and y interfere if V (x) 6= V (y) and one is live atthe definition of the other.

28 / 37

Page 55: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Improving code quality

Qualitative experiments with SPEC CINT2000

Number of remaining moves

29 / 37

Page 56: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

How to coalesce variables?

Two alternatives

Use a working interference graph where, in case of coalescing,corresponding vertices are merged. O(1) interference query.

Manipulate congruence classes, i.e., sets of coalescedvariables. Interferences must be tested between sets.

Chaitin, Sreedhar, Budimlic use congruence classes. Also useful toavoid interference graph. Naive algorithm: quadratic complexity.

30 / 37

Page 57: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Fast interference test for a set of variables

Key properties for linear-complexity live range intersection

2 SSA variables intersect if one is live at the definition of theother.

In this case, the first definition dominates the second one.

Budimlic: If a and b intersect (a dom b), then∀c with a dom c and c dom b: b and c interferes.

=⇒ For each variable, the only test needed is with the “closest”dominating variable

31 / 37

Page 58: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Linear interference test of two congruence classes

Generalization to interference test of two sets

DFS traversal of a tree⇒ Emulate traversal

Interference inside a set⇒ Interference between two sets

Take values into account

⇒ Test and merge linearly two sets of variables

Fewer intersection tests ⇒ more expensive queries for intersection,avoid interference graph:

Budimlic intersection test, using liveness sets.

Liveness checking (presented earlier)

32 / 37

Page 59: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Linear interference test of two congruence classes

Generalization to interference test of two sets

DFS traversal of a tree⇒ Emulate traversal

Interference inside a set⇒ Interference between two sets

Take values into account

⇒ Test and merge linearly two sets of variables

Fewer intersection tests ⇒ more expensive queries for intersection,avoid interference graph:

Budimlic intersection test, using liveness sets.

Liveness checking (presented earlier)

32 / 37

Page 60: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Memory footprint reduction for SPEC CINT2000: x10

Interference graph: half-size bit matrix.

Liveness sets: enumerated sets. Does not count construction.

Livenesss check: bit sets. Construction taken into account.

Max of memory footprint

33 / 37

Page 61: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Speed-up for SPEC CINT2000: x2

Time to go out of SSA (valgrind cycles)Default: Liveness sets + interference graph

34 / 37

Page 62: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Efficiency

Summary

General framework

Correctness clarified even for complex cases

Two phases solution, based on coalescing

Results

Value-based interference as good as Sreedhar III

Fast algorithm: Speed-up x2, memory reduction x10.

Simple implementation

35 / 37

Page 63: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Conclusion

SSA Live-range

SSIlive-range

TreeScan

SSAdestruction

Value

Live-check

Interf-check

Sparse

SSIdebunking

SSI Zoo

36 / 37

Page 64: Towards an SSA based compiler back-end: some interesting ...bboissin.appspot.com/static/report/bboissin_thesis_slides.pdf · Context Intermediate representation Liveness SSA destructionConclusion

Context Intermediate representation Liveness SSA destruction Conclusion

Perspectives

SSA Live-range

SSIlive-range

TreeScan

SSAdestruction

Value

Live-check

Interf-check

Sparse

SSIdebunking

SSI Zoo

37 / 37