1 Code Generation Code generator phase ... - VTU e-Learning
1 Code Generation Code generator phase ... - VTU e-Learning
1 Code Generation Code generator phase ... - VTU e-Learning
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Code</strong> Optimization has to mainly achieve two goals<br />
1. Preserve the meaning of code – The output generated before (without) <strong>Code</strong> Optimization<br />
should be same as the code after optimization.<br />
2. Optimization should reduce the cost of execution considerably. The effort spent on code<br />
optimization should be worth it.<br />
It implies that amount of time taken for optimization should be very less when compared to the<br />
reduction of overall execution time. Generally, a fast non optimizing compilers are preferred for<br />
debugging programs<br />
<strong>Code</strong> improvement always need not be in code optimization <strong>phase</strong>. It can be incorporated in source<br />
program or in intermediate code or on target code. In source program say, for sorting program, user<br />
can choose different algorithm based on the cost function like minimum space or minimum time.<br />
Each algorithm can be efficient it its own way or other, like quick sort is very fast on unsorted/random<br />
array where as other sorting like bubble sort is efficient on partially sorted array. Intermediate code<br />
can be improved by improving loops and efficient address calculation may give better results. In final<br />
code generation <strong>phase</strong>, optimized code can be efficiently generated by selecting appropriate<br />
instruction, use registers efficiently and some instruction transformations. Example: Keeping most<br />
used variables in registers which avoids frequent fetching and storing in memory location. This<br />
chapter deals with optimization of intermediate code represented as three address code. Intermediate<br />
code is relatively independent at target machine so optimization is machine independent.<br />
Programs are represented as flow graphs to study control flow and temporary variables are used to<br />
store intermediate results help in data flow analysis. It is seen that compilation speed is proportional to<br />
the size at program being compiled hence amount of time taken for code optimization should be<br />
relatively less.<br />
Principal of code optimizations<br />
This sections deals with identifying that part of the program where optimization is required. By using<br />
the concept of proper register allocation, elimination of dead code and finding the cost of instruction,<br />
it is possible to improve the efficiency of program statements.<br />
Unnecessary Operation<br />
In a program there may be some part of code which never executes. It would be waste to generate<br />
code for these statements. It may also happen that some of the values of temporary variables may<br />
never be used. These are called as dead codes, it has to be removed. There can be some subexpression<br />
whose value is computed many times. This can be optimized by calculating the value of<br />
sub-expression only once and other statements can just use this value.<br />
Example:<br />
x = 1<br />
while (x != 1)<br />
{ …}<br />
Statements of while never executed hence do not generate code for statements within while statement.<br />
Example:<br />
9