Top Banner
Challenge the future Delft University of Technology Course IN4303, 2016-2017 Compiler Construction Guido Wachsmuth, Eelco Visser Dataflow Analysis
110

Dataflow Analysis

Jan 25, 2017

Download

Software

Eelco Visser
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dataflow Analysis

Challenge the future

Delft University of Technology

Course IN4303, 2016-2017 Compiler Construction

Guido Wachsmuth, Eelco Visser

Dataflow Analysis

Page 2: Dataflow Analysis

today’s lecture

Dataflow Analysis

Overview

Control flow graphs

• intermediate representation

2

Page 3: Dataflow Analysis

today’s lecture

Dataflow Analysis

Overview

Control flow graphs

• intermediate representation

Non-local optimizations

• dead code elimination

• constant & copy propagation

• common subexpression elimination

2

Page 4: Dataflow Analysis

today’s lecture

Dataflow Analysis

Overview

Control flow graphs

• intermediate representation

Non-local optimizations

• dead code elimination

• constant & copy propagation

• common subexpression elimination

Data flow analyses

• liveness analysis

• reaching definitions

• available expressions

2

Page 5: Dataflow Analysis

today’s lecture

Dataflow Analysis

Overview

Control flow graphs

• intermediate representation

Non-local optimizations

• dead code elimination

• constant & copy propagation

• common subexpression elimination

Data flow analyses

• liveness analysis

• reaching definitions

• available expressions

Optimizations (again)

2

Page 6: Dataflow Analysis

Term Rewriting

Optimizations

I

3

Page 7: Dataflow Analysis

optimization

Dataflow Analysis

Optimizing Compilers

Fully optimizing compiler

• Opt(P) produces smallest program with same I/O behavior

• smallest program for non-terminating programs without I/O:Loop = (L : goto L)

• solves halting problem: Opt(Q) = Loop iff Q halts

Optimizing compiler

• produces program with same I/O behavior

• that is smaller or faster

Full employment theorem for compiler writers

• there is always a better optimizing compiler

4

Page 8: Dataflow Analysis

optimization

Dataflow Analysis

Local vs Non-Local Optimizations

Local optimizations

• peephole optimization

• constant folding

• pattern match + rewrite

Non-local optimizations

• constant propagation

• dead-code elimination

• require information from different parts of program

5

iload 1iload 1mul

iload 1dupmul

(3 + 4) * x 7 * x

Page 9: Dataflow Analysis

optimization

Dataflow Analysis

Program Optimizations

Register allocation

• keep non-overlapping temporaries in same register

Common-subexpression elimination

• if expression is computed more than once, eliminate one computation

Dead-code elimination

• delete computation whose result will never be used

Constant folding

• if operands of expression are constant, do computation at compile-time

And many more possible optimizations

6

Page 10: Dataflow Analysis

example

Dataflow Analysis

Dead Code Elimination

7

delete computation whose result will never be used

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

Page 11: Dataflow Analysis

example

Dataflow Analysis

Dead Code Elimination

7

a ← 0b ← a + 1c ← c + b

return c

delete computation whose result will never be used

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

Page 12: Dataflow Analysis

example

Dataflow Analysis

Constant Propagation

8

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

substitute reference to constant valued variable

Page 13: Dataflow Analysis

example

Dataflow Analysis

Constant Propagation

8

a ← 0b ← 0 + 1c ← c + ba ← 2 * breturn c

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

substitute reference to constant valued variable

Page 14: Dataflow Analysis

example

Dataflow Analysis

Constant Propagation + Folding

9

a ← 1b ← a + 1c ← c + ba ← 2 * breturn c

Page 15: Dataflow Analysis

example

Dataflow Analysis

Constant Propagation + Folding

9

a ← 0b ← 2c ← c + 2a ← 4return c

a ← 1b ← a + 1c ← c + ba ← 2 * breturn c

Page 16: Dataflow Analysis

example

Dataflow Analysis

Copy Propagation

10

a ← eb ← a + 1c ← c + ba ← 2 * breturn c

