Dataflow Analysis

eelcovisser 13,997 views 110 slides Jan 16, 2017
Slide 1
Slide 1 of 110
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110

About This Presentation

Lecture on data-flow analysis for Compiler Construction course at TU Delft


Slide Content

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

today’s lecture
Dataflow Analysis
Overview
Control flow graphs
•intermediate representation
2

today’s lecture
Dataflow Analysis
Overview
Control flow graphs
•intermediate representation
Non-local optimizations
•dead code elimination
•constant & copy propagation
•common subexpression elimination
2

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

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

Term Rewriting
Optimizations
I
3

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

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 1
iload 1
mul
iload 1
dup
mul
(3 + 4) * x 7 * x

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

example
Dataflow Analysis
Dead Code Elimination
7
delete computation whose result will never be used
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Dead Code Elimination
7
a ← 0
b ← a + 1
c ← c + b
return c
delete computation whose result will never be used
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Constant Propagation
8
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return c
substitute reference to constant valued variable

example
Dataflow Analysis
Constant Propagation
8
a ← 0
b ← 0 + 1
c ← c + b
a ← 2 * b
return c
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return c
substitute reference to constant valued variable

example
Dataflow Analysis
Constant Propagation + Folding
9
a ← 1
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Constant Propagation + Folding
9
a ← 0
b ← 2
c ← c + 2
a ← 4
return c
a ← 1
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Copy Propagation
10
a ← e
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Copy Propagation
10
a ← e
b ← e + 1
c ← c + b
a ← 2 * b
return c
a ← e
b ← a + 1
c ← c + b
a ← 2 * b
return c

example
Dataflow Analysis
Common Subexpression Elimination
11
c ← a + b
d ← 1
e ← a + b
if expression is computed more than once, eliminate one computation

example
Dataflow Analysis
Common Subexpression Elimination
11
c ← a + b
d ← 1
e ← a + b
x ← a + b
c ← x
d ← 1
e ← x
if expression is computed more than once, eliminate one computation

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

Dataflow Analysis
Control-flow Graphs
II
13

quadruples
Dataflow Analysis
Intermediate Language
Store
a ← b ⊕ c
a ← b
Memory access
a ← M[b]
M[a] ← b
Functions
f(a1, …, an)
b ← f(a1, …, an)
Jumps
L:
goto L
if a ⊗ b
goto L1
else
goto L2
14

example
Dataflow Analysis
Control-Flow Graphs
15
a ← 0
L1: b ← a + 1
c ← c + b
a ← 2 * b
if a < N
goto L1
else
goto L2
L2: return c

example
Dataflow Analysis
Control-Flow Graphs
15
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c
a ← 0
L1: b ← a + 1
c ← c + b
a ← 2 * b
if a < N
goto L1
else
goto L2
L2: return c

terminology
Dataflow Analysis
Control-Flow Graphs
16
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

terminology
Dataflow Analysis
Control-Flow Graphs
16
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c
node

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

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

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

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

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

Dataflow Analysis
Liveness Analysis
III
17

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

example
Dataflow Analysis
Register Allocation
19
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

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

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

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

a ← 0
return c
if a < N
b ← a + 1
a ← 2 * b
c ← c + b
example
Dataflow Analysis
Liveness Analysis
20

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

terminology
Dataflow Analysis
Liveness Analysis
22
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

terminology
Dataflow Analysis
Liveness Analysis
22
definition a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

terminology
Dataflow Analysis
Liveness Analysis
22
usage
definition a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

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

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

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

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

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

terminology
Dataflow Analysis
Liveness Analysis
22
usage
definition
usage
definition
live-in
live-out
live-in
live-out
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

terminology
Dataflow Analysis
Liveness Analysis
22
usage
definition
usage
definition
live-in
live-out
live-out
live-in
live-out
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

generalisation
Dataflow Analysis
Liveness Analysis
27
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

generalisation
Dataflow Analysis
Liveness Analysis
27
kill a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

generalisation
Dataflow Analysis
Liveness Analysis
27
gen
kill a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

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

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

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

generalisation
Dataflow Analysis
Liveness Analysis
27
gen
kill
gen
kill
in
out
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

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

generalisation
Dataflow Analysis
Liveness Analysis
27
gen
kill
gen
kill
in
out
out
in
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

generalisation
Dataflow Analysis
Liveness Analysis
27
gen
kill
gen
kill
in
out
out
in
out
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
if a < N
return c

gen & kill
Dataflow Analysis
Liveness Analysis
28
a ← b ⊕ c
a ← b
a ← 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}

gen & kill
Dataflow Analysis
Liveness Analysis
28
a ← b ⊕ c
a ← b
a ← 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}

generalisation
Dataflow Analysis
Liveness Analysis
in[n] = gen[n] ∪ (out[n] - kill[n])
out[n] = ∪s∈succ[n] in[s]
29

Dataflow Analysis
More Analyses
IV
30

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

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

gen & kill
Dataflow Analysis
Reaching Definitions
33
d: a ← b ⊕ c
d: a ← b
d: 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

gen & kill
Dataflow Analysis
Reaching Definitions
33
d: a ← b ⊕ c
d: a ← b
d: 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

formalisation
Dataflow Analysis
Reaching Definitions
in[n] = ∪p∈pred[n] out[p]
out[n] = gen[n] ∪ (in[n] - kill[n])
34

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

example
Dataflow Analysis
Available Expressions
36
c ← a + b
d ← 1
e ← a + b
1
2
3

gen & kill
Dataflow Analysis
Available Expressions
37
d: a ← b ⊕ c
d: a ← b
d: 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

gen & kill
Dataflow Analysis
Available Expressions
37
d: a ← b ⊕ c
d: a ← b
d: 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)

formalisation
Dataflow Analysis
Available Expressions
in[n] = ∩p∈pred[n] out[p]
out[n] = gen[n] ∪ (in[n] - kill[n])
38

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

Dataflow Analysis
Optimizations
V
40

example
Dataflow Analysis
Dead Code Elimination
41
a ← 0
b ← a + 1
c ← c + b
return c
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return c
consider:
(s : a ← b ⊕ c) or (s : a ← M[x])
if a is not live-out at s
transform:
remove s

example
Dataflow Analysis
Common Subexpression Elimination
42
c ← a + b
d ← 1
e ← a + b
x ← a + b
c ← x
d ← 1
e ← 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 variable
transform:
n : a ← b ⊕ c
n’ : e ← a

s : d ← e

example
Dataflow Analysis
Constant Propagation
43
a ← 0
b ← 0 + 1
c ← c + b
a ← 2 * b
return c
a ← 0
b ← a + 1
c ← c + b
a ← 2 * b
return 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)

example
Dataflow Analysis
Copy Propagation
44
a ← e
b ← e + 1
c ← c + b
a ← 2 * b
return c
a ← e
b ← a + 1
c ← c + b
a ← 2 * b
return 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)

Term Rewriting
Summary
V
45

Dataflow Analysis
Summary
Liveness analysis
•intermediate language
•control-flow graphs
•definition & algorithm
More dataflow analyses
•reaching definitions
•available expressions
Optimizations
46

learn more
Dataflow Analysis
Literature
Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in
Java, 2nd edition. 2002
47

Dataflow Analysis
Copyrights & Credits
48

Dataflow Analysis49

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