Unit-5: Error Handling, Code Optimization, and Target Code Generation
Unit-5: Error Handling, Code Optimization, and Target Code Generation
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