![Page 1: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/1.jpg)
Winter 2012-2013Compiler Principles
IR Local Optimizations – part 2Mayer Goldberg and Roman Manevich
Ben-Gurion University
![Page 2: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/2.jpg)
2
Today
• Review Sethi-Ullman code generation alg.• Define Basic blocks/control-flow graphs• Define analyses and related optimizations– Common sub-expression elimination– Copy propagation– Dead code elimination
• Formalize local analyses
![Page 3: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/3.jpg)
3
Naive cgen for expressions• Maintain a counter for temporaries in c• Initially: c = 0• cgen(e1 op e2) = {
Let A = cgen(e1) c = c + 1 Let B = cgen(e2) c = c + 1 Emit( _tc = A op B; ) Return _tc}
![Page 4: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/4.jpg)
Example
4
cgen( (a*b)-d)
![Page 5: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/5.jpg)
Example
5
c = 0cgen( (a*b)-d)
![Page 6: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/6.jpg)
Example
6
c = 0cgen( (a*b)-d) = { Let A = cgen(a*b) c = c + 1 Let B = cgen(d) c = c + 1 Emit( _tc = A - B; ) Return _tc }
![Page 7: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/7.jpg)
Example
7
c = 0cgen( (a*b)-d) = { Let A = { Let A = cgen(a) c = c + 1 Let B = cgen(b) c = c + 1 Emit( _tc = A * B; ) Return tc } c = c + 1 Let B = cgen(d) c = c + 1 Emit( _tc = A - B; ) Return _tc }
![Page 8: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/8.jpg)
Example
8
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code
here A=_t0
![Page 9: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/9.jpg)
Example
9
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;
here A=_t0
![Page 10: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/10.jpg)
Example
10
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;_t1=b;here A=_t0
![Page 11: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/11.jpg)
Example
11
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;_t1=b;_t2=_t0*_t1
here A=_t0
![Page 12: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/12.jpg)
Example
12
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;_t1=b;_t2=_t0*_t1
here A=_t0
here A=_t2
![Page 13: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/13.jpg)
Example
13
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;_t1=b;_t2=_t0*_t1_t3=d;
here A=_t0
here A=_t2
![Page 14: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/14.jpg)
Example
14
c = 0cgen( (a*b)-d) = { Let A = { Let A = { Emit(_tc = a;), return _tc } c = c + 1 Let B = { Emit(_tc = b;), return _tc } c = c + 1 Emit( _tc = A * B; ) Return _tc } c = c + 1 Let B = { Emit(_tc = d;), return _tc } c = c + 1 Emit( _tc = A - B; ) Return _tc }
Code_t0=a;_t1=b;_t2=_t0*_t1_t3=d;_t4=_t2-_t3
here A=_t0
here A=_t2
![Page 15: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/15.jpg)
15
Naive cgen for expressions• Maintain a counter for temporaries in c• Initially: c = 0• cgen(e1 op e2) = {
Let A = cgen(e1) c = c + 1 Let B = cgen(e2) c = c + 1 Emit( _tc = A op B; ) Return _tc}
• Observation: temporaries in cgen(e1) can be reused in cgen(e2)
![Page 16: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/16.jpg)
16
Improved cgen for expressions• Maintain temporaries stack by counter c• Initially: c = 0• cgen(e1 op e2) = {
Let _tc = cgen(e1) c = c + 1 Let _tc = cgen(e2) c = c - 1 Emit( _tc = _tc op _tc+1; ) Return tc}
![Page 17: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/17.jpg)
Example
17
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code
c=0
![Page 18: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/18.jpg)
Example
18
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;
c=1
![Page 19: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/19.jpg)
Example
19
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;
c=1
![Page 20: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/20.jpg)
Example
20
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;
c=0
![Page 21: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/21.jpg)
Example
21
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;_t0=_t0*_t1
c=0
![Page 22: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/22.jpg)
Example
22
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;_t0=_t0*_t1;
c=1
![Page 23: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/23.jpg)
Example
23
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;_t0=_t0*_t1;_t1=d;
c=1
![Page 24: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/24.jpg)
Example
24
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;_t0=_t0*_t1;_t1=d;
c=0
![Page 25: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/25.jpg)
Example
25
c = 0cgen( (a*b)-d) = { Let _tc = { Let _tc = { Emit(_tc = a;), return _tc } c = c + 1 Let _tc = { Emit(_tc = b;), return _tc } c = c - 1 Emit( _tc = _tc * _tc+1; ) Return _tc } c = c + 1 Let _tc = { Emit(_tc = d;), return _tc } c = c - 1 Emit( _tc = _tc - _tc+1; ) Return _tc }
Code_t0=a;_t1=b;_t0=_t0*_t1;_t1=d;_t0=_t0-_t1;
c=0
![Page 26: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/26.jpg)
26
Weighted register allocation for trees
• Sethi-Ullman’s algorithm generates code for side-effect-free expressions yields minimal number of registers
• Phase 0: check side-effect-free condition• Phase 1: Assign weights (weight = number of registers needed)
– Leaf weight known (usually 0 or 1)– Internal node weight
• w(left) > w(right) then w = left• w(right) > w(left) then w = right• w(right) = w(left) then w = left + 1
• Phase 2: translate heavier child first– Can be done by rewriting the expression such that heavier
expressions appear first and then using improved cgen
![Page 27: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/27.jpg)
Example_t0 = cgen( a+(b+(c*d)) )
+ and * are commutative operators
27
![Page 28: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/28.jpg)
Assigning weights_t0 = cgen( a+(b+(c*d)) )
+ and * are commutative operators
b
c d
*
+
+
a
w=1
w=1
28
w=1
w=2
w=2w=1
w=2
![Page 29: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/29.jpg)
Rewriting the expression_t0 = cgen( a+(b+(c*d)) )
+ and * are commutative operators
b
c d
*
+
+
a
w=1
w=1
29
w=1
w=2
w=2w=1
w=2
Rewriting expression yields:_t0 = cgen( ((c*d)+b)+a )
![Page 30: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/30.jpg)
30
Optimization points
sourcecode
Frontend IR Code
generatortargetcode
Userprofile program
change algorithm
Compilerapply IR optimizations
Compilerregister allocation
instruction selectionpeephole transformations
today
![Page 31: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/31.jpg)
31
Overview of IR optimization• Formalisms and Terminology– Control-flow graphs– Basic blocks
• Local optimizations– Speeding up small pieces of a function
• Global optimizations– Speeding up functions as a whole
• The dataflow framework– Defining and implementing a wide class of
optimizations
![Page 32: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/32.jpg)
32
Program Analysis
• In order to optimize a program, the compiler has to be able to reason about the properties of that program
• An analysis is called sound if it never asserts an incorrect fact about a program
• All the analyses we will discuss in this class are sound– (Why?)
![Page 33: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/33.jpg)
33
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
“At this point in theprogram, x holds some
integer value”
![Page 34: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/34.jpg)
34
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
“At this point in theprogram, x is either 137 or 42”
![Page 35: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/35.jpg)
35
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
“At this point in theprogram, x is 137”
![Page 36: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/36.jpg)
36
Soundness
int x;int y;
if (y < 5) x = 137;else x = 42;
Print(x);
“At this point in theprogram, x is either 137,
42, or 271”
![Page 37: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/37.jpg)
37
Semantics-preserving optimizations• An optimization is semantics-preserving if it does not
alter the semantics of the original program• Examples:– Eliminating unnecessary temporary variables– Computing values that are known statically at compile-time
instead of runtime– Evaluating constant expressions outside of a loop instead of
inside• Non-examples:– Replacing bubble sort with quicksort (why?)– The optimizations we will consider in this class are all
semantics-preserving
![Page 38: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/38.jpg)
38
A formalism for IR optimization
• Every phase of the compiler uses some new abstraction:– Scanning uses regular expressions– Parsing uses CFGs– Semantic analysis uses proof systems and symbol
tables– IR generation uses ASTs
• In optimization, we need a formalism that captures the structure of a program in a way amenable to optimization
![Page 39: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/39.jpg)
39
Visualizing IRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
![Page 40: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/40.jpg)
40
Visualizing IRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
![Page 41: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/41.jpg)
41
Visualizing IRmain:
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_L0:_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
_L1:Push a;Call _PrintInt;
_tmp0 = Call _ReadInteger;a = _tmp0;_tmp1 = Call _ReadInteger;b = _tmp1;
_tmp2 = 0;_tmp3 = b == _tmp2;_tmp4 = 0;_tmp5 = _tmp3 == _tmp4;IfZ _tmp5 Goto _L1;
c = a;a = b;_tmp6 = c % a;b = _tmp6;Goto _L0;
Push a;Call _PrintInt;
start
end
![Page 42: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/42.jpg)
42
Basic blocks
• A basic block is a sequence of IR instructions where– There is exactly one spot where control enters the
sequence, which must be at the start of the sequence
– There is exactly one spot where control leaves the sequence, which must be at the end of the sequence
• Informally, a sequence of instructions that always execute as a group
![Page 43: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/43.jpg)
43
Control-Flow Graphs• A control-flow graph (CFG) is a graph of the basic
blocks in a function• The term CFG is overloaded – from here on out,
we'll mean “control-flow graph” and not “context free grammar”
• Each edge from one basic block to another indicates that control can flow from the end of the first block to the start of the second block
• There is a dedicated node for the start and end of a function
![Page 44: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/44.jpg)
44
Types of optimizations
• An optimization is local if it works on just a single basic block
• An optimization is global if it works on an entire control-flow graph
• An optimization is interprocedural if it works across the control-flow graphs of multiple functions– We won't talk about this in this course
![Page 45: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/45.jpg)
45
Basic blocks exerciseint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
START:_t0 = 137;y = _t0;IfZ x Goto _L0;t1 = y;z = _t1;Goto END:
_L0:_t2 = y;x = _t2;
END:
Divide the code into basic blocks
![Page 46: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/46.jpg)
46
Control-flow graph exerciseint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
START:_t0 = 137;y = _t0;IfZ x Goto _L0;t1 = y;z = _t1;Goto END:
_L0:_t2 = y;x = _t2;
END:
Draw the control-flow graph
![Page 47: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/47.jpg)
47
Control-flow graph exerciseint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
![Page 48: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/48.jpg)
48
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
start
![Page 49: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/49.jpg)
49
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
_t0 = 137;y = _t0;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
![Page 50: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/50.jpg)
50
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
![Page 51: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/51.jpg)
51
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
_t1 = y;z = _t1;
_t2 = y;x = _t2;
End
![Page 52: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/52.jpg)
52
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = y;_t2 = y;x = _t2;
End
![Page 53: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/53.jpg)
53
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = y;_t2 = y;x = _t2;
End
![Page 54: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/54.jpg)
54
Local optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = y; x = y;
End
![Page 55: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/55.jpg)
55
Global optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = y; x = y;
End
![Page 56: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/56.jpg)
56
Global optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = y; x = y;
End
![Page 57: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/57.jpg)
57
Global optimizationsint main() {
int x;int y;int z;
y = 137;if (x == 0)
z = y;else
x = y;{
y = 137;IfZ x Goto _L0;
start
z = 137; x = 137;
End
![Page 58: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/58.jpg)
58
Local Optimizations
![Page 59: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/59.jpg)
59
Optimization path
IR Control-FlowGraph
CFGbuilder
ProgramAnalysis
AnnotatedCFG
OptimizingTransformation
TargetCode
CodeGeneration
(+optimizations)
donewith IR
optimizations
IRoptimizations
![Page 60: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/60.jpg)
60
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 61: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/61.jpg)
61
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = a + b;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 62: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/62.jpg)
62
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 63: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/63.jpg)
63
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = 4;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 64: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/64.jpg)
64
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 65: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/65.jpg)
65
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = _tmp4;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 66: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/66.jpg)
66
Common subexpression eliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 67: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/67.jpg)
67
Common Subexpression Elimination• If we have two variable assignments
v1 = a op b…v2 = a op b
• and the values of v1, a, and b have not changed between the assignments, rewrite the code asv1 = a op b…v2 = v1
• Eliminates useless recalculation• Paves the way for later optimizations
![Page 68: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/68.jpg)
68
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 69: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/69.jpg)
69
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(x);_tmp7 = *(_tmp6);Push _tmp5;Push x;Call _tmp7;
![Page 70: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/70.jpg)
70
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
![Page 71: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/71.jpg)
71
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = a + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
![Page 72: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/72.jpg)
72
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
![Page 73: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/73.jpg)
73
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push _tmp5;Push _tmp1;Call _tmp7;
![Page 74: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/74.jpg)
74
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
![Page 75: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/75.jpg)
75
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = *(_tmp1);_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
![Page 76: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/76.jpg)
76
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
![Page 77: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/77.jpg)
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp6);Push c;Push _tmp1;Call _tmp7;
77
![Page 78: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/78.jpg)
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
78
![Page 79: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/79.jpg)
Copy Propagation
79
Object x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp3;_tmp4 = _tmp3 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
![Page 80: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/80.jpg)
Copy PropagationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
80
![Page 81: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/81.jpg)
Copy Propagation
• If we have a variable assignmentv1 = v2then as long as v1 and v2 are not reassigned, we can rewrite expressions of the forma = … v1 …asa = … v2 …provided that such a rewrite is legal
81
![Page 82: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/82.jpg)
Dead Code EliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
82
![Page 83: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/83.jpg)
Dead Code EliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;x = _tmp1;_tmp3 = _tmp0;a = _tmp0;_tmp4 = _tmp0 + b;c = _tmp4;_tmp5 = c;_tmp6 = _tmp2;_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
83
values never read
values never read
![Page 84: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/84.jpg)
Dead Code EliminationObject x;int a;int b;int c;
x = new Object;a = 4;c = a + b;x.fn(a + b);
_tmp0 = 4;Push _tmp0;_tmp1 = Call _Alloc;Pop tmp2;*(_tmp1) = _tmp2;
_tmp4 = _tmp0 + b;c = _tmp4;
_tmp7 = *(_tmp2);Push c;Push _tmp1;Call _tmp7;
84
![Page 85: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/85.jpg)
85
Dead Code Elimination
• An assignment to a variable v is called dead if the value of that assignment is never read anywhere
• Dead code elimination removes dead assignments from IR
• Determining whether an assignment is dead depends on what variable is being assigned to and when it's being assigned
![Page 86: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/86.jpg)
86
Applying local optimizations• The different optimizations we've seen so far all
take care of just a small piece of the optimization• Common subexpression elimination eliminates
unnecessary statements• Copy propagation helps identify dead code• Dead code elimination removes statements that
are no longer needed• To get maximum effect, we may have to apply
these optimizations numerous times
![Page 87: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/87.jpg)
87
Applying local optimizations example
b = a * a;c = a * a;d = b + c;e = b + b;
![Page 88: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/88.jpg)
88
Applying local optimizations example
b = a * a;c = a * a;d = b + c;e = b + b;
Which optimization should we apply here?
![Page 89: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/89.jpg)
89
Applying local optimizations example
b = a * a;c = b;d = b + c;e = b + b;
Common sub-expression elimination
Which optimization should we apply here?
![Page 90: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/90.jpg)
90
Applying local optimizations example
b = a * a;c = b;d = b + c;e = b + b;
Which optimization should we apply here?
![Page 91: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/91.jpg)
91
Applying local optimizations example
b = a * a;c = b;d = b + b;e = b + b;
Which optimization should we apply here?
Copy propagation
![Page 92: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/92.jpg)
92
Applying local optimizations example
b = a * a;c = b;d = b + b;e = b + b;
Which optimization should we apply here?
![Page 93: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/93.jpg)
93
Applying local optimizations example
b = a * a;c = b;d = b + b;e = d;
Which optimization should we apply here?
Common sub-expression elimination (again)
![Page 94: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/94.jpg)
94
Other types of local optimizations
• Arithmetic Simplification– Replace “hard” operations with easier ones– e.g. rewrite x = 4 * a; as x = a << 2;
• Constant Folding– Evaluate expressions at compile-time if they have
a constant value.– e.g. rewrite x = 4 * 5; as x = 20;
![Page 95: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/95.jpg)
95
Optimizations and analyses
• Most optimizations are only possible given some analysis of the program's behavior
• In order to implement an optimization, we will talk about the corresponding program analyses
![Page 96: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/96.jpg)
96
Available expressions• Both common subexpression elimination and
copy propagation depend on an analysis of the available expressions in a program
• An expression is called available if some variable in the program holds the value of that expression
• In common subexpression elimination, we replace an available expression by the variable holding its value
• In copy propagation, we replace the use of a variable by the available expression it holds
![Page 97: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/97.jpg)
97
Finding available expressions
• Initially, no expressions are available• Whenever we execute a statement
a = b op c:– Any expression holding a is invalidated– The expression a = b op c becomes available
• Idea: Iterate across the basic block, beginning with the empty set of expressions and updating available expressions at each variable
![Page 98: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/98.jpg)
98
Available expressions example
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;{ a = b, c = b, d = b, e = a + b }
{ a = b, c = b, d = a + b, e = a + b }
{ a = b, c = b, d = a + b }
{ a = b, c = b }
{ a = b }
{ }
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 99: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/99.jpg)
99
Common sub-expression elimination
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;{ a = b, c = b, d = b, e = a + b }
{ a = b, c = b, d = a + b, e = a + b }
{ a = b, c = b, d = a + b }
{ a = b, c = b }
{ a = b }
{ }
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 100: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/100.jpg)
100
Common sub-expression elimination
a = b;
c = b;
d = a + b;
e = a + b;
d = b;
f = a + b;{ a = b, c = b, d = b, e = a + b }
{ a = b, c = b, d = a + b, e = a + b }
{ a = b, c = b, d = a + b }
{ a = b, c = b }
{ a = b }
{ }
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 101: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/101.jpg)
101
Common sub-expression elimination
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ a = b, c = b, d = b, e = a + b }
{ a = b, c = b, d = a + b, e = a + b }
{ a = b, c = b, d = a + b }
{ a = b, c = b }
{ a = b }
{ }
{ a = b, c = b, d = b, e = a + b, f = a + b }
![Page 102: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/102.jpg)
102
Live variables
• The analysis corresponding to dead code elimination is called liveness analysis
• A variable is live at a point in a program if later in the program its value will be read before it is written to again
• Dead code elimination works by computing liveness for each variable, then eliminating assignments to dead variables
![Page 103: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/103.jpg)
Computing live variables• To know if a variable will be used at some point, we
iterate across the statements in a basic block in reverse order
• Initially, some small set of values are known to be live (which ones depends on the particular program)
• When we see the statement a = b op c:– Just before the statement, a is not alive, since its value is
about to be overwritten– Just before the statement, both b and c are alive, since
we're about to read their values– (what if we have a = a + b?)
103
![Page 104: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/104.jpg)
Liveness analysis
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ b, d, e }
{ a, b, e }
{ a, b, d }
{ a, b }
{ a, b }
{ b }
{ b, d } - given
Which statements are dead?
104
![Page 105: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/105.jpg)
Dead Code Elimination
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ b, d, e }
{ a, b, e }
{ a, b, d }
{ a, b }
{ a, b }
{ b }
{ b, d }
Which statements are dead?
105
![Page 106: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/106.jpg)
Dead Code Elimination
a = b;
d = a + b;
e = d;
d = a;{ b, d, e }
{ a, b, e }
{ a, b, d }
{ a, b }
{ a, b }
{ b }
{ b, d }106
![Page 107: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/107.jpg)
Liveness analysis II
a = b;
d = a + b;
e = d;
d = a;{ b, d }
{ a, b }
{ a, b, d }
{ a, b }
{ b }
Which statements are dead?
107
![Page 108: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/108.jpg)
Liveness analysis II
a = b;
d = a + b;
e = d;
d = a;{ b, d }
{ a, b }
{ a, b, d }
{ a, b }
{ b }
Which statements are dead?
108
![Page 109: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/109.jpg)
Dead code elimination
a = b;
d = a + b;
e = d;
d = a;{ b, d }
{ a, b }
{ a, b, d }
{ a, b }
{ b }
Which statements are dead?
109
![Page 110: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/110.jpg)
Dead code elimination
a = b;
d = a + b;
d = a;{ b, d }
{ a, b }
{ a, b, d }
{ a, b }
{ b }
110
![Page 111: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/111.jpg)
Liveness analysis III
a = b;
d = a + b;
d = a;{ b, d }
{ a, b }
{ a, b }
{ b }
Which statements are dead?
111
![Page 112: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/112.jpg)
Dead code elimination
a = b;
d = a + b;
d = a;{ b, d }
{ a, b }
{ a, b }
{ b }
Which statements are dead?
112
![Page 113: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/113.jpg)
Dead code elimination
a = b;
d = a;{ b, d }
{ a, b }
{ a, b }
{ b }
113
![Page 114: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/114.jpg)
Dead code elimination
a = b;
d = a;
114
If we further apply copy propagation this statement can be eliminated too
![Page 115: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/115.jpg)
A combined algorithm
• Start with initial live variables at end of block• Traverse statements from end to beginning• For each statement– If assigns to dead variables – eliminate it– Otherwise, compute live variables before
statement and continue in reverse
115
![Page 116: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/116.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;
116
![Page 117: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/117.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ b, d }
117
![Page 118: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/118.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
f = e;{ b, d }
118
![Page 119: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/119.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
{ b, d }119
![Page 120: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/120.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
e = d;
d = a;
{ b, d }
{ a, b }
120
![Page 121: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/121.jpg)
A combined algorithm
121
a = b;
c = a;
d = a + b;
e = d;
d = a;
{ b, d }
{ a, b }
![Page 122: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/122.jpg)
A combined algorithm
122
a = b;
c = a;
d = a + b;
d = a;
{ b, d }
{ a, b }
![Page 123: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/123.jpg)
A combined algorithm
a = b;
c = a;
d = a + b;
d = a;
{ b, d }
{ a, b }
123
![Page 124: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/124.jpg)
A combined algorithm
a = b;
c = a;
d = a;
{ b, d }
{ a, b }
124
![Page 125: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/125.jpg)
A combined algorithm
a = b;
c = a;
d = a;
{ b, d }
{ a, b }
125
![Page 126: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/126.jpg)
A combined algorithm
a = b;
d = a;
{ b, d }
{ a, b }
126
![Page 127: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/127.jpg)
A combined algorithm
a = b;
d = a;
{ b, d }
{ a, b }
127
{ b }
![Page 128: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/128.jpg)
A combined algorithm
a = b;
d = a;
128
![Page 129: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/129.jpg)
129
High-level goals
• Generalize analysis mechanism– Reuse common ingredients for many analyses– Reuse proofs of correctness
• Generalize from basic blocks to entire CFGs– Go from local optimizations to global
optimizations
![Page 130: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/130.jpg)
130
Formalizing local analyses
a = b + c
Output ValueVout
Input ValueVin
Vout = fa=b+c(Vin)
Transfer Function
![Page 131: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/131.jpg)
131
Available Expressions
a = b + c
Output ValueVout
Input ValueVin
Vout = (Vin \ {e | e contains a}) {a=b+c}
Expressions of the forms a=… and x=…a…
![Page 132: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/132.jpg)
132
Live Variables
a = b + c
Output ValueVout
Input ValueVin
Vin = (Vout \ {a}) {b,c}
![Page 133: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/133.jpg)
133
Another view of local optimization
• In local optimization, we want to reason about some property of the runtime behavior of the program
• Could we run the program and just watch what happens?
• Idea: Redefine the semantics of our programming language to give us information about our analysis
![Page 134: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/134.jpg)
134
Properties of local analysis• The only way to find out what a program will
actually do is to run it• Problems:– The program might not terminate– The program might have some behavior we didn't
see when we ran it on a particular input• However, this is not a problem inside a basic
block– Basic blocks contain no loops– There is only one path through the basic block
![Page 135: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/135.jpg)
135
Assigning new semantics
• Example: Available Expressions• Redefine the statement a = b + c to mean “a
now holds the value of b + c, and any variable holding the value a is now invalid”
• Run the program assuming these new semantics
• Treat the optimizer as an interpreter for these new semantics
![Page 136: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/136.jpg)
136
Information for a local analysis
• What direction are we going?– Sometimes forward (available expressions)– Sometimes backward (liveness analysis)
• How do we update information after processing a statement?– What are the new semantics?– What information do we know initially?
![Page 137: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/137.jpg)
137
Formalizing local analyses
• Define an analysis of a basic block as a quadruple (D, V, F, I) where– D is a direction (forwards or backwards)– V is a set of values the program can have at any
point– F is a family of transfer functions defining the
meaning of any expression as a function f : V V• I is the initial information at the top (or
bottom) of a basic block
![Page 138: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/138.jpg)
138
Available Expressions• Direction: Forward• Domain: Sets of expressions assigned to variables• Transfer functions: Given a set of variable
assignments V and statement a = b + c:– Remove from V any expression containing a as a
subexpression– Add to V the expression a = b + c– Formally: Vout = (Vin \ {e | e contains a}) {a = b + c}
• Initial value: Empty set of expressions
![Page 139: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/139.jpg)
139
Liveness Analysis• Direction: Backward• Domain: Sets of variables• Transfer functions: Given a set of variable assignments
V and statement a = b + c:• Remove a from V (any previous value of a is now dead.)• Add b and c to V (any previous value of b or c is now
live.)• Formally: Vin = (Vout \ {a}) {b,c}• Initial value: Depends on semantics of language– E.g., function arguments and return values (pushes)
![Page 140: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/140.jpg)
Running local analyses
• Given an analysis (D, V, F, I) for a basic block.• Assume that D is “forward;” analogous for the
reverse case• Initially, set OUT[entry] to I• For each statement s, in order:– Set IN[s] to OUT[prev], where prev is the previous
statement– Set OUT[s] to fs(IN[s]), where fs is the transfer
function for statement s
140
![Page 141: Winter 2012-2013 Compiler Principles IR Local Optimizations – part 2](https://reader035.vdocuments.site/reader035/viewer/2022062520/568160c9550346895dcffac2/html5/thumbnails/141.jpg)
See you next time
141