IR Optimization · 2020-05-03 · Computing Live Variables (for dead code elimination) Initially,...
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/1.jpg)
∗ 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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/2.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/3.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/4.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/5.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/6.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/7.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/8.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/9.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/10.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/11.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/12.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/13.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/14.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/15.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/16.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/17.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/18.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/19.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/20.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/21.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/22.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/23.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/24.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/25.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/26.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/27.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/28.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/29.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/30.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/31.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/32.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/33.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/34.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/35.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/36.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/37.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/38.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/39.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/40.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/41.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/42.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/43.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/44.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/45.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/46.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/47.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/48.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/49.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/50.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/51.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/52.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/53.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/54.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/55.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/56.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/57.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/58.jpg)
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 +](https://reader036.fdocuments.us/reader036/viewer/2022081406/5f10d7b27e708231d44b1496/html5/thumbnails/59.jpg)
Global Dead Code Elimination
START:
x = y;
END: d = x + 6
44