GATE / CS ARCHIVE

GATE CSE 2010 / QUESTION 37

Minimum registers without spilling

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

GATE 2010, Q37 2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 37, GATE 2010: All operations take operands from registers. What is the minimum number of registers needed to execute this program without spilling?
a=1; b=10; c=20;
d=a+b; e=c+d; f=c+e;
b=c+e; e=b+f; d=5+e;
return d+f;
Options: (A) 2; (B) 3; (C) 4; (D) 6.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Immediately after c=20, a,b,c are all live because a and b are needed for d=a+b while c is needed later. At least three registers are therefore necessary. Three suffice: put a,b,c in registers r1,r2,r3; overwrite r1 with d=a+b, then with e=c+d; overwrite r2 with f=c+e; overwrite r3 with b=c+e after c dies; overwrite r1 with e=b+f and then d=5+e. The final d+f uses r1 and r2. Thus 3, B, under the usual model allowing a destination to reuse a dead operand register.

One possible register state trace is (a,b,c)→(d,b,c)→(e,b,c)→(e,f,c)→(e,f,b)→(e,f,b)→(d,f,b), where dead values may be overwritten. At the middle expression f=c+e, both c and e must remain available for the following b=c+e, so the third register is still useful. The lower-bound argument assumes executing the given operations, rather than constant-folding the entire straight-line program.

Original paper, page 9