Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Jai Gomathi.

NS III CSE A

Rearranging the order of final computation

than starting from a linear sequence of 3 address statements and quadruples(where dag is a tree).
The generated code is optimal.

Rearranging the order


Consider the following basic block whose dag representation is shown in the figure t1= a + b t2=c + d t3=e t2 t4=t1-t3
Expression: (a+b)-(e-(c+d))

Rearranging

Assume R0 and R1 is available and t4 is live on exit

A Heuristic ordering for DAGS


Heuristic Ordering Algorithm : The evaluation of the node immediately follows the evaluation of its leftmost argument.

Algorithm
while unlisted interior nodes remain do begin select an unlisted node n, all of whose parents have been listed ; list n; while the leftmost child m of n has no unlisted parents and is not a leaf do /* since n was just listed , m is not yet listed*/ begin list m; n=m end end

Example

List=1234568

Optimal ordering of trees


Yields the shortest instruction sequence It has 2 parts First part:

Labels each node of the tree bottom up, with an integer that denotes the fewest no. of registers required to evaluate the tree with no stores of information. Second Part: Tree traversal whose order is governed by computing nodes

Labelling algorithm
label (n) =

max (l1, l2) if l1 != l2 l1 + 1 if == l1=l2

Algorithm: if n is a leaf then if n is leftmost child of its parents then Label (n) = 1 else label (n) = 0 else begin /* n is an interior node */ let n1, n2 , . , nk be the children of n ordered by label , so label (n1) >= label (n2) >= .>= label (nk) ; label (n) = max1<i<k (label(ni) + i - 1) end

Example

Code Generation from labelled tree

Print MOV a,Ro print ADD b,Ro print SUB R1,Ro


An optimal code must perform: 1. An operation for each interior node. 2. A load for each leaf which is the leftmost child of its parent. 3. A store for every node both of whose children have labels equal or greater than r.

You might also like