0% found this document useful (0 votes)
54 views22 pages

Unit-5: Error Handling, Code Optimization, and Target Code Generation

This document discusses code optimization and error handling in compilers. It describes five techniques for code optimization: constant propagation, common subexpression elimination, dead code elimination, code movement, and strength reduction. It also discusses loop optimization techniques like frequency reduction, loop unrolling, and loop jamming. For machine-dependent optimization, it covers register allocation, addressing modes, and peephole optimization. The document also categorizes compiler errors as lexical, syntax, semantic, and logical errors and provides some examples.

Uploaded by

ash singh
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)
54 views22 pages

Unit-5: Error Handling, Code Optimization, and Target Code Generation

This document discusses code optimization and error handling in compilers. It describes five techniques for code optimization: constant propagation, common subexpression elimination, dead code elimination, code movement, and strength reduction. It also discusses loop optimization techniques like frequency reduction, loop unrolling, and loop jamming. For machine-dependent optimization, it covers register allocation, addressing modes, and peephole optimization. The document also categorizes compiler errors as lexical, syntax, semantic, and logical errors and provides some examples.

Uploaded by

ash singh
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/ 22

Unit-5

Error Handling, Code Optimization, and Target


Code Generation
Code Optimization
• It is a technique which tries to improve the code by eliminating
unused and unnecessary lines.
• Results in improvisation of execution time.
Adv-
1. Utilize memory efficiently
2. Better performance
Code Optimization
Types of optimization-
1. Machine independent (mostly used)
2. Machine dependent
Code Optimization
5 ways of code optimization
1. Compile time execution

This techniques is also called a constant replacement or constant


folding or constant propagation.

Example – float pi =3.14


Area = pi * r* r; // no use to write pi.
Can be replaced as – area=3.14 * r * r // no entry in AR and memory fetch
Code Optimization
2. Common sub-expression evaluation (Elimination)-
This techniques says that eliminate all the expressions, which are already computed
and used again in the code. (DAGs are specialized data structure in ICG to eliminate
CSEE)
Example 1–
x=a+b
y=a+b*c

Example 2- (Copy propagation)


c=a*b
y=a * b
z=y+10

Optimized code-
Code Optimization
Example 3-
x=t1
A[t2]=t3
A[t3]=x
If x>2 then goto L1
Optimized code-

Example 4–
A=20
B=3-a/2
Optimized code-
Code Optimization
3. Dead code elimination-
This techniques says that eliminate all the expressions, which are not used in
the code.
Example –
c=a*b
y=a
z=c+10
Optimized code- ?
int x=0;
if(x){
}
Optimized code- ?
Code Optimization
4. Code Movement
If any code which is written inside the loop, but can be moved out from the
loop and make no effect on final result.
Example –
int a=10;
while(a>=0)
{
x=b+c;
if(a%x==0)
printf(a)
a=a-1;
}
Optimized code ?
Code Optimization
5. Strength Reduction
Replace high strength operator with low strength.
Example –
int a=b*2;
Optimized codes?
Code Optimization
LOOP Optimization
1. Detect the loops.
2. Use control flow analysis (CFA) using program flow graph (PFG).
3. Find basic blocks for PFG.
Basic block –
These are sequence of statements written in 3-address code, where control will enter at the start and
leaves at the end without any jump.
4. Find the leaders in the code.
Leaders finding-
4. 1st statement is always leader
5. Statement number written with goto is a leader
If cond goto 10 or goto 1
3. Statement after the condition or unconditional goto is a leader.
Code Optimization
Example-
Code Optimization
Techniques for Loop Optimization-
1. Frequency Reduction- reduce number of lines if possible
2. Loop unrolling – try to reduce number of loops.
3. Loop Jamming – try to combine two or more loops.

Example 1-
i=1;
while(i<=10)
{
a=(x/y) * 10;
i++;
}
Code Optimization
Example 2- (Can we reduce number of times?)
i=1;
while(i<=10)
{
x[i]=0
i++
}
Example 3- (Can we reduce number of loops?)
For(i=0;i<3;i++)
for(j=0;j<5;j++)
x[i,j]=0;
For(i=0; i<3 ;i++)
x[i,i]=i+1;
Code Optimization
Machine Dependent Code Optimization-
ICG –TCG-CO
- It uses processor registers
1. Register allocation – example – a=b*c + d
2. Use of addressing Mode- try to follow addressing mode with less memory accessing is used.
3. Peephole Optimization – it works on the theory of replacement. Try to reduce load and store. Remove dead codes. (Improve performance, reduce
memory access, reduce size of code).
y=x+5
i=y
z=i
w=z+3
MOV R1,X
ADD R2, R1
MOV Y, R2
MOV I, Y
MOV Z,I
MOV R1,Z
MOV R2, 3
ADD R1, R2
MOV W, R1
Code Optimization
Peephole Optimization – it works on the
theory of replacement. Try to reduce load
and store. Remove dead codes. (Improve
performance, reduce memory access, y=x+5 y=x+5
reduce size of code).
i=y i=y
y=x+5
i=y z=i z=i
z=i w=z+3 w=z+3
w=z+3 MOV R1,X Final Code-
MOV R1,X
ADD R2, R1 MOV R1,X
ADD R2, R1
MOV Y, R2 // Y is in R2 ADD R2, R1
MOV Y, R2
MOV I, Y MOV I, Y MOV Y, R2 // Y is in R2
MOV Z,I MOV Z,I MOV R1, 3
MOV R1,Z MOV R1,Z ADD R1, R2 //
MOV R2, 3
MOV R1, 3 MOV W, R1
ADD R1, R2
ADD R1, R2 //
MOV W, R1
- Remove all dead codes (i=y and z=I from TCG) MOV W, R1
Some important concepts
- With in a basic block if common sub-expression elimination is applied, then its is called as local common sub-
expression elimination.
- If expression is computed in some other block and used in the block after that and value of variables used in
expression is not changed, then it is called as Global common sub-expression elimination.
Error Handling
Error Handling
• Error Detection – Detecting the error at the time of compilation
• Error Reporting- Give appropriate message against error.
• Error recovery- Mechanism made at the time of compiler design to
recover certain error and generate warnings.
Types of Errors
1. Lexical
2. Syntax
3. Semantic
4. Logical
Types of Errors
Example
main()
{
inti a, b, $;
float 1c=10.6;
a=10;
b=c;
if(a<=10) then
printf(“”,a);
If(a=5)
printf(“%d”,a);
}}
Types of Errors
Some error messages-
1. Unmatched comments
2. Spelling error
3. Exceeding length of identifier
4. Wrong Name of identifier or unidentified symbol
5. Missing semicolon
6. Declaration not allowed
7. undeclared identifier
8. Bracket missing
9. Incompatible types of operands
10. Not matching of actual argument with formal argument
Types of Errors

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy