compiler optimization presentation

22
Compiler Optimization Name : Khaled Hasan Rony ID : 011132009 Compiler Lab - Section SB 1

Upload: 19magnet

Post on 21-Jan-2018

639 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Compiler Optimization Presentation

Compiler Optimization

Name : Khaled Hasan Rony

ID : 011132009

Compiler Lab - Section SB

1

Page 2: Compiler Optimization Presentation

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

Page 3: Compiler Optimization Presentation

Instruction Combining

int i;

void f()

{

i++;

i++;

}

int i;

void f()

{

i += 2;

}

3

Page 4: Compiler Optimization Presentation

Constant Folding

int f()

{

// do other work

return (3 + 5);

}

int f()

{

// do other work

return 8;

}

4

Page 5: Compiler Optimization Presentation

Constant Propagation

void f()

{

x = 3;

y = x + 4;

}

void f()

{

x = 3;

y = 7;

}

5

Page 6: Compiler Optimization Presentation

Common SubExpression (CSE) Elimination

void f()

{

i = x + y + 1;

j = x + y;

}

void f()

{

t1 = x + y;

i = t1 + 1;

j = t1;

}

6

Page 7: Compiler Optimization Presentation

Integer Multiply Optimization

int f(int i)

{

return i * 4;

}

int f(int i)

{

return i << 2;

}

7

Page 8: Compiler Optimization Presentation

Integer Divide Optimization

int f(int i)

{

return i / 2;

}

int f(int i)

{

return i >> 1;

}

8

Page 9: Compiler Optimization Presentation

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

Page 10: Compiler Optimization Presentation

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

Page 11: Compiler Optimization Presentation

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

Page 12: Compiler Optimization Presentation

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

Page 13: Compiler Optimization Presentation

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

Page 14: Compiler Optimization Presentation

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

Page 15: Compiler Optimization Presentation

New Expression Optimization

{

int a[];

a = new int[100];

}

{

/*

a not used, so not allocated

*/

}

15

Page 16: Compiler Optimization Presentation

Try Catch Block Optimization

try

{

a = (int) 5;

}

Catch(Exception e)

{

//………

}

a = 5;

16

Page 17: Compiler Optimization Presentation

Loop Unrolling

for( i = 0; i < 100; i++ )

{

g();

}

for( i = 0; i < 100; i += 2 )

{

g();

g();

}

17

Page 18: Compiler Optimization Presentation

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

Page 19: Compiler Optimization Presentation

Why we need compiler optimization ?

19

Page 20: Compiler Optimization Presentation

Reference

http://www.compileroptimizations.com/index.html

20

Page 21: Compiler Optimization Presentation

Any questions ?

21

Page 22: Compiler Optimization Presentation

Thank you.

22