Compilers

What is the main effect of loop unrolling?

  1. AFewer counter checks and branches, and easier reordering
  2. BThe loop body is reduced to one copy, making the program's code smaller
  3. CThe loop is extracted into a function that can be called from elsewhere
Show answer and explanation

Answer: A. Fewer counter checks and branches, and easier reordering

Loop unrolling writes, say, four copies of the loop body and runs a quarter as many iterations. Counter checks and branches shrink, and instructions from the copies can be interleaved, making it easier to hide operation latencies. Code grows in exchange and can strain the instruction cache. A single copy with smaller code is the reverse, and extracting a loop into a function is a different transformation.

  • A. Correct. Branch overhead drops and instructions become easier to reorder.
  • B. Multiple copies of the body make code larger, not smaller.
  • C. Extracting into a function is a different transformation.
Practice this topic Back to topic

More Compilers questions

  1. In the loop above, which computation is a candidate for loop-invariant code motion (hoisting out of the loop)? (Assume x and y are not modified in the loop.)
  2. In the loop above, which transformation applies strength reduction to the index computation i * 4?
  3. Why is the loop above hard to vectorize with SIMD instructions?
  4. In the function above, why can the compiler not simply hoist the load of *b out of the loop?
  5. In register allocation by coloring an interference graph, what do the nodes and edges represent?