r20 CD Unit-5 Part 1
r20 CD Unit-5 Part 1
What is code optimization: The code optimization is required to produce an efficient target code.
Issues in code optimization:
1. The semantic equivalence of the source program must not be changed.
2. The improvement over the program efficiency.
Advantages:
1. The intermediate code can be optimized to produce efficient target code.
2. The code optimization allows faster execution of source program.
Consider the input source and the corresponding intermediate code in TAC format in below table.
The above table line (1) , (4) and(6) (9), are the same. The intermediate code compute the value of the
common sub expression i*8 and j*8. It is possible to optimize the intermediate code to have common sub
expressions computed only once in the function.
1| SIET D e p a r t me nt o f C S E
(0) proce_begin
(1) t0 = i*8
(2) t1=a[t0]
(3) x=t1
(4) t3=a[t0]
(5) t4=j*8
(6) t5 =a[t4]
(7) t3=t5
(8) t7=a[t4]
(9) t7=x
(10) proce_end
This process of identifying common sub expressions and eliminating their computation multiple times
in the intermediate code is known as common-sub expression elimination.
Copy propagation:-
Copy propagation is another commonly used transformation in order to improve the intermediate
code. In copy propagation, the use of the variable ‘y’ instead of ‘x’ is propagated in the stmts
following a copy stmt x=y.
(0) Proc_begin main
(1) t0 =0
(2) t1 =&arr1
(3) t1[t0] =3
(4) t2 =4
(5) t3=&arr1
(6) t3[t2] =4
(7) proce_end
In the above table, there are two assignment stmts (are called as copy stmts). They are
a. The assignment stmt(1) where the temporary variable t0 is assign 0.
b. The assign stmt(4), where the temporary variable t2 is assigned 4.
The use of the value 0 can be propagated in the place of t0 in the stmts following the assignment at
stmt(1). In other words, the variable t0 can be replaced with 0 in stmt(3). Similarly, the variable t2
can be replaced with value 4 in stmt(6) following the assignment at stmt(4). The resultant
intermediate code after the copy propagates is shown below table.
(0) proc begin main
(1) t0 =0
(2) t1 = &arr1
(3) t1[0] =3
(4) t2 =4
(5) t3 =& arr1
(6) t3[4] =4
(7) proc end main
The stmt(3) where t0 has been replaced with 0 and stmt(6) where t2 has been replaced with 4.
Elimination of Dead Code:-
Copy propagation does not vastly improve the quality of intermediate code. We will now see how copy
propagation facilitates an optimizing transformation called dead store elimination to be performed on the
resultant code.
(0) proc begin main
(1) t0=0
(2) t1 -&arr1
(3) t1[0] =3
(4) t2=4
(5) t3=&arr1
(6) t3[4]=4
(7) proc end main
2| SIET D e p a r t me nt o f C S E
In the intermediate cod shown in above table, the assignment stmt (1) can be eliminated, because t0 is no
longer used in any of the stmts following the assignment. similarly, the assignment stmt(4) can also be
eliminated. since t2 is no longer used in any of the stmts following the assignment.
1) Code motion:- Code motion reduces the number of instructions in a loop by moving instruction
outside a loop.
Ex:
while(x!=n-2)
{
x=x+2;
}
In the above loop, the value evaluated by the expression 'n-2' is independent of number of times the while
loop is executed. Now the expression n-2 can be written before the while loop begins as shown below
3| SIET D e p a r t me nt o f C S E
Ex: m=n-2;
while(x!=m)
{
x=x+2; }
4| SIET D e p a r t me nt o f C S E
int i=1;
while(i<=100)
{
a[i]=b[i];
i++; }
After optimization
int i=1;
while(i<=100)
{
a[i]=b[i];
i++;
a[i]=b[i];
i++;
}
5.Loop fusion:
In loop fusion method several loops are merged to one loop.
for i=1 to n do
a[i]=10 for i=1 to n do
for j=1 to n do a[i]=10
b[j]=20 b[i]=20
Basic Blocks:
Basic Block is a sequence of three-address instructions. We begin a new basic block with the first
instruction and keep adding instructions until we meet a jump, a conditional jump, or a label on the
following instruction.
Algorithm: Partitioning three-address instructions into basic blocks.
INPUT: A sequence of three-address instructions.
OUTPUT: A list of the basic blocks
METHOD:
1. The first three-address instruction in the intermediate code is a leader.
2. Any instruction that is the target of a conditional or unconditional jump is a leader.
3. Any instruction that immediately follows a conditional or unconditional jump is a leader.
Note: Refer class notes for example
Control Flow graphs:-
A flow graph is a graphical representation of three address statements. It is used to show the flow of
control information to the set of basic blocks of a program. It consists of nodes and edges. A node of
a flow graph is basic block that performs some computations.
}
5| SIET D e p a r t me nt o f C S E
(0) Proc begin
(1) V3 = v1+v2 B0
(2) If c>100 goto L o
(3) goto L1 B1
(4) Label L0
(5) V4 =v1 +v2
(6) v1 =0 B2
(7) Label L1
(8) V5 =v1 +v2 B3
Each node in a flow graph is a basic block. The starting node in a flow graph is called as initial node.
This is the basic block whose leader is the first TAC stmt in the procedure. The node B0 is the initial
node. The flow of control can go to back B1 or B2 from the block b0 .
Instruction Scheduling :- Using DAG we can rearrange some sequence of instructions and
generate an efficient code using minimum number of registers. The order of three address code
affects the cost of object code being generated. Changing the order in which computations are done
we can obtain the object code with minimum cost. Thus code optimization can be achieved by
scheduling the instructions in proper order. The technique of code optimization done by
rearrangement of some sequence of instructions is called Instruction Scheduling.
Now if we change the ordering sequence of the above three address code.
6| SIET D e p a r t me nt o f C S E
In the first case the assembly code contains 10 lines. After rearranging the three address code
sequence then the assembly code contains 8 lines. So by rearranging some sequence of instructions
we can generate an efficient code using minimum number of registers. Thus here, an optimal order
means the order that yields the shortest instruction sequence.
7| SIET D e p a r t me nt o f C S E
( 0) proc_fun
(1) t1=v1+v2 B0
(2) v3=t1
(3) If c>100 goto L0
(5) Label L0
(4)goto L1 B1 (6) t2 =v1+v2 B2
(7) v4=t2
(8) t3=v1+v2
(9) t4=t3+v3
(10) v5=t4
(9) t4=t3+v3
(10) v5=t4
(11) Label L1
(12) t5=v1+v2 B3
(13) v6=t5
Now we apply the transformations on block B2 .In block B2 there are common sub expressions
v1+v2. we will remove these common sub expressions and the code will be
(5) Label L0
(6) t2 =v1+v2 B2
(7) v4=t2
(9) t4=t2+v3
(10) v5=t4
Thus the local transformation to block B2 will be completed. Now we will apply the global
transformations: on B3. As v1+v2 is already computed as t2 in B2, so we can replace t2 by t1.
Similarly as v1+v2 is already computed as t5, so t5 can be replaced by t1. Hence t5 can be
eliminated.
(11) Label L1
(13) v6=t1
B3
8| SIET D e p a r t me nt o f C S E