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.7 Unswitching

    LOOP FOR I := 1 TO 1000

      IF (Test) THEN


        X (I) := A(I) + B(I)

      ELSE

        X(I) := A(I) - B(I)

    ENDLOOP

Here, the test is performed each time through the loop. The following performs the test once:

    IF (Test) THEN


      LOOP FOR I := 1 to 1000

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

      ENDLOOP
      ELSE

        LOOP FOR I := 1 to 100

          X(I) = A(I) - B(I)

        ENDLOOP

The second version here reduces the number of instructions executed, but the code takes more space.