Unit – 4 Code Optimization 1)

Code Optimization techniques (IMP) 1. Compile Time Evaluation Compile time evaluation means shifting of computations from run time to compilation. There are two methods used to obtain the compile time evaluation. Folding In the folding technique the computation of constant is done at compile time instead of run time. example : length = (22/7) * d Here folding is implied by performing the computation of 22/7 at compile time Constant propagation In this technique the value of variable is replaced and computation of an expression is done at the compilation time. example : pi = 3.14; r = 5; Area = pi * r * r Here at the compilation time the value of pi is replaced by 3.14 and r by 5 then computation of 3.14 * 5 * 5 is done during compilation. 2. Common Sub Expression Elimination The common sub expression is an expression appearing repeatedly in the program which is computed previously. Then if the operands of this sub expression do not get changed at all then result of such sub expression is used instead of recomputing it each time Example: t1 := 4 * i

t1=4*i

t2 := a[t1] t3 := 4 * j

t2=a[t1] t3=4*j

t4 : = 4 * i t5:= n

t5=n t6=b[t1]+t5

t6 := b[t4]+t5 The common sub expression t4:= 4 * i is eliminated as its computation is already in t1 and value of i is not been changed from definition to use.

3. Variable Propagation Variable propagation means use of one variable instead of another. Example : x = pi; area = x * r * r; The optimization using variable propagation can be done as follows, area = pi * r * r; Here the variable x is eliminated. Here the necessary condition is that a variable must be

assigned to another variable or some constant. 4. Code Movement There are two basic goals of code movement: 1.

To reduce the size of the code.

2.

To reduce the frequency of execution of code. Example : for(i=0;i<=10;i++)

temp=y*5

{

for(i=0;i<=10;i++) x=y*5;

{

k=(y*5)+50;

x=temp;

}

k=(temp) + 50; }

Loop invariant computation Loop invariant optimization can be

obtained by moving some amount

of code outside the loop and placing it

just before entering in the loop.

This method is also called code motion.

N=max-1;

Example:

While(i<=N)

while(i<=max-1) {

{ sum=sum+a[i]; }

sum=sum+a[i]; }

5. Strength Reduction Strength of certain operators is higher than others. For instance strength of * is higher than +. In this technique the higher strength operators can be replaced by lower strength operators. Example: for(i=1;i<=50;i++) { count = i x 7; } Here we get the count values as 7, 14, 21 and so on up to less than 50. This code can be replaced by using strength reduction as follows temp=7 for(i=1;i<=50;i++) {

count = temp; temp = temp+7; }

6. Dead Code Elimination A variable is said to be live in a program if the value contained into is subsequently. On the other hand, the variable is said to be dead at a point in a program if the value contained into it is never been used. The code containing such a variable supposed to be a dead code. And an optimization can be performed by eliminating such a dead code. Example : i=0; if(i==1) { a=x+5; } If statement is a dead code as this condition will never get satisfied hence, statement can be eliminated and optimization can be done.

2)

Explain Loop Optimization Loop optimization is a technique in which code optimization is performed on inner loops. The loop optimization is carried out by following methods: 1. code motion 2. strength reduction 3. loop unrolling 4. loop fusion we have already discussed code motion & strength reduction method above 3. Loop Unrolling In this method number of jumps and test can be reduced by writing the code two times. Example: int i=1;

int i=1;

while(i<=100)

while(i<=100)

{

{

}

A[i]=b[i];

A[i]=b[i];

i++;

i++; A[i]=b[i]; i++;

} 4. Loop fusion In loop fusion method several loop are merged to one loop. for i=1 to n do for j=1 to m do A[i,j]=10 Can be written as for i=1,j=1 to n*m do A[i]=10

3)

Explain Peephole optimization (Most Imp) Definition Peephole optimization is a simple and effective technique for locally improving target code. This technique is applied to improve the performance of the target program by examining the short sequence of target instructions and replacing these instructions by shorter or faster sequence. Characteristics of Peephole Optimization The peephole optimization can be applied on the target code using following characteristic. 1. Redundant instruction elimination. Especially the redundant loads and stores can be eliminated in this type of transformations. Example : MOV R0,x MOV x,R0 We can eliminate the second instruction since x is already in R0. But if (MOV x, RO) is a label statement then we cannot remove it 2. Flow of control optimization. Using peephole optimization unnecessary jumps on jumps can be eliminated 3. Algebraic simplification. Peephole optimization is an effective technique for algebraic simplification. The statements such as x := x + 0 or x := x* 1 can be eliminated by peephole optimization. 4. Reduction in strength Certain machine instructions are cheaper than the other.

In order to improve performance of the intermediate code we can replace these instructions by equivalent cheaper instruction. 2 For example, x is cheaper than x * x. Similarly addition and subtraction is cheaper than

multiplication and division. So we can add effectively equivalent addition and subtraction for multiplication and division. 5. Machine idioms The target instructions have equivalent machine instructions for performing some operations. Hence we can replace these target instructions by equivalent machine instructions in order to improve the efficiency. For example, some machines have auto-increment or auto-decrement addressing modes that are used to perform add or subtract operations.

