GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Allocate registers from overlapping live ranges

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Compute values live after each instruction and peak interference under no-spill fixed-order constraints. Distinct code and terminal live-out assumptions preserved; code-motion task excluded.

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Minimum registers without code motion

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

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Minimum registers without spilling

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