8.1.4 Loop Optimizations

Next: 8.1.5 Loop Reversing Up: 8.1 Optimizations Previous: 8.1.3 Copy-Propagation Contents Index

##
8.1.4 Loop Optimizations

Two types of loop optimizations are done by SDCC loop invariant lifting and strength reduction of loop induction variables. In addition to the strength reduction the optimizer marks the induction variables and the register allocator tries to keep the induction variables in registers for the duration of the loop. Because of this preference of the register allocator, loop induction optimization causes an increase in register pressure, which may cause unwanted spilling of other temporary variables into the stack / data space. The compiler will generate a warning message when it is forced to allocate extra space either on the stack or data space. If this extra space allocation is undesirable then induction optimization can be eliminated either for the entire source file (with --noinduction option) or for a given function only using #pragma noinduction.
Loop Invariant:

> `for (i = 0 ; i < 100 ; i ++)`
> `f += k + l;`

changed to

> `itemp = k + l;`
> `for (i = 0; i < 100; i++)`
> `f += itemp;`

As mentioned previously some loop invariants are not as apparent, all static address computations are also moved out of the loop.
Strength Reduction, this optimization substitutes an expression by a cheaper expression:

> `for (i=0;i < 100; i++)`
> `ar[i*5] = i*3;`

changed to

> `itemp1 = 0;`
> `itemp2 = 0;`
> `for (i=0;i< 100;i++) {`
> `ar[itemp1] = itemp2;`
> `itemp1 += 5;`
> `itemp2 += 3;`
> `}`

The more expensive multiplication is changed to a less expensive addition.

Next: 8.1.5 Loop Reversing Up: 8.1 Optimizations Previous: 8.1.3 Copy-Propagation Contents Index