IAE Academy

GATE 2023 CS Q.37: solution and answer

GATE 2023 · CSQ.37 · 2 marks · MCQ

Consider the control flow graph shown.

Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?

GATE 2023 CS question 37: Control flow graph with four basic blocks.Tap to open full size
Figure from the GATE 2023 CS paper. It is part of the question.
Figure description

Control flow graph with four basic blocks. ENTRY -> B1. B1 contains: i = m - 1, j = n, a = 10. B1 -> B2. B2 contains: i = i + 1, j = j - 1. B2 has two outgoing edges: B2 -> B3 and B2 -> B4. B3 contains a = 20; B3 -> B4. B4 contains i = a + 1. B4 has two outgoing edges: a back edge B4 -> B2 and B4 -> EXIT.

  1. (A)

    B1: {}, B2: {a}, B3: {a}, B4: {a}

  2. (B)

    B1: {i, j}, B2: {a}, B3: {a}, B4: {i}

  3. (C)

    B1: {a, i, j}, B2: {a, i, j}, B3: {a, i}, B4: {a}

  4. (D)

    B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}

Answer and solution

ANSWER

The live variables at the exit of the blocks are B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}, so the answer is (D).

Official answer: DSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. A variable is live at a point if its current value may be used before it is redefined. Use the backward equations and .

  2. Successors: B1 B2; B2 B3, B4; B3 B4; B4 B2 (back edge) and EXIT. Nothing is live at EXIT.

  3. USE/DEF: B1 uses and defines . B2 uses and defines . B3 uses nothing and defines . B4 uses and defines . ( and are never defined in the graph and do not appear in the options.)

  4. Because B2 uses before redefining them, . Also , since the back edge feeds B2 and EXIT contributes nothing.

  5. . and . Hence .

  6. Iterate from . Then and . So and . Recompute: , so again. Nothing changes, so this is the fixed point.

  7. Exit sets: ; ; ; .

  8. This is B1: , B2: , B3: , B4: , which is option (D).

KEY IDEA

Liveness is a backward data-flow problem. Compute IN and OUT by iterating to a fixed point, and remember that the loop's back edge feeds B4's exit into B2's entry.

WATCH OUT FOR

Forgetting the back edge B4 B2, so that OUT(B4) is taken as empty or as just {a}. Another slip is dropping from OUT(B4) because B4 redefines it. That is wrong, because is live after B4 through B2, where it is used in i = i + 1.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 6 Optimisation questions, with notes

More Optimisation questions