Loop unrolling is a code optimization technique:

2015

Loop unrolling is a code optimization technique:

  1. A.

    that avoids tests at every iteration of the loop

  2. B.

    that improves preformance by decreasing the number of instructions in a basic block

  3. C.

    that exchanges inner loops with outer loops

  4. D.

    that reorders operations to allow multiple computations to happen in parallel

Attempted by 111 students.

Show answer & explanation

Correct answer: A

Correct definition: Loop unrolling duplicates the loop body multiple times so the loop-control test (the branch) is executed less frequently. This reduces loop-test overhead and can improve performance.

  • How it works: duplicate the loop body N times, adjust the loop step by N, and add a short epilogue to handle remaining iterations.

  • Benefits: fewer branch/tests, reduced branch misprediction cost, and potential for better instruction-level parallelism.

  • Trade-offs: increases code size, can increase register pressure, and requires handling remainder iterations.

Example:

  1. Before: for (i = 0; i < n; i++) sum += a[i]; — loop test each iteration.

  2. After unrolling by 4: for (i = 0; i < n; i += 4) { sum += a[i]; sum += a[i+1]; sum += a[i+2]; sum += a[i+3]; } then handle remaining n mod 4 iterations.

Why the other descriptions are incorrect:

  • The description about decreasing the number of instructions in a basic block is not loop unrolling; unrolling normally increases instructions per block. That description is closer to peephole or local elimination optimizations.

  • Exchanging inner and outer loops refers to loop interchange, used to improve cache behavior, not unrolling.

  • Reordering operations to allow parallel computations describes instruction scheduling or vectorization, not loop unrolling.

Explore the full course: Mppsc Assistant Professor