Page 17: Dataflow Analysis

example

Dataflow Analysis

Copy Propagation

10

a ← eb ← e + 1c ← c + ba ← 2 * breturn c

a ← eb ← a + 1c ← c + ba ← 2 * breturn c

Page 18: Dataflow Analysis

example

Dataflow Analysis

Common Subexpression Elimination

11

c ← a + bd ← 1e ← a + b

if expression is computed more than once, eliminate one computation

Page 19: Dataflow Analysis

example

Dataflow Analysis

Common Subexpression Elimination

11

c ← a + bd ← 1e ← a + b

x ← a + bc ← xd ← 1e ← x

if expression is computed more than once, eliminate one computation

Page 20: Dataflow Analysis

optimization

Dataflow Analysis

Intraprocedural Global Optimization

Terminology

• Intraprocedural: within a single procedure or function

• Global: spans all statements with procedure

• Interprocedural: operating on several procedures at once

Recipe

• Dataflow analysis: traverse flow graph, gather information

• Transformation: modify program using information from analysis

12

Page 21: Dataflow Analysis

Dataflow Analysis

Control-flow Graphs

II

13

Page 22: Dataflow Analysis

quadruples

Dataflow Analysis

Intermediate Language

Store

a ← b ⊕ ca ← b

Memory access

a ← M[b]M[a] ← b

Functions

f(a1, …, an)b ← f(a1, …, an)

Jumps

L:goto Lif a ⊗ b goto L1 else goto L2

14

Page 23: Dataflow Analysis

example

Dataflow Analysis

Control-Flow Graphs

15

a ← 0L1: b ← a + 1 c ← c + b a ← 2 * b if a < N goto L1 else goto L2L2: return c

Page 24: Dataflow Analysis

example

Dataflow Analysis

Control-Flow Graphs

15

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

a ← 0L1: b ← a + 1 c ← c + b a ← 2 * b if a < N goto L1 else goto L2L2: return c

Page 25: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 26: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

Page 27: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

in-edge

Page 28: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

in-edge

in-edge

Page 29: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

predecessor

predecessor

in-edge

in-edge

Page 30: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

predecessor

predecessor

in-edge

out-edge

in-edge

Page 31: Dataflow Analysis

terminology

Dataflow Analysis

Control-Flow Graphs

16

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

node

predecessor

predecessor

successor

in-edge

out-edge

in-edge

Page 32: Dataflow Analysis

Dataflow Analysis

Liveness Analysis

III

17

Page 33: Dataflow Analysis

definition

Dataflow Analysis

Liveness Analysis

Motivation: register allocation

• intermediate code with unbounded number of temporary variables

• map to bounded number of registers

• if two variables are not ‘live’ at the same time, store in same register

Liveness

• a variable is live if it holds a value that may be needed in the future

18

Page 34: Dataflow Analysis

example

Dataflow Analysis

Register Allocation

19

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 35: Dataflow Analysis

example

Dataflow Analysis

Register Allocation

19

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

{a, b} ! r{c} ! q

Page 36: Dataflow Analysis

example

Dataflow Analysis

Register Allocation

19

r ← 0

r ← r + 1

q ← q + r

r ← 2 * r

if r < N

return q

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

{a, b} ! r{c} ! q

Page 37: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 38: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 39: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 40: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 41: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 42: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 43: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 44: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 45: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 46: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 47: Dataflow Analysis

a ← 0

return c

if a < N

b ← a + 1

a ← 2 * b

c ← c + b

example

Dataflow Analysis

Liveness Analysis

20

Page 48: Dataflow Analysis

definitions

Dataflow Analysis

Liveness Analysis

Def and use

• an assignment to a variable defines a variable

• an occurrence of a variable in an expression uses that variable

• def[x] = {n | n defines x}, def[n] = {x | n defines x}

• use[x] = {n | n uses x}, use[n] = {x | n uses x}

A variable is live on an edge if

• there is a directed path from that edge to a use of the variable

• that does not go through any def

A variable is live in/out at a node

