0% found this document useful (0 votes)
7 views16 pages

Unit 4

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

UNIT-4

Code Optimization:
Code optimization is: “the process of improving the efficiency, performance, and overall
quality of software code without changing its functionality

Code Optimization tries to improve the intermediate code by making it consume fewer
resources (i.e. CPU, Memory) so that faster-running machine code will result

Optimizing process should meet the following objectives


Optimization must be correct; it must not change the meaning of the program.

Optimization should increase the speed and performance of the program.

Compilation time must be kept reasonable

Optimization process should not delay the overall compiling process.

Principal Sources of Optimization


Basic Principal Sources of optimization are

Compile Time Evaluation:

Compile time evaluation means that the value of some variable was determined when
everything is compiled into object files.

Run time evaluation would mean that the value of some variable has to be determined at
runtime
Variable Propagation:

Replacing the variables with Known constants at complile time only which can improve the
optimization technique.

Constant Propagation:

If the value of a variable is a constant, then replace the variable with the constant. The
variable may not always be a constant.

Constant Folding:

Consider an expression : a = b op c and the values b and c are constants, then the value of
a can be computed at compile time.
Common Sub Expression Elimination

In the above example, a*b and x*b is a common sub expression so eliminating x and
replacing with a

Dead Code Elimination:

A variable is said to be dead if it is never used after its last definition.In order to find the
dead variables, a data flow analysis should be done.

Unreachable Code Elimination:


The block which does not have an incoming edge is an Unreachable code block. After
constant propagation and constant folding, the unreachable branches can be eliminated.

Induction Variable and Strength Reduction:

An induction variable is used in the loop for the following kind of assignment i = i +
constant. It is a kind of Loop Optimization Technique.

Strength reduction means replacing the high strength operator with a low strength
Basic Blocks
Basic block is a set of statements that always executes in a sequence one after the other.

Basic block contains a sequence of statement. The flow of control enters at the beginning of the
statement and leave at the end without any halt (except may be the last instruction of the block).

Characteristics of basic blocks are

 They do not contain any kind of jump statements in them.


 There is no possibility of branching or getting halt in the middle.
 All the statements execute in the same order they appear.
 They do not lose lose the flow control of the program.

Example Of Basic Block

Three Address Code for the expression a = b + c + d as basic block is


Here all the statements execute in a sequence one after the other. Thus, they form a basic block.

Example Of Not A Basic Block

Three Address Code for the expression If A<B then 1 else 0 is

Here the statements do not execute in a sequence one after the other.Thus, they do not form a
basic block.

Partitioning Intermediate Code Into Basic Blocks

Any given code can be partitioned into basic blocks using the following rules

Rule 1: Determining Leaders

Following statements of the code are called as Leaders

 First statement of the code.


 Statement that is a target of the conditional or unconditional goto statement.
 Statement that appears immediately after a goto statement.

Rule 2: Determining Basic Blocks

All the statements that follow the leader (including the leader) till the next leader appears form
one basic block. The first statement of the code is called as the first leader. The block containing
the first leader is called as Initial block
Consider the following source code for dot product of two vectors a and b of length 10

The three address code for the above source program is given below:
Optimization of basic Blocks
Optimization is applied to the basic blocks after the intermediate code generation phase of
the compiler. In optimization, high-level codes are replaced by their equivalent efficient low-
level codes.

Optimization of basic blocks can be machine-dependent or machine-independent.

There are two types of basic block optimizations

The structure-preserving transformation on basic blocks includes

1. Dead Code Elimination

Dead code is defined as that part of the code that never executes during the program
execution. So, for optimization, such code or dead code is eliminated
2. Common Sub Expression Elimination

The sub-expression which are common are used frequently are calculated only once and
reused when needed. DAG ( Directed Acyclic Graph ) is used to eliminate common sub
expressions.

3. Renaming of Temporary variables

Statements containing instances of a temporary variable can be changed to instances of a


new temporary variable without changing the basic block value.

Example: Statement t = a + b can be changed to x = a + b where t is a temporary variable


