ACD Unit-5
ACD Unit-5
Optimization:
Optimization is a program transformation technique, which tries to improve the code by
making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization,
high-level general programming constructs are replaced by very efficient low-level programming
codes.
It is the process of removing unneeded code from object code or substituting one piece of
code with another to make the object code faster without changing the output of the object code.
• Code optimization is essential to enhance the execution and efficiency of a source code.
• It is mandatory to deliver efficient target code by lowering the number of instructions in a
program.
Objectives of Optimization
• Machine-Independent
• Machine-Dependent
Machine Dependent Machine Independent
Machine dependent code optimization is Machine-independent code optimization is applied
applied to object code. to intermediate code.
Machine dependent optimization involves CPU Machine independent code optimization does not
registers and absolute memory references. involve CPU registers or absolute memory refe
The difference between machine dependent and machine independent code optimization
is that the machine dependent optimization is applied to object code whereas, the machine
independent code optimization is applied to intermediate code.
The expression or sub-expression that has been appeared and computed before and
appears again during the computation of the code is the common sub-expression. Elimination
of that sub-expression is known as Common sub-expression elimination.
The advantage of this elimination method is to make the computation faster and better by
avoiding the re-computation of the expression. In addition, it utilizes memory efficiently.
Example-1
t1=4*i
t2=a[t1]
t3=4*j
t4=4*i
t5=n
t6=b[t4]+t5
The above code can be optimized as
t1=4*i
t=a[t1]
t3=4*j
t5=n
t6=b[t1]+t5
Here the common sub expression t4=4*i is eliminated because its computation is already
done in t1 and value of I is not been changed
Example-2
Before elimination –
a = 10;
b = a + 1 * 2;
c = a + 1 * 2;
d = c + a;
after elimination
a = 10;
b = a + 1 * 2;
d = b + a;
2.Constant Folding
In constant folding the computation of constant is done at compile time instead of execution
time.
Example-1:
int x= 5+7+c;
//Folding applied
int x=12+c;
Example-2:
a=3.14157/2 is replaced by a=1.570
A variable is said to be live in a program if the value contained into it is used subsequently,
otherwise it is said to be dead code. We have to remove that dead code. (or)
When a program snippet is never used in a program it is removed from the program without
affecting the remaining program. This type of elimination is referred to as the dead code
Elimination.
Example-1:
i=0
if(i==1)
{
a=x+5;
}
Here if statement is dead code because this condition will never get satisfied. So we can delete
that if statement.
After eliminating dead code, the result will be
i=0
Example-2:
Before Optimization
int b = 24;
int c = b - 17;
int a = b;
printf(“%d”,b);
In the above code the integers c and a are never used hence they should be eliminated using the
dead code elimination. The optimized code is as follows.
int b = 24;
printf(“%d”,b);
4.Copy Propagation
Ex1:
Pi=3.14, r=5;
Area=pi*r*r;
After
Area=3.14*4*4
Ex:2
Before:
a = 30
b = 20 - a /2
c = b * ( 30 / a + 2 ) - a
After:
a = 30
b = 20 - 30/2
c = b * (30 / 30 + 2) – 30.
1. Code motion
2. Loop unrolling
3. Loop jamming
4. Strength reduction
5. Induction variable elimination
1. Code motion
Code motion is used to decrease the amount of code in loop. This transformation takes a
statement or expression which can be moved outside the loop body without affecting the
semantics of the program.
Example-1
Example-2:
for(i=0;i<=10;i++)
{
Add():
}
After optimization:
for(i=0;i<=5;i++)
{
Add():
Add();
}
3. Loop jamming(Loop Fusion)
Loop jamming is the combining the two or more loops in a single loop. It reduces the time
taken to compile the many number of loops.
Example-1:
Loop Optimizations:
1. Code motion
2. Loop unrolling
3. Loop jamming
4. Strength reduction
1. Structure-Preserving Transformations
2. Algebraic Transformations
Common sub expressions need not be computed over and over again. Instead they can be
computed once and kept in store from where it’s referenced.
Example:
a: =b+c
b: =a-d
c: =b+c
d: =a-d
The 2nd and 4th statements compute the same expression: b+c and a-d
Basic block can be transformed to
a: = b+c
b: = a-d
c: = a
B) Dead code elimination d: = b
Generally, a programmer doesn't introduce dead code intentionally. The dead code may be a
variable or the result of some expression computed by the programmer that may not have any
further uses. By eliminating these useless things from a code, the code will get optimize
Example:
A statement p = q + r appears in a block, and p is a dead symbol. It means that it will never be
used subsequently so that we can eliminate this statement. This elimination does not have any
impact on the values of the basic block.
If a block has two adjacent statements which are independent can be interchanged without
affecting the basic block value.
Example:
t1 = a + b t2 = c + d
t2 = c + d t1 = a + b
These two independent statements of a block can be interchanged without affecting the value
of the block.
2. Algebraic Transformations
A. Constant Folding:
Solve the constant terms which are continuous so that compiler does not need to solve this
expression.
Example:
x = 2 * 3 + y ⇒ x = 6 + y (Optimized code)
B. Copy Propagation:
It is of two types, Variable Propagation, and Constant Propagation.
Variable Propagation:
x=y ⇒ z = y + 2 (Optimized code)
z=x+2
Constant Propagation:
x=3 ⇒ z = 3 + a (Optimized code)
z=x+a
C. Strength Reduction:
Replace expensive statement/ instruction with cheaper ones.
x = 2 * y (costly) ⇒ x = y + y (cheaper)
x = 2 * y (costly) ⇒ x = y << 1 (cheaper)
Peephole Optimization
Peephole optimization is a type of code Optimization performed on a small part of the code. It
is performed on a very small set of instructions in a segment of code.
The small set of instructions or small part of code on which peephole optimization is
performed is known as peephole or window.
For e.g.
Initial code:
MOV R0, a
MOV a.R0
Optimized code:
MOV R0, a
Here we can eliminate second instruction because “a“ is already in R0
3. Strength Reduction: The operators that consume higher execution time are replaced by the
operators consuming less execution time. For e.g.
Initial code:
y = x * 2;
Optimized code:
y = x + x;
5. Use of Machine idioms: The target instructions have equivalent machine instructions for
performing some operations. Hence we can replace these target instructions by equivalent
machine instructions in order to improve the efficiency. For e.g., some machine have auto-
increment or auto-decrement addressing modes that are used to perform add or subtract
operations.
a=a+1 is replaced by inc a;
b=b-1 is replaced by dec b;
2. Reaching definition
A definition D is reaching a point x if D is not killed or redefined before that point. It is generally
used in variable/constant propagation.
Example:
In the above example, D1 is a reaching definition for block B2 since the value of x is not
changed (it is two only) but D1 is not a reaching definition for block B3 because the value of x is
changed to x + 2. This means D1 is killed or redefined by D2.
3. Live variable
A variable x is said to be live at a point p if the variable's value is not killed or redefined by some
block. If the variable is killed or redefined, it is said to be dead. It is generally used in register
allocation and dead code elimination.
Example:
In the above example, the variable a is live at blocks B1,B2, B3 and B4 but is killed at block B5
since its value is changed from 2 to b + c. Similarly, variable b is live at block B3 but is killed at
block B4.
4. Busy Expression
An expression is said to be busy along a path if its evaluation occurs along that path, but none of
its operand definitions appears before it. It is used for performing code movement optimization.
The code generation phase needs complete error-free intermediate code as an input requires.
2. Target program:
The target program is the output of the code generator. The output can be:
c) Absolute machine language: It can be placed in a fixed location in memory and can be
executed immediately.
3. Memory management
In the memory management design, the source program's frontend and code generator
map names address data items in run-time memory. It utilizes a symbol table. In a three-address
statement, a name refers to the name's symbol-table entry. Labels in three-address statements
must be transformed into instruction addresses.
4. Instruction Selection
Nature of instruction set of the target machine should be complete and uniform. If we consider
the efficiency of target machine then the instruction speed and machine idioms are important
factors. The quality of the generated code can be determined by its speed and size.
P:=Q+R
S:=P+T
MOV R0, Q
ADD R0, R
MOV R0, P
MOV R1,P
ADD R1, T
MOV R1, S
5. Register allocation
Register can be accessed faster than memory. The instructions involving operands in register are
shorter and faster than those involving in memory operand. The following sub-problems arise
when we use registers:
• Register allocation: In register allocation, we select the set of variables that will reside in
the register.
• Register assignment: In the Register assignment, we pick the register that contains a
variable.
6. Evaluation order
The efficiency of the target code can be affected by the order in which the computations are
performed. Some computation orders need fewer registers to hold results of intermediate than
others.