improvements to the compiler
DESCRIPTION
Improvements to the Compiler. Lecture 27 Mon, Apr 26, 2004. Overflowing the Stack. Each expression leaves a value on the stack. That is because expressions are typically subexpressions of larger expressions. Their values are used in the larger expression. Example: n = n + 1; - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/1.jpg)
Improvements to the Compiler
Lecture 27
Mon, Apr 26, 2004
![Page 2: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/2.jpg)
Overflowing the Stack
Each expression leaves a value on the stack. That is because expressions are typically
subexpressions of larger expressions. Their values are used in the larger expression.
Example: n = n + 1; However, at some point, the value is no
longer needed. If we leave it on the stack, then eventually the
stack will overflow.
![Page 3: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/3.jpg)
Example
read n;d = 2;while (d < n){ if (n % d == 0) { n = n / d; print d; } else d = d + 1;}
This loop will fail after about 500,000 iterations.
![Page 4: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/4.jpg)
Excessive Pushing and Popping
Most expressions Begin with a pop operation, and End with a push operation.
In most cases, the value being pushed by one expression is popped immediately by the next expression, making the two operations unnecessary.
![Page 5: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/5.jpg)
Example
n = n + 1;
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 6: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/6.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 7: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/7.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 8: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/8.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 9: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/9.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 10: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/10.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 11: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/11.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
![Page 12: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/12.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush %eaxpop %eaxpush (%eax)push $1pop %eaxpop %edxadd %edx,%eaxpush %eaxpop %eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 13: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/13.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 14: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/14.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 15: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/15.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 16: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/16.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 17: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/17.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxmov (%eax),%edxmov $1,%eaxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 18: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/18.jpg)
Example
lea n,%eaxpush %eaxlea n,%eaxpush (%eax)mov $1,%eaxpop %edxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
lea n,%eaxpush %eaxmov (%eax),%edxmov $1,%eaxadd %edx,%eaxpop %edxmov %eax,(%edx)push %eax
![Page 19: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/19.jpg)
Eliminating Dead Code
Code that cannot be reached by any logical path is called dead code.
Dead code can be detected by looking for unlabeled statements following unconditional jumps.
Such statements are unreachable.
![Page 20: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/20.jpg)
Eliminating Dead Code
Make one pass of the assembly language program, compiling a list of all labels that are referenced.
Make a second pass, removing all unreferenced labels.
Make a third pass removing all code occurring between unconditional jumps and the next label.
![Page 21: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/21.jpg)
ExampleL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret# Code for LABELL2: mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}
![Page 22: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/22.jpg)
Remove Unreferenced LabelsL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret# Code for LABELL2: mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}
![Page 23: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/23.jpg)
Remove Unreferenced LabelsL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret# Code for LABEL mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}
![Page 24: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/24.jpg)
Find Unconditional BranchesL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret# Code for LABEL mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}
![Page 25: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/25.jpg)
Remove Unreachable CodeL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret# Code for LABEL mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}
![Page 26: Improvements to the Compiler](https://reader031.vdocuments.us/reader031/viewer/2022020417/56813d6e550346895da74fbb/html5/thumbnails/26.jpg)
Remove Unreachable CodeL1:# Code for ID lea 8(%ebp),%eax push %eax# Code for DEREF pop %eax push (%eax) pop %eax mov %ebp,%esp pop %ebp ret
int copy(int a){
return a;}