Skip to main content
V.JOTHI LAKSHMI
 Useful data structures for implementing transformations on basic
blocks
 Gives a picture of how value computed by a statement is used in
subsequent statements
 Constructing DAG from 3 address statements is good way of
determining common sub-expressions
 A dag for a basic block has following labels on the nodes
 Leaves are labeled by unique identifiers, either variable names or constants
 Interior nodes are labeled by an operator symbol
 Nodes are also optionally given a sequence of identifiers for labels
2
1. t1 := 4 * i
2. t2 := a[t1]
3. t3 := 4 * i
4. t4 := b[t3]
5. t5 := t2 * t4
6. t6 := prod + t5
7. prod := t6
8. t7 := i + 1
9. i := t7
10. if i <= 20 goto (1)
4
+
prod0 *
[ ] [ ]
*
i04
ba +
1
20
<=
t1
t4
t5
t6
t7
(1)
t3
t2
prod
i
 Construct a dag for a basic block.
 Process each statement of the block inturn.
 Statement of the form x:=y+z
 Look for the nodes that represent the “current” values of y
and z.
 These could be leaves/interior nodes of dag
 If y & z has been evaluated by previous statements of block.
 We create a node labeled + and give it two children
 Left –node for y right- node for z
5
 Input - basic block.
 Output - label for each node.
label is an identifier for leaves.
label is an operator symbols for interior nodes.
each node has list of identifiers.
 Method - create nodes with one or two children left & right.
create linked list of attached identifiers for each node.
maintain all identifiers for which a node is associated.
node (identifier) represents value that identifier has the
current point in dag construction process.
Symbol table record for identifier- indicate the value of
node(identifier).
6
Given a function node(identifier) – it returns the node currently
associated with the identifier.
Statement types(i) x := y op z (ii)x := op y (iii) x := y
1. If node(y) is undefined then create a leaf labeled y and this is
now node(y) in case of x = y op z; do the same for z.
2.Determine if there is a node labeled “op” with node(y) & node(z)
as the left and right children in case of x = y op z.
3.Determine whether there is a node labeled op ,whose lone child
is node(y) , let n be node(y).
7
S1 = 4 * i
S2 = addr(A)-4
S3 = S2[S1]
S4 = 4 * i
S5 = addr(B)-4
S6 = S5[S4]
S7 = S3 * S6
S8 = prod+S7
prod = S8
S9 = I+1
I = S9
If I <= 20 goto (1)
8
S1 = 4 * i
S2 = addr(A)-4
S3 = S2[S1]
S5 = addr(B)-4
S6 = S5[S4]
S7 = S3 * S6
prod = prod + S7
I = I + 1
If I <= 20 goto (1)
 Consider
following basic
block
t1 = a + b
t2 = c + d
t3 = e –t2
X = t1 –t3
and its DAG
9
-
+
a b
-
e +
c d
X
t3
t2
t1
DAGs are useful for:
 Removing common local sub-expressions.
 Renaming temporaries.
 Finding names used inside the block but
evaluated outside.
 Finding statements in the block that could have
their computed values used outside the block.
 Statements that can be reordered (or executed in
parallel).
10
x :=a[i] x :=a[i]
a[j] :=y (9.5) z :=x  (9.6)
z :=a[i] a[j] :=y
 Both compute different values for z in case i=j and
y!=a[i].When we assign to an array a, we may be
changing the r-value of expression a[i],even though a
and i do not change . It is therefore necessary that
when processing an assignment to array a , we kill
all nodes labeled [],whose left arg is + or – constant.
 *p :=w, where p is pointer. If we do not know
what p might point to , every node currently
in the DAG being built must be killed in the
sense above.
 If p could only point to r or s , then only
node(r) and node(s) must be killed.
 A procedure call in a basic block kills all
nodes , since in the absence of knowledge
about the called procedure , we must assume
that any variable may be changed as a side
effect.
Dag representation of basic blocks