GATE 2021 CS (CS2) – Question 40
Apply common-subexpression elimination to the code. y points to a heap struct with f1,f2; x,y,z,p,q,i reside in registers. Count additions and y->field dereferences in the optimized execution.
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; }
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (D) 303 and 2
Explanation
Compute x+3 once and load the unchanged struct fields once each. Forming z then costs two additional additions, so initialization has three additions and two dereferences. Each of one hundred iterations needs two accumulator additions plus one loop increment, totalling three hundred. Overall there are 303 additions and two dereferences, D.