Code Optimization
Code Optimization
The output code must not, in any way, change the meaning of the program.
Optimization should increase the speed of the program and if possible, the program should demand less number
of resources.
Optimization should itself be fast and should not delay the overall compiling process.
Efforts for an optimized code can be made at various levels of compiling the
process.
At the beginning, users can change/rearrange the code or use better algorithms to write the code.
After generating intermediate code, the compiler can modify the intermediate code by address calculations and
improving loops.
While producing the target machine code, the compiler can make use of memory hierarchy and CPU registers.
Machine-independent Optimization
In this optimization, the compiler takes in the intermediate code and
transforms a part of the code that does not involve any CPU registers
and/or absolute memory locations. For example:
do
{
item = 10;
value = value + item;
} while(value<100);
Item = 10;
do
{
value = value + item;
} while(value<100);
should not only save the CPU cycles, but can be used on any processor.
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 memory hierarchy.