optimization compiler baojian hua [email protected]

27
Optimization Compiler Baojian Hua [email protected]

Upload: verity-osborne

Post on 15-Jan-2016

242 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimization

CompilerBaojian Hua

[email protected]

Page 2: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Middle End

AST translation IR1

asmother IR

and translation

translation IR2

Page 3: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimizations

AST translation IR1

asmother IR

and translation

translation IR2

opt

optopt

opt

opt

Page 4: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimizationin General

Page 5: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimization Optimization is a (semantics

preserving) code rewriting (transformation) such that the code after the transformation: is smaller is faster use less memory is more power efficient …

Page 6: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimization in practice Optimization is a code rewriting (transform

ation) such that the code after the transformation: is smaller is faster use less memory is more power efficient …(for some inputs, on some machines, on some OS

es, with particular cache size, with particular processes running, …)

Page 7: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Full Optimization is impossible

Could solve the halting problem: Compare Opt(p) with:

L: jmp L This is the famous fully employment t

heorem for compiler writers.

Page 8: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Optimization is difficult

No optimization always produces “better” result

Usually undecidable Correctness can be very subtle

A lot of research, in recent years, try to formally verify the correctness of optimizations

Page 9: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Caveat

Try to do good things most of the time not all programs are equally likely to be

seen, right? invent optimizations which work for most of

them for most of time

Don’t expect a perfect compiler if a compiler know enough optimization

tricks, we deem it mature

Page 10: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Today’s topics Early optimizations

local, flow-insensitive so don’t require analysis (almost) constant folding, algebraic simplifications, dea

d-code eliminations, etc. Flow-sensitive optimization

based on the result of (data-flow) analysis constant propagation, copy propagation, comm

on-subexpression elimination (CSE), dead-code elimination, etc.

Page 11: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Constant Folding

Page 12: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Constant Folding

Basic idea: calculate expressions known to be const

ants at compile-time e.g.: a = 3 + 5 ==> a = 8 e.g.: if (true && false) … ==> if (false)

Easy on integers or booleans, also possible on floats (but complicated)

Page 13: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Moral Easy to implement, can be performed on AS

T or low-level IRs Often implemented as a common subroutin

e to be called whenever desired Must be very careful with languages semant

ics overflow or exceptions

e.g.: 0xffffffff+1 ==> 0 (???)

Page 14: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Algebraic Simplification

Page 15: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Constant Folding Basic idea:

Make use of algebraic properties to simplify expressions

e.g.: a = 0+b ==> a = b e.g.: a = 1*b ==> a = b e.g.: 2*a ==> a + a e.g.: 2*a ==> a<<1 (strength reduction) e.g.: *(&a) ==> a

Must take care with overflow and exceptions e.g.: (i-j) + (i-j) ==> i+ i -j -j

Page 16: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Scalar Replacementof Aggregates

Page 17: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Scalar Replacement of Aggregates

Replace structures/arrays with collections of scalars

Expose the opportunity other further optimizations Especially register allocation

Page 18: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Exampletypedef struct{ int x; int y;}point;

void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}

int main ()

{

point p;

p.x = 1;

p.y = 2;

print (&p);

return 0;

}

Page 19: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Inliningtypedef struct{

int x;

int y;

}point;

void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}

int main ()

{

point p;

p.x = 1;

p.y = 2;

printf (“(%d, %d)”,

p.x, p.y);

return 0;

}

Page 20: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Replacementtypedef struct{

int x;

int y;

}point;

void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}

int main ()

{

int p_x;

int p_y;

p_x = 1;

p_y = 2;

printf (“(%d, %d)”,

p_x, p_y);

return 0;

}

Page 21: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Constant Propagationtypedef struct{

int x;

int y;

}point;

void print (point *p){ printf (“(%d, %d)”, p->x, p->y); return;}

int main ()

{

int p_x;

int p_y;

p_x = 1;

p_y = 2;

printf (“(%d, %d)”,

1, 2);

return 0;

}

Page 22: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Flow-sensitive Optimizations

Page 23: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Constant propagation

a = x

x = 3…

Can we replace this “x” with constant “3”?

Do reaching definition analysis, if the definition “x=3” is the only definition of “x” that can reach the assignment “a=z”, then the “x” in “a=x” can be replaced by 3.

Page 24: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Copy propagation

a = x

x = y…

Can we replace this “x” with the variable “y”?

Do reaching definition analysis, if the definition “x=y” is the only definition of “x” that can reach the assignment “a=x”, then the “x” in the assignment “a=x” can be replaced by “y”.

Page 25: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Dead code elimination

return 0

x = v…

…Can we eliminate this assignment?

Do liveness analysis, one can eliminate the assignment “x=v”, if “x” does not live out of this assignment.

Must pay special attention toside effects. e.g.:

void main (){

x = 1/0;

}

Page 26: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Common Subexpression Elimination (CSE)

z = a+b

x = a+b…

…Use available expressions analysis to determine whether or not the expression “a+b” is available at definition site of “z”.

Use reaching expression analysis to locate calculation sites of “a+b”.

Can we replace the variable “z” with the variable “x”?

Page 27: Optimization Compiler Baojian Hua bjhua@ustc.edu.cn

Common Subexpression Elimination (CSE)

z = t

t = a+b

x = tt = a+b

k = t h = a+b

t = ht is a fresh variable.

Similar code rewriting for other predecessor blocks.

Can we replace this “z” with “x”?