Unit 4
Unit 4
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
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
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.
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).
Here the statements do not execute in a sequence one after the other.Thus, they do not form a
basic block.
Any given code can be partitioned into basic blocks using the following rules
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.
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.
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:
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
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.
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.
2.Induction-Variable Elimination
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.
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.
1. To improve performance
2. To reduce memory footprint
3. To reduce code size
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.