180 likes | 200 Views
This chapter provides an overview of program optimizations, including undecidability, optimization criteria, placement of optimizations, and early optimizations. It also covers specific optimization techniques such as constant-folding, scalar replacement of aggregates, algebraic simplification, and value numbering.
E N D
Introduction to Program Optimizations Chapter 11 Mooly Sagiv
Outline • Undecidability of optimizations • Optimization Criteria • Placement of optimizations • Early Optimizations • Constant-Folding • Scalar replacement of aggregates • Algebraic simplification • Value numbering
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
Example: 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:
Optimization Criteria • Execution-time • Code space • Data space
Profiling • Instrument the code to measure certain “resource” consumption (profile information) • Optimize the next compile session with profile information
More Dimensions for Data-Flow Analysis • Flow-sensitive • Context-sensitive • May vs. Must
“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)
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
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
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
Order of Optimizations and Repetitions • Transformations are order-dependent • No “Optimal order” • May need to repeat certain transformations
Scalar replacement of array references Data-cache optimizations A HIR Procedure integration … B HIR|MIR Global value numbering … In-line expansion … Interprocedural register allocation … C MIR|LIR D LIR E link-time Textbook Order constant-folding simplifications
Early High Level Optimizations (B) HIR|MIR 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
MIR|LIR(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
LIR(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
Link-time optimizations(E) Interprocedural register allocation Aggregation global references Interprcudural I-cache optimizations
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 Inline expansion Leaf-routine optimizations Instruction 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 Interprocedural I-cache optimization