IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially,...

59
* Course website: https://www.cs.columbia.edu/ rgu/courses//spring IR Optimization Ronghui Gu Spring Columbia University

Transcript of IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially,...

Page 1: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

∗ Course website: https://www.cs.columbia.edu/ rgu/courses/4115/spring2019

IR Optimization

Ronghui GuSpring 2020

Columbia University

1

Page 2: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

IR Optimization

int avg (int a, int b) ...

Lexical Analysis

Syntax Analysis

Semantic Analysis

Intermediate Code Generation

IR Optimization

Code Generation

0101110101...

front-end

middle-end

back-end

2

Page 3: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

IR Optimization

Goal

• Runtime• Memory usage• Power Consumption

Sources?

3

Page 4: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from IR Generation

C code:int x;int y;bool b1;bool b2;bool b3;b1 = x + x < yb2 = x + x == yb3 = x + x > y

Three-Address:_t0 = x + x;_t1 = y ;b1 = _t0 < _t1 ;_t2 = x + x;_t3 = y ;b2 = _t2 == _t3;_t4 = x + x;_t5 = y ;b3 = _t5 < _t4;

4

Page 5: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from IR Generation

C code:int x;int y;bool b1;bool b2;bool b3;b1 = x + x < yb2 = x + x == yb3 = x + x > y

Three-Address:_t0 = x + x;_t1 = y ;b1 = _t0 < _t1 ;_t2 = x + x;_t3 = y;b2 = _t2 == _t3;_t4 = x + x;_t5 = y;b3 = _t5 < _t4;

5

Page 6: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from IR Generation

C code:int x;int y;bool b1;bool b2;bool b3;b1 = x + x < yb2 = x + x == yb3 = x + x > y

Three-Address:_t0 = x + x;_t1 = y ;b1 = _t0 < _t1 ;

b2 = _t0 == _t1;

b3 = _t0 < _t1;

6

Page 7: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from Lazy Coders

C code:while (x < y + z) {

x = x - y;}

Three-Address:_L0:

_t0 = y + z;_t1 = x < _t0;bz _L1 _t1 ;x = x − y;jmp _L0;

_L1 :

7

Page 8: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from Lazy Coders

C code:while (x < y + z) {

x = x - y;}

Three-Address:_L0:

_t0 = y + z;_t1 = x < _t0;bz _L1 _t1 ;x = x − y;jmp _L0;

_L1 :

8

Page 9: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Optimizations from Lazy Coders

C code:while (x < y + z) {

x = x - y;}

Three-Address:_t0 = y + z;

_L0:_t1 = x < _t0;bz _L1 _t1 ;x = x − y;jmp _L0;

_L1 :

9

Page 10: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

IR Optimization Discussion

Optimal? Undecidable!

Soundness: semantics-preserving

IR optimization v.s. code optimization:

x * 0.5 ⇒ x » 1

Local optimization v.s. global optimization

10

Page 11: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Local Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

t0 = 137;y = t0;bz L0 x;

t1 = y;z = t1;

t2 = y;x = t2;

END:

11

Page 12: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Local Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

t0 = 137;y = t0;bz L0 x;

t1 = y;z = t1;

t2 = y;x = t2;

END:

12

Page 13: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Local Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

y = 137;bz L0 x;

t1 = y;z = t1;

t2 = y;x = t2;

END:

13

Page 14: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Local Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

y = 137;bz L0 x;

z =y; x = y;

END:

14

Page 15: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

y = 137;IFZ x Goto L0;

z =y; x = y;

END:

15

Page 16: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Optimization

int main() {int y;int z;y = 137;if (x == 0)z = y;

elsex = y;

}

START:

IFZ x Goto L0;

z =137; x = 137;

END:

16

Page 17: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Local Optimization

Page 18: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Common Subexpression Elimination

v1 = a op b

. . .

v2 = a op b

If values of v1, a, and b have not changed, rewrite the code:

v1 = a op b

. . .

v2 = v1

17

Page 19: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Common Subexpression Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = _t0;_t1 = a + b;c = _t1 ;_t2 = a + b;param _t2call f ;

