CS6109 Module 12
CS6109 Module 12
Module – 12
Presented By
Dr. S. Muthurajkumar,
Assistant Professor,
Dept. of CT, MIT Campus,
Anna University, Chennai.
MODULE - 12
Principle sources of optimization
Optimization in Basic blocks
DAG
Structure Preserving transformation
Functional transformation
Loop optimization
Peep hole optimization
2
PRINCIPLE SOURCES OF
OPTIMIZATION
A compiler optimization must preserve the semantics of the original program.
Except in very special circumstances, once a programmer chooses and
implements a particular algorithm, the compiler cannot understand enough
about the program to replace it with a substantially different and more efficient
algorithm.
A compiler knows only how to apply relatively low-level semantic
transformations, using general facts such as algebraic identities like i+0 = i or
program semantics such as the fact that performing the same operation on the
same values yields the same result.
3
EXAMPLE
C code for quicksort
4
EXAMPLE
Three-address code for fragment of quicksort
5
EXAMPLE
t6 = 4*i
x = a[t6]
as shown in steps (14) and (15). Similarly, a[j] = x becomes
t10 = 4*j
a[t10] = x
in steps (20) and (21).
6
EXAMPLE
Flow graph for the quicksort fragment
7
Semantics-Preserving Transformations
There are a number of ways in which a compiler can improve a program
without changing the function it computes.
Common-subexpression elimination, copy propagation, dead-code elimination,
and constant folding are common examples of such function-preserving (or
semantics-preserving) transformations; we shall consider each in turn.
9
Semantics-Preserving Transformations
B5 and B6 after
common-subexpression elimination
10
Semantics-Preserving Transformations
Global Common Subexpressions
t8 = 4*j
t9 = a[t8]
a[t8] = x
in B5 can be replaced by
t9 = a[t4]
a[t4] = x
using t4 computed in block B3.
The statements
t9 = a[t4]
a[t6] = t9
in B5 therefore can be replaced by
a[t6] = t5
11
Semantics-Preserving Transformations
Copy Propagation
Copies introduced during common subexpression elimination
12
Semantics-Preserving Transformations
Copy Propagation
x = t3
a[t2] = t5
a[t4] = t3
goto B2
Basic block B5 after copy propagation
13
Semantics-Preserving Transformations
Dead-Code Elimination
a[t2] = t5
a[t4] = t3
goto B2
This code is a further improvement of block B5.
14
Semantics-Preserving Transformations
Code Motion
Loops are a very important place for optimizations, especially the inner loops
where programs tend to spend the bulk of their time.
The running time of a program may be improved if we decrease the number of
instructions in an inner loop, even if we increase the amount of code outside
that loop.
An important modification that decreases the amount of code in a loop is code
motion.
This transformation takes an expression that yields the same result independent
of the number of times a loop is executed (a loop-invariant computation) and
evaluates the expression before the loop.
15
Semantics-Preserving Transformations
Induction Variables and Reduction in Strength
Another important optimization is to find induction variables in loops and
optimize their computation.
A variable x is said to be an “induction variable” if there is a positive or
negative constant c such that each time x is assigned, its value increases by c.
16
Semantics-Preserving Transformations
Induction Variables and
Reduction in Strength
Strength reduction
applied to 4 * j in block B3
17
Semantics-Preserving Transformations
Induction Variables and
Reduction in Strength
18
PEEPHOLE OPTIMIZATION
A simple but effective technique for locally improving the target code is
peephole optimization, which is done by examining a sliding window of target
instructions (called the peephole) and replacing instruction sequences within
the peephole by a shorter or faster sequence, whenever possible.
Peephole optimization can also be applied directly after intermediate code
generation to improve the intermediate representation.
The peephole is a small, sliding window on a program.
The code in the peephole need not be contiguous, although some
implementations do require this.
It is characteristic of peephole optimization that each improvement may spawn
opportunities for additional improvements.
19
PEEPHOLE OPTIMIZATION
The program transformations that are characteristic of peephole optimizations:
Redundant-instruction elimination
Flow-of-control optimizations
Algebraic simplifications
Use of machine idioms
20
PEEPHOLE OPTIMIZATION
Eliminating Redundant Loads and Stores
If we see the instruction sequence
LD R0, a
ST a, R0
in a target program, we can delete the store instruction because whenever it is
executed, the first instruction will ensure that the value of a has already been
loaded into register R0.
21
PEEPHOLE OPTIMIZATION
Eliminating Unreachable Code
Another opportunity for peephole optimization is the removal of unreachable
instructions. An unlabeled instruction immediately following an unconditional
jump may be removed. This operation can be repeated to eliminate a sequence
of instructions.
In the intermediate representation, this code may look like
if debug == 1 goto L1
goto L2
L1: print debugging information
L2:
One obvious peephole optimization is to eliminate jumps over jumps.
22
PEEPHOLE OPTIMIZATION
Eliminating Unreachable Code
Thus, no matter what the value of debug, the code sequence above can be
replaced by
if debug != 1 goto L2
print debugging information
L2:
If debug is set to 0 at the beginning of the program, constant propagation would
transform this sequence into
if 0 != 1 goto L2
print debugging information
L2:
Now the argument of the first statement always evaluates to true, so the statement
can be replaced by goto L2. Then all statements that print debugging information
are unreachable and can be eliminated one at a time.
23
PEEPHOLE OPTIMIZATION
Flow-of-Control Optimizations
Simple intermediate code-generation algorithms frequently produce jumps to
jumps, jumps to conditional jumps, or conditional jumps to jumps.
These 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 sequence
goto L1
…
L1: goto L2
by the sequence
goto L2
….
L1: goto L2
24
PEEPHOLE OPTIMIZATION
Flow-of-Control Optimizations
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 the sequence
if a < b goto L2
…
L1: goto L2
25
PEEPHOLE OPTIMIZATION
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:
may be replaced by the sequence
if a < b goto L2
goto L3
...
L3:
While the number of instructions in the two sequences is the same, we sometimes
skip the unconditional jump in the second sequence, but never in the first. Thus,
the second sequence is superior to the first in execution time.
26
PEEPHOLE OPTIMIZATION
Algebraic Simplification and Reduction in Strength
These algebraic identities can also be used by a peephole optimizer to eliminate
three-address statements such as
x=x+0
or
x=x*1
in the peephole.
Similarly, reduction-in-strength transformations can be applied in the peephole
to replace 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.
27
PEEPHOLE OPTIMIZATION
Use of Machine Idioms
The target machine may have hardware instructions to implement certain
specific operations efficiently.
Detecting situations that permit the use of these instructions can reduce
execution time significantly.
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 the 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 x = x+1.
28
REFERENCE
1. Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, “Compilers:
Principles, Techniques and Tools”, Second Edition, Pearson Education Limited,
2014.
2. Randy Allen, Ken Kennedy, “Optimizing Compilers for Modern Architectures:
A Dependence-based Approach”, Morgan Kaufmann Publishers, 2002.
3. Steven S. Muchnick, “Advanced Compiler Design and Implementation”,
Morgan Kaufmann Publishers - Elsevier Science, India, Indian Reprint, 2003.
4. Keith D Cooper and Linda Torczon, “Engineering a Compiler”, Morgan
Kaufmann Publishers, Elsevier Science, 2004.
5. V. Raghavan, “Principles of Compiler Design”, Tata McGraw Hill Education
Publishers, 2010.
6. Allen I. Holub, “Compiler Design in C”, Prentice-Hall Software Series, 1993.
29
30