Loop optimization

Invariant code: The code which reside in a loop and computes same value at each iteration is known as invariant code. This code can be moved out of the loop to compute once, other than at each iteration, to reduce the computation time.

Induction analysis: A variable is called an induction variable if its value gets changed within the loop by a loop-invariant value.

Strength reduction: It means reducing the consumption of CPU cycles, time and memory, by replacing the strength consuming expressions with less or cheaper expressions without compromising the output of the expression.

Unoptimized code

repeat
S1;
i := j;
s2 
until p;

Optimized code

i := j;
repeat
Sl;
s2
until p;