compiler optimization

16
Compiler Optimization Speaker 呂呂呂 Adviser 呂呂呂 呂呂 Date 2006/10/11

Upload: zongying-lyu

Post on 15-Jun-2015

264 views

Category:

Software


2 download

DESCRIPTION

Compiler optimization

TRANSCRIPT

Page 1: Compiler optimization

Compiler Optimization

Speaker:呂宗螢Adviser:梁文耀 老師Date : 2006/10/11

Page 2: Compiler optimization

Embedded and Parallel Systems Lab

2

The Structure of Recent

Front end per language

High-level optimizations

Global optimizer

Code generator

Intermediate representation

Dependencies FunctionLanguage Dependent machine independent

Transform language to common intermediate form

Largely machine independent For example, procedure inlineing (also called procedure intergration)

Small language dependencies machine dependencies slight (e.g., register count/types)

Including global and local optimizations + register allocation

Highly machine dependent

Language independent

Detailed instruction selection and machine-dependent optimizations

May include or be followed by assembler

Page 3: Compiler optimization

Embedded and Parallel Systems Lab

3

Major types of optimizations

Page 4: Compiler optimization

Embedded and Parallel Systems Lab

4

Procedure integration

Replace procedure call by procedure body

Int a;

void up(){

a=a+1;

}

Void main(){

a=10;

up();

b=a+5;

}

Int a;

Void main(){

a=10;

a=a+1;

b=a+5;

}

Page 5: Compiler optimization

Embedded and Parallel Systems Lab

5

Common subexpression elimination

Replace two instances of same computation by single copy

a = b * c + g;

d = b * c * d;

tmp = b * c;

a = tmp + g;

d = tmp * d;

Page 6: Compiler optimization

Embedded and Parallel Systems Lab

6

Constant propagation

Replace all instances of a variable that is assigned a constant with the constant

int x = 14;

int y = 7 - x / 2;

return y * (28 / x + 2);

int x = 14;

int y = 7 - 14 / 2;

return y * (28 / 14 + 2);

int x = 14;

int y = 0;

return y * 4;

Page 7: Compiler optimization

Embedded and Parallel Systems Lab

7

Stack height reduction

Rearrange expression tree to minimize resources needed for expression evaluation

ADD R6,R2,R3ADD R7,R6,R4ADD R8,R7,R5

ADD R6,R2,R3ADD R7,R4,R5ADD R8,R7,R6

I1

I2

I3

I1 I2

I3

R8=((R2+R3)+R4)+R5 R8=(R2+R3)+(R4+R5)

Page 8: Compiler optimization

Embedded and Parallel Systems Lab

8

Copy propagation

Replace all instances of a variable A that has been assigned X (i.e., A = X) with X

y = x ;

z = 3 + y; z = 3 + x

Page 9: Compiler optimization

Embedded and Parallel Systems Lab

9

Code motion

Remove code from a loop that computes same value each iteration of the loop

Loop-invariant code

while (j < maximum - 1) {

x=1;

j = j + 4 * a;

}

int maxval = maximum - 1;

int calcval = 4 * a;

x=1;

while (j < maxval) {

j = j + calcval;

}

Page 10: Compiler optimization

Embedded and Parallel Systems Lab

10

Induction variable elimination

Simplify / eliminate array addressing calculations within loops

Int i=0;

while( i<10){

i=i+1;

p = 4*i ;

do some things;

}

Int p=0;

while( p<40){

p=p+4;

do some things;

}

Page 11: Compiler optimization

Embedded and Parallel Systems Lab

11

Strength reduction

Such as , replace multiply by constant with adds and shifts

for (i=0 ; i<n ; i++){

z = i * x;

do some things;

}

for (i=0 ; i<n ; i++){

z = z + x;

do some things;

}

Page 12: Compiler optimization

Embedded and Parallel Systems Lab

12

Branch offset optimization

Choose the shortest branch displacement that reaches target

if( a ){             statement 1} else {             goto L1;}statement 2L1:statement 3

if( !a ){                  goto L1;}statement 1statement 2L1:statement 3

Page 13: Compiler optimization

Embedded and Parallel Systems Lab

13

dead (unreachable) code elimination

Remove instructions that will not affect the behavior of the program.

Int func( int a){

int b, c;

b=a*2;

c=a*3;

return b;

}

Int func( int a){

int b, c;

b=a*2;

return b;

}

Page 14: Compiler optimization

Embedded and Parallel Systems Lab

14

Boolean Expression

If( a=0 || b=0 || c=0) {

 statement 1;

}

If( a=0) {

 statement 1;

}else if( b=0){

 statement 1;

}else if( c=0){

 statement 1;

}

Page 15: Compiler optimization

Embedded and Parallel Systems Lab

15

Loop unrolling

int i;

for (i = 0; i < 5; i++){

function(i);

}

function(0);

function(1);

function(2);

function(3);

function(4);

Duplicates the body of the loop multiple times, in order to decrease the number of times the loop condition is tested and the number of jumps, which hurt performance by impairing the instruction pipeline

Page 16: Compiler optimization

Embedded and Parallel Systems Lab

16

Thanks