GATE Computer ScienceVery hardGATE CS (PYQ pattern)

In 1: i = 0; 2: L1: if i >= n goto L3; 3: if i > k goto L2; 4: t = a * b; 5: x = t + 1; 6: L2: y = a + b; 7: i = i + 1; 8: goto L1; 9: L3: return t the loop is statements 2 to 8. A statement is loop-invariant if each operand is a constant, has all its reaching definitions outside the loop, or has exactly one reaching definition, which is an invariant statement in the loop. An invariant statement s: x = … may be hoisted to a preheader only if its block dominates every loop exit and it is the only definition of x in the loop. How many statements are invariant, and how many may be hoisted?

  1. A. 3 invariant, 1 hoistable
  2. B. 3 invariant, 3 hoistable
  3. C. 3 invariant, 0 hoistable
  4. D. 2 invariant, 0 hoistable

Correct answer: C. 3 invariant, 0 hoistable

t = a * b and y = a + b use only a and b, never assigned in the loop; x = t + 1 has the single reaching definition t = a * b, which is invariant: 3 invariant statements. The only exit is the test at L1 (statement 2), and neither the block {4, 5} (skipped when i > k) nor the block at L2 dominates it. So none of the three may be hoisted; t, for instance, is used after the loop and must not be computed when the loop body never takes that branch.

More Compiler Design questions

Exams that ask this topic