Code Optimization Techniques
Code Optimization Techniques
Compiler phases
Front end (3 phases) and back end
constant folding – technique of evaluating an expression whose operands are known to be constants at compile time
itself
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
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