Explain briefly any three of the commonly used code optimization techniques.
1. Common sub expression elimination:
In given expression as "(a+b)-(a+b)/4", in such "common sub-expression" termed to the duplicated "(a+b)". Compilers implementing such technique realize that "(a+b)" would not modify, and by itself, only compute its value once and use similar value next time.
2. Dead code Elimination:
Code which is unreachable or which does not influence the program (for example dead stores) can be removed. In the illustration below, the value assigned to i is never utilized, and the dead store can be removed. The first assignment to global is dead, and the third assignment to global is not reachable; both can be removed.
int global; void f (){ int i;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable */}
Below is the code fragment after dead code elimination.
int global;
void f (){ global = 2; return;}
3. Loop-invariant code motion
If a quantity is calculated inside a loop during iteration and its value is similar for iteration, this can vastly improve efficiency to hoist this outside the loop and calculate its value just once before the loop begins. It is particularly significant with the address-calculation expressions generated through loops over arrays. For accurate implementation, such technique must be utilized with loop inversion, since not all code is safe to be hoisted outside the loop.
For illustration:
for (i=1; i≤10, i++){
x=y*2-(1)
.
.
.
}
statement 1 can be hoisted outside the loop assuming value of x and y does not modify in the loop.