The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result. Compiler optimizing process should meet the following objectives :
- The optimization must be correct, it must not, in any way, change the meaning of the program.
- Optimization should increase the speed and performance of the program.
- The compilation time must be kept reasonable.
- The optimization process should not delay the overall compiling process.
When to Optimize?
Optimization of the code is often performed at the end of the development stage since it reduces readability and adds code that is used to increase the performance.
Types of Code Optimization –The optimization process can be broadly classified into two types :
- Machine Independent Optimization – This code optimization phase attempts to improve the intermediate code to get a better target code as the output. The part of the intermediate code which is transformed here does not involve any CPU registers or absolute memory locations.
- Machine Dependent Optimization – Machine-dependent optimization is done after the target code has been generated and when the code is transformed according to the target machine architecture. It involves CPU registers and may have absolute memory references rather than relative references. Machine-dependent optimizers put efforts to take maximum advantage of the memory hierarchy.
Compile Time Evaluation :
Variable Propagation :
Hence, after variable propagation, a*b and x*b will be identified as common sub-expression.
Dead code elimination : Variable propagation often leads to making assignment statement into dead code
Code Motion :
• Reduce the evaluation frequency of expression.
• Bring loop invariant statements out of the loop.