0% found this document useful (0 votes)
8 views13 pages

Code Generation and Optimization in Compiler Techniques: January 2017

The document discusses compiler techniques, focusing on intermediate code generation, code optimization, and code generation phases. It explains the three-address code method for representing code, optimization strategies for improving performance, and the final code generation process that translates intermediate representations into target language. The author, Dr. Qasim Mohammed Hussein, emphasizes the importance of preserving program semantics while optimizing for efficiency and resource usage.

Uploaded by

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

Code Generation and Optimization in Compiler Techniques: January 2017

The document discusses compiler techniques, focusing on intermediate code generation, code optimization, and code generation phases. It explains the three-address code method for representing code, optimization strategies for improving performance, and the final code generation process that translates intermediate representations into target language. The author, Dr. Qasim Mohammed Hussein, emphasizes the importance of preserving program semantics while optimizing for efficiency and resource usage.

Uploaded by

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

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/312120590

Code Generation and Optimization in Compiler techniques

Data · January 2017


DOI: 10.13140/RG.2.2.26091.95527

CITATIONS READS

0 450

1 author:

Qasim Mohammed Hussein


Al-Kunooze University College
108 PUBLICATIONS 157 CITATIONS

SEE PROFILE

All content following this page was uploaded by Qasim Mohammed Hussein on 06 January 2017.

The user has requested enhancement of the downloaded file.


Compiler Techniques Dr. Qasim Mohammed Hussein

This part includes brief


discussion about the phases of
compiler structure
- Intermediate code generation
- Code optimization
- Code generation
Compiler Techniques Dr. Qasim Mohammed Hussein

3. Intermediate code generation phase


In the process of translating a source program into target code, a compiler may
construct one or more intermediate representations. Many kinds of intermediate
representation are used such as Syntax tree, postfix notation and three address
code.
Three - address code method
Three - address code is a sequence of statements of the general form

Where X, Y and Z are names, constants, or compiler – generated temporaries;


op stands for any operator. There is only one operator on the right side of a
statement. Thus the expression " x + y * z" might be translated into a sequence:
t1 = y*z
t2 = x + t1
Where t1 and t2 are compiler – generated temporary names.
Example 1
Find the three - address code for a= b * – c + b * – c ?
Solution
t1 = – c
t2= b*t1
t3 = – c
t4 = b * t3
t 5 = t2 + t4
a = t5

Control statements such as while – do or if – then are transform into the lower
level conditional three – address statements.
Example 2
Find the three – address code for the following program segment?
Compiler Techniques Dr. Qasim Mohammed Hussein

while (A > B ) and ( A <= 2*B-5) do


A = A+B
Solution
Three - address Two operator
L1:if A>B A B L2 if >
goto L2
goto L3 L3 goto
L2:T1=2*B T1 2 B = *
T2 = T1 – 5 T2 T1 5 = –
if A<=T2 A T2 L4 if <=
goto L4
goto L3 L3 goto
L4: A=A+B A A B = +
goto L1 L1 goto –
L3

The reason for the t4.erm ' three-address code is that each statement usually
contains three addresses, two for operands and one for the result.

Example 3: Find the three – address code for the following program segment?
if (a > b+5*f ) or ( a <= 2*b-7) do
{ a = a+b * i;
b= b/3 +f;
}
f= f*2;
Solution
t1 = 5*f
t2= b + t1
Compiler Techniques Dr. Qasim Mohammed Hussein

if a > t2 goto L1
t3= 2*b
t4= t3-7
if a<= t4 goto L1
goto L2
L1: t5= b * i;
a = a + t5;
b = b+f;
L2 : f = f*2l
Compiler Techniques Dr. Qasim Mohammed Hussein

4. Code Optimization
Code optimization phase uses to improve the intermediate code so that the
object program run faster and take less memory. This options available to a
programmer and a complier for creating efficient target program.
The transformation provided by an optimizing compiler should have the
properties:
1. A transformation must preserve the meaning of the programs (the
optimization must not change the output produced by a program.
2. A transformation must speed the program by a measurable amount.
3. A transformation must be worth the effort
No compiler can find the best algorithm for a given program. Sometimes, a
compiler can replace a sequence of operations by an algebraically equivalent
sequence, and thereby reduce the running time of a program significantly.
There are two types of optimization: local optimization and loop optimization.
1. Local optimization.
The transformation is called local if it can be performed by looking at the
statements in a basic block.
Example 4: Optimize the statements?
A = B+C+D;
E = B+C +E;
Solution
The intermediate code (three address code) is:
T1 = B+C
A = T1 +D
T2 = B+C
E = T2 + E
We see that results of T1 and T are same, so in optimization, we calculate only
one time.
T1 = B+C
Compiler Techniques Dr. Qasim Mohammed Hussein

A = T1+D
E = T1+E
Also, optimization can be done by eliminating common sub expression.
Common sub expression are rarely written explicitly by the programmer but in
some cases generated by the compiler by subscript calculation.
Example
A[I] = B[I] + C[I] where A,B, and C real array variable.
You will recall this requires [base address array + (I+1)*2] to be evaluated and
in the above (i + 1)*2 will be evaluated three time. Optimization would evaluate
it one and then add base address A, base address B , and base address C to the
result to obtain the required elements address in A[I] , B[I] , C[I], the
programmer is unable to specify this in the source program.
2. Loop optimization
Loop are especially good target for optimization because programs spend most
of their time in inner loop. The running time of a program may be improved if
we decrease the number of instructions in an inner loop.
Typical improvement is to remove a computation that produces the same result
each time round the loop to a point just before entry to the loop. Such in
computation is called loop invariant.
Example 5: Optimize the following program segment
for (i=1; i<=100, i++)
if (i+j*k) > 0) m=m+i;
Solution:
The expression j*k does not change its value during execution of the loop, so it
move out the loop.
t1 = j*k
for (i=1; i<=100, i++)
if (i+t1) > 0) m=m+i;
Compiler Techniques Dr. Qasim Mohammed Hussein

