# CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral

## Presentation on theme: "CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral"— Presentation transcript:

CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral http://www.cs.ualberta.ca/~amaral/courses/680

CMPUT 680 - Compiler Design and Optimization2 Peephole “Optimization” Goals: - improve performance - reduce memory footprint - reduce code size Method: 1. Exam short sequences of target instructions 2. Replacing the sequence by a more efficient one. redundant-instruction elimination flow-of-control optimizations algebraic simplifications use of machine idioms

CMPUT 680 - Compiler Design and Optimization3 Redundant Load and Stores (1) LOAD R0, a (2) STORE a, R0 (1) LOAD R0, a If there are no changes to a between (1) and (2), then the store (2) is redundant.

CMPUT 680 - Compiler Design and Optimization4 Example debug = 0... if(debug) { print debugging information } debug = 0... if debug = 1 goto L1 goto L2 L1: print debugging information L2: Source Code: Intermediate Code: (Aho-Sethi-Ullman,pp. 555)

CMPUT 680 - Compiler Design and Optimization5 Eliminate Jump after Jump debug = 0... if debug = 1 goto L1 goto L2 L1: print debugging information L2: Before: (Aho-Sethi-Ullman,pp. 555) debug = 0... if debug  1 goto L2 print debugging information L2: After:

CMPUT 680 - Compiler Design and Optimization6 Constant Propagation Before: (Aho-Sethi-Ullman,pp. 555) After: debug = 0... if debug  1 goto L2 print debugging information L2: debug = 0... if 0  1 goto L2 print debugging information L2:

CMPUT 680 - Compiler Design and Optimization7 Unreachable Code (dead code elimination) Before: (Aho-Sethi-Ullman,pp. 555) After: debug = 0... if debug  1 goto L2 print debugging information L2: debug = 0...

CMPUT 680 - 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 680 - 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 680 - 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 680 - 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 680 - 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.

Download ppt "CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral"

Similar presentations