



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Data Flow Analysis
CMSC 631 — Program Analysis and
Understanding
Fall 2003
CMSC 631, Fall 2003 2
■ Simpler representation of the program
■ Fewer forms to reason about
Source
Code
Abstract
Syntax
Tree
Control
Flow
Graph
Object
Code
3
CMSC 631, Fall 2003
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
4
CMSC 631, Fall 2003
■ Works best on properties about how program
computes
■ Including infeasible paths
■ (^) e is computed on every path to p, and
■ (^) the value of e has not changed since the last time e is
computed on p
■ (^) If an expression is available, need not be recomputed
■ (^) a + b is available
■ (^) a * b is available
■ y > a + b is available
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
CMSC 631, Fall 2003 7
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
Stmt Gen Kill
x := a + b a + b
y := a * b a * b
y > a + b y > a + b
a := a + 1
a + b,
a * b,
y > a + b
CMSC 631, Fall 2003 8
■ succ(s) = { immediate successor statements of s }
■ (^) pred(s) = { immediate predecessor statements of s}
■ (^) In(s) = program point just before executing s
■ (^) Out(s) = program point just after executing s
s pred(s)
■ Note: These are also called transfer functions
9
CMSC 631, Fall 2003
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
{a + b}
{a + b, a * b}
{a + b, a * b,
y > a + b}
{a + b}
{a + b}
{a + b}
{a + b}
10
CMSC 631, Fall 2003
■ (^) v will be used on some execution path originating
from p...
■ before v is overwritten
■ (^) If a variable is not live, no need to keep it in a register
■ (^) If variable is dead at assignment, can eliminate
assignment
■ (^) Data flow propagate in same dir as CFG edges
■ (^) Expr is available only if available on all paths
■
■ To know if variable live, need to look at future uses
■ Variable is live if available on some path
s succ(s)
x := a + b
y := a * b
y > a + b
a := a + 1
x := a + b
Stmt Gen Kill
x := a + b a, b x
y := a * b a, b y
y > a + b y, a, b
a := a + 1 a a
CMSC 631, Fall 2003 19
■
■
x! ⊥ = ⊥
x! " = x
x! ⊥ = x
x! " = "
x ≤ y iff x " y = x
x ≤ y iff x # y = y
CMSC 631, Fall 2003 20
■ Example: Available expressions
a+b, a*b, y > a+b
a+b, a*b a+b, y > a+b
a+b
a*b, y > a+b
a*b y > a+b
(none)
21
CMSC 631, Fall 2003
■ Or, if you like, Out(s) = Top
■ Take s from W
■
In(s) := s pred(s)
Out(s )
■ temp := Gen(s) (In(s) - Kill(s))
■ if (temp != Out(s)) {
■ }
22
CMSC 631, Fall 2003
■ s pred(s)
Out(s)
■ (^) Gen(s) (In(s) - Kill(s))
x ≤ y ⇒ f (x) ≤ f (y)
■ Or, if you like, Out(s) = Top
■ Take s from W
■ (^) temp :=
■ (^) if (temp != Out(s)) {
■ }
f s
s ′^ ∈pred(s)
Out(s
′ ))
■ (^) The lattice has finite height
■ (^) The operations to compute In and Out are monotonic
■ (^) On every iteration, we remove a statement from the
worklist and/or move down the lattice
CMSC 631, Fall 2003 25
f (x! y) ≤ f (x)! f (y)
f (x! y) = f (x)! f (y)
CMSC 631, Fall 2003 26
f g
h
k
k(h(f (!) " g(!))) =
k(h(f (!)) " h(g(!))) =
k(h(f (!))) " k(h(g(!)))
27
CMSC 631, Fall 2003
■ Let f s
be the transfer function for statement s
■ If p is a path {s 1
, ..., s n
}, let f p
= f n
;...;f 1
■ Let path(s) be the set of paths from the entry to s
MOP(s) =! p∈ path(s)
fp (")
28
CMSC 631, Fall 2003
■ (^) Live variables
■ (^) Available expressions
■ (^) Reaching definitions
■ (^) Very busy expressions
x := 1
y := 2
x := 2
y := 1
z := x + y
■ Fact i
represented by bit i
■ Intersection = bitwise and, union = bitwise or, etc
■ (^) But very useful in practice
CMSC 631, Fall 2003 37
■ Ones that can be collapsed
■ Standard constructs yield only reducible flow graphs
■
A
B C
z w
CMSC 631, Fall 2003 38
■ The order of statements is taken into account
■ (^) I.e., we keep track of facts per program point
■ (^) Analysis the same regardless of statement order
■ Standard example: types
39
CMSC 631, Fall 2003
■ (^) (Not always followed in literature)
40
CMSC 631, Fall 2003
■ (^) Lots of proposed solutions in data flow analysis
literature
■
■ Call to function kills all data flow facts
■ (^) May be able to improve depending on language, e.g.,
function call may not affect locals
■ (^) But what about values stored in the heap?
■ (^) Not modeled in traditional data flow
■ Assume all data flow facts killed (!)
■ Or, assume x may affect any variable whose address
has not been taken