The GATE Grind

GATE 2021 CS (CS2) – Question 40

Compiler Design · Code Optimization and Data Flow Analysis · 2 marks · Multiple choice

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; }
}
  1. 403 and 102
  2. 203 and 2
  3. 303 and 102
  4. 303 and 2

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.

More Code Optimization and Data Flow Analysis questions

All Code Optimization and Data Flow Analysis questions →