Traverse the control flow graph (CFG) and gather information about what may happen at run time.
which variables are live? {b}
B0: a = 0
↓ which variables are live? {a, b}
B1: c = 0
↓ which variables are live? {a, b, c}
----- B2: a < N <-----------------------------------------------------|
| ↓ which variables are live? {a} ∪ ({b, c}- {b}) = {a, c} |
| B3: b = a + 1 |
| ↓ which variables are live? {b, c} ∪ ({b, c} - {c}) = {b, c} |
| B4: c = c + b |
| ↓ which variables are live? {b} ∪ ({a,b,c} - {a}) = {b, c} |
| B5: a = b + 2 ----------------------------------------------------|
|
| which variables are live? {b}
|---> B6: return b
which variables are live? {b}
B0: a = 0
↓ which variables are live? {a, b}
B1: c = 0
↓ which variables are live? {a, b, c}
----- B2: a < N <-----------------------------------------------------|
| ↓ which variables are live? {a} ∪ ({b, c}- {b}) = {a, c} |
| B3: b = a + 1 |
| ↓ which variables are live? {b, c} ∪ ({b, c} - {c}) = {b, c} |
| B4: c = c + b |
| ↓ which variables are live? {b} ∪ ({a,b,c} - {a}) = {b, c} |
| B5: a = b + 2 ----------------------------------------------------|
|
| which variables are live? {b}
|---> B6: return b
Forward | Backward | |
---|---|---|
may | ||
must |
Forward | Backward | |
---|---|---|
may | Liveness | |
must |
B1: x = 1
↓
B2: y = x + 1
B0: y < N -------------
↓ ↓
B1: x = 1 B3: x = 0
↓ |
B2: y = x + 1 <--------
B1: x = 1
↓
B2: y = x + 1
B0: y < N -------------
↓ ↓
B1: x = 1 B3: x = 0
↓ |
B2: y = x + 1 <--------
B1: x = 1
↓
B2: y = x + 1
B0: y < N -------------
↓ ↓
B1: x = 1 B3: x = 0
↓ |
B2: y = x + 1 <--------
x
defined?x
defined as a constant? B1: x = 1
↓ RD(x) = {??}
B2: y = x + 1
B0: y < N -------------------------
↓ ↓
B1: x = 1 B3: x = 0
↓ RD(x) = {??} |
B2: y = x + 1 <--------------------
B1: x = 1
↓ RD(x) = {B1}
B2: y = x + 1
B0: y < N -----------------------------
↓ ↓
B1: x = 1 B3: x = 0
↓ RD(x) = {B1, B3} |
B2: y = x + 1 <------------------------
B1: x = 1
↓ RD(x) = {B1}
B2: y = x + 1
B0: y < N -----------------------------
↓ ↓
B1: x = 1 B3: x = 0
↓ RD(x) = {B1, B3} |
B2: y = x + 1 <------------------------
Forward | Backward | |
---|---|---|
may | ||
must |
B1: x = 1
↓ RD(x) = {B1}
B2: y = x + 1
B0: y < N -----------------------------
↓ ↓
B1: x = 1 B3: x = 0
↓ RD(x) = {B1, B3} |
B2: y = x + 1 <------------------------
Forward | Backward | |
---|---|---|
may | RD | |
must |
B1: x = 1
↓ RD(x) = {B1}
B2: y = x + 1
B0: y < N -----------------------------
↓ ↓
B1: x = 1 B3: x = 0
↓ RD(x) = {B1, B3} |
B2: y = x + 1 <------------------------
x
is an instruction that assigns a value to x
. * Liveness Analysis
(V - {y}) ∪ {x}
↑
-------------
| y = x + 1 |
-------------
↑
V
IN[I] = (OUT[I] - def[I]) ∪ use[I]
* Liveness Analysis * Reaching Definitions Analysis
(V - {y}) ∪ {x} V
↑ ↓
------------- ----------------
| y = x + 1 | | d: y = x + 1 |
------------- ----------------
↑ ↓
V
IN[I] = (OUT[I] - def[I]) ∪ use[I]
* Liveness Analysis * Reaching Definitions Analysis
(V - {y}) ∪ {x} V
↑ ↓
------------- ----------------
| y = x + 1 | | d: y = x + 1 |
------------- ----------------
↑ ↓
V (V - defs(y)) ∪ {d}
IN[I] = (OUT[I] - def[I]) ∪ use[I]
* Liveness Analysis * Reaching Definitions Analysis
(V - {y}) ∪ {x} V
↑ ↓
------------- ----------------
| y = x + 1 | | d: y = x + 1 |
------------- ----------------
↑ ↓
V (V - defs(y)) ∪ {d}
IN[I] = (OUT[I] - def[I]) ∪ use[I] OUT[I] = (IN[I] - kill[I]) ∪ gen[I]
* Liveness Analysis
(V - {y}) ∪ {x}
↑
-------------
| y = x + 1 | <----- V2
-------------
↑
V1
OUT[B] = V1 ∪ V2
* Liveness Analysis * Reaching Definitions Analysis
(V - {y}) ∪ {x} V1
↑ ↓
------------- ----------------
| y = x + 1 | <----- V2 | d: y = x + 1 | <----- V2
------------- ----------------
↑ ↓
V1
OUT[B] = V1 ∪ V2
* Liveness Analysis * Reaching Definitions Analysis
(V - {y}) ∪ {x} V1
↑ ↓
------------- ----------------
| y = x + 1 | <----- V2 | d: y = x + 1 | <----- V2
------------- ----------------
↑ ↓
V1
OUT[B] = V1 ∪ V2 IN[B] = V1 ∪ V2
gen[B]: the set of definitions generated in B.
kill[B]: the set of definitions killed in B.
1: a = 5
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | ?? | ?? |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} |
6: c = 0
1: a = 5 Iteration 1
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | {1,2} | {2,5} |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} |
6: c = 0
1: a = 5 Iteration 1 Iteration 2
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} | ?? | ?? |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} | {1,2,4} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | {1,2} | {2,5} | {1,2,4} | {2,4,5} |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} | {2,4,5} | {5,6} |
6: c = 0
1: a = 5 Iteration 1 Iteration 2
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} | {1,2,4} | {1,2,4} |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} | {1,2,4} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | {1,2} | {2,5} | {1,2,4} | {2,4,5} |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} | {2,4,5} | {5,6} |
6: c = 0
1: a = 5 Iteration 1 Iteration 2
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} | {1,2,4} | {1,2,4} |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} | {1,2,4} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | {1,2} | {2,5} | {1,2,4} | {2,4,5} |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} | {2,4,5} | {5,6} |
6: c = 0
1: a = 5 Iteration 1 Iteration 2 Iteration 3
↓ | n | gen(n) | kill(n) | IN[n] | OUT[n] | IN[n] | OUT[n] | IN[n] | OUT[n] |
2: c = 1 |:---:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
↓ | 1 | {1} | {5} | ∅ | {1} | ∅ | {1} | ∅ | {1} |
-----> 3: c > a ------------- | 2 | {2} | {4,6} | {1} | {1,2} | {1} | {1,2} | {1} | {1,2} |
| ↓ | | 3 | ∅ | ∅ | {1,2} | {1,2} | {1,2,4} | {1,2,4} | {1,2,4} | {1,2,4} |
| 4: c = c + c ↓ | 4 | {4} | {2,6} | {1,2} | {1,4} | {1,2,4} | {1,4} | {1,2,4} | {1,4} |
|-------| 5: a = c - a | 5 | {5} | {1} | {1,2} | {2,5} | {1,2,4} | {2,4,5} | {1,2,4} | {2,4,5} |
↓ | 6 | {6} | {2,4} | {2,5} | {5,6} | {2,4,5} | {5,6} | {2,4,5} | {5,6} |
6: c = 0