Code Optimization

Code Optimization phase in mainly use to optimize the code for better utilization of memory and reduce the time taken for execution. Code optimization takes input from intermediate code generator and performs machine independent optimization. Code optimizer may also take input from code generator and perform machine dependent code optimization. Compilers that use code optimization transformations are called as optimizing compilers. Code optimization does not consider target machine properties for optimization (like register allocation and memory management) if input is from intermediate code generator.

Code optimization tries to optimize that part of the code which are executed more number of times, like statements within flow control block of for statement and while statement. This is because the most programs always spend maximum execution time on executing only few statements Code
optimization analysis programs in two levels control flow analysis and data flow analysis. In control flow analysis code optimization concentrates more on improving the code of inner loops than outer statements, as inner loops are executed more number of times than outer ones. A detailed data flow
analysis is required for debugging the optimized code. Data flow analysis collects the information of statistics about statements being executed more number of times. This information is used in the process if optimization. Code optimization should be such that best results crop up with minimum
effort.

Code Optimization has to mainly achieve two goals
1. Preserve the meaning of code – The output generated before (without) Code Optimization should be same as the code after optimization.
2. Optimization should reduce the cost of execution considerably. The effort spent on code optimization should be worth it.

It implies that amount of time taken for optimization should be very less when compared to the reduction of overall execution time. Generally, a fast non optimizing compilers are preferred for debugging programs

Code improvement always need not be in code optimization phase. It can be incorporated in source program or in intermediate code or on target code. In source program say, for sorting program, user can choose different algorithm based on the cost function like minimum space or minimum time. Each algorithm can be efficient it its own way or other, like quick sort is very fast on unsorted/random array where as other sorting like bubble sort is efficient on partially sorted array. Intermediate code can be improved by improving loops and efficient address calculation may give better results. In final code generation phase, optimized code can be efficiently generated by selecting appropriate instruction, use registers efficiently and some instruction transformations. Example: Keeping most
used variables in registers which avoids frequent fetching and storing in memory location. This chapter deals with optimization of intermediate code represented as three address code. Intermediate code is relatively independent at target machine so optimization is machine independent.


Programs are represented as flow graphs to study control flow and temporary variables are used to store intermediate results help in data flow analysis. It is seen that compilation speed is proportional to the size at program being compiled hence amount of time taken for code optimization should be relatively less.

0 comments