0% found this document useful (0 votes)
35 views13 pages

Optimization of Basic Blocks

The document discusses optimization techniques for basic blocks in compiler design, including common subexpression elimination, dead code elimination, renaming of temporary variables, and the interchange of independent statements. It also covers algebraic transformations, dominators in flow graphs, natural loops, and peephole optimizations to enhance code efficiency. Additionally, it highlights the importance of reducing strength and utilizing machine idioms for better performance.

Uploaded by

samabi236
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)
35 views13 pages

Optimization of Basic Blocks

The document discusses optimization techniques for basic blocks in compiler design, including common subexpression elimination, dead code elimination, renaming of temporary variables, and the interchange of independent statements. It also covers algebraic transformations, dominators in flow graphs, natural loops, and peephole optimizations to enhance code efficiency. Additionally, it highlights the importance of reducing strength and utilizing machine idioms for better performance.

Uploaded by

samabi236
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/ 13

SUBJECT CODE

TYPE THE SUBJECT NAME HERE

UNIT NO 5
CODE OPTIMIZATION

5.4 OPTIMIZATION OF BASIC BLOCKS

III VI
20CSPC602
COMPILER DESIGN
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

The primary Structure-Preserving Transformation on basic blocks are:

Common subexpression elimination


Dead code elimination
Renaming of temporary variables
Interchange of two independent adjacent statements.

Common subexpression elimination:

Common sub expressions need not be computed over and over again.

Instead they can be computed once and kept in store from where it is referenced when encountered
again – of course providing the variable values in the expression still remain constant.
Example: Basic block can be transformed to
a: =b+c a: = b+c
b: =a-d b: = a-d
c: =b+c c: = a
d: =a-d d: = b
The 2nd and 4th statements compute the same expression: b+c and a-d
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

Dead code elimination:


It's possible that a large amount of dead (useless) code may exist in the program.
This might be especially caused when introducing variables and procedures as part of construction
or error-correction of a program – once declared and defined, one forgets to remove them in case
they serve no purpose.
Eliminating these will definitely optimize the code.

Renaming of temporary variables:


A statement
t:=b+c
where t is a temporary name can be changed to u:=b+c where u is another temporary name, and
change all uses of t to u.
In this we can transform a basic block to its equivalent block called normal-form block.

Interchange of two independent adjacent statements:


Two statements
t1:=b+c
t2:=x+y
can be interchanged or reordered in its computation in the basic block when value of t1
does not affect the value of t2.
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

Algebraic Transformations:
Algebraic identities represent another important class of optimizations on basic blocks. This
includes simplifying expressions or replacing expensive operation by cheaper ones i.e. reduction in
strength.

Associative laws may also be applied to expose common sub expressions. For example, if the
source code has the assignments
a :=b+c
e :=c+d+b
the following intermediate code may be generated:
a :=b+c
t :=c+d
e :=t+b

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.
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

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.

*Initial node,node1 dominates every node.


*node 2 dominates itself
*node 3 dominates all but not 1 and 2.
*node 4 dominates all but not 1,2 and 3.
*node 5 and 6 dominates only
themselves,since flow of control can skip
around either by goin 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.
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

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.

The existence of dominator tree follows from a


property of dominators; each node has a
unique immediate dominator in that is the last
dominator of n on any path from the initial node
to n. In terms of the dom relation, the
immediate dominator m has the property is
d=!n and d dom n, then d dom m.
D(1)={1} D(2)={1,2}
D(3)={1,3}
D(4)={1,3,4}
D(5)={1,3,4,5}
D(6)={1,3,4,6}
D(7)={1,3,4,7}
D(8)={1,3,4,7,8}
D(9)={1,3,4,7,8,9}
D(10)={1,3,4,7,8,10}
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

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
The above edges will form a loop in the flow graph.
Given a back edge n → d, we define the natural loop of the edge to be d plus the set of nodes that
can reach n without going through d. Node d is the header of the loop.
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

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.

PEEPHOLE OPTIMIZATION
Redundant-instructions elimination
Flow-of-control optimizations
Algebraic simplifications
Use of machine idioms
Unreachable Code
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

Redundant Loads And Stores:


If we see the instructions sequence
(1) MOV R0,a
(2) MOV a,R0
we can delete instructions (2) because whenever (2) is executed. (1) will ensure that the value of a
is already in register R0.If (2) had a label we could not be sure that (1) was always executed
immediately before (2) and so we could not remove (2).

Flows-Of-Control Optimizations:
The unnecessary jumps can be eliminated in either the intermediate code or the target code by the
following types of peephole optimizations. We can replace the jump sequence

goto L1
....
L1: gotoL2
by the sequence
goto L2
....
L1: goto L2
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

If there are now no jumps to L1, then it may be possible to eliminate the statement L1:goto
L2 provided it is preceded by an unconditional jump .Similarly, the sequence
if a < b goto L1
....
L1: goto L2
can be replaced by
If a < b goto L2
....
L1: goto L2
Finally, suppose there is only one jump to L1 and L1 is preceded by an unconditional goto. Then the
sequence
goto L1
........
L1: if a < b goto L2
L3: .........................................(1)
May be replaced by
If a < b goto L2
goto L3
.......
L3: ........................................(2)
20CSPC602
COMPILER DESIGN

OPTIMIZATION OF BASIC BLOCKS

Reduction in Strength:
Reduction in strength replaces expensive operations by equivalent cheaper ones on the target
machine. Certain machine instructions are considerably cheaper than others and can often be used
as special cases of more expensive operators.
For example, x2 is invariably cheaper to implement as x*x than as a call to an exponentiation
routine. Fixed-point multiplication or division by a power of two is cheaper to implement as a shift.
Floating-point division by a constant can be implemented as multiplication by a constant, which may
be cheaper.
X
2 → X*X
Use of Machine Idioms:
The target machine may have hardware instructions to implement certain specific operations
efficiently. For example, some machines have auto-increment and auto-decrement addressing
modes. These add or subtract one from an operand before or after using its value. The use of these
modes greatly improves the quality of code when pushing or popping a stack, as in parameter
passing. These modes can also be used in code for statements like
i :=i+1.
i:=i+1 → i++
i:=i-1 → i- -
20CSPC602
COMPILER DESIGN

VIDEO

https://www.youtube.com/watch?v=OKSlupcFGjc

https://www.youtube.com/watch?v=fwJnIqv80jc
20CSPC602
COMPILER DESIGN

Thank You

You might also like