Question

Consider the control flow graph shown.

Control flow graph with basic blocks B1 (i = m - 1, j = n, a = 10), B2 (i = i + 1, j = j - 1), B3 (a = 20), and B4 (i = a + 1), edges ENTRY->B1->B2, B2->B3, B2->B4, B3->B4, B4->B2, and B4->EXIT.

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

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2023 answer key

Solution

Perform backward data-flow analysis to compute LiveOut for each basic block.

A variable is live at the exit of block BB if it is used along some execution path starting from an outgoing edge of BB before being redefined.

  1. In block B4B_4: statements are i=a+1i = a + 1. It uses variable aa and defines ii. At the exit of B4B_4, control can loop back to B2B_2, where i=i+1i = i + 1 and j=j−1j = j - 1 use variables ii and jj, while aa may be used in future iterations. Hence LiveOut(B4)={a,i,j}\text{LiveOut}(B_4) = \{a, i, j\}.
  2. In block B3B_3: statement is a=20a = 20. Successor is B4B_4 (and through loop back to B2B_2). At exit of B3B_3, ii is immediately overwritten by i=a+1i = a + 1 in B4B_4, but jj is needed in B2B_2, and aa is used in B4B_4. Hence LiveOut(B3)={a,j}\text{LiveOut}(B_3) = \{a, j\}.
  3. In block B2B_2: uses i,ji, j and updates them. Successors are B3B_3 and B4B_4. If taking the edge to B4B_4, aa (from B1B_1) is used, and jj is preserved. Hence LiveOut(B2)={a,j}\text{LiveOut}(B_2) = \{a, j\}.
  4. In block B1B_1: defines i=m−1i = m - 1, j=nj = n, a=10a = 10. The successor B2B_2 immediately uses ii and jj, and aa is preserved. Hence LiveOut(B1)={a,i,j}\text{LiveOut}(B_1) = \{a, i, j\}.

Thus, the live variable sets at block exits are B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}.

Final answer

Option (D)

Common trap

Assuming ii is live at the exit of B3B_3, forgetting that B4B_4 immediately defines ii before any use.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2023 Computer Science and Information Technology (CS)
Question
37 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.