Machine-translated by Google. Questions are authored in English.
Top

Code Optimization Questions and Answers

81.

Consider the control flow graph given below.

Control flow graph: ENTRY to B1 (a = b + c); B1 to B2 (d = a + e) and to B3 (e = a + f); B3 loops back to B1; B2 to B4 (g = d + e); B4 to EXIT

Which one of the following options is the set of live variables at the exit point of each basic block?

Exam Year(s)

GATE CS - 2026,

Answer: A

The live variables at block exits are B1:{a, b, c, e, f}, B2:{d, e}, B3:{b, c, e, f}, B4:∅.

Liveness flows backwards: OUT(B) is the union of IN over B's successors, and IN(B) = USE(B) ∪ (OUT(B) - DEF(B)). The edges are B1 → B2, B1 → B3, B3 → B1 and B2 → B4.

  • B4 (g = d + e) leads to EXIT, and g is never used: OUT(B4) = ∅, IN(B4) = {d, e}.
  • B2 (d = a + e): OUT(B2) = IN(B4) = {d, e}; IN(B2) = {a, e}.
  • B1 (a = b + c) and B3 (e = a + f) form a loop. Iterating to a fixed point: IN(B1) = {b, c, e, f} and IN(B3) = {a, b, c, f}.
  • OUT(B1) = IN(B2) ∪ IN(B3) = {a, b, c, e, f}; OUT(B3) = IN(B1) = {b, c, e, f}.

The options with B1:∅ ignore that a is used in both successors of B1. The option with B3:{c, e, f} drops b, which is live because B1, reached from B3, uses it.

Please log in or register to report this question.

82.

Consider the control flow graph shown.

Control flow graph: ENTRY to B1 (i = m - 1, j = n, a = 10), B1 to B2 (i = i + 1, j = j - 1), B2 to B3 (a = 20) and to B4 (i = a + 1), B3 to B4, B4 back to B2 and to EXIT

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

Exam Year(s)

GATE CS - 2023,

Answer: D

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

A variable is live at a point if some path from there uses it before redefining it. Work backwards from the uses, assuming nothing is live at EXIT:

  • B2 uses i and j (i = i + 1, j = j - 1) before defining them, so both are live on entry to B2.
  • B4 uses a, then redefines i. Its successors are B2 and EXIT, so live-out of B4 = live-in of B2.
  • B3 defines a and uses nothing; B2 flows to B3 and B4.

Solving: live-in(B4) = {a} plus (live-out(B4) minus {i}) = {a, j}; live-in(B3) = {a, j} minus {a} = {j}; live-out(B2) = {j} plus {a, j} = {a, j}; live-in(B2) = {i, j} plus ({a, j} minus {i, j}) = {a, i, j}, which is live-out of both B1 and B4. B3's exit set is live-in(B4) = {a, j}.

The first two options drop i and j at B1's exit although B2 reads them at once. The third keeps i live after B2, but i is redefined in B4 before any use on that path.

Please log in or register to report this question.

Loading…
Tags: Code Optimization Questions and Answers || Code Optimization MCQ Questions and Answers || Code Optimization GK Questions and Answers || Code Optimization GK MCQ Questions || Code Optimization Multiple Choice Questions and Answers || Code Optimization GK || GK on Code Optimization || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design