CCimp
CCimp
Lecture
Lect e 10:
10 Introduction
Int od ction to
to
Dataflow Analysis
Value Numbering
g Summary
y
• Forward symbolic execution of basic block
• Maps
– Var2Val – symbolic value for each variable
– Exp2Val – value of each evaluated expression
– Exp2Tmp – tmp that holds value of each evaluated expression
• Algorithm
– For each statement
• If variables in RHS not in the Var2Val add it with a new value
• If RHS expression in Exp2Tmp use that Temp
• If nott add 2V l with
dd RHS expressiion tto Exp2Val ith new vallue
• Copy the value into a new tmp and add to EXp2Tmp
• Reaching Definitions
• Available Expressions
• Liveness
Reaching
g Definitions
• Concept
p of definition and use
– a = x+y
– is a d o o
definition of a
– is a use of x and y
• A definition reaches a use if
– value written by definition
– may be read by use
b = 1; b = 2;
i<n
s = s + a*b;
i = i + 1; return s
i<n
s = s + a*b;
i = i + 1;; return s
i<n
s = s + 4*b;
i = i + 1;; return s
b = 1; b = 2;
i<n
s = s + a*b; s = 0;
i = i + 1; return s a = 4;
i = 0;
k == 0
b = 1; b = 2;
i<n i<n
s = s + aa*b;
b; s = s + aa*b;
b;
i = i + 1; return s i = i + 1; return s
b = 1; b = 2;
i<n
s = s + a*b; s = 0;
i = i + 1; return s a = 4;
i = 0;
k == 0
b = 1; b = 2;
i<n i<n
1234567 1234567
1110000 1110000
4: b = 1; 5: b = 2;
1111000 1110100
1234567
1111100
1111111
i<n
1234567 1111111
1111100
1234567
1111111
1111100 1111111
1111100
6: s = s + a*b; return s
7:
7 i = i + 1;
1 1111111
1111100
0101111
IN[n] = emptyset;
for all nodes p in predecessors(n)
IN[n]
[ ] = IN[n]
[ ] U OUT[p];
[p];
if (OUT[n] changed)
for all nodes s in successors(n)
Changed = Changed U { s };
Saman Amarasinghe 20 6.035 ©MIT Fall 1998
Q
Questions
• Does the algorithm halt?
– yes, because transfer function is monotonic
– if increase IN, increase OUT
– in limit, all bits are 1
• If bit is 0,
0 does the corresponding definition ever
reach basic block?
• If bit is 1,
1 is does the corresponding definition
always reach the basic block?
1234567 1234567
1110000 1110000
4: b = 1; 5: b = 2;
1111000 1110100
1234567
1111100
1111111
i<n
1234567 1111111
1111100
1234567
1111111
1111100 1111111
1111100
6: s = s + a*b; return s
7:
7 i = i + 1;
1 1111111
1111100
0101111
• Reaching Definitions
• Available Expressions
• Liveness
Available Expressions
p
• An expression x+y is available at a point p if
– every path from the initial node to p must evaluate
x+y before reaching p,
– and there are no assignments to x or y after the
evaluation but before p.
• Available Expression information can be used to
do global (across basic blocks) CSE
• If expression is available at use, no need to
reevaluate it
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=a+c
h=c+f
j=a+b+c+d
b=a+d
g=f
h=c+f
j=a+b+c+d
b=a+d
g=f
h=c+f
j=a+b+c+d
b=a+d
g=f
h=c+f
j=a+c+ b+d
b=a+d
g=f
h=c+f
j=f+ b+d
b=a+d
g=f
h=c+f
j=f+ b+d
1000
i<n
1100 1100
c = x+y; d = x+y
i = i+c;
Saman Amarasinghe 42 6.035 ©MIT Fall 2006
0000
Global CSE Transform a = x+y;
t=a
Expressions x == 0
1001
1: x+yy
x = z;
2: i<n
b = x+y;
3: i+c
t=b
4: x==0 1000
i = x+y;
must use same temp
for CSE in all blocks 1000
i<n
1100 1100
c = x+y; d = x+y
i = i+c;
Saman Amarasinghe 43 6.035 ©MIT Fall 2006
0000
Global CSE Transform a = x+y;
t=a
Expressions x == 0
1001
1: x+yy
x = z;
2: i<n
b = x+y;
3: i+c
t=b
4: x==0 1000
i = t;
must use same temp
for CSE in all blocks 1000
i<n
1100 1100
c = t; d=t
i = i+c;
Saman Amarasinghe 44 6.035 ©MIT Fall 2006
Formalizing
g Analysis
y
• Each basic block has
– IN - set of expressions available at start of block
– OUT - set of expressions available at end of block
– GEN - set of expressions computed in block
– KILL - set of expressions killed in in block
• GEN[[x = z;; b = x+y]
y] = 1000
• KILL[x = z; b = x+y] = 1001
• Compiler scans each basic block to derive GEN
and KILL sets
if (OUT[n] changed)
for all nodes s in successors(n)
Saman Amarasinghe Changed = Changed U { s48}; 6.035 ©MIT Fall 1998
Q
Questions
• Does algorithm always halt?
• Iff expression
i isi not available
il bl in
i some execution,
i
can it be marked as available in analysis?
• Reaching Definitions
• Available Expressions
• Liveness
Liveness Analysis
y
• A variable v is live at point p if
– v is used along some path starting at p, and
– no definition of v along the path before the use.
• When is a variable v dead at point p?
– No use of v on any path from p to exit node, or
– If allll paths
th ffrom p redefine
d fi v before
b f using
i v.
OUT[[n]] = empptyyset;;
for all nodes s in successors(n)
OUT[n] = OUT[n] U IN[p];
if (IN[n] changed)
for all nodes p in predecessors(n)
Changed = Changed U { p };
OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); IN[n] = use[n] U (out[n] - def[n]);
• Dataflow
D t fl Anallysiis
– Control flow graph
– IN[b], OUT[b], transfer functions, join points
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.