• live-in: it is live on any of the in-edges of that node

• live-out: it is live on any of the out-edges of the node

21

Page 49: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 50: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

definition a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 51: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 52: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

definition

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 53: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 54: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

live-in

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 55: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

live-in

live-out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 56: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

live-in

live-in

live-out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 57: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

live-in

live-outlive-in

live-out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 58: Dataflow Analysis

terminology

Dataflow Analysis

Liveness Analysis

22

usage

definition

usage

definition

live-in

live-out

live-outlive-in

live-out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 59: Dataflow Analysis

formalization

Dataflow Analysis

Liveness Analysis

in[n] = use[n] ∪ (out[n] - def[n])

out[n] = ∪s∈succ[n] in[s]

1. If a variable is used at node n, then it is live-in at n

2. If a variable is live-out but not defined at node n, it is live-in at n

3. If a variable is live-in at node n, then it is live-out at its predecessors

23

Page 60: Dataflow Analysis

algorithm

Dataflow Analysis

Liveness Analysis

for each n in[n] ← {} ; out[n] ← {}repeat for each n in'[n] = in[n] ; out'[n] = out[n] in[n] = use[n] ∪ (out[n] - def[n]) for each s in succ[n] out[n] = out[n] ∪ in[s]until for all n: in[n] = in'[n] and out[n] = out'[n]

24

Page 61: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

Page 62: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

Page 63: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

Page 64: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

Page 65: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

3

i

ac

bc

b

ac

c

o

a

bc

b

a

ac

Page 66: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

3

i

ac

bc

b

ac

c

o

a

bc

b

a

ac

4

i

ac

bc

b

ac

c

o

ac

bc

b

ac

ac

Page 67: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

3

i

ac

bc

b

ac

c

o

a

bc

b

a

ac

4

i

ac

bc

b

ac

c

o

ac

bc

b

ac

ac

5

i

c

ac

bc

bc

ac

c

o

ac

bc

b

ac

ac

Page 68: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

3

i

ac

bc

b

ac

c

o

a

bc

b

a

ac

4

i

ac

bc

b

ac

c

o

ac

bc

b

ac

ac

5

i

c

ac

bc

bc

ac

c

o

ac

bc

b

ac

ac

6

i

c

ac

bc

bc

ac

c

o

ac

bc

bc

ac

ac

Page 69: Dataflow Analysis

example

Dataflow Analysis

Liveness Analysis

25

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

1

2

3

4

5

6

u

a

bc

b

a

c

d

a

b

c

a

1

i

a

bc

b

a

c

o

a

2

i

a

bc

b

a

c

o

a

bc

b

a

ac

3

i

ac

bc

b

ac

c

o

a

bc

b

a

ac

4

i

ac

bc

b

ac

c

o

ac

bc

b

ac

ac

5

i

c

ac

bc

bc

ac

c

o

ac

bc

b

ac

ac

6

i

c

ac

bc

bc

ac

c

o

ac

bc

bc

ac

ac

7

i

c

ac

bc

bc

ac

c

o

ac

bc

bc

ac

ac

Page 70: Dataflow Analysis

optimization

Dataflow Analysis

Liveness Analysis

26

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

6

5

4

3

2

1

u

c

a

b

bc

a

Page 71: Dataflow Analysis

optimization

Dataflow Analysis

Liveness Analysis

26

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

6

5

4

3

2

1

u

c

a

b

bc

a

d

a

c

b

a

Page 72: Dataflow Analysis

optimization

Dataflow Analysis

Liveness Analysis

26

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

6

5

4

3

2

1

u

c

a

b

bc

a

d

a

c

b

a

1

o

c

ac

bc

bc

ac

i

c

ac

bc

bc

ac

a

Page 73: Dataflow Analysis

optimization

Dataflow Analysis

Liveness Analysis

26

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

6

5

4

3

2

1

u

c

a

b

bc

a

d

a

c

b

a

1

o

c

ac

bc

bc

ac

i

c

ac

bc

bc

ac

a

2

o

ac

ac