Also, we can reduce the number of loop iterations if the number of the
iteration is constant
Example 6: Optimize the following loop?
while (i<= 100) do
a[i] = 0;
Solution
i=1;
while i<=100
{
a[i] = 0;
i=i+1;
a[i] =0;
i=i+1
}

The problem of this method, the number of iteration must be accept the division
on number of iteration in the loop. For example, we can perform this operation
twice for the following loop.
While i<=100
{
A[i] = 0;
i=i+1;
}

Example 7: Optimize the following program segment?


for ( j=1; j<=15; j++)
{
m = j/2*b-c/a;
d = a/5;
n=b–m;
}
Compiler Techniques Dr. Qasim Mohammed Hussein

Solution
T1 = 2 * b
T2 = c/a These statements does not change their values in the loop.
d = a/5
for ( j=1; j<=15; j++)
{
T3 = j/T1;
m = T3 – T2;
n=b–m;
i++; Increment the loop variable
T3 = j/T1
m = j/T1-T2; Repeat the loop body
n=b–m;
i++;
T3 = j/T1;
m = j/T1-T2;
n=b–m;
}

Note 1: The number of iterations are 15. So, we can repeat the loop body 3 or 5
times.
Note 2: We must increase the loop variable value before each repeating.
Example 8: Optimize the following segment program?
for (i=1; i<= 9; i++)
{
a= b + c * i;
d = a – b +c;
cout<<a;
}
m = b+c+ a;
Compiler Techniques Dr. Qasim Mohammed Hussein

Solution
t1= b + c
For (i=1; i<=9; i++) Note: the body of the loop
{ must repeated 3 times
a = t1 * i;
d = a – t1
i++
a = t1 * i;
d = a – t1
i++
a = t1 * i;
d = a – t1
}
Compiler Techniques Dr. Qasim Mohammed Hussein

5. Code generation phase


Code generation is the final phase of the compiler structure. The code
generator takes as input an intermediate representation of the source program
and maps it into the target language. If the target language is machine code,
registers 0r memory locations are selected for each of the variables used by the
program. Then, the intermediate instructions are translated into sequences of
machine instructions that perform the same task.

The target program must preserve the semantic meaning of the source
program and be of high quality; that is, it must make effective use of the
available resources of the target machine. Moreover, the code generator itself
must run efficiently

Object code depend on target language, OS (memory management, instruction


selection, register allocation, and evaluation order).

There are several choices for the intermediate language, including:

1. Linear representation such as postfix notation.


2. Three – address code representation such as quadruples.
3. Virtual machine representation such as stack machine code.
4. Graphical representation such as syntax tree.
Intermediate language can be represented by quantities that the target machine
can directly manipulate (bits, integer, float, pointer).
The input must be free from errors.
This phase deal directly with computer architecture, so we need knowledge
about the details of computer.
The output of the code generation is the target program. The output may
takes on a variety forms:
1. Absolute machine language. It has the advantages that it can be placed
in a fixed location in memory and intermediately executed.
Compiler Techniques Dr. Qasim Mohammed Hussein

2. Relocatable machine language. It allows subprograms to be compiled


separately. A set of relocatable object modules can be linked together
and loaded for execution by a linking loader.
3. Assembly language. We can generate symbolic instructions and use the
macro facilities of the assembler to help generate code.
The quality of generation code is determined by its speed and size. The
instruction set of the target machine determines the difficulty of instruction
selection. The target machine with a rich instruction set provide several ways of
implementation.
A code generator has three primary tasks: instruction selection, instruction
ordering, and register allocation and assignment.

The problems that are associated with code generation are:


1. What instruction should be generate?
For example X= X+1 can performed as follow:
MOV X ,R0
ADD #1 ,R0
MOV R0 ,X
But, if the INC (increment) instruction is available the
X=X+1 can be performed by:
INC X
2. What order should we perform computation.
For example
T = A+B
T = T*C
T = T/D

LOAD R1, A
ADD R1,B
Compiler Techniques Dr. Qasim Mohammed Hussein

MUL R0 , C { MUL = MULTIPLY}

DIV R0 , D
STOR R1, T
Example 2
T = A+B
T = T+C
T=T/D

LOAD R0,A
ADD R0,B
ADD R0,C
SR R0,32 { SR = shift right }
DIV R0, D
STORE R1,T

3. What register should we use?


The use of registers is often subdivided into two sub problems:

a) Register allocation, during which we select the set of variables that will
reside in registers at each point in the program.

b) Register assignment, during which we pick the specific register that a


variable will reside in.

View publication stats

You might also like