0% found this document useful (0 votes)
62 views25 pages

Code Optimization

Code optimization aims to produce faster and more efficient target code by improving the source, intermediate, and target code through various transformations and optimizations. These include common subexpression elimination, redundant instruction removal, loop optimizations, and peephole optimizations. The goal is to generate code comparable to handwritten assembly through optimizations that preserve meaning while improving speed and reducing space usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
62 views25 pages

Code Optimization

Code optimization aims to produce faster and more efficient target code by improving the source, intermediate, and target code through various transformations and optimizations. These include common subexpression elimination, redundant instruction removal, loop optimizations, and peephole optimizations. The goal is to generate code comparable to handwritten assembly through optimizations that preserve meaning while improving speed and reducing space usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 25

CODE OPTIMIZATION

1
Aim of Code Optimization
• Producing target code comparable to hand
written code
• Faster running code, occupying lesser
space
• Optimizing Compilers
• Bottlenecks
• Profiling
• Loops
2
Criteria for code Improving
Transformations
• Meaning Preserving
• Speedup program considerably
• Must be worth the effort

3
Places for Improvement
• Source Code
– User can profile program, change algorithm,
transform loops
• Intermediate code
– Compiler can improve loops, procedure calls,
address calculations
• Target code
– Compiler can use registers, select
instructions, peephole transformations

4
Constraints
• Some languages may not permit certain
code improvements
– Array accesses in C Vs FORTRAN
• Compiler must use machine’s resources
efficiently
– Registers to accommodate heavily used
variables
– Use a variety of Addressing modes

5
Organization of Code Optimizer
• Control Flow Analysis
• Data Flow Analysis
• Transformations
• Advantages
– Intermediate code reflects HLL – can be
optimized
– Target machine independence

6
Basic Block
• Sequence of consecutive statements in
which flow of control enters at the
beginning and leaves at the end without
halt or possibility of branching

7
Partitioning into Basic Blocks
• Identify leaders
– First Statement
– Any statement that is the target of a
conditional or unconditional goto
– Any statement immediately following a goto /
conditional goto
• Group all statements from one leader to
next

8
9
Intermediate Code for Segment of QuickSort
Basic Block
partitioning and
Flow Graph
Construction

10
Transformations on Basic Blocks
• Two Basic blocks are said to be equivalent if
they compute the same set of expressions
• Types of transformations
– Structure Preserving
– Algebraic
• Structure Preserving Transformations
– Common Sub-expression elimination
– Dead-code elimination
– Renaming Temporary variables
– Interchanging statements

11
Common Sub Expression
elimination

a:=b+c a:=b+c
b:=a–d b:=a–d
c:=b+c c:=b+c
d:=a–d d:=b
Before After

12
Renaming temporary variables
• t = b + c is changed to u = b + c where u is
a new temporary variable and all instances
of t are changed to u then the basic block
remains unchanged
• Normal-form basic block

13
Interchange of Statements
• Two statements
t1 : = b + c and
t2 : = x + y can be interchanged
without affecting the value of the block if
and only if neither x nor y is t1 and neither
b nor c is t2.

14
Algebraic Transformations
• Statements such as:
x := x + 0 and x := x * 1 can be
eliminated
• x : = y ** 2 can be replaced by x := y * y

15
Principal Sources of Optimization
• Local Vs Global
• Function Preserving transformations
– Common Sub Expression Elimination
– Copy Propagation
– Dead code Elimination
• Constant folding
– Loop Optimization
• Code Motion
• Induction Variables and Reduction in Strength

16
Local Optimization – Common Sub
Expression Elimination
17
After Global
Common Sub
Expression
elimination

18
Strength
Reduction

19
Copy
Propagation
and Dead
Code
Elimination in
Blocks 5 and 6

20
Peephole Optimization
• Performed after code generation
• Attempts to improve the performance of
the target program by examining a short
sequence of target instructions – peephole
• Peephole is a small moving window of the
target program – not necessarily
contiguous
• Repeated passes maybe carried out
21
Redundant Instruction elimination
• Redundant Loads and Stores
(a) MOV R0, a (b) MOV a, R0
Here (b) can be eliminated provided there is no label
attached
• Unreachable Code
# define debug 0
if(debug)
{
print debugging information
}
22
Redundant Instruction elimination
(contd.)
• Intermediate code rep:
if debug = 1 goto L1
goto L2
L1: print debugging information
L2:
• Can eliminate Jumps over jumps
if debug <> 1 goto L2
print debugging information
L2:
• Here debug = 0 so only goto L2 will be carried out –
dead code
23
Flow of Control Optimizations
• Elimination of Unnecessary jumps
goto L1 goto L2
L1: goto L2 L2: goto L2
Before After
if a < b goto L1 if a < b goto L2
L1: goto L2 L1: goto L2
Before After

24
Other optimizations
• Algebraic Simplification
• Reduction in Strength
• Use of Machine Idioms
– Usage of auto increment and auto decrement
modes

25

You might also like