0% found this document useful (0 votes)
39 views9 pages

Code Optimization Techniques

Uploaded by

sai teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views9 pages

Code Optimization Techniques

Uploaded by

sai teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Code Optimization Techniques

To produce an efficient code

Two issues in code optimization is


1. Semantic equivalence of the source program must not be changed
2. It must be achieved without changing the algorithm of the program
Program becomes inefficient because of programmer or due to compiler transformation
Now where code optimization should be applied – before code generation or after code generation

Compiler phases
Front end (3 phases) and back end

Where the code optimization has to be applied is shown above

Different techniques for code optimization


we optimize the code at the compile time – two techniques -constant folding and constant propogation

constant folding – technique of evaluating an expression whose operands are known to be constants at compile time
itself

Above 22/7 is a constant expression

So first solve the constant expression before and keep the constant value – so called folding the constant and place
the value at compile time since evaluated at compile time and keep the value of reuse
Example – finding area of circle

So values of variables are directly substituted at the place of refrence or useage and executed at compile time itself
Above is the 3 address code

So 4*I is not executed second time for T4 and value of T1 is taken for T4

So no redundant expression in the optimized code


Replace an expression having more pritority with less priority expression

Loop Optimization Techniques


Code optimization can be done in loops
1. Code motion technique or code movement
We optimize the code in inner loop

2. Induction variable
Variable x is an induction variable of loop L such that
Value of the induction variable of loop get changed every time – either incremented or decremented

Above t1 depends on I and so t1 and I are locked and so called induction variables of the block
3. Loop invariant method
Computation inside the loop is avoided and thereby computation overhead on compiler is avoided
Ex:

4. Loop unrolling
Number of jumps and tests can be reduced by writing the code two times

Ex:
So we reduce the no of steps to some extent by repeating the operation in the body
5. Loop fusion or loop jamming
Several loops are merged to one loop

You might also like