GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 60

Identifying unavailable loop optimization

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

GATE 2006, Q60 2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 60, GATE 2006: Consider C code (with defined arithmetic and x,y live after the fragment):
for (i=0;i<n;i++) {
 for (j=0;j<n;j++) {
  if (i%2) { x+=(4*j+5*i); y+=(7+4*j); }
 }
}
Which optimization claim is not supported by this fragment?
Options:
(A) It contains loop-invariant computation
(B) Common-subexpression elimination is possible
(C) Strength reduction is possible
(D) Dead-code elimination is possible
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Within the inner j loop, i%2 and 5*i are invariant. The expression 4*j occurs in both updates and can be shared.

2. The products by loop indices can use strength reduction through induction-variable increments. However x and y are updated and may be used later; nothing in the snippet establishes that those results are dead.

3. Therefore the claim of available dead-code elimination is unsupported and is the intended false statement, D. This uses the ordinary compiler-question assumption that program behavior is defined and x,y remain live afterward.

4. Teaching check: An expression can be expensive and still necessary. Dead-code elimination requires showing that its computed value never affects observable behavior, which is not established for x or y. In contrast, the repeated arithmetic patterns are visible directly within the loops.

Original paper, page 9