Compiler Optimization Techniques PART -II

6. Loop Invariant Code Motion

Here, the part of code that remains unchanged by the iterations of its immediate loops, is moved out of the loop.
By doing so, the redundant computation is eliminated.

Ex:
int main ( void )
{
int i;
int a, b, c;

. . . .
for ( i = 0; i < x; i++ )
{
a[i] = b[i] + c[i];
z = y[x]; /* this statement is redundant here as it is independant of any computations within the loop */
}
. . . .

return i;
}

This can be simplified as follows :

int main ( void )
{
int i;
int a, b, c;

. . . .
for ( i = 0; i < x; i++ )
{
a[i] = b[i] + c[i];
}
z = y[x]; /* by doing this the computation time is reduced */
. . . .

return i;
}

7. Loop Unrolling

Here, the objective is to reduce the number of iterations in the body of the loop. This is done by
combining multiple iterations of the loop into a single iteration. This helps to speed up the execution.

Ex:
int main ( void )
{
int i;

. . . .
for ( i = 0; i < 100; i++ )
{
printf ( "%d", i );
}
. . . .

return i;
}

This code is modified as :

int main ( void )
{
int i;

. . . .
for ( i = 0; i < 100; i += 5 ) /* By doing so, number of iterations for comparing the value of 'i' with 100 is reduced */
{
printf ( "%d", i );
printf ( "%d", i + 1 );
printf ( "%d", i + 2 );
printf ( "%d", i + 3);
printf ( "%d", i + 4 );
}
. . . .

return i;
}

8. Dead Code Elimination

Certain part of the code may not affect the program, or they might not even be reachable.
These are called dead code. These codes are eliminated by the compiler.

Ex :
int main ( void )
{
int i;

i = 10; /* this part of code does not affect the program */
i = 20;
if ( 0 )
{
i = 30 /* this part of code is never reached */
}

return i;
}

The code is then modified as follows :
int main ( void )
{
int i;

i = 20;

return i;

}

Your feedback helps to make the article better.

--
Pradeep
ckpradip@yahoo.com
Moderator: c4swimmers@yahoogroups.com