































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!
































Nodes of graph: basic blocks (straight-line computations)
Edges of graph: flows of control
Detect loops, remove redundant computations, register
allocation, instruction scheduling…
i = 0
while (i < 50) {
t1 = b * 2;
a = a + t1;
i = i + 1;
if I < 50
t1 := b * 2;
a := a + t1;
i = i + 1;
i =0;
Identify all the basic blocks
Create a flow graph node for each basic block
For each basic block B
If B1 ends with a jump to a statement that starts basic block B2,
create an edge from B1 to B
If B1 does not end with an unconditional jump, create an edge from
B1 to the basic block that immediately follows B1 in the original
evaluation order
……
i := 0
s0: if i < 50 goto s
goto s
s1: t1 := b * 2
a := a + t
goto s
S2: …
S0: if i < 50 goto s
goto s
s1: t1 := b * 2
a := a + t
goto s
i :=
Example Dataflow
Live variable analysis
A data-flow analysis problem
Live variable analysis can be used in
Dead variables no longer need to be in registers
Dead variable don’t need to be stored back to memory
No variable should be alive at program entry point
All variables inside a function
UEVar(n)
vars used before defined
VarKill(n)
vars defined (killed by n)
LiveOut(n)=∪m∈succ(n)LiveIn(m)
LiveIn(m)=UEVar(m) ∪
(LiveOut(m)-VarKill(m))
LiveOut(n)= ∪ m∈succ(n)
(UEVar(m) ∪
(LiveOut(m)-VarKill(m))
m:=a+b
n:=a+b
p:=c+d
r:=c+d
q:=a+b
r:=c+d
e:=b+
s:=a+b
u:=e+f
e:=a+
t:=c+d
u:=e+f
v:=a+b
w:=c+d
m:=a+b
n:=c+d
UEVar(n)=variables used before any definition in n
VarKill(n)=variables defined (modified) in n (killed by n)
LiveOut(n)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m))
m∈succ(n)
for each basic block bi
compute UEVar(bi) and VarKill(bi)
LiveOut(bi) := ∅
for (changed := true; changed; )
changed = false
for each basic block bi
old = LiveOut(bi)
LiveOut(bi)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m))
if (LiveOut(bi) != old) changed := true
m∈succ(bi)
The aim of the Available Expressions Analysis is to
determine
Optimized code:
All expressions within a
function
DEexp(n)
Exps evaluated without any
operand redefined
ExpKill(n)
Exps whose operands are
redefined (exps killed by n)
AvailOut(m) = DEexp(m)∪
(AvailIn(m)-ExpKill(m))
==>
(DEexp(m) ∪
(AvailIn(m)-ExpKill(m))
m:=a+b
b:=c+d
p:=a+b
e:=c+d
q:=a+b
e:=c+d
e:=b+
s:=a+b
a:=e+f
e:=a+
t:=c+d
b:=e+f
w:=a+b
X:=e+f
y:=a+b
c:=c+d
m:=a+b
b:=c+d
p:=a+b
e:=c+d
q:=a+b
e:=c+d
e:=b+
s:=a+b
a:=e+f
e:=a+
t:=c+d
b:=e+f
w:=a+b
X:=e+f
y:=a+b
c:=c+d
1,2 2 12345 1, G
12345 2, ∅
1, F
2,4,5 1,3,4 12345 1, E
3,4 1,4,5 12345 1, D
1,2 4 12345 2 C
1,2 4 12345 2 B
2 1, A
DEexp ExpKil Avail Avail
Domain: a+b(1), c+d(2),
b+18(3),e+f(4), a+17(5)
Does the algorithm always
terminate?
If the result sets are
bounded and grow
monotonically, then yes;
Otherwise, no.
Fixed-point solution is
independent of evaluation
order
What answer does the
algorithm compute?
Unique fixed-point solution
The meet-over-all-paths
solution
How long does it take the
algorithm to terminate?
Depends on traversing order
of basic blocks
for each basic block bi
compute Gen(bi) and Kill(bi)
Result(bi) := ∅ or Domain
for (changed := true; changed; )
changed = false
for each basic block bi
old = Result(bi)
Result(bi)=
∩ or ∪
[m∈pred(bi) or succ(bi)]
(Gen(m) ∪ (Result(m)-Kill(m))
if (Result(bi) != old)
changed := true
Input(n) := ∅ if n is program entry/exit
Result(n) = ƒn (Input(n))
where Λ is ∩ or ∪ (may vs. must analysis)
May analysis: detect properties satisfied by at least one path (∪)
Must analysis: detect properties satisfied by all paths(∩)
Forward flow: data flow forward along control-flow edges.
Input(n) is data entering n, Result is data exiting n
Input(n) is ∅ if n is program entry
Backward flow: data flow backward along control-flow edges.
Input(n) is data exiting n, Result is data entering n
Input(n) is ∅ if n is program exit
The Mathematical Foundation of
Dataflow Analysis
Mathematical formulation of dataflow analysis
The combination operator Λ: P(L) → L is used to combine
A set P is an ordered set if a partial order ≤ can be
defined s.t. ∀x,y,z∈P
x ≤ x (reflexive)
Example: Power(L) with ⊆ define the partial order
Each subset Y ⊆ L has a least upper bound and a greatest lower bound
LeastUpperBound(Y) = Vm∈Y m
GreatestLowerBound(Y) = Λ m∈Y m
For any x, y∈I, x Λ y = min(x,y), x V y = max(x,y)
But LeastUpperBound(I) does not exist
I ∪ {+∞,−∞} is a complete lattice
A top element: the least element
A bottom element: the greatest element
For all sequences x
1
≤ x
2
≤ …, there exists n such that
x
n
= x
n+
That is, all chains in S have an finite upper bound
If l1≤ l2 ≤ l3 ≤ … , then there is an upper bound ln = ln+1=ln+2…
This means starting from an arbitrary element e ∈ L, one can only
increase e by a finite number of times before reaching an upper bound