0% found this document useful (0 votes)
26 views59 pages

CD Unit4

This document discusses compiler code optimization techniques. It describes code optimization as improving intermediate code to consume fewer resources and run faster. Types of optimization include machine independent and dependent. Techniques covered are common subexpression elimination, dead code elimination, code motion, strength reduction, induction variables, loop optimization like frequency reduction, loop jamming, and loop unrolling. The goal of optimization is to improve program performance and speed while maintaining correctness.

Uploaded by

Mayank Hora
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)
26 views59 pages

CD Unit4

This document discusses compiler code optimization techniques. It describes code optimization as improving intermediate code to consume fewer resources and run faster. Types of optimization include machine independent and dependent. Techniques covered are common subexpression elimination, dead code elimination, code motion, strength reduction, induction variables, loop optimization like frequency reduction, loop jamming, and loop unrolling. The goal of optimization is to improve program performance and speed while maintaining correctness.

Uploaded by

Mayank Hora
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/ 59

Compiler Design

VI SEMESTER
ETCS-302

Department of Computer Science and Engineering, BVCOE, New Delhi


1
UNIT-IV
Compiler Design

 Code Optimization
Type of code optimization
Loop optimization
Basic Blocks
Live variables

2 Department of Computer Science and Engineering, BVCOE, New Delhi


Code optimization
The code optimization in the synthesis phase is a program
transformation technique, which tries to improve the intermediate
code by making it consume fewer resources (i.e. CPU, Memory)
so that faster-running machine code will result. Compiler
optimizing process should meet the following objectives :
The optimization must be correct, it must not, in any way, change
the meaning of the program.
Optimization should increase the speed and performance of the
program.
The compilation time must be kept reasonable.
The optimization process should not delay the overall compiling
process.

Department of Computer Science and Engineering, BVCOE, New Delhi


3
Types of Code Optimization 
The optimization process can be broadly classified into two types :
Machine Independent Optimization – This code optimization
phase attempts to improve the intermediate code to get a better
target code as the output. The part of the intermediate code which
is transformed here does not involve any CPU registers or absolute
memory locations.
Machine Dependent Optimization – Machine-dependent
optimization is done after the target code has been generated and
when the code is transformed according to the target machine
architecture. It involves CPU registers and may have absolute
memory references rather than relative references. Machine-
dependent optimizers put efforts to take maximum advantage of
the memory hierarchy.
Department of Computer Science and Engineering, BVCOE, New Delhi
4
Optimization

Department of Computer Science and Engineering, BVCOE, New Delhi


5
Advantages

 The optimized code has the following advantages-


Optimized code has faster execution speed.
Optimized code utilizes the memory efficiently.
Optimized code gives better performance.

Department of Computer Science and Engineering, BVCOE, New Delhi


6
Code Optimization Techniques-
 Important code optimization techniques are-
1. Compile Time Evaluation
2. Common sub-expression elimination
3. Dead Code Elimination
4. Code Movement
5. Strength Reduction

Department of Computer Science and Engineering, BVCOE, New Delhi


7
Compile Time Evaluation:
(a) z = 5*(45.0/5.0)*r
     Perform 5*(45.0/5.0)*r at compile time.
(b) x = 5.7
     y = x/3.6
    Evaluate x/3.6 as 5.7/3.6 at compile time.

Department of Computer Science


8 and Engineering, BVCOE, New Delhi
Variable Propagation
c = a * b                                               
Before Optimization the code is:
 
x = a                                                   
till                                                        
    
d = x * b + 4
After Optimization the code is:

c = a * b    
x = a  
till  
 d = a * b + 4  
Here, after variable propagation a*b and x*b identified as common sub expression.

   
Department of Computer Science
9 and Engineering, BVCOE, New Delhi
Dead code elimination:
Before elimination the code is:
 c = a * b                                                  
c = a * b                                                  
x = b                                                 
 x = b                                                 
till                                                          
till                                                          
d = a * b + 4  
d = a * b + 4    
After elimination the code is:
 c = a * b  
c = a * b  
till  
 till  
d = a * b + 4  
d = a * b + 4  
Here, x= b is a dead state because it will never subsequently
used in the program. So, we can eliminate this state.
Department of Computer Science
10 and Engineering, BVCOE, New Delhi
Code Motion
It reduces the evaluation frequency of expression.
It brings loop invariant statements out of the loop.
do  
do  
{  
{  
   item = 10;  
   item = 10;  
   valuevalue = value + item;   
   valuevalue = value + item;   
} while(value<100)
} while(value<100);  
This code can be further optimized as    
item = 10;  
item = 10;  
do  
do  
{  {  

   valuevalue = value + item;   
   valuevalue = value + item;   
} while(value<100);
} while(value<100);  
Department of Computer Science
11 and Engineering, BVCOE, New Delhi
Strength Reduction:
Strength reduction is used to replace the high strength
operator by the low strength.
Before Strength reduction After Strength Reduction
int j = 0; int j = 0;
for (int i = 0; i < 100; i++) for (int i = 0; i < 100; i++)
{ {
j = 2*i; j = j + 2;
} }
return j; return j;

Department of Computer Science


12 and Engineering, BVCOE, New Delhi
Induction Variable
An induction variable is a variable whose value on each loop iteration is a
linear function of the iteration index. When such variables and the
expressions they compute are found, often the variable itself can be
eliminated or a strength reduction can int
be performed.
max = 10;
int result = 0;
for (int i = 0; i < max; i++)
{
result += 2; }
return result;

After Induction variable removed


int max = 10;
intresult
int max == 10;
0;
for
int(;result
result= <0;max*2; result+=2) {}
return result;
for (; result < max*2; result+=2) {}
 return result;
Department of Computer Science
13 and Engineering, BVCOE, New Delhi
Common Sub-Expression Elimination-
The expression that has been already computed before
and appears again in the code for computation is called
as Common Sub-Expression.
Example-

Department of Computer Science and Engineering, BVCOE, New Delhi


14
Loop Optimization
Loop Optimization is the process of increasing
execution speed and reducing the overheads associated
with loops. It plays an important role in improving
cache performance and making effective use of
parallel processing capabilities. Most execution time of
a scientific program is spent on loops.
Loop Optimization is a machine independent
optimization.
Decreasing the number of instructions in an inner loop
improves the running time of a program even if the
amount of code outside that loop is increased.
Department of Computer Science
15 and Engineering, BVCOE, New Delhi
Loop Optimization Techniques
Frequency Reduction (Code Motion)
Loop Unrolling
Loop Jamming

Department of Computer Science


16 and Engineering, BVCOE, New Delhi
Frequency Reduction (Code Motion)
In frequency reduction, the amount of code in loop is decreased. A
statement or expression, which can be moved outside the loop body
without affecting the semantics of the program, is moved outside the
loop.
Initial code: while(i<100)
{ a = Sin(x)/Cos(x) + i ;
i++; }

Optimized code:

t = Sin(x)/Cos(x);
while(i<100) { a = t + i;
i++; }

Department of Computer Science


17 and Engineering, BVCOE, New Delhi
Loop Jamming
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.
Initial Code: for(int i=0; i<5; i++)
a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;

Optimized code: for(int i=0; i<5; i++)


{
a = i + 5;
b = i + 10;
}

Department of Computer Science


18 and Engineering, BVCOE, New Delhi
Loop Unrolling
Loop unrolling is a loop transformation technique that helps to optimize the
execution time of a program. We basically remove or reduce iterations. Loop
unrolling increases the program’s speed by eliminating loop control
instruction and loop test instructions.
Example:-
Initial code:
for (int i=0; i<5; i++)
printf("Priya\n");
Optimized code:
printf("Priya\n");
 printf("Priya\n");
printf("Priya\n");
printf("Priya\n");
printf("Priya\n");
Department of Computer Science
19 and Engineering, BVCOE, New Delhi
Loop Unrolling
Example:
Initial Code Optimized code:
I=1 I=1
While (I<=100) While (I<=100)
{ {
X[I] = 0; X[I] = 0;
I = I+1 ; I = I+1 ;
} X[I] = 0;
I = I+1
}

Department of Computer Science


20 and Engineering, BVCOE, New Delhi
Q
Consider following code:
 x= 10;
 y=5+x; 5+10; 15;
 z= (y+z)*2 = (15 +z)*2 = (15+z)+(15+z)
Which of the following code optimization technique is not
possible to apply on the above code
(1) constant folding
(2) copy propagation
(3) strength reduction
(4) code motion
(A) 1,4 (B) 2,4 (C) 3,4 (D) 4 only
Department of Computer Science
21 and Engineering, BVCOE, New Delhi
Elimination of Common Subexpression
(1) S1 = 4*I
(2) S2 = addr(A) – 4
(3) S3 = S2[S1]
(4) S4 = 4*I
(5)S5 = addr(B) – 4
(6) S6 = S5[S4]
(7) S7 = S3*S6
(8) S8 = PROD +S7
(9) PROD = S8
(10) S9 = I+1
(11) I = S9
(12) If I<=20 GOTO (1)
Department of Computer Science
22 and Engineering, BVCOE, New Delhi
Construct DAG
1. Very first instruction (1) is S1 = 4*I,

Department of Computer Science


23 and Engineering, BVCOE, New Delhi
Continue…
After 2nd instruction DAG will be
S2 = addr(A) – 4

Department of Computer Science


24 and Engineering, BVCOE, New Delhi
Continue….
S3 = S2[S1]

Department of Computer Science


25 and Engineering, BVCOE, New Delhi
Continue…
(5)S5 = addr(B) – 4
(6) S6 = S5[S4]
(7) S7 = S3*S6

Department of Computer Science


26 and Engineering, BVCOE, New Delhi
Continue…
..

Department of Computer Science


27 and Engineering, BVCOE, New Delhi
Optimized code
(1) s1= 4*I
(2) S2 = addr(A)-4
(3) S3 = S2[S1]
(4) S5= addr(B) -4
(5) S6=S5[S1]
(6) S7=S3*S6
(7) PROD = PROD +S7
(8) I = I +1
(9) IF I<=20 goto (1)

Department of Computer Science


28 and Engineering, BVCOE, New Delhi
Basic Blocks in Compiler Design
Basic Block is a sequence of consecutive statements in
which flow of control enters at the beginning and
leaves at the end without halt or possibly of branching
except at the has no branches in and out branches
except to the entry and at the end respectively. Basic
Block is a set of statements which always executes one
after other, in a sequence.
A=b+c
F=d
D<1

Department of Computer Science


29 and Engineering, BVCOE, New Delhi
Rules to identify the basic blocks
The first three-address instruction of the intermediate
code is a leader.
Instructions which are targets of unconditional or
conditional jump/goto statements are leaders.
Instructions which immediately follows unconditional
or conditional jump/goto statements are considered as
leaders.

Department of Computer Science


30 and Engineering, BVCOE, New Delhi
Example
1) i=1 //leader
2) j=1 //leader
3) t1 = 10 * i //leader
4) t2 = t1 + j
5) t3 = 8 * t2
6) t4 = t3 - 88
7) a[t4] = 0.0
8) j = j + 1
9) If j<=1 goto (3)
10) i = i + 1 //leader
11) if i <= 10 goto (2)
12) i = 1 //leader
13) t5 = i – 1 //leader
14) t6 = 88 * t5
15) a[t6] = 1.0
16) i = i + 1
17) if i <= 10 goto (13)

Department of Computer Science


31 and Engineering, BVCOE, New Delhi
Solution
There are 6 Basic Blocks in the above code :
B1) Statement 1
B2) Statement 2
B3) Statement 3-9
B4) Statement 10-11
B5) Statement 12
B6) Statement 13-17

Department of Computer Science


