CD Unit-6
CD Unit-6
(CE6003)
Unit – 6
Code Generation
and Code
Optimization
t := a +
b
t := t * c
Thet shortest
:= t / d
assembly code sequence will be
MOV a,
R0
ADD b,
R0
MUL c,
R0
DIV d,
R0 CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 13
Choice of evaluation
The order in which computations are performed can affect the
efficiency of the target code.
Some computation orders require fewer registers to hold
intermediate results than others.
Picking a best order is one of the difficulty in code generation.
Mostly, we can avoid this problem by generating code for the
three-address statements in the order in which they have been
produced by the intermediate code generator.
3 4
6
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 42
Inner Loops
The inner loop is a loop that contains no other loop.
Here the inner loop is 42 that mean edge given by 2-3-4.
3 4
2 3
Example:
(1) t1 := 4*i t6, prod
(2) t2 := a [t1] +¿
(3) t3 := 4*i
(4) t4 :=b [t3]
prod ∗ t5
t4
(5) t5 := t2*t4 [ ]t 2
[] t1,t3
(6) t6 := prod
+t5
(7) prod := t6
∗ +¿ t7, i
a b
4 i 1
(8) t7 := i+1
(9) i := t7
t1:=a+b t2:=c+d
t2:=c+d Re-arrange
t3:=e-t2
t3:=e-t2 t1:=a+b
t4:=t1-t3 MOV a, R0
Three Address
t4:=t1-t3
Code
ADD b, R0 Three Address
MOV c, R1 Code MOV c, R0
ADD d, R1 ADD d, R0
MOV R0, t1 MOV e, R1
MOV e, R0 SUB R0, R1
SUB R1, R0 MOV a, R0
MOV t1, R1 ADD b, R0
SUB R0, R1 SUB R1, R0
MOV R1, t4 MOV R0, t4
Assembly Code Assembly Code
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 58
Heuristic Ordering
The heuristic ordering algorithm is as follows:
1. Obtain all the interior nodes. Consider these interior nodes as unlisted
nodes.
2. while(unlisted interior nodes remain)
3. {
4. pick up an unlisted node n, whose parents have been listed
5. list n;
6. while(the left child m of n has no unlisted parent AND is not leaf)
7. {
8. List m;
9. n=m;
10. }
11. }
𝟏
−𝟓 𝟖 +¿ Leftchild
Leftchild of
Parent 5 is 1not
Parent
of 21==
listed so
is listed 𝟔
𝟐
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 can’t list26
so list
𝟗𝑎 𝑏 10
Listed 1 2
Node
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 60
Example: Heuristic Ordering
∗𝟏
𝟐
+¿ − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Leftchild of 1
Rightchild
Parent
Parent2,3
1 isare
3=
𝟑
𝟒
listed
listed
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 solist
so list34
𝟗𝑎 𝑏 10
Listed 1 2 3 4
Node
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 61
Example: Heuristic Ordering
∗𝟏
𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
𝟐
+¿ − 𝟑
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Leftchild of 4
Parent 2,54are
Parent
5=
𝟔
𝟓
listed so
is listed
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 solist
list6 5
𝟗𝑎 𝑏 10
Listed 1 2 3 4 5 6
Node
∗𝟏
𝟐
+¿ − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Rightchild of 4 =
Parent 4 is listed 𝟖
+¿
so list 8
𝟔 𝟕𝑐 𝑑11 𝑒 12
𝟗𝑎 𝑏 10
Listed 1 2 3 4 5 6 8
Node
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 63
Example: Heuristic Ordering
∗𝟏
Listed 1 2 3 4 5 6 8
𝟐
+¿ −𝟑 Node
Reverse Order for three address code = 8 6
54321
∗𝟒 t8=d+e
t6=a+b Optim
t5=t6-c alThre
−𝟓 𝟖+¿ t4=t5*t8
t3=t4-e
e
Addre
+¿
ss
𝟔 𝟕𝑐 𝑑11 𝑒 12 t2=t6+t4
t1=t2*t3
code
𝑎 𝑏 10
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 64
Labeling Algorithm
Labeling algorithm is used by compiler during code generation
phase. Basically, this algorithm is used to find out how many
registers will be required by a program to complete its execution.
Using labeling algorithm the labeling can be done to tree by
visiting nodes in bottom up order.
For computing the label at node n with the label L1 to left child
and label L2 to right child as,
t1:=a+b 𝑳𝒂𝒃𝒆𝒍(𝒏)=¿
t2:=c+d
t3:=e-t2
t4:=t1-t3 𝟐t
Three Address −4
Code 𝟐t
t
1
−3
𝟏
+¿ 𝟏e
𝟏t
+¿ 2
𝟏a b𝟎
𝟏c d𝟎
𝒑𝒐𝒔𝒕𝒐𝒓𝒅𝒆𝒓 𝒕𝒓𝒂𝒗𝒆𝒓𝒔𝒂𝒍=𝒂𝒃𝒕𝟏𝒆𝒄𝒅𝒕𝟐𝒕𝟑𝒕𝟒
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 66
Code Optimization
Machine independent optimization
Code Optimization
Code Optimization is a program transformation technique which tries to
improve the code by eliminating unnecessary code lines and arranging the
statements in such a sequence that speed up the execution without
wasting the resources.
Advantag
es
1. Faster execution
2. Better performance
3. Improves the efficiency
t1 := 4 * i
t2 := a + 2
t3 := 4 * j
t4 : = 4 * i
t5:= n
t6 := b[t4]+t5
Before
After
Optimization
CD Unit 6 – Code Generation and Code
Prof. Jay R Dhamsaniya #3130006 (PS) Unit 1 – Basic Probability 71
Copy Propagation
Copy propagation means use of one variable instead of
another.
Example:
x = pi;
area = x * r * r;
area = pi * r * r;
While(i<=ma N=max-1;
x-1) While(i<=N)
{ {
sum=sum+a[ sum=sum+a[i];
i]; Before } After
}Optimization Optimization
Goto L1
…… Goto L2
L1: goto L2