bc

bc

ac

i

c

ac

bc

bc

ac

c

Page 74: Dataflow Analysis

optimization

Dataflow Analysis

Liveness Analysis

26

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

1

2

3

4

5

6

6

5

4

3

2

1

u

c

a

b

bc

a

d

a

c

b

a

1

o

c

ac

bc

bc

ac

i

c

ac

bc

bc

ac

a

2

o

ac

ac

bc

bc

ac

i

c

ac

bc

bc

ac

c

3

o

ac

ac

bc

bc

ac

i

c

ac

bc

bc

ac

c

Page 75: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 76: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

kill a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 77: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 78: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

kill

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 79: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 80: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

in

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 81: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

in

outa ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 82: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

in

out

out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 83: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

in

out

outin

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 84: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

27

gen

kill

gen

kill

in

out

outin

out

a ← 0

b ← a + 1

c ← c + b

a ← 2 * b

if a < N

return c

Page 85: Dataflow Analysis

gen & kill

Dataflow Analysis

Liveness Analysis

28

a ← b ⊕ ca ← ba ← M[b]M[a] ← b

f(a1, …, an)a ← f(a1, …, an)goto L

if a ⊗ b

gen

{b,c}{b}{b}{a,b}

{a1,…,an}{a1,…,an}

{a,b}

Page 86: Dataflow Analysis

gen & kill

Dataflow Analysis

Liveness Analysis

28

a ← b ⊕ ca ← ba ← M[b]M[a] ← b

f(a1, …, an)a ← f(a1, …, an)goto L

if a ⊗ b

gen

{b,c}{b}{b}{a,b}

{a1,…,an}{a1,…,an}

{a,b}

kill

{a}{a}{a}

{a}

Page 87: Dataflow Analysis

generalisation

Dataflow Analysis

Liveness Analysis

in[n] = gen[n] ∪ (out[n] - kill[n])

out[n] = ∪s∈succ[n] in[s]

29

Page 88: Dataflow Analysis

Dataflow Analysis

More Analyses

IV

30

Page 89: Dataflow Analysis

definition

Dataflow Analysis

Reaching Definitions

Unambiguous definition of a

• a statement of the form (d : a ← b ⊕ c) or (d : a ← M[x])

Definition d reaches statement u if

• there is some path in control-flow graph from d to u

• that does not contain an unambiguous definition of a

Used in

• constant propagation

31

Page 90: Dataflow Analysis

example

Dataflow Analysis

Reaching Definitions

32

a ← 5

c ← 1

if c > a

c ← c + c

goto 3

a ← c - a

1

2

3

4

5

6

Page 91: Dataflow Analysis

gen & kill

Dataflow Analysis

Reaching Definitions

33

d: a ← b ⊕ cd: a ← bd: a ← M[b]M[a] ← b

f(a1, …, an)d: a ← f(a1, …, an)goto L

if a ⊗ b

gen

{d}{d}{d}

{d}

defs(a) : all definitions of a

Page 92: Dataflow Analysis

gen & kill

Dataflow Analysis

Reaching Definitions

33

d: a ← b ⊕ cd: a ← bd: a ← M[b]M[a] ← b

f(a1, …, an)d: a ← f(a1, …, an)goto L

if a ⊗ b

gen

{d}{d}{d}

{d}

kill

defs(a)-{d}defs(a)-{d}defs(a)-{d}

defs(a)-{d}

defs(a) : all definitions of a

Page 93: Dataflow Analysis

formalisation

Dataflow Analysis

Reaching Definitions

in[n] = ∪p∈pred[n] out[p]

out[n] = gen[n] ∪ (in[n] - kill[n])

34

Page 94: Dataflow Analysis

definition

Dataflow Analysis

Available Expressions

An expression (b ⊕ c) is available at node n if

• on every path from the entry node to node n

• (b ⊕ c) is computed at least once, and

• there are no definitions of b or c since most recent occurrence of (b ⊕ c) on that path

Used in

• common-subexpression elimination

35

Page 95: Dataflow Analysis

example

Dataflow Analysis

Available Expressions

36

c ← a + b

d ← 1

e ← a + b

1

2

3

Page 96: Dataflow Analysis

gen & kill

Dataflow Analysis

Available Expressions

37

d: a ← b ⊕ cd: a ← bd: a ← M[b]M[a] ← b

f(a1, …, an)d: a ← f(a1, …, an)goto L

if a ⊗ b

gen

{b⊕c}-kill

{M[b]}-kill

Page 97: Dataflow Analysis

gen & kill

Dataflow Analysis

Available Expressions

37

d: a ← b ⊕ cd: a ← bd: a ← M[b]M[a] ← b

f(a1, …, an)d: a ← f(a1, …, an)goto L

if a ⊗ b

gen

{b⊕c}-kill

{M[b]}-kill

kill

exps(a)

exps(a)exps(M[_])

exps(M[_])exps(M[_]) ∪ exps(a)

Page 98: Dataflow Analysis

formalisation

Dataflow Analysis

Available Expressions

in[n] = ∩p∈pred[n] out[p]

out[n] = gen[n] ∪ (in[n] - kill[n])

38

Page 99: Dataflow Analysis

definition

Dataflow Analysis

Reaching Expressions

An expression (s : a ← b ⊕ c) reaches node n if

• there is a path from s to n

• that does not go through assignment to b or c

• or through any computation of (b ⊕ c)

Used in

• common-subexpression elimination

39

Page 100: Dataflow Analysis

Dataflow Analysis

Optimizations

V

40

Page 101: Dataflow Analysis

example

Dataflow Analysis

Dead Code Elimination

41

a ← 0b ← a + 1c ← c + b

return c

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

consider: (s : a ← b ⊕ c) or (s : a ← M[x]) if a is not live-out at s transform: remove s

Page 102: Dataflow Analysis

example

Dataflow Analysis

Common Subexpression Elimination

42

c ← a + bd ← 1e ← a + b

x ← a + bc ← xd ← 1e ← x

consider: (n : a ← b ⊕ c) reaches (s : d ← b ⊕ c) path from n to s does not compute b ⊕ c or define b or c e is a fresh variabletransform: n : a ← b ⊕ c n’ : e ← a … s : d ← e

Page 103: Dataflow Analysis

example

Dataflow Analysis

Constant Propagation

43

a ← 0b ← 0 + 1c ← c + ba ← 2 * breturn c

a ← 0b ← a + 1c ← c + ba ← 2 * breturn c

consider: (d : a ← c) & (n : e ← a ⊕ b)if c is constant & (d reaches n) & (no other def of a reaches n)transform: (n : e ← a ⊕ b) => (n : e ← c ⊕ b)

Page 104: Dataflow Analysis

example

Dataflow Analysis

Copy Propagation

44

a ← eb ← e + 1c ← c + ba ← 2 * breturn c

a ← eb ← a + 1c ← c + ba ← 2 * breturn c

consider: (d : a ← z) & (n : e ← a ⊕ b)if z is a variable & (d reaches n) & (no other def of a reaches n) & (no def of z on path from d to n)transform: (n : e ← a ⊕ b) => (n : e ← z ⊕ b)

Page 105: Dataflow Analysis

Term Rewriting

Summary

V

45

Page 106: Dataflow Analysis

Dataflow Analysis

Summary

Liveness analysis

• intermediate language

• control-flow graphs

• definition & algorithm

More dataflow analyses

• reaching definitions

• available expressions

Optimizations

46

Page 107: Dataflow Analysis

learn more

Dataflow Analysis

Literature

Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002

47

Page 108: Dataflow Analysis

Dataflow Analysis

Copyrights & Credits

48

Page 110: Dataflow Analysis

copyrights

Dataflow Analysis

Pictures

Slide 1: ink swirl by Graham Richardson, some rights reserved

Slide 25: Seattle's Best Coffee by Dominica Williamson, some rights reserved

Slide 32: Animal Ark Reno by Joel Riley, some rights reserved

50