GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Minimize registers for expression-tree evaluation

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Label/evaluate subtrees under the supplied load-store instruction constraints and choose order minimizing live intermediates. Different trees and immediate-operand permissions are preserved; not repeats.

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

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Registers for fixed expression-tree evaluation

Code Generation & Register Allocation

Question 36, GATE 2011: Evaluate the fixed expression tree (a-b)+(e-(c+d)) on a load-store machine. Leaves a,b,c,d,e start in memory. Binary operations require both operands in registers and leave their result in a register; no intermediate result may be stored in memory. What is the minimum number of registers needed? The tree must be evaluated as given.

(A) 2
(B) 9
(C) 5
(D) 3
Tap to enlarge
Your answer
Answer & solution

Official answer: D

A leaf needs 1 register; a binary node with equally demanding children needs one more than either, otherwise it needs the larger requirement when evaluated in the favorable order. Thus a−b needs 2; c+d needs 2; e−(c+d) also needs 2; the root combining two 2-register subtrees needs 3. Three suffice by retaining one subtree result while evaluating the other in two registers. Two cannot hold one completed child while evaluating the other fixed subtree. Hence D. This evaluates the supplied tree without algebraic reassociation; rewriting the arithmetic expression into a different tree changes the problem.

A concrete three-register schedule is: load c and d into R1,R2; replace R1 by c+d; load e into R2 and replace R2 by e-R1; then load a into R1 and b into R3, replace R1 by a-b, and add R1 to R2. The two main subtrees each need two registers to combine two operands. Whichever subtree finishes first leaves one live result while the second is evaluated, forcing a third register without spilling or reassociation.

Original paper, page 10 · Answer key