Title: Introduction to Program Optimizations Chapter 11
1Introduction to Program Optimizations Chapter 11
2Outline
- Undecidability of optimizations
- Optimization Criteria
- Placement of optimizations
- Early Optimizations
- Constant-Folding
- Scalar replacement of aggregates
- Algebraic simplification
- Value numbering
3Undecidability of Optimizations
- Optimization More efficient generated code
- It is generally undecidable to determine
- If a given optimization always/sometimes leads to
improvement on a given program - If a given optimization is applicable
- Solutions
- Allow optimizations to slow some programs
- Focus on improvements for reasonable programs
- Apply optimization conservatively
4Example Loop Invariant Code Motion
t ?y/z L1 if goto L2 r ?r
t goto L1 L2
L1 if goto L2 t ?y/z r ?r
t goto L1 L2
5Optimization Criteria
- Execution-time
- Code space
- Data space
6Profiling
- Instrument the code to measure certain resource
consumption (profile information) - Optimize the next compile session with profile
information
7More Dimensions for Data-Flow Analysis
- Flow-sensitive
- Context-sensitive
- May vs. Must
8Important Optimizations
- Loop optimizations
- Memory hierarchy optimizations
- Global register optimizations
- Instruction and data cache
- Virtual memory
- Interprocedural optimizations (OO programs)
- Tail call optimizations (recursive programs)
9Importance of Individual Optimizations
- Group I Operate on loops, important for all
programs - constant folding
- algebraic simplifications and reassociations
- global value numbering
- sparse conditional constant propagation
- common-subexpression elimination, loop-invariant
code motions, partial redundency elimination - strength reduction
- induction variable elimination
- dead code elimination
- unreachable code elimination
- graph coloring register allocation
- software pipelining, loop unrolling, register
renaming - branch and basic block scheduling
10Importance of Individual Optimizations
- Group II Operate on loops, important for many
programs - Local and global copy propagation
- leaf-routine optimization
- machine idioms and instruction combining
- branch optimization and loop inversion (while ?
repeat) - unnecessary bound-checking elimination
- branch predication
11Importance of Individual Optimizations
- Group III apply to whole procedures, help other
optimizations - procedure integration
- tail-call elimination
- in-line expansion
- shrink wrapping (move/decrease prologue and
epilogue) - scalar replacement of aggregates
- additional control flow optimizations
(straithening, if simplification, unswitching,
conditional moves) - Group IV Decrease code size
- code hoisting
- tail merging
12Order of Optimizations and Repetitions
- Transformations are order-dependent
- No Optimal order
- May need to repeat certain transformations
13Textbook Order
constant-folding simplifications
14Early High Level Optimizations (B) HIRMIR
Procedure integration Tail-call
elimination Scalar replacement of
aggregates Sparse conditional constant
propagation Interprocedural constant
propagation Procedure specialization and
cloning Sparse conditional constant propagation
constant-folding simplifications
15MIRLIR(C)
Global value numbering Local and global copy
propagation Sparse conditional constant
propagation Dead code elimination
constant-folding simplifications
Common Subexpression Elimination Loop invariant
code motion
Partial redundency Elimination
Dead code elimination code hoisting induction
variable strength reduction linear function test
replacement induction variable removal bound
checking elimination, control flow optimizations
16LIR(D)
Inline expansion Leaf-routine optimizations Shrink
wrapping Machine idioms Tail merging Branch
optimization and conditional moves Dead code
elimination Software pipelining, Instruction
Scheduling 1 Register allocation Instruction
Scheduling 2 Intraprocedural I-cache
optimizations Instruction prefetching Data
prefertching Branch predication
constant-folding simplifications
17Link-time optimizations(E)
Interprocedural register allocation Aggregation
global references Interprcudural I-cache
optimizations
18Scalar replacement of array references Data-cache
optimizations
Procedure integration Tail-call
elimination Scalar replacement of
aggregates Sparse constant propagation Interproced
ural constant propagation Procedure
specialization and cloning Sparse conditional
constant propagation
Inline expansion Leaf-routine optimizations Instru
ction Scheduling 1 Register allocation Instruction
Scheduling 2 Intraprocedural I-cache
optimizations Instruction prefetching Data
prefertching Branch predication
Global value numbering Local and global copy
propagation Sparse conditional constant
propagation Dead code elimination
Common Subexpression Elimination Loop invariant
code motion
Partial redundency Elimination
Interprocedural register allocation Interprocedura
l I-cache optimization