GATE Computer ScienceVery hardGATE CS (PYQ pattern)

In 1: i = 0; 2: c = 2; 3: L1: if i >= n goto L3; 4: d = c * 3; 5: if i > k goto L2; 6: c = 2; 7: L2: e = d + 1; 8: f = a * b; 9: i = i + 1; 10: goto L1; 11: L3: return e the loop is statements 3 to 10. Using the textbook definition (every operand is a constant, has all its reaching definitions outside the loop, or has exactly one reaching definition, which is an invariant statement inside the loop), which statements are loop-invariant?

  1. A. 4, 6, 7 and 8
  2. B. 8 only
  3. C. 4, 6 and 8 only
  4. D. 6 and 8 only

Correct answer: D. 6 and 8 only

c = 2 (6) has only a constant operand and f = a * b (8) uses variables never assigned in the loop. d = c * 3 (4) sees two reaching definitions of c, statement 2 outside and statement 6 inside, so it fails the test even though both assign 2 (only constant propagation would discover that). e = d + 1 (7) depends on the non-invariant 4.

More Compiler Design questions

Exams that ask this topic