compiler optimization presentation
TRANSCRIPT
Compiler Optimization
Name : Khaled Hasan Rony
ID : 011132009
Compiler Lab - Section SB
1
What is compiler optimization ?
compiler optimization = code optimization
transformations, algorithms which take a program and transform it to an equivalent output program that uses fewer resources.
• minimizing program executing time
• minimizing memory use
• minimizing the power consumed by a program
2
Instruction Combining
int i;
void f()
{
i++;
i++;
}
int i;
void f()
{
i += 2;
}
3
Constant Folding
int f()
{
// do other work
return (3 + 5);
}
int f()
{
// do other work
return 8;
}
4
Constant Propagation
void f()
{
x = 3;
y = x + 4;
}
void f()
{
x = 3;
y = 7;
}
5
Common SubExpression (CSE) Elimination
void f()
{
i = x + y + 1;
j = x + y;
}
void f()
{
t1 = x + y;
i = t1 + 1;
j = t1;
}
6
Integer Multiply Optimization
int f(int i)
{
return i * 4;
}
int f(int i)
{
return i << 2;
}
7
Integer Divide Optimization
int f(int i)
{
return i / 2;
}
int f(int i)
{
return i >> 1;
}
8
Loop Fusion
void f()
{
int i;
for( i = 0; i < 100; i++ )
a[i] += 10;
for( i = 0; i < 100; i++ )
b[i] += 20;
}
void f()
{
int i;
for( i = 0; i < 100; i++ )
{
a[i] += 10;
b[i] += 20;
}
}
9
Dead Code Elimination
int global;
void f()
{
int i ;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable*/
}
int global;
void f()
{
global = 2;
return;
}
10
Expression Simplification
void f(int i)
{
a[0] = i + 0;
a[1] = i * 0;
a[2] = i - i;
a[3] = 1 + i + 1;
}
void f(int i)
{
a[0] = i;
a[1] = 0;
a[2] = 0;
a[3] = 2 + i;
}
11
Forward Store
int sum;
void f()
{
int i;
sum = 0;
for( i = 0; i < 100; i++ )
{
sum += a[i];
}
}
int sum;
void f()
{
int i;
register int t;
t = 0;
for( i = 0; i < 100; i++ )
{
t += a[i];
}
sum = t;
}
12
If Optimization (1)
void f(int *p)
{
if( p )
g(1);
if( p )
g(2);
}
void f(int *p)
{
if( p )
{
g(1);
g(2);
}
}
13
If Optimization (2)
void f(int *p){
if( p ){
g(1);if( p )
g(2);g(3);
}}
void f(int *p){
if( p ){
g(1);g(2);g(3);
}}
14
New Expression Optimization
{
int a[];
a = new int[100];
}
{
/*
a not used, so not allocated
*/
}
15
Try Catch Block Optimization
try
{
a = (int) 5;
}
Catch(Exception e)
{
//………
}
a = 5;
16
Loop Unrolling
for( i = 0; i < 100; i++ )
{
g();
}
for( i = 0; i < 100; i += 2 )
{
g();
g();
}
17
Unswitching
for( i = 0; i < 100; i++ ){
if( x )a[i] = i;
elseb[i] = i;
}
if( x ){
for( i = 0; i < 100; i++ )a[i] = i;
}else{
for( i = 0; i < 100; i++ )b[i] = i;
}
18
Why we need compiler optimization ?
19
Reference
http://www.compileroptimizations.com/index.html
20
Any questions ?
21
Thank you.
22