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.

83.

................. refers to the order in which individual statements or instructions of a program are executed.

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: B

The order in which statements are executed is the Control flow.

Control flow is the sequence in which a program's statements, instructions or function calls are executed, shaped by conditionals, loops, jumps and calls. Compilers represent it as a control flow graph.

Data flow concerns how values move between definitions and uses, register allocation assigns variables to registers, and instruction scheduling reorders machine instructions for the pipeline.

Please log in or register to report this question.

Answer: D

Loop unrolling aims at expanding the body of a loop to reduce overhead and improve parallelism.

Unrolling replicates the loop body several times and adjusts the loop counter, so the test, increment and branch are executed less often. The longer straight-line body also gives the scheduler more independent instructions to overlap.

It does not reduce the number of loops in the program, it does not remove conditionals inside the body, and it makes the loop larger, not smaller.

Please log in or register to report this question.

85.

In peephole optimization, constant folding involves :

Exam Year(s)

MH SET - 2025, SET Exam - 2025,

Answer: C

Constant folding means folding constant expressions during compilation.

When an expression's operands are all known at compile time, the compiler evaluates it once and replaces it with the result: x = 2 * 3 + 4 becomes x = 10. This saves the computation at run time.

Combining consecutive loads, eliminating dead code and unrolling loops are separate optimisations. Each removes or restructures code, but none computes constant expressions in advance.

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