Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · •...

315
Pedro Diniz [email protected] Introduction to Data-Flow Analysis Copyright 2016, Pedro C. Diniz, all rights reserved. Dr. Pedro C. Diniz University of Southern California Seminar at the ICMC, São Carlos, Univ. of São Paulo, SP, Brasil May 24, 2016

Transcript of Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · •...

Page 1: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 2: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 3: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 4: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 5: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 6: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 7: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 8: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 9: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 10: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 11: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 12: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 13: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 14: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 15: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 16: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 17: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 18: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 19: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 20: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 21: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 22: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 23: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 24: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 25: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 26: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 27: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 28: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 29: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 30: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 31: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 32: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 33: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 34: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 35: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 36: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 37: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 38: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 39: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 40: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 41: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 42: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 43: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 44: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 45: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 46: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 47: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 48: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 49: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 50: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 51: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 52: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 53: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 54: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 55: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 56: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 57: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 58: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 59: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 60: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 61: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 62: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 63: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 64: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 65: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 66: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 67: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 68: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 69: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 70: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 71: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 72: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 73: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 74: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 75: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 76: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 77: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 78: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 79: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 80: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 81: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 82: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 83: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 84: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 85: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 86: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 87: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 88: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 89: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 90: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 91: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 92: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 93: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 94: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 95: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 96: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 97: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 98: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 99: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 100: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 101: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 102: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 103: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

103

Algorithm for Available Expression• Assign a Number to each Expression in the Program

Introduction to Data-Flow Analysis

Page 104: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 105: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 106: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 107: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 108: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 109: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 110: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 111: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 112: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 113: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 114: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

114

Aggregate Gen Set

OutGEN =

a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }

InGEN set

OutGEN set

Introduction to Data-Flow Analysis

Page 115: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 116: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 117: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 118: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 119: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 120: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 121: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 122: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 123: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 124: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 125: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 126: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 127: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 128: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

128

Aggregate Kill Set

OutKILL =

a = b + c 1gen = { 1 }kill = { 3, 4, 5, 7 }

InKILL set

OutKILL set

Introduction to Data-Flow Analysis

Page 129: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 130: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 131: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 132: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 133: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 134: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 135: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 136: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 137: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 138: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 139: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 140: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 141: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 142: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 143: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 144: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 145: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 146: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

146

Propagate Available Expression Set

OUT =

gen = { … }kill = { ... }

IN set

OUT set

Introduction to Data-Flow Analysis

Page 147: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 148: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 149: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

149

OUT = gen ∪ (IN - kill)

IN set

IN =

Propagate Available Expression Set

Introduction to Data-Flow Analysis

Page 150: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 151: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 152: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 153: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 154: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 155: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 156: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 157: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 158: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 159: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 160: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 161: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 162: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 163: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 164: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 165: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 166: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 167: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 168: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 169: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 170: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 171: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 172: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 173: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 174: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 175: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 176: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 177: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 178: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 179: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 180: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 181: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 182: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 183: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 184: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 185: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 186: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 187: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 188: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 189: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 190: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 191: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 192: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 193: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 194: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 195: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 196: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 197: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 198: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 199: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 200: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 201: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 202: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 203: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 204: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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⊥ = { }

Page 205: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 206: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 207: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 208: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 209: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 210: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 211: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 212: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 213: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 214: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 215: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 216: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 217: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 218: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 219: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 220: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 221: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 222: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 223: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 224: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 225: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 226: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 227: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 228: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 229: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 230: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 231: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 232: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 233: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 234: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 235: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 236: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 237: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 238: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 239: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 240: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 241: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 242: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 243: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 244: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 245: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 246: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 247: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 248: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 249: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 250: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 251: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 252: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 253: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 254: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 255: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 256: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 257: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 258: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 259: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 260: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 261: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 262: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 263: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 264: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 265: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 266: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 267: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 268: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

268

a = 1 b = a + 2c = a + ba = a + 1d = a + b

Static Single Assignment (SSA) Form

Introduction to Data-Flow Analysis

Page 269: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 270: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 271: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 272: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 273: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 274: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Pedro [email protected]

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

Page 275: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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.

Page 276: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 277: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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.

Page 278: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 279: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 280: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 281: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 282: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 283: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 284: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 285: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 286: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 287: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 288: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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))

Page 289: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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}

Page 290: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 291: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 292: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 293: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 294: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 295: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 296: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 297: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 298: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 299: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 300: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 301: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 302: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 303: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 304: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 305: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 306: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 307: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 308: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 309: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 310: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 311: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 312: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 313: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 314: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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

Page 315: Introduction to Data-Flow Analysislcr.icmc.usp.br/en/DataFlowAnalysis-Seminar-PedroDiniz.pdf · • Motivation: Why Data-Flow Analysis? • Control-Flow Analysis • Available Expressions

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

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