GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 49

Minimum registers without code motion

Compiler Design · Code Generation & Register Allocation · Full GATE CSE 2013 paper

GATE 2013, Q49 2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 49, GATE 2013: For the same code c=a+b;d=c*a;e=c+a;x=c*c;if(x>a)y=a*a;else{d=d*d;e=e*e;}, all variables dead afterward. With original statement order and only register-allocation optimization, how many registers avoid spills? A3, B4, C5, D6.
Question 49, GATE 2013: For the same code c=a+b;d=c*a;e=c+a;x=c*c;if(x>a)y=a*a;else{d=d*d;e=e*e;}, all variables dead afterward. With original statement order and only register-allocation optimization, how many registers avoid spills? A3, B4, C5, D6.
Tap to enlarge
Your answer
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.

Original paper, page 11 · Answer key