32 and Engineering, BVCOE, New Delhi
Problem 1
(1) PROD = 0 //l
(2) I = 1
(3) T2 = addr(A) – 4
(4) T4 = addr(B) – 4
(5) T1 = 4 x I //l
(6) T3 = T2[T1]
(7) T5 = T4[T1]
(8) T6 = T3 x T5
(9) PROD = PROD + T6
(10) I = I + 1
(11) IF I <=20 GOTO (5)
Compute the basic blocks for the given three address statements-
Draw a flow graph for the three address statements given in problem-01.compute no of
blocks and edges
Eliminate common sub expression
Find induction variables and eliminate then where possible

Department of Computer Science


33 and Engineering, BVCOE, New Delhi
Scope of optimization
Local optimization –with in a block
Global optimization – between blocks –extended to
entire control graph

Department of Computer Science


34 and Engineering, BVCOE, New Delhi
Directed Acyclic Graph-
• Directed Acyclic Graph (DAG) is a special kind of 
Abstract Syntax Tree. 
Each node of it contains a unique value.
It does not contain any cycles in it, hence called Acyclic.
 
Optimization Of Basic Blocks- 
DAG is a very useful data structure for implementing
transformations on Basic Blocks. 
A DAG is constructed for optimizing the basic block.
A DAG is usually constructed using Three Address Code.
Transformations such as dead code elimination and common sub
expression elimination are then applied.
Department of Computer Science
35 and Engineering, BVCOE, New Delhi
Construction of DAGs-
 Following rules are used for the construction of DAGs- 
Rule-01: 
In a DAG,
Interior nodes always represent the operators.
Exterior nodes (leaf nodes) always represent the names, identifiers or constants.  
Rule-02:
While constructing a DAG,
A check is made to find if there exists any node with the same value.
A new node is created only when there does not exist any node with the same value.
This action helps in detecting the common sub-expressions and avoiding the re-
computation of the same.
Rule-03: 
The assignment instructions of the form x:=y are not performed unless they are
necessary.

Department of Computer Science


36 and Engineering, BVCOE, New Delhi
Problem-01:
• (a+b)x(a+b+c)

Department of Computer Science


37 and Engineering, BVCOE, New Delhi
Problem-02:
( ( ( a + a ) + ( a + a ) ) + ( ( a + a ) + ( a + a ) ) )

Department of Computer Science


38 and Engineering, BVCOE, New Delhi
DAG using value added method
(b*c)+(b*c)*b

s.no Op/ opr1 opr2


var
1 id b
2 id c
3 * 1 2
4 * 1 3
5 + 3 4

Department of Computer Science


39 and Engineering, BVCOE, New Delhi
Problem-03:
a= b* -c+b * -c

Department of Computer Science


40 and Engineering, BVCOE, New Delhi
Problem-04:
(1) a = b x c
(2) d = b
(3) e = d x c
(4) b = e
(5) f = b + c
(6) g = f + d
After simplifying
 a=b*c = d*c = e=b
 f=b+c
 g=f+d
Department of Computer Science
41 and Engineering, BVCOE, New Delhi
Problem-05:
s.no Op/id opr1 opr2
a= a+b 1 id a
e=a+d+e 2 id b
3 id e
4 id d
5 + 1 2
6 = 1 5
7 + 1 4
8 + 7 3
9 = 3 8

Department of Computer Science


42 and Engineering, BVCOE, New Delhi
Problem-06:
a+a*(b-c)+(b-c)*d

Department of Computer Science


43 and Engineering, BVCOE, New Delhi
Problem-07:
A[I] = B
*P = C
D = A[I]
E= *P
*P = A[I]

Department of Computer Science


44 and Engineering, BVCOE, New Delhi
Problem 8
Consider a basic block given below
a=b+c
c=a+d
d=b+c
e = d-b
a=e+b
Minimum number of nodes present in DAG
representation of above basic block
(A) 6,6 (B) 8,10 (C) 9,12 (D) 4,4

Department of Computer Science


