GateAI
Local Optimisation

GATE CSE 2021 Set 2 · Q30

MCQ2 marksMedium

Time

Consider the following ANSI C code segment:z = x + 3 + y->f1 + y->f2; for (i = 0; i < 200; i = i + 2){ if (z > i) { p = p + x + 3; q = q + y->f1; } else { p = p + y->f2; q = q + x + 3; } }Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and dereference operations (of the form y->f1 or y->f2 ) in the optimized code, respectively, are:

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) 303 and 2

Local Optimisation

### 1. Analysis of Expressions Before and Inside the Loop

Before the loop:

  • The sub-expressions evaluated are x + 3, y->f1, and y->f2.

  • z = ((x + 3) + y->f1) + y->f2 requires 33 additions and 22 dereferences (y->f1 and y->f2).

Using Common Sub-expression Elimination (CSE) and Loop Invariant Code Motion:

  • The expressions x + 3, y->f1, and y->f2 do not change throughout the execution of the loop because x, y, y->f1, and y->f2 are never modified.

  • Thus, the compiler stores the results of x + 3, y->f1, and y->f2 in temporary registers before the loop.

### 2. Operations Inside the Loop

  • The loop executes for values i=0,2,4,…,198i = 0, 2, 4, \dots, 198, giving a total of 200−02=100\frac{200 - 0}{2} = 100 iterations.

  • In each iteration:

    • The if branch or else branch is executed. Both branches perform exactly 22 additions (e.g., p = p + (x + 3) and q = q + (y->f1) using precomputed values).

    • The loop increment i = i + 2 performs 11 addition.

    • Hence, each iteration requires 2+1=32 + 1 = 3 additions.

    • Across 100100 iterations: 100×3=300100 \times 3 = 300 additions.

    • No further pointer dereferences are executed inside the loop (00 dereferences).

### 3. Total Operations

  • Total Additions:3 (outside)+300 (inside)=3033 \text{ (outside)} + 300 \text{ (inside)} = 303

  • Total Dereferences:2 (outside)+0 (inside)=22 \text{ (outside)} + 0 \text{ (inside)} = 2

Therefore, the optimized code has 303 additions and 2 dereferences, which corresponds to option (D).

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 Q30 of the GATE CSE 2021 Set 2 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 2021 Set 2.

How GATE tests Local Optimisation

More from GATE CSE 2021 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2021 Set 2 · Q30 · MCQ

Consider the following ANSI C code segment: z = x + 3 + y->f1 + y->f2; for (i = 0; i i) { p = p + x + 3; q = q + y->f1; } else { p = p + y->f2; q = q + x + 3; } } Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2 , and the local variables x , y , z , p , q , and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and dereference operations (of the form y->f1 or y->f2 ) in the optimized code, respectively, are:

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.