Loop-Invariant Computations and Code Motion

Induction Variable Detection and Elimination

Strength Reduction

Loop Unrolling

Loop Jamming (Fusion)

Counting up to Zero

Unswitching

Loop Collapse

10.2.5 Loop Jamming (Fusion)

Sometimes two loops may be replaced by one. Consider the following loops:

    LOOP I = 1 to 100


      A(I) := 0

    ENDLOOP
    LOOP I := 1 to 100

      B(I) = X(I) + Y

    ENDLOOP

These two loops can be "fused":

    LOOP I = 1 to 100


      A(I) = 0
      B(I) = X(I) + Y

    ENDLOOP

The loop overhead is reduced, resulting in a speed-up in execution, as well as a reduction in code space. Once again, instructions are exposed for parallel execution.

The conditions for performing this optimization are that the loop indices be the same, and the computations in one loop cannot depend on the computations in the other loop.

Sometimes loops may be fused when one loop index is a subset of the other:

    FOR I := 1 TO10000 DO


      A[I] := 0

    FOR J := 1 TO 5000 DO


      B[J] := 0

      FOR I := 1 to 10000 DO


        IF I <= 5000 THEN B[I] := 0
        A[I] := 0

Here, a test has been added to compute the second loop within the first. The loop must execute many times for this optimization to be worthwhile.