ECE540S Optimizing Compilers - PowerPoint PPT Presentation

1 / 145
About This Presentation
Title:

ECE540S Optimizing Compilers

Description:

... effect at beginning/end of BB. Global: within a procedure (across ... 11. Reaching Definitions - Solving the Data Flow Equations. c = c c. Exit. BB 1. BB 2 ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 146
Provided by: Michae7
Category:

less

Transcript and Presenter's Notes

Title: ECE540S Optimizing Compilers


1
ECE540SOptimizing Compilers
  • http//www.eecg.toronto.edu/voss/ece540/
  • Data Flow Analysis, Jan. 20, 2004

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

3
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.

4
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

5
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

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

8
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 gt 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.
9
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 gt 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
10
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.
11
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?
12
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
13
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
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
d1,d2,d3
d4,d5
d7
d6
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
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
d1,d2,d3,d6
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, d3,d6
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
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
d4,d5,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
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
d4,d5,d6
d4,d5,d7
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
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
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
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,d4,d5,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
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
34
Reaching Definitions Questions
  • Does the algorithm terminate?
  • Does it produce correct answers?
  • In what sense is it conservative?

35
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.
  • The semilattice (L, Ù) satisfies the 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) (X Ù Y) Ù Z.
  • Ù is commutative X Ù Y Y Ù X.
  • Ù is idempotent X Ù X X.
  • F is a family of transfer functions that satisfy
  • F is monotone X Y Þ f(X) f(Y) " f Î
    F.
  • F is closed under composition " f,g Î F, f(g(X))
    Î F.

36
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.
37
Meet, partial orders and height
  • Ù defines a partial order on L
  • x y iff x Ù y x
  • reflexive x x
  • antisymmetric if x y and y x then x y
  • transitive if x y and y z then x z
  • For L, Ù is
  • idempotent x Ù x x
  • commutative x Ù y y Ù x
  • associative x Ù (y Ù z) (x Ù y) Ù z
  • Important examples of meet operators
  • Union, U
  • Intersection, n
  • The height of a lattice is the largest number of
    relations that fit in a descending chain.

38
Transfer Functions
  • F is a family of transfer functions
  • f L ? L
  • F is closed under composition iff
  • " f,g Î F, f(g(X)) Î F
  • A framework (L, Ù , F) is monotone iff
  • X Y Þ f(X) f(Y) " f Î F
  • A framework is also monotone iff
  • f(X Ù Y) f(X) Ù f(Y) " f Î F

39
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) and bottom element.
  • 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!

40
Proof in words
  • Set initial conditions at all points to T
  • A points value cannot go up (new old)
  • first application of function/meet result T
  • If input to meet gets smaller, result old
    result
  • If input to transfer function gets smaller,
    result old result
  • Values go down due to constraints in equations
  • Framework iterates until equations are satisfied
  • The result is the largest (closest to T) solution

41
What does the solution mean?
  • 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 cant know at compile-time which path might
    possibly execute, so we want to compute the
    meet-over-all-path (MOP) solution
  • MOP(b) Ù
    fp(bn)
    all paths P to b
  • Iteration computes the Maximal Fixed Point (MFP).
  • If F is distributive (" f Î F, f(X Ù Y) f(X)
    Ù f(Y))
  • then MFP MOP. Otherwise MFP MOP
    (conservative).
  • FP MFP MOP Ideal FP, MFP and MOP are
    safe.
  • If distrubtive, FP MFP MOP Ideal
  • Most data flow problems we encounter are
    distributive.

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

43
Data Flow Frameworks (cont)
a a 1
c c 1
d1
d2
d1,d2
All Definitions that may reach a point.
44
  • Reaching definitions is an example of an any-path
    forward-analysis data flow problem.

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

46
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)

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

48
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).
49
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
50
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.

51
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?
52
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
53
Live Variables Solver - Questions
  • Does this algorithm terminate?
  • Does it compute the correct answer?
  • In what sense is it conservative?

54
Live Variables - Example
55
Live Variables - Example
F
56
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
57
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
58
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
59
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
F
F
60
Live Variables - Example
c
b,d
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
61
Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
62
Live Variables - Example
c
b,d,e,g
a,b,d,e,g
b,d,e,g
b,d,e,g
F
F
63
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
64
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
65
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
66
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
67
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
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
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
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
72
Live Variables Analysis
  • Example of any-path backward-analysis data flow
    problem
  • any-path?
  • backward-analysis?
  • What is Live Variable Analysis good for?

73
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.

74
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?
75
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).
76
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
77
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.

78
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?
79
Available Expressions Solver Questions(as an
exercise)
  • Does this algorithm terminate?
  • Does it compute the correct answer?
  • In what sense is it conservative?

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

107
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.

108
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).
109
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
110
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.

111
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
112
Very Busy Expressions Solver Questions(as an
exercise)
  • Does this algorithm terminate?
  • Does it compute the correct answer?
  • In what sense is it conservative?

113
Very Busy Expressions - Example
U cd, ad, ac, ab
114
Very Busy Expressions - Example
U cd, ad, ac, ab
F
115
Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
116
Very Busy Expressions - Example
U
cd,ac,ab
U
U
U
U cd, ad, ac, ab
F
117
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
118
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
119
Very Busy Expressions - Example
U
cd
U
F
U
U
U cd, ad, ac, ab
F
F
120
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
121
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
122
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
123
Very Busy Expressions - Example
U
cd
U
F
ab,ac
U
U cd, ad, ac, ab
F
F
U
124
Very Busy Expressions - Example
U
cd
U
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
125
Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
126
Very Busy Expressions - Example
U
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
127
Very Busy Expressions - Example
U
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
128
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
129
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
130
Very Busy Expressions - Example
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
U
131
Very Busy Expressions - Example
cd
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
cd
cd
cd
ab,ac,cd
F
ab,ac
ab,ac
U
U cd, ad, ac, ab
F
F
ab,ac,cd
134
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
135
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
136
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
137
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?

138
Other data flow abstractions
139
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.

140
Example
141
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.

142
Example
143
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 replaced with the
    use of some xi

Muchnick Section 8.11
144
? 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 variable 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.

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