Download presentation
Presentation is loading. Please wait.
1
Introduction to Program Optimizations Chapter 11 Mooly Sagiv
2
Outline Undecidability of optimizations Optimization Criteria Placement of optimizations Early Optimizations –Constant-Folding –Scalar replacement of aggregates –Algebraic simplification –Value numbering
3
Undecidability 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
4
Example: Loop Invariant Code Motion L1: if … goto L2 … t y/z r r + t … goto L1 L2: t y/z L1: if … goto L2 … r r + t … goto L1 L2:
5
Optimization Criteria Execution-time Code space Data space
6
Profiling Instrument the code to measure certain “resource” consumption (profile information) Optimize the next compile session with profile information
7
More Dimensions for Data-Flow Analysis Flow-sensitive Context-sensitive May vs. Must
8
“Important” Optimizations Loop optimizations Memory hierarchy optimizations –Global register optimizations –Instruction and data cache –Virtual memory Interprocedural optimizations (OO programs) Tail call optimizations (recursive programs)
9
Importance 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
10
Importance 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
11
Importance 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
12
Order of Optimizations and Repetitions Transformations are order-dependent No “Optimal order” May need to repeat certain transformations
13
Textbook Order Scalar replacement of array references Data-cache optimizations A HIR Global value numbering … C MIR|LIR Procedure integration … B HIR|MIR In-line expansion … D LIR Interprocedural register allocation … E link-time constant-folding simplifications
14
Procedure integration Tail-call elimination Scalar replacement of aggregates Sparse conditional constant propagation Interprocedural constant propagation Procedure specialization and cloning Sparse conditional constant propagation Early High Level Optimizations (B) HIR|MIR constant-folding simplifications
15
MIR|LIR(C) constant-folding simplifications 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 Dead code elimination code hoisting induction variable strength reduction linear function test replacement induction variable removal bound checking elimination, control flow optimizations
16
LIR(D) constant-folding simplifications 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
17
Link-time optimizations(E) Interprocedural register allocation Aggregation global references Interprcudural I-cache optimizations
18
Scalar replacement of array references Data-cache optimizations Procedure integration Tail-call elimination Scalar replacement of aggregates Sparse constant propagation Interprocedural constant propagation Procedure specialization and cloning Sparse conditional constant propagation 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 Inline expansion Leaf-routine optimizations Instruction Scheduling 1 Register allocation Instruction Scheduling 2 Intraprocedural I-cache optimizations Instruction prefetching Data prefertching Branch predication Interprocedural register allocation Interprocedural I-cache optimization
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.