0% found this document useful (0 votes)
10 views

Unit-4 LMD CD

Uploaded by

mayur1000.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit-4 LMD CD

Uploaded by

mayur1000.m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

UNIT 4
CODE OPTIMIZATION AND GENERATION

School of Computer Science Engineering


Darshan L M
[email protected]
AY: 2023 - 2024
BASIC BLOCKS AND FLOW GRAPHS
Basic block is a set of statements that always executes in a sequence one after the other.
A basic block is a sequence of consecutive statements in which flow of control enters at the
beginning and leaves at the end without any halt or possibility of branching except at the end.

I=1

T1=i+10
T2=T1*10

The characteristics of the basic blocks are:


• It do not contain any kind of jump statements or any halt in the middle of the block.
• All the statements execute in the same order they appear.
• They do not lose the flow control of the program.
BASIC BLOCK CONSTRUCTION:

Algorithm: Partition the three-address code into basic blocks


Input: A sequence of three-address statements
Output: A list of basic blocks with each three-address statement in exactly one block
Method:
Step 1: Determine a set of leaders based on the below rules.
• The first statement is a leader
• Any statement that is a target of conditional/unconditional Goto.
• Any statement that immediately follows a goto/conditional Goto.
Step 2: For each leader
Include the leader and all statements upto the next leader or the end of the
program.
THREE ADDRESS CODE

1. It is an intermediate code; the given expression is broken down into several


piece of code.
2. Each Three address code instruction has at most three operands. It is a
combination of assignment and a binary operator.
General format: Y=a op b
Eg: t1=10
t2=t1*a
P= (10*a)+(-c*d)
t3=-c
t4=t3*d
t5=t2+t4
P=t5
FLOW GRAPHS
1. It is a directed graph, where the flow of control among basic block is represented by a
flow graph.
Block: B i=1

T1=i+10
Block: C
T2=T1*10

• The nodes of the graph are the basic blocks.


• There is an edge from block B to block C , if and only if first instruction of block c
immediately follow the last instruction of block B.
• There is a conditional or unconditional jump from the end of B to the beginning of C.
Consider the below piece of three address code, compute its basic blocks and draw the
flow graph.
ENTRY
(1) PROD = 0 PROD = 0
I=1
(2) I = 1 B1 T2 = addr(A) – 4
(3) T2 = addr(A) – 4 T4 = addr(B) – 4

(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

1. There are different variants of Syntax tree


a. Directed acyclic graph using SDD
b. Directed acyclic graph using value number construction
DAG
1. It is an abstract syntax tree used to represent the basic blocks, expressions , and
others for optimization.
e.G
SDD FOR CREATING DAG’S

Production Semantic Rules


1) E -> E1+T E.node= new Node(‘+’, E1.node,T.node)
2) E -> E1-T E.node= new Node(‘-’, E1.node,T.node)
3) E -> T E.node = T.node
4) T -> (E) T.node = E.node
5) T -> id T.node = new Leaf(id, id.entry)
6) T -> num T.node = new Leaf(num, num.val)

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.

• Indirect triple representation: An indirect triple representation consists of an additional array


that contains the pointers to the triples in the desired order.
PROBLEM
Consider the below expression and translate it into quadruple, triple, and indirect triple code.
x=a+b*c
Sol:
Step 1: Convert the above expression into three address code.

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

You might also like