CD Unit-5 Full Notes Ok
CD Unit-5 Full Notes Ok
B4: t4=a[t3]
Expression 4 ∗ 𝑖 is the available expression for 𝐵2, 𝐵3 and 𝐵4 because this expression has
not been changed by any of the block before appearing in 𝐵4.
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 7
Reaching Definition
A definition 𝐷 reaches at the point 𝑃 if there is a path from 𝐷 to 𝑃 along which 𝐷 is not
killed.
A definition 𝐷 of variable 𝑥 is killed when there is a redefinition of 𝑥.
D1: y=2 B1
D2: y=y+2 B2
D3: x=y+2 B3
The definition 𝐷1 is reaching definition for block 𝐵2, but the definition 𝐷1 is not reaching
definition for block 𝐵3, because it is killed by definition 𝐷2 in block 𝐵2.
Goto L1
…… Goto L2
L1: goto L2
It may be possible to eliminate the statement L1: goto L2 provided it is preceded by an
unconditional jump. Similarly, the sequence can be replaced by:
If a<b goto L1
…… If a<b goto L2
L1: goto L2
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 5
Algebraic simplification
Peephole optimization is an effective technique for algebraic simplification.
The statements such as x = x + 0 or x := x* 1 can be eliminated by peephole optimization.
The code is thus generated by translating the three address code line by line
MOV a, R0
ADD b, R0
MOV c, R1
SUB d, R1
MOV R0, t t1:= a+b
MOV e, R0 R1 has c-d
ADD R0, R1 /* R1 contains e + (c – d)*/
MOV t1, R0 /R0 contains a + b*/
ADD R1, R0
MOV R0, t4
Now, if the ordering sequence of the three address code is changed
t2:= c - d
t3:= e + t2
t1:= a + b
t4:= t1 + t3
Then, an improved code is obtained as:
MOV c, R0
SUB D, R0
MOV e, R1
ADD R0, R1
MOV a, R0
ADD b, R0
ADD R1, R0
MOV R0, t4
The heuristic ordering algorithm attempts to make the evaluation of a nod the evaluation
of its leftmost argument. 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
end
end
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.
Labeling Algorithm
Labeling algorithm deals with the tree representation of a sequence of three address statements
It could similarly be made to work if the intermediate code structure was a parse tree. This
algorithm has two parts:
The first part labels each node of the tree from the bottom up, with an integer that denotes
the minimum number of registers required to evaluate the tree, and with no storing of
intermediate results
The second part of the algorithm is a tree traversal that travels the tree in an order governed
by the computed labels in the first part, and which generates the code during the tree
traversal
if n is a leaf then
if n is leftmost child of its parents then
Label(n) = 1
else label(n) = 0
else
{
/*n is an interior node*/
let n1, n2, …..,nk be the children of ordered by label,
so label(n1) >= label(n2) >= … >= label(nk);
label(n) = max (label (ni) + i – 1)
}
Example: