Data Flow Analysis: CS 201 Compiler Construction
Data Flow Analysis: CS 201 Compiler Construction
3/31/09
3/31/09
1. Reaching Definitions
Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression evaluation. Definition d of variable v reaches a point p if there exists a path from immediately after d to p such that definition d is not killed along the path. Definition d is killed along a path between two points if there exists an assignment to variable v along the path.
5
Example
d reaches u along path2 & d does not reach u along path1 Since there exists a path from d to u along which d is not killed (i.e., path2), d reaches u.
6
3/31/09
IN[B] OUT[B]
d1: X=
3/31/09
GEN[B]: Definitions within B that reach the end of B. KILL[B]: Definitions that never reach the end of B due to redefinitions of variables in B.
9
10
3/31/09
Copy Propagation
11
2. Available Expressions
An expression is generated at a point if it is computed at that point. An expression is killed by redefinitions of operands of the expression. An expression A+B is available at a point if every path from the start node to the point evaluates A+B and after the last evaluation of A+B on each path there is no redefinition of either A or B (i.e., A+B is not killed).
12
3/31/09
Available Expressions
Available expressions problem computes: at each program point the set of expressions available at that point.
13
GEN[B]: Expressions computed within B that are available at the end of B. KILL[B]: Expressions whose operands are redefined in B.
14
3/31/09
16
3/31/09
GEN[B]: Variables that are used in B prior to their definition in B. KILL[B]: Variables definitely assigned value in B before any use of that variable in B.
17
18
3/31/09
19
Compute for each program point the set of very busy expressions at the point.
20
10
3/31/09
GEN[B]: Expression computed in B and variables used in the expression are not redefined in B prior to expressions evaluation in B. KILL[B]: Expressions that use variables that are redefined in B. 21
22
11
3/31/09
Summary
May/Union Forward Backward Reaching Definitions Live Variables Must/ Intersection Available Expressions Very Busy Expressions
23
Conservative Analysis
Optimizations that we apply must be Safe => the data flow facts we compute should definitely be true (not simply possibly true). Two main reasons that cause results of analysis to be conservative: 1. Control Flow 2. Pointers & Aliasing
24
12
3/31/09
Conservative Analysis
1. Control Flow we assume that all paths are executable; however, some may be infeasible.
25
Conservative Analysis
2. Pointers & Aliasing we may not know what a pointer points to.
1. X = 5 2. *p = 3. = X // p may or may not point to X
Constant propagation: assume p does point to X (i.e., in statement 3, X cannot be replaced by 5). Dead Code Elimination: assume p does not point to X (i.e., statement 1 cannot be deleted).
26
13
3/31/09
For each expression, variable, definition we have one bit intersection and union operations can be implemented using bitwise and & or operations.
27
28
14
3/31/09
29
30
15
3/31/09
31
16