90 likes | 268 Views
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
E N D
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 • Miss opportunities for improvement rather than produce erroneous results* 2
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
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
def X:=3 def X:=1 use Y:=x
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
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
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
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