Flow Analysis
Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM
Flow Analysis Data-flow analysis, Control-flow analysis, Abstract - - PowerPoint PPT Presentation
Flow Analysis Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM Helpful Reading: Sections 1.1-1.5, 2.1 Data-flow analysis (DFA) A framework for statically proving facts about program data. Focuses on simple, finite
Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM
Helpful Reading: Sections 1.1-1.5, 2.1
define fact(n : int) { s := 1; while (n > 1) { s := s*n; n := n-1; } return s; } s := 1;
fact(n) entry
n > 1 s := s*n; return s; n := n-1;
fact(n) exit
s := 1;
fact(n) entry
n > 1 s := s*n; return s; n := n-1;
fact(n) exit
have a single entry/exit.
block or a single statement.
has 1+ predecessor and 1+ successor (stmt or block).
diverge and a join point is where paths come together.
assignments may reach each variable reference (use).
down all possible paths forward.
entry (if entry(s) grows, exit(s) grows); entry for exits of preds.
exit(s) = ∅ W = all statements s //worklist while W not empty: remove s from W entry(s) = ∪s’∈pred(s)exit(s’) update = (entry(s) \ kill(s)) ∪ gen(s) if update != exit(s): exit(s) = update W = W ∪ succ(s)
s := 1;1
fact(n)0 entry
n > 12 s := s*n;3 return s;5 n := n-1;4
fact(n) exit
Stmt GEN KILL
s := 1;1 (s,1) (s,*) n > 12 s := s*n;3 (s,3) (s,1) (s,3) n := n-1;4 (n,4) (n,0) (n,4) return s;5
s := 1;1
fact(n)0 entry
n > 12 s := s*n;3 return s;5 n := n-1;4
fact(n) exit (n,0) (n,0) (s,1) (n,0) (s,1) (n,0) (s,3) (n,4) (s,3) (n,4) (s,3) (n,4) (s,3) (n,4)
s := 1;1
fact(n)0 entry
n > 12 s := s*n;3 return s;5 n := n-1;4
fact(n) exit RDexit(0) = {(n,0)} RDexit(1) = RDentry(1)\all(s) ∪ {(s,1)} RDexit(2) = RDentry(2) RDexit(3) = RDentry(3)\all(s) ∪ {(s,3)} RDexit(4) = RDentry(4)\all(n) ∪ {(n,4)} RDexit(5) = RDentry(5) all(x) = {(x,ℓ) | ∀ℓ} RDentry(1) = RDexit(0) RDentry(2) = RDexit(1) ∪ RDexit(4) RDentry(3) = RDexit(2) RDentry(4) = RDexit(3) RDentry(5) = RDexit(2)
upper bounds) and meets (greatest lower bounds).
{(s,1),(s,3),(n,4)}
∅
{(s,1),(n,4)} {(s,3)} {(s,1)} {(n,4)} {(s,1),(s,3)} {(s,3),(n,4)}
⊤ ⟂
upper bounds) and meets (greatest lower bounds).
co-domain is a lattice.
are defined in terms of one another. Written as a vector of sets:
RD = F(RD) = Fn(⟂) for some n RD
F(…,RDexit(1),…,RDexit(4),…) = (…,RDexit(1) ∪ RDexit(4),…)
RD0 ⊑ RD1 ⇒ F(RD0) ⊑ F(RD1)
paths forward before any subexpressions change value.
for expressions containing the right hand side (assigned var).
s’∈succ(s) at each fork point to obtain exit(s).
s := 1;1
fact(n)0 entry
n > 12 s := s*n;3 return s;5 n := n-1;4
fact(n) exit
Stmt GEN KILL
s := 1;1 1 s s*n n > 12 s := s*n;3 s*n s s*n n := n-1;4 n-1 n-1 s*n return s;5
s := 1;1
fact(n)0 entry
n > 12 s := s*n;3 return s;5 n := n-1;4
fact(n) exit n-1
∅
s*n, n-1 ∅ ∅ ∅ ∅ 1
May Must Forward Forward, computes exit(s) from entry(s) Join (∪) at CFG join points e.g., Reaching Defs (use-def) (which assignments reach uses) Forward, computes exit(s) from entry(s) Meet (∩) at CFG join points e.g., Available Expressions Backward Backward, computes entry(s) from exit(s) Join (∪) at CFG fork points e.g., Live Variables Backward, computes entry(s) from exit(s) Meet (∩) at CFG fork points e.g., Very Busy Expressions
exit(s) = ⟂ W = all statements s //worklist while W not empty: remove s from W entry(s) = ∪s’∈pred(s)exit(s’) update = (entry(s) \ kill(s)) ∪ gen(s) if update != exit(s): exit(s) = update W = W ∪ succ(s)
exit(s) = ⊤ // except ⟂ at function entry W = all statements s //worklist while W not empty: remove s from W entry(s) = ∩s’∈pred(s)exit(s’) update = (entry(s) \ kill(s)) ∪ gen(s) if update != exit(s): exit(s) = update W = W ∪ succ(s)
entry(s) = ⟂ W = all statements s //worklist while W not empty: remove s from W exit(s) = ∪s’∈succ(s)entry(s’) update = (exit(s) \ kill(s)) ∪ gen(s) if update != entry(s): entry(s) = update W = W ∪ succ(s)
entry(s) = ⊤ // except ⟂ at function exit W = all statements s //worklist while W not empty: remove s from W exit(s) = ∩s’∈succ(s)entry(s’) update = (exit(s) \ kill(s)) ∪ gen(s) if update != entry(s): entry(s) = update W = W ∪ succ(s)
analyses, given a precise semantics for the target language.
and a Galois connection between concrete and abstract.
used to justify or calculate an abstract interpretation:
^ ^
α∘ F ∘ γ ⊑ F
^
α( x ) ⊑ x if and only if x ⊑ γ( x )
^ ^
X
^
X
x
^
x
^
γ( )
x
α( x ) γ α
Simple Types Values
{1,2,3,…} {1} {2} {3} …
α( 2 ) = Pos-Int γ α
{…,-1,0,1,…}
Pos-Int Int γ
⊤ ⟂ 1 2 … “a” … #f void … … … …
(lambda (k f x y) (let ([a (prim + x y)]) (f k a)))
What value can f be? Depends on call sites for the lambda that binds it.
(lambda (k f x y) (let ([a (prim + x y)]) (f k a)))
Where does control propagate from this call-site? Depends on the possible values of parameter f.
Data-flow and control-flow properties are thoroughly entangled and mutually dependent.
Simultaneously model control-flow behavior and data-flow behavior in a single analysis.
analysis of all interdependent program properties.
methodology for deriving CFAs from concrete (precise) abstract machines! (Van Horn and Might 2010)
State: Exp x Env Env: Var → Clo Clo: Lam x Env ((ae0 … aej), env) → (e0, env’), where
A(lam,env) = (lam,env) A(x,env) = env(x)
((lambda (x1 … xj) e0),envc) = A(ae0) cloi = A(aei) env’ = envc[xi→cloi]
DOMAINS ATOMIC EVAL SMALL-STEP TRANSITION
e
Env: Var → Clo Exp x Env
State: Exp x Env X Store Env: Var → Addr Store: Addr → Clo Clo: Lam x Env Addr: some infinite set
A(lam,env,st) = (lam,env) A(x,env,st) = st(env(x))
DOMAINS ATOMIC EVAL
((ae0 … aej), env, st) → (e0, env’, st’),
where
((lambda (x1 … xj) e0),envc) = A(ae0) cloi = A(aei) env’ = envc[xi→alloc(xi)]
SMALL-STEP TRANSITION
alloc(xi) = fresh address = xi (yields 0-CFA) st’ = st[alloc(xi)→cloi]
State: Exp x Env X Store Env: Var → Addr Store: Addr → Clo Clo: Lam x Env
State: Exp x Env X Store Env: Var → Addr Store: Addr → Clo: Lam x Env Clo ℘( )
… … Int
Int Zero 1 2 3 Positive Negative … …
abstraction
Cousot and Cousot (1977), Cousot (2000)
… …
abstraction
[x → ax, y → ay, f → af]
[ax → {Int}, ay → {Int}, af → {(λ (w) e1), (λ (z) e2)}] )
(f x)
[x → ax, y → ay, f → af]
[ax → {Int}, ay → {Int}, af → {(λ (w) e1), (λ (z) e2)}] )
(f x)
e
concrete transition abstract transition abstraction abstraction
addresses
values
✔ ✔ ✔ ✔ ✔
… … … …
Control-flow graph per-point stores
✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
… …
… … … …
Control-flow graph Global store
✔ ✔ ✔ ✔ ✔
call Control-flow graph (of call-sites) Variables map to sets of lambdas
✔ ✔ ✔ ✔ ✔
call call call
lambdas
variables