and x is a new temporary variable without changing the value of the basic block.

4. Interchange of two independent adjacent statements

If a block has two adjacent statements which are independent can be interchanged without
affecting the basic block value.
These two independent statements of a block can be interchanged without affecting the
value of the block.

Algebraic Transformation:

Algebraic transformations can be used to change the set of expressions computed by a


basic block into an algebraically equivalent set

1. Constant Folding

Solve the constant terms which are continuous so that compiler does not need to solve this
expression.

2. Copy Propagation
It is of two types, Variable Propagation, and Constant Propagation.

3. Strength Reduction
Replace expensive statement/ instruction with cheaper ones.
Flow Graphs

Flow graph is a directed graph. It contains the flow of control information for the set of basic block.

A control flow graph is used to depict that how the program control is being parsed among the
blocks. It is useful in the loop optimization

Flow graph for the vector dot product is given as follows:

 Block B1 is the initial node. Block B2 immediately follows B1, so from B2 to B1 there is
an edge.
 The target of jump from last statement of B1 is the first statement B2, so from B1 to B2
there is an edge.
 B2 is a successor of B1 and B1 is the predecessor of B2.

LOOP OPTIMIZATION
Loop optimization is most valuable machine-independent optimization because program's
inner loop takes bulk to time of a programmer.

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

For loop optimization the following three techniques are important:


1. Code motion
2. Induction-variable elimination
3. Strength reduction

1.Code Motion:
Code motion is used to decrease the amount of code in loop. This transformation takes a
statement or expression which can be moved outside the loop body without affecting the
semantics of the program.

In the while statement, the limit-2 equation is a loop invariant equation.

2.Induction-Variable Elimination

Induction variable elimination is used to replace variable from inner loop.

It can reduce the number of additions in a loop. It improves both code space and run time
performance.
In the above figure, we can replace the assignment t4:=4*j by t4:=t4-4.

The only problem which will be arose that t4 does not have a value when we enter block B2 for the
first time. So we place a relation t4=4*j on entry to the block B2.

3. Reduction in Strength
Strength reduction is used to replace the expensive operation by the cheaper once on the
target machine.

Addition of a constant is cheaper than a multiplication. So we can replace multiplication with


an addition within the loop.

Multiplication is cheaper than exponentiation. So we can replace exponentiation with


multiplication within the loop.

In the above code, it is cheaper to compute s=s+6 than j=3 *i


DATA FLOW ANALYSIS
Data flow analysis is a global code optimization technique. The compiler performs code
optimization efficiently by collecting all the information about a program and distributing it
to each block of its control flow graph (CFG). This process is known as data flow analysis.

Since optimization has to be done on the entire program, the whole program is examined,
and the compiler collects information.

Note: The control flow graph represents a program in blocks depicting how the control of the
program is being passed from one block to another.

Basic Technologies
Below are some basic terminologies related to data flow analysis.

 Definition Point- A definition point is a point in a program that defines a data item.
 Reference Point- A reference point is a point in a program that contains a reference to a
data item.
 Evaluation Point- An evaluation point is a point in a program that contains an
expression to be evaluated.

The below diagram shows an example of a definition point, a reference point, and an
evaluation point in a program.
PREEHOLE OPTMIZATION
Peephole optimization is a type of code Optimization performed on a small part of the
code. It is performed on a very small set of instructions in a segment of code.

It basically works on the theory of replacement in which a part of code is replaced by shorter
and faster code without a change in output. The peephole is machine-dependent
optimization.

Objectives of Peephole Optimization:

1. To improve performance
2. To reduce memory footprint
3. To reduce code size

Some of Peephole Optimization Techniques

Redundant load and store elimination

This technique used to redundancy is eliminated.

Constant folding:

The code that can be simplified by the user itself, is simplified. Here simplification to be done
at runtime are replaced with simplified code to avoid additional computation.
Strength Reduction:

The operators that consume higher execution time are replaced by the operators consuming
less execution time.

You might also like