Skip to content
Official PYQ · 2010

GATE CS 2010 Q37 — Runtime Environments

Explain with AI

The program below uses six temporary variables a,b,c,d,e,fa, b, c, d, e, f.

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 

Assuming that all operations take their operands from registers, what is the minimum number of registers needed to execute this program without spilling?

Choose one option
View worked solution

Answer: B

To find the minimum number of registers needed without spilling, we perform liveness analysis for each variable at every program point. Since this is a basic block (straight-line code), the live ranges form an interval graph, and the minimum number of registers required is equal to the maximum number of variables live at any single point.

Let's trace the set of live variables after each instruction:

  1. a = 1                                    →Live: {a}\rightarrow \text{Live: } \{a\}
  2. b = 10                                   →Live: {a,b}\rightarrow \text{Live: } \{a, b\}
  3. c = 20                                   →Live: {a,b,c}\rightarrow \text{Live: } \{a, b, c\}   (Size = 3)
  4. d = a + b                                →Live: {c,d}\rightarrow \text{Live: } \{c, d\}   (a, b die)
  5. e = c + d                                →Live: {c,e}\rightarrow \text{Live: } \{c, e\}   (d dies)
  6. f = c + e                                →Live: {c,e,f}\rightarrow \text{Live: } \{c, e, f\}   (Size = 3)
  7. b = c + e                                →Live: {b,f}\rightarrow \text{Live: } \{b, f\}   (c, e die)
  8. e = b + f                                →Live: {e,f}\rightarrow \text{Live: } \{e, f\}   (b dies)
  9. d = 5 + e                                →Live: {d,f}\rightarrow \text{Live: } \{d, f\}   (e dies)
  10. return d + f                            →Live: {}\rightarrow \text{Live: } \{\}   (d, f die)

The maximum number of variables live simultaneously at any point is 33 (at steps 3 and 6). Thus, the minimum number of registers needed to execute this program without spilling is 33.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions