GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Build expression DAGs with shared subexpressions

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Intern equal operands/operators into shared DAG nodes, then count requested nodes or edges. Expressions and requested metric differ; numeral 1 is preserved.

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

2 marks · MCQ
Report issue

Code Optimization & Data-Flow Analysis

Question 34, GATE 2014: The basic block executes a=b+c; c=a+d; d=b+c; e=d−b; a=e+b. What are the minimum DAG node and edge counts? A (6,6); B (8,10); C (9,12); D (4,4).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A with algebraic simplification

Step 1: Preserve old values when tracking assignments: let the input leaves be b0,c0,d0. The first three operations create u=b0+c0, v=u+d0, w=b0+v. The current c is v and current d is w. Step 2: Algebraically, e=w−b0=(b0+v)−b0=v, and final a=e+b0=v+b0=w. Therefore e shares v's node and a shares w's node. Step 3: The minimized DAG has three input leaves and three binary addition nodes: six nodes. Each operation has two incoming operand edges, giving six edges, A. Without cancellation, a purely syntactic DAG gives eight nodes and ten edges. The keyed minimum assumes valid algebraic identities, not unrestricted floating-point cancellation.

Original paper, page 60 · Answer key