DAG in Compiler Design Examples Gate Vidyalay
DAG in Compiler Design Examples Gate Vidyalay
com/tag/dag-in-compiler-design-examples/
Before you go through this article, make sure that you have gone through the
previous articles on-
Problem-01:
c=0
1 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
do
if (a < b) then
x++;
else
x–;
c++;
} while (c < 5)
Solution-
1. c = 0
2. if (a < b) goto (4)
3. goto (7)
4. T1 = x + 1
5. x = T1
6. goto (9)
7. T2 = x – 1
8. x = T2
9. T3 = c + 1
10. c = T3
11. if (c < 5) goto (2)
12.
2 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Problem-02:
if A = 1 then C = C + 1
else
while A <= D
do A = A + B
Solution-
3 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Problem-03:
switch (ch)
case 1 : c = a + b;
break;
case 2 : c = a – b;
break;
Solution-
4 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
if ch = 1 goto L1
if ch = 2 goto L2
L1:
T1 = a + b
c = T1
goto Last
L2:
T1 = a – b
c = T2
goto Last
Last:
Problem-04:
1. a = b + c
2. t1 = a x a
3. b = t1 + a
4. c = t1 x b
5. t2 = c + b
6. a = t2 + t2
5 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Solution-
Directed acyclic graph for the given three address code is-
6 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Problem-05:
prod = 0 ;
i=1;
do
prod = prod + a[ i ] x b[ i ] ;
i=i+1;
Solution-
Part-01:
7 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
prod = 0
i=1
T1 = 4 x i
T2 = a[T1]
T3 = 4 x i
T4 = b[T3]
T5 = T2 x T4
T6 = T5 + prod
prod = T6
T7 = i + 1
i = T7
Part-02:
Step-01:
8 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Step-02:
The above generated three address code can be partitioned into 2 basic blocks
as-
9 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Step-03:
10 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
11 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
12 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Properties-
Applications-
• To determine the expressions which have been computed more than once
(called common sub-expressions).
• To determine the names whose computation has been done outside the
block but used inside the block.
• To determine the statements of the block whose computed value can be
made available outside the block.
• To simplify the list of Quadruples by not executing the assignment
instructions x:=y unless they are necessary and eliminating the common
sub-expressions.
Construction of DAGs-
Rule-01:
13 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
In a DAG,
Rule-02:
• A check is made to find if there exists any node with the same value.
• A new node is created only when there does not exist any node with the
same value.
• This action helps in detecting the common sub-expressions and avoiding
the re-computation of the same.
Rule-03:
The assignment instructions of the form x:=y are not performed unless they are
necessary.
14 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
GRAPHS-
Problem-01:
(a+b)x(a+b+c)
Solution-
T1 = a + b
T2 = T1 + c
T3 = T1 x T2
15 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
NOTE
Problem-02:
(((a+a)+(a+a))+((a+a)+(a+a)))
16 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Solution-
Problem-03:
(1) a = b x c
(2) d = b
(3) e = d x c
17 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
(4) b = e
(5) f = b + c
(6) g = f + d
Solution-
Problem-04:
18 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
Solution-
Step-01:
Firstly, construct a DAG for the given block (already done above).
Step-02:
(1) a = b x c
(2) d = b
(3) f = a + c
(4) g = f + d
Problem-05:
19 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
B10:
S1 = 4 x I
S2 = addr(A) – 4
S3 = S2[S1]
S4 = 4 x I
S5 = addr(B) – 4
S6 = S5[S4]
S7 = S3 x S6
S8 = PROD + S7
PROD = S8
S9 = I + 1
I = S9
Solution-
20 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
After eliminating S4, S8 and S9, we get the following basic block-
B10:
S1 = 4 x I
S2 = addr(A) – 4
21 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
S3 = S2[S1]
S5 = addr(B) – 4
S6 = S5[S1]
S7 = S3 x S6
PROD = PROD + S7
I=I+1
22 of 23 04/02/2023, 11:37 pm
DAG in Compiler Design Examples | Gate Vidyalay https://www.gatevidyalay.com/tag/dag-in-compiler-design-examples/
23 of 23 04/02/2023, 11:37 pm