Professional Documents
Culture Documents
Acd 5
Acd 5
optimization techniques:
Machine-dependent code optimization is applied to object code. Machine-independent code
optimization is applied to intermediate code. Machine-dependent optimization involves CPU
registers and absolute memory references. Machine-independent code optimization does not
involve CPU registers or absolute memory references.
Improved performance
Greater control
Reduced portability
Portability
Improved security
Improved performance
Disadvantages of machine-dependent code:
Lack of portability
Reduced performance
Machine-dependent
Complexity
Reduced performance
Additional dependencies
------------------------------------------------------------------------------------------------
Code Generator:
OR
Answer:
A code-generation algorithm:
Code generator is used to produce the target code for three-address statements. It uses
registers to store the operands of the three address statement.
The algorithm takes a sequence of three-address statements as input. For each three
address statement of the form x= y op z perform the various actions. Assume L is the
location where the output of y op z to be stored.
1. Call a function getreg to find out the location L where the result of computation y op z
should be stored.
2. Determine the current location of y by consulting the address descriptor of y. If y is not
presented in the L then generate the instruction MOV y’, L to place a copy of y to L.
3. Generate the instruction OP z' , L where z' is used to show the current location of z.
Update the address descriptor of x to indicate that x is in location L. If x is in L then
update its descriptor and remove x from all other descriptor.
4. If the current value of y or z have no next uses or not live on exit from the block or in
register then alter the register descriptor to indicate that after execution of x : = y op z
those register will no longer contain y or z.
t:= a-b
u:= a-c
v:= t +u
d:= v+u
Code sequence for the example is as follows:
-----------------------------------------------------------------------------------------
Design Issues
In the code generation phase, various issues can arises:
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
o During code generation process the symbol table entries have to be mapped to actual p
addresses and levels have to be mapped to instruction address.
o Mapping name in the source program to address of data is co-operating done by the
front end and code generator.
o Local variables are stack allocation in the activation record while global variables are in
static area.
4. Instruction selection:
o Nature of instruction set of the target machine should be complete and uniform.
o When you consider the efficiency of target machine then the instruction speed and
machine idioms are important factors.
o The quality of the generated code can be determined by its speed and size.
Example:
The Three address code is:
a:= b + c
d:= a + e
MOV b, R0 R0→b
ADD c, R0 c + R0
MOV R0, a a → R0
MOV a, R0 R0→ a
ADD e, R0 R0 → e + R0
MOV R0, d d → R0
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.
Register allocation: In register allocation, we select the set of variables that will reside
in register.
Certain machine requires even-odd pairs of registers for some operands and result.
For example:
Consider the following division instruction of the form:
D x, y
Where,
y is the divisor
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.
--------------------------------------------------------------------------------------------------------------------------------
Peephole optimization
Peephole is a small, moving window on the target program.Peephole optimization is a simple
and effective technique for locally improving target code. This technique is applied to improve
the performance of the target program by examining the short sequence of target instructions
(called the peephole) and replace these instructions replacing by shorter or faster sequence
whenever possible.
For e.g.
Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;
Optimized code:
y = x + 5;
i = y;
w = y * 3;
2. Constant folding:
The code that can be simplified by user itself, is simplified.
For e.g.
Initial code:
x = 2 * 3;
Optimized code:
x = 6;
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;
4. Algebraic Simplification:
Peephole optimization is an effective technique for algebraic simplification. The statements such
as x = x + 0 or x = x * 1 can eliminated by peephole optimization.
5. 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.
EX: a=a+1 can be performed by Inc a