• No results found

CODE OPTIMIZATION

N/A
N/A
Protected

Academic year: 2020

Share "CODE OPTIMIZATION"

Copied!
45
0
0

Loading.... (view fulltext now)

Full text

(1)

Group Members

Rabbia Nazeer

Waqas Nazeer

(2)

Topic

(3)

What is code Optimization?

• Is a set of methods of code modification to

improve code quality and efficiency.

• Transforming a program into an equivalent but

better form.

• Elimination of extra code.

• It can be performed by automatic optimizers.

• An optimizer is either a specialized software tool

(4)

Why is code optimized?

• Compiler can be considered to have efficient performance if it translates a program of

about 1000 lines in just few seconds.

• Code optimization is done to get satisfactory performance of compiler.

(5)

Goals of Code Optimization

• To make the code generation faster.

• To Make the code smaller.

• To make the compiler more efficient.

(6)

Disadvantages of code Optimization

• Rearrange and removal of code will reduce the effectiveness of debuggers.

• Compiler that you are using may contain bugs that only exist when it is performing

(7)

Disadvantages of Code Optimization

(cont.)

• Optimization of high level language is

(8)

Optimization is done of 3 level of

program:

1) Source level Optimization.

2) Intermediate Code Optimization.

(9)
(10)

Source level Optimization:

• It does not requires any knowledge of machine

instructions.

• This level is machine independent.

• It can be done by replacing a slow or inefficient

algorithm with a quick algorithm.

• For example Bubble Sort is not much efficient and

(11)
(12)
(13)

Intermediate Code Optimization:

• Optimization done on intermediate code.

• Most Optimizations are performed on intermediate level.

(14)

Intermediate Code Optimization

• Techniques used in intermediate code optimization are:

1) Local Optimization.

2) Loop Optimization.

(15)

Local Optimization

• Optimization done with in a single basic block.

• Techniques:

1) Constant Folding.

2) Copy Propagation.

3) Algebraic Simplification.

4) Elimination of common sub expressions.

5) Reduction in operator strength.

(16)

Constant Folding:

• To computer constant expressions at compile

time.

• For example:

int a=4; int a=4 i=2+a; i=6;

j=i*5+b; j=30+b;

• String literals can be constant folded, such as

(17)

Copy Propagation

• It identifies and eliminates those statements which are redundant.

• Example:

• int i=0; int i=0

• A=b+c; a=b+c

(18)

Algebraic Simplification

• Simplifications use algebraic properties.

• Useless instructions can be removed entirely via algebraic identities.

a*1 a ( Multiplicative Identity) a/1 a

a*0 0

a+0 a (Additive identity) a-0 a

(19)

Elimination of common Sub expression

• Replace re computation of expression by use of temp which holds value.

Examples :

1) a = b * c + g; tmp = b * c; d = b * c * e; a = tmp + g; d = tmp * e;

• 2) j= D+C*e temp=D+C k=D+C/F j=temp*e

(20)

Common Sub expression

Elimination cont.

3) A[ i+1 ] = B[ i+1 ]; tmp = i+1;

A[ tmp ] = B[ tmp ];

• Detection and elimination of redundant computations is the major goal of an

(21)

Reduction in operator strength

• Replacing an expensive operator by cheaper one.

• For example:

Multiplying integers usually take longer than adding them.

x = 2 * y; x = y + y;

(22)

Inline expansion of code.

• Replaces a function with its body of the code.

• It might occupy more memory but reduced compile time.

Example:

(23)

Loop Optimization

Loop optimization is the process of the

increasing execution speed and reducing the overheads associated of loops.

Techniques used in loop optimization:

1) Loop in-variant code hoisting.

2) Loop Fusion.

(24)

Loop in variant code hoisting

Loop-invariant code consists of statements or

expressions which can be moved outside the body of a loop.

• It do not affect the semantics of the program.

• Example:

for( I =0 ; i< 10 ; i++) {

a[i] = b[i] + c/d; }

Int temp= c/d;

