Code Optimization
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
8
Basic Blocks
9
Identifying Basic Blocks (BBs)
10
Identifying Basic Blocks (BBs)
Example:
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
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.
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
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)