GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 30

GATE CSE 2021 Set 2 Q30

Compiler Design · Code Optimization & Data-Flow Analysis · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q30 2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 30, GATE 2021: Code: 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;}}. y points to a heap struct; x,y,z,p,q,i reside in registers. After common subexpression elimination, count additions and field dereferences. A (403,102); B (203,2); C (303,102); D (303,2).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Compute and reuse x+3, y->f1, and y->f2. The initial expression then uses 3 additions and 2 dereferences. There are 100 loop iterations; either branch needs one addition for p and one for q, and the increment i=i+2 needs one more. Thus total additions 3+100·3=303, while both dereferenced fields remain reusable because the loop does not modify them. Answer D, under the specified CSE model.

An optimized schematic is u=x+3;v=y->f1;w=y->f2;z=u+v+w; followed by branches (p+=u;q+=v) or (p+=w;q+=u). There are 100 iterations because i runs 0,2,…,198. CSE does not erase the loop-counter addition or the two updates in either branch. The count assumes the problem's arithmetic regrouping/CSE model and no intervening field writes or aliasing stores.

Original paper, page 71 · Answer key