0% found this document useful (0 votes)
111 views32 pages

Code Optimization

Uploaded by

Ankita Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views32 pages

Code Optimization

Uploaded by

Ankita Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Code Optimization

Abhimanyu Sahu,
Assistant Professor
Dept. of Computer Science and Engineering
Motilal Nehru National Institute of Technology Allahabad
1
Code Optimization
• Elimination of unnecessary instruction in object code, or the
replacement of one sequence of instruction by a faster sequence of
instruction-that does the same thing is usually called as code
optimization.
• The two constraints on the techniques used to perform code
optimization are:
– They must semantic equivalence with the source program
– Improvement of the program efficiency must be achieved without changing the algorithms
used in the program
• Optimizations also differ in terms of the level at which they are
performed and their scope. This can be performed at two levels:
– Machine dependent optimization: It is performed through a better choice of instruction,
better addressing modes and usage of machine registers.
– Machine independent optimization: It is based on the use of semantic preserving
transformation applied independent to the target machine. This include optimization like
common sub-expression elimination and loop optimization.

2
Structure-Preserving Transformations

3
Structure-Preserving Transformations
• Common subexpression elimination:
– If it was previously computed
– Values of variables have not changed

Ex 2:

4
Structure-Preserving Transformations
• Copy propagation: Assignment of the form f:=g are called copy
statements.
– Use g for f after f=g.

x=a x=a
y=x*b => y=a*b
z=x*c z=a*c
• Dead code Elimination : Remove statements that define variables that
are dead
Example :
D:=0
------
If (D==1) goto L1
L2: ….
5
Loop optimization
• Most execution time of a program is spent in loop.
• Decreasing the number of instructions in an inner loop, improves the
running time of a program
• Loop optimization techniques are : a) Code motion b) Induction
variable elimination & reduction in strength c) loop unrolling and d)
loop jamping
• Code Motion: In this technique, loop invariants are removed from the
loop and placed before it.
while (i<=10) x=y+z;
{ while (i<=10)
=>
x=y+z; {
i=i+1; i=i+1;
} }
6
Loop optimization
• Induction variable Elimination & Reduction in strength:

i=1; t=4;
while (i <= 10) while (t < 40)
{ {
=>
t=t*4; t=t+4;
i=i+1; }
}

• Loop unrolling :
– duplicates the body of the loop multiple times, inorder to decrease number of times of loop
condition is tested.
• Loop jamming :
– Combine the bodies of two adjacent loops that would iterate the same number of times.

7
Basic Blocks and Flow Graphs
• This is used for performing transformations in code optimization

• The various steps involved in the construction of flow graph are:


– Construction of TAC for the source code
– Construction of basic blocks
– Construction of flow graph
– Code optimization
– Construction of DAG for each basic block in the flow graph
– Optimization code

8
Basic Blocks

9
Identifying Basic Blocks (BBs)

10
Identifying Basic Blocks (BBs)
Example:

• statement (1) is a leader by rule 1 B1


• statement (2) is a leader by rule 1
B2 11
Control Flow Graph (CFG)
• Graphical representation of control flow during execution
– An edge represents possible transfer of control between BBs
• Each node represents a statement or a BB
• Dummy entry and exit nodes are often added
• Used for compiler optimizations and static analysis

12
Example of a CFG

13
DAG Representation of Basic Block

14
Construction of DAGs-

Rule-01:
In a DAG,
 Interior nodes always represent the operators.
 Exterior nodes (leaf nodes) always represent the names,
identifiers or constants.
Rule-02:
While constructing a DAG,
 A check is made to find if there exists any node with the same
value.
 A new node is created only when there does not exist any node
with the same value.
 This action helps in detecting the common sub-expressions and
avoiding the re-computation of the same.
Rule-03:
 The assignment instructions of the form x:=y are not performed
unless they are necessary.
Construct DAG for expression a+a*(b-c)+(b-c)*d
Construct DAG

a=b+c
b=b-d
c=c+d
e=b+c
Peephole Optimization

 Peephole optimization is a type of Code Optimization performed on a


