GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Dead-code elimination deletes every assignment whose target is not live immediately after it (live-variable analysis), and is repeated until nothing changes. How many statements does it delete from 1: s = 0; 2: i = 0; 3: L1: if s > n goto L2; 4: s = s + 1; 5: i = i + 1; 6: goto L1; 7: L2: return s? (i is never used outside statements 2 and 5.)

  1. A. 2
  2. B. 1
  3. C. 0
  4. D. 3

Correct answer: C. 0

i is useless, but liveness cannot see it: i = i + 1 reads i, and around the back edge that read keeps i live after statement 5 and after statement 2. So no assignment has a dead target and nothing is deleted. Removing statements 2 and 5 needs faint-variable (or 'useful code') analysis, which marks a variable used only to compute itself as faint.

More Compiler Design questions

Exams that ask this topic