retagged by
10,465 views
12 votes
12 votes

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 $\text{(DAG)}$. The number of nodes in the $\text{DAG}$ is _____________

retagged by

4 Answers

Best answer
27 votes
27 votes

Here  $a$ and $d$ are same as both add same values $(bc)$ (common sub-expression elimination)

Since $a$ and $d$ are same $f$ and $e$ are also same as they compute $a+1$ and $d+1$ respectively.

  • $a = d =b+c$
  • $e = f = a+1$
  • $g = e + e$ ($f$ and $e$ being same) 

So total no of nodes is $6$ $( a, b, c, e, 1,g)$

Ans $:6$ nodes

selected by
20 votes
20 votes
the given c code can be written as:
a=b+c
e=a+1 =b+c+1
d=b+c
f=d+1 =b+c+1
g=e+f=(b+c+1)+(b+c+1)

Syntax tree of  $g=(b+c+1)+(b+c+1)$ is:

Since DAG  reused common sub-expression so redundant part is eliminated and join by single edges.

So number of nodes in dag are $6$ that are $(b,c,1,+,+,+)$ .

Answer:

Related questions

0 votes
0 votes
1 answer
1
newdreamz a1-z0 asked Jan 12, 2019
1,327 views
Consider the basic block given below:u=u+vv=v+wx=v-wy=v-xz=u+vThe minimum number of nodes and edges present in the DAG representations of the above basic block respective...
2 votes
2 votes
1 answer
2
pm9999 asked Oct 5, 2017
549 views
Minimum number of edges in the dag that represents the expression :x + x + x + x + x + x + x + x + x
0 votes
0 votes
1 answer
3
thor asked Jan 22, 2017
675 views
How does answer change when it is in SSA form? does answer remains 4 or 5
0 votes
0 votes
3 answers
4
radha gogia asked Dec 9, 2015
1,367 views
In this one I am unable to follow in the above node marked as "-" ,it has two edges one upward and one downward for "+" node so then how to proceed with this ?