Loop Optimization
Loop Optimization
After optimization:
t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}
If the value of any variable in any loop gets changed every time, then such
a variable is known as an induction variable. With each iteration, its value
either gets incremented or decremented by some constant value.
Example:
Before optimization:
B1
i:= i+1
x:= 3*i
y:= a[x]
if y< 15, goto B2
In the above example, i and x are locked, if i is incremented by 1 then x is
incremented by 3. So, i and x are induction variables.
After optimization:
B1
i:= i+1
x:= x+4
y:= a[x]
if y< 15, goto B2
3. Strength Reduction
After optimization:
s = x/y;
for (int i=0; i<10;i++)
t= i+ s;
...
end;
5. Loop Unrolling
After optimization:
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
6. Loop Jamming
After optimization: