GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Register allocation by graph colouring is done on the loop 1: L1: v1 = v4 + 1; 2: v2 = v5 + 1; 3: v3 = v1 + 1; 4: v4 = v2 + 1; 5: v5 = v3 + 1; 6: if v5 > n goto L1; 7: return v5, n is an input live throughout. Two variables interfere if one is live at the point where the other is defined. Variables: n, v1, v2, v3, v4, v5. What is the minimum number of registers that allows allocation with no spill?

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

Correct answer: D. 4

The interference graph is the 5-cycle v1-v2-v3-v4-v5 (each value overlaps the next one around the loop) plus n, adjacent to all five. An odd cycle needs 3 colours and n needs a fourth, although no more than 3 values are ever live together (max-live is only a lower bound).

More Compiler Design questions

Exams that ask this topic