Loop Optimizations: Choosing the Correct Answer (Loop Rolling vs. Loop Jamming)
To answer the question, we need the meaning of the relevant compiler optimization terms.
The method that merges the bodies of two loops is called loop jamming. In contrast:
- loop rolling typically restructures iteration counts/overhead rather than directly combining two loop bodies.
- constant folding is unrelated because it operates on constant expressions, not loop bodies.
Therefore, the correct option is (ii) loop jamming.
References for the above definitions are included in the footnotes at the end. 2
Footnotes
-
Loop jamming - explanation - Describes the loop optimization that combines loops for better performance. ↩
-
Constant folding - explanation - Defines constant folding as compile-time evaluation of constant expressions, not loop-body merging. ↩
Loop Jamming / Loop Transformations (Compiler Optimizations) — Learning Resource
Key terms you must distinguish
- loop jamming
- loop rolling
- constant folding
- loop transformation
Use these to map the question’s phrase “merges the bodies of two loops” to the correct optimization.
How to pick the correct option in loop-optimization MCQs
- 1Step 1
“Merges the bodies of two loops” directly corresponds to loop jamming.
- 2Step 2
If an option mentions expression simplification (e.g., constant folding), eliminate it because it does not transform loop bodies.
- 3Step 3
loop rolling focuses on iteration restructuring rather than combining two loop bodies into one.
MCQ shortcut
When the question says “merge/combine two loops,” look for a term explicitly describing “combining loops” (i.e., loop jamming), not expression optimizations like constant folding.
Common confusion
Students sometimes confuse loop-related transformations. But “constant folding” is not about loops; it’s about compile-time evaluation of constant expressions. constant folding will never be the answer to “merge two loops.”
Where this fits in compiler optimization
Identify optimization opportunities
Early analysisDetect patterns such as adjacent loops that could be combined."
Apply loop-specific rewrites
Loop transformation phaseUse loop jamming to merge loop bodies."
Apply non-loop optimizations
Expression simplificationUse constant folding where applicable."
Final answer
The correct answer is: (ii) loop jamming. 2
Footnotes
-
Loop jamming - explanation - Describes the loop optimization that combines loops for better performance. ↩
-
Constant folding - explanation - Defines constant folding as compile-time evaluation of constant expressions, not loop-body merging. ↩
Knowledge Check
The method which merges the bodies of two loops is