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

Code Optimization

The document discusses code optimization techniques used by compilers. It aims to produce faster and more efficient target code that is comparable to handwritten code. Some key optimization strategies covered include common subexpression elimination, dead code elimination, loop optimizations like induction variable analysis, and peephole optimizations on the target code to improve performance by examining short sequences of instructions. The document also discusses organizing the optimizer with control and data flow analysis and applying transformations at different stages of compilation.

Uploaded by

rizankyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views25 pages

Code Optimization

The document discusses code optimization techniques used by compilers. It aims to produce faster and more efficient target code that is comparable to handwritten code. Some key optimization strategies covered include common subexpression elimination, dead code elimination, loop optimizations like induction variable analysis, and peephole optimizations on the target code to improve performance by examining short sequences of instructions. The document also discusses organizing the optimizer with control and data flow analysis and applying transformations at different stages of compilation.

Uploaded by

rizankyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Compiler Design

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