0
0
Compiler Designknowledge~10 mins

Loop optimization (invariant code motion) in Compiler Design - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loop optimization (invariant code motion)
Start Loop
Identify Invariant Code
Move Invariant Code Outside Loop
Execute Loop Body Without Invariant Code
Loop End
Repeat or Exit
The process finds code inside a loop that does not change and moves it outside to run once, improving efficiency.
Execution Sample
Compiler Design
for i in range(3):
    x = 5 * 2
    print(i + x)
This loop calculates 5*2 inside each iteration, but since 5*2 is always 10, it can be moved outside the loop.
Analysis Table
IterationiExpression '5 * 2' Computed?Value of xActionOutput
10Yes10Calculate x inside loop0 + 10 = 10
21Yes10Calculate x inside loop1 + 10 = 11
32Yes10Calculate x inside loop2 + 10 = 12
Optimization: Move 'x = 5 * 2' outside loop
10No (moved outside)10Use precomputed x0 + 10 = 10
21No (moved outside)10Use precomputed x1 + 10 = 11
32No (moved outside)10Use precomputed x2 + 10 = 12
Loop ends after 3 iterations
💡 Loop ends after 3 iterations as i reaches 3, condition i < 3 is False
State Tracker
VariableStartAfter 1After 2After 3Final
iundefined0123 (loop exit)
xundefined10101010 (constant after optimization)
Key Insights - 3 Insights
Why can 'x = 5 * 2' be moved outside the loop?
Because '5 * 2' always equals 10 and does not depend on the loop variable 'i', so computing it once before the loop saves repeated work (see execution_table rows 1-3 vs 5-7).
Does moving invariant code outside the loop change the output?
No, the output remains the same because the value of 'x' is constant and used in the same way inside the loop (compare outputs in execution_table rows 1-3 and 5-7).
What happens if the code depends on the loop variable?
If the code uses the loop variable or changes each iteration, it cannot be moved outside because its value changes every time (not invariant).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at iteration 2 before optimization, is '5 * 2' computed inside the loop?
ANo, it is computed only once
BYes, it is computed every iteration
CIt depends on the value of i
DIt is never computed
💡 Hint
Check execution_table row 2 under 'Expression 5 * 2 Computed?' column
At which iteration does the loop stop executing according to the execution_table?
AAfter iteration 2
BAfter iteration 4
CAfter iteration 3
DIt never stops
💡 Hint
Look at the exit_note and the 'i' variable values in variable_tracker
If 'x = 5 * i' instead of 'x = 5 * 2', can 'x' be moved outside the loop?
ANo, because x depends on i which changes each iteration
BYes, because multiplication is fast
CYes, but only for the first iteration
DNo, because 5 * 2 is different
💡 Hint
Refer to key_moments about dependency on loop variable
Concept Snapshot
Loop optimization (invariant code motion):
- Find code inside loops that does not change each iteration.
- Move that code outside the loop to run once.
- This reduces repeated work and speeds up execution.
- Only safe if code does not depend on loop variables.
- Output remains the same after optimization.
Full Transcript
Loop optimization by invariant code motion means moving calculations or code inside a loop that do not change during the loop outside it. For example, in a loop running three times, if a calculation like 5 times 2 is done inside each iteration, it can be moved before the loop to run once. This saves time because the calculation is constant. The execution table shows that before optimization, the expression is computed every iteration, but after optimization, it is computed once and reused. Variables like 'i' change each iteration, but 'x' remains constant after moving outside. This optimization does not change the output but makes the loop faster. If the code depends on the loop variable, it cannot be moved outside safely.