Presentation is loading. Please wait.

Presentation is loading. Please wait.

Optimization Compiler Baojian Hua

Similar presentations


Presentation on theme: "Optimization Compiler Baojian Hua"— Presentation transcript:

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

2 Middle End AST translation IR1 asm other IR and translation translation IR2

3 Optimizations AST translation IR1 asm other IR and translation translation IR2 opt

4 Optimization in General

5 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 …

6 Optimization in practice Optimization is a code rewriting (transformation) 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 OSes, with particular cache size, with particular processes running, … )

7 Full Optimization is impossible Could solve the halting problem: Compare Opt(p) with: L: jmp L This is the famous fully employment theorem for compiler writers.

8 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

9 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

10 Today ’ s topics Early optimizations local, flow-insensitive so don ’ t require analysis (almost) constant folding, algebraic simplifications, dead- code eliminations, etc. Flow-sensitive optimization based on the result of (data-flow) analysis constant propagation, copy propagation, common- subexpression elimination (CSE), dead-code elimination, etc.

11 Constant Folding

12 Basic idea: calculate expressions known to be constants 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)

13 Moral Easy to implement, can be performed on AST or low-level IRs Often implemented as a common subroutine to be called whenever desired Must be very careful with languages semantics overflow or exceptions e.g.: 0xffffffff+1 ==> 0 (???)

14 Algebraic Simplification

15 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

16 Scalar Replacement of Aggregates

17 Scalar Replacement of Aggregates Replace structures/arrays with collections of scalars Expose the opportunity other further optimizations Especially register allocation

18 Example typedef 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; }

19 Inlining typedef 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; }

20 Replacement typedef 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; }

21 Constant Propagation typedef 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; }

22 Flow-sensitive Optimizations

23 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.

24 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 ”.

25 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; }

26 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 ” ?

27 Common Subexpression Elimination (CSE) z = t t = a+b x = t t = a+b k = t h = a+b t = h t is a fresh variable. Similar code rewriting for other predecessor blocks. Can we replace this “ z ” with “ x ” ?


Download ppt "Optimization Compiler Baojian Hua"

Similar presentations


Ads by Google