1 / 9

Lecture 27

Lecture 27. Optimization. Program Optimization. More than one way to compute the result Optimization produces equivalent program that is Smaller Faster Optimization has two components Program analysis Transformation Optimization is conservative

finley
Download Presentation

Lecture 27

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 27 Optimization

  2. Program Optimization • More than one way to compute the result • Optimization produces equivalent program that is • Smaller • Faster • Optimization has two components • Program analysis • Transformation • Optimization is conservative • Miss opportunities for improvement rather than produce erroneous results* 2

  3. Program Analysis • Variety of techniques for deducing properties of programs • E.g. x always has value of 2, y is never used • Some properties can be found by inspection • E.g. variaables that are defined, but never used • Most Properties require flow analysis • Assign rules to statements • Propagate sets of facts through programs • As fact passes statement, rule may modify it • Assumption: all paths through the program are possible • Do not know outcome of conditional branches • Avoid Turing halting problem

  4. Flow Analysis(Reaching Definitions) • Definition (assignment) reaches use if: • The definition assigns variable a variable x • The use references variable x • There exists a path def => use without a definition of x • Number all definition in the program • For each basic block b, compute the following sets: • Def(b)={d|d is a definition in block b) • Kill(b)={d|x is a definition in block b and x assigns the same variable as d} • Write the following equations: • In(b)=U out(p) where p is the predecessor of b • Out(b)=in(b)-kill(b) U def(b) • Can solve the equations in wide variety ways

  5. def X:=3 def X:=1 use Y:=x

  6. Reaching Definition Example 1 2 X:=1 Y:=2 Def={1,2} Kill={1,2,3,4} Def={} Kill={} X<y Def={4} Kill={2,4} Rd=1,4} Def={3} Kill={1,3} Y:=y+x X:=x+3 3 4 Def={} Kill={} Rd={1,2,3,4} X==200

  7. Other Analysis • Constant propagation • Is the value of a variable at a point in program always the same • Live variable analysis • Is the value in a variable used subsequently in the program? • Dead Code detection • Is the piece of code ever executed • Available expressions • Is a subexpression already evaluated along all parts to another instance of subexpression

  8. Transformation • After analyzing a program, change it to run better • Many different transformations have been published • Value of each depends on characteristics of source program • Also may depend on machine characteristics • Common subexpression elimination • Copy propagation • Loop invariant code motion • Induction variable elimination • Strength reduction

  9. Transformation Example • Loop invariant code motion • Move code that always produces same value out of loop • For i:= 1 to 100 do a[i]= a[0]*55+8-I; • Expression a[0]*55+8 is invariant • To recognize loop invariant code • Compute reaching definitions • If all definitions reaching an expression are from out of the loop, then expression is invariant • Move loop invariant code before the loop • T:=a[0]*55+8 • For i:=1 to 100 do a[i] :=t-I; • May have to iterate process as code moves out of loop

More Related