Transcript

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."

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”

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.

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.

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.

Before After

B5 and B6 after common-sub expression elimination

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.

Copies introduced during common sub expression elimination

Basic block B5 after copy propagation

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.

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

Dead-code x=t3

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.

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

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.

Strength reduction applied to 4 * j in block B3

Flow graph after induction-variable elimination

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.

top related