School of EECS, Peking University Advanced Compiler Techniques"(Fall 2011) Introduction to Optimizations Guo yao
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Introduction to Optimizations Guo, Yao
Outline Optimization rules Basic blocks Control Flow Graph(CFG) a Loops a Local Optimizations Peephole optimization Fa|2011 Advanced Compiler techniques
2 Fall 2011 “Advanced Compiler Techniques” Outline ◼ Optimization Rules ◼ Basic Blocks ◼ Control Flow Graph (CFG) ◼ Loops ◼ Local Optimizations ◼ Peephole optimization
Levels of Optimizations ■Loca a inside a basic block Global (intraprocedural) Across basic blocks Whole procedure analysis Interprocedural Across procedures Whole program analysis Fa|2011 Advanced Compiler techniques
3 Fall 2011 “Advanced Compiler Techniques” Levels of Optimizations ◼ Local ◼ inside a basic block ◼ Global (intraprocedural) ◼ Across basic blocks ◼ Whole procedure analysis ◼ Interprocedural ◼ Across procedures ◼ Whole program analysis
The Golden Rules of optimization Premature Optimization is Evil a Donald Knuth, premature optimization is the root of al∥evi Optimization can introduce new, subtle bugs Optimization usually makes code harder to understand and maintain Get your code right first, then, if really needed, optimize计t Document optimizations carefully a Keep the non-optimized version handy, or even as a comment in your code Fa|2011 Advanced Compiler techniques
4 Fall 2011 “Advanced Compiler Techniques” The Golden Rules of Optimization Premature Optimization is Evil ◼ Donald Knuth, premature optimization is the root of all evil ◼ Optimization can introduce new, subtle bugs ◼ Optimization usually makes code harder to understand and maintain ◼ Get your code right first, then, if really needed, optimize it ◼ Document optimizations carefully ◼ Keep the non-optimized version handy, or even as a comment in your code
The Golden Rules of optimization The 80/20 Rule In general, 80% percent of a program's execution time is spent executing 20% of the code 90°%/10°% for performance- hungry programs Spend your time optimizing the important 10/20% of your program Optimize the common case even at the cost of making the uncommon case slower Fa|2011 Advanced Compiler techniques
5 Fall 2011 “Advanced Compiler Techniques” The Golden Rules of Optimization The 80/20 Rule ◼ In general, 80% percent of a program’s execution time is spent executing 20% of the code ◼ 90%/10% for performance-hungry programs ◼ Spend your time optimizing the important 10/20% of your program ◼ Optimize the common case even at the cost of making the uncommon case slower