Introduction to Optimization Chapter 11 Introduction Optimizations generally

  • Slides: 20
Download presentation
Introduction to Optimization (Chapter 11)

Introduction to Optimization (Chapter 11)

Introduction • Optimizations generally improve performance, sometimes substantially, although it is entirely possible that

Introduction • Optimizations generally improve performance, sometimes substantially, although it is entirely possible that they may decrease it or make no difference for some possible inputs to a given problem. • By applying optimization, we are improving the code, but never at the expense of making it incorrect.

 • Ex. We can prove by data flow analysis that an operation such

• Ex. We can prove by data flow analysis that an operation such as x=y/z in a while loop always produces the same value during any particular execution of the procedure containing it, then it would generally be desirable to move it out of the loop, but if we cannot guarantee that the operation never produces a divide-by-zero exception, then we must not move it, unless we can also prove that the loop is always executed at least once.

 • Effect of an optimization may have on the performance of a program,

• Effect of an optimization may have on the performance of a program, it is also undecidable whether an optimization is applicable to a particular procedure. • There are two fundamental criteria that decides which optimizations should be applied to a procedure, namely speed and space • Space or Time which matters more depends on the characteristics of the system on which the resulting program is to be run.

 • If the system has small main memory and / or a small

• If the system has small main memory and / or a small cache, minimizing code space may be very important. • In most cases, maximizing speed is much more important than minimizing space. • Optimizations that apply to loops, global register allocation and instruction scheduling are almost always essential to achieving high performance.

 • Which optimizations are most important for a particular program varies according to

• Which optimizations are most important for a particular program varies according to the structure of the program. • Ex. Programs written in object oriented languages, encourage the use of many small procedures, procedure integration and leafroutine optimization may be essential. • For highly recursive programs, tail-call optimizations, which replaces some calls by jumps and simplifies the procedure entry and exit sequences, may be of great value.

 • For self recursive routines, a special case of tail-call optimization called tail-recursion

• For self recursive routines, a special case of tail-call optimization called tail-recursion elimination gives better results. • Some efforts at optimization may waste more compilation time than they are worth in execution-time improvement. These are costly so rarely used.

Series of optimizations • • Early optimizations (Chapter 12) Redundancy elimination (Chapter 13) Loop

Series of optimizations • • Early optimizations (Chapter 12) Redundancy elimination (Chapter 13) Loop optimizations (chapter 14) Procedure Optimizations ( Chapter 15) Register Allocation (Chapter 16) Instruction Scheduling (Chapter 17) Control flow and low level Optimizations (chapter 18)

Flow sensitivity and May Vs. Must information May versus must classification distinguishes what may

Flow sensitivity and May Vs. Must information May versus must classification distinguishes what may occur on some path through a flow graph from what must occur on all paths through it. Ex. If a procedure begins with an assignment to variable a followed by an if whose left branch assigns a value to b and right to c, then the assignment to a is must information and the assignments to b and c are may information.

Flow sensitive Vs Flow insensitive • Any of the optimizations for which we must

Flow sensitive Vs Flow insensitive • Any of the optimizations for which we must do data-flow analysis to determine their applicability are flow sensitive, while those for which we need not do data flow analysis are flow insensitive. • It determines the computation complexity of the problem under consideration.

Importance of individual optimizations • 4 categories of all optimizations (Chap 12 to Chap

Importance of individual optimizations • 4 categories of all optimizations (Chap 12 to Chap 18) – Group III – Group IV

Group I: – – – Constant folding Algebraic simplifications and reassociation Global value numbering

Group I: – – – Constant folding Algebraic simplifications and reassociation Global value numbering Sparse conditional constant propagation Partial redundancy elimination Strength reduction Removal of induction variables and linear function test replacement Dead code elimination Unreachable code elimination (a control flow optimization) Graph colouring register allocation Software pipelining Branch and basic block (list) scheduling

Group II • • • Local and global copy propagation Leaf-routine optimization Machine idioms

Group II • • • Local and global copy propagation Leaf-routine optimization Machine idioms and instruction combining Branch optimizations and loop inversion Unnecessary bounds – checking elimination Branch prediction

Group III • Procedure integration • Tail-call optimization and tail-recursion elimination • Inline expansion

Group III • Procedure integration • Tail-call optimization and tail-recursion elimination • Inline expansion • Shrink wrapping • Scalar replacement of aggregates • Additional control flow optimizations

Group IV • Code hoisting • Tail merging

Group IV • Code hoisting • Tail merging

 • The above fig shows the possible order for performing the optimizations. •

• The above fig shows the possible order for performing the optimizations. • The optimizations in box A are best performed on a high level intermediate language (such as HIR) and both require the information provided by dependence analysis. • The optimization in box B are best performed on a high or medium level intermediate language (HIR or MIR)and early in the optimization process.

 • The optimizations in box C are best done on a medium or

• The optimizations in box C are best done on a medium or low-level intermediate language (MIR or LIR) and after the optimization in box B. • Box C is also categorize the functionality in C 1, C 2, C 3 and C 4 as shown in the diagram. • The optimizations in box D are best done late in the optimization process and on a low -level intermediate code(LIR) or on assembly language.

 • The optimizations in box E are done on the relocatable load module

• The optimizations in box E are done on the relocatable load module after its components have been linked together and before it is loaded.