ECE540S Optimizing Compilers - PowerPoint PPT Presentation

1 / 149
About This Presentation
Title:

ECE540S Optimizing Compilers

Description:

d2: j = n d6: a = u2. d3: a = u1 d7: a = u3. d4: i = i 1 ... a = u2. i = m-1. j = n. a = u1. F. 15. Reaching Definitions - Example. d1: i = m-1 d5: j = j-1 ... – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 150
Provided by: Michae7
Category:

less

Transcript and Presenter's Notes

Title: ECE540S Optimizing Compilers


1
ECE540SOptimizing Compilers
  • http//www.eecg.toronto.edu/voss/ece540/
  • Lecture 05, Jan. 21, 2002

2
Data Flow Analysis
3
Outline
  • Quick Overview / Motivation
  • Reaching Definitions
  • Data Flow Frameworks
  • Live Variables
  • Available Expressions
  • Very Busy Expressions

4
Data 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.

5
Data 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

6
Data 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

7
Basic Approach
Must be conservative!
8
Example 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.

9
Reaching 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.
10
Reaching 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
11
Reaching 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.
12
Reaching 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?
13
Reaching 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
14
Reaching 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
15
Reaching 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
16
Reaching 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
17
Reaching 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
18
Reaching 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
19
Reaching 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
20
Reaching 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
21
Reaching 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
22
Reaching 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
23
Reaching 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
24
Reaching 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
25
Reaching 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
26
Reaching 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
27
Reaching 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
28
Reaching 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
29
Reaching 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
30
Reaching 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
31
Reaching 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
32
Reaching 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
33
Reaching 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
34
Reaching 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
35
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • How quickly does it terminate?
  • In what sense is it conservative?

36
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • How quickly does it terminate?
  • In what sense is it conservative?

37
Data 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.

38
Data 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!

39
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • How quickly does it terminate?
  • In what sense is it conservative?

40
Data 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.

41
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • How quickly does it terminate?
  • In what sense is it conservative?

42
Data 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.

43
Proofs
  • 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.

44
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • How quickly does it terminate?
  • In what sense is it conservative?

45
Data 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.

47
Data 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.

48
General 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)

49
Live 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.

50
Live 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).
51
Live Variables - Def Set
  • Def (b) Set of variables defined in b.

Def (BB 1) a, b
Def (BB 2) c
Def (BB 3) F
52
Live 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.

53
LV - 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?
54
Live 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
55
Live Variables Solver - Questions
  • Does this algorithm terminate?
  • Does it compute the correct answer?
  • How fast does it terminate?
  • In what sense is it conservative?

56
Data 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.

57
Partial 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.
58
Live Variables - Example
59
Live Variables - Example
F
60
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
61
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
62
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
63
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
64
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
65
Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
66
Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
67
Live 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
68
Live 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
69
Live 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
70
Live 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
71
Live 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
72
Live 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
73
Live 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
74
Live 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
75
Live 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
76
Live Variables Analysis
  • Example of any-path backward-analysis data flow
    problem
  • any-path?
  • backward-analysis?
  • What is Live Variable Analysis good for?

77
Available 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.

78
Entry
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?
79
Available 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).
80
Available 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
81
AE - 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.

82
Available 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?
83
Available 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?

84
Available Expressions - Example
U ab, ac, c2, dd, i1
85
Available Expressions - Example
F
U ab, ac, c2, dd, i1
86
Available Expressions - Example
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
87
Available Expressions - Example
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
88
Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
89
Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
90
Available Expressions - Example
F
F
ab,ac,dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
91
Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,c2,dd,i1
U ab, ac, c2, dd, i1
U
U
U
92
Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
93
Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
94
Available Expressions - Example
F
F
ab,ac,dd
ab,ac, dd
ab,dd
ab,dd
U ab, ac, c2, dd, i1
U
U
U
95
Available 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
96
Available 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
97
Available 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
98
Available 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
99
Available 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
100
Available 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
101
Available 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
102
Available 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
103
Available 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
104
Available 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
105
Available 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
106
Available 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
107
Available 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
108
Available 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
109
Available 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
110
Available Expression Analysis
  • An all-paths forward-analysis data flow problem
  • all-paths?
  • forward-analysis?
  • What is available expressions analysis good for?

111
Very-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.

112
Very 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).
113
Very 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
114
VBE - 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.

115
Very 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
116
Very 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?

117
Very Busy Expressions - Example
U cd, ad, ac, ab
118
Very Busy Expressions - Example
U cd, ad, ac, ab
F
119
Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
120
Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
121
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
122
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
123
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
F
124
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
125
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
126
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
127
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
128
Very Busy Expressions - Example
U
cd
U
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
129
Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
130
Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
131
Very Busy Expressions - Example
U
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
132
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
133
Very 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
134
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
135
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
136
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
137
Very 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
138
Very 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
139
Very 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
140
Very 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
141
Very Busy Expressions
  • An example of a all-paths backward-analysis data
    flow problem
  • all-paths?
  • backward-analysis?
  • What is Very Busy Expressions useful for?

142
Other data flow abstractions
143
Use-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.

144
Example
145
Definition-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.

146
Example
147
Single 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.

149
Example
X 1
Y X 1
X 5
Z X
X 100
W X X
Write a Comment
User Comments (0)
About PowerShow.com