1 / 27

Optimization

Optimization. Compiler Baojian Hua bjhua@ustc.edu.cn. Middle End. translation. AST. IR1. translation. IR2. other IR and translation. asm. Optimizations. opt. opt. translation. AST. IR1. opt. translation. IR2. opt. opt. other IR and translation. asm. Optimization

melliott
Télécharger la présentation

Optimization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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. Constant Folding • 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 x = 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. … a = x Can we replace this “x” with constant “3”?

  24. Copy propagation x = 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”. … a = x Can we replace this “x” with the variable “y”?

  25. Dead code elimination 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. return 0 Must pay special attention toside effects. e.g.: void main (){ x = 1/0; }

  26. Common Subexpression Elimination (CSE) 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”. … z = a+b Can we replace the variable “z” with the variable “x”?

  27. Common Subexpression Elimination (CSE) 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. z = t Can we replace this “z” with “x”?

More Related