0% found this document useful (0 votes)
9 views12 pages

Principle Sources of Optimization

Uploaded by

nexexa6242
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)
9 views12 pages

Principle Sources of Optimization

Uploaded by

nexexa6242
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/ 12

Department of Computer Science & Engineering

UIT-RGPV

Topic: Principle Sources of Optimization

Course: Compiler Design (CS701)

By:
Mr. Praveen Yadav
Assistant Professor
DoCSE, UIT-RGPV
Bhopal
Principle Sources of Optimization
• Local Optimization: Transformations performed on a basic block (i.e
local transformation).
• Global Optimization: Transformation performed on more/all basic
blocks as a whole without changing their function. Flow of control
analysis and Data flow analysis are used for global optimization.
• We distinguish local transformations, from global transformations.
• A basic block computes a set of expressions: A number of
transformations can be applied to a basic block without changing
the expressions computed by the block.
• Following optimization techniques can be performed at local and
global level.
1. Function Preserving Transformations (Structure Preserving)
2. Loop Optimization
Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 2
Function Preserving Transformations
• There are number of ways in which a compiler can improve a
program without changing the function it computes.
• Some common function preserving transformations are:
1. Common Sub-expressions elimination.
2. Copy Propagation (Variable Propagation).
3. Dead-Code elimination.
4. Constant Folding and Constant Propagation.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 3


Common Sub-expressions Elimination

•Instead of using t7 we can use t6. hence t7 = 4*i can be


eliminated.
•Instead of using t10 we can use t8. hence t10 = 4*j can be
eliminated

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 4


Common Sub-expressions Elimination

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 5


Common Sub-expressions Elimination

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 6


Copy Propagation
• Copy Propagation Rule: Given the copy statement, x := y, use
y for x whenever possible after the copy statement.
• Copy Propagation applied to Block B5 yields (see previous
example):
x := t3
a[t2] := t5
a[t4] := t3
gotoB2

• This transformation together with Dead-Code Elimination (see


next slide) will give us the opportunity to eliminate the
assignment x := t3 altogether.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 7


Dead-Code Elimination
• Intuition: A variable is live at a point in a program if its value
can be used subsequently, otherwise it is dead.
• Dead Code. A piece of code is dead if data computed is never
used elsewhere and can be eliminated.
• Example. Considering the Block B5 after Copy Propagation we
can see that x is never reused all over the code. Thus, x is a
dead variable and we can eliminate the assignment x := t3
from B5.

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 8


Constant Folding and Constant Propagation
I. Constant Folding:
• Intuition: Based on deducing at compile-time that the value
of an expression (and in particular of a variable) is a constant.
• Constant Folding is the transformation that substitutes an
expression with a constant.
• Constant folding is the process of recognizing and
evaluating constant expressions at compile time rather than
computing them at runtime.
• Example: consider the expression:
• i = 320 *5;
• Then at compile time this expression become i = 1600

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 9


Constant Folding and Constant Propagation
II. Constant Propagation:
Constant propagation is the process of substituting the values of
known constants in expressions at compile time. Such
constants include those defined above
• Consider the following pseudo code:
int x = 14;
int y = 7 - x / 2;
return y * (28 / x + 2);

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 10


Constant Folding and Constant Propagation
• Propagating x yields:
int x = 14;
int y = 7 - 14 / 2;
return y * (28 / 14 + 2);
• Continuing to propagate yields the following (which would
likely be further optimized by dead code elimination of both x
and y.)
int x = 14;
int y = 0;
return 0;

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 11


Thank You

Compiler Design By Asst. Prof. Praveen Yadav, DoCSE, UIT-RGPV 12

You might also like