small part of the code. It is performed on the very small set of
instructions in a segment of code.
 The small set of instructions or small part of code on which peephole
optimization is performed is known as peephole or window.
 It basically works on the theory of replacement in which a part of code
is replaced by shorter and faster code without change in output.
 Peephole is the machine dependent optimization.
Objectives of Peephole Optimization
The objective of peephole optimization is:
• To improve performance
• To reduce memory footprint
• To reduce code size
Peephole Optimization Techniques
1. Redundant –instruction elimination
2. Unreachable code
3. Flow control optimizations
4. Algebraic simplications
5. Use of machine code
1. Redundant –instruction elimination
In this technique the redundancy is eliminated.
Initial code:
y = x + 5; Example 2:
i = y; If we use the instruction sequence
z = i;
w = z * 3; MOV R0, x
MOV x, R0

Optimized code:
y = x + 5;
i = y;
w = y * 3;
2. Unreachable code
Another opportunity for peephole optimization is the removal of
unreachable code instructions.
• An unlabeled instruction immediately following an unconditional jump
may be removed.

Initial code: TAC:


x=0; x=0
if (x==1) if x=1 goto L1
{ goto L2
L1: a=b
a=b;
L2:
}
3. Flow control optimizations

The unnecessary jumps can be eliminated in either intermediate code or the


target code :

Example: 1 Example 2:

if a<b goto L1
goto L1 goto L2
………
……. …….
L1: goto L2
L1: goto L2 L1: goto L2
……
……
L2: a=b if a<b goto L2
L2: a=b
………
L1: goto L2
4. Algebraic simplications/ strength reduction
Algebraic simplication:
x:=x+0
x:=x*1

Reduction strength:
The operators that consume higher execution time are replaced by the
operators consuming less execution time.
Initial code: Initial code:
y = x * 2; y=x2

Optimized code: Optimized code:


y = x + x; y = x *x;
5. Use of machine code

Auto increment and auto decrement addressing mode:


i=i+1 INC I

i=i-1 DEC i

Null sequences-
Useless operations are deleted.
Combine operations-
Several operations are replaced by a single equivalent operation.
Data-flow Analysis

• These are techniques that derive information about the flow of data
along program execution paths
• An execution path (or path) from point p1 to point pn is a sequence of
points p1, p2, ..., pn such that for each i = 1, 2, ..., n − 1, either
– pi is the point immediately preceding a statement and pi+1 is the point immediately
following that same statement, or
– pi is the end of some block and pi+1 is the beginning of a successor block
• In general, there is an infinite number of paths through a program and
there is no bound on the length of a path
• Program analyses summarize all possible program states that can occur
at a point in the program with a finite set of facts
• No analysis is necessarily a perfect representation of the state
Data-flow Analysis
• A data-flow value for a program point represents an abstraction of the
set of all possible program states that can be observed for that point
• IN[s] and OUT[s]: data-flow values before and after each statement s
• The data-flow problem is to find a solution to a set of constraints on
IN[s] and OUT[s], for all statements s
• Two kinds of constraints
– Those based on the semantics of statements (transfer functions)
– Those based on flow of control
• A DFA schema consists of
– A control-flow graph
– A direction of data-flow (forward or backward)
– A set of data-flow values
– A confluence operator (normally set union or intersection)
– Transfer functions for each block
Data-flow Analysis
• The data-flow equations (constraints)

• If some definitions reach B1 (entry), then IN[B1] is initialized to that set


• Forward flow DFA problem (since OUT[B] is expressed in terms of
IN[B]), confluence operator is
• GEN[B] = set of all definitions inside B that are “visible” immediately
after the block - downwards exposed definitions
• KILL[B] = union of the definitions in all the basic blocks of the flow
graph, that are killed by individual statements in B
Data-flow Analysis-An Example - Pass 1
Data-flow Analysis-An Example - Pass 2
Data-flow Analysis-An Example - Final

You might also like