GATE / CS ARCHIVE

GATE CSE 2017 SET 1 / QUESTION 52

GATE CSE 2017 Set 1 Q52

Compiler Design · Code Generation & Register Allocation · Full GATE CSE 2017 Set 1 paper

GATE 2017 Set 1, Q52 2 marks · NAT
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 52, GATE 2017: For (a−1)*(((b+c)/3)+d), find minimum registers for optimal load/store code generation without spills. Only loads/stores can access memory; arithmetic operands must be registers or immediate constants.
Tap to enlarge
Answer & solution

Official answer: 2.0

Our answer: 2

1. Evaluate the more demanding right subtree first. Load b into R1 and c into R2; add into R1. Divide R1 by immediate 3, then reuse R2 to load d and add it to R1. Now R1 holds ((b+c)/3)+d.

2. Keep R1 live and load a into R2. Subtract immediate 1 from R2, then multiply R2 and R1 into either register. At no time are more than two registers live, and no intermediate is stored to memory.

3. One register cannot perform b+c under the stated arithmetic rule because both non-immediate operands must be in registers simultaneously. Hence both lower and upper bounds are 2.

Original paper, page 23 · Answer key