4)

Loop in a flow graph 1. Dominators In a flow graph, a node d dominates n if every path to node n from initial node goes Every initial node dominates all the remaining nodes in the flow graph. Similarly every node dominates itself. 1

2 3

4 5

Node 1 is initial node and it dominates every node as it is initial node. Node 2 dominates 3, 4 and 5 Node 3 dominates itself similarly node 4 dominates itself. 2. Natural loops Loop in a flow graph can be denoted by n-> d such that d dom n. This edge is called back edges and for a loop there can be more than one back edge. If there is p -> q then q is a head and p is a tail. And head dominates tail. 1

3

2 4

5

The loop in above graph can be denoted by 4->1 i.e. 1 dom 4. Similarly 5->4 i.e. 4 dom 5 The natural loop can be can be defined by a back edge n->d such there exist a collection of all the node that can reach to n without going through d and at the same time d also can be added to this collection. 1

2

4

3 5

6 6->1 is a natural loop because we can reach to all the remaining nodes from 6. 3. Inner loops The inner loop is a loop that contains no other loop. Here the inner loop is 4-> 2 that mean edge given by 2-3-4. 1

2

3 4

5

4. Pre-header

Preheader

Header

B 0

The pre-header is a new block created such that successor of this block is the block. All the computations that can be made before the header block can be made the pre-header block.

5. Reducible flow graph The reducible graph is a flow graph in which there are two types of edges forward edges and backward edges. These edges have following properties, a. The forward edge forms an acyclic graph. b. The back edges are such edges whose head dominates their tail 1

2

4

3 5

Can be reduced as 1

2 3

4 5

The above flow graph is reducible. We can reduce this graph by removing the edge from 3

to 2 edge. Similarly by removing the back edge from 5 to 1. We can reduce above flow graph and the resultant graph is a cyclic graph. 6. Non-reducible flow graph A non reducible flow graph is a flow graph in which: a. There are no back edges b. Forward edges may produce cycle in the graph. c. d. For example - Following flow graph is nonreducible. 2

4

3 5

5)

Global Data Flow Analysis Data flow equations are the equations representing the expressions that are appearing in the flow graph. These equations are useful for computing live variables, available and reaching definitions. Representing Data Flow information Data flow information can be represented by either a set of properties or using bit vector with each bit representing a property. Data Flow Equations for Programming Constructs The data flow equation written in a form of equation such that out [S] = gen [S] U (in [S] -kill [S]) S represents the statement for which the data flow equation can be written. Out represents an output Gen represents the information generated within the statement. In represents the gathered information before entering in the loop. Kill represents the information that is killed with in the control flow. Hence the above equation can be read as "the data flow information at the statement is either the information generated within the statement or the information gathered before entering the loop and not killed when control flows through statement."

6)

Data Flow Properties A program point containing the definition is called definition point. A program point at which a reference to a data item is made is called reference point. A program point at which some evaluating expression is given is called evaluation point.

W1:x=3

Definition point

W2: y=x

Reference point

W3: z=a*b

Evaluation point

1. Available expression An expression x+y is available at a program point w if and only if along all paths are reaching to w. 1. The expression x+y is said to be available at its evaluation point. 2. The expression x+y is said to be available if no definition of any operand of the expression (here either x or y) follows its last evaluation along the path. In other word, if neither of the two operands get modified before their use. B1: t1=4*i

B3: t2=4*i

B2:

B4: t4=a[t2] Expression 4 * i is the available expression for B2, B3 and B4 because this expression is not been changed by any of the block before appearing in B4. 2. Reaching definition A definition D reaches at the point P if there is a path from D to P along witch D is not killed.

A definition D of variable x is killed when there is a redefinition of x. D1: y=2

B1

D2: y=y+2

B2

D3: x=y+2

B3

The definition D1 is reaching definition for block B2, but the definition D1 not is reaching definition for block B3, because it is killed by definition D2 in block B2. 3. Live variable A live variable x is live at point p if there is a path from p to the exit, along which the value of x is used before it is redefined. Otherwise the variable is said to be dead at the point. 4. Busy expression An expression e is said to be busy expression along some path pi..pj if and only if an evolution of e its evaluation along the path.

Unit - 4 Code Optimization.pdf

There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Unit - 4 Code ...

217KB Sizes 1 Downloads 208 Views

Recommend Documents

UNIT 4 REVIEW
2 mol Al x. 2. 3. 1 mol Cr O. 26.98 g Al. 1 mol Al x. 7.10 g Al. = mass of Al(s) required to have a 20% excess = 120% 7.10 g 8.52 g x. = Part 2. (Pages 350–351).

unit 4.pdf
The first object may generate a display asking for the object file, list file and ... executable filename can further be entered at the DOS prompt to execute the file.

UNIT-4.pdf
If return type is not explicitly specified, C will. assume that it is an integer type. If the function is not returning anything the return. type is void. Function_name ...

