CMPUT Compiler Design and Optimization8 Flow-of-control optimizations goto L1... L1: goto L2 goto L2... L1: goto L2 if a < b goto L1... L1: goto L2 if a < b goto L2... L1: goto L2 goto L1... L1: if a < b goto L2 L3: if a < b goto L2 goto L3... L3: (Aho-Sethi-Ullman,pp. 556)
CMPUT Compiler Design and Optimization9 Transformations on Basic Blocks zStructure-Preserving Transformations: ycommon subexpression elimination ydead code elimination yrenaming of temporary variables yinterchange of two independent adjacent statements
CMPUT Compiler Design and Optimization10 Examples of Transformations Common subexpression elimination: a := b + c b := a - d c := b + c d := a - d a := b + c b := a - d c := b + c d := b Dead code elimination: if x is never referenced after the statement x = y+z, the statement can be safely eliminated.
CMPUT Compiler Design and Optimization11 Examples of Transformations Interchange of statements: t1 := b + c t2 := x + y t1 := b + c Renaming temporary variables: if there is a statement t := b + c, we can change it to u := b + c and change all uses of t to u.
CMPUT Compiler Design and Optimization12 Examples of Transformations Algebraic transformations: x := x + 0 x := x * 1 x := y**2 z := 2*x x := y*y z := x + x Changes such as y**2 into y*y and 2*x into x+x are also known as strength reduction.