Q1.The following code segment is executed on a processor which allows only register operands in
its instructions. Each instruction can have almost 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?
Answer : Option BExplaination / Solution:
After applying the code motion optimization the statement d=c*a; and e=c+a; can be moved
down to else block as d and e are not used anywhere before that and also value of a and c is
not changing.
In the above code total number of spills to memory is 1
Q4.The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have almost 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;
}
What is the minimum number of registers needed in the instruction set architecture of the
processor to compile this code segment without any spill to memory? Do not apply any
optimization other than optimizing register allocation
Answer : Option BExplaination / Solution:
In the above code minimum number of registers needed are = 4
Answer : Option DExplaination / Solution:
There is an algorithm to check whether the given CFG is empty, finite or infinite and also to
convert NFA to DFA hence 1 and 4 are decidable
Q10.Consider an undirected random graph of eight vertices. The probability that there is an edge
between a pair of vertices is ½. What is the expected number of unordered cycles of length
three?