Unit 4 homework.pdf
8 G s s : as & 0 & 6 a 6 s is 3 s p r s ( & & ) & 6 s et & q' () e s - G - e. e. e. e s is a e s e & & c s s is 8 & 9 s so 6 c q & s e s 6 & 8 & e & G. & 6 e s & e s 8. Write the ...

Unit 4.pdf
HINDUSTAN UNIVERSITY. Page 1 of 48 ... temperature inside an occupied area. Page 4 of 48. Unit 4.pdf. Unit 4.pdf. Open ... Displaying Unit 4.pdf. Page 1 of ...

Clean Code “Unit Tests” - GitHub
It's more what you'd call a "guideline" ... LinkedHashMap groups = ... assertEquals("+SHRA +FC FZDZ +TSRAGR", groups.get(WEATHER));.

Naming Unit Tests Responsibly Code
Jan 30, 2007 - For example, when it's hard to construct a sentence where the first word is the class under test, it suggests the test may be in the wrong place. And classes that are hard to describe in general often need to be broken down into smalle

unit-4-qb-w-ans 4- BY Civildatas.blogspot.in.pdf
Page 1 of 14. MAKALAH GLOBAL WARMING. BAB 1. PENDAHULUAN. 1.1. Latar Belakang Masalah. Makalah ini dibuat untuk menambah pengetahuan ...

PSC unit (4)_NoRestriction.pdf
grade is usdl for casting the flange at the site. 4. Write the advantages of composite construction in PSC. (MAY/JUNE 2009, Noy/DEc 2009). r' Appreciable ...

Unit 4 Circles Review Key.pdf
Download. Connect more apps... Try one of the apps below to open or edit this item. Unit 4 Circles Review Key.pdf. Unit 4 Circles Review Key.pdf. Open. Extract.

Math 6 Unit 4 Overview.pdf
Finding the Least Common Multiple. Finding the Greatest Common Factor. Multiplication Facts (0-12). This unit builds to the following future skills and. concepts: Solving Formulas. Distributive Property. Converting Fractions, Decimals, and Percent. A

Unit 4 notes Earthquake .pdf
o Laser-Ranging Devices: Uses laser beams to detect even. tiny fault movements. o Tiltmeters: measures tilting of the ground. o Satellite Monitors: satellite ...

MODULE 4 - UNIT 2 - HANDOUT.pdf
Page 1 of 3. MODULE 4 – UNIT 2. “ADVERTISING”. Language functions. 1. Expressing hypotheses. - If we film here, it will look great. / If we use this guy, the advert will be a disaster. - If you take VitaVit, You'll be fit for life. / We won't s

unit 4 drug abuse.pdf
Whoops! There was a problem loading more pages. Retrying... unit 4 drug abuse.pdf. unit 4 drug abuse.pdf. Open. Extract. Open with. Sign In. Main menu.

MA2262 unit-4.pdf
In other cases the queue may have a finite capacity, such as a waiting room. with limited seating. Number of servers The simplest queueing system is the single ...

unit 4 extension methods
must visit remote and generally unaccessible farms and homes to make the ..... lnust start the training program~e on the appointed date, time and venue. .... remove doubts. s~~perstitions aiid ~ulfavourable att~tudes about the new practice,. 0.

os unit 4.pdf
The file system consists of two distinct parts: a collection of files, each storing. related data, and a directory structure, which organizes and provides information.

rrs unit 4 16marks_NoRestriction.pdf
Shotcrcte is a recent development on the similar principle of guniling for. achieving g'eater thickncss with small coarse aggregate. 'fhere are two process in usc.

Unit-4 Wind Energy.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Unit-4 Wind ...

unit-44 4- BY Civildatas.blogspot.in.pdf
UNIT- 4 QUALITY CONTROL AND SAFETY DURING CONSTRUCTION. (PART A- 2MARKS). 1. Define Acceptance Quality Level(AQL). AQL is defined as the maximum percent defectives that for the purpose of. samples inspection can be considered satisfactory as a progre

ES-unit-4-Envr_POLLUTION_2016.pdf
This can be liquid or solid and are form by combustion or other chemical. process. ... iv) Cleaning the flue gases: If it is not possible to prevent the production of ...

1º ESO Unit 4 glossary.pdf
Page 1 of 1. IES Vasco de la Zarza – Ávila Geography – 1o British. Adriana Carriles García. UNIT 4: The Earth's relief. GLOSSARY 1. UNIT 4 - GLOSSARY. 1. CONTINENTAL AND COASTAL RELIEF. Mountains: elevations of the terrain above the surrounding

Grade 4, Unit 5 Memoir.pdf
Page 1 of 15. 1. 4. th Grade. Writer's Workshop. Unit 5. 3-5 Book 6. Memoir: The Art of Writing Well. The heart of the CSISD Writers Workshop Units of Study stem ...

Unit 4 Retest REVIEW.pdf
Directions: Work must be shown to receive full credit. Figures are NOT ... 5) Find the length of the diagonal of a square with a perimeter of 16 inches. 6) Find the ...