Unit-4 LMD CD
Unit-4 LMD CD
UNIT 4
CODE OPTIMIZATION AND GENERATION
I=1
T1=i+10
T2=T1*10
T1=i+10
Block: C
T2=T1*10
(4) T4 = addr(B) – 4
(5) T1 = 4 x I
T1 = 4 x I
(6) T3 = T2[T1] B2
T3 = T2[T1]
(7) T5 = T4[T1]
T5 = T4[T1]
(8) T6 = T3 x T5 T6 = T3 x T5
(9) PROD = PROD + T6 PROD = PROD + T6
(10) I = I + 1 I=I+1
(11) IF I <=20 GOTO (5) IF I <=20 GOTO B2
EXIT
Consider the below piece of three address code, compute its basic blocks and draw the flow
graph.
P=0 B1
(1). P = 0
I=1
(2) I = 1
(3) P = P + I B2
P=P+I
(4) IF P <= 60 GOTO (7) IF P <= 60 GOTO (7)
(5) P = 0
(6) I = 5 B3
P=0
(7) T1 = I * 2 I=5
(8) I = T1 + 1
(9) IF I <= 20 GOTO (3) T1=I*2 B4
(10) K = P * 3 I=T1+1
IF I <= 20 GOTO (3)
K=P*3 B5
OPTIMIZATION OF BASIC BLOCKS
The DAG is a directed acyclic graph used to represent an expression/ a basic block to perform
code optimization.
It is constructed using 3-address code.
Applications:
• To eliminate common sub-expression
• To eliminate dead code
• Reordering of statements
To construct DAG, the rules are:
• Create a node for each initial value of the variables in the block
• The interior nodes always represents operators
• The exterior node always represents name, identifier, or constants
• Create a new node only when it does not exist in any node with the same
value.
Problem: Consider a DAG for the below expression.
(a+b)*(a+b+c)
Sol:
Step 1: The 3-address code:
t1=a+b *
t2=t1+c
+
t3=t1*t2
Step 2: Construct DAG +
a b
Problem: Consider a DAG for the below expression.
(((a+a)+(a+a)) + ((a+a)+(a+a)))
Sol:
Step 1: The 3-address code: t3
+
t1=a+a
t2=t1+t1 t2
+
t3=t2+t2
t1
Step 2: Construct DAG +
a
ELIMINATE LOCAL COMMON SUB EXPRESSION
The expressions that produces the same results are called as common sub
expressions, which are to be removed from the original code.
T4
E.g. T1=4+I T1=4+I
T2=T2+T1 +
T2=T2+T1
T3=4+I T4=T1+T2
T4=T2+T3 T2
T3 +
+
T1
+ T2
4 i
Problem: Consider the below code, draw the DAG and eliminate local common
sub expression.
a=b+c
b=a-d
c=b+c
d=a-d
Sol:
c
Step 1: Construct DAG for the above code. +
a=b+c
b,d d=a-d
- c=d+c
d
a
+
b c0
DEAD CODE
1. Dead cde is one or more than statements are either never executed or un reachable
2. If executed, their output is never used.
E.g.
T1=x+y
C=10 - is a dead code
T2=T1+10
INTER MEDIATE CODE GENERATOR
1. Intermediate code is the interface between front end and back end in a compiler
Code
Static Intermediate
Parser Generato
Checker Code Generator
r
Front end Back end
2. It involves in transforming the source code of a source program into an intermediate representation,
that are either easier to convert into executable code.
t1=id1
t2=id2*id3 Important
T3=t1+t2
Intermediate code
VARIANTS OF SYNTAX TREE
Example:
1) p1=Leaf(id, entry-a)
2) P2=Leaf(id, entry-a)=p1 8) p8=Leaf(id,entry-b)=p3
3) p3=Leaf(id, entry-b) 9) p9=Leaf(id,entry-c)=p4
4) p4=Leaf(id, entry-c) 10) p10=Node(‘-’,p3,p4)=p5
5) p5=Node(‘-’,p3,p4) 11) p11=Leaf(id,entry-d)
6) p6=Node(‘*’,p1,p5) 12) p12=Node(‘*’,p5,p11)
7) p7=Node(‘+’,p1,p6) 13) p13=Node(‘+’,p7,p12)
(a+b) *(a+b+c)+(a+b)
*
+
+
c
a b
T1=T1+3
T2=T2+T1 -
T3=T2-40
+ 3
T1 3
The different forms of IC reepresentation:
a. Three-address code: It is a form of intermediate code representation generated by the compiler
whose general form is
Y=op1 operator op2
b. The different 3-address code forms are:
• Assignment statement: The three address code for the assignment statement is given by
a = b opr c
e.g. : t1=a+b
• Copy statement: The three address code for the copy statement is of the form
x=y
• Conditional jump: The three address code for the conditional jump is of the form
if x relop y
goto lablel
• Unconditional jump: The three address code for the unconditional jump is given by,
goto label;
• Procedure call: The three address code for the function call p(x1,x2,x3…xn) is given by,
function name
param list
call p,n
• Quadruple representation: In quadruples representation, each instruction is splitted into
the following 4 different fields:
• Triple representation: In triples, for representing any three-address statement three fields are
used, namely, operator, operand 1 and operand 2,
where operand 1 and operand 2 are pointers to either symbol table or they are
pointers to the records (for temporary variables) within the triple representation itself.
1. t1 = b * c
2. t2 = a + t1
3. x = t2
Step 2: Represent the three address code in quadruple, triple, and indirect triple code.
• Quadruple Triple Indirect triple
PROBLEM-1
Consider the below expression and translate it into quadruple, triple, and indirect triple code.
X = - (a + b) * (c + d) + (a + b + c)
Sol:
Step 1: Convert the above expression into three address code.
t1 = a + b
t2 = -t1
t3 = c + d
t4 = t2 * t3
t5 = t1 + c
t6 = t4 + t5
X = t6
Step 2: Represent the three address code in quadruple, triple, and indirect triple code.
• Quadruple:
Indirect triple:
• Triple:
Pointer
Array
PROBLEM-2
Consider the below expression and translate it into quadruple, triple, and indirect triple code.
S = -z/a * (x + y)
Step 1: Convert the above expression to three address code.
t1= x + y
t2 = a * t1
t3 = - z
t4 = t3/t2
S = t4
Step 2: Represent the three address code in quadruple, triple, and indirect triple code.
• Quadruple:
• Triple:
• Indirect triple:
Ptr Array
201 0
202 1
203 2
204 3
205 4
PROBLEM: 3
Consider the below expression and translate it into quadruple, triple, and indirect triple
code.
a + b * c / e ^f + b * a
Sol:
Step 1: Convert the expression to three address code.
T1 = e ^ f
T2 = b * c
T3 = t2 / t1
T4 = b * a
T5 = a + t3
T6 = t5 + t4
Step 2: quadruple
• Quadruple:
• Triple:
• Indirect triple:
PROBLEM-4
Generate the three-address code for the following program segment.
main( )
{
int k = 1;
int a[5];
while (k <= 5)
{
a[k] = 0; k++;
}
} 1. k: = 1
2. if k <= 5 goto (4)
The three-address code for the given program segment is given below: 3. goto (8)
Sol: 4. t1: = k * width
5. t2: = addr(a)-width
6. t2[t1]: = 0
7. t3 : = k + 1
8. k: = t3
9. goto (2)
10. Next
PROBLEM-5
Translate the following program segment into three-address statements:
for (k = 1; k <= 12; k++)
if x < y then
a = b + c;
Sol:
1. k: = 1
2. if k <= 12 goto (4)
3. goto (11)
4. if x < y goto (6)
5. goto (8)
6. t1: = b + c
7. a: = t1
8. t2: = k + 1
9. k: = t2
10. goto (2)
11. Next
DISCUSSION
5 MINUTES