• edited by
45,001 views
55 55 votes

The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and one destination operand. Assume that all variables are dead after this code segment.

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; 
} 

Q.48 Suppose the instruction set architecture of the processor has only two registers. The only allowed compiler optimization is code motion, which moves statements from one place to another while preserving correctness. What is the minimum number of spills to memory in the compiled code?

  1. 0
  2. 1
  3. 2
  4. 3

6 Answers

Best answer
54 54 votes
We can do code motion as follows:

c = a + b; //a and b in register and b replaced by the result c after the instruction
x = c * c; //x replaces c in register and c is spilled (moved to memory)
if (x > a) { //x and a in registers
 y = a * a; 
 d = c * a; //spilled c taken from memory and replaces x in register. 
 e = c + a; 
} 
else { 
 d = c * a; //spilled c taken from memory and replaces x in register. d replaces a in register
 d = d * d; //c and d in register
 e = c + a; //a  taken from memory and e replaces c in register (a taken from memory is not a spill, it is a fill. The reason is a is not modified till this point.)
 e = e * e; 
} 

So, we need minimum 1 spill in the compiled code.

https://en.wikipedia.org/wiki/Register_allocation 

• edited by
58 58 votes
                r1=a   r2=b
c = a + b;      r1=a   r2=c
x = c * c;         a      x     c spilled memory 
if (x > a) {                                   //jump to either block 
 d = c * a;        a      d     c
 e = c + a;        a      e     c
 y = a * a;        y      e     c
}                                         
else { 
 d = c * a;        a      d     c        
 e = c + a;        e      d     c
 d = d * d;               d
 e = e * e;        e
} 

Only c needs to be in memory and spilling (register to memory) is done only once so answer would be 1

3 3 votes

Only one memory spill is required to store x. because when else part to be executed we need to store c after replacing x in register R2 

3 3 votes

To minimize spills to memory in the compiled code, we need to consider the limited number of registers available in the instruction set architecture. In this case, the processor has only two registers.

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;
}

The limited register constraint may lead to spills to memory when there are more than two live variables at the same time.

  • Before c = a + b;, a and b are live.
  • Before d = c * a;, a, b, and c are live.
  • Before e = c + a;, a, b, and c are live.
  • Before x = c * c;, a, b, and c are live.
  • Before the if statement, a, b, and c are live.
  • Inside the if branch, a and y are live.
  • Inside the else branch, a, b, c, d, and e are live.

Now, let's consider the limited registers (R1 and R2). We can observe that at most three variables (a, b, and c) are live simultaneously before the if statement. Therefore, we may need to spill one variable to memory.

The minimum number of spills to memory is:

1

1 1 vote

Spill: Move data from a Register to Memory.

Fill: Move data from Memory to a Register.
Although, I couldn't find actual definition of fill, taking it from Arjun sir's answer.


Since question mentions

Assume that all variables are dead after this code segment

d & e needs to be calculated only in the else block, so we move it there.
 

ignoring obvious loads

c = a + b; // R2{c} <- R1{a} + R2{b}
x = c * c; // Need 'a' next, so spill c in memory. And then R2{x} <- R2{c} * R2 {c}
// above if you spill 'a' to memory, you need to spill c to memory again later (to load x), so to minimize spill c into memory
// also, a is never modifed in the program, so we can 'fill' it anytime.
if (x > a) { // CMP R2{x}, R1{a}
  y = a * a;  // R2{} <- R1{a}*R1{a}
}
else {
//since question says minimum spill to memory, c can be read (or fill'ed) multiple times
  d = c * a; // load R2,c; R2{d} <- R2{c}*R1{a}
  d = d * d; // R2{d} <- R2{d} * R2{d}
  e = c + a; // load R2,c; R2{e} <- R2{c} + R1{a}
  e = e * e; // R2{e} <- R2{e} * R2 {e}
}
0 0 votes
We can do code motion as follows:
c = a + b; //a and b in register and b replaced by the result c after the instruction
x = c * c; //x replaces c in register and c is spilled (moved to memory)
if (x > a) { //x and a in memory
y = a * a;
d = c * a; //spilled c taken from memory and replaces x in register.
e = c + a;
}
else {
d = c * a; //spilled c taken from memory and replaces x in register. d replaces a in register
d = d * d; //c and d in register
e = c + a; //a  taken from memory and e replaces c in register (a taken from memory is not a spill, it is a fill)
e = e * e;
} ..
1 flag:
✌ Duplicate (spongebob)
Answer:
Position:
Show:

Related questions

22 22 votes
3 answers 3 answers
12.1k
12.1k views
go_editor asked Apr 21, 2016
12,065 views
The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and on...
70 70 votes
6 answers 6 answers
20.9k
20.9k views
Arjun asked Sep 24, 2014
20,901 views
Consider the following two sets of $\textsf{LR(1)}$ items of an $\textsf{LR(1)}$ grammar.$ $\begin{array}{l|l}X \rightarrow c.X, c∕d &X → c.X, \$\\X \rightarrow .cX, c∕ ...
126 126 votes
18 answers 18 answers
64.9k
64.9k views
Arjun asked Sep 23, 2014
64,918 views
What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon and unit-production (i.e., of type $A \rightarrow \epsilo...
106 106 votes
4 answers 4 answers
34.5k
34.5k views
gatecse asked Aug 7, 2014
34,453 views
What is the return value of $f(p,p)$, if the value of $p$ is initialized to $5$ before the call? Note that the first parameter is passed by reference, whereas the second ...