for( I =0 ; i< 10 ; i++) {

(25)

Loop Fusion

• Replacing multiple loops having identical headers with a single one.

• It is done to reduce number of tests. Example:

int i, a[100], b[100];

for (i = 0; i < 100; i++) a[i] = 1;

for (i = 0; i < 100; i++) b[i] = 2;

int i,

a[100], b[100];

for (i = 0; i < 100; i++) {

(26)

Loop Collapsing

• Flatten a multi dimensional loop.

• Nested loops can be collapsed into a

single-nested loop to reduce loop overhead and improve run-time performance.

Example:

for (i=0; i<N; i++) for (j=0; j<M; j++) ... a[ i ][ j ] ... ;

for (ij=0; i<M*N; ij++) {

(27)

Global Optimization

• Whole program is analyzed in global optimization.

• It needs data flow analysis.

It includes:

a) Remove unreferenced variables.

b) Avoid calculation whose results are not used.

c) Remove the code which is not called.

(28)

Remove unreferenced variables

Int main() int main() { {

int j=0; Int []temp=new int[5]; int [] temp=new int[5];

for(int i=0;i<tem.Length;i++) for(int i=0;i<temp.Length;i++) { {

Console.WriteLine(temp[i]); Console.WriteLine(temp[i]); } }

} }

(29)

Avoid calculation whose results are not

used

Int main() { int a; int b; int c;

String [] text=textBox1.Text; c=a+b;

for(int i=0;i<text;Length;i++) {

if(temp[i]==‘a’) {

Console.WriteLine(“string accepted”); }

else

Console.writeLine(“String rejected”); }

}

(30)

Remove the code which is not called.

int SQR(int a); void main(void) { int x=2; Int square; square=x*x; }

int SQR(int a) {

return a*a; }

We have declared the SQR() function and also defined it but the function is not

called any where in program This function is taking extra memory

(31)

Remove un initialized variables

• Void main()

• {

• int i;

• int temp=0;

• int d=5;

• int f=2;

• temp=d+f;

return 0; }

(32)

Dataflow Analysis

• Technique for gathering information about

the possible set of values calculated at various points in a computer program.

• Control flow graph is used to determine those parts of a program to which a particular value assigned to a variable.

(33)

Target Level Optimizations

• Also called machine dependent optimization.

(34)

Target Level Optimizations

Techniques for Machine Dependent Optimization.

1) Register Allocation.

a) Graph Coloring.

2) Instruction Scheduling.

(35)

Register Allocation

• It refers to choosing which values are kept in

which registers during program execution.

• Registers are fastest kind of memory on the

target machine.

• Storing and accessing variables from registers is

(36)

Register Allocation

• Efficient use of registers is very important, it causes to reduce memory access.

• Virtual registers are mapped on to physical registers

(37)

Example

(38)

Register Allocation by Graph Coloring

• It is used when register is needed for

computation but all available registers are in use.

• Then the content of one of the used register

must be stored into a memory location in order to free up the register.

(39)

Graph coloring technique

• Variable c wants

• to be loaded in a

register but there is no free register so a register whose value is currently not in use will shift its variable in

(40)

Instruction scheduling

• It involves reordering the instructions before machine code is generated.

(41)
(42)

Peephole optimization

Very simple and effective technique for improving target code.

Uses a window of only few consecutive instructions at a time(called peephole)

It includes:

a) Redundant instruction elimination.

c) Elimination of load of value when previous instruction stored

that value from a register to a memory location.

(43)

Peephole Optimization

• Lets see an example f=a+10 Store R3,f store R4,f load R1,a Load R2,10

Add R3,R1,R2 load R1,a Load R2,10 Add R3,R1,R2

Store R4,f Load R1,a Add R3,R1,10

(44)
(45)

And SORRY if you got a

References

Related documents

When agents’ wealth-weighted average belief about the future short rates is higher than the econometrician’s belief, they would discount bonds more heavily and the equilibrium

As these results show, a change in the terms of trade has opposite effects on the real wages of male and female workers whether considering the real wage either in terms of

45 In parallel and following the Vienna World Conference on Human Rights, the newly consolidated Office of the United Nations High Commissioner for Human Rights (OHCHR)

(2) Mobile phone research (calling and text messaging) conducted internationally is the next most frequently researched platform (.. [Citation: Journal/Monograph Title, Vol.

Abiotic (drought, heat, and cold stress) and biotic (pod borers – Helicoverpa armigera and Spodoptera exigua, aphids – Aphis crac- civora, leaf miner – Liriomyza cicerina, and

Parser (Syntax analysis Semantic analysis Intermediate code generation Machine-independent code optimization.. Target

The point at which the resultant of lift forces acts is: A) the hub. B) the center of gravity. D) the center of pressure.. A) airflow velocity increasing downward having been

– Translate source code from high-level language to low- level language (object code or machine code). – Create an