GATE CS 2021 Set 2 | Question 40
Compiler Design › Intermediate Code & Code Optimization · MCQ · 2 marks · Answer: D
- Question 40Compiler Designmcq2 marksConsider the following ANSI C code segment:
Assume thatz = 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; } }ypoints to astruct(allocated on the heap) with two fieldsf1andf2, and that the local variablesx, y, z, p, q, iare allotted registers. Common sub-expression elimination (CSE) is applied. The number of addition operations and dereference operations (of the formy->f1ory->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 dereferencesy->f1andy->f2are loop-invariant and computed once (2 total) after CSE. Counting the additions that remain after eliminating the common subexpressionx+3across the loop's 100 iterations gives 303 additions. Hence 303 and 2. - A.
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 →