18

Page 20: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Common Subexpression Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = _t0;_t1 = a + b;c = _t1 ;_t2 = a + b;param _t2call f ;

19

Page 21: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Common Subexpression Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = _t0;_t1 = a + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

20

Page 22: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

If we have

v1 = v2

then as long as v1 and v2 have not changed, we can rewrite

a = ... v1 ...

as

a = ... v2 ...

21

Page 23: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = _t0;_t1 = a + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

22

Page 24: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = a + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

23

Page 25: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = a + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

24

Page 26: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = 4 + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

25

Page 27: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = 4 + b;c = _t1 ;_t2 = _t1 ;param _t2call f ;

26

Page 28: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Copy Propagation

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = 4 + b;c = _t1 ;_t2 = _t1 ;param _t1call f ;

27

Page 29: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Dead Code Elimination

An assignment to a variable v is called dead if its value isnever read anywhere.

28

Page 30: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Dead Code Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = 4 + b;c = _t1 ;_t2 = _t1 ;param _t1call f ;

29

Page 31: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Dead Code Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = 4;_t1 = 4 + b;c = _t1 ;_t2 = _t1 ;param _t1call f ;

30

Page 32: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Dead Code Elimination

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t1 = 4 + b;param _t1call f ;

31

Page 33: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

For Comparison

C code:int a;int b;int c;a = 4;c = a + b;f(a + b);

Three-address code:_t0 = 4;a = _t0;_t1 = a + b;c = _t1 ;_t2 = a + b;param _t2call f ;

Optimized code:

_t1 = 4 + b;param _t1call f ;

32

Page 34: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

Initially, some small set of values are known to be live.

When we see the statement a = b + c:

• Just before the statement, a is not alive, since its value isabout to be overwritten.

• Just before the statement, both b and c are alive, sincewe’re about to read their values.

• what if we have a = b + a?

33

Page 35: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 36: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 37: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 38: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 39: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 40: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 41: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Live Variables (for dead code elimination)

{ b }

a = b;

c = a;

d = a + b;

e = d;

{ a, b }

d = a;

f = e;

{b, d}

34

Page 42: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

An expression is called available if some variable in theprogram holds the value of that expression.

Both common subexpression elimination and copypropagation depend on an analysis of the availableexpressions in a program.

Initially, no expressions are available.

When we see the statement a = b + c:

• Any expression holding a is invalidated.• The expression a = b + c becomes available.

35

Page 43: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 44: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 45: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 46: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 47: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 48: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 49: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = a + b;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = a + b;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 50: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Computing Available Expressions

{ }

a = b;

{ a=b }

c = b;

{ a=b, c=b }

d = a + b;

{ a=b, c=b, d=a+b }

e = d;

{ a=b, c=b, d=a+b, e=a+b }

d = b;

{ a=b, c=b, d=b, e=a+b }

f = e;

{ a=b, c=b, d=b, e=a+b, f=a+b }

36

Page 51: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Other Types of Local Optimization

Arithmetic simplication:

• e.g., rewrite x = 4 * a as x = a « 2

Constant folding:

• e.g., rewrite x = 4 * 5 as x = 20

37

Page 52: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Optimization

Page 53: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Constant Propagation

START:

a = 6;

b = a; c = b;

END: d = a

38

Page 54: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Constant Propagation

Replace each variable that is known to be a constant valuewith the constant.

39

Page 55: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Constant Propagation

START:

a = 6;x = y;

b = a; c = b;

END: d = x + a

40

Page 56: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Constant Propagation

START:

a = 6;x = y;

b = 6; c = b;

END: d =x + 6

41

Page 57: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Dead Code Elimination

START:

a = 6;x = y;

b = 6; c = b;

END: d = x + 6

42

Page 58: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Dead Code Elimination

START:

a = 6;x = y;

b = 6; c = b;

END: d = x + 6

43

Page 59: IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially, some small set of values are known to be live. When we see the statement a = b +

Global Dead Code Elimination

START:

x = y;

END: d = x + 6

44