GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 55

Common-subexpression reuse across an array store

Compiler Design · Code Optimization & Data-Flow Analysis · Full GATE CSE 2006 paper

GATE 2006, Q55 2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 55, GATE 2006: Consider C functions:
int work 1(int *a,int i,int j) {
 int x=a[i+2]; a[j]=x+1; return a[i+2]-3;
}
int work 2(int *a,int i,int j) {
 int t1=i+2; int t2=a[t1]; a[j]=t2+1; return t2-3;
}
S1: work 2 always gives the same output and effect on state as work 1.
S2: the transformations always reduce CPU time.
Which truth values apply?
Options:
(A) S1 false, S2 false
(B) S1 false, S2 true
(C) S1 true, S2 false
(D) S1 true, S2 true
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. work 1 reads a[i+2], writes a[j], then reads a[i+2] again in its return expression. work 2 saves the first value and reuses it after the store. If j=i+2, the store changes the location that work 1 rereads, so the functions need not return the same value; S1 is false.

2. Compiler transformations also do not universally improve runtime on every target/input, and this transformation is not even semantics-preserving without a non-alias condition. S2 is false.

3. Thus A.

4. For i=0, j=2, initial a[2]=5, work 1 returns 3 whereas work 2 returns 2.

5. Teaching check: Take i=0,j=2,a[2]=5. Both functions store 6 into a[2], but work 1 then rereads 6 and returns 3, while work 2 reuses saved 5 and returns 2. Reusing a load across a potentially aliasing store is not a valid unconditional common-subexpression transformation.

Original paper, page 8