45 and Engineering, BVCOE, New Delhi
Problem 9
Consider the following expression in c program
 x= a+ b*c – d* e
Find the minimum number of nodes in equivalent
DAG for given expression

Department of Computer Science


46 and Engineering, BVCOE, New Delhi
Problem 10
A directed acyclic graph represents one form of
intermediate representation. The number of non
terminal nodes in DAG of a = (b+c)*(b+c) expression
is:
(A) 2
(B) 3
(C) 4
(D) 5

Department of Computer Science


47 and Engineering, BVCOE, New Delhi
Live Variables
A variable x is said to be live at a statement Si in a
program if the following of these conditions hold
simultaneously.
(i) There exist a statement Sj that uses x
(ii) There is a path from Si to Sj in the flow graph
corresponding to the program
(iii) The path has no intervening assignment to x
including Si and Sj

Department of Computer Science


48 and Engineering, BVCOE, New Delhi
Example
. p=q+r
s= p+q
u= s*v

v=r+u q=s*u

q=v+r

Department of Computer Science


49 and Engineering, BVCOE, New Delhi
Q1
. Consider the following grammar G, with A as start by
 A -> BA|a
 B -> +B|-B|ε
The grammar G is ____________
(A) LL(1)
(B) LR(0)
(C) SLR(1)
(D)None of the above

Department of Computer Science


50 and Engineering, BVCOE, New Delhi
Q2
Consider the following C code:
 void main()
{
int /* comment */ t x;
float comment */y;
}
Find the number of tokens produced by lexical analyzer
(A) 16
(B) 15
(C) 14
(D) None of these
Department of Computer Science
51 and Engineering, BVCOE, New Delhi
Q3
. The job of lexical Analyzer during compilation is
_______.
(A) Verifying the syntax of program.
(B) Identifying the functions in the program.
(C) Recognizing the lexemes of the program.
(D) All the above

Department of Computer Science


52 and Engineering, BVCOE, New Delhi
Q4
. Which of the following can be performed with the
help of SDT in compiler.
(A) Type Checking
(B) Declaration of variables before it use
(C) Definition of functions before the function call
(D) All the above

Department of Computer Science


53 and Engineering, BVCOE, New Delhi
Q5
. Consider the following program
main()
{
in/* comment */ t a;
gate;
}
Find number of tokens in the program
(A) 11
(B) 12
(C) 16
(D) Lexical error
Department of Computer Science
54 and Engineering, BVCOE, New Delhi
Q6
. Consider the SDT
E->E+T {E.x = E1.x * T.x}
E->T {E.x = T.x}
T->T*F {T.x = T1.x - F.x}
F->id {F.x = id.lexeme}
above SDT is
(A) L-attributed only
(B) S- attributed only
(C) Both
(D) None of above
Department of Computer Science
55 and Engineering, BVCOE, New Delhi
Q7
Match the following groups :
(a) Lexical error 1. int float;
(b) Syntax error 2. int x='A';
(c) Semantic error 3. int 1x23;
(d) logical error 4. int
x=x/0;
code a b c d
(A) 2 3 1 4
(B) 1 2 3 4
(C) 3 1 2 4
(D) 2 4 3 1
Department of Computer Science
56 and Engineering, BVCOE, New Delhi
Q8
Consider the following program
int main()
{
int 45;
}
Find number of tokens present in the program
(A) 11
(B) 10
(C) 9
(D) Lexical error
Department of Computer Science
57 and Engineering, BVCOE, New Delhi
Q9
Identify an error which cannot be detected during
compilation.
(A) Unbalanced parenthesis
(B) Function not defined
(C) Divide by zero
(D) None of the above

Department of Computer Science


58 and Engineering, BVCOE, New Delhi
Q10
Which of the following grammar is LL(1)
(A) S->(S)|SS|ε
(B) S->aSb|ab|ε
(C) S->aS|Sb|ε
(D) None of these

Department of Computer Science


59 and Engineering, BVCOE, New Delhi

You might also like