GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 50

GATE CSE 2021 Set 1 Q50

Compiler Design · Code Optimization & Data-Flow Analysis · Full GATE CSE 2021 Set 1 paper

GATE 2021 Set 1, Q50 2 marks · NAT
Report issue

Code Optimization & Data-Flow Analysis

Question 50, GATE 2021: A compiler represents this basic block by a common-subexpression DAG: a=b+c; e=a+1; d=b+c; f=d+1; g=e+f;. How many DAG nodes are needed? The symbol in the additions to a and d is the constant digit 1.
Tap to enlarge
Answer & solution

Official answer: 6

The code uses the constant 1, not a variable i. A common-subexpression DAG has three leaves b, c, 1. One addition node b+c is labeled by both a and d. A second addition node (b+c)+1 is labeled by both e and f. The final addition node adds that second node to itself and is labeled g. Hence there are 3 leaves plus 3 operation nodes, totaling 6. Variable labels are not separate DAG nodes in this representation.

Name the six nodes: n1=b, n2=c, n3=constant 1, n4=+(n1,n2) labeled a,d, n5=+(n4,n3) labeled e,f, n6=+(n5,n5) labeled g. The code does not change b or c, so the two b+c expressions really share one node. Repeated variable names attached as labels are not new values/nodes. This is common-subexpression DAG construction, not a syntax tree that duplicates every appearance or algebraic strength reduction to multiplication.

Original paper, page 39 · Answer key