GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A DAG is built for the basic block x = a * b; y = b * a; z = x + y; a = z − x; w = a * b; v = y + x (+ and * commutative, a new leaf for each variable's initial value, a reassigned variable refers to its new node). How many nodes does the DAG have, and how many instructions does the optimised block need if only v and w are live on exit (one instruction per needed interior node)?

  1. A. 7 nodes; 4 instructions
  2. B. 6 nodes; 4 instructions
  3. C. 6 nodes; 5 instructions
  4. D. 8 nodes; 6 instructions

Correct answer: B. 6 nodes; 4 instructions

Leaves a0, b0. x and y are the same node n1 = a0 * b0; z = n1 + n1 = n2, and v = y + x is n2 again; a = n2 − n1 = n3; w = a * b uses the new a: n4 = n3 * b0 (a different node from n1). Nodes: 2 leaves + 4 interior = 6. v and w need n1, n2, n3, n4: t = a * b; v = t + t; u = v − t; w = u * b — 4 instructions.

More Compiler Design questions

Exams that ask this topic