CD Unit-5
CD Unit-5
Code Optimization
• Optimization is a program transformation technique, which tries to improve the
code by making it consume less resources (i.e. CPU, Memory) and deliver high
speed.
• In optimization, high-level general programming constructs are replaced by very
efficient low-level programming codes. A code optimizing process must follow the
three rules given below:
• 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.
• Optimization can be categorized broadly into two types : machine independent and
machine dependent.
Machine-Independent Optimization
• (a) z = 5*(45.0/5.0)*r
Perform 5*(45.0/5.0)*r at compile time.
• (b) x = 5.7
y = x/3.6
Evaluate x/3.6 as 5.7/3.6 at compile time.
(2) Variable Propagation:
• Step 1:
• If y operand is undefined then create node(y).
• If z operand is undefined then for case(i) create node(z).
• Step 2:
• For case(i), create node(OP) whose right child is node(z) and left
child is node(y).
• For case(ii), check whether there is node(OP) with one child
node(y).
• For case(iii), node n will be node(y).
• Output:
• For node(x) delete x from the list of identifiers. Append x to
attached identifiers list for the node n found in step 2. Finally set
node(x) to n.
Example: