GateAI
Local Optimisation

GATE CSE 2013 · Q48

MCQ2 marksMedium

Time

Common Data for Questions 48 and 49

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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 1

Local Optimisation

To determine the minimum number of spills with 2 available registers (R1R_1 and R2R_2):

  1. By code motion, the computations for dd and ee (d = c * a, e = c + a) can be moved entirely into the else block because neither dd nor ee is live or used inside the if block or branch condition.

  2. Before the branch, we must compute c = a + b and x = c * c, and then evaluate the condition x > a.

    • Load aa into R1R_1 and bb into R2R_2.

    • Compute c=a+bc = a + b into R2R_2 (bb is now dead). Currently, R1=aR_1 = a and R2=cR_2 = c.

    • To compute x=c∗cx = c * c, if we overwrite R2R_2 with xx, the value of cc is lost. However, cc will be needed in the else block to compute dd and ee.

    • Therefore, cc must be spilled to memory (1 spill).

    • Now compute x=c∗cx = c * c into R2R_2, evaluate the condition x > a comparing R2R_2 and R1R_1.

  3. In the if branch, compute y=a∗ay = a * a into R2R_2 (0 additional spills).

  4. In the else branch, reload cc from memory and compute dd and ee sequentially using the 2 registers (0 additional spills).

Thus, the minimum number of spills to memory is 1.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Local Optimisation previous year question is Q48 of the GATE CSE 2013 paper. Find more GATE CSE PYQs on Local Optimisation in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 4 related Local Optimisation PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Local Optimisation in GATE CSE

Local Optimisation has 4 GATE CSE PYQs across 4 papers from 2013 to 2025. 3 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2013.

How GATE tests Local Optimisation

More from GATE CSE 2013

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2013 · Q48 · MCQ

Common Data for Questions 48 and 49: 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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.