Question
Consider the expression . Let be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture, in which (i) only load and store instructions can have memory operands and (ii) arithmetic instructions can have only register or immediate operands. The value of is ______.
Official answer and solution
- Official answer
- 2
- From
- Official GATE 2017 answer key
Solution
Construct the optimal instruction sequence for evaluating the expression tree without register spilling on a load/store architecture.
We are given the expression on a load/store architecture where memory operands must be loaded first, and arithmetic operations can use registers or immediate constants.
Let us determine if 2 registers (R1, R2) are sufficient without spilling:
- Evaluate :
LOAD R1, b(R1 holds )LOAD R2, c(R2 holds )ADD R1, R1, R2(R1 holds ; R2 is now free)DIV R1, R1, #3(R1 holds using immediate 3)
- Add :
LOAD R2, d(R2 holds )ADD R1, R1, R2(R1 holds ; R2 is free again)
- Evaluate while holding the previous subexpression in R1:
LOAD R2, a(R2 holds )SUB R2, R2, #1(R2 holds using immediate 1)
- Final multiplication:
MUL R1, R1, R2(R1 holds the final result)
At every instruction, at most 2 registers (R1 and R2) are required simultaneously, and no register spill to memory is needed. Hence, the minimum number of registers is 2.
2.0
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2017 Computer Science and Information Technology (CS) Session 1
- Question
- 52 of 65, printed as CS Q.52
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.