Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- http//www.eecg.toronto.edu/voss/ece540/
- Lecture 05, Jan. 21, 2002
2Data Flow Analysis
3Outline
- Quick Overview / Motivation
- Reaching Definitions
- Data Flow Frameworks
- Live Variables
- Available Expressions
- Very Busy Expressions
4Data Flow Analysis
- Goal make assertions about the data usage in a
program - Use these assertions to determine if and when
optimizations are legal - Local within a single basic block
- Analyze effect of each instruction
- Compose effect at beginning/end of BB
- Global within a procedure (across BBs)
- Consider the effect of control flow
- Inter-procedural across procedures
- References
- Muchnick, Chapter 8.
- Dragon Book, 608-611, 624-627, 631.
5Data Flow Analysis
- Compile-time reasoning about the run-time flow of
values in the program - Represent facts about the run-time behavior
- Represent effect of executing each basic block
- Propagate facts around the control flow graph
6Data Flow Analysis
- Formulated as a set of simultaneous equations
- Sets attached to the nodes and edges
- Lattice to describe the relation between values
- Usually represented as a bit or bit vectors
- Solve equations using iterative framework
- Start with initial guess of facts at each node
- Propagate until stabilizes at maximal fixed
point. - Would like meet over all paths (MOP) solution
7Basic Approach
Must be conservative!
8Example Reaching Definitions
Problem statement for each basic block b find
which of all definitions in the program reach the
boundaries of b.
Definition A definition of a variable x is an
instruction that assigns (or may assign) a value
to x.
- Reaches A definition d of variable x reaches a
point p in the program if there exists a path
from the point immediately following d to p such
that d is not killed by another definition of x
along this path.
9Reaching Definitions Gen Set
Gen(b) the set of definitions that appear in a
basic block b and reach its end.
Entry
BB 1
Gen (BB 1) d2, d3
a 5 c 1 a a 1 c a?
Gen (BB 2) d4
Gen (BB 3) d5, d6
c c c
BB 2
BB 3
a c - a c 0
Exit
Finding Gen(b) is doing local reaching
definitions analysis.
10Reaching Definitions Kill Set
Kill(b) Set of definitions in other basic blocks
that are killed in b (i.e., by instructions in
b). For each variable v defined in b, the kill
set contains all definitions of v in other basic
blocks.
Entry
Kill (BB 1) d5, d4, d6
BB 1
a 5 c 1 a a 1 c a?
Kill (BB 2) d2, d6
Kill (BB 3) d1, d3, d2, d4
c c c
BB 2
BB 3
a c - a c 0
Exit
11Reaching Definitions Data Flow Equations
RDin(b) Set of definitions that reach the
beginning of b. RDout(b) Set of definitions
that reach the end of b.
12Reaching Definitions - Solving the Data Flow
Equations
F
RDin(BB 1) RDout(Entry) È RDout(BB
2)d3,d4 RDout(BB 1) Gen(BB 1) È RDin(BB
1)-Kill (BB 1) d2,d3 È d3,d4-d4,d5,d6
d2,d3
Repeat!
no change Þ done
Where do we start? Why?
13Reaching Definitions Solver
RDout(Entry) F RDout(b) F " b ÃŽ N-Entry,
Exit // Gen(b) is better change true while
(change) change false for each b ÃŽ
N-Entry, Exit // Depth-first order
oldout RDout(b) if (RDout(b) ¹
oldout) change true
14Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
15Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
d1,d2,d3
d4,d5
d7
d6
16Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d4,d5
d7
d6
17Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d4,d5
d7
d6
18Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d4,d5
d7
d6
19Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5
d7
d6
20Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d7
d6
21Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d7
d6
22Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d7
d6
23Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d7
d4,d5,d6
24Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d7
d4,d5,d6
25Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d7
d4,d5,d6
26Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
27Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
CHANGED!
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
28Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
29Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
30Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d4,d5,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
31Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d4,d5,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
32Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d4,d5,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
33Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
F
F
d1,d2,d3
d1,d2,d3,d4,d5,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
34Reaching Definitions - Example
d1 i m-1 d5 j j-1 d2 j n d6 a
u2 d3 a u1 d7 a u3 d4 i i1
NO CHANGE!
F
F
d1,d2,d3
d1,d2,d3,d4,d5,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5, d3,d6
d4,d5,d6
d4,d5,d7
35Reaching Definitions Questions
- Does the algorithm terminate?
- Does it produce correct answers?
- How quickly does it terminate?
- In what sense is it conservative?
36Reaching Definitions Questions
- Does the algorithm terminate?
- Does it produce correct answers?
- How quickly does it terminate?
- In what sense is it conservative?
37Data Flow Frameworks
- A monotone data flow framework (L, Ù,F) consists
of - a domain of values L.
- a confluence operator Ù L L. (meet)
- a set of transfer functions F. If f ÃŽ F, then f
L L. - (L, Ù,F) satisfies a number of conditions
- Ù defines a partial order on L.
- L contains a top element T such that T Ù X X
" X Î L. - L contains a bottom element ? such that ? Ù X ?
" X Î L. - Ù is associative X Ù (Y Ù Z) (Z Ù Y) Ù
Z. - Ù is commutative X Ù Y Y Ù X.
- Ù is idempotent X Ù X X.
- F is monotone X Y Þ f(X) f(Y) " f Î
F. - F is closed under composition " f,g ÃŽ F,
f(g(X)) ÃŽ F. - The iterative algorithm for this data flow
framework will always terminate.
38Data Flow Frameworks (cont)
- Is Reaching Definitions a monotone data flow
framework? - L is the set of definitions in the program.
- The confluence operator Ù is set union.
- The partial order is defined as X Y Þ X Ê Y.
- There is a top element (F).
- Set union is associative, commutative and
idempotent. - The transfer function is and is monotone.
- The transfer function is closed under
composition. - The iterative algorithm will terminate!
39Reaching Definitions Questions
- Does the algorithm terminate?
- Does it produce correct answers?
- How quickly does it terminate?
- In what sense is it conservative?
40Data Flow Frameworks (cont)
- Given a path P b0 b1 b2 bn-1 bn,
the transfer function at entry of bn is fp(bn)
fn-1(fn-2(f0(T)) ). - We want to compute meet-over-all-path (MOP)
solution MOP(b)
Ù fp(bn)
all paths P to b - The iterative algorithm computes the Maximal
Fixed Point (MFP) solution. - If F is distributive, i.e., " f Î F, f(X Ù Y)
f(X) Ù f(Y), then MFP MOP. Otherwise MFP MOP
(conservative?). - Most data flow problems we encounter are
distributive.
41Reaching Definitions Questions
- Does the algorithm terminate?
- Does it produce correct answers?
- How quickly does it terminate?
- In what sense is it conservative?
42Data Flow Frameworks (cont)
- A rapid data flow framework is a monotone and
distributive data flow framework such that
" f Î F and X Î L X Ù f(T) f(X) - The iterative algorithm for a rapid data flow
framework stabilize in no more than d(G) 2
passes over G, where d(G) is the maximum number
of retreating edges on any acyclic path in
G. - In practice d(G) is less than 3! Hence, the
iterative algorithm is highly effective in
practice.
43Proofs
- Gary Kildall, A Unified Approach to Global
Program Optimization, Proceedings of the First
POPL, Boston, MA, USA October 1973. If all flow
functions are monotone and distributive,
iterations yields MFP MOP. - J.B. Kam and J.D. Ullman, Monotone Data Flow
Analysis Frameworks, Acta Informatica,
7305-317, 1977. If all flow functions are
monotone, iterations yields MFP.
44Reaching Definitions Questions
- Does the algorithm terminate?
- Does it produce correct answers?
- How quickly does it terminate?
- In what sense is it conservative?
45Data Flow Frameworks (cont)
a a 1
c c 1
d1
d2
d1,d2
All Definitions that may reach a point.
46- Reaching definitions is an example of an any-path
forward-analysis data flow problem.
47Data Flow Analysis Review
- What is Data Flow Analysis
- Reaching Definitions
- Simple Example Solver
- More Realistic Example
- Characteristics of Data Flow Framework
- What is sufficient to show that they (a)
terminate (b) generate correct results and (c)
generate MOPMFP.
48General Data Flow Equations
- Any-path forward-flow (Reaching Definitions)
- Any-path backward-flow (Live Variable Analysis)
- All-path forward-flow (Available Expressions)
- All-path backward-flow (Very Busy Expressions)
49Live Variables
- A variable v is said to be live at a point p in
the program if the current value of v will be
used before being assigned a new value, or before
the program ends. Otherwise it is dead.
- Problem statement determine the set of variables
that are live at the boundaries of each basic
block in the program.
50Live Variables - Use Set
- Use (b) Set of variables that are used before
(possibly) being defined in b.
Use (BB 1) F
Use (BB 2) a, b
Use (BB 3) a, c
This is doing LV analysis in a BB (local
analysis).
51Live Variables - Def Set
- Def (b) Set of variables defined in b.
Def (BB 1) a, b
Def (BB 2) c
Def (BB 3) F
52Live Variables - Data flow Equations
- LVin(b) Set of live variable at the beginning
of b. - LVout(b) Set of live variables at the end of b.
53LV - Solving the Data Flow Equations
LVout(BB 1) LVin(BB 2) a,b LVin(BB 1) F
È LVout(BB 1)-Def(BB 1) F È a,b-a,b
F
LVout(BB 2) LVin(BB 3) È LVin(BB 2) a,c È
a,b a,b,c LVin(BB
2) Use(BB 2) È LVout(BB 2)-Def(BB 2)
a,b È a,b,c-c a,b
F
LVout(BB 3) LVin(Exit) F LVin(BB 3)
Use(BB 3) È LVout(BB 3)-Def(BB 3) a,c È
F-F a,c
Repeat!
no change Þ done
Where do we start? Why?
54Live Variables Solver
LVin(Exit) F LVin(b) F " b ÃŽ N-Entry, Exit
// USE(b) is better change true while
(change) change false for each b ÃŽ
N-Entry, Exit // Reverse DFO
oldin LVin(b) if (LVin(b) ¹ oldin)
change true
55Live Variables Solver - Questions
- Does this algorithm terminate?
- Does it compute the correct answer?
- How fast does it terminate?
- In what sense is it conservative?
56Data Flow Frameworks
- A monotone data flow framework (L, Ù,F) consists
of - a domain of values L.
- a confluence operator Ù L L. (meet)
- a set of transfer functions F. If f ÃŽ F, then f
L L. - (L, Ù,F) satisfies a number of conditions
- Ù defines a partial order on L.
- L contains a top element T such that T Ù X X
" X Î L. - L contains a bottom element ? such that ? Ù X ?
" X Î L. - Ù is associative X Ù (Y Ù Z) (Z Ù Y) Ù
Z. - Ù is commutative X Ù Y Y Ù X.
- Ù is idempotent X Ù X X.
- F is monotone X Y Þ f(X) f(Y) " f Î
F. - F is closed under composition " f,g ÃŽ F,
f(g(X)) ÃŽ F. - The iterative algorithm for this data flow
framework will always terminate.
57Partial Ordering of Lattice
T (F)
d1
d3
d1,d3
d2
d1,d2
d2,d3
? d1,d2,d3
The meet operation creates a partial order of
L. For ?, ? X T is ? ? X ? T A monotonic
framework must terminate if the height of the
Lattice is finite.
58Live Variables - Example
59Live Variables - Example
F
60Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
61Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
62Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
63Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
64Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
65Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
66Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
67Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
68Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
69Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
70Live Variables - Example
Some LVin has changed! Repeat!
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
71Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
72Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
73Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
74Live Variables - Example
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
75Live Variables - Example
No LVin has changed! Done!
c
b,d,e,g
a,b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
b,d,e,g
F
F
76Live Variables Analysis
- Example of any-path backward-analysis data flow
problem - any-path?
- backward-analysis?
- What is Live Variable Analysis good for?
77Available Expressions
- An expression expr is said to be available at the
entry to a basic block b if along every
control-flow path from Entry to block b there is
an evaluation of expr that is not killed by
having one or more of its operands assigned a new
value.
- Problem statement for each basic block b
determine the set of available expressions at its
boundaries.
78Entry
f d d
d 5 e d d
c 5 w x y
Is the expression d d available here?
Whats different between this and reaching
definitions?
79Available Expressions - Eval Set
Eval(b) Set of expressions evaluated in b and
are still available at its exit.
Eval (BB 1) xb, df
Eval (BB 2) F
Eval (BB 3) k1
Eval (BB 4) df, xb
This is doing AE analysis in a BB (local
analysis).
80Available Expressions - Kill Set
Kill(b) Set of expressions killed in b.
Kill (BB 1) F
Kill (BB 2) xb
Kill (BB 3) F
Kill (BB 4) F
81AE - Data flow Equations
- AEin(b) Set of available expression at the
beginning of b. - AEout(b) Set of available that reach the end
of b.
82Available Expressions Solver
AEout(Entry) F AEout(b) Eval (b) U U
-Kill(b) " b ÃŽ N-Entry, Exit change
true while (change) change false for
each b ÃŽ N-Entry, Exit oldout
AEout(b) if (AEout(b) ¹ oldout)
change true
Why does this work?
83Available Expressions Solver Questions(as an
exercise)
- Does this algorithm terminate?
- Does it compute the correct answer?
- How fast does it terminate?
- In what sense is it conservative?
84Available Expressions - Example
U ab, ac, c2, dd, i1
85Available Expressions - Example
F
U ab, ac, c2, dd, i1
86Available Expressions - Example
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
87Available Expressions - Example
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
88Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
89Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
90Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
91Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
92Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
93Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
94Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
95Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
U
ac,ab, dd
U
96Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
U
ac,ab, dd
U
97Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
U
ac,ab, dd
U
98Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ac,ab, dd
U
99Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ac,ab, dd
U
100Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
U
101Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
102Available Expressions - Example
An AEout has changed! Repeat!
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
103Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
104Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
105Available Expressions - Example
F
F
ab,ac,dd
ab,dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
106Available Expressions - Example
F
F
ab,ac,dd
ab,dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
107Available Expressions - Example
F
F
ab,ac,dd
ab,dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
108Available Expressions - Example
F
F
ab,ac,dd
ab,dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
109Available Expressions - Example
No AEout set has changed! Were Done.
F
F
ab,ac,dd
ab,dd
ab,dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
ab,dd
ab,dd
ac,ab, dd
ab,dd
110Available Expression Analysis
- An all-paths forward-analysis data flow problem
- all-paths?
- forward-analysis?
- What is available expressions analysis good for?
111Very-Busy Expressions
- An expression is said to be very-busy at point p
if the the expression is evaluated along all
paths before the expression is killed, or Exit.
- Problem statement determine very-busy
expressions at the boundaries of each basic block.
112Very Busy Expressions - Eval Set
Eval(b) Set of expressions evaluated before any
of their operands are (possibly) assigned to in b.
Eval (BB 1) F
Eval (BB 2) cd
Eval (BB 3) ac, cd
Eval (BB 4) ab, ac
Eval (BB 5) ab, ad
This is doing VBE analysis in a BB (local
analysis).
113Very Busy Expressions - Kill Set
Kill(b) Set of expressions killed in b.
Kill (BB 1) F
Kill (BB 2) ad,cd
Kill (BB 3) F
Kill (BB 4) F
Kill (BB 5) F
114VBE - Data flow Equations
- VBEin(b) Set of very busy expressions at the
beginning of b. - VBEout(b) Set of very busy expressions at the
end of b.
115Very Busy Expressions Solver
VBEin(Exit) F VBEin(b) Eval(b) U U
-Kill(b) " b ÃŽ N-Entry, Exit change
true while (change) change false for
each b ÃŽ N-Entry, Exit oldin
VBEin(b) if (VBEin(b) ¹ oldin)
change true
116Very Busy Expressions Solver Questions(as an
exercise)
- Does this algorithm terminate?
- Does it compute the correct answer?
- How fast does it terminate?
- In what sense is it conservative?
117Very Busy Expressions - Example
U cd, ad, ac, ab
118Very Busy Expressions - Example
U cd, ad, ac, ab
F
119Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
120Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
121Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
122Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
123Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
F
124Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
125Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
126Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
127Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
128Very Busy Expressions - Example
U
cd
U
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
129Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
130Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
131Very Busy Expressions - Example
U
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
132Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
133Very Busy Expressions - Example
VBEins have changed! Repeat!
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
134Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
135Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
136Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
137Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
ab,ac,cd
138Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
ab,ac,cd
139Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
ab,ac,cd
140Very Busy Expressions - Example
No VBEins have changed! Were all done.
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
ab,ac,cd
141Very Busy Expressions
- An example of a all-paths backward-analysis data
flow problem - all-paths?
- backward-analysis?
- What is Very Busy Expressions useful for?
142Other data flow abstractions
143Use-Definition (UD) Chains
(Muchnick Section 8.10)
- The set of definitions reaching a use of a
variable is called the use-definition chain
(ud-chain) for the variable. - UD chains are derived from reaching definitions
- Gen(b) Set of definitions that appear in b and
reach its end. - Kill(b) Set of definitions in other basic
blocks that are killed in b. - RDin(b) Set of definitions that reach the
beginning of b. - RDout(b) Set of definitions that reach the end
of b.
144Example
145Definition-Use (DU) Chains
- The set of uses reached by the definition of a
variable is called the definition-use chain
(du-chain) for the variable. - UD can also be solved as a data flow problem
- Use(b) Set of variable uses in b with no prior
definition of - the variable in b.
- Kill(b) Set of variable uses that are killed
by b. The set of - variables defined in b and used
in other BBs. - In(b) Set of uses available at the beginning
of b. - Out(b) Set of uses of available at the end of b.
146Example
147Single Static Assignment Form
- Optimization is often complicated by the fact the
variables can be assigned in multiple places. - Reaching Definitions is all definitions that
might reach the point - Available Expressions look for all expressions
that might be killed by the assignment to a
variable - Wouldnt it be nice if variables were only
assigned to in one place? - a singe unique definition point
- In SSA Form, each static definition of a
variable, x, becomes the definition of a new
variable xi - If there are n definitions then there will be an
x1 - xn - Uses of the variable must be replace with the use
of some xi
Muchnick Section 8.11
148? Functions Dealing with joins
X2 5
X1 5
X2 5
X1 5
X?
X3 ? (X1 , X2 ) X3
- Control flow cannot be predicted at compile-time,
so there may be more than 1 definition that might
reach a use. - So, insert ? (Phi) functions.
- create a new variable that is defined by a phi
function joining the reaching definitions - The new variables is assigned the value of the
definition that is reached by the runtime control
flow path, i.e. if the X1 block were executed
then ? (X1 , X2) X1.
149Example
X 1
Y X 1
X 5
Z X
X 100
W X X