Similar questions 1 more →
Code Generation & Register Allocation


Answer & solution
Official answer: B
Retain the printed statement order. Immediately after e=c+a and before x=c*c, a is live for the comparison and true branch, c is live for x, and d, e are live for their possible false-branch squarings. Thus {a, c, d, e} is a four-value live set, giving a lower bound of four registers. Four also suffice: R1 holds a throughout the straight-line part; R2 starts as b, is overwritten by c=a+b, then is overwritten by x=c*c once c has had its last use; R3 holds d=c*a and R4 holds e=c+a. The comparison uses R2 and R1. On the true branch, y=a*a can overwrite R1. On the false branch, d² and e² overwrite R3 and R4 respectively. No extra destination register is needed because destinations may alias source registers. Hence the exact minimum is four, option B.
Liveness asks whether a computed value will still be needed on some future path. Before x is computed, a is required for comparison, c for the square, and d/e for the possible false branch. Even though only one branch eventually executes, register allocation cannot discard d/e before the condition is known under the fixed order. Four live values therefore impose four registers. At the next step c has its final use, so x can overwrite its register and no fifth register is needed.