Compiler Design
Introduction to Code Optimization
Introduction to Code Optimization
Code Optimization is the phase in a compiler that attempts to improve the intermediate code so that the final machine code will run faster or take up less space (or both), without changing the program's output or meaning.
- Execution Speed: Make the code run faster (e.g., by moving code out of loops).
- Code Size: Reduce the memory footprint of the program, which is crucial for embedded systems.
- Energy Efficiency: Fewer instructions generally lead to lower power consumption.
- Preserve Semantics: The optimized code MUST produce the exact same output and side effects as the original code for all valid inputs.
- Profitability: The optimization should noticeably improve performance on average.
- Compile-time Efficiency: The optimization process itself should not take an unreasonable amount of time during compilation.
Scope of Optimization
| Optimization Type | Scope | Examples |
|---|---|---|
| Local Optimization | Within a single Basic Block. | DAG construction, Peephole optimization. |
| Global Optimization | Across basic blocks within a single function. | Live variable analysis, Global common subexpression elimination. |
| Inter-procedural | Across multiple functions / procedures. | Function inlining, Inter-procedural constant propagation. |