Document 7821001
Download
Report
Transcript Document 7821001
Advanced Compilers
CMPSCI 710
Spring 2003
Yet more data flow analysis
Emery Berger
University of Massachusetts, Amherst
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
Project Stuff
1 to 2 person teams
Implement optimization/analysis in:
Jikes RVM (IBM’s research Java compiler)
Broadway (UTexas “metacompiler”)
other (subject to approval)
Due dates:
02/11/03:
One-page project description.
02/25/03:
2-4 page project design.
03/25/03:
Project implementation review.
04/29/03:
Implementation due.
05/06-13/03:
In-class presentations.
05/13/03:
Project report.
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
2
Yet More Data Flow Analysis
Last time:
The iterative worklist algorithm
Today:
Live variable analysis
backwards problem
Constant propagation
algorithms
def-use chains
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
3
Live Variable Analysis
Variable x is live at point p if:
used before being redefined along some
path starting at p
Use(p):
backwards problem
variables that may be used starting at p
Def(p):
variables that may be defined in p
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
4
Use, Def, Live Variables:
Example
Use
1:
2:
3:
4:
5:
6:
Def
Live
x = 12;
y = 14;
z = x;
y = 15;
q = z + z;
halt;
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
5
Defining Live Variable Analysis
Lattice elements =
In(Exit) =
Out(v) = uP in SUCC(S)In(P)
u=
In(v) = Use(v) u (Out(v) – Def(v))
x 2 Use(v) iff x may be used before defined
Use(d: v = …x…) =
Use(d: if (…x…)) =
x 2 Def(v) iff x defined before used in v
Def(d: v = exp) =
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
6
Iterative Worklist Algorithm,
Live Variables
for v 2 V
OUT(v) =
IN(v) = Use(v)
worklist à V
while (worklist )
for v 2 worklist
oldin(v) = IN(v)
OUT(v) = up 2 SUCC(v) IN(p)
IN(v) = Use(v) u (OUT(v) – Def(v))
if (oldin(v) IN(v))
worklist à worklist [ PRED(v)
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
7
Live Variables Example
Entry
1: parameter a;
2: parameter b;
3: x=a*b;
use(1) =
use(2) =
use(3) =
use(4) =
use(5) =
use(6) =
use(7) =
def(1) =
def(2) =
def(3) =
def(4) =
def(5) =
def(6) =
def(7) =
4: y=a*b;
5: if y > a+b
6: a = a+1;
7: x = a+b;
Exit
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
8
Outline
Today:
Live variable analysis
backwards problem
Constant propagation
algorithms
def-use chains
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
9
Constant Propagation
Discovers constant variables & expressions
Propagates them as far forward as possible
Uses:
Evaluate expressions at compile-time
Eliminates dead code
e.g., debugging code
Improves effectiveness of many optimizations
Always a win
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
10
Constant Propagation Lattice,
Revisited
Meet rules:
>
… -2 -1 0 1 2 …
?
au>=a
au?=?
constant u constant = constant (if equal)
constant u constant = ? (if not equal)
Initialization:
Optimistic assumption:
all variables unknown constant = >
Pessimistic assumption:
all variables not constant = ?
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
11
Wegman & Zadeck:
TOPLAS 1991
Relates &
improves on
previous constant
propagation
algorithms
Sparsity
incorporates info
from branches
conditional
constant
faster
improves speed
Conditional:
simple
constant
more
constants
sparse
simple
constant
faster
more
constants
sparse
conditional
constant
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
12
simple
constant
Kildall’s Algorithm
Worklist-based:
faster
sparse
simple
constant
conditional
constant
faster
more
constants
sparse
conditional
constant
add successors of Entry
remove and examine a node from worklist
evaluate expressions to compute new In and Out
if the Out value changes,
more
constants
add successors to worklist
Finds simple constants:
no information about direction of branches
one value per variable along each path
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
13
simple
constant
Kildall’s Algorithm:
Example
more
constants
faster
sparse
simple
constant
conditional
constant
faster
more
constants
sparse
conditional
constant
iÃ1
i à 1
j à 2
if (j == 2)
i à 3
z à i
jÃ2
if (j==2)
iÃ3
zÃi
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
14
simple
constant
Kildall’s Algorithm:
Analysis
In terms of N, E, V:
faster
sparse
simple
constant
conditional
constant
faster
more
constants
sparse
conditional
constant
N = assignments + expressions in branches
more
constants
for convenience: N = nodes in CFG
E = edges in CFG
V = variables
Iterations = 2 * V * I (in-edges)
Runtime = iterations * operations
=
Space = lattice values
=
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
15
simple
constant
Reif & Lewis
Kildall’s (SC):
more
constants
conditional
constant
faster
sparse
simple
constant
faster
more
constants
sparse
conditional
constant
at each node, computes value of all variables at
entry and produces set of values for all
variables at exit
Reif & Lewis (SSC):
also finds simple constants, but faster
sparse representation
original formulation based on def-use graph
revised version based on SSA form
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
16
Def-Use Graph
Graph of def-use chains:
connection from definition site
(assignment) to use site along path in CFG
does not pass through another definition
Includes infeasible paths
misses some constants
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
17
Def-Use Graph:
Example
i à 1
j à 2
if (j == 2)
i à 3
z à i
iÃ1
jÃ2
if (j==2)
iÃ3
zÃi
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
18
simple
constant
Reif and Lewis
Worklist:
more
constants
faster
sparse
simple
constant
conditional
constant
faster
more
constants
sparse
conditional
constant
Put root edges from def-use graph in worklist
if def site in roots can be evaluated to constant,
assign that to variable, otherwise ?
assign all other variables >
remove def-join edges from worklist:
propagate value of def to use using meet rules
if value is lowered, add node to worklist
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
19
Reif and Lewis:
Example
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
20
simple
constant
Wegman & Zadeck:
Conditional Definition
more
constants
faster
sparse
simple
constant
faster
more
constants
Conditional definition:
keeps track of conditional branches
conditional
constant
sparse
conditional
constant
form of dead code elimination
constant expr in branch
) mark appropriate branch as executable
use symbolic execution to mark edges
ignore non-executable edges at joins when
propagating constants
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
21
Def-Use Chains: Problem
switch
case
case
case
switch
case
case
case
(j)
x: i
y: i
z: i
(k)
x: a
y: b
z: c
à 1;
à 2;
à 3;
à i;
à i;
à i;
worst-case size of graph =
O(?)
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
22
Next Time
“SSA is a better way”
Dominance & dominance frontiers
Control dependence
Read ACDI Chapter 8, pp. 252—258
UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
23