Post on 24-Jul-2020
Pedro Dinizpedro@isi.edu
Introduction to Data-Flow Analysis
Copyright 2016, Pedro C. Diniz, all rights reserved.
Dr. Pedro C. DinizUniversity of Southern California
Seminar at the ICMC, São Carlos,Univ. of São Paulo, SP, Brasil
May 24, 2016
Pedro Dinizpedro@isi.edu
2
Outline• Motivation: Why Data-Flow Analysis?• Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Formulating a Data-Flow Analysis Problem
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
3
Exampleint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
4
test:subu $fp, 16sw zero, 0($fp) # x = 0sw zero, 4($fp) # y = 0sw zero, 8($fp) # i = 0
lab1: # for(i=0;i<N; i++)mul $t0, $a0, 4 # a*4div $t1, $t0, $a1 # a*4/blw $t2, 8($fp) # imul $t3, $t1, $t2 # a*4/b*ilw $t4, 8($fp) # iaddui $t4, $t4, 1 # i+1lw $t5, 8($fp) # iaddui $t5, $t5, 1 # i+1mul $t6, $t4, $t5 # (i+1)*(i+1)addu $t7, $t3, $t6 # a*4/b*i + (i+1)*(i+1)lw $t8, 0($fp) # xadd $t8, $t7, $t8 # x = x + a*4/b*i + (i+1)*(i+1)sw $t8, 0($fp)...
Example in Assembly
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
5
...lw $t0, 4($fp) # ymul $t1, $t0, a1 # b*ylw $t2, 0($fp) # xadd $t2, $t2, $t1 # x = x + b*ysw $t2, 0($fp)
lw $t0, 8($fp) # iaddui $t0, $t0, 1 # i+1sw $t0, 8($fp)ble $t0, $a3, lab1
lw $v0, 0($fp)addu $fp, 16b $ra
Example in Assembly
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
6
Let’s Optimize...int sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
7
Constant Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
8
Constant Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
9
Constant Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*y;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
10
Constant Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
11
Algebraic Simplificationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
12
Algebraic Simplificationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + b*0;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
13
Algebraic Simplificationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + 0;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
14
Algebraic Simplificationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x + 0;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
15
Algebraic Simplificationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
16
Copy Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);x = x;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
17
Copy Propagationint sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
18
Common Sub-expression Elimination (CSE)int sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
19
int sumcalc(int a, int b, int N){
int i;int x, y;x = 0;y = 0;for(i = 0; i <= N; i++) {x = x + (4*a/b)*i + (i+1)*(i+1);
}return x;
}
Common Sub-expression Elimination (CSE)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
20
int sumcalc(int a, int b, int N){
int i;int x, y, t;x = 0;y = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Common Sub-expression Elimination (CSE)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
21
Dead Code Eliminationint sumcalc(int a, int b, int N){
int i;int x, y, t;x = 0;y = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
22
Dead Code Eliminationint sumcalc(int a, int b, int N){
int i;int x, y, t;x = 0;y = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
23
Dead Code Eliminationint sumcalc(int a, int b, int N){
int i;int x, t;x = 0;
for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
24
Loop Invariant Removalint sumcalc(int a, int b, int N){
int i;int x, t;x = 0;
for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
25
Loop Invariant Removalint sumcalc(int a, int b, int N){
int i;int x, t;x = 0;
for(i = 0; i <= N; i++) {t = i+1;x = x + (4*a/b)*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
26
Loop Invariant Removalint sumcalc(int a, int b, int N){
int i;int x, t, u;x = 0;u = (4*a/b);for(i = 0; i <= N; i++) {t = i+1;x = x + u *i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
27
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u;x = 0;u = (4*a/b);for(i = 0; i <= N; i++) {t = i+1;x = x + u*i + t * t;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
28
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u;x = 0;u = (4*a/b);for(i = 0; i <= N; i++) {t = i+1;x = x + u*i + t * t;
}return x;
}
u*0, u*1, u*2, u*3, u*4,...
v=0, v=v+u,v=v+u,v=v+u,v=v+u,...
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
29
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = (4*a/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + u*i + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
30
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = (4*a/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + v + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
31
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = (4*a/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + v + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
32
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = (4*a/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + v + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
33
Strength Reductionint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = ((a<<2)/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + v + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
34
Optimized Exampleint sumcalc(int a, int b, int N){
int i;int x, t, u, v;x = 0;u = ((a<<2)/b);v = 0;for(i = 0; i <= N; i++) {t = i+1;x = x + v + t*t;v = v + u;
}return x;
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
35
test:subu $fp, 16add $t9, zero, zero # x = 0sll $t0, $a0, 2 # a<<2div $t7, $t0, $a1 # u = (a<<2)/badd $t6, zero, zero # v = 0add $t5, zero, zero # i = 0
lab1: # for(i=0;i<N; i++)addui$t8, $t5, 1 # t = i+1mul $t0, $t8, $t8 # t*taddu $t1, $t0, $t6 # v + t*taddu $t9, t9, $t1 # x = x + v + t*t
addu $6, $6, $7 # v = v + u
addui$t5, $t5, 1 # i = i+1ble $t5, $a3, lab1
addu $v0, $t9, zeroaddu $fp, 16b $ra
Optimized Example in Assembly
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
36
test:subu $fp, 16add $t9, zero, zero
sll $t0, $a0, 2div $t7, $t0, $a1add $t6, zero, zero
add $t5, zero, zero
lab1:addui $t8, $t5, 1
mul $t0, $t8, $t8addu $t1, $t0, $t6addu $t9, t9, $t1
addu $6, $6, $7
addui $t5, $t5, 1
ble $t5, $a3, lab1
addu $v0, $t9, zeroaddu $fp, 16b $ra
test:subu $fp, 16sw zero, 0($fp)sw zero, 4($fp)sw zero, 8($fp)
lab1:mul $t0, $a0, 4div $t1, $t0, $a1lw $t2, 8($fp)mul $t3, $t1, $t2lw $t4, 8($fp)addui $t4, $t4, 1lw $t5, 8($fp)addui $t5, $t5, 1mul $t6, $t4, $t5addu $t7, $t3, $t6lw $t8, 0($fp)add $t8, $t7, $t8sw $t8, 0($fp)lw $t0, 4($fp)mul $t1, $t0, a1lw $t2, 0($fp)add $t2, $t2, $t1sw $t2, 0($fp)lw $t0, 8($fp)addui $t0, $t0, 1sw $t0, 8($fp)ble $t0, $a3, lab1
lw $v0, 0($fp)addu $fp, 16b $ra
4*ld/st + 2*add/sub + br +N*(9*ld/st + 6*add/sub + 4* mul + div + br)= 7 + N*21
6*add/sub + shift + div + br +N*(5*add/sub + mul + br)= 9 + N*7
Unoptimized Code Optimized Code
Optimized Example in Assembly
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
37
What Made these Transformations Possible?
• Reasoning About Run-Time Values of Variables or Expressions and Control-Flow
• At Different Program Points – Which assignment statements produced value of variable at this point? – Which variables contain values that are no longer used after this
program point? – What is the range of possible values of variable at this program point?
• Conclusion: – Need to understand very clearly what is the Control-Flow of the
Program and what Program Points are.
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
38
Program Points• One program point
– before each node/instruction– after each node/instruction
• Join point– point with multiple predecessors
• Split point– point with multiple successors
Introduction to Data-Flow Analysis
test:subu $fp, 16add $t9, zero, zerosll $t0, $a0, 2div $t7, $t0, $a1add $t6, zero, zeroadd $t5, zero, zero
lab1:addui $t8, $t5, 1
mul $t0, $t8, $t8addu $t1, $t0, $t6addu $t9, t9, $t1
addu $6, $6, $7
addui $t5, $t5, 1
ble $t5, $a3, lab1
addu $v0, $t9, zeroaddu $fp, 16b $ra
Pedro Dinizpedro@isi.edu
39
Program Points• One program point
– before each node/instruction– after each node/instruction
• Join point– point with multiple predecessors
• Split point– point with multiple successors
Introduction to Data-Flow Analysis
test:subu $fp, 16add $t9, zero, zerosll $t0, $a0, 2div $t7, $t0, $a1add $t6, zero, zeroadd $t5, zero, zero
lab1:addui $t8, $t5, 1
mul $t0, $t8, $t8addu $t1, $t0, $t6addu $t9, t9, $t1
addu $6, $6, $7
addui $t5, $t5, 1
ble $t5, $a3, lab1
addu $v0, $t9, zeroaddu $fp, 16b $ra
“regular” points
“join” points“split” points
Pedro Dinizpedro@isi.edu
40
Control Flow of a Program
• Forms a Graph
• A Very Large Graph• Create Basic Blocks• A Control-Flow Graph (CFG) connects basic blocks
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
41
Control Flow Graph (CFG)• Control-Flow Graph G = <N, E>• Nodes(N): Basic Blocks• Edges(E): (x,y) ∈E iff first instruction in the basic
block y follows the last instruction in the basic block x
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
42
Identifying Recursive Structures Loops
• Identify Back Edges• Find the nodes and edges in the
loop given by the back edge• Other than the Back Edge
– Incoming edges only to the basic block with the back edge head
– one outgoing edge from the basic block with the tail of the back edge
• How do I find the Back Edges?
bb1
bb2
bb4bb3
bb5
bb6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
43
Computing Dominators• Algorithm
– Make dominator set of the entry node as itself– Make dominator set of the remainder nodes include all the nodes– Visit the nodes in any order– Make dominator set of the current node as the intersection of the
dominator sets of the predecessor nodes and the current node– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
44
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
nodes to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
45
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
nodes to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
bb1bb2bb3bb4bb5bb6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
46
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
nodes to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
47
Computing Dominators
bb1
bb2
bb4bb3
bb5
bb6
{bb1}
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
48
Computing Dominators
bb1
bb2
bb4bb3
bb5
bb6
{bb1}
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
49
Computing Dominators
bb1
bb2
bb4bb3
bb5
bb6
{bb1}
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
50
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2
bb1bb2bb3bb4bb5bb6
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
51
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2bb3bb4bb5bb6
bb1bb2
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
52
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2
bb1bb2bb3bb4bb5bb6
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
53
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
54
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb4bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
55
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
56
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb3bb5bb6
bb1bb2bb3bb4bb5bb6
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
57
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
58
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
59
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
60
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
61
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
62
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb3bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
63
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
64
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb3bb5bb6
bb1bb2bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
65
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
66
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
67
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
bb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
• Algorithm– Make dominator set of the entry node
as itself– Make dominator set of the remainder
node to include all the nodes– Visit the nodes in any order– Make dominator set of the current
node as the intersection of the dominator sets of the predecessor nodes and the current node
– Repeat until no change
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
68
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
69
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
70
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
71
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
72
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
73
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
74
Computing Dominators
bb2
bb4bb3
bb5
bb6
{bb1} bb1
• Algorithm– Make dominator set of the entry node
has itself– Make dominator set of the rest have
all the nodes– Visit the nodes in any order– Make dominator set of the current
node intersection of the dominator sets of the predecessor nodes + the current node
– Repeat until no changebb1bb2bb4
bb1bb2bb3
bb1bb2
bb1bb2bb5bb6
bb1bb2bb5
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
75
Computing Dominators• What we just witness was an Iterative Data-Flow
Analysis Algorithm in Action– Initialize all the nodes to a given value– Visit nodes in some order– Calculate the node’s value– Repeat until no value changes (fixed-point computation)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
76
Data-Flow AnalysisA collection of techniques for compile-time reasoning
about the runtime flow of values in a program
• Local Analysis– Analyze the “effect” of each Instruction in each Basic Block– Compose “effects” of instructions to derive information from
beginning of basic block to each instruction
• Data-Flow Analysis– Iteratively propagate basic block information over the control-flow
graph until no changes– Calculate the final value(s) at the beginning/end of the Basic Block
• Local Propagation– Propagate the information from the beginning/end of the Basic
Block to each instruction
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
77
Basic Idea• Information about Program represented using Values
from Algebraic Structure called Lattice • Analysis produces Lattice Value for each Program
Point• Two flavors of Analysis:
– Forward dataflow analysis– Backward dataflow analysis
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
78
Forward Data-Flow Analysis• Analysis propagates values forward through control
flow graph with flow of control • Each node has a transfer function f
• Input – value at program point before node• Output – new value at program point after node
• Values flow from program points after predecessor nodes to program points before successor nodes
• At join points, values are combined using a merge function
• Canonical Example: Reaching Definitions
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
79
Backward Data-Flow Analysis• Analysis propagates values backward through control
flow graph against flow of control • Each node has a transfer function f
• Input – value at program point after node• Output – new value at program point before node
• Values flow from program points before successor nodes to program points after predecessor nodes
• At split points, values are combined using a merge function
• Canonical Example: Live Variables
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
80
Outline• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Practical Issues: Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
81
Example: Available Expression• An Expression is Available at point p if and only if
– All paths of execution reaching the current point passes through the point where the expression was defined
– No variable used in the expression was modified between the definition point and the current point p
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
82
Example: Available Expression• An Expression is Available at point p if and only if
– All paths of execution reaching the current point passes through the point where the expression was defined
– No variable used in the expression was modified between the definition point and the current point p
• In other words: Expression is still current at p
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
83
Example: Available Expression• An Expression is Available at point p if and only if
– All paths of execution reaching the current point passes through the point where the expression was defined
– No variable used in the expression was modified between the definition point and the current point p
• In other words: Expression is still current at p
• Why is this a Data-Flow Problem?
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
84
Example: Available Expression• An Expression is Available at point p if and only if
– All paths of execution reaching the current point passes through the point where the expression was defined
– No variable used in the expression was modified between the definition point and the current point p
• In other words: Expression is still current at p
• Why is this a Data-Flow Problem?– We have to “know” a property about the program’s execution that
depends on the control-flow of the program!– All-Paths or At-Least-One-Path Issue.
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
85
Example: Available Expression
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
86
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
YES!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
87
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
YES!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
88
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
NO!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
89
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
NO!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
90
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
NO!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
91
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
NO!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
92
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
YES!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
93
Is the Expression Available?
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
YES!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
94
Use of Available Expressions
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
95
Use of Available Expressions
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
96
Use of Available Expressions
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
97
Use of Available Expressions
a = b + cd = e + ff = a + c
g = f
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
98
Use of Available Expressions
a = b + cd = e + ff = a + c
g = f
j = a + b + c + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
99
Use of Available Expressions
a = b + cd = e + ff = a + c
g = f
j = a + c + b + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
100
Use of Available Expressions
a = b + cd = e + ff = a + c
g = f
j = f + b + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
101
Use of Available Expressions
a = b + cd = e + ff = a + c
g = f
j = f + b + d
b = a + dh = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
102
Outline• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
103
Algorithm for Available Expression• Assign a Number to each Expression in the Program
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
104
Example: Available Expression
a = b + cd = e + ff = a + c
g = a + c
j = a + b + c + d
b = a + d h = c + f
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
105
Example: Available Expression
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
106
Gen and Kill Sets• Gen Set
– If a Basic Block (or instruction) defines the expression then the expression number is in the Gen Set for that Basic Block (or instruction)
• Kill Set– If a Basic Block (or instruction) (re)defines a variable in the expression
then that expression number is in the Kill Set for that Basic Block (or instruction)
– Expression is thus not valid after that Basic Block (or instruction)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
107
Algorithm for Available Expression• Assign a Number to each Expression in the Program• Compute Gen Set and Kill Set for each Basic Block
(or instruction)– Compute Gen Set and Kill Set for each Instruction in Basic Block
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
108
Gen and Kill Sets
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
109
Gen and Kill Sets
a = b + c 1
d = e + f 2
f = a + c 3
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
110
Gen and Kill Sets
a = b + c 1gen = { b + c }kill = { any expr with a }
d = e + f 2gen = { e + f }kill = { any expr with d }
f = a + c 3gen = { a + c }kill = { any expr with f }
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
111
Gen and Kill Sets
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
112
Algorithm for Available Expression• Assign a Number to each Expression in the Program• Compute Gen Set and Kill Set for each Basic Block
(or instruction)– Compute Gen Set and Kill Set for each Instruction in Basic Block– Compose them to create Basic Block Gen and Kill Sets
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
113
Aggregate Gen and Kill Sets
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
• Propagate all the Gen Sets and Kill Sets from top of the basic block to the bottom of the basic block
• How?
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
114
Aggregate Gen Set
OutGEN =
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InGEN set
OutGEN set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
115
Aggregate Gen Set • An expression in the Gen Set in the
current instruction should be in the OutGEN Set
OutGEN = gen
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InGEN set
OutGEN set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
116
Aggregate Gen Set• An expression in the Gen Set in the
current instruction should be in the OutGEN Set
• Any expression in the InGEN Set that is not killed should be in the OutGEN Set
OutGEN = gen È(InGEN - kill)
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InGEN set
OutGEN set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
117
Aggregate Gen Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
118
Aggregate Gen Set
OutGEN = gen È(InGEN - kill)
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
119
Aggregate Gen Set
OutGEN = { 1 } È({ } - { 3,4,5,7})
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
120
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
121
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = gen È(InGEN - kill)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
122
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 2 } È({ 1 } - { 5,7 })
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
123
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 1, 2 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
124
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 1, 2 }InGEN = { 1, 2 }
OutGEN = gen È(InGEN - kill)Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
125
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 1, 2 }InGEN = { 1, 2 }
OutGEN = { 3 } È({1, 2} - {2, 6})Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
126
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 1, 2 }InGEN = { 1, 2 }
OutGEN = { 1, 3 }Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
127
Aggregate Gen Set
OutGEN = { 1 }
InGEN = { }a = b + c 1
gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
InGEN = { 1 }
OutGEN = { 1, 2 }InGEN = { 1, 2 }
OutGEN = { 1, 3 }
GE
N =
{ 1
, 3 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
128
Aggregate Kill Set
OutKILL =
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InKILL set
OutKILL set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
129
Aggregate Kill Set • An expression in the Kill Set in
the current instruction should be in the OutKILL set
OutKILL = kill
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InKILL set
OutKILL set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
130
Aggregate Kill Set • An expression in the Kill Set in
the current instruction should be in the OutKILL set
• Any expression in the InKILLset should be in OutKILL
OutKILL = kill ÈInKILL
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
InKILL set
OutKILL set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
131
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
132
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = kill ÈInKILL
InKILL = { }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
133
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 } È{ }
InKILL = { }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
134
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
135
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = kill ÈInKILL
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
136
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = { 5, 7 } È{ 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
137
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = { 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
138
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = { 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
OutKILL = kill ÈInKILL
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
139
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = { 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
OutKILL = { 2, 6 } È{ 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
140
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
OutKILL = { 3, 4, 5, 7 }
InKILL = { }
OutKILL = { 3, 4, 5, 7 }
InKILL = { 3, 4, 5, 7 }
OutKILL = { 2, 3, 4, 5, 6, 7 }
InKILL = { 3, 4, 5, 7 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
141
Aggregate Kill Set
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
d = e + f 2gen = { 2 }kill = { 5, 7 }
f = a + c 3gen = { 3 }kill = { 2, 6 }
KIL
L =
{ 2, 3
, 4, 5
, 6, 7
}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
142
Aggregate Gen and Kill Sets
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
143
Algorithm for Available Expression• Assign a Number to each Expression in the Program• Compute Gen and Kill Sets for each Instruction• Computer Aggregate Gen and Kill Sets for each Basic
Block• Initialize Available Set at each Basic Block to be the
entire set (Universe element of the set of expressions)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
144
Aggregate Gen and Kill SetsIN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
145
Algorithm for Available Expression• Assign a Number to each Expression in the Program• Compute Gen and Kill sets for each Instruction• Compute Aggregate Gen and Kill sets for each Basic Block• Initialize available set at each basic block to be the entire set• Iteratively propagate available expression set over the CFG
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
146
Propagate Available Expression Set
OUT =
gen = { … }kill = { ... }
IN set
OUT set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
147
• If the expression is generated (in the Gen set) then it is available at the end – should in the OUT set
OUT = gen
gen = { … }kill = { ... }
IN set
OUT set
Propagate Available Expression Set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
148
• If the expression is generated (in the Gen set) then it is available at the end – should in the OUT set
• Any expression available at the input (in the IN set) and not killed should be available at the end
OUT = gen ∪ (IN - kill)
gen = { … }kill = { ... }
IN set
OUT set
Propagate Available Expression Set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
149
OUT = gen ∪ (IN - kill)
IN set
IN =
Propagate Available Expression Set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
150
• Expression is available only if it is available in All Input Paths
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Propagate Available Expression Set
IN set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
151
Aggregate Gen and Kill SetsIN = { }
OUT = {1,2,3,4,5,6,7}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
152
Aggregate Gen and Kill SetsIN = { }
OUT = {1,2,3,4,5,6,7}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
153
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
154
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
155
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1, 3}
OUT = {1,2,3,4,5,6,7}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
156
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
157
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1,2,3,4,5,6,7}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
158
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
159
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
160
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1,2,3,4,5,6,7}
OUT = {1,2,3,4,5,6,7}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
161
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {1,2,3,4,5,6,7}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
162
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
163
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
164
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
165
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
166
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = {1, 3, 4}
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
167
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {1, 3, 4, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
168
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
169
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = {1, 3}
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
170
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
171
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
172
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
173
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
174
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
175
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Gen = { 4 }Kill = { }
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
176
Aggregate Gen and Kill SetsIN = { }
OUT = {1, 3}
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1, 3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5, 6 }Kill = { 1, 7 }
Gen = { 7 }Kill = { }
IN = { 3 }
OUT = {3, 5, 6}
OUT = {3, 7}
IN = { 3 }
IN = {1, 3}
OUT = {1, 3, 4}
Introduction to Data-Flow Analysis
OUT = gen ∪ (IN - kill)
IN = ∩ OUT
Pedro Dinizpedro@isi.edu
177
Algorithm for Available Expression• Assign a Number to each Expression in the Program• Calculate Gen and Kill Sets for each Instruction• Calculate aggregate Gen and Kill Sets for each Basic Block• Initialize Available Set at each Basic Block to be the entire set• Iteratively propagate Available Expression set over the CFG• Propagate within the Basic Block
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
178
Propagate within the Basic Block • Start with the IN set of available
expressions• Linearly propagate down the
basic block– same as data-flow step– single pass since no back edges
OUT = gen È(IN - kill)
a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }
IN set
OUT set
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
179
Available Expressionsae = { }
a = b + c 1ae = { 1 }
d = e + f 2ae = { 1, 2 }
f = a + c 3ae = { 1, 3 }
ae = { 1, 3 }g = a + c 4
ae = { 1, 3, 4 }
ae = { 3 }j = a + b + c + d 7
ae = { 3, 7 }
ae = { 3 }b = a + d 5
ae = { 3, 5 }h = c + f 6
ae = { 3, 5, 6 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
180
Outline• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Practical Issues: Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
181
Practical Issues: Bit Sets• Assign a bit to each element of the set
– Union = bit OR– Intersection = bit AND – Subtraction = bit NEGATE and AND
• Fast implementation– 32 elements packed to each word– AND and OR are single instructions
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
182
Aggregate Gen and Kill Sets
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1,3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5,6 }Kill = { 1,7 }
Gen = { 7 }Kill = { }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
183
Aggregate Gen and Kill Sets
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
Gen = { 1,3}Kill = { 2,3,4,5,6,7 }
Gen = { 4 }Kill = { }
Gen = { 5,6 }Kill = { 1,7 }
Gen = { 7 }Kill = { }
7 bits per set required
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
184
Aggregate Gen and Kill Sets
a = b + c 1d = e + f 2f = a + c 3
g = a + c 4
j = a + b + c + d 7
b = a + d 5h = c + f 6
7 bits per set required Gen = 1010000Kill = 0111111
Gen = 0001000Kill = 0000000
Gen = 0000110Kill = 1000001
Gen = 0000001Kill = 0000000
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
185
Outline• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Practical Issues: Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains• SSA Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
186
Formulating an IterativeData-Flow Analysis Problem
• Problem Independent– Calculate Gen and Kill Sets for each Basic Block– Iterative Propagation of Information until Convergence– Propagation of Information within the Basic Block
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
187
Formulating an IterativeData-Flow Analysis Problem
• We need to Build the Control-Flow Graph – Defines predecessors and successors
• Run the Round-Robin Work-list Algorithm – Initializes abstract valued for each node n– Iterates until it reaches a fixed point
• To Solve another Data-Flow Problem – Replace the initialization step and the fixed-point equations – Fixed-point equations includes direction of propagation – Predecessors or successors, as needed
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
188
Formulating an IterativeData-Flow Analysis Problem
• Questions We Must Ask – Termination: Does it Halt? – Correctness: What answer does it Produce? – Speed: How quickly does it find that Answer? COMP 512, Spring 2009! 7!
Classic Algorithm: Round-robin Iterative Algorithm
Questions we must ask
•! Termination: does it halt?
•! Correctness: what answer does it produce?
•! Speed: how quickly does it find that answer?
DOM(n0 ) & Ø
for i & 1 to |N | DOM(ni ) & { N }
change & true
while (change) change & false
for i & 0 to |N |
TEMP &{ ni } # ($p!pred(ni) DOM(p) if DOM(ni ) ! TEMP then
change & true DOM(ni ) & TEMP
Just the fixed-point equation
Data-flow Analysis
The basics
•! Data-flow sets are drawn from a semi-lattice, L, of facts
•! Sets are modified by transfer functions, fi, that model effect of code on contents of the sets
>! Function space of all possible transfer functions is F
•! Properties of L and F govern termination, correctness, & speed
To reason about the properties of a (proposed ) data-flow problem,
we cast it into a lattice-theory framework and prove some simple theorems about the problem
COMP 512, Spring 2009! 8!
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
189
Data-Flow Analysis : The Basics• Data-Flow Sets are drawn from a Semi-Lattice, L, of facts
• Sets are modified by Transfer Functions, fi, that model effect of code on contents of the sets
• Function Space of all possible Transfer Functions is F– Properties of L and F govern termination, correctness, & speed
To reason about the properties of a data-flow problem,we cast it into a lattice-theory framework and
prove some simple theorems about the problem
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
190
Data-Flow Analysis : The Basics
• Lattice– Abstract quantities over which the analysis will operate– Example: Sets of Available Expressions
• Flow Functions– How each control-flow and computational construct
affects the abstract quantitiesExample: the OUT equation for each statement
• Merging of Control-Flow Paths– Combining operator of data-flow “meet” operation – Typically Union or Intersection– not the same as the lattice “meet” or “join”...
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
191
Data-Flow Analysis: Semilattice
COMP 512, Spring 2009! 9!
Data-flow Analysis
Limitations
1. Precision – “up to symbolic execution”
>! Assume all paths are taken
2. Solution – cannot afford to compute MOP solution
>! Large class of problems where MOP = MFP= LFP
>! Not all problems of interest are in this class
3. Arrays – treated naively in classical analysis
>! Represent whole array with a single fact
4. Pointers – difficult (and expensive) to analyze
>! Imprecision rapidly adds up
>! Need to ask the right questions
Summary
For scalar values, we can quickly solve simple problems
Good news:
Simple problems can carry us pretty far
*
COMP 512, Spring 2009! 10!
Data-flow Analysis
Semilattice
A semilattice is a set L and a meet operation ' such that,
! a, b, & c ( L :
1. a ' a = a
2. a ' b = b ' a
3. a ' (b ' c) = (a ' b) ' c
' imposes an order on L, ! a, b, & c ( L :
1. a " b ) a ' b = b
2. a > b ) a " b and a ! b
A semilattice has a bottom element, denoted *
1. ! a ( L, * ' a = *
2. ! a ( L, a " *
The meet operator combines the sets when two paths
converge, or meet.
Sometimes we work with a lattice, which has a top
element, denoted
! a ( L, ' a = a
"
"
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
192
Reaching Definitions Problem
It may be important to know which are the set of possible variable definitions that reach each specific variable use.
• Why?– If the set is a singleton and a constant then we can
propagate value...– Basis for the Webs in Register Allocation !
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
193
Reaching Definitions Problem
Def: A definition d of a variable b reaches instruction i if and only if instruction i reads the value of v and there exists a path from d to i that does
not (re)define v.
• Data-Flow (Forward) Problem:– Each program point in CFG annotated with Reaches(n)– Initialization: Reaches(n) = ∅, ∀n
• Equation: Reaches(n) = – where DEDef(m) is the set of downward-exposed definition in m:
those definitions in m that are not redefined subsequently in m;– DefKill(m) contains all the definition points that are obscured by a
definition of the same variable v in m
(DEDef(m)∪ (Reaches(m)∩DefKill(m))) m∈ preds(n)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
194
Lattice• A Lattice L consists of
– A Set of Values – Two operations meet ( ⋀ ) and join ( ⋁ )– A top value (T) and a bottom value (⊥)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
195
Lattice• Example: the Lattice for the Reaching Definitions
Problem where there are only 3 definitions: {d1, d2, d3}
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
⊥ = { }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
196
Meet and Join Operations• Meet and Join forms a Closure
– For all a, b ÎL there exist a unique c and d ÎL such thata Ùb = c a Úb = d
• Meet and Join are Commutative– a Ùb = b Ùa a Úb = b Úa
• Meet and Join are Associative– (a Ùb) Ùc = b Ù(a Ùc) (a Úb) Úc = b Ú(a Úc)
• There exist a unique Top element (T) and Bottom element ( )̂ in L such that– a Ù =̂ ^ a ÚT = T
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
197
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
⊥ = { }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } ⋁ { d2, d3 } = ???
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
198
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ù{ d2, d3 } = ???
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
199
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ù{ d2, d3 } = { d2 }
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
200
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ú{ d3 } = ???
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
201
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ú{ d3 } = ???
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
202
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ú{ d3 } = ???
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
203
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ú{ d3 } = ???
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
204
Meet and Join Operations
{ d1, d2 }
{ d2, d3 }
{ d1 }
{ d3 }
T = { d1, d2, d3 }
{ d1, d3 }{ d2 }
{ d1, d2 } Ú{ d3 } = { d1, d2, d3}
Introduction to Data-Flow Analysis⊥ = { }
Pedro Dinizpedro@isi.edu
205
Meet and Join Operations
• Meet Operation: Greatest Lower Bound - GLB({x,y})– Typically Set Intersection– Follow the lines downwards from the two elements in the lattice until
they meet at a single unique element
• Join Operation: Lowest Upper Bound - LUB({x,y})– Typically Set Union– There is a unique element in the lattice from where there is a
downwards path (with no shared segment) to both elements
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
206
Intuition about Termination• Data-Flow Analysis starts assuming most optimistic
values (T)• Each Stage applies a Flow Function
– Vnew⊆ Vprev– Moves Downwards/Upwards in the Lattice
• Until stable (values don’t change)– A fixed point is reached at every basic block
• Lattice has a finite height ⇒ should terminate
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
207
TerminationIf every fn∈ F is monotone, i.e., x ⊆ y ⇒ F(x) ⊆ F(y) andIf the lattice is bounded, i.e, every descending chain is finite:
– Chain is a sequence x1, x2, ..., xn where xi∈ L– xi > xi+1, 1 ≤ i ≤ n ⇒ chain is descending
Then– The set of values can only change finite number of times– The iterative algorithm must halt on an instance of the problem
• Observations:– Any finite semilattice is bounded– Some infinite semilatices are bounded (see right)
COMP 512, Spring 2009! 11!
Data-flow Analysis
How does this relate to data-flow analysis?
•! Choose a semilattice to represent the facts
•! Attach a meaning to each a ( L
Each a ( L is a distinct set of known facts
•! With each node n, associate a function fn : L " L
fn models behavior of code in block corresponding to n
•! Let F be the set of all functions that the code might generate
Example — DOM
•! Semilattice is (2N,'), where N is the set of nodes in the flow graph and ' is $, and * is Ø
•! For a node n, fn has the form fn(x) = x World’s simplest data-flow equation
COMP 512, Spring 2009! 12!
Iterative Data-flow Analysis
•! Any finite semilattice is bounded
•! Some infinite semilattices are bounded -.002 … -.001 … 0 … .001 … .002 …
Real constants
Termination
•! If every fn ( F is monotone, i.e., x # y + f(x) # f(y), and
•! If the lattice is bounded, i.e., every descending chain is finite
>! Chain is sequence x1, x2, …, xn where xi ( L, 1 # i # n
>! xi > xi+1, 1 # i < n + chain is descending
Then
•! The set at each node can only change a finite number of times
•! The iterative algorithm must halt on an instance of the problem ,
DOM:
f(x) = x is monotone
N is finite and, thus,
bounded
Finite lattice, bounded descending chains,
& monotone functions + termination
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
208
Correctness
COMP 512, Spring 2009! 13!
Iterative Data-flow Analysis
Correctness (What does it compute?)
•! If every fn ( F is monotone, i.e., x # y + f(x) # f(y), and
•! If the semilattice is bounded, i.e., every descending chain is finite
>! Chain is sequence x1, x2, …, xn where xi ( L, 1 # i # n
>! xi > xi+1, 1 # i < n + chain is descending
Given a bounded semilattice S and a monotone function space F
•! - k such that f k(*) = f j(*) ! j > k
•! f k(*) is called the least fixed-point of f over S
•! If L has a T, then - k such that f k(T) = f j(T) ! j > k and
f k(T) is called the maximal fixed-point of f over S optimism
f k(x) is the application of f to x k times
COMP 512, Spring 2009! 14!
Iterative Data-flow Analysis
Correctness
•! If every fn ( F is monotone, i.e., f(x#y) # f(x) ' f(y), and
•! If the lattice is bounded, i.e., every descending chain is finite
>! Chain is sequence x1, x2, …, xn where xi ( L, 1 # i # n
>! xi > xi+1, 1 # i < n + chain is descending
Then
•! The round-robin algorithm computes a least fixed-point (LFP)
•! The uniqueness of the solution depends on other properties of F
•! Unique solution + it finds the one we want
•! Multiple solutions + we need to know which one it finds
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
209
Correctness
COMP 512, Spring 2009! 13!
Iterative Data-flow Analysis
Correctness (What does it compute?)
•! If every fn ( F is monotone, i.e., x # y + f(x) # f(y), and
•! If the semilattice is bounded, i.e., every descending chain is finite
>! Chain is sequence x1, x2, …, xn where xi ( L, 1 # i # n
>! xi > xi+1, 1 # i < n + chain is descending
Given a bounded semilattice S and a monotone function space F
•! - k such that f k(*) = f j(*) ! j > k
•! f k(*) is called the least fixed-point of f over S
•! If L has a T, then - k such that f k(T) = f j(T) ! j > k and
f k(T) is called the maximal fixed-point of f over S optimism
f k(x) is the application of f to x k times
COMP 512, Spring 2009! 14!
Iterative Data-flow Analysis
Correctness
•! If every fn ( F is monotone, i.e., f(x#y) # f(x) ' f(y), and
•! If the lattice is bounded, i.e., every descending chain is finite
>! Chain is sequence x1, x2, …, xn where xi ( L, 1 # i # n
>! xi > xi+1, 1 # i < n + chain is descending
Then
•! The round-robin algorithm computes a least fixed-point (LFP)
•! The uniqueness of the solution depends on other properties of F
•! Unique solution + it finds the one we want
•! Multiple solutions + we need to know which one it finds
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
210
Correctness
COMP 512, Spring 2009! 15!
Iterative Data-flow Analysis
Correctness
•! Does the iterative algorithm compute the desired answer?
Admissible Function Spaces
1.! ! f ( F, ! x,y ( L, f (x 'y) = f (x) ' f (y)
2. - fi ( F such that ! x ( L, fi(x) = x
3. f,g ( F - h ( F such that h(x ) = f (g(x))
4. ! x ( L, - a finite subset H . F such that x = 'f ( H f (*)
If F meets these four conditions, then an instance of the problem will have a unique fixed point solution (instance $ graph + initial values)
+ LFP = MFP = MOP
+ order of evaluation does not matter
If meet does not distribute over function application, then the fixed point solution may not be unique. The iterative algorithm will find a LFP.
*
COMP 512, Spring 2009! 16!
Iterative Data-flow Analysis
If a data-flow framework meets those admissibility conditions
then it has a unique fixed-point solution
•! The iterative algorithm finds the (best) answer
•! The solution does not depend on order of computation
•! Algorithm can choose an order that converges quickly
Intuition
•! Choose an order so that changes propagate as far as possible on
each “sweep”
>! Process a node’s predecessors before the node
•! Cycles pose problems, of course
>! Ignore back edges when computing the order?
*
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
211
Data-Flow Analysis : Limitations• Precision – “up to symbolic execution”
– Assume all paths are taken
• Solution – cannot afford to compute MOP solution – Large class of problems where MOP = MFP= LFP – Not all problems of interest are in this class
• Arrays – treated naively in classical analysis – Represent whole array with a single fact
• Pointers – difficult (and expensive) to analyze– Imprecision rapidly adds up – Need to ask the right questions
• Summary– For scalar values, we can quickly solve simple problems
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
212
Questions
• Is it Sound (i.e., are the results conservative)? • How good are the results?• How fast does it run?
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
213
Maximal Fixed Point Solution (MFP)• Claim:
Among all the solutions to the system of dataflow equations, the iterative solution is the most precise
• Intuition: – We start with the top element at each program point (i.e. most imprecise
or conservative information)– Then refine the information at each iteration to satisfy the dataflow
equations– Final result will be the closest to the top
• Iterative solution for dataflow equations is called Maximal Fixed Point solution (MFP)
• For any Fixed-Point (FP) solution of the equations: FP ⊆ MFP
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
214
Meet Over Paths Solution (MOP)• Is MFP the best solution to the Analysis Problem?
• Another approach: consider a lattice framework, but use a different way to compute the solution – Let G be the control flow graph with start node n0
– For each path pk=[n0, n1, …, nk] from entry to node nk:in[pk] = Fnk-1 ( … (Fn1(Fn0(d0))))
– Compute solution asin[n] = ∨ { in[pk] | all paths pk from n0 to nk}
• This solution is the Meet Over Paths solution (MOP)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
215
MFP versus MOP
• Precision: can prove that MOP solution is always more precise than MFP
MFP ⊆MOP
• Why not use MOP?• MOP is intractable in practice
1. Exponential number of paths: for a program consisting of a sequence of N if statements, there will 2N paths in the CFG2. Infinite number of paths: for loops in the CFG
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
216
Importance of Distributivity
• Property: if transfer functions are distributive, then the solution to the dataflow equations is identical to the meet-over-paths solution
MFP = MOP
• For distributive transfer functions, can compute the intractable MOP solution using the iterative fixed-point algorithm
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
217
Can We Do Better Than MOP?• Is MOP the best solution to the analysis problem?
• MOP computes solution for all path in the CFG• There may be paths which will
never occur in any execution • So MOP is conservative
• IDEAL = solution which takes into account only paths whichoccur in some execution
• This is the best solution– but it is undecidable
x = 1 x = 2
y = y+2
if (c)
if (c)
y = x+1
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
218
Speed
• If a Data-Flow Framework meets those admissibility conditions then is has a unique fixed-point solution– The iterative algorithm finds the (best) answer– The solution does not depend on the order of computation– Algorithm can choose an order that converges quickly
• Intuition:– Choose an order so that changes propagate as far as possible on
each “sweep” or “pass” over the CFG• Process a node’s predecessors before the node
– Cycles pose problems, naturally• Ignore back edges when computing evaluation order
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
219
Speed
COMP 512, Spring 2009! 17!
Ordering the Nodes to Maximize Propagation
2 3
4
1
Postorder
3 2
1
4
Reverse Postorder
•! Reverse postorder visits predecessors before visiting a node
•! Use reverse preorder for backward problems
>! Reverse postorder on reverse CFG is reverse preorder
N+1 - postorder number
COMP 512, Spring 2009! 18!
Iterative Data-flow Analysis
Speed
•! For a problem with an admissible function space & a bounded semilattice,
•! If the functions all meet the rapid condition, i.e.,
!f,g ( F, ! x ( L, f (g(*)) " g(*) ' f (x) ' x
then, a round-robin, reverse-postorder iterative algorithm
will halt in d(G)+3 passes over a graph G
d(G) is the loop-connectedness of the graph w.r.t a DFST
>! Maximal number of back edges in an acyclic path
>! Several studies suggest that, in practice, d(G) is small (<3)
>! For most CFGs, d(G) is independent of the specific DFST
Sets stabilize in two passes around a loop
Each pass does O(E ) meets & O(N ) other operations
*
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
220
Speed
COMP 512, Spring 2009! 19!
Iterative Data-flow analysis
What does this mean?
•! Reverse postorder
>! Easily computed order that increases propagation per pass
•! Round-robin iterative algorithm
>! Visit all the nodes in a consistent order (RPO)
>! Do it again until the sets stop changing
•! Rapid condition
>! Most classic global data-flow problems meet this condition
These conditions are easily met
>! Admissible framework, rapid function space
>! Round-robin, reverse-postorder, iterative algorithm
+! The analysis runs in (effectively) linear time
COMP 512, Spring 2009! 20!
Some problems are not admissible
Global constant propagation
•! First condition in admissibility
! f ( F, ! x,y ( L, f (x 'y) = f (x) ' f (y)
•! Constant propagation is not admissible
>! Kam & Ullman time bound does not hold
>! There are tight time bounds, however, based on lattice height
>! Require a variable-by-variable formulation …
a & b + c
•! Function “f” models block’s effects
•! f(S1) = {a=7,b=3,c=4}
•! f(S2) = {a=7,b=1,c=6}
•! f(S1'S2) = Ø
S1: {b=3,c=4} S2: {b=1,c=6}
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
221
Summary• Data-Flow Analysis
– Sets up System of Equations– Iteratively computes MFP– Terminates because Transfer Functions are monotonic
and Lattice has finite height
• Other possible solutions: FP, MOP, IDEAL• All are safe solutions, but some are more precise:
FP ⊆ MFP ⊆MOP ⊆ IDEAL• MFP = MOP if distributive transfer functions• MOP and IDEAL are intractable• Compilers use dataflow analysis and MFP
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
222
DU-Chain Data-Flow Problem Formulation
• Lattice: The Set of Definitions– Bit-vector format: a bit for each variable definition in the procedure– Label the definitions in the input program as d1,vk, d2,vk, …
• Direction: Forward Flow• Flow Functions:
– Gen(n) = { di,…, di,n | where di,vk = 1 for definition di,vk in n not killed inside the same basic block by a subsequent definition*}
– Kill(n) = { di,…, di,n | where di,vk = 1 iff variable vk is defined in n}– OUT(n) = Gen(n) ∪ (IN(n) – Kill(n))
– IN(n) = ∪OUT(p) for all predecessors nodes p of node n
* This is the notion of downwards exposed definitionIntroduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
223
Gen and Kill Functions
• Gen = Downwards Exposed Definitions– Dual to Upwards Exposed Reads (see Live Variables Analysis)– Can be computed on a Forward pass of the Basic Block
• Keep a list of variables defined in the block• Remove and keep only the last definition as you go along
k = … 1
i = … 2
j = … 3
i = i + … 4
BBn
Gen(n) = { d1, d3 , d4 }Kill(n) = { d1, d2, d3, d4, … }
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = falsei = 1 j = 2
j = j * 2 k = true i = i + 1 print j i = i + 1
k
exit
i < n
Introduction to Data-Flow Analysis224
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
Introduction to Data-Flow Analysis225
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
Introduction to Data-Flow Analysis226
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { }IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis227
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { }IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis228
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { }IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis229
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis230
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis231
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis232
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis233
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { }
OUT = { } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis234
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { }
OUT = { 4, 5, 6 } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis235
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { }
OUT = { 4, 5, 6 } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis236
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis237
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { } OUT = { }IN = { }
Introduction to Data-Flow Analysis238
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { }IN = { }
Introduction to Data-Flow Analysis239
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { }IN = { }
Introduction to Data-Flow Analysis240
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { }
Introduction to Data-Flow Analysis241
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis242
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis243
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis244
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis245
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis246
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis247
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis248
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis249
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis250
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis251
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis252
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis253
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 7 }
Introduction to Data-Flow Analysis254
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis255
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis256
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis257
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis258
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis259
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis260
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis261
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 } OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
Introduction to Data-Flow Analysis262
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6
print j i = i + 1 7
k
exit
i < n
gen ={ 1, 2, 3 }kill = { 4,5,6,7 }
gen ={ 4,5,6 }kill = { 1,2,3,7 }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ }kill = { }
gen ={ 7 }kill = { 2,6 }
OUT = { 1, 2, 3 }IN = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { }
OUT = { 1, 2, 3, 4, 5, 6 }
OUT = IN = { 1, 2, 3, 4, 5, 6 }
OUT = { 4, 5, 6 }
OUT = { 1, 2, 3, 4, 5, 6 } OUT = { 1, 3, 4, 5, 7 }IN = { 1, 2, 3, 4, 5, 6, 7 }
IN= { 1, 2, 3, 4, 5, 6 }
Introduction to Data-Flow Analysis263
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = false 1i = 1 2j = 2 3
j = j * 2 4k = true 5i = i + 1 6 print j i = i + 1 7
k
exit
i < nIN = { 1, 2, 3, 4, 5, 6 }
IN = { }
IN = { 1, 2, 3, 4, 5, 6 }
IN = { 1, 2, 3, 4, 5, 6 }
IN = { 1, 2, 3, 4, 5, 6, 7 }
IN = { 1, 2, 3, 4, 5, 6 }
IN = { 1, 2, 3, 4, 5, 6 }
Introduction to Data-Flow Analysis264
Pedro Dinizpedro@isi.edu
265
DU Chains• At each use of a variable, indicates all its possible
definitions (and thus its points)– Very Useful Information– Used in Many Optimizations
• Information can be Incorporate in the representation– SSA From
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
266
Outline• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Practical Issues: Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains• SSA Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
267
Static Single Assignment (SSA) Form• Each definition has a unique variable name
– Original name + a version number
• Each use refers to a definition by name
• What about multiple possible definitions?– Add special merge nodes so that there can be only a single definition
(Ffunctions)
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
268
a = 1 b = a + 2c = a + ba = a + 1d = a + b
Static Single Assignment (SSA) Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
269
a = 1 b = a + 2c = a + ba = a + 1d = a + b
a1 = 1 b1 = a1 + 2c1 = a1 + b1
a2 = a1 + 1d1 = a2 + b1
Static Single Assignment (SSA) Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
270
a = 1 c = a + 2
b = 1 c = b + 2
d = a + b + c
Static Single Assignment (SSA) Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
271
a = 1 c = a + 2
b = 1 c = b + 2
d = a + b + c
a1 = 1 c1 = a1 + 2
b1 = 1 c2 = b1 + 2
c3 = F(c1, c2)d1 = a1 + b1 + c3
Static Single Assignment (SSA) Form
Introduction to Data-Flow Analysis
Pedro Dinizpedro@isi.edu
DU Exampleentry
k = falsei = 1 j = 2
j = j * 2 k = true i = i + 1 print j i = i + 1
k
exit
i < n
Introduction to Data-Flow Analysis272
Pedro Dinizpedro@isi.edu
DU Exampleentry
k1 = false i1 = 1 j1 = 2
j2 = j3 * 2 k2 = true i2 = i3 + 1 print j3 i4 = i3 + 1
k3
i5 = F(i3, i4) exit
i3 = F(i1, i2) j3 = F(j1, j2) k3 = F(k1, k2)
i1 < n
Introduction to Data-Flow Analysis273
Pedro Dinizpedro@isi.edu
274
Summary
• Overview of Control-Flow Analysis• Available Expressions Data-Flow Analysis Problem• Algorithm for Computing Available Expressions • Practical Issues: Bit Sets• Formulating a Data-Flow Analysis Problem• DU Chains• SSA Form
Introduction to Data-Flow Analysis
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
Data-Flow AnalysisLive-Variable Analysis
Copyright 2016, Pedro C. Diniz, all rights reserved.Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
2
Live-Variable Analysis• What is Live-Variable Analysis?
– For each Variable x where is the last program point p where the a specific value of x is used.
– In other words, for x and program point p determine if the value of x at pcan still be used along some path starting at p.
• If so, x is live at p
• If not x is dead at p
– Must take Control-Flow into account : a Data-Flow Problem !!!
• Applications:– Register Allocation: If a variable is dead at a given point p
• Can reuse its storage, i.e, the register it occupies if any;
• If its value as been modified must save the value to storage unless it is not live on exit of the procedure or loop
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
3
Live-Variable Analysis: Illustration• At point p0 the x variable is live:
– There is a path to p1 where value at p0 is used
– Beyond px towards p2 the value of x is no longer needed and is dead
… = x
… = xx = …
p0
p1
p2
px
• Need to observe for each variable and for each program point:– Where is the last program point beyond which the value is not used
– Trace back from uses to definitions and observe the first definition (backwards) that reaches that use.
– That definition kills all uses backwards of it.
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
4
Data-Flow Analysis Formulation
IN(B) = Use(B) ∪ (OUT(B) - Def(B))
OUT(B) = ∪ IN(s)
• Variable is live at a point p if its value is used along at least one Path
– A use of x prior to any definition in basic block means x must be alive– A definition of x in B prior to any subsequent use means previous uses must
be dead
• Gen Set: Set of Variables Used in B– Upward Exposed Reads of B
• Kill Set: Set of Variables Defined in B
S a successor of B
OUT set
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
5
Data-Flow Analysis Formulation
• Initialize IN(B) to Empty Set• Compute Gen/Use and Kill/Def for each Basic Block
– Tracing backwards from end of block to beginning of block– Initialize Last Instruction’s Out(i) to Empty– Use IN(i) = use(i) ∪ (OUT(i) - def(i))
• Iteratively Apply Relations to Basic Block Until Convergence– OUT(B) = ∪ IN(s)– IN(B) = Use(B) ∪ (OUT(B) - Def(B))
• Given OUT(B) use relations at instruction level to determine the live variables after each instruction
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
6
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
Example
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
7
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
In = Use ∪ (Out - Def)
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
8
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
In = {a,b} ∪ ({} - {}) = {a,b}
In(i) = Use(i) ∪ (Out (i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
9
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
In = Use ∪ (Out - Def)
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
10
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
In = {t} ∪ ({a,b} - {b})
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
11
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
Out = {a,t}
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
12
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
Out = {a,t}
In = Use ∪ (Out - Def)
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
13
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
Out = {a,t}
In = {a} ∪ ({a,t} - {t})
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
14
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
Out = { }
Out = {a,b}
Out = {a,t}
In = {a}
In(i) = Use(i) ∪ (Out(i) - Def(i))
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
15
Use & Def Functions for a Basic Block
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutUse = { }
OutUse = {a,b}
OutUse = {a,t}
InUse = {a}
InUse(i) = Use(i) ∪ (OutUse(i) - Def(i))
Use
(B) =
{a}
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
16
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutDef = { }
InDef(i) = Def(i) ∪ OutDef(i)
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
17
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutDef = { }
InDef(i) = Def(i) ∪ OutDef(i)
InDef = Def ∪ OutDef = { }
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
18
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutDef = { }
InDef(i) = Def(i) ∪ OutDef(i)
OutDef = { }
InDef = Def ∪ OutDef = {b}
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
19
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutDef = { }
InDef(i) = Def(i) ∪ OutDef(i)
OutDef = { }
OutDef = {b}
InDef = Def ∪ OutDef = {t}∪{b}
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
20
t = a +1
b = t
if (a = b) goto L2use = { a, b }def = { }
use = { t }def = { b }
use = { a }def = { t }
OutDef = { }
InDef(i) = Def(i) ∪ OutDef(i)
OutDef = { }
OutDef = {b}
InDef = {t, b}
Def
(B)
= {t
,b}
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
21
• Can be Accomplished by a Forward Scanning of the Block– Keep Track of Which Variables are Read before they are written thus
computing the Upwards Exposed Reads (UpExp) or Use Function– Track Variables that are Written or Killed (VarKill) or Def Function
// Assume instruction in format “x = y op z”for i = 1 to Num Instructions in B do
if (instr(i) is leader of B) thenb = Number(B);UpExp(b) = ∅;VarKill(b) = ∅;
if y ∉ VarKill(b) thenUpExp(b) = UpExp(b) ∪ {y}
if z ∉ VarKill(b) thenUpExp(b) = UpExp(b) ∪ {z}
VarKill(b) = VarKill(b) ∪ {x}
Use & Def Functions for a Basic Block
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
22
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Example
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
23
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Example
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
24
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { }
Out = { }
Out = { }
In = { } In = { }
In = { }Out = { }
Out = { }
In = { }
Out = { }
Out = { }In = { }
Example
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
25
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { }
In = { } In = { }
In = { }Out = { }
Out = { }
In = { }
Out = { }
Out = { }In = { }
OUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Example
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
26
i = 0
b = 0x = p
if(i < p) goto L1
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { }
In = { } In = { }
In = { }Out = { b }
Out = { i, p, b}
In = { }
Out = { }
Out = { }In = { }
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
27
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { }
In = { } In = { }
In = { }Out = { b }
Out = { i, p, b}
In = { p }
Out = { }
Out = { }In = { }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
28
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { i }
In = { } In = { }
In = { }Out = { b }
Out = { i, p, b}
In = { p }
Out = { }
Out = { i }In = { i }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
29
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { i }
In = { x, i } In = { }
In = { }Out = { b }
Out = { i, p, b}
In = { p }
Out = { }
Out = { i }In = { i }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
30
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { i }
In = { x, i } In = { x, i }
In = { }Out = { b }
Out = { i, p, b}
In = { p }
Out = { }
Out = { i }In = { i }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
31
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { }
Out = { i }
In = { x, i } In = { x, i }
In = { a, i, x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { x, i }
Out = { i }In = { i }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
32
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a, i, x }
Out = { i }
In = { x, a } In = { x, a }
In = { a, i, x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { x, a }
Out = { i }In = { a, i, x }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
33
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x + 1 a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a, i, x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a, i, x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { x, a }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
34
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a, , x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
a = x + 1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
35
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a,i,x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
a = x + 1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
36
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a,i,x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
a = x + 1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
37
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a,i,x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { b }
Out = { i, p, b}
In = { p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
a = x + 1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
38
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a,i,x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { a,b,i,x }
Out = { a,b,i,p,x }
In = { p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
if(i < p) goto L1
a = x + 1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
39
i = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
use = { a }
def = { t, b }
use = { x }
def = { a }
use = { x }
def = { a }
use = { i }
def = { i }
use = { b }
def = { x, b }
use = { i, p }
def = { }
use = { p }
def = { i, b, x }
Out = { }
In = { b }
Out = { a,i,x }
Out = { a,i,x }
In = { i,x } In = { i,x }
In = { a,i,x }Out = { a,b,i,x }
Out = { a,b,i,p,x }
In = { a, p }
Out = { i,x }
Out = { a,i,x }In = { a, i, x }
a = x + 1
if(i < p) goto L1
ExampleOUT(B) = ∪ IN(s)
IN(B) = Use(B) ∪(OUT(B) - Def(B))
S a successor of B
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
40
Examplei = 0
b = 0x = p
t = a +1
b = tif (a = b) goto L2
a = x - 1
i = i + 1goto L3
b = b + 1
x = 0
Out = { }
Out = { a,i,x }
Out = { a,i,x }
Out = { a,b,i,x }
Out = { a,b,i,p,x }
Out = { i,x }
Out = { a,i,x }
a = x + 1
if(i < p) goto L1
Spring 2016CSCI 565 - Compiler Design
Pedro Dinizpedro@isi.edu
41
Summary• What is Live-Variable Analysis?
– Backward Data-Flow Analysis Problem
– Upwards Exposed (Gen) - Computed in a Forward Pass
• Most Significant Application– Register Allocation