0
0
Compiler Designknowledge~20 mins

Loop optimization (invariant code motion) in Compiler Design - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding loop-invariant code motion

Which of the following best describes the purpose of loop-invariant code motion in compiler optimization?

ARemoving all loops from the program to simplify control flow.
BDuplicating loop body instructions to increase parallel execution.
CMoving calculations that do not change inside the loop to outside the loop to reduce repeated work.
DChanging the loop counter variable to a constant value.
Attempts:
2 left
💡 Hint

Think about what can be done to avoid repeating the same calculation multiple times inside a loop.

📋 Factual
intermediate
2:00remaining
Identifying loop-invariant statements

Consider the following loop:

for (int i = 0; i < n; i++) {
  int x = a + b;
  arr[i] = x * i;
}

Which statement is loop-invariant and can be moved outside the loop?

Aarr[i] = x * i;
Bint x = a + b;
Cint i = 0;
Di &lt; n
Attempts:
2 left
💡 Hint

Look for calculations that do not depend on the loop variable i.

🔍 Analysis
advanced
2:00remaining
Effect of loop-invariant code motion on performance

Given a loop that executes 1,000,000 times, which of the following changes will most likely improve performance by applying loop-invariant code motion?

for (int i = 0; i < 1000000; i++) {
  int y = 5 * 10;
  process(y, i);
}
AChange the loop variable <code>i</code> to a floating-point number.
BReplace <code>process(y, i);</code> with <code>process(50, i);</code> inside the loop.
CIncrease the loop count to 2,000,000 to utilize more CPU.
DMove <code>int y = 5 * 10;</code> outside the loop so it is computed once.
Attempts:
2 left
💡 Hint

Consider which calculation is repeated unnecessarily inside the loop.

Comparison
advanced
2:00remaining
Comparing loop-invariant code motion with other optimizations

Which of the following statements correctly compares loop-invariant code motion with loop unrolling?

ALoop-invariant code motion moves constant computations outside the loop; loop unrolling duplicates the loop body to reduce loop overhead.
BLoop-invariant code motion duplicates the loop body; loop unrolling moves computations outside the loop.
CBoth techniques remove loops entirely from the program.
DLoop unrolling moves invariant code outside the loop; loop-invariant code motion duplicates the loop body.
Attempts:
2 left
💡 Hint

Think about what each optimization tries to reduce: repeated calculations or loop control overhead.

Reasoning
expert
2:00remaining
Identifying safe loop-invariant code motion in nested loops

Consider the nested loops below:

for (int i = 0; i < n; i++) {
  for (int j = 0; j < m; j++) {
    int z = a + b;
    arr[i][j] = z * i * j;
  }
}

Which is the safest and most effective loop-invariant code motion to optimize this code?

AMove <code>int z = a + b;</code> outside both loops.
BMove <code>int z = a + b;</code> inside the inner loop only.
CMove <code>int z = a + b;</code> outside the outer loop only.
DDo not move <code>int z = a + b;</code> because it depends on <code>i</code> and <code>j</code>.
Attempts:
2 left
💡 Hint

Check if the expression depends on loop variables i or j.