Professional Documents
Culture Documents
Design
Spring 2017
2
Wishlist
§ Given a statement S
§ dest = operand1 ⊕ operand2
§ Compiler would like to know at point P if there is a further
use of dest
§ I.e., there is a statement S’ that uses dest and there is a path from P
to Pbefore_S’
§ One interesting point is Pafter_S or Pafter_B (with B the basic
block of S)
§ But every point could be interesting
§ Like to know if some operandi is used again in another statement
3
Data flow problem
§ Similar to “reaching definitions” but not interested what has
happened prior to reaching P but what happens after P
§ Consider all paths that start at P and go to EXIT
§ A variable V is live at point P if there is a path from P to EXIT that
contains a statement S that uses V, and there is no definition of V
on this path between P and S.
§ Path can start anywhere
§ Often we care about paths that start after a definition of V
§ A single path with a use is enough for V to be live at P
§ A variable that is not live is called dead
§ Recall that we assume all statements have clearly identifiable
operands and destinations
4
Live variables
5
S1: V = … B
S2: W = expr
S3: = X ⊕ V
S4: = Y ⊕ 1
§ No need to store value S5: = Z
computed by S2 in
variable W
Live = {X , Z}
§ If expr has no side effect
stmt can be removed
§ V is local to basic block B
§ If Y is in a register this
register can be released
after S4
6
Transfer function
… B
d: … = var ⊕ …
…
7
Transfer function
… B
d: … = var ⊕ …
d”: var = …
8
Transfer function
d’: var = B
d: … = var ⊕ …
…
9
Transfer function
… B
d: … = var ⊕ …
…
10
12
ENTRY
Running example
d1: i = m – 1
def = {i, j, a}
d2: j = n
use = {m, n } d3: a = …
def = {}
d4: i = i + 1
use = {i, j} d5: j = j - 1
def = {a}
use = ∅ d6: a = …
def = {i}
use = ∅ d7: i = …
14
Transfer function
15
IN(B) = {v1, v2, …, vn}
Transfer function
…
d: …= var ⊕ …
…
§ Variable v ∈IN(B)
§ v is used in B prior to any definition, i.e. v ∈ useB
§ v ∈ OUT(B) and v not set by statements in B, i.e. v ∉ defB
§ IN(B) = useB ∪ (OUT(B) – defB)
16
Transfer function
B2
… …
B3
B1
...
§ Given IN(B1)
§ What should be OUT(B2) and OUT(B3)?
§ OUT(B2) = IN(B1), OUT(B3) = IN(B1)
17
18
Transfer function
... B1
B2 … B3 …
19
20
Transfer function
... B1
B2 … B3 …
§ Given IN(Bi)
§ OUT(B) = ∪ Bi, Bi is successor of B in CFG IN(Bi)
21
Finding IN and OUT
22
ENTRY
Running example
d1: i = m – 1
def = {i, j, a}
d2: j = n
use = {m, n } d3: a = …
def = {}
d4: i = i + 1
use = {i, j} d5: j = j - 1
def = {a}
use = ∅ d6: a = …
def = {i}
use = ∅ d7: i = …
24
Finding live variables
IN[EXIT] = ∅
25
Finding live variables
IN[EXIT] = ∅
26
Initialize IN[B] = ∅
while (changes to any IN(B)) {
for (each basic block B ≠ EXIT) { ENTRY
OUT(B) = ∪ Bi, Bi is successor of B in CFG IN(Bi)
IN(B) = useB ∪ (OUT(B) – defB)
} d1: i = m – 1 IN(B1) =
def = {i, j, a}
d2: j = n
use = { m, n } d3: a = … OUT(B1) =
d4: i = i + 1 IN(B2) =
def = {}
use = {i, j} d5: j = j - 1 OUT(B2) =
IN(B3) =
d6: a = …
def = {a} OUT(B3) =
use = ∅
IN(B4) =
def = {i} d7: i = …
use = ∅ OUT(B4) =