in Compiler Design edited by
21,412 views
64 votes
64 votes

The program below uses six temporary variables $a, 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?

  1. $2$
  2. $3$
  3. $4$
  4. $6$
in Compiler Design edited by
21.4k views

4 Comments

@jatinmittal199510   can you explain how you write number on nodes abcdf….

0
0

@jugnu1337 this is nothing but graph coloring.And whatever coloring he has done is wrong..You try to color this graph , you will get ans as 3. 

2
2

A little Error correction, you were updating the R2 value @e=f+b , so we would loose the f/b value @R2, so we better bring R3 here itself, so that we can use the f/b from R2 at last step

2
2

7 Answers

72 votes
72 votes
Best answer
Here in these types of compiler questions, idea is "map/assign multiple temporaries to one registers."

here $a, b,$ and $c$ all are having $3$ different values so i need atleast $3$ registers $r1$, $r2$ and $r3$.
$a$ is mapped to $r1$, $b$ to $r2$ and $c$ to $r3$.

$d = a + b$, after this line if u notice '$a$' is never present on right hand side, so I can map (register of $a$ which is $r1$ ) $d$ to $r1$.
$e = c + d$, after this line '$d$' is never present on rhs, so I can map (register of $d$ which is $r1$ ) $e$ to $r1$.

at this time mapping is
$r1 --- e$
$r2 --- b$
$r3 --- c$

(at this moment I have registers for $e, b$ and $c$. if I introduce new variable then I may need different register)
now at this point if u see
$f = c + e$
$b = c + e$
these two are essentially doing same thing, after these two line '$b$' and '$f$' are same so I can skip computing '$f$'. and whereever $f$ is present I will replace it with '$b$'. (because neither of '$f$' and '$b$' are changing after these two lines, so value of these will be '$c+e$' forever)

(seems like I introduced one more variable $f$, and register is needed for that, but actually I did not really introduce '$f$'. I am skipping computation of '$f$')
now at second last line "$d = 5 + e$"
here I introduced '$d$', I can map it to any of the register $r1$ or $r3$, because after this line neither of '$e$' or '$c$' is required. (value of '$b$' is required because I need to return '$d+f$', and '$f$' is essentially equal to '$b$')

finally code becomes

$r1 = 1$
$r2 = 10$
$r3 = 20$
$r1 = r1 + r2$
$r1 = r3 + r1$

(skipping '$f$' computation)
$r2 = r3 + r1$
$r2 = r3 + r1$
$r1 = r2 + r2$
$r3 = 5 + r1$
return $r3 + r2$

Therefore minimum $3$ registers needed.

Correct Answer: $B$
edited by

4 Comments

edited by

here \(a\), \(b\), and \(c\) all are having \(3\) different values so i need atleast \(3\) registers \(r1\), \(r2\) and \(r3\).

@Sachin Mittal 1 Sir, would this always be true? For example – suppose that the code was such that after all the assignment statements to the variables \(a\), \(b\) and \(c\), only the variable \(b\) was never again used later in the code. Then, during the compiler's code optimization phase, the variable \(b\)’s assignment statement is removed from the code (dead code elimination), and in which case we would require only two registers, though three variables are getting initialized with different values, right?

0
0

@Sachin Mittal 1 @GO Classes  Why we can’t just store value of f in register containing original b,as original b value will not be used again?

So without ignoring any step we can do it with 3 registers.

0
0

@Abhrajyoti00 ig if we change the initial values of a,b,c then your new code wont work as you have hardwired the initial value of b to 10 by writing r1 = r1 +10 . Therefore only 2 registers wont do .

0
0
24 votes
24 votes

All of the given expressions use at-most 3 variables, so we never nee more than 3 registers.

See  http://en.wikipedia.org/wiki/Register_allocation

It requires minimum 3 registers.

Principle of Register Allocation : If a variable needs to be allocated to a register, the system checks for any free register available, if it finds one, it allocates. If there is no free register, then it checks for a register that contains a dead variable ( a variable whose value is not going to be used in future ), and if it finds one then it allocates. Otherwise it goes for Spilling ( it checks for a register whose value is needed after the longest time, saves its value into the memory, and then use that register for current allocation, later when the old value of the register is needed, the system gets it from the memory where it was saved and allocate it in any register which is available ).

But here we should not apply spilling as directed in the question.

Let’s allocate the registers for the variables.

a = 1 ( let’s say register R1 is allocated for variable ‘a’ )

b = 10 ( R2 for ‘b’ , because value of ‘a’ is going to be used in the future, hence can not replace variable of ‘a’ by that of ‘b’ in R1)

c = 20 ( R3 for ‘c’, because values of ‘a’ and ‘b’ are going to be used in the future, hence can not replace variable ‘a’ or ‘b’ by ‘c’ in R1 or R2 respectively)

d = a+b ( now, ‘d’ can be assigned to R1 because R1 contains dead variable which is ‘a’ and it is so called because it is not going to be used in future, i.e. no subsequent expression uses the value of variable ‘a’)

e = c+d ( ‘e’ can be assigned to R1, because currently R1 contains value of varibale ‘d’ which is not going to be used in the subsequent expression.)

Note: an already calculated value of a variable is used only by READ operation ( not WRITE), hence we have to see only on the RHS side of the subsequent expressions that whether the variable is going to be used or not.

f = c+e ( ‘ f ‘ can be assigned to R2, because vaule of ‘b’ in register R2 is not going to be used in subsequent expressions, hence R2 can be used to allocate for ‘ f ‘ replacing ‘b’ )

b = c+e ( ‘ b ‘ can be assigned to R3, because value of ‘c’ in R3 is not being used later )

e = b+f ( here ‘e’ is already in R1, so no allocation here, direct assignment )

d = 5+e ( ‘d’ can be assigned to either R1 or R3, because values in both are not used further, let’s assign in R1 )

return d+f ( no allocation here, simply contents of registers R1 and R2 are added and returned)

hence we need only 3 registers, R1 R2 and R3.

ref-http://quiz.geeksforgeeks.org/gate-gate-cs-2010-question-37/

by

1 comment

simple and clear,great job....
0
0
14 votes
14 votes
After making the interference graph it can be colored with 3 different colors. Therefore minimum number of color needed to execute the program without spilling would be 3 (B).
by

4 Comments

with interfacing(DAG) diagram also it will take only 2 registers
1
1
Thanks Nirmal that was helpful.
1
1
10 votes
10 votes

3 registers.

here,

d = a + b
e = c + (a+b) 
f = c + (c+ (a+b)) 
b = c + (c+ (a+b) = f 
e = b + (c+(c+(a+b))) 
d = 5 + (b+(c+(c+(a+b))))
return d + f 

first evaluate b using two registers and then store its value in f because f and b are equal.

now evaluate d with two registers. then return d+f

hence 3 registers.

4 Comments

thanx @ jayendra
2
2
In the step d=5+e 5 should be in a register as it is given in the question that all operations take their operands from registers
1
1

Aishwarya Gujrathi 1
Operands should be in Memory not constant values its like immediate adressing mode right?

1
1
Answer:

Related questions