Question
Consider the following C code segment:
a = b + c;
e = a + 1;
d = b + c;
f = d + 1;
g = e + f;
In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is __________
Official answer and solution
- Official answer
- 6
- From
- Official GATE 2021 answer key
Solution
Construct the Directed Acyclic Graph (DAG) for the basic block by reusing identical subexpression nodes.
Let us trace the DAG construction statement by statement:
- Initial leaves: Variables
bandcare live upon entry. Create leaf node and leaf node . - Statement
a = b + c: Create interior operator node with left child and right child . Attach labelato . - Create constant leaf node .
- Statement
e = a + 1: Create interior operator node with left child and right child . Attach labeleto .
- Statement
d = b + c: The subexpressionb + calready corresponds to existing node . Attach labeldto without creating a new node. - Statement
f = d + 1: Sincedrefers to ,d + 1is , which already corresponds to node . Attach labelfto without creating a new node. - Statement
g = e + f: Botheandfrefer to node . Create interior operator node with left child and right child . Attach labelgto .
Count total nodes in the DAG:
- Leaf nodes: (3 leaf nodes)
- Operator nodes: (3 interior nodes)
Total number of nodes .
Final answer
6
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology (CS, Set-1)
- Question
- 60 of 65, printed as CS Q.50
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.