# Lecture 015

## Dataflow Analysis

Say if we want to optimize the following program

x = 1
if y:
pass
else:
pass
z = x

optimize to

x = 1
if y:
pass
else:
pass
z = 1


It is possible because in neither of the branch, x is assigned. But if only one of the branch assign new value to x, optimization will not work. However, if both branch do reassignment to the same value, we can still optimize.

The condition in which optimization is possible is not trivial to check. It is difficult for loops. Checking condition requires global dataflow analysis.

Global Optimization

• optimization depend on knowing X at particular point in program

• proving X at any point require knowledge of entire program (expensive)

• it is ok to be conservative and say "I don't know" (just don't optimize)

### Constant Propagation

Value Interpretation
$\perp$ statement never execute
$C$ X = constant
$T$ X is not constant

To automatically analyze the program, we define

• a set of rules determine the state of a variable before the execution of the current statement

• a set of rules determine the state of a variable after the execution of the current statement

### Loop Analysis

It is still possible to analyze rule by initially setting all variables to $\perp$.

### Orderings

Ordering: we define $\perp < c < T$ and so we can have operation $lub()$ on them

• $lub(\perp, 1) = 1$

• $lub(\perp, T) = T$

• $lib(1, 2) = T$ (this is because $c$ does not have internal ordering)

Now we can summarize the rules to

C(s, x, in) = lib \{\C(p, x, out) | p \text{ is a predecessor of } s}

This ordering also shows that our algorithm must terminate (ie. no oscillation) because the value only increase. Therefore the algorithm cost is bounded to $4 \times \test{the number of program statements}$

### Liveness Analysis

Liveness: we want to be able to illuminate unused assignments (dead statement). A statement s with variable x is life if:

• there exists a statement s' that uses x

• there is a path from s to s'

• that path has no intervening assignment to x

if a statement is not live, then its dead

Since we initialize every point to false and true can never be changed to false. Our algorithm terminates.

Constant propagation is a forward analysis while liveness analysis is a backward analysis.

Table of Content