int x; int x;
x = 10; --->
x = 20; x = 20;
printf("%d", x); printf("%d", x);
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
l1: x = 5
x is defined at l1.l2: if (x > 0)
x is used at l2.l3: x = x + 1
x is used and redefined at l3.l1: x = 5;
l2: z = 10;
l3: x = x + 1;
l4: y = x + z;
x at location l1 reaches location l3.x at l3.x at l4.
x at l4. ┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
x?
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
x?
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
x?
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
x?
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
true │ │ false
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
└─────────┬────┘ └────┬─────────┘
│ │
└─────┬─────┘
│
▼
┌────────────────┐
│ l5: y = x + 1; │
└────────────────┘
x?
Information of interest: Set of definitions that may reach each program point
Transfer function
Join function
Information of interest: Set of definitions that may reach each program point
Transfer function
Join function
Constant propagation
┌───────────────┐
│ l1: x = 5; │
│ l2: y = 0; │
└───────┬───────┘
│ {x = 5, y = 0}
▼
┌────────────┐
│ l3: if (b) │
└──┬─────┬───┘
{x = 5, y = 0} │ │ {x = 5, y = 0}
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l4: y=x + 1; │ │ l5: y=x + 2; │
└─────────┬────┘ └────┬─────────┘
{x = 5, y = 6} │ │ {x = 5, y = 7}
└─────┬─────┘
│
▼
┌────────────────┐ {x = 5}
│ l6: z=x*2; │
│ l7: w=y*2; │
└────────────────┘ {x = 5, z = 10}
Reaching Definitions
┌───────────────┐
│ l1: x = 5; │
└───────┬───────┘
│ {l1}
▼
┌────────────┐
│ l2: if (b) │
└──┬─────┬───┘
{l1} │ │ {l1}
▼ ▼
┌──────────────┐ ┌──────────────┐
│ l3: x = 10; │ │ l4: x = 20; │
{l3}└─────────┬────┘ └────┬─────────┘ {l4}
│ │
└─────┬─────┘
│
▼
┌────────────────┐ {l3, l4}
│ l5: y = x + 1; │
└────────────────┘ {l3, l4, l5}
Forward Analysis
Backward Analysis
x is live at program point p if there exists a path from p to a use of x along which x is not redefined.l1: x1 = 5;
{x1}
l2: y1 = 10;
{x1, y1}
l3: z1 = x1 + 1;
{y1}
l4: x2 = 20;
{y1}
l5: w1 = y1 + 1;
{}
┌────────────────┐ {b}
│ l1: x1 = 5; │
└───────┬────────┘ {x1, b}
│
▼
┌────────────┐ {x1, b}
│ l2: if (b) │
└──┬─────┬───┘ {x1}
│ │
▼ ▼
{x1} ┌────────────────┐ ┌────────────────┐ {}
│ l3: y1=x1 + 1; │ │ l4: y1 = 10; │
{y1} └─────────┬──────┘ └──────┬─────────┘ {y1}
│ │
└──────┬────────┘
│
▼
┌────────────────┐ {y1}
│ l5: z1=y1 * 2; │
└────────────────┘ {}
Information of interest: Set of live variables
Transfer function
Join function
| Forward | Backward | |
|---|---|---|
| may | Reaching Definitions | Live Variables |
| must | Constant Propagation |
| Forward | Backward | |
|---|---|---|
| may | Reaching Definitions | Live Variables |
| must | Constant Propagation | Very Busy Expression |
e is very busy at program point p if e is evaluated on every path from p to program exit without any of its operands being redefined. ┌────────────┐ {a+b}
│ l1: if (c) │
└──┬─────┬───┘ {a+b}
│ │
▼ ▼
{a+b} ┌────────────────┐ ┌────────────────┐ {a+b}
│ l2: x = a + b; │ │ l3: y = a + b; │
└─────────┬──────┘ └──────┬─────────┘
│ │
└──────┬────────┘
▼
┌────────────────┐
│ l4: ... │
└────────────────┘
Information of interest: Set of expressions that are very busy at each program point
Transfer function
Join function
if (c) { t = a + b;
x = a + b; if (c) {
} else { ----> x = t;
y = a + b; } else {
} y = t;
}