Questions: Loop Invariant Code Motion (LICM)

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Inside a loop, the expression `result = numerator / divisor` appears inside an `if (divisor != 0)` check. Neither `numerator` nor `divisor` is modified in the loop. A compiler identifies this as loop-invariant. Should it hoist the division to the preheader?

AYes — since neither operand changes, the division computes the same value every iteration and is safe to move
BYes — hoisting loop-invariant expressions to the preheader is always a performance improvement with no correctness risk
CNo — hoisting moves the division outside the conditional, causing it to execute even when divisor is zero, which could fault on iterations where the original code would have skipped the division
DNo — division operations are never loop-invariant because they depend on hardware state
Question 2 Multiple Choice

A compiler wants to hoist `cost = base_rate * multiplier` from inside an `if (apply_rate)` block within a loop. Under what condition is this safe?

AWhen `base_rate` and `multiplier` are global variables visible across the entire program
BWhen the compiler can prove the expression executes on every iteration (dominates all loop exits) and has no observable side effects
CWhen the loop is guaranteed to run more than a fixed threshold number of iterations
DWhen `apply_rate` is set before the loop begins and never changes inside the loop
Question 3 True / False

Any expression inside a loop whose operands are not modified by the loop can generally be safely hoisted to the loop's preheader.

TTrue
FFalse
Question 4 True / False

LICM can only improve performance on loops that run at least twice; for a loop that always executes exactly once, hoisting an invariant expression to the preheader provides no speedup.

TTrue
FFalse
Question 5 Short Answer

Explain why hoisting a loop-invariant expression from inside a conditional branch to the loop's preheader can be unsafe, and what condition the compiler must verify before proceeding.

Think about your answer, then reveal below.