Top Banner
Optimization
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Optimization

Optimization

Page 2: Optimization

Introduction

• High-level language constructs can introduce substantial run-time overhead if we naively translate each construct independently into machine code.

• Elimination of unnecessary instructions in object code, or the replacement of one sequence of instructions by a faster sequence of instructions that does the same thing is usually called "code improvement" or "code optimization."

Page 3: Optimization

Optimization Transform the program, that can often be

made to run faster or take les space or both. If we can identify the frequently executed

parts of a program and then make these parts as efficient as possible.

“most programs spend 90% of their execution time in 10% of the code”

Page 4: Optimization
Page 5: Optimization
Page 6: Optimization

Optimization Local Optimization

Transformations are performed by looking only at the statements in a basic block.

Global Optimization Transformations can be performed at both the

local and global levels.

Page 7: Optimization

Local Optimization

Function Preserving Transformations Common Sub Expression elimination. Copy Propagation Dead-Code elimination Constant folding Loop Optimizations Code Motion Induction Variables and Reductions in strength.

Page 8: Optimization

Common Sub Expression Elimination

Expression is called common sub expression if was previously computed, and the value of variables in have not changed since the previous computation.

We can avoid re-computing the expression if we can use the previously computed value.

Page 9: Optimization

Before After

Page 10: Optimization

B5 and B6 after common-sub expression elimination

Page 11: Optimization

Copy Propagation

• The idea behind the copy-propagation transformation is to use for , wherever possible after the copy statement .

• called copy statements, or copies for short.

• Normal algorithm for eliminating common sub expressions introduces copies.

Page 12: Optimization

Copies introduced during common sub expression elimination

Page 13: Optimization

Basic block B5 after copy propagation

Page 14: Optimization

Dead-Code Elimination

• A variable is live at a point in a program if its value can be used subsequently; otherwise, it is dead at that point.

• Dead (or useless)code - statements that compute values that never get used.

• Dead code may appear as the result of previous transformations.

Page 15: Optimization

One advantage of copy propagation is that it often turns the copy statement into dead code.

Dead-code x=t3

Page 16: Optimization

Code Motion

• Loops are a very important place for optimizations, especially the inner loops where programs tend to spend the bulk of their time.

• The running time of a program may be improved if we decrease the number of instructions in an inner loop, even if we increase the amount of code outside that loop.

Page 17: Optimization

Code Motion (Cont.…)

• Takes an expression that yields the same result independent of the number of times a loop is executed (a loop-invariant computation) and evaluates the expression before the loop.

while (i <= limit-2) /* statement does not change l i m i t */

t = limit-2while ( i <= t ) /* statement does not change l i m i t or t */

Code motion will result in the equivalent code

Page 18: Optimization

Induction Variables and Reduction in Strength

• A variable x is said to be an "induction variable“ if there is a positive or negative constant c such that each time x is assigned, its value increases by c.

• The transformation of replacing an expensive operation, such as multiplication, by a cheaper one, such as addition, is known as strength reduction.

Page 19: Optimization

Strength reduction applied to 4 * j in block B3

Page 20: Optimization

Flow graph after induction-variable elimination

Page 21: Optimization

c) Identify any global common sub expressions for each loop.

d) Identify any induction variables for each loop. Be sure to take into account

any constants introduced in (b).

e) Identify any loop-invariant computations for each loop.

For the flow graph in Figure:

a) Identify the loops of the flow graph.

b) Statements (1) and (2) in B1 are both copy statements,

in which a and b are given constant values. For which

uses of a and b can we perform copy propagation and

replace these uses of variables by uses of a constant? Do

so, wherever possible.