CD Unit V
CD Unit V
UNIT- V
Code Optimization
Introduction:
• It is possible for programmer to perform compiler using his or her knowledge in choosing
better algorithm and data items.
• Optimization is process of transformation of code to an efficient code in terms of space and
time without changing the meaning of code.
• Following constraints are to be considered while applying techniques for code
improvements.
1. Transformation preserves meaning of program. i.e., target code should ensure semantic
equivalence with source program.
2. Program efficiency must be improved by measurable amount without changing
algorithm used in program.
3. When technique is applied on special format, then it is worth transforming to that
format only.
4. Some transformations are applied only after detailed analysis, which is time
consuming. It is not worthy if program runs very few number of times.
2. Copy propagation:
• If there are copy statements of form x=y then in copy propagation use of x is
replaced by y in subsequent expressions.
• Copy propagation is possible if none of the variable is changed after this argument.
Example: consider following code:
a=b+c
d=c //copy statement
........
e=b+d-3
10 marks
LOOPS IN FLOW GRAPH
• A graph representation of three-address statements, called a flow graph, is useful for
understanding code-generation algorithms, even if the graph is not explicitly
constructed by a code-generation algorithm. Nodes in the flow graph represent
computations, and the edges represent the flow of control.
Dominators:
• In a flow graph, a node d dominates node n, if every path from initial node of the flow
graph to n goes through d. This will be denoted by d dom n.
• Every initial node dominates all the remaining nodes in the flow graph and the entry of
a loop dominates all nodes in the loop. Similarly every node dominates itself.
Example:
Flow graph
*In the flow graph above,
• Initial node, node1 dominates every node.
• node 2 dominates itself
• node 3 dominates all but 1 and 2.
• node 4 dominates all but 1, 2 and 3.
• node 5 and 6 dominates only themselves ,since flow of control can skip around
either by going through the other.
• node 7 dominates 7,8 ,9 and 10.
• node 8 dominates 8,9 and 10.
• node 9 and 10 dominates only themselves.
• The way of presenting dominator information is in a tree, called the dominator tree in
which the initial node is the root.
• The parent of each other node is its immediate dominator. Each node d dominates only
its descendents in the tree.
Dominator tree
D(1)={1} D(6)={1,3,4,6}
D(2)={1,2} D(7)={1,3,4,7}
D(3)={1,3} D(8)={1,3,4,7,8}
D(4)={1,3,4} D(9)={1,3,4,7,8,9}
D(5)={1,3,4,5} D(10)={1,3,4,7,8,10}
Natural Loop:
• One application of dominator information is in determining the loops of a flow graph
suitable for improvement.
• The properties of loops are
• A loop must have a single entry point, called the header. This entry point-dominates all
nodes in the loop, or it would not be the sole entry to the loop.
• There must be at least one way to iterate the loop(i.e.)at least one path back to the
header.
• One way to find all the loops in a flow graph is to search for edges in the flow graph
whose heads dominate their tails. If a→b is an edge, b is the head and a is the tail. These
types of edges are called as back edges.
Example:
In the above graph,
7 → 4 4 DOM 7
10 →7 7 DOM 10
4→3
8→3
9 →1
Inner loop:
• If we use the natural loops as “the loops”, then we have the useful property that unless
two loops have the same header, they are either disjointed or one is entirely contained
in the other. Thus, neglecting loops with the same header for the moment, we have a
natural notion of inner loop: one that contains no other loop.
• When two natural loops have the same header, but neither is nested within the other,
they are combined and treated as a single loop.
Pre-Headers:
• Several transformations require us to move statements “before the header”.
Therefore begin treatment of a loop L by creating a new block, called the preheater.
• The pre-header has only the header as successor, and all edges which formerly
entered the header of L from outside L instead enter the pre-header.
• Edges from inside loop L to the header are not changed.
• Initially the pre-header is empty, but transformations on L may place statements in it.
5 marks
Constant Propagation:
• Constant Propagation is process of recognizing and evaluating constant expressions
at compile time rather than run time.
Example: Pi=2217
void area_per(int r)
{
float area, perimeter;
area = Pi * r * r;
print area, perimeter;
}
• At line 1, variable Pi is constant and its value 3.413 is assigned at compile time.
• At line 5 and 6 value of Pi is assigned with 3.413 in runtime.
Benefits of PRE:
• We can significantly improve the performance of a program by eliminating
partial redundancies.
1. Execution Time: PRE will reduce the number of instructions that are needed
to be executed to run a program. Since our system has fewer instructions to
execute, it will result in a faster execution time.
2. Power Consumption: PRE will reduce the number of instructions that are
needed to be executed, which will save us power and make our system more
power efficient.
3. Readability: PRE will improve the readability of our program by removing
repeated code and redundancies, which will make it easier for the developers
to read and maintain the code.
4. Debug: PRE will remove unwanted or unnecessary code from our code, which
will make debugging easier and enable developers to find and correct any
errors in the code.
PRE algorithms:
There are various algorithms that are available in the market for our use. Some of the
algorithms that are used worldwide are:
1. Lazy Code Motion Algorithm (LCM)
2. Global Value Numbering Algorithm (GVN).
The Lazy Code Motion Algorithm (LCM):
• The Lazy code motion algorithm (LCM) aims to minimize the total number of
computations that is to be performed to execute a program.
• LCM is the algorithm that works by identifying partially redundant expressions.
And then, it will move them to a point in the control flow graph where they are
guaranteed to be executed at most once.
• It is a Four-Step Algorithm:
1. The first step uses anticipation to determine where the expression can be
placed.
2. The second step will be computing the earliest and latest points for each
expression.