Unit V - Directed Acyclic Graph (DAG)
Unit V - Directed Acyclic Graph (DAG)
Graph (DAG)
1
DAG Representation of Basic Blocks
• A DAG for a basic block is a directed acyclic graph with
the following labels on nodes:
– Leaf nodes represent identifiers, names or constants.
– Interior nodes represent operators.
– Interior nodes also represent the results of expressions or the
identifiers/name where the values are to be stored or assigned.
• DAGs are useful data structures for implementing
transformations on basic blocks.
• Directed Acyclic Graph (DAG) is a tool that depicts the
structure of basic blocks, helps to see the flow of values
flowing among the basic blocks, and offers optimization
too.
2
Algorithm for construction of DAG
Input: A basic block
Output: A DAG for the basic block containing the following
information:
1. A label for each node. For leaves, the label is an identifier. For
interior nodes, an operator symbol.
2. For each node a list of attached identifiers to hold the
computed values.
Case (i)x := y OP z
Case (ii)x := OP y
Case (iii)x := y
3
Contd…
Method:
Step 1: If y is undefined then create node(y).
If z is undefined, create node(z) for case(i).
4
Contd…
5
Applications of DAG
• To detect common sub expressions.
• To determine which identifiers have their values used
in the block.
• To determine which statements computed values that
could be used outside the block.
6
Example - 1
• Represent the following statement by means of DAG
i=i+10
:=
i +
i 10
7
Example - 2
• Represent the following statement by means of DAG
a=b*-c+b*-c
:=
a +
-
b
c
8
Example - 3
• Represent the following three address statement by
means of DAG
t0 = a + b d +
t1 = t0 + c
d = t0 + t1 t1 + t1 +
t0 +
+ c
t0 t0 + c
a b
a b a b
9
Exercise
• Represent the following sequence of three address
statements (basic block) by means of DAG.
t1=4*I
t2=a[t1]
t3=4*I
t4=b[t3]
t5=t2*t4
t6=prod+t5
prod=t6
10
Generating Code from DAG
• Advantage:
– From a DAG, we can easily see how to rearrange the
order of the final computation sequence than we can
start from a linear sequence of three-address
statements or quadruples.
11
Contd…
• Rearranging the order: The order in which computations are
done can affect the cost of resulting object code.
12
Contd…
• Generated code sequence for basic block:
MOV a , R0
ADD b , R0
MOV c , R1
ADD d , R1
MOV R0 , t1
MOV e , R0
SUB R1 , R0
MOV t1 , R1
SUB R0 , R1
MOV R1 , t4
13
Contd…
• Rearranged basic block:
t2 : = c + d
t3 : = e – t2
t1 : = a + b
t4 : = t1 – t3
14
Contd…
• Revised code sequence:
MOV c , R0
ADD d , R0
MOV e , R1
SUB R1 , R0
MOV a , R1
ADD b , R1
SUB R1 , R0
MOV R0 , t4
15
A Heuristic ordering for Dags
• The heuristic ordering algorithm attempts to make
the evaluation of a node immediately follow the
evaluation of its leftmost argument.
16
Contd…
• The algorithm shown below produces the ordering in
reverse.
Algorithm:
1. while unlisted interior nodes remain do begin
2. select an unlisted node n, all of whose parents have been listed;
3. list n;
4. while the leftmost child m of n has no unlisted parents and is
not a leaf do begin
5. list m;
6. n : = m
7. End
8. End
17
Contd…
• Example: Consider the following DAG:
18
Contd…
• Initially, the only node with no unlisted parents is 1 so set n=1 at
line (2) and list 1 at line (3).
19
Contd…
• Code sequence:
t8 : = d +e
t6 : = a + b
t5 : = t6 – c
t4 : = t5 * t8
t3 : = t4 – e
t2 : = t6 + t4
t1 : = t2 * t3
• This will yield an optimal code for the DAG on machine whatever
be the number of registers.
20