Question

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:

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2021 answer key

Solution

Apply common subexpression elimination (CSE) to hoist loop-invariant expressions and reuse precomputed results, then count total additions and pointer dereferences.

Variables x, y, z, p, q, and i are in registers. The pointer y is not modified inside the loop, nor is x. Therefore, the expressions (x+3)(x + 3), y→f1y\to f1, and y→f2y\to f2 are loop-invariant common subexpressions.

Before the loop, the compiler precomputes these subexpressions into registers:

  • t1=x+3t_1 = x + 3 (1 addition)
  • t2=y→f1t_2 = y\to f1 (1 dereference)
  • t3=y→f2t_3 = y\to f2 (1 dereference)
  • z=t1+t2+t3z = t_1 + t_2 + t_3 (2 additions)
    Operations executed before the loop:
Additions=1+2=3\text{Additions} = 1 + 2 = 3
Dereferences=2\text{Dereferences} = 2

The loop header is for (i = 0; i < 200; i = i + 2). It executes for i=0,2,4,…,198i = 0, 2, 4, \dots, 198, which is exactly 2002=100\frac{200}{2} = 100 iterations.
In each iteration, the loop increment i = i + 2 performs 1 addition:

Loop increment additions=100×1=100\text{Loop increment additions} = 100 \times 1 = 100

Inside each iteration of the loop, exactly one branch of the if-else is executed:

  • If z>iz > i: p=p+t1p = p + t_1 (1 addition) and q=q+t2q = q + t_2 (1 addition), totaling 2 additions.
  • Else: p=p+t3p = p + t_3 (1 addition) and q=q+t1q = q + t_1 (1 addition), totaling 2 additions.
    No dereferences occur inside the loop because the precomputed register values t2t_2 and t3t_3 are used directly.
Loop body additions=100×2=200\text{Loop body additions} = 100 \times 2 = 200
Loop body dereferences=0\text{Loop body dereferences} = 0

Summing all operations:

Total additions=3 (before loop)+100 (increment)+200 (body)=303\text{Total additions} = 3 \text{ (before loop)} + 100 \text{ (increment)} + 200 \text{ (body)} = 303
Total dereferences=2 (before loop)+0=2\text{Total dereferences} = 2 \text{ (before loop)} + 0 = 2

This matches option D (303 and 2).

Final answer

D

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2021 Computer Science and Information Technology - Session 2 (CS2)
Question
40 of 65, printed as CS Q.30
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.