GATE CS 2021 Set 2 | Question 40

Compiler Design › Intermediate Code & Code Optimization · MCQ · 2 marks · Answer: D

  1. Question 40Compiler Designmcq2 marks
    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 y points to a struct (allocated on the heap) with two fields f1 and f2, and that the local variables x, y, z, p, q, i are allotted registers. Common sub-expression elimination (CSE) is applied. The number of addition operations and dereference operations (of the form y->f1 or y->f2) in the optimized code, respectively, are:
    • A.
      403 and 102
    • B.
      203 and 2
    • C.
      303 and 102
    • D.
      303 and 2
      ✓

    Answer: D

    Solution

    The dereferences y->f1 and y->f2 are loop-invariant and computed once (2 total) after CSE. Counting the additions that remain after eliminating the common subexpression x+3 across the loop's 100 iterations gives 303 additions. Hence 303 and 2.

More GATE PYQ on Intermediate Code & Code Optimization

All GATE CS Compiler Design PYQ →

Attempt GATE CS 2021 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →