Direction: 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;
}
-
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?
-
- 0
- 1
- 2
- 3
- 0
Correct Option: B
In this question we have to minimize spills to memory (writer). As variables d,e are only used in else part. So we can move d,e instructions inside else. The code we get is
c = a + b; Assumption-Register R1 and R2.
x = c * c; Already holds a and b respectively,
if (x > a)
{ We can first calculate the number of
y = a * a memory spill. The machine code is equivalent to
⇒ There has been only 1 memory write observed in the give code.
So the answer is 1.